Java Code Examples for com.igormaznitsa.jbbp.io.JBBPBitInputStream#readByteArray()

The following examples show how to use com.igormaznitsa.jbbp.io.JBBPBitInputStream#readByteArray() . 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: SessionData.java    From zxpoly with GNU General Public License v3.0 6 votes vote down vote up
public SessionData(final JBBPBitInputStream inStream) throws IOException {
  baseAddress = inStream.readInt(JBBPByteOrder.BIG_ENDIAN);
  showGrid = inStream.readBoolean();
  showColumns = inStream.readBoolean();
  zxAddressing = inStream.readBoolean();
  invertBase = inStream.readBoolean();
  mode512x384 = inStream.readBoolean();
  columnNumber = inStream.readInt(JBBPByteOrder.BIG_ENDIAN);
  attributeMode =
      EditorComponent.AttributeMode.values()[inStream.readInt(JBBPByteOrder.BIG_ENDIAN)];
  zoom = inStream.readInt(JBBPByteOrder.BIG_ENDIAN);

  extraProperties = new Properties();

  if (inStream.hasAvailableData()) {
    final int extraDataLength = inStream.readInt(JBBPByteOrder.BIG_ENDIAN);
    final byte[] propertyData = inStream.readByteArray(extraDataLength);
    final Properties properties = new Properties();
    properties.load(new StringReader(new String(propertyData, StandardCharsets.UTF_8)));
    properties.stringPropertyNames().forEach(name -> {
      this.extraProperties.put(name, properties.getProperty(name));
    });
  }
}
 
Example 2
Source File: PackedBCDCustomFieldTest.java    From java-binary-block-parser with Apache License 2.0 6 votes vote down vote up
public static long readValueFromPackedDecimal(final JBBPBitInputStream in, final int len, final boolean signed) throws IOException {
  final byte[] data = in.readByteArray(len);

  StringBuilder digitStr = new StringBuilder();
  for (int i = 0; i < len * 2; i++) {
    byte currentByte = data[i / 2];
    byte digit = (i % 2 == 0) ? (byte) ((currentByte & 0xff) >>> 4) : (byte) (currentByte & 0x0f);
    if (digit < 10) {
      digitStr.append(digit);
    }
  }

  if (signed) {
    byte sign = (byte) (data[len - 1] & 0x0f);
    if (sign == 0x0b || sign == 0x0d) {
      digitStr.insert(0, '-');
    }
  }

  return Long.parseLong(digitStr.toString());
}
 
Example 3
Source File: Parser.java    From Flashtool with GNU General Public License v3.0 6 votes vote down vote up
private static String getSin(String folder, byte[] source) throws Exception {
 Collection<File> sinfiles = FileUtils.listFiles(new File(folder), new String[] {"sin"}, true);
 Iterator<File> ifiles = sinfiles.iterator();
 while (ifiles.hasNext()) {
  try {
	  SinFile  sinfile = new SinFile(ifiles.next());
	  if (sinfile.getVersion()!=4) {
		  JBBPBitInputStream sinStream = new JBBPBitInputStream(new FileInputStream(sinfile.getFile()));
		  byte[] res = sinStream.readByteArray(source.length);
		  if (Arrays.equals(source, res))
			  return sinfile.getShortName();
	  }
	  else {
		  if (Arrays.equals(source, sinfile.getHeader())) return sinfile.getFile().getName();
	  }
  } catch (EOFException eof) {
  }			  
 }
 return "Not identified";
}
 
Example 4
Source File: Info.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
public Info(final InputStream in) throws IOException {
  final JBBPBitInputStream bitin = new JBBPBitInputStream(in);
  this.name = new String(bitin.readByteArray(bitin.readByte()), StandardCharsets.US_ASCII);
  this.type = (char) bitin.readUnsignedShort(JBBPByteOrder.BIG_ENDIAN);
  this.startAddress = bitin.readInt(JBBPByteOrder.BIG_ENDIAN);
  this.length = bitin.readInt(JBBPByteOrder.BIG_ENDIAN);
  this.offset = bitin.readInt(JBBPByteOrder.BIG_ENDIAN);

  this.extra = bitin.readByteArray(bitin.readInt(JBBPByteOrder.BIG_ENDIAN));
}
 
Example 5
Source File: SCLPlugin.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ReadResult readFrom(final File file, final int index) throws IOException {
  final List<SCLCatalogItem> list = new ArrayList<>();
  final JBBPBitInputStream in = new JBBPBitInputStream(new FileInputStream(file));
  try {
    final long id = in.readLong(JBBPByteOrder.BIG_ENDIAN);
    if (id == 0x53494E434C414952L) {
      // it's scl
      final int fileNumber = in.readByte();
      for (int i = 0; i < fileNumber; i++) {
        final SCLCatalogItem item = CATALOG_PARSER.parse(in).mapTo(new SCLCatalogItem());
        list.add(item);
      }

      final SCLCatalogItem itemToRead = list.get(index);

      for (int i = 0; i < index; i++) {
        final int len = list.get(i).sectors * 256;
        if (len != in.skip(len)) {
          throw new IllegalStateException("Can't skip bytes:" + list.get(i).length);
        }
      }
      final long offset = in.getCounter();
      return new ReadResult(new ZXPolyData(
          new Info(itemToRead.name, itemToRead.type, itemToRead.start, itemToRead.length,
              (int) offset), this, in.readByteArray(itemToRead.sectors * 256)), null);

    } else {
      throw new IllegalArgumentException("It's not a SCl file: " + file);
    }
  } finally {
    JBBPUtils.closeQuietly(in);
  }
}
 
Example 6
Source File: TRDPlugin.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ReadResult readFrom(final File file, final int index) throws IOException {
  final JBBPBitInputStream inStream =
      new JBBPBitInputStream(new FileInputStream(file), JBBPBitOrder.LSB0);
  try {
    final List<TRDosCatalogItem> list = new ArrayList<>();
    for (int i = 0; i < 128; i++) {
      final TRDosCatalogItem item = CATALOG_PARSER.parse(inStream).mapTo(new TRDosCatalogItem());
      if (item.name.charAt(0) > 1) {
        list.add(item);
      }
    }

    final TRDosCatalogItem info = list.get(index);

    final int offsetToFile = ((info.track << 4) + info.firstSector) * 256;
    final long toskip = offsetToFile - inStream.getCounter();
    final long skept = inStream.skip(toskip);
    if (skept != toskip) {
      throw new IllegalStateException("Can't skip needed byte number [" + toskip + ']');
    }
    return new ReadResult(
        new ZXPolyData(new Info(info.name, info.type, info.start, info.length, offsetToFile),
            this, inStream.readByteArray(info.sectors << 8)), null);

  } finally {
    JBBPUtils.closeQuietly(inStream);
  }
}
 
Example 7
Source File: ZXEMLSnapshotFormatTest.java    From zxpoly with GNU General Public License v3.0 5 votes vote down vote up
private static byte [] loadResource(final String name) throws Exception {
  final InputStream ins = ZXEMLSnapshotFormatTest.class.getResourceAsStream(name);
  assertNotNull("Can't find resource "+name,ins);
  final JBBPBitInputStream in = new JBBPBitInputStream(ins);
  final byte [] result = in.readByteArray(-1);
  in.close();
  return result;
}
 
Example 8
Source File: S1Packet.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void finalise() throws IOException {
	if (data!=null) {
		JBBPBitInputStream dataStream = new JBBPBitInputStream(new ByteArrayInputStream(data));
		if (data.length >4)
			data = dataStream.readByteArray(data.length-4);
		else data = null;
		crc = dataStream.readByteArray(4);
	}
	if (data==null) datalength=0;
	else datalength = data.length;		
}
 
Example 9
Source File: S1Packet.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public TAUnit getTA() {
	try {
		JBBPBitInputStream taStream = new JBBPBitInputStream(new ByteArrayInputStream(data));
		int unit=taStream.readInt(JBBPByteOrder.BIG_ENDIAN);
		int talength = taStream.readInt(JBBPByteOrder.BIG_ENDIAN);
		TAUnit u = new TAUnit(unit, taStream.readByteArray(talength));
		taStream.close();
		return u;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 10
Source File: SinParser.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void parseHash(JBBPBitInputStream sinStream) throws IOException {
 JBBPParser hashBlocksV2 = JBBPParser.prepare(
           "block[_] {int offset;"
                   + "int length;"
                   + "byte hashLen;"
                   + "byte[hashLen] crc;}"
 );
 if (hashLen>0) {
  byte[] hashBlocks = sinStream.readByteArray(hashLen);
  blocks = hashBlocksV2.parse(hashBlocks).mapTo(new org.sinfile.parsers.v2.HashBlocks());
  certLen = sinStream.readInt(JBBPByteOrder.BIG_ENDIAN);
  cert = sinStream.readByteArray(certLen);
  if (blocks.block.length==1 && blocks.block[0].offset!=0) blocks.block[0].offset=0;
  if (blocks.block[0].length==16) {
	  partitioninfo = sinStream.readByteArray(16);
	  JBBPParser partInfo = JBBPParser.prepare(
	            "<int mot1;"
	          + "<int mot2;"
	          + "<int offset;"
	          + "<int blockcount;"
	  );
	  parti = partInfo.parse(partitioninfo).mapTo(new org.sinfile.parsers.v1.PartitionInfo());
	  if (blocks.block.length>1)
		  dataSize=parti.blockcount*blocks.block[1].length;
  }
  blocks.setSpare(this.payloadType);
 }
 dataType=getDataTypePriv();
}
 
Example 11
Source File: SinParser.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void parseHash(JBBPBitInputStream sinStream) throws IOException {
 JBBPParser hashBlocksV2 = JBBPParser.prepare(
           "block[_] {int offset;"
                   + "int length;"
                   + "byte hashLen;"
                   + "byte[hashLen] crc;}"
 );
 if (hashLen>0) {
  byte[] hashBlocks = sinStream.readByteArray(hashLen);
  blocks = hashBlocksV2.parse(hashBlocks).mapTo(new org.sinfile.parsers.v2.HashBlocks());
  certLen = sinStream.readInt(JBBPByteOrder.BIG_ENDIAN);
  cert = sinStream.readByteArray(certLen);
  if (blocks.block.length==1 && blocks.block[0].offset!=0) blocks.block[0].offset=0; 
  if (blocks.block[0].length==16) {
	  byte[] partinfo = sinStream.readByteArray(16);
	  JBBPParser partInfo = JBBPParser.prepare(
	            "<int mot1;"
	          + "<int mot2;"
	          + "<int offset;"
	          + "<int blockcount;"
	  );
	  parti = partInfo.parse(partinfo).mapTo(new org.sinfile.parsers.v1.PartitionInfo());
  }
 }
 dataType=getDataTypePriv();
 dataSize = getDataSizePriv();
}
 
Example 12
Source File: SinParser.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void parseHash(JBBPBitInputStream sinStream) throws IOException {

		  hashBlocks = sinStream.readByteArray(hashLen);
		  certLen = sinStream.readInt(JBBPByteOrder.BIG_ENDIAN);
		  cert = sinStream.readByteArray(certLen);
		  
		  JBBPParser hashBlocksV3 = JBBPParser.prepare(
		            "blocks[_] {int length;"
	              + "byte["+hashv3len[hashType]+"] crc;}"
	      );
		  blocks = hashBlocksV3.parse(hashBlocks).mapTo(new org.sinfile.parsers.v3.HashBlocks());

	  }
 
Example 13
Source File: TARawUnit.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public void fetchContent(JBBPBitInputStream stream) throws IOException {
	  if (magic==0x3BF8E9C1) {
		  unit = new TAUnit(unitNumber,stream.readByteArray(length));
		  if (length % 4 != 0) {
			  stream.skip(4 - length % 4);
		  }
	  }
}
 
Example 14
Source File: TAPPlugin.java    From zxpoly with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ReadResult readFrom(final File file, final int index) throws IOException {
  JBBPBitInputStream in = new JBBPBitInputStream(new FileInputStream(file));
  try {
    int curindex = 0;

    while (in.hasAvailableData()) {
      final int length = in.readUnsignedShort(JBBPByteOrder.LITTLE_ENDIAN);
      final int flag = in.readByte();

      final int offset = (int) in.getCounter();
      final Info info;
      if (flag == 0) {
        // standard rom
        final int standardflag = in.readByte();
        final byte[] data = in.readByteArray(length - 2);
        final int datalen = extractDataLengthField(data);
        final int address = extractStartAddressField(data);

        switch (standardflag) {
          case 0: {
            // program header
            info = new Info(extractHeaderName(data), 'B', address, datalen, offset);
          }
          break;
          case 1: {
            // numeric data array header
            info = new Info(extractHeaderName(data), 'N', address, datalen, offset);
          }
          break;
          case 2: {
            // alphanumeric data array header
            info = new Info(extractHeaderName(data), 'S', address, datalen, offset);
          }
          break;
          case 3: {
            // code block
            info = new Info(extractHeaderName(data), 'C', address, datalen, offset);
          }
          break;
          default: {
            // unknown
            info = new Info("<Unknown>", 'U', address, length, offset);
          }
          break;
        }

        if (curindex < index) {
          curindex++;
        } else {
          throw new IllegalArgumentException("Selected item is not a data block but a header");
        }
      } else {
        if (flag == 0xFF) {
          // data block
          info = new Info("<Code>", 'D', -1, length - 2, offset);
        } else {
          // custom
          info = new Info("<Unknown>", 'U', -1, length, offset);
        }

        if (curindex < index) {
          curindex++;
          in.skip(length - 1);
        } else {
          return new ReadResult(new ZXPolyData(info, this, in.readByteArray(length - 1)), null);
        }
      }
    }
    throw new IllegalArgumentException("Can't find file for index " + index);

  } finally {
    JBBPUtils.closeQuietly(in);
  }
}
 
Example 15
Source File: USBRecord.java    From Flashtool with GNU General Public License v3.0 4 votes vote down vote up
public void parse(JBBPBitInputStream usbStream) throws IOException {

		JBBPParser USBHeaderParser = JBBPParser.prepare(
				"<short usb_Length;" +
				"<short usb_Function;" +
				"<int usb_Status;" +
				"<long usb_UsbDeviceHandle;" +
				"<long usb_UsbdFlags;" +
				"<long usb_PipeHandle;" +
				"<int usb_TransferFlags;" +
				"<int usb_TransferBufferLength;" +
				"<long usb_TransferBuffer;" +
				"<long usb_TransferBufferMDL;" +
				"<long usb_UrbLink;" +
				"<long usb_hcdendpoint;" +
				"<long usb_hcdirp;" +
				"<long usb_hcdlistentry;" +
				"<long usb_flink;" +
				"<long usb_blink;" +
				"<long usb_hcdlistentry2;" +
				"<long usb_hcdcurrentflushpointer;" +
				"<long usb_hcdextension;"
				);

		JBBPParser USBHeaderExtended = JBBPParser.prepare(
				"byte brmRequestType;" + 
				"byte bRequest;" + 
				"<short wValue;" + 
				"<short wIndex;" + 
				"<short wLength;"
				);
		if (recordlength>=128) {
			header = USBHeaderParser.parse(usbStream).mapTo(new USBHeader());
		}
		else {
			usbStream.skip(recordlength);
		}
		try {
			if (header.usb_Length!=128) {
				header=null;
				usbStream.skip(recordlength-128);
			}
			else data = usbStream.readByteArray(recordlength-128);
		} catch (NullPointerException npe) {}
	}