Module BlobReader
Parses binary data from memory.
Functions
new ([data[, sizeOrByteOrder[, size]]]) | Creates a new BlobReader instance. |
read () | Reads a string , a number , a boolean or a table from the input data. |
number () | Reads a Lua number from the input data. |
string () | Reads a string from the input data. |
bool () | Reads a boolean value from the input data. |
table ([result]) | Reads a Lua table from the input data. |
u8 () | Reads one unsigned 8-bit value from the input data. |
s8 () | Reads one signed 8-bit value from the input data. |
u16 () | Reads one unsigned 16-bit value from the input data. |
s16 () | Reads one signed 16 bit value from the input data. |
u32 () | Reads one unsigned 32 bit value from the input data. |
s32 () | Reads one signed 32 bit value from the input data. |
u64 () | Reads one unsigned 64 bit value from the input data. |
s64 () | Reads one signed 64 bit value from the input data. |
f32 () | Reads one 32 bit floating point value from the input data. |
f64 () | Reads one 64 bit floating point value from the input data. |
vu32 () | Reads a length-encoded unsigned 32 bit integer value from the input data. |
vs32 () | Reads a length-encoded signed 32 bit integer value from the input data. |
raw (len) | Reads raw binary data from the input data. |
cdata () | Reads a cdata object from the input data. |
skip (len) | Skips a number of bytes in the input data. |
cstring () | Reads a zero-terminated string from the input data (up to 2 ^ 32 - 1 bytes). |
array (valueType[, count[, result]]) | Reads a sequential table of typed values. |
unpack (format) | Parses data into separate values according to a format string. |
size () | Returns the size of the input data in bytes. |
reset (data[, size]) | Re-initializes the reader with new data and resets the read position. |
seek (pos) | Moves the read position to pos |
position () | Returns the current read position as an offset from the start of the input data in bytes. |
setByteOrder (byteOrder) | Set source data byte order. |
Functions
- new ([data[, sizeOrByteOrder[, size]]])
-
Creates a new BlobReader instance.
Parameters:
- data string or cdata optional Source data
- sizeOrByteOrder
number or string
optional
Size of
data
(required when data is acdata
pointer) or the byte order of the dataByte order: Use
le
or<
for little endian;be
or>
for big endian;host
,=
ornil
to use the host’s native byte order (default) - size
number
optional
Size of
data
(required whendata
is acdata
pointer)
Returns:
-
BlobReader
A new BlobReader instance.
Usage:
reader = BlobReader(data)
reader = BlobReader(data, '>')
reader = BlobReader(cdata, 1000)
- read ()
-
Reads a
string
, anumber
, aboolean
or atable
from the input data.The data must have been written by BlobWriter:write. The type of the value is automatically detected from the input metadata.
Returns:
See also:
- number ()
-
Reads a Lua number from the input data.
Returns:
-
number
The number read read from the input data
- string ()
-
Reads a string from the input data.
The string must have been written by BlobWriter:write or BlobWriter:string
Returns:
-
string
The string read from the input data
See also:
- bool ()
-
Reads a boolean value from the input data.
The data is expected to be 8 bits long,
0 == false
, any other value ==true
Returns:
-
bool
The boolean value read from the input data
- table ([result])
-
Reads a Lua table from the input data.
The table must have been written by BlobWriter.write or BlobWriter.table.
Parameters:
- result table optional Table to put values in
Returns:
-
table
The table read from the input data
See also:
- u8 ()
-
Reads one unsigned 8-bit value from the input data.
Returns:
-
number
The unsigned 8-bit value read from the input data
- s8 ()
-
Reads one signed 8-bit value from the input data.
Returns:
-
number
The signed 8-bit value read from the input data
- u16 ()
-
Reads one unsigned 16-bit value from the input data.
Returns:
-
number
The unsigned 16-bit value read from the input data
- s16 ()
-
Reads one signed 16 bit value from the input data.
Returns:
-
number
The signed 16-bit value read from the input data
- u32 ()
-
Reads one unsigned 32 bit value from the input data.
Returns:
-
number
The unsigned 32-bit value read from the input data
- s32 ()
-
Reads one signed 32 bit value from the input data.
Returns:
-
number
The signed 32-bit value read from the input data
- u64 ()
-
Reads one unsigned 64 bit value from the input data.
Returns:
-
number
The unsigned 64-bit value read from the input data
- s64 ()
-
Reads one signed 64 bit value from the input data.
Returns:
-
number
The signed 64-bit value read from the input data
- f32 ()
-
Reads one 32 bit floating point value from the input data.
Returns:
-
number
The 32-bit floating point value read from the input data
- f64 ()
-
Reads one 64 bit floating point value from the input data.
Returns:
-
number
The 64-bit floating point value read from the input data
- vu32 ()
-
Reads a length-encoded unsigned 32 bit integer value from the input data.
See BlobWriter:vu32 for more details about this data type.
Returns:
-
number
The unsigned 32-bit integer value read from the input data
See also:
- vs32 ()
-
Reads a length-encoded signed 32 bit integer value from the input data.
See BlobWriter:vs32 for more details about this data type.
Returns:
-
number
The signed 32-bit integer value read from the input data
See also:
- raw (len)
-
Reads raw binary data from the input data.
Parameters:
- len number The length of the data (in bytes) to read
Returns:
-
string
A string with raw data
- cdata ()
-
Reads a
cdata
object from the input data.This function can only read data that was written by BlobWriter:cdata.
Returns:
-
cdata
The
cdata
object - skip (len)
-
Skips a number of bytes in the input data.
Parameters:
- len number The number of bytes to skip
Returns:
-
BlobReader
self
- cstring ()
-
Reads a zero-terminated string from the input data (up to 2 ^ 32 - 1 bytes).
Keeps reading bytes until a null byte is encountered.
Returns:
-
string
The string read from the input data
- array (valueType[, count[, result]])
-
Reads a sequential table of typed values.
Parameters:
- valueType
string
Type of the values in the array
Valid types are
s8
,u8
,s16
,u16
,s32
,u32
,vs32
,vu32
,s64
,u64
,f32
,f64
,number
,string
,bool
,cstring
,table
, andcdata
. - count
number
optional
Number of values to read. If
nil
, precedingvu32
encoded array length information is expected to be precede the array, as written by BlobWriter:array. - result table optional Table to put the values in
Returns:
-
table
A sequential table, starting at index 1
See also:
- valueType
string
Type of the values in the array
- unpack (format)
-
Parses data into separate values according to a format string.
Parameters:
- format
string
Data format specification
See BlobWriter:pack for a list of supported identifiers.
Additional format specifiers for unpack:
x[n]
: skipn
bytes of datan
defaults to 1 if no length was specified.
Returns:
-
All values parsed from the input data
See also:
Usage:
byte, float, bool = reader:unpack('x4Bfy') -- skips 4 bytes before actual data
- format
string
- size ()
-
Returns the size of the input data in bytes.
Returns:
-
number
Data size in bytes
- reset (data[, size])
-
Re-initializes the reader with new data and resets the read position.
Parameters:
- data string, cdata or nil The source data
- size
number
optional
The length of the data (only required when
data
is acdata
pointer)
Returns:
-
BlobReader
self
- seek (pos)
-
Moves the read position to
pos
Parameters:
- pos number New read position (offset in bytes from the start of data). Negative values will seek from the end of data
Returns:
-
BlobReader
self
- position ()
-
Returns the current read position as an offset from the start of the input data in bytes.
Returns:
-
number
Current read position in bytes
- setByteOrder (byteOrder)
-
Set source data byte order.
Parameters:
- byteOrder
string
Byte order.
Can be either
le
or<
for little endian,be
or>
for big endian, orhost
ornil
for native host byte order.
Returns:
-
BlobReader
self
- byteOrder
string
Byte order.