Module data
Direct memory access and manipulation.
This library allows creating data objects that represent blocks of memory. These objects can then be used to read and write various integer types (signed/unsigned, 8/16/32/64-bit) and raw byte strings at specific offsets.
Class data
| data.L | Bounds-checked pointer calculation. |
| data.L | Resizes an SKB (socket buffer) to the specified size. |
| data:__len () | Returns the length of the data object in bytes. |
| data:__tostring () | Returns the content of the data object as a Lua string. |
| data:checksum ([offset[, length]]) | Perform a raw checksum on a given buffer. |
| data:getbyte (offset) | Extracts an unsigned 8-bit integer (a byte) from the data object. |
| data:getint16 (offset) | Extracts a signed 16-bit integer from the data object. |
| data:getint32 (offset) | Extracts a signed 32-bit integer from the data object. |
| data:getint64 (offset) | Extracts a signed 64-bit integer from the data object. |
| data:getint8 (offset) | Extracts a signed 8-bit integer from the data object. |
| data:getnumber (offset) | Extracts a Lua integer from the data object. |
| data:getstring (offset[, length]) | Extracts a string from the data object. |
| data:getuint16 (offset) | Extracts an unsigned 16-bit integer from the data object. |
| data:getuint32 (offset) | Extracts an unsigned 32-bit integer from the data object. |
| data:getuint8 (offset) | Extracts an unsigned 8-bit integer from the data object. |
| data:resize (new_size) | Resizes the memory block represented by the data object. |
| data:setbyte (offset, value) | Inserts an unsigned 8-bit integer (a byte) into the data object. |
| data:setint16 (offset, value) | Inserts a signed 16-bit integer into the data object. |
| data:setint32 (offset, value) | Inserts a signed 32-bit integer into the data object. |
| data:setint64 (offset, value) | Inserts a signed 64-bit integer into the data object. |
| data:setint8 (offset, value) | Inserts a signed 8-bit integer into the data object. |
| data:setnumber (offset, value) | Inserts a Lua integer into the data object. |
| data:setstring (offset, s) | Inserts a string into the data object. |
| data:setuint16 (offset, value) | Inserts an unsigned 16-bit integer into the data object. |
| data:setuint32 (offset, value) | Inserts an unsigned 32-bit integer into the data object. |
| data:setuint8 (offset, value) | Inserts an unsigned 8-bit integer into the data object. |
data
| new (size) | Creates a new data object, allocating a fresh block of memory. |
Class data
Represents a raw block of memory.
This is a userdata object returned by
data.new() or created internally
by other Lunatik modules (e.g., for network packet buffers).
- data.L
-
Bounds-checked pointer calculation. Returns pointer on success, raises Lua error on failure.
- L Lua state
- ix Argument index for error reporting
- data luadata object
- offset Byte offset
- length Access length
- data.L
-
Resizes an SKB (socket buffer) to the specified size.
Expands the buffer using skbput() if newsize > current size,
or shrinks it using skbtrim() if newsize < current size.
- L Lua state for error reporting
- data luadata object wrapping the SKB
- new_size The desired size in bytes
- data:__len ()
-
Returns the length of the data object in bytes.
This is the Lua __len metamethod, allowing use of the
#operator.Returns:
-
integer
The total size of the memory block in bytes.
- data:__tostring ()
-
Returns the content of the data object as a Lua string.
This is the Lua __tostring metamethod.
Returns:
-
string
A string representation of the entire data block.
- data:checksum ([offset[, length]])
-
Perform a raw checksum on a given buffer.
Parameters:
- offset integer , from where to checksum (optional)
- length integer , total length checksum (optional)
Raises:
Error if the write operation (offset + length of data). - data:getbyte (offset)
-
Extracts an unsigned 8-bit integer (a byte) from the data object.
Alias for getuint8.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The byte value (0-255) at the specified offset.
Raises:
Error if offset is out of bounds.See also:
- data:getint16 (offset)
-
Extracts a signed 16-bit integer from the data object.
Assumes host byte order. For specific byte orders, use
linux.be16tohetc. on the result.Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The signed 16-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getint32 (offset)
-
Extracts a signed 32-bit integer from the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The signed 32-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getint64 (offset)
-
Extracts a signed 64-bit integer from the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The signed 64-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getint8 (offset)
-
Extracts a signed 8-bit integer from the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The signed 8-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getnumber (offset)
-
Extracts a Lua integer from the data object.
Alias for getint64. Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The Lua integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getstring (offset[, length])
-
Extracts a string from the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- length
integer
Number of bytes to read. If omitted, reads all bytes from
offsetto the end of the data block. (optional)
Returns:
-
string
The string containing the bytes read from the data object.
Raises:
Error if offset/length is out of bounds. - data:getuint16 (offset)
-
Extracts an unsigned 16-bit integer from the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The unsigned 16-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getuint32 (offset)
-
Extracts an unsigned 32-bit integer from the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The unsigned 32-bit integer value at the specified offset.
Raises:
Error if offset is out of bounds. - data:getuint8 (offset)
-
Extracts an unsigned 8-bit integer from the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
Returns:
-
integer
The unsigned 8-bit integer value (0-255) at the specified offset.
Raises:
Error if offset is out of bounds. - data:resize (new_size)
-
Resizes the memory block represented by the data object.
If the object is a network packet (SKB), it uses skbput() to expand
or skbtrim() to shrink the buffer. For raw buffers, it updates the size.
Parameters:
- new_size integer The desired size of the memory block in bytes.
Raises:
Error if the data object is read-only or if resize fails. - data:setbyte (offset, value)
-
Inserts an unsigned 8-bit integer (a byte) into the data object.
Alias for setuint8.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The byte value (0-255) to write.
Raises:
Error if offset is out of bounds or the data object is read-only.See also:
- data:setint16 (offset, value)
-
Inserts a signed 16-bit integer into the data object.
Assumes host byte order. For specific byte orders, use
linux.htobe16etc. on the value before setting.Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The signed 16-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setint32 (offset, value)
-
Inserts a signed 32-bit integer into the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The signed 32-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setint64 (offset, value)
-
Inserts a signed 64-bit integer into the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The signed 64-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setint8 (offset, value)
-
Inserts a signed 8-bit integer into the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The signed 8-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setnumber (offset, value)
-
Inserts a Lua integer into the data object.
Alias for setint64. Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The Lua integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setstring (offset, s)
-
Inserts a string into the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed) where writing will begin.
- s string The string to write into the data object.
Raises:
Error if the write operation (offset + length of string) goes out of bounds, or if the data object is read-only. - data:setuint16 (offset, value)
-
Inserts an unsigned 16-bit integer into the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The unsigned 16-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setuint32 (offset, value)
-
Inserts an unsigned 32-bit integer into the data object.
Assumes host byte order.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The unsigned 32-bit integer value to write.
Raises:
Error if offset is out of bounds or the data object is read-only. - data:setuint8 (offset, value)
-
Inserts an unsigned 8-bit integer into the data object.
Parameters:
- offset integer Byte offset from the start of the data block (0-indexed).
- value integer The unsigned 8-bit integer value (0-255) to write.
Raises:
Error if offset is out of bounds or the data object is read-only.
data
- new (size)
-
Creates a new data object, allocating a fresh block of memory.
Parameters:
- size integer The number of bytes to allocate for the data block.
Returns:
-
data
A new, writable data object.
Raises:
Error if memory allocation fails.