/* (non-Javadoc)
@see com.ibm.ws.sib.mfp.jmf.impl.JSchemaInterpreter#checkSchemata(byte[], int)
487488
public long[] checkSchemata(byte[] contents, int offset)
      throws JMFMessageCorruptionException {
    int numUnknown = 0;
    long[] unknownSchemata = null;
    try {
      //find out how many schemata there are
      int nSchemata = ArrayUtil.readShort(contents, offset);
      // Make sure the size looks valid before we allocate space and start reading
      if (nSchemata < 0 || offset+2 + nSchemata*8 > contents.length) {
        JMFMessageCorruptionException jmce =  new JMFMessageCorruptionException(
            "Bad schemata length: " + nSchemata + " at offset " + offset);
        FFDCFilter.processException(jmce, "com.ibm.ws.sib.mfp.jmf.impl.JSchemaInterpreterImpl.checkSchemata", "98", this,
            new Object[] { MfpConstants.DM_BUFFER, contents, Integer.valueOf(0), Integer.valueOf(contents.length) });
        throw jmce;
      }
      offset += 2;
      unknownSchemata = new long[nSchemata];
      for (int i = 0; i < nSchemata; i++) {
        //read the schema id
        long schemaId = ArrayUtil.readLong(contents, offset);
        offset += 8;
        //try to look up the schema
        JMFSchema schema = JMFRegistry.instance.retrieve(schemaId);
        //if we don't find the schema, record the id in the array to be
        //returned
        if(schema == null){
          unknownSchemata[numUnknown] = schemaId;
          numUnknown++;
        }
      }
    } catch (ArrayIndexOutOfBoundsException e) {
      FFDCFilter.processException(e, "com.ibm.ws.sib.mfp.jmf.impl.JSchemaInterpreterImpl.checkSchemata", "101", this,
          new Object[] { MfpConstants.DM_BUFFER, contents, Integer.valueOf(0), Integer.valueOf(contents.length) });
      // If we get an ArrayIndexOutOfBounds error we have tried to read beyond the end
      // of the supplied contents buffer.  This means the data provided must be invalid.
      throw new JMFMessageCorruptionException("Attempt to read beyond end of data buffer", e);
    }
    //copy the unknown ids in to an array of the right size
    if(numUnknown > 0){
      long[] result = new long[numUnknown];
      System.arraycopy(unknownSchemata, 0, result, 0, numUnknown);
      return result;
    }
    else
    {
      return new long[0];
    }
  }
781143
public static <C extends Compound> String checksum(Sequence<C> sequence) {
		CRC64Checksum checksum = new CRC64Checksum();
		for (C compound : sequence) {
			checksum.update(compound.getShortName());
		}
		return checksum.toString();
	}
515448
private void recordCompatibilityInfo(JMFType access, JSchema accSchema, JMFType encoding,
      JSchema encSchema) throws JMFSchemaViolationException
  {
    if (access instanceof JSPrimitive)
      if (encoding instanceof JSPrimitive)
        recordCompatibilityInfo((JSPrimitive)access, accSchema, (JSPrimitive)encoding,
          encSchema);
      else if (encoding instanceof JSVariant)
        checkForDeletingVariant(access, (JSVariant)encoding, false, accSchema, encSchema);
      else {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) JmfTr.debug(this, tc, "Compatibility violation (type mismatch) for JSPrimitive");
        violation(access, encoding);
       }
    else if (access instanceof JSEnum)
      if (encoding instanceof JSEnum)
        recordCompatibilityInfo((JSEnum)access, accSchema, (JSEnum)encoding, encSchema);
      else if (encoding instanceof JSVariant)
        checkForDeletingVariant(access, (JSVariant)encoding, false, accSchema, encSchema);
      else {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) JmfTr.debug(this, tc, "Compatibility violation (type mismatch) for JSEnum");
        violation(access, encoding);
      }
    else if (access instanceof JSDynamic)
      if (encoding instanceof JSDynamic)
        recordOnePair((JSField)access, accSchema, (JSField)encoding, encSchema);
      else if (encoding instanceof JSVariant)
        checkForDeletingVariant(access, (JSVariant)encoding, false, accSchema, encSchema);
      else {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) JmfTr.debug(this, tc, "Compatibility violation (type mismatch) for JSDynamic");
        violation(access, encoding);
      }
    else if (access instanceof JSRepeated)
      if (encoding instanceof JSRepeated)
        recordCompatibilityInfo(((JSRepeated)access).getItemType(), accSchema,
          ((JSRepeated)encoding).getItemType(), encSchema);
      else if (encoding instanceof JSVariant)
        checkForDeletingVariant(access, (JSVariant)encoding, false, accSchema, encSchema);
      else {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) JmfTr.debug(this, tc, "Compatibility violation (type mismatch) for JSRepeated");
        violation(access, encoding);
      }
    else if (access instanceof JSTuple)
      if (encoding instanceof JSTuple)
        recordCompatibilityInfo((JSTuple)access, accSchema, (JSTuple)encoding, encSchema);
      else if (encoding instanceof JSVariant)
        checkForDeletingVariant(access, (JSVariant)encoding, false, accSchema, encSchema);
      else {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) JmfTr.debug(this, tc, "Compatibility violation (type mismatch) for JSTuple");
        violation(access, encoding);
      }
    else if (access instanceof JSVariant) {
      JSVariant accVar = (JSVariant)access;
      if (encoding instanceof JSVariant) {
        JSVariant encVar = (JSVariant)encoding;
        recordCompatibilityInfo(accVar, accSchema, encVar, encSchema);
      } else
        checkForDeletingVariant(encoding, accVar, true, accSchema, encSchema);
    }
  }
110903
final String debugSchema(JMFSchema schema) {
    if (schema != null)
      return schema.getName() + "(" + debugId(schema.getID()) + ")";
    else
      return "<null>";
  }
156221
private void understandSchema(String schema) throws JSONException {

    JSONObject j1 = new JSONObject(schema);
    JSONArray fields = j1.getJSONArray(FIELDS);

    String fieldName;
    String fieldTypeValue;
    Object recName;

    for (int k = 0; k < fields.length(); k++) {
      if (fields.get(k) == null) {
        continue;
      }
      JSONObject allEvents = new JSONObject(fields.get(k).toString());
      Object name = allEvents.get(NAME);
      if (name != null) {
        if (name.toString().equalsIgnoreCase(EVENT)) {
          JSONArray allTypeDetails = allEvents.getJSONArray(TYPE);
          for (int i = 0; i < allTypeDetails.length(); i++) {
            JSONObject actual = (JSONObject) allTypeDetails.get(i);
            JSONArray types = actual.getJSONArray(FIELDS);
            Map<String, String> typeDetails = new HashMap<String, String>();
            for (int j = 0; j < types.length(); j++) {
              if (types.getJSONObject(j) == null ) {
                continue;
              }
              fieldName = types.getJSONObject(j).getString(NAME);
              fieldTypeValue = types.getJSONObject(j).getString(TYPE);
              if ((fieldName != null) && (fieldTypeValue != null)) {
                typeDetails.put(fieldName, fieldTypeValue);
              }
            }

            recName = actual.get(NAME);
            if (recName != null) {
              /* the next statement may throw an IllegalArgumentException if
               * it finds a new string that's not part of the Hadoop2RecordType enum
               * that way we know what types of events we are parsing
               */
              fieldTypes.put(Hadoop2RecordType.valueOf(recName.toString()), typeDetails);
            }
          }
        }
      }
    }
  }
1069340
static void sendSchemas(CommsConnection conn, JMFSchema[] schemas)
      throws SIConnectionLostException, SIConnectionUnavailableException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "sendSchemas", conn);

    // Check if the recepient has all the schemas needed to decode the message.
    SchemaSet ids = (SchemaSet)conn.getSchemaSet();
    if (ids == null) {
      if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) SibTr.debug(tc, "getSchemaSet() returned null for CommsConnection: " + conn);
      throw new IllegalStateException("CommsConnection returned null SchemaSet");
    }

    JMFSchema[] missing = new JMFSchema[schemas.length];
    int count = 0;
    for (int i = 0; i < schemas.length; i++) {
      if (!ids.contains(schemas[i].getLongID()))
        missing[count++] = schemas[i];
    }

    // Send any missing schemas
    if (count > 0) {
      conn.sendMFPSchema(makeSchemaDefinitionList(missing, count));
      // Update the list
      for (int i = 0; i < count; i++)
        ids.add(missing[i].getLongID());
    }

    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "sendSchemas");
  }

  //take an encoded list of schema IDs and return the corresponding encoded list of schemas
  static byte[] getEncodedSchemataByEncodedIDs(byte[] encodedSchemaIDs) {
    //double check that there are ids to decode
    if(encodedSchemaIDs != null && encodedSchemaIDs.length >= 8){
      //work out how many there are
      int numSchemas = encodedSchemaIDs.length / 8;
      long[] schemaIDs = new long[numSchemas];
      int offset = 0;
      //decode each one
      for(int i=0; i<numSchemas; i++){
        schemaIDs[i] = ArrayUtil.readLong(encodedSchemaIDs, offset);
        offset += 8;
      }
      //get the encoded schemas
      return getEncodedSchemataBySchemaIDs(schemaIDs);
    }
    else{
      return new byte[0];
    }
  }

  //get an encoded list of schemas from a list of schema IDs
  static byte[] getEncodedSchemataBySchemaIDs(long[] schemaIDs) {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "getEncodedSchemataBySchemaIDs", schemaIDs);

    JMFSchema[] temp = new JMFSchema[schemaIDs.length];
    int j = 0;

    //get each one from the registry
    for (int i = 0; i < schemaIDs.length; i++) {
      JMFSchema schema = JSRegistry.instance.retrieve(schemaIDs[i]);
      if (schema == null) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) SibTr.debug(tc, "Unable to retrieve message schema "+schemaIDs[i]);
        MessageDecodeFailedException e = new MessageDecodeFailedException("No schema registered for schema id "+schemaIDs[i]);
        FFDCFilter.processException(e, "com.ibm.ws.sib.mfp.impl.SchemaManager.getEncodedSchemasBySchemaIDs", "281");
      }
      else{
        temp[j++] = schema;
      }
    }

    JMFSchema[] found = new JMFSchema[j];
    System.arraycopy(temp,0,found,0,j);

    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "getEncodedSchemataBySchemaIDs");

    //encode the list and return it
    return makeSchemaDefinitionList(found, j);
  }

  /*
   * Helper methods
   */

  // Create the encoded list of schema ids
  private static byte[] makeSchemaIdList(JMFSchema[] schemas) {
    Coder coder = new Coder(ArrayUtil.LONG_SIZE * schemas.length, schemas.length);
    for (int i = 0; i < coder.count; i++) {
      ArrayUtil.writeLong(coder.buffer, coder.offset, schemas[i].getID());
      coder.offset += ArrayUtil.LONG_SIZE;
    }

    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.debug(tc, "Encoding " + coder.count + " schema ids for handshake");
    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.bytes(tc, coder.buffer, Coder.HDR_LENGTH);
    return coder.buffer;
  }

  // Create an initial Set of incoming schema ids
  private static SchemaSet makeSchemaIdSet(Coder coder) {
    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.debug(tc, "Decoding " + coder.count + "schema ids from handshake");
    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.bytes(tc, coder.buffer, Coder.HDR_LENGTH);

    SchemaSet result = new SchemaSet();
    for (int i = 0; i < coder.count; i++) {
      long id = ArrayUtil.readLong(coder.buffer, coder.offset);
      coder.offset += ArrayUtil.LONG_SIZE;
      result.add(Long.valueOf(id));
    }
    return result;
  }

  // Create the encoded list of schema definitions
  private static byte[] makeSchemaDefinitionList(JMFSchema[] schemas, int count) {
    // Calculate the buffer size needed to encode all the schema defintions
    int length = 0;
    for (int i = 0; i < count; i++)
      length += ArrayUtil.INT_SIZE + schemas[i].toByteArray().length;

    // And encode them
    Coder coder = new Coder(length, count);
    for (int i = 0; i < coder.count; i++) {
      byte[] b = schemas[i].toByteArray();
      if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) SibTr.debug(tc, "   Encoded Schema "+ debugSchema(schemas[i]) + " for transmission");
      ArrayUtil.writeInt(coder.buffer, coder.offset, b.length);
      coder.offset += ArrayUtil.INT_SIZE;
      System.arraycopy(b, 0, coder.buffer, coder.offset, b.length);
      coder.offset += b.length;
    }

    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.debug(tc, "Encoding " + coder.count + "new schema definitions");
    return coder.buffer;
  }
1112201
public Map<ChecksumType, String> calculateJavaScriptHashes(byte[] byteArray) throws WssHashException {
        Map<ChecksumType, String> checksums = new EnumMap<>(ChecksumType.class);
        try {
            String fileContent = IOUtils.toString(byteArray, UTF_8);
            ParseResult parseResult = new JavaScriptParser().parse(fileContent);
            if (parseResult != null) {
                // no comments
                String contentWithoutComments = parseResult.getContentWithoutComments();
                if (StringUtils.isNotBlank(contentWithoutComments)) {
                    HashCalculationResult noCommentsSha1 = calculateSuperHash(contentWithoutComments.getBytes());
                    if (noCommentsSha1 != null) {
                        checksums.put(ChecksumType.SHA1_NO_COMMENTS_SUPER_HASH, noCommentsSha1.getFullHash());
                    }
                }

                // no headers
                String headerlessContent = parseResult.getContentWithoutHeaderComments();
                if (StringUtils.isNotBlank(headerlessContent)) {
                    String headerlessChecksum = calculateByteArrayHash(headerlessContent.getBytes(), HashAlgorithm.SHA1);
                    checksums.put(ChecksumType.SHA1_NO_HEADER, headerlessChecksum);
                }
            }
        } catch (Exception e) {
            throw new WssHashException("Error calculating JavaScript hash: "+ e.getMessage());
        }
        return checksums;
    }
342939
private void checkDynamic(int accessor) throws JMFSchemaViolationException {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            JmfTr.entry(this, tc, "checkDynamic", new Object[] { Integer.valueOf(accessor) });

        JSField field = getFieldDef(accessor, false);

        if (field instanceof JSDynamic) {
            if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
                JmfTr.exit(this, tc, "checkDynamic");
            return;
        }

        JMFSchemaViolationException e = new JMFSchemaViolationException("Field type at accessor " + accessor + " is not Dynamic");
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            JmfTr.exit(this, tc, "checkDynamic", e);
        throw e;
    }
1231028
private static String calculateChecksum(String string) {
        byte[] bytes = string.getBytes(UTF_8);
        Checksum checksum = new CRC32();
        checksum.update(bytes, 0, bytes.length);
        return Long.toHexString(checksum.getValue()).toUpperCase();
    }
214544
private static void addSchemaDefinitions(SchemaSet ids, Coder coder) throws JMFException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
      SibTr.debug(tc, "Decoding " + coder.count + "new schema definitions");

    // Decode the new schemas and add them to the registry and the list of known
    // schemas for this connection.
    for (int i = 0; i < coder.count; i++) {
      int length = ArrayUtil.readInt(coder.buffer, coder.offset);
      coder.offset += ArrayUtil.INT_SIZE;
      JMFSchema schema = JMFRegistry.instance.createJMFSchema(coder.buffer, coder.offset, length);
      JMFRegistry.instance.register(schema);
      ids.add(schema.getLongID());
      if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) SibTr.debug(tc, "   Added Schema "+ debugSchema(schema));
      coder.offset += length;
    }
  }
461175
public void setChecksum(ChecksumAlgorithm algorithm,
                            String value)
        throws IOException, ServerException {
        String arguments = algorithm.toFtpCmdArgument() + " " + value;

        Command cmd = new Command("SCKS", arguments);
        try {
            controlChannel.execute(cmd);
        } catch (UnexpectedReplyCodeException urce) {
            throw ServerException.embedUnexpectedReplyCodeException(urce);
        } catch (FTPReplyParseException rpe) {
            throw ServerException.embedFTPReplyParseException(rpe);
        }
    }
420432
function checkSchemaHasReferences(schema) {
  if (schema.$ref) {
    return true;
  }

  return Object.values(schema).some((value) => {
    if (_.isArray(value)) {
      return value.some(checkSchemaHasReferences);
    } else if (_.isObject(value)) {
      return checkSchemaHasReferences(value);
    }

    return false;
  });
}
209085
public void setChecksum(final String checksum) {
        if (checksum == null && this.checksum == null) {
            return;
        } else if (checksum == null) {
            removeChild(this.checksum);
            this.checksum = null;
        } else if (this.checksum == null) {
            this.checksum = new KeyValueNode<String>(CommonConstants.CS_CHECKSUM_TITLE, checksum);
            appendChild(this.checksum, false);
        } else {
            this.checksum.setValue(checksum);
        }
    }
1360370
public static void validateAgainstSchema(final String sbeSchemaFilename, final String xsdFilename)
        throws Exception
    {
        final ParserOptions.Builder optionsBuilder = ParserOptions.builder()
            .xsdFilename(System.getProperty(VALIDATION_XSD))
            .xIncludeAware(Boolean.parseBoolean(System.getProperty(XINCLUDE_AWARE)))
            .stopOnError(Boolean.parseBoolean(System.getProperty(VALIDATION_STOP_ON_ERROR)))
            .warningsFatal(Boolean.parseBoolean(System.getProperty(VALIDATION_WARNINGS_FATAL)))
            .suppressOutput(Boolean.parseBoolean(System.getProperty(VALIDATION_SUPPRESS_OUTPUT)));

        final Path path = Paths.get(sbeSchemaFilename);
        try (InputStream in = new BufferedInputStream(Files.newInputStream(path)))
        {
            final InputSource inputSource = new InputSource(in);
            if (path.toAbsolutePath().getParent() != null)
            {
                inputSource.setSystemId(path.toUri().toString());
            }

            XmlSchemaParser.validate(xsdFilename, inputSource, optionsBuilder.build());
        }
    }
103570
function checkSum(buffer, offset, length) {
            if (buffer instanceof ArrayBuffer) {
                return checkSumArrayBuffer(buffer, offset, length);
            }
            else if (buffer instanceof Array) {
                return checkSumArray(buffer, offset, length);
            }

            throw new Error('not support checksum buffer type');
        }
773325
@Override
    public XMLValidationSchema createSchema(InputStream in, String encoding,
    		String publicId, String systemId)
        throws XMLStreamException
    {
        ReaderConfig rcfg = createPrivateReaderConfig();
        return doCreateSchema(rcfg, StreamBootstrapper.getInstance
        		(publicId, SystemId.construct(systemId), in), publicId, systemId, null);
    }
225528
function validateChecksum(requester, params) {
        params = params || {
            filePath: installerPath,
            expectedChecksum: _updateParams.checksum
        };

        var hash = crypto.createHash('sha256'),
            currentRequester = requester || "";

        if (fs.existsSync(params.filePath)) {
            var stream = fs.createReadStream(params.filePath);

            stream.on('data', function (data) {
                hash.update(data);
            });

            stream.on('end', function () {
                var calculatedChecksum = hash.digest('hex'),
                    isValidChecksum = (params.expectedChecksum === calculatedChecksum),
                    status;

                if (isValidChecksum) {
                    if (process.platform === "darwin") {
                        status = {
                            valid: true,
                            installerPath: installerPath,
                            logFilePath: logFilePath,
                            installStatusFilePath: installStatusFilePath
                        };
                    } else if (process.platform === "win32") {
                        status = {
                            valid: true,
                            installerPath: quoteAndConvert(installerPath, true),
                            logFilePath: quoteAndConvert(logFilePath, true),
                            installStatusFilePath: installStatusFilePath
                        };
                    }
                } else {
                    status = {
                        valid: false,
                        err: nodeErrorMessages.CHECKSUM_DID_NOT_MATCH
                    };
                }
                postMessageToBrackets(MessageIds.NOTIFY_VALIDATION_STATUS, currentRequester, status);
            });
        } else {
            var status = {
                valid: false,
                err: nodeErrorMessages.INSTALLER_NOT_FOUND
            };
            postMessageToBrackets(MessageIds.NOTIFY_VALIDATION_STATUS, currentRequester, status);
        }
    }