com.igormaznitsa.jbbp.model.JBBPFieldArrayByte Java Examples

The following examples show how to use com.igormaznitsa.jbbp.model.JBBPFieldArrayByte. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WAVParsingTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
private static void assertWavChunks(final JBBPFieldStruct parsedWav, final String... chunks) {
  assertEquals(0x46464952, parsedWav.findFieldForNameAndType("ChunkID", JBBPFieldInt.class).getAsInt());
  assertEquals(0x45564157, parsedWav.findFieldForNameAndType("Format", JBBPFieldInt.class).getAsInt());

  int calculatedSize = 4;

  int index = 0;

  assertEquals(chunks.length, parsedWav.findFieldForNameAndType("SubChunks", JBBPFieldArrayStruct.class).size(), "Number of parsed subchunks must be [" + chunks.length + ']');

  for (final JBBPFieldStruct subchunk : parsedWav.findFieldForNameAndType("SubChunks", JBBPFieldArrayStruct.class)) {
    final String strChunkId = chunks[index++];
    assertEquals(4, strChunkId.length(), "WAV subchunk must have 4 char length [" + strChunkId + ']');
    assertEquals(strChunkId, wavInt2Str(subchunk.findFieldForNameAndType("SubChunkID", JBBPFieldInt.class).getAsInt()));
    final int subChunkSize = subchunk.findFieldForNameAndType("SubChunkSize", JBBPFieldInt.class).getAsInt();
    assertEquals(subChunkSize, subchunk.findFieldForNameAndType("data", JBBPFieldArrayByte.class).size());
    calculatedSize += subChunkSize + 8 + (subChunkSize & 1);
  }

  assertEquals(calculatedSize, parsedWav.findFieldForNameAndType("ChunkSize", JBBPFieldInt.class).getAsInt());
}
 
Example #2
Source File: PNGParsingTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
private static void assertChunk(final String name, final int length, final JBBPFieldStruct chunk) {
  final int chunkName = (name.charAt(0) << 24) | (name.charAt(1) << 16) | (name.charAt(2) << 8) | name.charAt(3);

  assertEquals(chunkName, chunk.findFieldForNameAndType("type", JBBPFieldInt.class).getAsInt(), "Chunk must be " + name);
  assertEquals(length, chunk.findFieldForNameAndType("length", JBBPFieldInt.class).getAsInt(), "Chunk length must be " + length);

  final PureJavaCrc32 crc32 = new PureJavaCrc32();
  crc32.update(name.charAt(0));
  crc32.update(name.charAt(1));
  crc32.update(name.charAt(2));
  crc32.update(name.charAt(3));

  if (length != 0) {
    final byte[] array = chunk.findFieldForType(JBBPFieldArrayByte.class).getArray();
    assertEquals(length, array.length, "Data array " + name + " must be " + length);
    for (final byte b : array) {
      crc32.update(b & 0xFF);
    }
  }

  final int crc = (int) crc32.getValue();
  assertEquals(crc, chunk.findLastFieldForType(JBBPFieldInt.class).getAsInt(), "CRC32 for " + name + " must be " + crc);

}
 
Example #3
Source File: TGAParsingTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
private void assertTgaFile(final JBBPFieldStruct parsedTga, final String imageId, final int width, final int height, final int pixelDepth, final int colorTableItems, final int imageDataSize) {
  final JBBPFieldArrayByte imageIdArray = parsedTga.findFieldForNameAndType("ImageID", JBBPFieldArrayByte.class);
  if (imageId == null || imageId.length() == 0) {
    assertEquals(0, imageIdArray.size());
  } else {
    assertEquals(imageId.length(), imageIdArray.size());
    for (int i = 0; i < imageId.length(); i++) {
      assertEquals(imageId.charAt(i) & 0xFF, imageIdArray.getArray()[i] & 0xFF);
    }
  }

  assertEquals(width, parsedTga.findFieldForPathAndType("header.Width", JBBPFieldUShort.class).getAsInt());
  assertEquals(height, parsedTga.findFieldForPathAndType("header.Height", JBBPFieldUShort.class).getAsInt());
  assertEquals(pixelDepth, parsedTga.findFieldForPathAndType("header.PixelDepth", JBBPFieldUByte.class).getAsInt());
  assertEquals(colorTableItems, parsedTga.findFieldForNameAndType("ColorMap", JBBPFieldArrayStruct.class).size());
  assertEquals(imageDataSize, parsedTga.findFieldForNameAndType("ImageData", JBBPFieldArrayByte.class).size());
}
 
Example #4
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_NegativeExpressonResult_ExpressionWithNegativResult_FlagOn() throws Exception {
  final JBBPBitInputStream stream = new JBBPBitInputStream(new ByteArrayInputStream(new byte[] {4, 1, 2, 3}));
  final JBBPParser parser = JBBPParser.prepare("byte len; byte [len-8] arr;", JBBPParser.FLAG_NEGATIVE_EXPRESSION_RESULT_AS_ZERO);
  final JBBPFieldStruct result = parser.parse(stream);
  assertEquals(4, result.findFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0, result.findFieldForType(JBBPFieldArrayByte.class).getArray().length);
}
 
Example #5
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_NegativeExpressonResult_OneFieldAsExpression_FlagOn() throws Exception {
  final JBBPBitInputStream stream = new JBBPBitInputStream(new ByteArrayInputStream(new byte[] {(byte) 0xEF, 1, 2, 3}));
  final JBBPParser parser = JBBPParser.prepare("byte len; byte [len] arr;", JBBPParser.FLAG_NEGATIVE_EXPRESSION_RESULT_AS_ZERO);
  final JBBPFieldStruct result = parser.parse(stream);
  assertEquals((byte) 0xEF, result.findFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0, result.findFieldForType(JBBPFieldArrayByte.class).getArray().length);
}
 
Example #6
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseArrayWithZeroLengthForResetCounter() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("byte; byte [$$] a; reset$$; byte[$$] b; byte [2] c;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {1, 2, 3, 4});
  final JBBPFieldArrayByte a = parsed.findFieldForNameAndType("a", JBBPFieldArrayByte.class);
  final JBBPFieldArrayByte b = parsed.findFieldForNameAndType("b", JBBPFieldArrayByte.class);
  final JBBPFieldArrayByte c = parsed.findFieldForNameAndType("c", JBBPFieldArrayByte.class);
  assertArrayEquals(new byte[] {2}, a.getArray());
  assertArrayEquals(new byte[0], b.getArray());
  assertArrayEquals(new byte[] {3, 4}, c.getArray());
}
 
Example #7
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseScopeOfVisibilityOfFieldInsideStructure() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte a; b { byte a; byte [a] d; } byte [a] aa;").parse(new byte[] {1, 2, 3, 4, 5, 6});
  assertEquals(1, parsed.findFieldForPathAndType("a", JBBPFieldByte.class).getAsInt());
  assertEquals(2, parsed.findFieldForPathAndType("b.a", JBBPFieldByte.class).getAsInt());
  assertArrayEquals(new byte[] {3, 4}, parsed.findFieldForPathAndType("b.d", JBBPFieldArrayByte.class).getArray());
  assertArrayEquals(new byte[] {5}, parsed.findFieldForPathAndType("aa", JBBPFieldArrayByte.class).getArray());
}
 
Example #8
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseScopeOfVisibilityOfFieldIfTheSameInStructBefore() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte a; b { byte a;} byte [a] aa;").parse(new byte[] {1, 2, 3});
  assertEquals(1, parsed.findFieldForPathAndType("a", JBBPFieldByte.class).getAsInt());
  assertEquals(2, parsed.findFieldForPathAndType("b.a", JBBPFieldByte.class).getAsInt());
  assertArrayEquals(new byte[] {3}, parsed.findFieldForPathAndType("aa", JBBPFieldArrayByte.class).getArray());
}
 
Example #9
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseManyFieldsWithTheSameName() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte l; a { byte a; b { byte a; c { byte a;}}} byte [a.b.c.a] aa;").parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
  assertEquals(1, parsed.findFieldForPathAndType("l", JBBPFieldByte.class).getAsInt());
  assertEquals(2, parsed.findFieldForPathAndType("a.a", JBBPFieldByte.class).getAsInt());
  assertEquals(3, parsed.findFieldForPathAndType("a.b.a", JBBPFieldByte.class).getAsInt());
  assertEquals(4, parsed.findFieldForPathAndType("a.b.c.a", JBBPFieldByte.class).getAsInt());
  assertArrayEquals(new byte[] {5, 6, 7, 8}, parsed.findFieldForPathAndType("aa", JBBPFieldArrayByte.class).getArray());
}
 
Example #10
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseWithStreamPositionMacrosInExpressions() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("int start; byte [$$-2] array; byte [$$-4] array2; int end;").parse(new byte[] {1, 2, 3, 4, 0x1A, 0x1B, 0x1C, 0x1D, 4, 3, 2, 1});
  assertEquals(0x01020304, parsed.findFieldForPathAndType("start", JBBPFieldInt.class).getAsInt());
  assertArrayEquals(new byte[] {0x1A, 0x1B}, parsed.findFieldForPathAndType("array", JBBPFieldArrayByte.class).getArray());
  assertArrayEquals(new byte[] {0x1C, 0x1D}, parsed.findFieldForPathAndType("array2", JBBPFieldArrayByte.class).getArray());
  assertEquals(0x04030201, parsed.findFieldForPathAndType("end", JBBPFieldInt.class).getAsInt());
}
 
Example #11
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseWithStreamPositionMacros() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("int start; byte [$$] array; int end;").parse(new byte[] {1, 2, 3, 4, 0x1A, 0x1B, 0x1C, 0x1D, 4, 3, 2, 1});
  assertEquals(0x01020304, parsed.findFieldForPathAndType("start", JBBPFieldInt.class).getAsInt());
  assertArrayEquals(new byte[] {0x1A, 0x1B, 0x1C, 0x1D}, parsed.findFieldForPathAndType("array", JBBPFieldArrayByte.class).getArray());
  assertEquals(0x04030201, parsed.findFieldForPathAndType("end", JBBPFieldInt.class).getAsInt());
}
 
Example #12
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_Byte_ArrayForCalculatedLength() throws Exception {
  final JBBPFieldArrayByte array = JBBPParser.prepare("ubyte len; byte[len*6/2-2];").parse(new byte[] {2, 1, 2, 3, 4}).findFieldForType(JBBPFieldArrayByte.class);

  assertEquals(1, array.getAsInt(0));
  assertEquals(2, array.getAsInt(1));
  assertEquals(3, array.getAsInt(2));
  assertEquals(4, array.getAsInt(3));
}
 
Example #13
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_WholeByteStream() throws Exception {
  final JBBPFieldArrayByte array = JBBPParser.prepare("byte[_];").parse(new byte[] {1, 2, 3, 4}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(1, array.getAsInt(0));
  assertEquals(2, array.getAsInt(1));
  assertEquals(3, array.getAsInt(2));
  assertEquals(4, array.getAsInt(3));
}
 
Example #14
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_VarByteArrayWhenSizeInStruct() throws Exception {
  final JBBPFieldArrayByte array = JBBPParser.prepare("str {byte size;} byte[str.size];").parse(new byte[] {4, 1, 2, 3, 4}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, array.size());
  assertEquals(1, array.getAsInt(0));
  assertEquals(2, array.getAsInt(1));
  assertEquals(3, array.getAsInt(2));
  assertEquals(4, array.getAsInt(3));
}
 
Example #15
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_VarByteArray() throws Exception {
  final JBBPFieldArrayByte array = JBBPParser.prepare("byte size; byte[size];").parse(new byte[] {4, 1, 2, 3, 4}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, array.size());
  assertEquals(1, array.getAsInt(0));
  assertEquals(2, array.getAsInt(1));
  assertEquals(3, array.getAsInt(2));
  assertEquals(4, array.getAsInt(3));
}
 
Example #16
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_NonFixedByteArray_LittleEndian() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare("<byte[_];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x87, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x21, bytes.getAsInt(3) & 0xFF);
}
 
Example #17
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_NonFixedByteArray_BigEndian() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare(">byte[_];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x21, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x87, bytes.getAsInt(3) & 0xFF);
}
 
Example #18
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_NonFixedByteArray_Default() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare("byte[_];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x21, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x87, bytes.getAsInt(3) & 0xFF);
}
 
Example #19
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_FixedByteArray_LittleEndian() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare("<byte[4];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x87, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x21, bytes.getAsInt(3) & 0xFF);
}
 
Example #20
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_FixedByteArray_BigEndian() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare(">byte[4];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x21, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x87, bytes.getAsInt(3) & 0xFF);
}
 
Example #21
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_FixedByteArray_Default() throws Exception {
  final JBBPFieldArrayByte bytes = JBBPParser.prepare("byte[4];").parse(new byte[] {0x21, 0x43, 0x65, (byte) 0x87}).findFieldForType(JBBPFieldArrayByte.class);
  assertEquals(4, bytes.size());
  assertEquals(0x21, bytes.getAsInt(0) & 0xFF);
  assertEquals(0x43, bytes.getAsInt(1) & 0xFF);
  assertEquals(0x65, bytes.getAsInt(2) & 0xFF);
  assertEquals(0x87, bytes.getAsInt(3) & 0xFF);
}
 
Example #22
Source File: CustomThreeByteIntegerTypeTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadThreeByteInteger_NamedCustomFieldInExpression() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("int24 value1; int24 value2; byte [value1+value2];", new Int24CustomTypeProcessor());
  final JBBPFieldStruct struct = parser.parse(new byte[] {0, 0, 2, 0, 0, 3, 1, 2, 3, 4, 5});
  assertEquals(5, struct.findFieldForType(JBBPFieldArrayByte.class).size());
  assertEquals(2, struct.findFieldForNameAndType("value1", JBBPFieldInt.class).getAsInt());
  assertEquals(3, struct.findFieldForNameAndType("value2", JBBPFieldInt.class).getAsInt());
}
 
Example #23
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testParse_WholeByteStream_Empty() throws Exception {
  assertEquals(0, JBBPParser.prepare("byte[_];").parse(new byte[0]).findFieldForType(JBBPFieldArrayByte.class).size());
}
 
Example #24
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testParse_ByteArray_IgnoredForZeroLength() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte len; byte [len]; ushort;").parse(new byte[] {0x0, 0x01, (byte) 0x02});
  assertEquals(0, parsed.findFieldForType(JBBPFieldArrayByte.class).size());
  assertEquals(0x0102, parsed.findFieldForType(JBBPFieldUShort.class).getAsInt());
}
 
Example #25
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testParse_NonFixedByteArray_ParsedAsEmptyArray() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte; byte[_] array;").parse(new byte[] {1});
  assertEquals(0, parsed.findFieldForNameAndType("array", JBBPFieldArrayByte.class).size());
}
 
Example #26
Source File: CustomThreeByteIntegerTypeTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadThreeByte_NamedCustomFieldAsArrayLength() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("int24 value; byte [value];", new Int24CustomTypeProcessor());
  assertEquals(5, parser.parse(new byte[] {0, 0, 5, 1, 2, 3, 4, 5}).findFieldForType(JBBPFieldArrayByte.class).size());
}
 
Example #27
Source File: BasedOnQuestionsAndCasesTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
/**
 * Case 28-jul-2016
 * Simultaneous usage of expression evaluator from multiple threads.
 *
 * <a href="https://github.com/raydac/java-binary-block-parser/issues/10">Issue #10, assertArrayLength throws exception in multi-thread</a>
 *
 * @throws Exception for any error
 */
@Test
public void testMutlithredUsageOfParser() throws Exception {
  final JBBPParser parserIP = JBBPParser.prepare("skip:14; // skip bytes till the frame\n"
      + "bit:4 InternetHeaderLength;"
      + "bit:4 Version;"
      + "bit:2 ECN;"
      + "bit:6 DSCP;"
      + "ushort TotalPacketLength;"
      + "ushort Identification;"
      + "bit:8 IPFlagsAndFragmentOffset_low;"
      + "bit:5 IPFlagsAndFragmentOffset_high;"
      + "bit:1 MoreFragment;"
      + "bit:1 DonotFragment;"
      + "bit:1 ReservedBit;"
      + "ubyte TTL;"
      + "ubyte Protocol;"
      + "ushort HeaderChecksum;"
      + "int SourceAddress;"
      + "int DestinationAddress;"
      + "byte [(InternetHeaderLength-5)*4] Options;");

  final JBBPParser parserTCP = JBBPParser.prepare("skip:34; // skip bytes till the frame\n"
      + "ushort SourcePort;"
      + "ushort DestinationPort;"
      + "int SequenceNumber;"
      + "int AcknowledgementNumber;"
      + "bit:1 NONCE;"
      + "bit:3 RESERVED;"
      + "bit:4 HLEN;"
      + "bit:1 FIN;"
      + "bit:1 SYN;"
      + "bit:1 RST;"
      + "bit:1 PSH;"
      + "bit:1 ACK;"
      + "bit:1 URG;"
      + "bit:1 ECNECHO;"
      + "bit:1 CWR;"
      + "ushort WindowSize;"
      + "ushort TCPCheckSum;"
      + "ushort UrgentPointer;"
      + "byte [HLEN*4-20] Option;"
      + "byte [_] Data;");

  byte[] testArray;
  try (InputStream inStream = getResourceAsInputStream("tcppacket.bin")) {
    testArray = new JBBPBitInputStream(inStream).readByteArray(-1);
    assertEquals(173, testArray.length);
  }

  final byte[] theData = testArray;

  final AtomicInteger errorCounter = new AtomicInteger();
  final AtomicLong parsingCounter = new AtomicLong();

  final int ITERATIONS = 1000;

  final Runnable test = () -> {
    for (int i = 0; i < ITERATIONS; i++) {
      try {
        Thread.sleep(System.nanoTime() & 0xF);
        final byte[] ippacket = parserTCP.parse(theData).findFieldForNameAndType("Data", JBBPFieldArrayByte.class).getArray();
        assertEquals(119, ippacket.length);
        final byte[] optionsip = parserIP.parse(ippacket).findFieldForNameAndType("Options", JBBPFieldArrayByte.class).getArray();
        assertEquals(4, optionsip.length);
        parsingCounter.incrementAndGet();
      } catch (Exception ex) {
        ex.printStackTrace();
        errorCounter.incrementAndGet();
      }
    }
  };

  final Thread[] threads = new Thread[15];

  for (int i = 0; i < threads.length; i++) {
    final Thread testThread = new Thread(test, "jbbp_test_thread" + i);
    testThread.setDaemon(true);
    threads[i] = testThread;
    testThread.start();
  }

  for (final Thread t : threads) {
    t.join();
  }

  assertEquals(threads.length * ITERATIONS, parsingCounter.get());
  assertEquals(0, errorCounter.get());
}
 
Example #28
Source File: Z80_v1_ParsingTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Override
public Object prepareObjectForMapping(JBBPFieldStruct parsedBlock, Bin annotation, Field field) {
  if (field.getName().equals("data")) {
    final byte[] data = parsedBlock.findFieldForNameAndType("data", JBBPFieldArrayByte.class).getArray();

    if (parsedBlock.findFieldForPathAndType("flags.compressed", JBBPFieldBit.class).getAsBool()) {
      // RLE compressed
      final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length << 1);
      int i = 0;

      // check end marker
      assertEquals(0x00, data[data.length - 1] & 0xFF);
      assertEquals(0xED, data[data.length - 2] & 0xFF);
      assertEquals(0xED, data[data.length - 3] & 0xFF);
      assertEquals(0x00, data[data.length - 4] & 0xFF);

      final int len = data.length - 4;

      while (i < len) {
        final int a = data[i++] & 0xFF;
        if (a == 0xED) {
          final int b = data[i++] & 0xFF;
          if (b == 0xED) {
            int num = data[i++] & 0xFF;
            final int val = data[i++] & 0xFF;
            while (num > 0) {
              baos.write(val);
              num--;
            }
          } else {
            baos.write(a);
            baos.write(b);
          }
        } else {
          baos.write(a);
        }
      }
      return baos.toByteArray();
    } else {
      // uncompressed
      return data;
    }
  } else {
    fail("Unexpected field");
    return null;
  }
}
 
Example #29
Source File: Z80InZXPOutPlugin.java    From zxpoly with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Object prepareObjectForMapping(JBBPFieldStruct parsedBlock, Bin annotation,
                                      Field field) {
  if (this.version == VERSION_1) {
    if (field.getName().equals("data")) {
      final byte[] data =
          parsedBlock.findFieldForNameAndType("data", JBBPFieldArrayByte.class).getArray();

      if (parsedBlock.findFieldForPathAndType("flags.compressed", JBBPFieldBit.class)
          .getAsBool()) {
        return decodeRLE(data, 0, data.length);
      } else {
        // uncompressed
        return data;
      }
    } else {
      return null;
    }
  } else {
    if (field.getName().equalsIgnoreCase("data")) {
      return parsedBlock.findFieldForNameAndType("data", JBBPFieldArrayByte.class).getArray();
    } else if (field.getName().equalsIgnoreCase("banks")) {
      final byte[] rawdata =
          parsedBlock.findFieldForNameAndType("data", JBBPFieldArrayByte.class).getArray();
      int pos = 0;
      int len = rawdata.length;
      final List<Bank> banks = new ArrayList<>();
      while (len > 0) {
        final int blocklength = ((rawdata[pos++] & 0xFF)) | ((rawdata[pos++] & 0xFF) << 8);
        final int page = rawdata[pos++] & 0xFF;
        len -= 3 + (blocklength == 0xFFFF ? PAGE_SIZE : blocklength);
        final byte[] uncompressed = unpackBank(rawdata, pos, blocklength);
        pos += blocklength == 0xFFFF ? PAGE_SIZE : blocklength;
        banks.add(new Bank(page, uncompressed));
      }
      return banks.toArray(new Bank[0]);
    } else {
      return null;
    }
  }
}