com.igormaznitsa.jbbp.model.JBBPFieldStruct Java Examples

The following examples show how to use com.igormaznitsa.jbbp.model.JBBPFieldStruct. 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: JBBPMapper.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
/**
 * Map a structure to a class instance.
 *
 * @param <T>                  the mapping class type
 * @param rootStructure        a structure to be mapped, must not be null
 * @param instance             a class instance to be destination for map
 *                             operations, must not be null
 * @param customFieldProcessor a custom field processor to provide custom
 *                             values, it can be null if there is not any mapping field desires the
 *                             processor
 * @param flags                special flags for mapping process
 * @param instantiators        functions to produce class instance by request, must
 *                             not be null
 * @return the processed class instance, the same which was the argument for
 * the method.
 * @throws JBBPMapperException for any error
 * @see #FLAG_IGNORE_MISSING_VALUES
 * @since 1.1
 */
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> T map(final JBBPFieldStruct rootStructure, final T instance, final JBBPMapperCustomFieldProcessor customFieldProcessor, final int flags, final Function<Class<?>, Object>... instantiators) {
  JBBPUtils.assertNotNull(rootStructure, "The Root structure must not be null");
  JBBPUtils.assertNotNull(instance, "The Mapping class instance must not be null");

  // Don't use forEach() for Android compatibility!
  for (final MappedFieldRecord record : findAffectedFields(instance)) {
    processFieldOfMappedClass(
        record,
        rootStructure,
        instance,
        customFieldProcessor,
        flags,
        instantiators
    );
  }
  return instance;
}
 
Example #2
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testParse_SeveralPrimitiveFields() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("bit:4; bit:4; bool; byte; ubyte; short; ushort; int; long;");
  final JBBPFieldStruct result = parser.parse(new byte[] {0x12, 1, 87, (byte) 0xF3, 1, 2, (byte) 0xFE, 4, 6, 7, 8, 9, (byte) 0xFF, 1, 2, 3, 5, 6, 7, 8, 9});

  assertEquals(2, result.findFirstFieldForType(JBBPFieldBit.class).getAsInt());
  assertEquals(1, result.findLastFieldForType(JBBPFieldBit.class).getAsInt());

  try {
    result.findFieldForType(JBBPFieldBit.class);
    fail("Must throw JBBPTooManyFieldsFoundException");
  } catch (JBBPTooManyFieldsFoundException ex) {
    assertEquals(2, ex.getNumberOfFoundInstances());
  }

  assertTrue(result.findFieldForType(JBBPFieldBoolean.class).getAsBool());
  assertEquals(87, result.findFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0xF3, result.findFieldForType(JBBPFieldUByte.class).getAsInt());
  assertEquals(0x0102, result.findFieldForType(JBBPFieldShort.class).getAsInt());
  assertEquals(0xFE04, result.findFieldForType(JBBPFieldUShort.class).getAsInt());
  assertEquals(0x06070809, result.findFieldForType(JBBPFieldInt.class).getAsInt());
  assertEquals(0xFF01020305060708L, result.findFieldForType(JBBPFieldLong.class).getAsLong());
}
 
Example #3
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 #4
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 #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_SkipStructureForZeroItems() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("byte len; sss [len]{ sss2[10]{ sss3{long;} sss4[45]{ushort; bool [11]; short; bit:4;} byte;}} byte end;").parse(new byte[] {0x00, 0x1F});
  assertEquals(0, parsed.findFieldForPathAndType("len", JBBPFieldByte.class).getAsInt());
  assertEquals(0, parsed.findFieldForPathAndType("sss", JBBPFieldArrayStruct.class).size());
  assertEquals(0x1F, parsed.findFieldForPathAndType("end", JBBPFieldByte.class).getAsInt());
}
 
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 testParse_Align_ExtraNumericFieldAsExpression() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("ubyte a; align:(a+1); ubyte b;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {2, 0x12, 0x34, 0x11, 0x22, 0x56});
  assertEquals(2, parsed.findFieldForNameAndType("a", JBBPFieldUByte.class).getAsInt());
  assertEquals(0x11, parsed.findFieldForNameAndType("b", JBBPFieldUByte.class).getAsInt());
}
 
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 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 #8
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_Align_Int_WithoutEffect() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("byte; byte; byte; byte; align:4; int;");
  final JBBPFieldStruct result = parser.parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
  assertEquals(1, result.findFirstFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(4, result.findLastFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0x05060708, result.findLastFieldForType(JBBPFieldInt.class).getAsInt());
}
 
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 testParseFixedSizeStructureArray() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("int val1; inner [2] { byte a; byte b;}");

  final JBBPFieldStruct parsed = parser.parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
  assertEquals(0x01020304, parsed.findFieldForPathAndType("val1", JBBPFieldInt.class).getAsInt());

  final JBBPFieldArrayStruct structArray = parsed.findFieldForNameAndType("inner", JBBPFieldArrayStruct.class);

  assertEquals(2, structArray.size());
}
 
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 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 #11
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 #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_Skip_LongDistance() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("byte; skip:32000; short;");
  final byte[] data = new byte[32003];
  data[0] = 1;
  data[32001] = 0x0A;
  data[32002] = 0x0B;
  final JBBPFieldStruct result = parser.parse(data);
  assertEquals(1, result.findFirstFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0x0A0B, result.findFieldForType(JBBPFieldShort.class).getAsInt());
}
 
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_Skip_ShortDistance() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("byte; skip:3; short;");
  final JBBPFieldStruct result = parser.parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
  assertEquals(1, result.findFirstFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0x0506, result.findFieldForType(JBBPFieldShort.class).getAsInt());
}
 
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_Skip_WithoutArgument() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("byte; skip; short;");
  final JBBPFieldStruct result = parser.parse(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
  assertEquals(1, result.findFirstFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0x0304, result.findFieldForType(JBBPFieldShort.class).getAsInt());
}
 
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_Align_Default() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("bit:4; align; bool; byte; ubyte; short; ushort; int; long;");
  final JBBPFieldStruct result = parser.parse(new byte[] {0x12, 1, 87, (byte) 0xF3, 1, 2, (byte) 0xFE, 4, 6, 7, 8, 9, (byte) 0xFF, 1, 2, 3, 5, 6, 7, 8, 9});
  assertEquals(2, result.findFieldForType(JBBPFieldBit.class).getAsInt());
  assertTrue(result.findFieldForType(JBBPFieldBoolean.class).getAsBool());
  assertEquals(87, result.findFieldForType(JBBPFieldByte.class).getAsInt());
  assertEquals(0xF3, result.findFieldForType(JBBPFieldUByte.class).getAsInt());
  assertEquals(0x0102, result.findFieldForType(JBBPFieldShort.class).getAsInt());
  assertEquals(0xFE04, result.findFieldForType(JBBPFieldUShort.class).getAsInt());
  assertEquals(0x06070809, result.findFieldForType(JBBPFieldInt.class).getAsInt());
  assertEquals(0xFF01020305060708L, result.findFieldForType(JBBPFieldLong.class).getAsLong());
}
 
Example #16
Source File: JBBPDslBuilderTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testReportedIssue_20_NPEforOutBitNumber() throws Exception {
  class BreakJBBPDslBuilder {
    @Bin(order = 1, comment = "Reserved", type = BinType.BIT_ARRAY, arraySizeExpr = "4")
    public byte[] reserved;
  }

  final String dsl = Begin().AnnotatedClass(BreakJBBPDslBuilder.class).End();

  assertEquals("BreakJBBPDslBuilder{bit:8[4] reserved;// Reserved\n}", dsl);

  JBBPFieldStruct struct = JBBPParser.prepare(dsl).parse(new byte[] {1, 2, 3, 4});
  assertArrayEquals(new byte[] {1, 2, 3, 4}, struct.findFieldForType(JBBPFieldStruct.class).findFieldForType(JBBPFieldArrayBit.class).getArray());
}
 
Example #17
Source File: JBBPMapper.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
@SuppressWarnings("varargs")
private static void processFieldOfMappedClass(
    final MappedFieldRecord record,
    final JBBPFieldStruct rootStructure,
    final Object instance,
    final JBBPMapperCustomFieldProcessor customFieldProcessor,
    final int flags,
    final Function<Class<?>, Object>... instantiators
) {
  if (record.binAnnotation.custom()) {
    JBBPUtils.assertNotNull(customFieldProcessor, "There is a custom mapping field, in the case you must provide a custom mapping field processor");
    final Object value = customFieldProcessor.prepareObjectForMapping(rootStructure, record.binAnnotation, record.mappingField);
    MappedFieldRecord.setFieldValue(instance, record.setter, record.mappingField, null, value);
  } else {
    final JBBPAbstractField binField;

    if (record.fieldPath.length() == 0) {
      binField = record.fieldName.length() == 0 ? rootStructure.findFieldForType(record.fieldType.getFieldClass()) : rootStructure.findFieldForNameAndType(record.fieldName, record.fieldType.getFieldClass());
    } else {
      binField = rootStructure.findFieldForPathAndType(record.fieldPath, record.fieldType.getFieldClass());
    }

    if (binField == null) {
      if ((flags & FLAG_IGNORE_MISSING_VALUES) != 0) {
        return;
      }
      throw new JBBPMapperException("Can't find value for mapping field [" + record.mappingField + ']', null, record.mappingClass, record.mappingField, null);
    }

    if (record.bitWideField && record.mappedBitNumber != JBBPBitNumber.BITS_8 && ((BitEntity) binField).getBitWidth() != record.mappedBitNumber) {
      throw new JBBPMapperException("Can't map mapping field because wrong field bitness [" + record.mappedBitNumber + "!=" + ((BitEntity) binField).getBitWidth().getBitNumber() + ']', null, record.mappingClass, record.mappingField, null);
    }
    record.proc.apply(record, rootStructure, instance, customFieldProcessor, binField, flags, instantiators);
  }
}
 
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_BitArray_ExtraNumericFieldAsExpression() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("ubyte s; ubyte a; bit:(a*4-12) [s] b; bit:(a*2) d;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {2, 4, 0x12, 0x34});
  assertEquals(2, parsed.findFieldForNameAndType("s", JBBPFieldUByte.class).getAsInt());
  assertEquals(4, parsed.findFieldForNameAndType("a", JBBPFieldUByte.class).getAsInt());
  assertArrayEquals(new byte[] {2, 1}, parsed.findFieldForNameAndType("b", JBBPFieldArrayBit.class).getArray());
  assertEquals(0x34, parsed.findFieldForNameAndType("d", JBBPFieldBit.class).getAsInt());
}
 
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_Skip_ExtraNumericFieldAsExpression() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("ubyte a; skip:(a*2); ubyte b;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {2, 0x12, 0x34, 0x11, 0x22, 0x56});
  assertEquals(2, parsed.findFieldForNameAndType("a", JBBPFieldUByte.class).getAsInt());
  assertEquals(0x56, parsed.findFieldForNameAndType("b", JBBPFieldUByte.class).getAsInt());
}
 
Example #20
Source File: PNGParsingTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testPngParsing() throws Exception {
  final InputStream pngStream = getResourceAsInputStream("picture.png");
  try {

    final JBBPParser pngParser = JBBPParser.prepare(
        "long header;"
            + "// chunks\n"
            + "chunk [_]{"
            + "   int length; "
            + "   int type; "
            + "   byte[length] data; "
            + "   int crc;"
            + "}"
    );

    final JBBPFieldStruct result = pngParser.parse(pngStream);

    assertEquals(0x89504E470D0A1A0AL, result.findFieldForNameAndType("header", JBBPFieldLong.class).getAsLong());

    final JBBPFieldArrayStruct chunks = result.findFieldForNameAndType("chunk", JBBPFieldArrayStruct.class);

    final String[] chunkNames = new String[] {"IHDR", "gAMA", "bKGD", "pHYs", "tIME", "tEXt", "IDAT", "IEND"};
    final int[] chunkSizes = new int[] {0x0D, 0x04, 0x06, 0x09, 0x07, 0x19, 0x0E5F, 0x00};

    assertEquals(chunkNames.length, chunks.size());

    for (int i = 0; i < chunks.size(); i++) {
      assertChunk(chunkNames[i], chunkSizes[i], chunks.getElementAt(i));
    }

    assertEquals(3847, pngParser.getFinalStreamByteCounter());
  } finally {
    JBBPUtils.closeQuietly(pngStream);
  }
}
 
Example #21
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 #22
Source File: TGAParsingTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testTgaParsing_Cbw8() throws Exception {
  final InputStream tgaStream = getResourceAsInputStream("cbw8.tga");
  try {
    final JBBPFieldStruct result = TGAParser.parse(tgaStream);
    assertTgaFile(result, "Truevision(R) Sample Image", 128, 128, 8, 0, 8715);
  } finally {
    JBBPUtils.closeQuietly(tgaStream);
  }
}
 
Example #23
Source File: TGAParsingTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testTgaParsing_Xingt32() throws Exception {
  final InputStream tgaStream = getResourceAsInputStream("xing_t32.tga");
  try {
    final JBBPFieldStruct result = TGAParser.parse(tgaStream);
    assertTgaFile(result, "", 240, 164, 32, 0, 240 * 164 * 4);
  } finally {
    JBBPUtils.closeQuietly(tgaStream);
  }
}
 
Example #24
Source File: TGAParsingTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testTgaParsing_Logo() throws Exception {
  final InputStream tgaStream = getResourceAsInputStream("logo.tga");
  try {
    final JBBPFieldStruct result = TGAParser.parse(tgaStream);
    assertTgaFile(result, "", 319, 165, 32, 0, 116944);
    assertEquals(0, result.findFieldForPathAndType("Header.XOffset", JBBPFieldShort.class).getAsInt());
    assertEquals(165, result.findFieldForPathAndType("Header.YOffset", JBBPFieldShort.class).getAsInt());
  } finally {
    JBBPUtils.closeQuietly(tgaStream);
  }
}
 
Example #25
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldNameCaseInsensetive() throws Exception {
  final JBBPFieldStruct parsed = JBBPParser.prepare("bool FiElD1;").parse(new byte[] {1});
  assertTrue(parsed.nameExists("fiELD1"));
  assertTrue(parsed.pathExists("fiELD1"));
  assertNotNull(parsed.findFieldForName("fiELD1"));
  assertNotNull(parsed.findFieldForNameAndType("fiELD1", JBBPFieldBoolean.class));
  assertNotNull(parsed.findFieldForPathAndType("fiELD1", JBBPFieldBoolean.class));
}
 
Example #26
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 #27
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 #28
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_SingleDefaultNonamedBool_LittleEndian() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("<bool;");
  JBBPFieldStruct result = parser.parse(new byte[] {(byte) 1});
  assertTrue(result.findFieldForType(JBBPFieldBoolean.class).getAsBool());
  result = parser.parse(new byte[] {(byte) 0});
  assertFalse(result.findFieldForType(JBBPFieldBoolean.class).getAsBool());
}
 
Example #29
Source File: Bin2Json.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private JsonObject convert(JBBPFieldStruct data) throws ConversionException {
    try {
        LocalDateTime start = LocalDateTime.now();
        final JsonObject json = convertToJSon(data);
        if (logger.isTraceEnabled()) {
            Duration duration = Duration.between(start, LocalDateTime.now());
            logger.trace("Conversion time={}, json={}", duration, json);
        }
        return json;
    } catch (JBBPException e) {
        throw new ConversionException(String.format("Unexpected error, reason: %s", e.getMessage(), e));
    }
}
 
Example #30
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());
}