예제 #1
0
        public void testBytes_to_double()
        {
            try {
                PickleUtils.bytes_to_double(new byte[] {}, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            try {
                PickleUtils.bytes_to_double(new byte[] { 0 }, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}
            Assert.AreEqual(0.0d, PickleUtils.bytes_to_double(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(1.0d, PickleUtils.bytes_to_double(new byte[] { 0x3f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(1.1d, PickleUtils.bytes_to_double(new byte[] { 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a }, 0));
            Assert.AreEqual(1234.5678d, PickleUtils.bytes_to_double(new byte[] { 0x40, 0x93, 0x4a, 0x45, 0x6d, 0x5c, 0xfa, 0xad }, 0));
            Assert.AreEqual(2.17e123d, PickleUtils.bytes_to_double(new byte[] { 0x59, 0x8a, 0x42, 0xd1, 0xce, 0xf5, 0x3f, 0x46 }, 0));
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 0));
            Assert.AreEqual(double.PositiveInfinity, PickleUtils.bytes_to_double(new byte[] { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            Assert.AreEqual(double.NegativeInfinity, PickleUtils.bytes_to_double(new byte[] { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 }, 0));
            try
            {
                PickleUtils.bytes_to_double(new byte[] { 200, 50, 25, 100 }, 0);
                Assert.Fail("expected PickleException");
            } catch (PickleException) {}

            // test offset
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0, 0, 0, 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, 3));
            Assert.AreEqual(1.23456789e300d, PickleUtils.bytes_to_double(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a, 0, 0, 0 }, 0));
        }
예제 #2
0
 protected long[] constructLongArrayFromInt64(int machinecode, byte[] data)
 {
     long[] result    = new long[data.Length / 8];
     byte[] bigendian = new byte[8];
     for (int i = 0; i < data.Length / 8; i++)
     {
         if (machinecode == 12)
         {
             // little endian can go
             result[i] = PickleUtils.bytes_to_long(data, i * 8);
         }
         else
         {
             // 13=big endian, swap
             bigendian[0] = data[7 + i * 8];
             bigendian[1] = data[6 + i * 8];
             bigendian[2] = data[5 + i * 8];
             bigendian[3] = data[4 + i * 8];
             bigendian[4] = data[3 + i * 8];
             bigendian[5] = data[2 + i * 8];
             bigendian[6] = data[1 + i * 8];
             bigendian[7] = data[0 + i * 8];
             result[i]    = PickleUtils.bytes_to_long(bigendian, 0);
         }
     }
     return(result);
 }
예제 #3
0
 public void testBytes_to_integer()
 {
     try {
         PickleUtils.bytes_to_integer(new byte[] {});
         Assert.Fail("expected PickleException");
     } catch (PickleException) {}
     try {
         PickleUtils.bytes_to_integer(new byte[] { 0 });
         Assert.Fail("expected PickleException");
     } catch (PickleException) {}
     Assert.AreEqual(0x00000000, PickleUtils.bytes_to_integer(new byte[] { 0x00, 0x00 }));
     Assert.AreEqual(0x00003412, PickleUtils.bytes_to_integer(new byte[] { 0x12, 0x34 }));
     Assert.AreEqual(0x0000ffff, PickleUtils.bytes_to_integer(new byte[] { 0xff, 0xff }));
     Assert.AreEqual(0x00000000, PickleUtils.bytes_to_integer(new byte[] { 0, 0, 0, 0 }));
     Assert.AreEqual(0x12345678, PickleUtils.bytes_to_integer(new byte[] { 0x78, 0x56, 0x34, 0x12 }));
     Assert.AreEqual(-8380352, PickleUtils.bytes_to_integer(new byte[] { 0x40, 0x20, 0x80, 0xff }));
     Assert.AreEqual(0x01cc02ee, PickleUtils.bytes_to_integer(new byte[] { 0xee, 0x02, 0xcc, 0x01 }));
     Assert.AreEqual(-872288766, PickleUtils.bytes_to_integer(new byte[] { 0x02, 0xee, 0x01, 0xcc }));
     Assert.AreEqual(-285212674, PickleUtils.bytes_to_integer(new byte[] { 0xfe, 0xff, 0xff, 0xee }));
     try
     {
         PickleUtils.bytes_to_integer(new byte[] { 200, 50, 25, 100, 1, 2, 3, 4 });
         Assert.Fail("expected PickleException");
     } catch (PickleException) {}
 }
예제 #4
0
 protected double[] constructDoubleArray(int machinecode, byte[] data)
 {
     double[] result    = new double[data.Length / 8];
     byte[]   bigendian = new byte[8];
     for (int i = 0; i < data.Length / 8; ++i)
     {
         if (machinecode == 16)
         {
             result[i] = PickleUtils.bytes_to_double(data, i * 8);
         }
         else
         {
             // 17=big endian, flip the bytes
             bigendian[0] = data[7 + i * 8];
             bigendian[1] = data[6 + i * 8];
             bigendian[2] = data[5 + i * 8];
             bigendian[3] = data[4 + i * 8];
             bigendian[4] = data[3 + i * 8];
             bigendian[5] = data[2 + i * 8];
             bigendian[6] = data[1 + i * 8];
             bigendian[7] = data[0 + i * 8];
             result[i]    = PickleUtils.bytes_to_double(bigendian, 0);
         }
     }
     return(result);
 }
예제 #5
0
 protected char[] constructCharArrayUTF32(int machinecode, byte[] data)
 {
     char[] result    = new char[data.Length / 4];
     byte[] bigendian = new byte[4];
     for (int index = 0; index < data.Length / 4; ++index)
     {
         if (machinecode == 20)
         {
             int    codepoint = PickleUtils.bytes_to_integer(data, index * 4, 4);
             string cc        = char.ConvertFromUtf32(codepoint);
             if (cc.Length > 1)
             {
                 throw new PickleException("cannot process UTF-32 character codepoint " + codepoint);
             }
             result[index] = cc[0];
         }
         else
         {
             // big endian, swap
             bigendian[0] = data[3 + index * 4];
             bigendian[1] = data[2 + index * 4];
             bigendian[2] = data[1 + index * 4];
             bigendian[3] = data[index * 4];
             int    codepoint = PickleUtils.bytes_to_integer(bigendian);
             string cc        = char.ConvertFromUtf32(codepoint);
             if (cc.Length > 1)
             {
                 throw new PickleException("cannot process UTF-32 character codepoint " + codepoint);
             }
             result[index] = cc[0];
         }
     }
     return(result);
 }
예제 #6
0
        protected float[] constructFloatArray(int machinecode, byte[] data)
        {
            var result = new float[data.Length / 4];

            if (machinecode == 15)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = PickleUtils.bytes_bigendian_to_float(data, i * 4);
                }
            }
            else
            {
                var bigendian = new byte[4];
                for (int i = 0; i < result.Length; i++)
                {
                    // 14=little endian, flip the bytes
                    bigendian[0] = data[3 + i * 4];
                    bigendian[1] = data[2 + i * 4];
                    bigendian[2] = data[1 + i * 4];
                    bigendian[3] = data[0 + i * 4];
                    result[i]    = PickleUtils.bytes_bigendian_to_float(bigendian, 0);
                }
            }
            return(result);
        }
예제 #7
0
        protected double[] constructDoubleArray(int machinecode, byte[] data)
        {
            var result = new double[data.Length / 8];

            if (machinecode == 17)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = PickleUtils.bytes_bigendian_to_double(data, i * 8);
                }
            }
            else
            {
                var bigendian = new byte[8];
                for (int i = 0; i < result.Length; i++)
                {
                    // 16=little endian, flip the bytes
                    bigendian[0] = data[7 + i * 8];
                    bigendian[1] = data[6 + i * 8];
                    bigendian[2] = data[5 + i * 8];
                    bigendian[3] = data[4 + i * 8];
                    bigendian[4] = data[3 + i * 8];
                    bigendian[5] = data[2 + i * 8];
                    bigendian[6] = data[1 + i * 8];
                    bigendian[7] = data[0 + i * 8];
                    result[i]    = PickleUtils.bytes_bigendian_to_double(bigendian, 0);
                }
            }
            return(result);
        }
예제 #8
0
        protected uint[] constructUIntArrayFromUInt32(int machinecode, byte[] data)
        {
            var result = new uint[data.Length / 4];

            if (machinecode == 6)
            {
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = PickleUtils.bytes_to_uint(data, i * 4);
                }
            }
            else
            {
                var bigendian = new byte[4];
                for (int i = 0; i < result.Length; i++)
                {
                    // big endian, swap
                    bigendian[0] = data[3 + i * 4];
                    bigendian[1] = data[2 + i * 4];
                    bigendian[2] = data[1 + i * 4];
                    bigendian[3] = data[0 + i * 4];
                    result[i]    = PickleUtils.bytes_to_uint(bigendian, 0);
                }
            }
            return(result);
        }
예제 #9
0
        public void TestBinint2WithObject()
        {
            Unpickler u      = new Unpickler();
            var       data   = PickleUtils.str2bytes("\u0080\u0002cIgnore.Ignore\nIgnore\n)\u0081M\u0082#.");
            int       result = (int)u.loads(data);

            Assert.Equal(9090, result);
        }
예제 #10
0
        public void TestBYTEARRAY8()
        {
            // BYTEARRAY8 = 0x96 (pickle protocol 5)
            Unpickler u = new Unpickler();

            byte[] data   = PickleUtils.str2bytes("\u0080\u0005\u0095\u000e\u0000\u0000\u0000\u0000\u0000\u0000\u0090\u0096\u0003\u0000\u0000\u0000\u0000\u0000\u0000\u0000abc\u0094.");
            byte[] result = (byte[])u.loads(data);
            Assert.Equal(new byte[] { 97, 98, 99 }, result);
        }
예제 #11
0
 public void TestDecode_unicode_escaped()
 {
     Assert.Equal("abc", PickleUtils.decode_unicode_escaped("abc"));
     Assert.Equal("a\\c", PickleUtils.decode_unicode_escaped("a\\\\c"));
     Assert.Equal("a\u0042c", PickleUtils.decode_unicode_escaped("a\\u0042c"));
     Assert.Equal("a\nc", PickleUtils.decode_unicode_escaped("a\\nc"));
     Assert.Equal("a\tc", PickleUtils.decode_unicode_escaped("a\\tc"));
     Assert.Equal("a\rc", PickleUtils.decode_unicode_escaped("a\\rc"));
 }
예제 #12
0
 public void testDecode_escaped()
 {
     Assert.AreEqual("abc", PickleUtils.decode_escaped("abc"));
     Assert.AreEqual("a\\c", PickleUtils.decode_escaped("a\\\\c"));
     Assert.AreEqual("a\u0042c", PickleUtils.decode_escaped("a\\x42c"));
     Assert.AreEqual("a\nc", PickleUtils.decode_escaped("a\\nc"));
     Assert.AreEqual("a\tc", PickleUtils.decode_escaped("a\\tc"));
     Assert.AreEqual("a\rc", PickleUtils.decode_escaped("a\\rc"));
     Assert.AreEqual("a'c", PickleUtils.decode_escaped("a\\'c"));
 }
예제 #13
0
        public void testBinint2WithObject()
        {
            Unpickler u = new Unpickler();

            Unpickler.registerConstructor("Pyro4.core", "URI", new StringConstructor());
            byte[] data   = PickleUtils.str2bytes("\u0080\u0002cPyro4.core\nURI\n)\u0081M\u0082#.");
            int    result = (int)u.loads(data);

            Assert.AreEqual(9090, result);
        }
예제 #14
0
 public void TestInteger_to_bytes()
 {
     Assert.Equal(new byte[] { 0, 0, 0, 0 }, PickleUtils.integer_to_bytes(0));
     Assert.Equal(new byte[] { 0x78, 0x56, 0x34, 0x12 }, PickleUtils.integer_to_bytes(0x12345678));
     Assert.Equal(new byte[] { 0x40, 0x20, 0x80, 0xff }, PickleUtils.integer_to_bytes(-8380352));
     Assert.Equal(new byte[] { 0xfe, 0xff, 0xff, 0xee }, PickleUtils.integer_to_bytes(-285212674));
     Assert.Equal(new byte[] { 0xff, 0xff, 0xff, 0xff }, PickleUtils.integer_to_bytes(-1));
     Assert.Equal(new byte[] { 0xee, 0x02, 0xcc, 0x01 }, PickleUtils.integer_to_bytes(0x01cc02ee));
     Assert.Equal(new byte[] { 0x02, 0xee, 0x01, 0xcc }, PickleUtils.integer_to_bytes(-872288766));
 }
예제 #15
0
        public void TestReadbytes_into()
        {
            Stream bis = new MemoryStream(_filedata);

            byte[] bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            PickleUtils.readbytes_into(bis, bytes, 1, 4);
            Assert.Equal(new byte[] { 0, 115, 116, 114, 49, 0, 0, 0, 0, 0 }, bytes);
            PickleUtils.readbytes_into(bis, bytes, 8, 1);
            Assert.Equal(new byte[] { 0, 115, 116, 114, 49, 0, 0, 0, 10, 0 }, bytes);
        }
예제 #16
0
        public void TestBINPERSID()
        {
            //BINPERSID      = b'Q'   #  push persistent object; id is taken from stack
            var       pickle    = PickleUtils.str2bytes("\u0080\u0004\u0095\u000f\u0000\u0000\u0000\u0000\u0000\u0000\u0000]\u0094(K*\u008c\u00049999\u0094Qe.");
            Unpickler unpickler = new PersistentIdUnpickler();
            IList     result    = (IList)unpickler.loads(pickle);

            Assert.Equal(2, result.Count);
            Assert.Equal(42, result[0]);
            Assert.Equal("PersistentObject", result[1]);
        }
예제 #17
0
        public void TestPERSID()
        {
            //PERSID         = b'P'   # push persistent object; id is taken from string arg
            var       pickle    = PickleUtils.str2bytes("(lp0\nI42\naP9999\na.");
            Unpickler unpickler = new PersistentIdUnpickler();
            IList     result    = (IList)unpickler.loads(pickle);

            Assert.Equal(2, result.Count);
            Assert.Equal(42, result[0]);
            Assert.Equal("PersistentObject", result[1]);
        }
예제 #18
0
        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);
        }
예제 #19
0
        public void TestProtocol5bytearray()
        {
            // ["string", bytearray(b'irmen')]  ->  produces a protocol 5 pickle with opcode BYTEARRAY8
            Unpickler u = new Unpickler();

            byte[]    data   = PickleUtils.str2bytes("\u0080\u0005\u0095\u001d\u0000\u0000\u0000\u0000\u0000\u0000\u0000]\u0094(\u008c\u0006string\u0094\u0096\u0005\u0000\u0000\u0000\u0000\u0000\u0000\u0000irmen\u0094e.");
            ArrayList result = (ArrayList)u.loads(data);

            Assert.Equal(2, result.Count);
            Assert.Equal("string", result[0]);
            Assert.Equal(new byte[] { 105, 114, 109, 101, 110 }, result[1]);
        }
예제 #20
0
 public void TestDouble_to_bytes()
 {
     Assert.Equal(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }, PickleUtils.double_to_bytes_bigendian(0.0d));
     Assert.Equal(new byte[] { 0x3f, 0xf0, 0, 0, 0, 0, 0, 0 }, PickleUtils.double_to_bytes_bigendian(1.0d));
     Assert.Equal(new byte[] { 0x3f, 0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a }, PickleUtils.double_to_bytes_bigendian(1.1d));
     Assert.Equal(new byte[] { 0x40, 0x93, 0x4a, 0x45, 0x6d, 0x5c, 0xfa, 0xad }, PickleUtils.double_to_bytes_bigendian(1234.5678d));
     Assert.Equal(new byte[] { 0x59, 0x8a, 0x42, 0xd1, 0xce, 0xf5, 0x3f, 0x46 }, PickleUtils.double_to_bytes_bigendian(2.17e123d));
     Assert.Equal(new byte[] { 0x7e, 0x3d, 0x7e, 0xe8, 0xbc, 0xaf, 0x28, 0x3a }, PickleUtils.double_to_bytes_bigendian(1.23456789e300d));
     // cannot test NaN because it's not always the same byte representation...
     // Assert.Equal(new byte[]{0xff,0xf8,0,0,0,0,0,0}, p.double_to_bytes(Double.NaN));
     Assert.Equal(new byte[] { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }, PickleUtils.double_to_bytes_bigendian(double.PositiveInfinity));
     Assert.Equal(new byte[] { 0xff, 0xf0, 0, 0, 0, 0, 0, 0 }, PickleUtils.double_to_bytes_bigendian(double.NegativeInfinity));
 }
예제 #21
0
 public void testStr2Bytes()
 {
     byte[] bytes = PickleUtils.str2bytes(STRING256);
     for (int i = 0; i < 256; ++i)
     {
         int b = bytes[i];
         if (b < 0)
         {
             b += 256;
         }
         Assert.AreEqual(i, b, "byte@" + i);
     }
 }
예제 #22
0
        public void TestStr2Bytes()
        {
            var bytes = PickleUtils.str2bytes(STRING256);

            for (int i = 0; i < 256; ++i)
            {
                int b = bytes[i];
                if (b < 0)
                {
                    b += 256;
                }
                Assert.Equal(i, b);          //"byte@"+i
            }
        }
예제 #23
0
        public void testReadline()
        {
            Stream bis = new MemoryStream(filedata);

            Assert.Equal("str1", PickleUtils.readline(bis));
            Assert.Equal("str2  ", PickleUtils.readline(bis));
            Assert.Equal("  str3  ", PickleUtils.readline(bis));
            Assert.Equal("end", PickleUtils.readline(bis));
            try
            {
                PickleUtils.readline(bis);
                Assert.True(false, "expected IOException");
            }
            catch (IOException) {}
        }
예제 #24
0
        public void TestReadbytes()
        {
            Stream bis = new MemoryStream(_filedata);

            Assert.Equal(115, PickleUtils.readbyte(bis));
            Assert.Equal(new byte[] {}, PickleUtils.readbytes(bis, 0));
            Assert.Equal(new byte[] { 116 }, PickleUtils.readbytes(bis, 1));
            Assert.Equal(new byte[] { 114, 49, 10, 115, 116 }, PickleUtils.readbytes(bis, 5));
            try
            {
                PickleUtils.readbytes(bis, 999);
                Assert.True(false, "expected IOException");
            }
            catch (IOException) {}
        }
예제 #25
0
        public void TestReadlineWithLf()
        {
            Stream bis = new MemoryStream(_filedata);

            Assert.Equal("str1\n", PickleUtils.readline(bis, true));
            Assert.Equal("str2  \n", PickleUtils.readline(bis, true));
            Assert.Equal("  str3  \n", PickleUtils.readline(bis, true));
            Assert.Equal("end", PickleUtils.readline(bis, true));
            try
            {
                PickleUtils.readline(bis, true);
                Assert.True(false, "expected IOException");
            }
            catch (IOException) {}
        }
예제 #26
0
 public void TestBytes_to_uint()
 {
     try {
         PickleUtils.bytes_to_uint(new byte[] {}, 0);
         Assert.True(false, "expected PickleException");
     } catch (PickleException) {}
     try {
         PickleUtils.bytes_to_uint(new byte[] { 0 }, 0);
         Assert.True(false, "expected PickleException");
     } catch (PickleException) {}
     Assert.Equal(0x000000000L, PickleUtils.bytes_to_uint(new byte[] { 0, 0, 0, 0 }, 0));
     Assert.Equal(0x012345678L, PickleUtils.bytes_to_uint(new byte[] { 0x78, 0x56, 0x34, 0x12 }, 0));
     Assert.Equal(0x0ff802040L, PickleUtils.bytes_to_uint(new byte[] { 0x40, 0x20, 0x80, 0xff }, 0));
     Assert.Equal(0x0eefffffeL, PickleUtils.bytes_to_uint(new byte[] { 0xfe, 0xff, 0xff, 0xee }, 0));
 }
예제 #27
0
        public void testReadlineWithLF()
        {
            Stream bis = new MemoryStream(filedata);

            Assert.AreEqual("str1\n", PickleUtils.readline(bis, true));
            Assert.AreEqual("str2  \n", PickleUtils.readline(bis, true));
            Assert.AreEqual("  str3  \n", PickleUtils.readline(bis, true));
            Assert.AreEqual("end", PickleUtils.readline(bis, true));
            try
            {
                PickleUtils.readline(bis, true);
                Assert.Fail("expected IOException");
            }
            catch (IOException) {}
        }
예제 #28
0
        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) {}
        }
예제 #29
0
        public void testReadline()
        {
            Stream      bis = new MemoryStream(filedata);
            PickleUtils p   = new PickleUtils(bis);

            Assert.AreEqual("str1", p.readline());
            Assert.AreEqual("str2  ", p.readline());
            Assert.AreEqual("  str3  ", p.readline());
            Assert.AreEqual("end", p.readline());
            try
            {
                p.readline();
                Assert.Fail("expected IOException");
            }
            catch (IOException) {}
        }
예제 #30
0
		public static void Main()
		{
			Unpickler u=new Unpickler();

			Console.WriteLine("here we go; 1");
			var data = PickleUtils.str2bytes("\u0080\u0002carray\narray\nq\u0000U\u0001iq\u0001]q\u0002\u0086q\u0003Rq\u0004.");
			var result = u.loads(data);
			PrettyPrint.print(result);
				
			Console.WriteLine("here we go; 2");
			data=PickleUtils.str2bytes("\u0080\u0003carray\n_array_reconstructor\nq\u0000(carray\narray\nq\u0001X\u0001\u0000\u0000\u0000iq\u0002K\u0008C\u000c\u000f'\u0000\u0000\u00b8\"\u0000\u0000a\u001e\u0000\u0000q\u0003tq\u0004Rq\u0005.");
			result=u.loads(data);
			PrettyPrint.print(result);
			
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}