📄 struct.java
字号:
buf.writeByte((int)(v >>> 0) & 0xFF); } void LEwriteInt(ByteStream buf, int v) { buf.writeByte((int)(v >>> 0) & 0xFF); buf.writeByte((int)(v >>> 8) & 0xFF); buf.writeByte((int)(v >>> 16) & 0xFF); buf.writeByte((int)(v >>> 24) & 0xFF); } int BEreadInt(ByteStream buf) { int b1 = buf.readByte(); int b2 = buf.readByte(); int b3 = buf.readByte(); int b4 = buf.readByte(); return ((b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0)); } int LEreadInt(ByteStream buf) { int b1 = buf.readByte(); int b2 = buf.readByte(); int b3 = buf.readByte(); int b4 = buf.readByte(); return ((b1 << 0) + (b2 << 8) + (b3 << 16) + (b4 << 24)); } } static class ByteStream { char[] data; int len; int pos; ByteStream() { data = new char[10]; len = 0; pos = 0; } ByteStream(String s) { int l = s.length(); data = new char[l]; s.getChars(0, l, data, 0); len = l; pos = 0; } int readByte() { return data[pos++] & 0xFF; } void read(char[] buf, int pos, int len) { System.arraycopy(data, this.pos, buf, pos, len); this.pos += len; } String readString(int l) { char[] data = new char[l]; read(data, 0, l); return new String(data); } private void ensureCapacity(int l) { if (pos + l >= data.length) { char[] b = new char[(pos + l) * 2]; System.arraycopy(data, 0, b, 0, pos); data = b; } } void writeByte(int b) { ensureCapacity(1); data[pos++] = (char)(b & 0xFF); } void write(char[] buf, int pos, int len) { ensureCapacity(len); System.arraycopy(buf, pos, data, this.pos, len); this.pos += len; } void writeString(String s, int pos, int len) { char[] data = new char[len]; s.getChars(pos, len, data, 0); write(data, 0, len); } int skip(int l) { pos += l; return pos; } int size() { return pos; } public String toString() { return new String(data, 0, pos); } } static class PadFormatDef extends FormatDef { int doPack(ByteStream buf, int count, int pos, PyObject[] args) { while (count-- > 0) buf.writeByte(0); return 0; } void doUnpack(ByteStream buf, int count, PyList list) { while (count-- > 0) buf.readByte(); } } static class StringFormatDef extends FormatDef { int doPack(ByteStream buf, int count, int pos, PyObject[] args) { PyObject value = args[pos]; if (!(value instanceof PyString)) throw StructError("argument for 's' must be a string"); String s = value.toString(); int len = s.length(); buf.writeString(s, 0, Math.min(count, len)); if (len < count) { count -= len; for (int i = 0; i < count; i++) buf.writeByte(0); } return 1; } void doUnpack(ByteStream buf, int count, PyList list) { list.append(Py.newString(buf.readString(count))); } } static class PascalStringFormatDef extends StringFormatDef { int doPack(ByteStream buf, int count, int pos, PyObject[] args) { PyObject value = args[pos]; if (!(value instanceof PyString)) throw StructError("argument for 'p' must be a string"); buf.writeByte(Math.min(value.toString().length(), count-1)); return super.doPack(buf, count-1, pos, args); } void doUnpack(ByteStream buf, int count, PyList list) { int n = buf.readByte(); if (n >= count) n = count-1; super.doUnpack(buf, n, list); buf.skip(Math.max(count-n-1, 0)); } } static class CharFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { if (!(value instanceof PyString) || value.__len__() != 1) throw StructError("char format require string of length 1"); buf.writeByte(value.toString().charAt(0)); } Object unpack(ByteStream buf) { return Py.newString((char)buf.readByte()); } } static class ByteFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { buf.writeByte(get_int(value)); } Object unpack(ByteStream buf) { int b = buf.readByte(); if (b > Byte.MAX_VALUE) b -= 0x100; return Py.newInteger(b); } } static class UnsignedByteFormatDef extends ByteFormatDef { Object unpack(ByteStream buf) { return Py.newInteger(buf.readByte()); } } static class LEShortFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { int v = get_int(value); buf.writeByte(v & 0xFF); buf.writeByte((v >> 8) & 0xFF); } Object unpack(ByteStream buf) { int v = buf.readByte() | (buf.readByte() << 8); if (v > Short.MAX_VALUE) v -= 0x10000 ; return Py.newInteger(v); } } static class LEUnsignedShortFormatDef extends LEShortFormatDef { Object unpack(ByteStream buf) { int v = buf.readByte() | (buf.readByte() << 8); return Py.newInteger(v); } } static class BEShortFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { int v = get_int(value); buf.writeByte((v >> 8) & 0xFF); buf.writeByte(v & 0xFF); } Object unpack(ByteStream buf) { int v = (buf.readByte() << 8) | buf.readByte(); if (v > Short.MAX_VALUE) v -= 0x10000; return Py.newInteger(v); } } static class BEUnsignedShortFormatDef extends BEShortFormatDef { Object unpack(ByteStream buf) { int v = (buf.readByte() << 8) | buf.readByte(); return Py.newInteger(v); } } static class LEIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { LEwriteInt(buf, get_int(value)); } Object unpack(ByteStream buf) { int v = LEreadInt(buf); return Py.newInteger(v); } } static class LEUnsignedIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { LEwriteInt(buf, (int)(get_ulong(value) & 0xFFFFFFFF)); } Object unpack(ByteStream buf) { long v = LEreadInt(buf); if (v < 0) v += 0x100000000L; return new PyLong(v); } } static class BEIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { BEwriteInt(buf, get_int(value)); } Object unpack(ByteStream buf) { return Py.newInteger(BEreadInt(buf)); } } static class BEUnsignedIntFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { BEwriteInt(buf, (int)(get_ulong(value) & 0xFFFFFFFF)); } Object unpack(ByteStream buf) { long v = BEreadInt(buf); if (v < 0) v += 0x100000000L; return new PyLong(v); } } static class LEUnsignedLongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long lvalue = get_ulong( value ); if (lvalue < 0) { throw StructError("can't convert negative long to unsigned"); } int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); LEwriteInt( buf, low ); LEwriteInt( buf, high ); } Object unpack(ByteStream buf) { long low = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL ); long high = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL ); java.math.BigInteger result=java.math.BigInteger.valueOf(high); result=result.multiply(java.math.BigInteger.valueOf(0x100000000L)); result=result.add(java.math.BigInteger.valueOf(low)); return new PyLong(result); } } static class BEUnsignedLongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long lvalue = get_ulong( value ); if (lvalue < 0) { throw StructError("can't convert negative long to unsigned"); } int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 ); int low = (int) ( lvalue & 0x00000000FFFFFFFFL ); BEwriteInt( buf, high ); BEwriteInt( buf, low ); } Object unpack(ByteStream buf) { long high = ( BEreadInt( buf ) & 0X00000000FFFFFFFFL ); long low = ( BEreadInt( buf ) & 0X00000000FFFFFFFFL ); java.math.BigInteger result=java.math.BigInteger.valueOf(high); result=result.multiply(java.math.BigInteger.valueOf(0x100000000L)); result=result.add(java.math.BigInteger.valueOf(low)); return new PyLong(result); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -