typedef struct nbt_tag_t nbt_tag_t;
struct nbt_tag_t {
nbt_tag_type_t type;
char* name;
size_t name_size;
union {
struct {
int8_t value;
} tag_byte;
struct {
int16_t value;
} tag_short;
struct {
int32_t value;
} tag_int;
struct {
int64_t value;
} tag_long;
struct {
float value;
} tag_float;
struct {
double value;
} tag_double;
struct {
int8_t* value;
size_t size;
} tag_byte_array;
struct {
char* value;
size_t size;
} tag_string;
struct {
nbt_tag_t** value;
nbt_tag_type_t type;
size_t size;
} tag_list;
struct {
nbt_tag_t** value;
size_t size;
} tag_compound;
struct {
int32_t* value;
size_t size;
} tag_int_array;
struct {
int64_t* value;
size_t size;
} tag_long_array;
struct {
int16_t* value;
size_t size;
} tag_short_array;
struct {
float* value;
size_t size;
} tag_float_array;
struct {
double* value;
size_t size;
} tag_double_array;
struct {
nbt_tag_t** value;
size_t size;
} tag_string_array;
};
};
nbt_tag_t
is a struct which represents a single NBT tag. It contains the type of the tag as well as the name and the value of the tag.
type
: The type of the tag (see thenbt_tag_type_t
enum).name
: The name of the tag. If the tag does not have a name (e.g. members of a list), this will be a null pointer. This string is guaranteed to be null terminated for convenience, but embedded nulls may also be present.name_size
: The number of bytes used to store the name, excluding the null terminator. If non-ASCII characters are used, this may not be equal to the number of characters in the name. If the tag does not have a name, this will be 0.tag_xxx
(wherexxx
is an NBT tag type, in lower case): The value of the NBT tag. Only the one corresponding to the tag's type should be accessed, with the values of the other members being undefined.
typedef struct {
size_t (*read)(void* userdata, uint8_t* data, size_t size);
void* userdata;
} nbt_reader_t;
nbt_reader_t
is a struct which can be used to read NBT data from an arbitrary source.
It contains a pointer to a function used to read data, and a user-provided pointer which is passed to this function.
read
: A pointer to the function which is used to read data. The function is expected to copy up tosize
bytes into the buffer pointed to bydata
, with the return value being the number of bytes actually read.userdata
: An arbitrary, user-provided pointer which is passed as theuserdata
parameter to the aforementionedread
function.
typedef struct {
size_t (*write)(void* userdata, uint8_t* data, size_t size);
void* userdata;
} nbt_writer_t;
nbt_writer_t
is a struct which can be used to write NBT data to an arbitrary destination.
It contains a pointer to a function used to write data, and a user-provided pointer which is passed to this function.
write
: A pointer to the function which is used to write data. The function is expected to write up tosize
bytes from the buffer pointed to bydata
, with the return value being the number of bytes actually written.userdata
: An arbitrary, user-provided pointer which is passed as theuserdata
parameter to the aforementionedwrite
function.
typedef enum {
NBT_TYPE_END,
NBT_TYPE_BYTE,
NBT_TYPE_SHORT,
NBT_TYPE_INT,
NBT_TYPE_LONG,
NBT_TYPE_FLOAT,
NBT_TYPE_DOUBLE,
NBT_TYPE_BYTE_ARRAY,
NBT_TYPE_STRING,
NBT_TYPE_LIST,
NBT_TYPE_COMPOUND,
NBT_TYPE_INT_ARRAY,
NBT_TYPE_LONG_ARRAY,
NBT_TYPE_SHORT_ARRAY,
NBT_TYPE_FLOAT_ARRAY,
NBT_TYPE_SHORT_ARRAY,
NBT_TYPE_DOUBLE_ARRAY,
NBT_TYPE_STRING_ARRAY,
NBT_NO_OVERRIDE // Only used internally.
} nbt_tag_type_t;
Represents an NBT tag type. Each type (including TAG_End
) can be represented and has the same integer value as in the official specification.
NBT_NO_OVERRIDE
is only used internally and will not appear in any output provided by the library, and must not be used in any input provided to it.
typedef enum {
NBT_PARSE_FLAG_USE_GZIP = 1,
NBT_PARSE_FLAG_USE_ZLIB = 2,
NBT_PARSE_FLAG_USE_RAW = 3,
} nbt_parse_flags_t;
Represents flags which can be provided to nbt_parse
. See nbt_parse
for their meanings.
typedef enum {
NBT_WRITE_FLAG_USE_GZIP = 1,
NBT_WRITE_FLAG_USE_ZLIB = 2,
NBT_WRITE_FLAG_USE_RAW = 3
} nbt_write_flags_t;
Represents flags which can be provided to nbt_write
. See nbt_write
for their meanings.
nbt_tag_t* nbt_parse(nbt_reader_t reader, int parse_flags);
Parses a stream of bytes provided by reader
into memory. Both raw and compressed streams are supported, with both zlib and Gzip formatted compressed streams being supported.
reader
: Thenbt_reader_t
struct used to provide input.parse_flags
: Flags used to control parsing.NBT_PARSE_FLAG_FORCE_GZIP
: Used to force Gzip decompression (as used by most .nbt files)NBT_PARSE_FLAG_FORCE_ZLIB
: Used to force zlib decompression (as used by chunks stored in .mca files).NBT_PARSE_FLAG_FORCE_RAW
: Used to force no decompression.
The root tag of the parsed NBT structure, or NULL
if parsing was unsuccessful.
This value is dynamically allocated and should be freed using nbt_free_tag
.
void nbt_write(nbt_writer_t writer, nbt_tag_t* tag, int write_flags);
Converts the NBT tag structure tag
to bytes and writes them to a stream provided by writer
. Both raw and compressed streams are supported, with both zlib and Gzip formatted compressed streams being supported.
writer
: Thenbt_writer_t
struct used to provide output.tag
: The tag structure to be written.write_flags
: Flags used to control parsing.NBT_WRITE_FLAG_FORCE_GZIP
: Used to force Gzip decompression (as used by most .nbt files)NBT_WRITE_FLAG_FORCE_ZLIB
: Used to force zlib decompression (as used by chunks stored in .mca files).NBT_WRITE_FLAG_FORCE_RAW
: Used to force no decompression.
None.
nbt_tag_t* nbt_new_tag_byte(int8_t value);
nbt_tag_t* nbt_new_tag_short(int16_t value);
nbt_tag_t* nbt_new_tag_int(int32_t value);
nbt_tag_t* nbt_new_tag_long(int64_t value);
nbt_tag_t* nbt_new_tag_float(float value);
nbt_tag_t* nbt_new_tag_double(double value);
nbt_tag_t* nbt_new_tag_byte_array(int8_t* value, size_t size);
nbt_tag_t* nbt_new_tag_string(const char* value);
nbt_tag_t* nbt_new_tag_string0(const char* value, size_t size);
nbt_tag_t* nbt_new_tag_list(nbt_tag_type_t type);
nbt_tag_t* nbt_new_tag_compound(void);
nbt_tag_t* nbt_new_tag_int_array(int32_t* value, size_t size);
nbt_tag_t* nbt_new_tag_long_array(int64_t* value, size_t size);
nbt_tag_t* nbt_new_tag_short_array(int16_t* value, size_t size);
nbt_tag_t* nbt_new_tag_float_array(float* value, size_t size);
nbt_tag_t* nbt_new_tag_double_array(double* value, size_t size);
nbt_tag_t* nbt_new_tag_string_array(char** value, size_t size);
Creates a new tag of the given type.
The newly created type will have no name. Use nbt_tag_set_name
to set its name.
value
: The value with which to initialise the tag.
Parameters (byte_array
, string
, int_array
, long_array
, short_array
, float_array
, double_array
, string_array
)
value
: The array of values with which to initialise the tag. This is copied, so it is safe to modify or free the value passed to this parameter later. In the case ofstring
, the copy will be null-terminated for convenience, but embedded nulls may be present.size
: The size of the array of values passed tovalue
, in elements.
type
: The type of the entries of the list.
None.
The newly created tag. This value is dynamically allocated and should be freed using nbt_free_tag
.
void nbt_set_tag_name(nbt_tag_t* tag, const char* name);
Sets the name of the tag.
tag
: The tag to set the name of. This is copied, so it is safe to modify or free the value passed to this parameter later.name
: The new name of the tag.- By default the size of the string
name
without \0 end caracter is taken.
None.
void nbt_set_tag_name0(nbt_tag_t* tag, const char* name, size_t size);
Sets the name of the tag.
tag
: The tag to set the name of. This is copied, so it is safe to modify or free the value passed to this parameter later.name
: The new name of the tag.size
: The size of the name, in bytes.
None.
void nbt_tag_list_append(nbt_tag_t* list, nbt_tag_t* value);
Appends a tag to the end of a list tag.
list
: The list tag to append to.value
: The tag to be appended.
None.
nbt_tag_t* nbt_tag_list_get(nbt_tag_t* tag, size_t index);
Gets the tag at index index
of list tag tag
.
This is equivalent to tag->tag_list.value[index]
and is provided for consistency with nbt_tag_compound_get
.
tag
: The list tag.index
: The index to get.
The tag at index
.
void nbt_tag_compound_append(nbt_tag_t* compound, nbt_tag_t* value);
Appends a tag to a compound tag.
The key of the tag is simply its name.
The result of appending multiple tags with the same name is undefined.
tag
: The compound tag to append to.value
: The tag to append.
None.
nbt_tag_t* nbt_tag_compound_get(nbt_tag_t* tag, const char* key);
Gets the tag with key key
in compound tag tag
.
tag
: The list tag.key
: The key to search for.
The tag with key key
, or NULL
if no tag with that key was found.
void nbt_free_tag(nbt_tag_t* tag);
Frees the memory allocated for tag
.
In the case of list and compound tags, this function is called recursively on all children, so they do not need to be freed manually.
tag
: The tag to free.
None.
int8_t nbt_pack_8bits(bool* array, uint8_t size);
int16_t nbt_pack_16bits(bool* array, uint16_t size);
int32_t nbt_pack_32bits(bool* array, uint32_t size);
Pack a multiple of 8 bits from a boolean array to its integer equivalent. Order is based on your system (little/big endian) when you use the hardware acceleration or multiply bit macro.
array
: The unpacked array to transform.
size
: The number of element in this array used during the process. The size cannot be greater than the number of bits in the returned integer (8/16/32).
The packed integer.
bool* nbt_unpack_8bits(int8_t value, int size);
bool* nbt_unpack_16bits(int16_t value, int size);
bool* nbt_unpack_32bits(int32_t value, int size);
Unpack a multiple of 8 bits from an integer to a boolean array. Order is based on your system (little/big endian) when you use the hardware acceleration or multiply bit macro.
value
: The packed integer to transform.
size
: The number of element in this array used during the process. The size cannot be greater than the number of bits ofvalue
(8/16/32).
The unpacked boolean array.