コード例 #1
0
    def test_simple_struct(self):
        Data = struct.pack("<IB", 0xAABBCCDD, 0xEE)
        O = unpack(S0, Data)
        self.assertEqual(pack(S0, O), Data)

        Data = struct.pack("<IIB", 0xAABBCCDD, 0, 0xEE)
        O = unpack(S1, Data)
        self.assertEqual(pack(S1, O), Data)
コード例 #2
0
 def __getattr__(self, n):
     ty = self._ty
     attr_ty = ty.types.get(n, None)
     if attr_ty is None:
         raise KeyError("unknown attribute '%s'" % n)
     data = getattr(self, "_data")
     from purectypes.unpack import unpack
     return unpack(attr_ty, data[:attr_ty.size])
コード例 #3
0
    def test_array(self):
        Ref = (0, 1, 2, 3)
        O = pack(ArrayTy(UI64, 4), Ref)
        self.assertEqual(O, struct.pack("<QQQQ", *Ref))

        Data = struct.pack("<IB", 0xAABBCCDD, 0xEE)
        O = unpack(S0, Data)
        P = pack(ArrayTy(S0, 2), [O, O])
        self.assertEqual(len(P), 2 * 8)
コード例 #4
0
 def test_union0(self):
     Ref = [0xAA, 0xBB, 0xCC, 0xDD]
     Data = struct.pack("<BBBB", *Ref)
     O = unpack(U0, Data)
     self.assertEqual(O.ar, Ref)
     self.assertEqual(O.ip.a, 0xAA)
     self.assertEqual(O.ip.b, 0xBB)
     self.assertEqual(O.ip.c, 0xCC)
     self.assertEqual(O.ip.d, 0xDD)
     self.assertEqual(O.i32, 0xDDCCBBAA)
コード例 #5
0
 def test_union(self):
     Ref = [0xAA, 0xBB, 0xCC, 0xDD]
     Data = struct.pack("<BBBB", *Ref)
     self.assertEqual(pack(U0, Ref), Data)
     self.assertEqual(pack(U0, 0xDDCCBBAA), Data)
     self.assertEqual(pack(U0, unpack(SIpAddr, Data)), Data)
コード例 #6
0
 def test_ptr(self):
     self.assertEqual(unpack(PointerTy(UI8, "<Q"), struct.pack("<Q", 10)),
                      10)
コード例 #7
0
 def test_union1(self):
     Data = b"\xAA" * 10
     O = unpack(U1, Data)
     self.assertEqual(O.ptr, 0xAAAAAAAAAAAAAAAA)
コード例 #8
0
 def test_array(self):
     Data = struct.pack("<QQQQ", 0, 1, 2, 3)
     O = unpack(ArrayTy(UI64, 4), Data)
     self.assertEqual(O, list(range(4)))
コード例 #9
0
 def test_ui(self):
     self.assertEqual(unpack(UI32, struct.pack("<I", 0xAABBCCDD)),
                      0xAABBCCDD)
     self.assertEqual(unpack(UI8, bytes((10, ))), 10)