2 #############################################################
6 # Contains functions that are used to pack and unpack data
7 # from Samba's tdb databases. Samba sometimes represents complex
8 # data structures as a single value in a database. These functions
9 # allow other python scripts to package data types into a single python
10 # string and unpackage them.
13 # XXXXX: This code is no longer used; it's just here for testing
14 # compatibility with the new (much faster) C implementation.
16 ##############################################################
19 def pack(format,list):
23 # Cycle through format entries
25 # Null Terminated String
26 if (type == 'f' or type == 'P'):
27 retstring = retstring + list[listind] + "\000"
30 retstring = retstring + PackNum(list[listind],4)
33 retstring = retstring + PackNum(list[listind],2)
37 retstring = retstring + PackNum(1,4)
39 retstring = retstring + PackNum(0,4)
43 length = list[listind]
44 retstring = retstring + PackNum(length,4)
48 retstring = retstring + list[listind][:length]
54 def unpack(format,buffer):
62 newvalue = UnpackNum(buffer[bufind:bufind+4])
68 retlist.append(newvalue)
69 # Previous character till end of data
72 while (bufind < len(buffer)):
74 while (buffer[bufind] != '\000'):
75 newstring = newstring + buffer[bufind]
78 retlist.append(newstring)
79 # Null Terminated String
80 elif (type == 'f' or type == 'P'):
82 while (buffer[bufind] != '\000'):
83 newstring = newstring + buffer[bufind]
86 retlist.append(newstring)
89 newvalue = UnpackNum(buffer[bufind:bufind+4])
91 retlist.append(newvalue)
94 newvalue = UnpackNum(buffer[bufind:bufind+2])
96 retlist.append(newvalue)
100 length = UnpackNum(buffer[bufind:bufind+4])
102 retlist.append(length)
105 retlist.append(buffer[bufind:bufind+length])
106 bufind = bufind + length
110 return ((retlist,buffer[bufind:]))
112 def PackNum(myint,size):
115 hint = hex(myint)[2:]
117 # Check for long notation
118 if (hint[-1:] == 'L'):
121 addon = size - len(hint)
122 for i in range(0,addon):
126 val = string.atoi(hint[size-2:size],16)
127 retstring = retstring + chr(val)
132 def UnpackNum(buffer):
136 for i in range(size-1,-1,-1):
137 val = hex(ord(buffer[i]))[2:]
140 mystring = mystring + val
141 if (len(mystring) > 4):
142 return string.atol(mystring,16)
144 return string.atoi(mystring,16)