void load_global() { string module = PickleUtils.readline(input); string name = PickleUtils.readline(input); load_global_sub(module, name); }
void load_short_binstring() { byte len = PickleUtils.readbyte(input); byte[] data = PickleUtils.readbytes(input, len); stack.add(PickleUtils.rawStringFromBytes(data)); }
void load_binunicode8() { long len = PickleUtils.bytes_to_long(PickleUtils.readbytes(input, 8), 0); byte[] data = PickleUtils.readbytes(input, len); stack.add(Encoding.UTF8.GetString(data)); }
void load_short_binunicode() { int len = PickleUtils.readbyte(input); byte[] data = PickleUtils.readbytes(input, len); stack.add(Encoding.UTF8.GetString(data)); }
void load_binstring() { int len = PickleUtils.bytes_to_integer(PickleUtils.readbytes(input, 4)); byte[] data = PickleUtils.readbytes(input, len); stack.add(PickleUtils.rawStringFromBytes(data)); }
void load_binunicode() { int len = PickleUtils.bytes_to_integer(PickleUtils.readbytes(input, 4)); byte[] data = PickleUtils.readbytes(input, len); stack.add(Encoding.UTF8.GetString(data)); }
void load_float() { string val = PickleUtils.readline(input, true); double d = double.Parse(val, NumberStyles.Float | NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo); stack.add(d); }
void load_string() { string rep = PickleUtils.readline(input); bool quotesOk = false; foreach (string q in new string[] { "\"", "'" }) // double or single quote { if (rep.StartsWith(q)) { if (!rep.EndsWith(q)) { throw new PickleException("insecure string pickle"); } rep = rep.Substring(1, rep.Length - 2); // strip quotes quotesOk = true; break; } } if (!quotesOk) { throw new PickleException("insecure string pickle"); } stack.add(PickleUtils.decode_escaped(rep)); }
void load_long1() { byte n = PickleUtils.readbyte(input); byte[] data = PickleUtils.readbytes(input, n); stack.add(PickleUtils.decode_long(data)); }
void load_long4() { int n = PickleUtils.bytes_to_integer(PickleUtils.readbytes(input, 4)); byte[] data = PickleUtils.readbytes(input, n); stack.add(PickleUtils.decode_long(data)); }
void load_int() { string data = PickleUtils.readline(input, true); object val; if (data == Opcodes.FALSE.Substring(1)) { val = false; } else if (data == Opcodes.TRUE.Substring(1)) { val = true; } else { string number = data.Substring(0, data.Length - 1); try { val = int.Parse(number); } catch (OverflowException) { // hmm, integer didnt' work.. is it perhaps an int from a 64-bit python? so try long: val = long.Parse(number); } } stack.add(val); }
private void put_float(double d) { outs.WriteByte(Opcodes.BINFLOAT); var output = PickleUtils.double_to_bytes_bigendian(d); outs.Write(output, 0, output.Length); }
/** * Check the memo table and output a memo lookup if the object is found */ private bool LookupMemo(Type objectType, object obj) { if (!useMemo) { return(false); } if (!objectType.IsPrimitive) { int memo_index; if (memo.TryGetValue(obj, out memo_index)) { if (memo_index <= 0xff) { outs.WriteByte(Opcodes.BINGET); outs.WriteByte((byte)memo_index); } else { outs.WriteByte(Opcodes.LONG_BINGET); var index_bytes = PickleUtils.integer_to_bytes(memo_index); outs.Write(index_bytes, 0, 4); } return(true); } } return(false); }
void load_long4() { int n = PickleUtils.bytes_to_integer(pu.readbytes(4)); byte[] data = pu.readbytes(n); stack.add(pu.decode_long(data)); }
private void load_bytearray8() { // this is the same as load_binbytes8 because we make no distinction // here between the bytes and bytearray python types long len = BinaryPrimitives.ReadInt64LittleEndian(input.ReadBytes(sizeof(long))); stack.add(input.ReadBytes(PickleUtils.CheckedCast(len)).ToArray()); }
public ReadOnlySpan <byte> ReadBytes(int bytesCount) { EnsureByteBufferLength(bytesCount); PickleUtils.readbytes_into(input, buffer, 0, bytesCount); return(new ReadOnlySpan <byte>(buffer, 0, bytesCount)); }
void put_string(string str) { byte[] encoded = Encoding.UTF8.GetBytes(str); outs.WriteByte(Opcodes.BINUNICODE); byte[] output = PickleUtils.integer_to_bytes(encoded.Length); outs.Write(output, 0, output.Length); outs.Write(encoded, 0, encoded.Length); WriteMemo(str); }
private void load_proto() { byte proto = PickleUtils.readbyte(input); if (proto > HIGHEST_PROTOCOL) { throw new PickleException("unsupported pickle protocol: " + proto); } }
void load_long_binget() { int i = PickleUtils.bytes_to_integer(PickleUtils.readbytes(input, 4)); if (!memo.ContainsKey(i)) { throw new PickleException("invalid memo key"); } stack.add(memo[i]); }
void load_binget() { byte i = PickleUtils.readbyte(input); if (!memo.ContainsKey(i)) { throw new PickleException("invalid memo key"); } stack.add(memo[(int)i]); }
void load_get() { int i = int.Parse(PickleUtils.readline(input)); if (!memo.ContainsKey(i)) { throw new PickleException("invalid memo key"); } stack.add(memo[i]); }
/** * Pickle a given object graph, writing the result to the output stream. */ public void dump(object o, Stream stream) { outs = stream; utils = new PickleUtils(null); outs.WriteByte(Opcodes.PROTO); outs.WriteByte((byte)PROTOCOL); save(o); outs.WriteByte(Opcodes.STOP); outs.Flush(); }
/** * Read a pickled object representation from the given input stream. * * @return the reconstituted object hierarchy specified in the file. */ public object load(Stream stream) { pu = new PickleUtils(stream); stack = new UnpickleStack(); try { while (true) { byte key = pu.readbyte(); dispatch(key); } } catch (StopException x) { return(x.value); } }
/** * Read a pickled object representation from the given input stream. * * @return the reconstituted object hierarchy specified in the file. */ public object load(Stream stream) { input = stream; stack = new UnpickleStack(); while (true) { byte key = PickleUtils.readbyte(input); object value = dispatch(key); if (value != NO_RETURN_VALUE) { return(value); } } }
/** * Read a pickled object representation from the given input stream. * * @return the reconstituted object hierarchy specified in the file. */ public object load(Stream stream) { input = stream; stack = new UnpickleStack(); try { while (true) { byte key = PickleUtils.readbyte(input); dispatch(key); } } catch (StopException x) { return(x.value); } }
void load_long() { string val = PickleUtils.readline(input); if (val.EndsWith("L")) { val = val.Substring(0, val.Length - 1); } long longvalue; if (long.TryParse(val, out longvalue)) { stack.add(longvalue); } else { throw new PickleException("long too large in load_long (need BigInt)"); } }
void load_inst() { string module = PickleUtils.readline(input); string classname = PickleUtils.readline(input); ArrayList args = stack.pop_all_since_marker(); IObjectConstructor constructor; if (objectConstructors.ContainsKey(module + "." + classname)) { constructor = objectConstructors[module + "." + classname]; } else { constructor = new ClassDictConstructor(module, classname); args.Clear(); // classdict doesn't have constructor args... so we may lose info here, hmm. } object obj = constructor.construct(args.ToArray()); stack.add(obj); }
void put_long(long v) { byte[] output; // choose optimal representation // first check 1 and 2-byte unsigned ints: if (v >= 0) { if (v <= 0xff) { outs.WriteByte(Opcodes.BININT1); outs.WriteByte((byte)v); return; } if (v <= 0xffff) { outs.WriteByte(Opcodes.BININT2); outs.WriteByte((byte)(v & 0xff)); outs.WriteByte((byte)(v >> 8)); return; } } // 4-byte signed int? long high_bits = v >> 31; // shift sign extends if (high_bits == 0 || high_bits == -1) { // All high bits are copies of bit 2**31, so the value fits in a 4-byte signed int. outs.WriteByte(Opcodes.BININT); output = PickleUtils.integer_to_bytes((int)v); outs.Write(output, 0, output.Length); return; } // int too big, store it as text outs.WriteByte(Opcodes.INT); output = Encoding.ASCII.GetBytes("" + v); outs.Write(output, 0, output.Length); outs.WriteByte((byte)'\n'); }
/** * Write the object to the memo table and output a memo write opcode * Only works for hashable objects */ protected void WriteMemo(object obj) { if (!this.useMemo) { return; } if (!memo.ContainsKey(obj)) { int memo_index = memo.Count; memo[obj] = memo_index; if (memo_index <= 0xFF) { outs.WriteByte(Opcodes.BINPUT); outs.WriteByte((byte)memo_index); } else { outs.WriteByte(Opcodes.LONG_BINPUT); byte[] index_bytes = PickleUtils.integer_to_bytes(memo_index); outs.Write(index_bytes, 0, 4); } } }
private void load_int() { int len = PickleUtils.readline_into(input, ref byteBuffer, includeLF: true); object val; if (len == 3 && byteBuffer[2] == (byte)'\n' && byteBuffer[0] == (byte)'0') { if (byteBuffer[1] == (byte)'0') { load_false(); return; } else if (byteBuffer[1] == (byte)'1') { load_true(); return; } } len--; if (len > 0 && Utf8Parser.TryParse(byteBuffer.AsSpan(0, len), out int intNumber, out int bytesConsumed) && bytesConsumed == len) { val = intNumber; }
public void testReadlineWithLF() { Stream bis=new MemoryStream(filedata); PickleUtils p=new PickleUtils(bis); Assert.AreEqual("str1\n", p.readline(true)); Assert.AreEqual("str2 \n", p.readline(true)); Assert.AreEqual(" str3 \n", p.readline(true)); Assert.AreEqual("end", p.readline(true)); try { p.readline(true); Assert.Fail("expected IOException"); } catch(IOException) {} }
public void testReadbytes_into() { Stream bis=new MemoryStream(filedata); PickleUtils p=new PickleUtils(bis); byte[] bytes = new byte[] {0,0,0,0,0,0,0,0,0,0}; p.readbytes_into(bytes, 1, 4); Assert.AreEqual(new byte[] {0,115,116,114,49,0,0,0,0,0}, bytes); p.readbytes_into(bytes, 8, 1); Assert.AreEqual(new byte[] {0,115,116,114,49,0,0,0,10,0}, bytes); }
public void testReadbytes() { Stream bis=new MemoryStream(filedata); PickleUtils p=new PickleUtils(bis); Assert.AreEqual(115, p.readbyte()); Assert.AreEqual(new byte[]{}, p.readbytes(0)); Assert.AreEqual(new byte[]{116}, p.readbytes(1)); Assert.AreEqual(new byte[]{114,49,10,115,116}, p.readbytes(5)); try { p.readbytes(999); Assert.Fail("expected IOException"); } catch(IOException) {} }
public void testInteger_to_bytes() { PickleUtils p=new PickleUtils(null); Assert.AreEqual(new byte[]{0,0,0,0}, p.integer_to_bytes(0)); Assert.AreEqual(new byte[]{0x78, 0x56, 0x34, 0x12}, p.integer_to_bytes(0x12345678)); Assert.AreEqual(new byte[]{0x40, 0x20, 0x80, 0xff}, p.integer_to_bytes(-8380352)); Assert.AreEqual(new byte[]{0xfe, 0xff, 0xff ,0xee}, p.integer_to_bytes(-285212674)); Assert.AreEqual(new byte[]{0xff, 0xff, 0xff, 0xff}, p.integer_to_bytes(-1)); Assert.AreEqual(new byte[]{0xee, 0x02, 0xcc, 0x01}, p.integer_to_bytes(0x01cc02ee)); Assert.AreEqual(new byte[]{0x02, 0xee, 0x01, 0xcc}, p.integer_to_bytes(-872288766)); }
public void testDouble_to_bytes() { PickleUtils p=new PickleUtils(null); Assert.AreEqual(new byte[]{0,0,0,0,0,0,0,0}, p.double_to_bytes(0.0d)); Assert.AreEqual(new byte[]{0x3f,0xf0,0,0,0,0,0,0}, p.double_to_bytes(1.0d)); Assert.AreEqual(new byte[]{0x3f,0xf1,0x99,0x99,0x99,0x99,0x99,0x9a}, p.double_to_bytes(1.1d)); Assert.AreEqual(new byte[]{0x40,0x93,0x4a,0x45,0x6d,0x5c,0xfa,0xad}, p.double_to_bytes(1234.5678d)); Assert.AreEqual(new byte[]{0x59,0x8a,0x42,0xd1,0xce,0xf5,0x3f,0x46}, p.double_to_bytes(2.17e123d)); Assert.AreEqual(new byte[]{0x7e,0x3d,0x7e,0xe8,0xbc,0xaf,0x28,0x3a}, p.double_to_bytes(1.23456789e300d)); // cannot test NaN because it's not always the same byte representation... // Assert.AreEqual(new byte[]{0xff,0xf8,0,0,0,0,0,0}, p.double_to_bytes(Double.NaN)); Assert.AreEqual(new byte[]{0x7f,0xf0,0,0,0,0,0,0}, p.double_to_bytes(Double.PositiveInfinity)); Assert.AreEqual(new byte[]{0xff,0xf0,0,0,0,0,0,0}, p.double_to_bytes(Double.NegativeInfinity)); }
public void testDecode_long() { PickleUtils p=new PickleUtils(null); Assert.AreEqual(0L, p.decode_long(new byte[0])); Assert.AreEqual(0L, p.decode_long(new byte[]{0})); Assert.AreEqual(1L, p.decode_long(new byte[]{1})); Assert.AreEqual(10L, p.decode_long(new byte[]{10})); Assert.AreEqual(255L, p.decode_long(new byte[]{0xff,0x00})); Assert.AreEqual(32767L, p.decode_long(new byte[]{0xff,0x7f})); Assert.AreEqual(-256L, p.decode_long(new byte[]{0x00,0xff})); Assert.AreEqual(-32768L, p.decode_long(new byte[]{0x00,0x80})); Assert.AreEqual(-128L, p.decode_long(new byte[]{0x80})); Assert.AreEqual(127L, p.decode_long(new byte[]{0x7f})); Assert.AreEqual(128L, p.decode_long(new byte[]{0x80, 0x00})); Assert.AreEqual(128L, p.decode_long(new byte[]{0x80, 0x00})); Assert.AreEqual(0x78563412L, p.decode_long(new byte[]{0x12,0x34,0x56,0x78})); Assert.AreEqual(0x785634f2L, p.decode_long(new byte[]{0xf2,0x34,0x56,0x78})); Assert.AreEqual(0x12345678L, p.decode_long(new byte[]{0x78,0x56,0x34,0x12})); Assert.AreEqual(-231451016L, p.decode_long(new byte[]{0x78,0x56,0x34,0xf2})); Assert.AreEqual(0xf2345678L, p.decode_long(new byte[]{0x78,0x56,0x34,0xf2,0x00})); }
/** * Read a pickled object representation from the given input stream. * * @return the reconstituted object hierarchy specified in the file. */ public object load(Stream stream) { pu = new PickleUtils(stream); stack = new UnpickleStack(); try { while (true) { byte key = pu.readbyte(); dispatch(key); } } catch (StopException x) { return x.value; } }