1UNPACK(3)                         unpack 2.4                         UNPACK(3)
2
3
4

NAME

6       packet.unpack - Unpack module
7

DESCRIPTION

9       Provides  the object for managing and unpacking raw data from a working
10       buffer.
11

CLASSES

13   class Unpack(__builtin__.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_uint>, ltype=<function unpack_uint>, 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_uint>, ltype=<function unpack_uint>, 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_int(self)
224       Get a signed integer
225
226       unpack_int64(self)
227       Get a signed 64 bit integer
228
229       unpack_list(self, *kwts, **kwds)
230       Get an indeterminate size list, the type of objects in the list
231       is given by the unpacking function unpack_item and the type
232       to decode the next item flag is given by ltype
233
234
235              unpack_item:
236                     Unpack function for each item in the list [default: unpack_uint]
237
238              ltype: Function to decode the next item flag [default: unpack_uint]
239
240              uargs: Named arguments to pass to unpack_item function [default: {}]
241
242       unpack_opaque(self, maxcount=0)
243       Get a variable length opaque up to a maximum length of maxcount
244
245       unpack_short(self)
246       Get a signed short integer
247
248       unpack_string(self, ltype=<function unpack_uint>, pad=0, maxcount=0)
249       Get a variable length string
250
251
252              ltype: Function to decode length of string [default: unpack_uint]
253                     Could also be given as an integer to have a fixed length string
254
255              pad:   Get and discard padding bytes [default: 0]
256                     If given, string is padded to this byte boundary
257
258              maxcount:
259                     Maximum length of string [default: any length]
260
261       unpack_uchar(self)
262       Get an unsigned char
263
264       unpack_uint(self)
265       Get an unsigned integer
266
267       unpack_uint64(self)
268       Get an unsigned 64 bit integer
269
270       unpack_ushort(self)
271       Get an unsigned short integer
272

BUGS

274       No known bugs.
275

AUTHOR

277       Jorge Mora (mora@netapp.com)
278
279
280
281NFStest 2.1.5                  14 February 2017                      UNPACK(3)
Impressum