com.igormaznitsa.jbbp.model.JBBPFieldBit Java Examples

The following examples show how to use com.igormaznitsa.jbbp.model.JBBPFieldBit. 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: 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 #2
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParse_Bit_ExtraNumericFieldAsExpression() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("ubyte a; bit:(a*4-12) b; bit:(a) c; bit:(a*2) d;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {4, 0x12, 0x34});
  assertEquals(4, parsed.findFieldForNameAndType("a", JBBPFieldUByte.class).getAsInt());
  assertEquals(2, parsed.findFieldForNameAndType("b", JBBPFieldBit.class).getAsInt());
  assertEquals(1, parsed.findFieldForNameAndType("c", JBBPFieldBit.class).getAsInt());
  assertEquals(0x34, parsed.findFieldForNameAndType("d", JBBPFieldBit.class).getAsInt());
}
 
Example #3
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 #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_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 #5
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseResetCounterWithCachedBits() throws Exception {
  final JBBPParser parser = JBBPParser.prepare("bit:4 a; reset$$; byte b;");
  final JBBPFieldStruct parsed = parser.parse(new byte[] {0x3, 0x1F});
  assertEquals(3, parsed.findFieldForNameAndType("a", JBBPFieldBit.class).getAsInt());
  assertEquals(0x1F, parsed.findFieldForNameAndType("b", JBBPFieldByte.class).getAsInt());
  assertEquals(1, parser.getFinalStreamByteCounter());
}
 
Example #6
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;
    }
  }
}
 
Example #7
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 #8
Source File: JBBPParserTest.java    From java-binary-block-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testParse_BitFields_SizeProvidedThroughExpression() throws Exception {
  assertEquals(4, JBBPParser.prepare("ubyte a; ubyte b; bit:(a+b) c;").parse(new byte[] {1, 2, (byte) 0xB4}).findFieldForType(JBBPFieldBit.class).getAsInt());
  assertEquals(20, JBBPParser.prepare("ubyte a; ubyte b; bit:(a+b) c;").parse(new byte[] {3, 2, (byte) 0xB4}).findFieldForType(JBBPFieldBit.class).getAsInt());
}