private void setupRandPartA() {
    if (i2 <= m_last) {
      chPrev = ch2;
      ch2 = m_ll8[m_tPos];
      m_tPos = m_tt[m_tPos];
      if (m_rNToGo == 0) {
        m_rNToGo = RAND_NUMS[m_rTPos];
        m_rTPos++;
        if (m_rTPos == 512) {
          m_rTPos = 0;
        }
      }
      m_rNToGo--;
      ch2 ^= ((m_rNToGo == 1) ? 1 : 0);
      i2++;

      m_currentChar = ch2;
      m_currentState = RAND_PART_B_STATE;
      m_crc.updateCRC(ch2);
    } else {
      endBlock();
      initBlock();
      setupBlock();
    }
  }
Exemplo n.º 2
0
  /** Calculates a crc checksum-sequence over an array. */
  public static byte BlockSequenceCRCByte(byte base[], int offset, int length, int sequence) {
    if (sequence < 0) Sys.Error("sequence < 0, this shouldn't happen\n");

    // p_ndx = (sequence % (sizeof(chktbl) - 4));
    int p_ndx = (sequence % (1024 - 4));

    // memcpy(chkb, base, length);
    length = Math.min(60, length);
    System.arraycopy(base, offset, chkb, 0, length);

    chkb[length] = chktbl[p_ndx + 0];
    chkb[length + 1] = chktbl[p_ndx + 1];
    chkb[length + 2] = chktbl[p_ndx + 2];
    chkb[length + 3] = chktbl[p_ndx + 3];

    length += 4;

    // unsigned short
    int crc = CRC.CRC_Block(chkb, length);

    int x = 0;
    for (int n = 0; n < length; n++) x += chkb[n] & 0xFF;

    crc ^= x;

    return (byte) (crc & 0xFF);
  }
Exemplo n.º 3
0
  /**
   * @param defMsg
   * @param msg
   * @param in
   * @throws IOException
   */
  protected void readDataMessage(
      final DefinitionMessage defMsg, final DataMessage msg, InputStream in) throws IOException {
    crc.update(msg.getHeader());

    final ByteOrder bo = defMsg.getByteOrder();
    for (final FieldDefinition def : defMsg.getFields()) {
      final byte[] value = new byte[def.getSize()];
      readBytes += in.read(value);
      crc.update(value);

      final Field<?> field = FieldFactory.build(def, value, bo);
      msg.getFields().add(field);

      // Update timestamp.
      if (field.isTimestamp() && field.toNumber() != null) {
        timestampOffset.updateTimestamp(field.toNumber().longValue());
      }
    }
  }
Exemplo n.º 4
0
  /**
   * @param fitFile
   * @return
   */
  public static boolean isValid(final Path fitFile) {
    final InputStream in;
    try {
      in = Files.newInputStream(fitFile);
    } catch (final IOException | SecurityException e) {
      logger.info("Could not open fit file.", e);
      return false;
    }

    final Header header = new Header();
    try {
      header.readHeader(in);
    } catch (final IOException e) {
      logger.info("Error while reading fit file header.", e);
      return false;
    }

    if (!header.isValid()) {
      logger.info("Fit file header is not valid.");
      return false;
    }

    final byte[] data = new byte[header.getDataSize() + 2];
    try {
      in.read(data);
    } catch (final IOException e) {
      logger.info("Error while reading fit records and CRC.", e);
      return false;
    }

    final CRC crc = new CRC();
    crc.update(header.getHeader(), header.getHeaderSize());
    crc.update(data);

    if (crc.getValue() != 0x0000) {
      logger.info("Fit file CRC error.");
      return false;
    }

    return true;
  }
  private void endBlock() {
    m_computedBlockCRC = m_crc.getFinalCRC();
    /*
     * A bad CRC is considered a fatal error.
     */
    if (m_storedBlockCRC != m_computedBlockCRC) {
      crcError();
    }

    m_computedCombinedCRC = (m_computedCombinedCRC << 1) | (m_computedCombinedCRC >>> 31);
    m_computedCombinedCRC ^= m_computedBlockCRC;
  }
 private void setupRandPartC() {
   if (j2 < z) {
     m_currentChar = ch2;
     m_crc.updateCRC(ch2);
     j2++;
   } else {
     m_currentState = RAND_PART_A_STATE;
     i2++;
     count = 0;
     setupRandPartA();
   }
 }
Exemplo n.º 7
0
  private void initBlock() {
    //        blockNo++;
    mCrc.initialiseCRC();
    last = -1;
    //        ch = 0;

    for (int i = 0; i < 256; i++) {
      inUse[i] = false;
    }

    /* 20 is just a paranoia constant */
    allowableBlockSize = baseBlockSize * blockSize100k - 20;
  }
Exemplo n.º 8
0
  private void endBlock() throws IOException {
    blockCRC = crc.getFinalCRC();
    combinedCRC = (combinedCRC << 1) | (combinedCRC >>> 31);
    combinedCRC ^= blockCRC;

    /*
     * sort the block and establish posn of original string
     */
    doReversibleTransformation();

    /*
     * A 6-byte block header, the value chosen arbitrarily
     * as 0x314159265359 :-).  A 32 bit value does not really
     * give a strong enough guarantee that the value will not
     * appear by chance in the compressed datastream.  Worst-case
     * probability of this event, for a 900k block, is about
     * 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48 bits.
     * For a compressed file of size 100Gb -- about 100000 blocks --
     * only a 48-bit marker will do.  NB: normal compression/
     * decompression do *not* rely on these statistical properties.
     * They are only important when trying to recover blocks from
     * damaged files.
     */
    bsPutUChar(0x31);
    bsPutUChar(0x41);
    bsPutUChar(0x59);
    bsPutUChar(0x26);
    bsPutUChar(0x53);
    bsPutUChar(0x59);

    /*
     * Now the block's CRC, so it is in a known place.
     */
    bsPutint(blockCRC);

    /*
     * Now a single bit indicating randomisation.
     */
    if (blockRandomised) {
      bsW(1, 1);
    } else {
      bsW(1, 0);
    }

    /*
     * Finally, block's contents proper.
     */
    moveToFrontCodeAndSend();
  }
Exemplo n.º 9
0
  /**
   * @param defMsg
   * @param in
   * @throws IOException
   */
  protected void readDefinitionMessage(final DefinitionMessage defMsg, InputStream in)
      throws IOException {
    final byte[] params = new byte[5];
    readBytes += in.read(params);

    crc.update(defMsg.getHeader());
    crc.update(params);

    defMsg.setReserved(params[0]);
    defMsg.setArchitecture(params[1]);
    defMsg.setFitMessageNumber(
        ByteBuffer.wrap(params, 2, 2).order(defMsg.getByteOrder()).getShort());
    defMsg.setNumOfFields(params[4]);

    final List<FieldDefinition> fields = new ArrayList<>();
    for (int i = 0; i < defMsg.getNumOfFields(); i++) {
      final byte[] fieldDef = new byte[3];
      readBytes += in.read(fieldDef);
      crc.update(fieldDef);

      fields.add(new FieldDefinition(fieldDef[0], fieldDef[1], fieldDef[2]));
    }
    defMsg.setFields(fields);
  }
  private void setupNoRandPartA() {
    if (i2 <= m_last) {
      chPrev = ch2;
      ch2 = m_ll8[m_tPos];
      m_tPos = m_tt[m_tPos];
      i2++;

      m_currentChar = ch2;
      m_currentState = NO_RAND_PART_B_STATE;
      m_crc.updateCRC(ch2);
    } else {
      endBlock();
      initBlock();
      setupBlock();
    }
  }
Exemplo n.º 11
0
 private void writeRun() throws IOException {
   if (last < allowableBlockSize) {
     inUse[currentChar] = true;
     for (int i = 0; i < runLength; i++) {
       crc.updateCRC((char) currentChar);
     }
     switch (runLength) {
       case 1:
         last++;
         block[last + 1] = (char) currentChar;
         break;
       case 2:
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         break;
       case 3:
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         break;
       default:
         inUse[runLength - 4] = true;
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) currentChar;
         last++;
         block[last + 1] = (char) (runLength - 4);
         break;
     }
   } else {
     endBlock();
     initBlock();
     writeRun();
   }
 }
  private void initBlock() {
    final char magic1 = readUnsignedChar();
    final char magic2 = readUnsignedChar();
    final char magic3 = readUnsignedChar();
    final char magic4 = readUnsignedChar();
    final char magic5 = readUnsignedChar();
    final char magic6 = readUnsignedChar();
    if (magic1 == 0x17
        && magic2 == 0x72
        && magic3 == 0x45
        && magic4 == 0x38
        && magic5 == 0x50
        && magic6 == 0x90) {
      complete();
      return;
    }

    if (magic1 != 0x31
        || magic2 != 0x41
        || magic3 != 0x59
        || magic4 != 0x26
        || magic5 != 0x53
        || magic6 != 0x59) {
      badBlockHeader();
      m_streamEnd = true;
      return;
    }

    m_storedBlockCRC = readInt();

    if (bsR(1) == 1) {
      m_blockRandomised = true;
    } else {
      m_blockRandomised = false;
    }

    // currBlockNo++;
    getAndMoveToFrontDecode();

    m_crc.initialiseCRC();
    m_currentState = START_BLOCK_STATE;
  }
Exemplo n.º 13
0
 @Override
 public void write(int b) {
   CRC.UpdateByte(b);
 }
Exemplo n.º 14
0
 public void Init() {
   CRC.Init();
 }
Exemplo n.º 15
0
 public int GetDigest() {
   return CRC.GetDigest();
 }
Exemplo n.º 16
0
 @Override
 public void write(byte[] b) {
   CRC.Update(b);
 }
Exemplo n.º 17
0
 @Override
 public void write(byte[] b, int off, int len) {
   CRC.Update(b, off, len);
 }
Exemplo n.º 18
0
  /**
   * @param in
   * @return
   * @throws IOException
   */
  public boolean read(InputStream in) throws IOException {
    // Read header.
    final Header header = new Header();
    header.readHeader(in);

    if (!header.isValid()) {
      logger.error("Fit file header is not valid.");
      return false;
    }

    crc.reset();
    readBytes = 0;
    stop = false;
    for (int i = 0; i < header.getHeaderSize(); i++) {
      crc.update(header.getHeader()[i]);
    }

    dispatcher.onHeader(header);

    // Read data records.
    final DefinitionMessage[] definitions = new DefinitionMessage[MAX_LOCAL_MESSAGE_NUMS];
    final int dataSize = header.getDataSize();

    int data = -1;
    while (readBytes < dataSize && (data = in.read()) > -1) {
      // Record header byte was read.
      readBytes++;

      final int recordType = data & RECORD_HEADER_TYPE_MASK;

      if (recordType == DEFINITION_MESSAGE) {
        final DefinitionMessage defMsg = new DefinitionMessage((byte) data);
        readDefinitionMessage(defMsg, in);

        // Store new definition message.
        definitions[defMsg.getLocalMessageType()] = defMsg;

        dispatcher.onDefinitionMessage(defMsg);
      } else {
        final DataMessage msg;
        if (recordType == COMPRESSED_TIMESTAMP) {
          final int timeOffset = data & COMPRESSED_TIMESTAMP_MASK;
          timestampOffset.setOffset(timeOffset);

          msg = new DataMessage((byte) data, timestampOffset.getTimestamp());
        } else if (recordType == DATA_MESSAGE) {
          msg = new DataMessage((byte) data);
        } else {
          logger.error(
              "Unknown message header type. message header: 0x{}", Integer.toHexString(data));
          return false;
        }

        final DefinitionMessage defMsg = definitions[msg.getLocalMessageType()];
        if (defMsg == null) {
          logger.error(
              "Definition message does not exists. message header: {}, read: {} bytes",
              data,
              header.getHeaderSize() + readBytes);
          return false;
        }

        readDataMessage(defMsg, msg, in);

        dispatcher.onDataMessage(defMsg, msg);
      }

      if (stop) {
        logger.info("Stop reading. read: {} bytes", header.getHeaderSize() + readBytes);
        return false;
      }
    }

    if (readBytes != dataSize) {
      logger.error(
          "A mismatch between read message size and defined size of the header. read: {} bytes, defined: {} bytes",
          readBytes,
          dataSize);
      return false;
    }

    // read crc(2 bytes).
    for (int i = 0; i < 2; i++) {
      final int d = in.read();
      if (d < 0) {
        logger.error(
            "Unexpected EOF while reading crc. read: {} bytes",
            header.getHeaderSize() + readBytes + i + 1);
        return false;
      }
      crc.update((byte) d);
    }

    if (crc.getValue() != 0) {
      logger.error("Fit file CRC error.");
      return false;
    }

    // check EOF.
    if (in.read() > 0) {
      logger.error(
          "Unexpected data exists after crc. read: {} bytes",
          header.getHeaderSize() + readBytes + 2);
      return false;
    }

    return true;
  }
Exemplo n.º 19
0
  public static int LzmaBenchmark(int numIterations, int dictionarySize) throws Exception {
    if (numIterations <= 0) return 0;
    if (dictionarySize < (1 << 18)) {
      System.out.println("\nError: dictionary size for benchmark must be >= 18 (256 KB)");
      return 1;
    }
    System.out.print("\n	   Compressing				Decompressing\n\n");

    SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
    SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

    if (!encoder.SetDictionarySize(dictionarySize))
      throw new Exception("Incorrect dictionary size");

    int kBufferSize = dictionarySize + kAdditionalSize;
    int kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;

    ByteArrayOutputStream propStream = new ByteArrayOutputStream();
    encoder.WriteCoderProperties(propStream);
    byte[] propArray = propStream.toByteArray();
    decoder.SetDecoderProperties(propArray);

    CBenchRandomGenerator rg = new CBenchRandomGenerator();

    rg.Set(kBufferSize);
    rg.Generate();
    CRC crc = new CRC();
    crc.Init();
    crc.Update(rg.Buffer, 0, rg.BufferSize);

    CProgressInfo progressInfo = new CProgressInfo();
    progressInfo.ApprovedStart = dictionarySize;

    long totalBenchSize = 0;
    long totalEncodeTime = 0;
    long totalDecodeTime = 0;
    long totalCompressedSize = 0;

    MyInputStream inStream = new MyInputStream(rg.Buffer, rg.BufferSize);

    byte[] compressedBuffer = new byte[kCompressedBufferSize];
    MyOutputStream compressedStream = new MyOutputStream(compressedBuffer);
    CrcOutStream crcOutStream = new CrcOutStream();
    MyInputStream inputCompressedStream = null;
    int compressedSize = 0;
    for (int i = 0; i < numIterations; i++) {
      progressInfo.Init();
      inStream.reset();
      compressedStream.reset();
      encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
      long encodeTime = System.currentTimeMillis() - progressInfo.Time;

      if (i == 0) {
        compressedSize = compressedStream.size();
        inputCompressedStream = new MyInputStream(compressedBuffer, compressedSize);
      } else if (compressedSize != compressedStream.size()) throw (new Exception("Encoding error"));

      if (progressInfo.InSize == 0) throw (new Exception("Internal ERROR 1282"));

      long decodeTime = 0;
      for (int j = 0; j < 2; j++) {
        inputCompressedStream.reset();
        crcOutStream.Init();

        long outSize = kBufferSize;
        long startTime = System.currentTimeMillis();
        if (!decoder.Code(inputCompressedStream, crcOutStream, outSize))
          throw (new Exception("Decoding Error"));
        decodeTime = System.currentTimeMillis() - startTime;
        if (crcOutStream.GetDigest() != crc.GetDigest()) throw (new Exception("CRC Error"));
      }
      long benchSize = kBufferSize - progressInfo.InSize;
      PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
      System.out.print("	 ");
      PrintResults(dictionarySize, decodeTime, kBufferSize, true, compressedSize);
      System.out.println();

      totalBenchSize += benchSize;
      totalEncodeTime += encodeTime;
      totalDecodeTime += decodeTime;
      totalCompressedSize += compressedSize;
    }
    System.out.println("---------------------------------------------------");
    PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
    System.out.print("	 ");
    PrintResults(
        dictionarySize,
        totalDecodeTime,
        kBufferSize * (long) numIterations,
        true,
        totalCompressedSize);
    System.out.println("	Average");
    return 0;
  }