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:__len () | Returns the length of the data object in bytes. |
data:__tostring () | Returns the content of the data object as a Lua string. |
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 (platform-dependent: 64-bit on LP64, 32-bit otherwise) 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: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 (platform-dependent: 64-bit on LP64, 32-bit otherwise) 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:__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: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.be16toh etc. 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 (platform-dependent: 64-bit on LP64, 32-bit otherwise) from the data object.
Alias for getint64 on LP64 systems, getint32 otherwise. 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
offset
to 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: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.htobe16 etc. 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 (platform-dependent: 64-bit on LP64, 32-bit otherwise) into the data object.
Alias for setint64 on LP64 systems, setint32 otherwise. 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.