public void TestBytes_to_integer() { try { PickleUtils.bytes_to_integer(new byte[] {}); Assert.True(false, "expected PickleException"); } catch (PickleException) {} try { PickleUtils.bytes_to_integer(new byte[] { 0 }); Assert.True(false, "expected PickleException"); } catch (PickleException) {} Assert.Equal(0x00000000, PickleUtils.bytes_to_integer(new byte[] { 0x00, 0x00 })); Assert.Equal(0x00003412, PickleUtils.bytes_to_integer(new byte[] { 0x12, 0x34 })); Assert.Equal(0x0000ffff, PickleUtils.bytes_to_integer(new byte[] { 0xff, 0xff })); Assert.Equal(0x00000000, PickleUtils.bytes_to_integer(new byte[] { 0, 0, 0, 0 })); Assert.Equal(0x12345678, PickleUtils.bytes_to_integer(new byte[] { 0x78, 0x56, 0x34, 0x12 })); Assert.Equal(-8380352, PickleUtils.bytes_to_integer(new byte[] { 0x40, 0x20, 0x80, 0xff })); Assert.Equal(0x01cc02ee, PickleUtils.bytes_to_integer(new byte[] { 0xee, 0x02, 0xcc, 0x01 })); Assert.Equal(-872288766, PickleUtils.bytes_to_integer(new byte[] { 0x02, 0xee, 0x01, 0xcc })); Assert.Equal(-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.True(false, "expected PickleException"); } catch (PickleException) {} }
protected int[] constructIntArrayFromInt32(int machinecode, byte[] data) { var result = new int[data.Length / 4]; if (machinecode == 8) { for (int i = 0; i < result.Length; i++) { result[i] = PickleUtils.bytes_to_integer(data, i * 4, 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_integer(bigendian); } } return(result); }
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); }
protected char[] constructCharArrayUTF16(int machinecode, byte[] data) { char[] result = new char[data.Length / 2]; byte[] bigendian = new byte[2]; for (int index = 0; index < data.Length / 2; ++index) { if (machinecode == 18) { result[index] = (char)PickleUtils.bytes_to_integer(data, index * 2, 2); } else { // big endian, swap bigendian[0] = data[1 + index * 2]; bigendian[1] = data[0 + index * 2]; result[index] = (char)PickleUtils.bytes_to_integer(bigendian); } } return(result); }