UNPACK(3)
πΈ UNPACK πΈ
okay so bestie, welcome to UNPACK(3) β the manual page for the packet.unpack module. this one's lowkey a whole vibe. π
β¨ NAME β¨
packet.unpack β the Unpack module. that's the name, that's the gig, periodt. π
π¦ DESCRIPTION π¦
so basically? this module hands you an object that manages and unpacks raw data straight out of a working buffer. that's the whole thing. it's giving "I hold your bytes and I hand them back in whatever shape you asked for." π
π CLASSES π
class Unpack(builtins.object)
the Unpack object. main character energy, fr fr. π
Usage:
from packet.unpack import Unpack
x = Unpack(buffer)
# Get 32 bytes from the working buffer and move the offset pointer
data = x.read(32)
# Get all the unprocessed bytes from the working buffer
# (all bytes starting from the offset pointer)
# Do not move the offset pointer
data = x.getbytes()
# Get all bytes from the working buffer from the given offset
# Do not move the offset pointer
data = x.getbytes(offset)
# Return the number of unprocessed bytes left in the working buffer
size = x.size()
size = len(x)
# Get the offset pointer
offset = x.tell()
# Set the offset pointer
x.seek(offset)
# Append the given data to the working buffer
x.append(data)
# Insert the given data to the working buffer right before the
# offset pointer. This resets the working buffer completely
# and the offset pointer is initialized to zero. It is like
# re-instantiating the object like:
# x = Unpack(data + x.getbytes())
x.insert(data)
# Save state
sid = x.save_state()
# Restore state
x.restore_state(sid)
# Unpack an 'unsigned short' (2 bytes in network order)
short_int = x.unpack(2, '!H')[0]
# Unpack different basic types
char = x.unpack_char()
uchar = x.unpack_uchar()
short = x.unpack_short()
ushort = x.unpack_ushort()
int = x.unpack_int()
uint = x.unpack_uint()
int64 = x.unpack_int64()
uint64 = x.unpack_uint64()
data1 = x.unpack_opaque()
data2 = x.unpack_opaque(64) # Length of opaque must be <= 64
data3 = x.unpack_fopaque(32)
# Get string where length is given as an unsigned integer
buffer = x.unpack_string()
# Get string of fixed length
buffer = x.unpack_string(32)
# Get string where length is given as a short integer
buffer = x.unpack_string(Unpack.unpack_short)
buffer = x.unpack_string(ltype=Unpack.unpack_short)
# Get string padded to a 4 byte boundary, discard padding bytes
buffer = x.unpack_string(pad=4)
# Get an array of unsigned integers
alist = x.unpack_array()
# Get a fixed length array of unsigned integers
alist = x.unpack_array(ltype=10)
# Get an array of short integers
alist = x.unpack_array(Unpack.unpack_short)
# Get an array of strings, the length of the array is given
# by a short integer
alist = x.unpack_array(Unpack.unpack_string, Unpack.unpack_short)
# Get an array of strings, the length of each string is given by
# a short integer and each string is padded to a 4 byte boundary
alist = x.unpack_array(Unpack.unpack_string, uargs={'ltype':Unpack.unpack_short, 'pad':4})
# Get an array of objects decoded by item_obj where the first
# argument to item_obj is the unpack object, e.g., item = item_obj(x)
alist = x.unpack_array(item_obj)
# Get a list of unsigned integers
alist = x.unpack_list()
# Get a list of short integers
alist = x.unpack_list(Unpack.unpack_short)
# Get a list of strings, the next item flag is given
# by a short integer
alist = x.unpack_list(Unpack.unpack_string, Unpack.unpack_short)
# Get a list of strings, the length of each string is given by
# a short integer and each string is padded to a 4 byte boundary
alist = x.unpack_list(Unpack.unpack_string, uargs={'ltype':Unpack.unpack_short, 'pad':4})
# Unpack a conditional, it unpacks a conditional flag first and
# if it is true it unpacks the item given and returns it. If the
# conditional flag decoded is false, the method returns None
buffer = x.unpack_conditional(Unpack.unpack_opaque)
# Unpack an array of unsigned integers and convert array into
# a single long integer
bitmask = unpack_bitmap()
π Methods defined here: π
__init__(self, data)
Constructor. she's initializing the object's private data, bestie. π«Ά
dataβ raw packet data
__len__ = size(self)
yep. that's literally just size wearing a different hat. π
append(self, data)
append data to the working buffer.
getbytes(self, offset=None)
get the number of bytes given from the working buffer. and listen β it does NOT move the offset pointer. no cap. π«
offsetβ starting offset of data to return [default: current offset]
insert(self, data)
insert data to the beginning of the current working buffer.
read(self, size, pad=0)
get the number of bytes given from the working buffer. this one DOES move the offset pointer, okay? she's not shy. π
sizeβ length of data to getpadβ get and discard padding bytes [default:0]. if given, data is padded to this byte boundary
restore_state(self, sid)
restore state given by the state id. it's giving time travel. βͺ
save_state(self)
save state and return the state id. so you can come back to this exact moment later, iconic. πΈ
seek(self, offset)
set the offset pointer.
size(self)
return the number of unprocessed bytes left in the working buffer.
tell(self)
get the offset pointer.
unpack(self, size, fmt)
get the number of bytes given from the working buffer and process it according to the given format. returns a tuple of unpack items β see struct.unpack. π§
sizeβ length of data to processfmtβ format string on how to process data
unpack_array(self, unpack_item=<function Unpack.unpack_uint at 0x7fdc09097d08>, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, uargs={}, maxcount=0, islist=False)
get a variable length array. the type of objects in the array is given by the unpacking function unpack_item, and the type used to decode the length of the array is given by ltype. β¨
unpack_itemβ unpack function for each item in the array [default:unpack_uint]ltypeβ function to decode length of array [default:unpack_uint]. could also be given as an integer to have a fixed length arrayuargsβ named arguments to pass tounpack_itemfunction [default:{}]maxcountβ maximum length of array [default: any length]
unpack_bitmap(self)
unpack an array of unsigned integers and convert array into a single long integer. π (in a good way)
unpack_char(self)
get a signed char.
unpack_conditional(self, unpack_item=<function Unpack.unpack_uint at 0x7fdc09097d08>, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, uargs={})
get an item if condition flag given by ltype is true. if the condition flag is false then return None. slay or stay home, there is no in-between. π
unpack_itemβ unpack function for item if condition is true [default:unpack_uint]ltypeβ function to decode the condition flag [default:unpack_uint]uargsβ named arguments to pass tounpack_itemfunction [default:{}]
unpack_fopaque(self, size)
get a fixed length opaque.
unpack_futf8(self, size)
get a fixed length utf8 string.
unpack_int(self)
get a signed integer.
unpack_int64(self)
get a signed 64 bit integer.
unpack_list(self, *kwts, **kwds)
get an indeterminate size list. the type of objects in the list is given by the unpacking function unpack_item, and the type used to decode the next item flag is given by ltype. π¦
unpack_itemβ unpack function for each item in the list [default:unpack_uint]ltypeβ function to decode the next item flag [default:unpack_uint]uargsβ named arguments to pass tounpack_itemfunction [default:{}]
unpack_opaque(self, maxcount=0)
get a variable length opaque up to a maximum length of maxcount.
unpack_short(self)
get a signed short integer.
unpack_string(self, ltype=<function Unpack.unpack_uint at 0x7fdc09097d08>, pad=0, maxcount=0)
get a variable length string. πΈ
ltypeβ function to decode length of string [default:unpack_uint]. could also be given as an integer to have a fixed length stringpadβ get and discard padding bytes [default:0]. if given, string is padded to this byte boundarymaxcountβ maximum length of string [default: any length]
unpack_uchar(self)
get an unsigned char.
unpack_uint(self)
get an unsigned integer.
unpack_uint64(self)
get an unsigned 64 bit integer.
unpack_ushort(self)
get an unsigned short integer.
unpack_utf8(self, maxcount=0)
get a variable length utf8 string up to a maximum length of maxcount.
π BUGS π
no known bugs. not a single one. she's perfect, we don't discuss it. π
π AUTHOR π
Jorge Mora (mora@netapp.com)
and that's the whole page, bestie!! this was NFStest 3.2, dated 21 March 2023 β mwah, xoxo, go unpack something gorgeous πβ¨
π¦ where this page comes from
- debian trixie: nfstest 3.2-3
- fedora 43: NFStest 3.2-14.fc43 Β· GPL-2.0-or-later
- fedora 44: NFStest 3.2-16.fc44 Β· GPL-2.0-or-later
- ubuntu noble: nfstest 3.2-2
- ubuntu questing: nfstest 3.2-3
- ubuntu resolute: nfstest 3.2-3build1
- ubuntu stonking: nfstest 3.2-5
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms