public void testChars()
	{
		ArrayConstructor ac=new ArrayConstructor();
		char EURO=(char)0x20ac;
		
		// c/u
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('c', 18, new byte[]{65,0,0xac,0x20}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('u', 18, new byte[]{65,0,0xac,0x20}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('c', 19, new byte[]{0,65,0x20,0xac}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('u', 19, new byte[]{0,65,0x20,0xac}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('c', 20, new byte[]{65,0,0,0,0xac,0x20,0,0}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('u', 20, new byte[]{65,0,0,0,0xac,0x20,0,0}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('c', 21, new byte[]{0,0,0,65,0,0,0x20,0xac}));
		Assert.AreEqual(new char[]{'A',EURO}, (char[])ac.construct('u', 21, new byte[]{0,0,0,65,0,0,0x20,0xac}));
		try {
		    ac.construct('u', 21, new byte[]{0,1,0,65}); // out of range codepoint
		    Assert.Fail("expected error");
		} catch (PickleException) {
		    // ok
		}
		
		// b/B
		Assert.AreEqual(new sbyte[]{1,2,3,4,-1,-2,-3,-4}, (sbyte[])ac.construct('b', 1, new byte[]{1,2,3,4,0xff,0xfe,0xfd,0xfc}));
		Assert.AreEqual(new byte[]{1,2,3,4,0xff,0xfe,0xfd,0xfc}, (byte[])ac.construct('B', 0, new byte[]{1,2,3,4,0xff,0xfe,0xfd,0xfc}));
	}
        public void testFloats()
        {
            // f/d
            ArrayConstructor ac=new ArrayConstructor();
            Assert.AreEqual(16711938.0f,
                ((float[])ac.construct('f', 15, new byte[]{0x4b,0x7f,0x01,0x02}))[0] );
            Assert.AreEqual(float.PositiveInfinity,
                ((float[])ac.construct('f', 15, new byte[]{0x7f,0x80,0x00,0x00}))[0]);
            Assert.AreEqual(float.NegativeInfinity,
                ((float[])ac.construct('f', 15, new byte[]{0xff,0x80,0x00,0x00}))[0]);
            Assert.AreEqual(-0.0f,
                ((float[])ac.construct('f', 15, new byte[]{0x80,0x00,0x00,0x00}))[0]);

            Assert.AreEqual(16711938.0f,
                ((float[])ac.construct('f', 14, new byte[]{0x02,0x01,0x7f,0x4b}))[0]);
            Assert.AreEqual(float.PositiveInfinity,
                ((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x80,0x7f}))[0]);
            Assert.AreEqual(float.NegativeInfinity,
                ((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x80,0xff}))[0]);
            Assert.AreEqual(-0.0f,
                ((float[])ac.construct('f', 14, new byte[]{0x00,0x00,0x00,0x80}))[0]);

            Assert.AreEqual(9006104071832581.0d,
                ((double[])ac.construct('d', 17, new byte[]{0x43,0x3f,0xff,0x01,0x02,0x03,0x04,0x05}))[0]);
            Assert.AreEqual(double.PositiveInfinity,
                ((double[])ac.construct('d', 17, new byte[]{0x7f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
            Assert.AreEqual(double.NegativeInfinity,
                ((double[])ac.construct('d', 17, new byte[]{0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);
            Assert.AreEqual(-0.0d,
                ((double[])ac.construct('d', 17, new byte[]{0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00}))[0]);

            Assert.AreEqual(9006104071832581.0d,
                ((double[])ac.construct('d', 16, new byte[]{0x05,0x04,0x03,0x02,0x01,0xff,0x3f,0x43}))[0]);
            Assert.AreEqual(double.PositiveInfinity,
                ((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x7f}))[0]);
            Assert.AreEqual(double.NegativeInfinity,
                ((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0xff}))[0]);
            Assert.AreEqual(-0.0d,
                ((double[])ac.construct('d', 16, new byte[]{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80}))[0]);

            // check if multiple values in an array work
            Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[])  ac.construct('f', 15, new byte[]{0x3f,0x8c,0xcc,0xcd, 0x40,0x0c,0xcc,0xcd}));
            Assert.AreEqual(new float[] {1.1f, 2.2f}, (float[])  ac.construct('f', 14, new byte[]{0xcd,0xcc,0x8c,0x3f, 0xcd,0xcc,0x0c,0x40}));
            Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 17, new byte[]{0x3f,0xf1,0x99,0x99,0x99,0x99,0x99,0x9a, 0x40,0x01,0x99,0x99,0x99,0x99,0x99,0x9a}));
            Assert.AreEqual(new double[]{1.1d, 2.2d}, (double[]) ac.construct('d', 16, new byte[]{0x9a,0x99,0x99,0x99,0x99,0x99,0xf1,0x3f, 0x9a,0x99,0x99,0x99,0x99,0x99,0x01,0x40}));
        }
	public void testInvalidMachineTypes()
	{
		ArrayConstructor ac=new ArrayConstructor();
		try {
			ac.construct('b', -1, new byte[]{0});
			Assert.Fail("expected pickleexception");
		} catch (PickleException) {
			//ok
		}

		try {
			ac.construct('b', 0, new byte[]{0});
			Assert.Fail("expected pickleexception");
		} catch (PickleException) {
			//ok
		}

		try {
			ac.construct('?', 0, new byte[]{0});
			Assert.Fail("expected pickleexception");
		} catch (PickleException) {
			//ok
		}
		
		try {
			ac.construct('b', 22, new byte[]{0});
			Assert.Fail("expected pickleexception");
		} catch (PickleException) {
			//ok
		}

		try {
			ac.construct('d', 16, new byte[]{0});
			Assert.Fail("expected pickleexception");
		} catch (PickleException) {
			//ok
		}
	}
Beispiel #4
0
        public object construct(object[] args)
        {
            // args for array constructor: [ string typecode, ArrayList values ]
            // or: [ constructor_class, typecode, machinecode_type, byte[] ]
            if (args.Length == 4)
            {
                ArrayConstructor constructor = (ArrayConstructor)args[0];
                char             arraytype   = ((string)args[1])[0];
                int machinecodeType          = (int)args[2];
                var data = (byte[])args[3];
                return(constructor.construct(arraytype, machinecodeType, data));
            }
            if (args.Length != 2)
            {
                throw new PickleException("invalid pickle data for array; expected 2 args, got " + args.Length);
            }

            string    typecode = (string)args[0];
            ArrayList values   = args[1] as ArrayList;

            if (values == null)
            {
                // python 2.6 encodes the array as a string sequence rather than a list
                // unpickling this is not supported at this time
                throw new PickleException("unsupported Python 2.6 array pickle format");
            }

            switch (typecode[0])
            {
            case 'c':    // character 1 -> char[]
            case 'u':    // Unicode character 2 -> char[]
            {
                var result = new char[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = ((string)values[i])[0];
                }
                return(result);
            }

            case 'b':    // signed char -> sbyte[]
            {
                var result = new sbyte[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToSByte(values[i]);
                }
                return(result);
            }

            case 'B':    // unsigned char -> byte[]
            {
                var result = new byte[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToByte(values[i]);
                }
                return(result);
            }

            case 'h':    // signed short -> short[]
            {
                var result = new short[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToInt16(values[i]);
                }
                return(result);
            }

            case 'H':    // unsigned short -> ushort[]
            {
                var result = new ushort[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToUInt16(values[i]);
                }
                return(result);
            }

            case 'i':    // signed integer -> int[]
            {
                var result = new int[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToInt32(values[i]);
                }
                return(result);
            }

            case 'I':    // unsigned integer 4 -> uint[]
            {
                var result = new uint[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToUInt32(values[i]);
                }
                return(result);
            }

            case 'l':    // signed long -> long[]
            {
                var result = new long[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToInt64(values[i]);
                }
                return(result);
            }

            case 'L':    // unsigned long -> ulong[]
            {
                var result = new ulong[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToUInt64(values[i]);
                }
                return(result);
            }

            case 'f':    // floating point 4 -> float[]
            {
                var result = new float[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToSingle(values[i]);
                }
                return(result);
            }

            case 'd':    // floating point 8 -> double[]
            {
                var result = new double[values.Count];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToDouble(values[i]);
                }
                return(result);
            }

            default:
                throw new PickleException("invalid array typecode: " + typecode);
            }
        }
	public void testInts()
	{
		ArrayConstructor ac=new ArrayConstructor();

		//h
		Assert.AreEqual((short)-0x7f01, ((short[])ac.construct('h', 5, new byte[]{0x80,0xff}))[0]);
		Assert.AreEqual((short)0x7fff, ((short[])ac.construct('h', 5, new byte[]{0x7f,0xff}))[0]);
		Assert.AreEqual((short)-1, ((short[])ac.construct('h', 5, new byte[]{0xff,0xff}))[0]);
		Assert.AreEqual((short)-1, ((short[])ac.construct('h', 4, new byte[]{0xff,0xff}))[0]);
		Assert.AreEqual(new short[]{0x1234,0x5678}, (short[])ac.construct('h', 5, new byte[]{0x12,0x34,0x56,0x78}));
		Assert.AreEqual(new short[]{0x3412,0x7856}, (short[])ac.construct('h', 4, new byte[]{0x12,0x34,0x56,0x78}));

		//H
		Assert.AreEqual((ushort)0x80ff, ((ushort[])ac.construct('H', 3, new byte[]{0x80,0xff}))[0]);
		Assert.AreEqual((ushort)0x7fff, ((ushort[])ac.construct('H', 3, new byte[]{0x7f,0xff}))[0]);
		Assert.AreEqual((ushort)0xffff, ((ushort[])ac.construct('H', 3, new byte[]{0xff,0xff}))[0]);
		Assert.AreEqual((ushort)0xffff, ((ushort[])ac.construct('H', 2, new byte[]{0xff,0xff}))[0]);
		Assert.AreEqual(new ushort[]{0x1234,0x5678}, (ushort[])ac.construct('H', 3, new byte[]{0x12,0x34,0x56,0x78}));
		Assert.AreEqual(new ushort[]{0x3412,0x7856}, (ushort[])ac.construct('H', 2, new byte[]{0x12,0x34,0x56,0x78}));

		//i
		Assert.AreEqual((int)-0x7fffff01, ((int[])ac.construct('i', 9, new byte[]{0x80,0x00,0x00,0xff}))[0]);
		Assert.AreEqual((int)0x7f0000ff, ((int[])ac.construct('i', 9, new byte[]{0x7f,0x00,0x00,0xff}))[0]);
		Assert.AreEqual((int)-0xfffff0f, ((int[])ac.construct('i', 9, new byte[]{0xf0,0x00,0x00,0xf1}))[0]);
		Assert.AreEqual((int)-2, ((int[])ac.construct('i', 8, new byte[]{0xfe,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(new int[]{0x11223344,0x55667788}, (int[])ac.construct('i', 9, new byte[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}));
		Assert.AreEqual(new int[]{0x44332211,-0x778899ab}, (int[])ac.construct('i', 8, new byte[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}));

		//l-4bytes
		Assert.AreEqual(-0x7fffff01, ((int[])ac.construct('l', 9, new byte[]{0x80,0x00,0x00,0xff}))[0]);
		Assert.AreEqual(0x7f0000ff, ((int[])ac.construct('l', 9, new byte[]{0x7f,0x00,0x00,0xff}))[0]);
		Assert.AreEqual(-0x0fffff0f, ((int[])ac.construct('l', 9, new byte[]{0xf0,0x00,0x00,0xf1}))[0]);
		Assert.AreEqual(-2, ((int[])ac.construct('l', 8, new byte[]{0xfe,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(new int[]{0x11223344,0x55667788}, (int[])ac.construct('l', 9, new byte[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}));
		Assert.AreEqual(new int[]{0x44332211,-0x778899ab}, (int[])ac.construct('l', 8, new byte[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88}));
		//l-8bytes
		Assert.AreEqual(0x3400000000000012L, ((long[])ac.construct('l', 12, new byte[]{0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x34}))[0]);
		Assert.AreEqual(0x3400009009000012L, ((long[])ac.construct('l', 12, new byte[]{0x12,0x00,0x00,0x09,0x90,0x00,0x00,0x34}))[0]);
		Assert.AreEqual(0x1200000000000034L, ((long[])ac.construct('l', 13, new byte[]{0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x34}))[0]);
		Assert.AreEqual(0x1200000990000034L, ((long[])ac.construct('l', 13, new byte[]{0x12,0x00,0x00,0x09,0x90,0x00,0x00,0x34}))[0]);

		Assert.AreEqual(0x7fffffffffffffffL, ((long[])ac.construct('l', 13, new byte[]{0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(0x7fffffffffffffffL, ((long[])ac.construct('l', 12, new byte[]{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f}))[0]);

		Assert.AreEqual(-2L, ((long[])ac.construct('l', 12, new byte[]{0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(-2L, ((long[])ac.construct('l', 13, new byte[]{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe}))[0]);
		Assert.AreEqual(new long[]{1,2}, (long[])ac.construct('l', 13, new byte[]{0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,2}));
		Assert.AreEqual(new long[]{1,2}, (long[])ac.construct('l', 12, new byte[]{1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0}));

		//I 
		Assert.AreEqual(0x001000000u, ((uint[])ac.construct('I', 6, new byte[]{0,0,0,0x01}))[0]);
		Assert.AreEqual(0x088000000u, ((uint[])ac.construct('I', 6, new byte[]{0,0,0,0x88}))[0]);
		Assert.AreEqual(0x000000001u, ((uint[])ac.construct('I', 7, new byte[]{0,0,0,0x01}))[0]);
		Assert.AreEqual(0x000000088u, ((uint[])ac.construct('I', 7, new byte[]{0,0,0,0x88}))[0]);
		Assert.AreEqual(0x099000088u, ((uint[])ac.construct('I', 7, new byte[]{0x99,0,0,0x88}))[0]);

		//L-4 bytes
		Assert.AreEqual(0x22000011U, ((uint[])ac.construct('L', 6, new byte[]{0x11,0,0,0x22}))[0]);
		Assert.AreEqual(0x11000022U, ((uint[])ac.construct('L', 7, new byte[]{0x11,0,0,0x22}))[0]);
		Assert.AreEqual(0xfffffffeU, ((uint[])ac.construct('L', 6, new byte[]{0xfe,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(0xfffffffeU, ((uint[])ac.construct('L', 7, new byte[]{0xff,0xff,0xff,0xfe}))[0]);
    	
    	//L-8 bytes
		Assert.AreEqual(0x4400003322000011UL, ((ulong[])ac.construct('L', 10, new byte[]{0x11,0,0,0x22,0x33,0,0,0x44}))[0]);
		Assert.AreEqual(0x1100002233000044UL, ((ulong[])ac.construct('L', 11, new byte[]{0x11,0,0,0x22,0x33,0,0,0x44}))[0]);
		Assert.AreEqual(0xfffffffffffffffeUL, ((ulong[])ac.construct('L', 10, new byte[]{0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff}))[0]);
		Assert.AreEqual(0xfffffffffffffffeUL, ((ulong[])ac.construct('L', 11, new byte[]{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe}))[0]);
	}
Beispiel #6
0
        public object construct(object[] args)
        {
            // args for array constructor: [ string typecode, ArrayList values ]
            // or: [ constructor_class, typecode, machinecode_type, byte[] ]  (this form is not supported yet)
            if (args.Length == 4)
            {
                ArrayConstructor constructor = (ArrayConstructor)args[0];
                char             arraytype   = ((string)args[1])[0];
                int    machinecodeType       = (int)args[2];
                byte[] data = (byte[])args[3];
                return(constructor.construct(arraytype, machinecodeType, data));
            }
            if (args.Length != 2)
            {
                throw new PickleException("invalid pickle data for array; expected 2 args, got " + args.Length);
            }

            string    typecode = (string)args[0];
            ArrayList values   = (ArrayList)args[1];

            switch (typecode[0])
            {
            case 'c':    // character 1 -> char[]
            case 'u':    // Unicode character 2 -> char[]
            {
                char[] result = new char[values.Count];
                int    i      = 0;
                foreach (var c in values)
                {
                    result[i++] = ((string)c)[0];
                }
                return(result);
            }

            case 'b':    // signed char -> sbyte[]
            {
                sbyte[] result = new sbyte[values.Count];
                int     i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToSByte(c);
                }
                return(result);
            }

            case 'B':    // unsigned char -> byte[]
            {
                byte[] result = new byte[values.Count];
                int    i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToByte(c);
                }
                return(result);
            }

            case 'h':    // signed short -> short[]
            {
                short[] result = new short[values.Count];
                int     i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToInt16(c);
                }
                return(result);
            }

            case 'H':    // unsigned short -> ushort[]
            {
                ushort[] result = new ushort[values.Count];
                int      i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToUInt16(c);
                }
                return(result);
            }

            case 'i':    // signed integer -> int[]
            {
                int[] result = new int[values.Count];
                int   i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToInt32(c);
                }
                return(result);
            }

            case 'I':    // unsigned integer 4 -> uint[]
            {
                uint[] result = new uint[values.Count];
                int    i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToUInt32(c);
                }
                return(result);
            }

            case 'l':    // signed long -> long[]
            {
                long[] result = new long[values.Count];
                int    i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToInt64(c);
                }
                return(result);
            }

            case 'L':    // unsigned long -> ulong[]
            {
                ulong[] result = new ulong[values.Count];
                int     i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToUInt64(c);
                }
                return(result);
            }

            case 'f':    // floating point 4 -> float[]
            {
                float[] result = new float[values.Count];
                int     i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToSingle(c);
                }
                return(result);
            }

            case 'd':    // floating point 8 -> double[]
            {
                double[] result = new double[values.Count];
                int      i      = 0;
                foreach (var c in values)
                {
                    result[i++] = Convert.ToDouble(c);
                }
                return(result);
            }

            default:
                throw new PickleException("invalid array typecode: " + typecode);
            }
        }