1UNPACK(3) unpack 2.4 UNPACK(3)
2
3
4
6 packet.unpack - Unpack module
7
9 Provides the object for managing and unpacking raw data from a working
10 buffer.
11
13 class Unpack(builtins.object)
14 Unpack object
15
16 Usage:
17 from packet.unpack import Unpack
18
19 x = Unpack(buffer)
20
21 # Get 32 bytes from the working buffer and move the offset pointer
22 data = x.read(32)
23
24 # Get all the unprocessed bytes from the working buffer
25 # (all bytes starting from the offset pointer)
26 # Do not move the offset pointer
27 data = x.getbytes()
28
29 # Get all bytes from the working buffer from the given offset
30 # Do not move the offset pointer
31 data = x.getbytes(offset)
32
33 # Return the number of unprocessed bytes left in the working buffer
34 size = x.size()
35 size = len(x)
36
37 # Get the offset pointer
38 offset = x.tell()
39
40 # Set the offset pointer
41 x.seek(offset)
42
43 # Append the given data to the working buffer
44 x.append(data)
45
46 # Insert the given data to the working buffer right before the
47 # offset pointer. This resets the working buffer completely
48 # and the offset pointer is initialized to zero. It is like
49 # re-instantiating the object like:
50 # x = Unpack(data + x.getbytes())
51 x.insert(data)
52
53 # Save state
54 sid = x.save_state()
55
56 # Restore state
57 x.restore_state(sid)
58
59 # Unpack an 'unsigned short' (2 bytes in network order)
60 short_int = x.unpack(2, '!H')[0]
61
62 # Unpack different basic types
63 char = x.unpack_char()
64 uchar = x.unpack_uchar()
65 short = x.unpack_short()
66 ushort = x.unpack_ushort()
67 int = x.unpack_int()
68 uint = x.unpack_uint()
69 int64 = x.unpack_int64()
70 uint64 = x.unpack_uint64()
71 data1 = x.unpack_opaque()
72 data2 = x.unpack_opaque(64) # Length of opaque must be <= 64
73 data3 = x.unpack_fopaque(32)
74
75 # Get string where length is given as an unsigned integer
76 buffer = x.unpack_string()
77 # Get string of fixed length
78 buffer = x.unpack_string(32)
79 # Get string where length is given as a short integer
80 buffer = x.unpack_string(Unpack.unpack_short)
81 buffer = x.unpack_string(ltype=Unpack.unpack_short)
82 # Get string padded to a 4 byte boundary, discard padding bytes
83 buffer = x.unpack_string(pad=4)
84
85 # Get an array of unsigned integers
86 alist = x.unpack_array()
87 # Get a fixed length array of unsigned integers
88 alist = x.unpack_array(ltype=10)
89 # Get an array of short integers
90 alist = x.unpack_array(Unpack.unpack_short)
91 # Get an array of strings, the length of the array is given
92 # by a short integer
93 alist = x.unpack_array(Unpack.unpack_string, Unpack.unpack_short)
94 # Get an array of strings, the length of each string is given by
95 # a short integer and each string is padded to a 4 byte boundary
96 alist = x.unpack_array(Unpack.unpack_string, uargs={'ltype':Unpack.unpack_short, 'pad':4})
97 # Get an array of objects decoded by item_obj where the first
98 # argument to item_obj is the unpack object, e.g., item = item_obj(x)
99 alist = x.unpack_array(item_obj)
100
101 # Get a list of unsigned integers
102 alist = x.unpack_list()
103 # Get a list of short integers
104 alist = x.unpack_list(Unpack.unpack_short)
105 # Get a list of strings, the next item flag is given
106 # by a short integer
107 alist = x.unpack_list(Unpack.unpack_string, Unpack.unpack_short)
108 # Get a list of strings, the length of each string is given by
109 # a short integer and each string is padded to a 4 byte boundary
110 alist = x.unpack_list(Unpack.unpack_string, uargs={'ltype':Unpack.unpack_short, 'pad':4})
111
112 # Unpack a conditional, it unpacks a conditional flag first and
113 # if it is true it unpacks the item given and returns it. If the
114 # conditional flag decoded is false, the method returns None
115 buffer = x.unpack_conditional(Unpack.unpack_opaque)
116
117 # Unpack an array of unsigned integers and convert array into
118 # a single long integer
119 bitmask = unpack_bitmap()
120
121
122 Methods defined here:
123 ---------------------
124
125 __init__(self, data)
126 Constructor
127
128 Initialize object's private data.
129
130
131 data: Raw packet data
132
133 __len__ = size(self)
134
135 append(self, data)
136 Append data to the working buffer.
137
138 getbytes(self, offset=None)
139 Get the number of bytes given from the working buffer.
140 Do not move the offset pointer.
141
142
143 offset:
144 Starting offset of data to return [default: current offset]
145
146 insert(self, data)
147 Insert data to the beginning of the current working buffer.
148
149 read(self, size, pad=0)
150 Get the number of bytes given from the working buffer.
151 Move the offset pointer.
152
153
154 size: Length of data to get
155
156 pad: Get and discard padding bytes [default: 0]
157 If given, data is padded to this byte boundary
158
159 restore_state(self, sid)
160 Restore state given by the state id
161
162 save_state(self)
163 Save state and return the state id
164
165 seek(self, offset)
166 Set the offset pointer.
167
168 size(self)
169 Return the number of unprocessed bytes left in the working buffer
170
171 tell(self)
172 Get the offset pointer.
173
174 unpack(self, size, fmt)
175 Get the number of bytes given from the working buffer and process
176 it according to the given format.
177 Return a tuple of unpack items, see struct.unpack.
178
179
180 size: Length of data to process
181
182 fmt: Format string on how to process data
183
184 unpack_array(self, unpack_item=<function Unpack.unpack_uint at 0x7fdc09097d08>, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, uargs={}, maxcount=0, islist=False)
185 Get a variable length array, the type of objects in the array
186 is given by the unpacking function unpack_item and the type
187 to decode the length of the array is given by ltype
188
189
190 unpack_item:
191 Unpack function for each item in the array [default: unpack_uint]
192
193 ltype: Function to decode length of array [default: unpack_uint]
194 Could also be given as an integer to have a fixed length array
195
196 uargs: Named arguments to pass to unpack_item function [default: {}]
197
198 maxcount:
199 Maximum length of array [default: any length]
200
201 unpack_bitmap(self)
202 Unpack an array of unsigned integers and convert array into
203 a single long integer
204
205 unpack_char(self)
206 Get a signed char
207
208 unpack_conditional(self, unpack_item=<function Unpack.unpack_uint at 0x7fdc09097d08>, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, uargs={})
209 Get an item if condition flag given by ltype is true, if condition
210 flag is false then return None
211
212
213 unpack_item:
214 Unpack function for item if condition is true [default: unpack_uint]
215
216 ltype: Function to decode the condition flag [default: unpack_uint]
217
218 uargs: Named arguments to pass to unpack_item function [default: {}]
219
220 unpack_fopaque(self, size)
221 Get a fixed length opaque
222
223 unpack_futf8(self, size)
224 Get a fixed length utf8 string
225
226 unpack_int(self)
227 Get a signed integer
228
229 unpack_int64(self)
230 Get a signed 64 bit integer
231
232 unpack_list(self, *kwts, **kwds)
233 Get an indeterminate size list, the type of objects in the list
234 is given by the unpacking function unpack_item and the type
235 to decode the next item flag is given by ltype
236
237
238 unpack_item:
239 Unpack function for each item in the list [default: unpack_uint]
240
241 ltype: Function to decode the next item flag [default: unpack_uint]
242
243 uargs: Named arguments to pass to unpack_item function [default: {}]
244
245 unpack_opaque(self, maxcount=0)
246 Get a variable length opaque up to a maximum length of maxcount
247
248 unpack_short(self)
249 Get a signed short integer
250
251 unpack_string(self, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, pad=0, maxcount=0)
252 Get a variable length string
253
254
255 ltype: Function to decode length of string [default: unpack_uint]
256 Could also be given as an integer to have a fixed length string
257
258 pad: Get and discard padding bytes [default: 0]
259 If given, string is padded to this byte boundary
260
261 maxcount:
262 Maximum length of string [default: any length]
263
264 unpack_uchar(self)
265 Get an unsigned char
266
267 unpack_uint(self)
268 Get an unsigned integer
269
270 unpack_uint64(self)
271 Get an unsigned 64 bit integer
272
273 unpack_ushort(self)
274 Get an unsigned short integer
275
276 unpack_utf8(self, maxcount=0)
277 Get a variable length utf8 string up to a maximum length of maxcount
278
280 No known bugs.
281
283 Jorge Mora (mora@netapp.com)
284
285
286
287NFStest 3.2 21 March 2023 UNPACK(3)