libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
NBT

Classes

struct  clod_nbt_iter

Macros

#define CLOD_NBT_ZERO   (char)(0)
#define CLOD_NBT_INT8   (char)(1)
#define CLOD_NBT_INT16   (char)(2)
#define CLOD_NBT_INT32   (char)(3)
#define CLOD_NBT_INT64   (char)(4)
#define CLOD_NBT_FLOAT32   (char)(5)
#define CLOD_NBT_FLOAT64   (char)(6)
#define CLOD_NBT_INT8_ARRAY   (char)(7)
#define CLOD_NBT_INT32_ARRAY   (char)(11)
#define CLOD_NBT_INT64_ARRAY   (char)(12)
#define CLOD_NBT_STRING   (char)(8)
#define CLOD_NBT_LIST   (char)(9)
#define CLOD_NBT_COMPOUND   (char)(10)
#define CLOD_NBT_ROOT_COMPOUND_INIT   ((char[]){CLOD_NBT_COMPOUND, 0, 0, 0})
#define CLOD_NBT_ROOT_LIST_INIT(type)
#define CLOD_NBT_ITER_ZERO   { .type = CLOD_NBT_ZERO }

Functions

size_t clod_nbt_payload_size (const char *restrict payload, const void *end, char payload_type)
size_t clod_nbt_tag_size (const char *restrict tag, const void *end)
char * clod_nbt_tag_payload (const char *restrict tag, const void *end)
clod_sstr clod_nbt_tag_name (const char *tag, const void *end)
bool clod_nbt_iter_next (const char *restrict payload, const void *end, char payload_type, struct clod_nbt_iter *iter)
char * clod_nbt_compound_get (const char *restrict compound, const void *end, clod_sstr name)
char * clod_nbt_compound_add (char *restrict compound, const void **end, ptrdiff_t *free, clod_sstr name, char type)
bool clod_nbt_compound_del (char *restrict compound, const void **end, ptrdiff_t *free, clod_sstr name)
bool clod_nbt_list_resize (char *restrict list, const char **end, ptrdiff_t *free, char type, uint32_t length)

Detailed Description

The NBT format is a serialised depth-first tree with no indexing. The primary limitation of interacting with such a structure is simply figuring out where the nodes actually are. To find a given NBT, every single tag before it must be recursively parsed to find the wanted NBT's offset. The majority of the entire tree must be parsed for most operations. As such, traversing the tree becomes the core limitation of any NBT operation and the primary focus for optimisation. This traversal method is clod_nbt_payload_size.

The library only performs some basic sanity checks on top of ensuring memory safety. Implementing complex data analysis to discern the likelihood that a given set of NBT data has been modified from its original state is an insane alternative to using a checksum and out of scope for this library. Please, for the love of good code, rely on dedicated verification methods instead of incidental parsing errors.

Macro Definition Documentation

◆ CLOD_NBT_ZERO

#define CLOD_NBT_ZERO   (char)(0)

Definition at line 28 of file nbt.h.

◆ CLOD_NBT_INT8

#define CLOD_NBT_INT8   (char)(1)

Definition at line 29 of file nbt.h.

◆ CLOD_NBT_INT16

#define CLOD_NBT_INT16   (char)(2)

Definition at line 30 of file nbt.h.

◆ CLOD_NBT_INT32

#define CLOD_NBT_INT32   (char)(3)

Definition at line 31 of file nbt.h.

◆ CLOD_NBT_INT64

#define CLOD_NBT_INT64   (char)(4)

Definition at line 32 of file nbt.h.

◆ CLOD_NBT_FLOAT32

#define CLOD_NBT_FLOAT32   (char)(5)

Definition at line 33 of file nbt.h.

◆ CLOD_NBT_FLOAT64

#define CLOD_NBT_FLOAT64   (char)(6)

Definition at line 34 of file nbt.h.

◆ CLOD_NBT_INT8_ARRAY

#define CLOD_NBT_INT8_ARRAY   (char)(7)

Definition at line 35 of file nbt.h.

◆ CLOD_NBT_INT32_ARRAY

#define CLOD_NBT_INT32_ARRAY   (char)(11)

Definition at line 36 of file nbt.h.

◆ CLOD_NBT_INT64_ARRAY

#define CLOD_NBT_INT64_ARRAY   (char)(12)

Definition at line 37 of file nbt.h.

◆ CLOD_NBT_STRING

#define CLOD_NBT_STRING   (char)(8)

Definition at line 38 of file nbt.h.

◆ CLOD_NBT_LIST

#define CLOD_NBT_LIST   (char)(9)

Definition at line 39 of file nbt.h.

◆ CLOD_NBT_COMPOUND

#define CLOD_NBT_COMPOUND   (char)(10)

Definition at line 40 of file nbt.h.

◆ CLOD_NBT_ROOT_COMPOUND_INIT

#define CLOD_NBT_ROOT_COMPOUND_INIT   ((char[]){CLOD_NBT_COMPOUND, 0, 0, 0})

Definition at line 42 of file nbt.h.

◆ CLOD_NBT_ROOT_LIST_INIT

#define CLOD_NBT_ROOT_LIST_INIT ( type)
Value:
((char[]){CLOD_NBT_LIST, type, 0, 0, 0, 0})

Definition at line 43 of file nbt.h.

◆ CLOD_NBT_ITER_ZERO

#define CLOD_NBT_ITER_ZERO   { .type = CLOD_NBT_ZERO }

Definition at line 109 of file nbt.h.

Function Documentation

◆ clod_nbt_payload_size()

size_t clod_nbt_payload_size ( const char *restrict payload,
const void * end,
char payload_type )

Get the size of a payload. This is the primary NBT traversing function; everything else is built on top of this.

Parameters
[in]payloadThe payload to get the size of.
[in]payload_typeThe type of the payload.
[in]endEnd of the NBT data.
Returns
The size of the payload, or 0 on failure.

Definition at line 26 of file nbt.c.

◆ clod_nbt_tag_size()

size_t clod_nbt_tag_size ( const char *restrict tag,
const void * end )

Get the size of a tag including its payload.

Parameters
[in]tagThe tag to get the size of.
[in]endEnd of the NBT data.
Returns
Size of the tag, or 0 on failure.

Definition at line 94 of file nbt.c.

◆ clod_nbt_tag_payload()

char * clod_nbt_tag_payload ( const char *restrict tag,
const void * end )

Get a tag's payload.

Parameters
[in]tagThe tag to get the payload of.
[in]endPoint past which the method will never read.
Returns
The tag's payload.

Definition at line 102 of file nbt.c.

◆ clod_nbt_tag_name()

clod_sstr clod_nbt_tag_name ( const char * tag,
const void * end )

Get the name of a tag.

Parameters
[in]tagThe tag to get the name of.
[in]endPoint past which the method will never read.
Returns
The tag's name.

◆ clod_nbt_iter_next()

bool clod_nbt_iter_next ( const char *restrict payload,
const void * end,
char payload_type,
struct clod_nbt_iter * iter )

Iterate over elements in a payload.

Parameters
[in]payloadThe payload whose elements are to be iterated over.
[in]endEnd of NBT data.
[in]payload_typeType of the payload. An invalid payload_type makes the function a false-returning no-op.
[in,out]iterIterator. Upon completion (false return), iter.tag is always set to
Returns
True if the last element was found, false if it was not. A false return and null iter.payload field indicates error.

Definition at line 118 of file nbt.c.

◆ clod_nbt_compound_get()

char * clod_nbt_compound_get ( const char *restrict compound,
const void * end,
clod_sstr name )

Get an element in a compound payload.

Parameters
[in]compoundPayload to find element in.
[in]endEnd of the NBT data.
[in]nameName of the element.
Returns
Element tag, or null if none was found.

Definition at line 269 of file nbt.c.

◆ clod_nbt_compound_add()

char * clod_nbt_compound_add ( char *restrict compound,
const void ** end,
ptrdiff_t * free,
clod_sstr name,
char type )

Get or create an element in a compound payload.

Parameters
[in]compoundPayload to find or create element in. If compound is null, then the size that would be written on creation is subtracted from free.
[in,out]endEnd of the NBT data.
[in,out]freeFree space in the buffer. It is modified to reflect the change in NBT data size. A negative value after return indicates the writing failed due to lack of space.
[in]nameName of the element to search for.
[in]typeType of the new element if creation occurs.
Returns
Element tag if one was found, the created element if it was created, or null if there isn't enough free space.

Definition at line 281 of file nbt.c.

◆ clod_nbt_compound_del()

bool clod_nbt_compound_del ( char *restrict compound,
const void ** end,
ptrdiff_t * free,
clod_sstr name )

Delete an element in a compound payload.

Parameters
[in]compoundPayload to delete element in.
[in,out]endEnd of the NBT data.
[in,out]freeFree space in the buffer.
[in]nameName of the element to delete.
Returns
True on success, false on failure.

Definition at line 318 of file nbt.c.

◆ clod_nbt_list_resize()

bool clod_nbt_list_resize ( char *restrict list,
const char ** end,
ptrdiff_t * free,
char type,
uint32_t length )

Resize a list payload.

Parameters
[in]listThe list payload to resize.
[in,out]endEnd of NBT data.
[in,out]freeFree space in the buffer.
[in]typeIf non-zero, the type of list elements will be set to this. Changing types forces wiping all existing elements in the list.
[in]lengthNew length.
Returns
True on success, false on failure.

Definition at line 336 of file nbt.c.