📄 struct.java
字号:
} static class LELongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long lvalue = get_ulong( value ); 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 )<<32)&(0xFFFFFFFF00000000L) ); long result=(high|low); return new PyLong(result); } } static class BELongFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long lvalue = get_ulong( value ); 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 )<<32)&(0xFFFFFFFF00000000L) ); long low= ( BEreadInt( buf )&(0x00000000FFFFFFFFL) ); long result=(high|low); return new PyLong(result); } } static class LEFloatFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { int bits = Float.floatToIntBits((float)get_float(value)); LEwriteInt(buf, bits); } Object unpack(ByteStream buf) { int v = LEreadInt(buf); return Py.newFloat(Float.intBitsToFloat(v)); } } static class LEDoubleFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long bits = Double.doubleToLongBits(get_float(value)); LEwriteInt(buf, (int)(bits & 0xFFFFFFFF)); LEwriteInt(buf, (int)(bits >>> 32)); } Object unpack(ByteStream buf) { long bits = (LEreadInt(buf) & 0xFFFFFFFFL) + (((long)LEreadInt(buf)) << 32); return Py.newFloat(Double.longBitsToDouble(bits)); } } static class BEFloatFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { int bits = Float.floatToIntBits((float)get_float(value)); BEwriteInt(buf, bits); } Object unpack(ByteStream buf) { int v = BEreadInt(buf); return Py.newFloat(Float.intBitsToFloat(v)); } } static class BEDoubleFormatDef extends FormatDef { void pack(ByteStream buf, PyObject value) { long bits = Double.doubleToLongBits(get_float(value)); BEwriteInt(buf, (int)(bits >>> 32)); BEwriteInt(buf, (int)(bits & 0xFFFFFFFF)); } Object unpack(ByteStream buf) { long bits = (((long)BEreadInt(buf)) << 32) + (BEreadInt(buf) & 0xFFFFFFFFL); return Py.newFloat(Double.longBitsToDouble(bits)); } } private static FormatDef[] lilendian_table = { new PadFormatDef() .init('x', 1, 0), new ByteFormatDef() .init('b', 1, 0), new UnsignedByteFormatDef() .init('B', 1, 0), new CharFormatDef() .init('c', 1, 0), new StringFormatDef() .init('s', 1, 0), new PascalStringFormatDef() .init('p', 1, 0), new LEShortFormatDef() .init('h', 2, 0), new LEUnsignedShortFormatDef() .init('H', 2, 0), new LEIntFormatDef() .init('i', 4, 0), new LEUnsignedIntFormatDef() .init('I', 4, 0), new LEIntFormatDef() .init('l', 4, 0), new LEUnsignedIntFormatDef() .init('L', 4, 0), new LELongFormatDef() .init('q', 8, 8), new LEUnsignedLongFormatDef() .init('Q', 8, 8), new LEFloatFormatDef() .init('f', 4, 0), new LEDoubleFormatDef() .init('d', 8, 0), }; private static FormatDef[] bigendian_table = { new PadFormatDef() .init('x', 1, 0), new ByteFormatDef() .init('b', 1, 0), new UnsignedByteFormatDef() .init('B', 1, 0), new CharFormatDef() .init('c', 1, 0), new StringFormatDef() .init('s', 1, 0), new PascalStringFormatDef() .init('p', 1, 0), new BEShortFormatDef() .init('h', 2, 0), new BEUnsignedShortFormatDef() .init('H', 2, 0), new BEIntFormatDef() .init('i', 4, 0), new BEUnsignedIntFormatDef() .init('I', 4, 0), new BEIntFormatDef() .init('l', 4, 0), new BEUnsignedIntFormatDef() .init('L', 4, 0), new BELongFormatDef() .init('q', 8, 8), new BEUnsignedLongFormatDef() .init('Q', 8, 8), new BEFloatFormatDef() .init('f', 4, 0), new BEDoubleFormatDef() .init('d', 8, 0), }; private static FormatDef[] native_table = { new PadFormatDef() .init('x', 1, 0), new ByteFormatDef() .init('b', 1, 0), new UnsignedByteFormatDef() .init('B', 1, 0), new CharFormatDef() .init('c', 1, 0), new StringFormatDef() .init('s', 1, 0), new PascalStringFormatDef() .init('p', 1, 0), new BEShortFormatDef() .init('h', 2, 2), new BEUnsignedShortFormatDef() .init('H', 2, 2), new BEIntFormatDef() .init('i', 4, 4), new BEUnsignedIntFormatDef() .init('I', 4, 4), new BEIntFormatDef() .init('l', 4, 4), new BEUnsignedIntFormatDef() .init('L', 4, 4), new BELongFormatDef() .init('q', 8, 8), new BEUnsignedLongFormatDef() .init('Q', 8, 8), new BEFloatFormatDef() .init('f', 4, 4), new BEDoubleFormatDef() .init('d', 8, 8), }; private static FormatDef[] whichtable(String pfmt) { char c = pfmt.charAt(0); switch (c) { case '<' : return lilendian_table; case '>': case '!': // Network byte order is big-endian return bigendian_table; case '=': return bigendian_table; case '@': default: return native_table; } } private static FormatDef getentry(char c, FormatDef[] f) { for (int i = 0; i < f.length; i++) { if (f[i].name == c) return f[i]; } throw StructError("bad char in struct format"); } private static int align(int size, FormatDef e) { if (e.alignment != 0) { size = ((size + e.alignment - 1) / e.alignment) * e.alignment; } return size; } private static int calcsize(String format, FormatDef[] f) { int size = 0; int len = format.length(); for (int j = 0; j < len; j++) { char c = format.charAt(j); if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!')) continue; if (Character.isWhitespace(c)) continue; int num = 1; if (Character.isDigit(c)) { num = Character.digit(c, 10); while (++j < len && Character.isDigit((c = format.charAt(j)))) { int x = num*10 + Character.digit(c, 10); if (x/10 != num) throw StructError("overflow in item count"); num = x; } if (j >= len) break; } FormatDef e = getentry(c, f); int itemsize = e.size; size = align(size, e); int x = num * itemsize; size += x; if (x/itemsize != num || size < 0) throw StructError("total struct size too long"); } return size; } /** * Return the size of the struct (and hence of the string) * corresponding to the given format. */ static public int calcsize(String format) { FormatDef[] f = whichtable(format); return calcsize(format, f); } /** * Return a string containing the values v1, v2, ... packed according * to the given format. The arguments must match the * values required by the format exactly. */ static public String pack(PyObject[] args) { if (args.length < 1) Py.TypeError("illegal argument type for built-in operation"); String format = args[0].toString(); FormatDef[] f = whichtable(format); int size = calcsize(format, f); ByteStream res = new ByteStream(); int i = 1; int len = format.length(); for (int j = 0; j < len; j++) { char c = format.charAt(j); if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!')) continue; if (Character.isWhitespace(c)) continue; int num = 1; if (Character.isDigit(c)) { num = Character.digit(c, 10); while (++j < len && Character.isDigit((c = format.charAt(j)))) num = num*10 + Character.digit(c, 10); if (j >= len) break; } FormatDef e = getentry(c, f); // Fill padd bytes with zeros int nres = align(res.size(), e) - res.size(); while (nres-- > 0) res.writeByte(0); i += e.doPack(res, num, i, args); } if (i < args.length) throw StructError("too many arguments for pack format"); return res.toString(); } /** * Unpack the string (presumably packed by pack(fmt, ...)) according * to the given format. The result is a tuple even if it contains * exactly one item. * The string must contain exactly the amount of data required by * the format (i.e. len(string) must equal calcsize(fmt)). */ public static PyTuple unpack(String format, String string) { int len = string.length(); FormatDef[] f = whichtable(format); int size = calcsize(format, f); if (size != len) throw StructError("unpack str size does not match format"); PyList res = new PyList(); ByteStream str = new ByteStream(string); int flen = format.length(); for (int j = 0; j < flen; j++) { char c = format.charAt(j); if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!')) continue; if (Character.isWhitespace(c)) continue; int num = 1; if (Character.isDigit(c)) { num = Character.digit(c, 10); while (++j < flen && Character.isDigit((c = format.charAt(j)))) num = num*10 + Character.digit(c, 10); if (j > flen) break; } FormatDef e = getentry(c, f); str.skip(align(str.size(), e) - str.size()); e.doUnpack(str, num, res); } return __builtin__.tuple(res); } private static PyException StructError(String explanation) { return new PyException(error, explanation); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -