Firmware
Classes | Macros | Typedefs | Enumerations | Functions
tinybson.h File Reference

A simple subset SAX-style BSON parser and generator. More...

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  bson_node_s
 Node structure passed to the callback. More...
 
struct  bson_decoder_s
 
struct  bson_encoder_s
 Encoder state structure. More...
 

Macros

#define BSON_MAXNAME   32
 Maximum node name length.
 
#define BSON_BUF_INCREMENT   128
 Buffer growth increment when writing to a buffer.
 

Typedefs

typedef enum bson_binary_subtype bson_binary_subtype_t
 
typedef struct bson_node_sbson_node_t
 Node structure passed to the callback.
 
typedef struct bson_decoder_sbson_decoder_t
 
typedef int(* bson_decoder_callback) (bson_decoder_t decoder, void *priv, bson_node_t node)
 Node callback. More...
 
typedef struct bson_encoder_sbson_encoder_t
 Encoder state structure.
 

Enumerations

enum  bson_type_t {
  BSON_EOO = 0, BSON_DOUBLE = 1, BSON_STRING = 2, BSON_OBJECT = 3,
  BSON_ARRAY = 4, BSON_BINDATA = 5, BSON_UNDEFINED = 6, BSON_BOOL = 8,
  BSON_DATE = 9, BSON_nullptr = 10, BSON_INT32 = 16, BSON_INT64 = 18
}
 subset of the BSON node types we might care about
 
enum  bson_binary_subtype { BSON_BIN_BINARY = 0, BSON_BIN_USER = 128 }
 

Functions

__EXPORT int bson_decoder_init_file (bson_decoder_t decoder, int fd, bson_decoder_callback callback, void *priv)
 Initialise the decoder to read from a file. More...
 
__EXPORT int bson_decoder_init_buf (bson_decoder_t decoder, void *buf, unsigned bufsize, bson_decoder_callback callback, void *priv)
 Initialise the decoder to read from a buffer in memory. More...
 
__EXPORT int bson_decoder_next (bson_decoder_t decoder)
 Process the next node from the stream and invoke the callback. More...
 
__EXPORT int bson_decoder_copy_data (bson_decoder_t decoder, void *buf)
 Copy node data. More...
 
__EXPORT size_t bson_decoder_data_pending (bson_decoder_t decoder)
 Report copyable data size. More...
 
__EXPORT int bson_encoder_init_file (bson_encoder_t encoder, int fd)
 Initialze the encoder for writing to a file. More...
 
__EXPORT int bson_encoder_init_buf_file (bson_encoder_t encoder, int fd, void *buf, unsigned bufsize)
 Initialze the encoder for writing to a file. More...
 
__EXPORT int bson_encoder_init_buf (bson_encoder_t encoder, void *buf, unsigned bufsize)
 Initialze the encoder for writing to a buffer. More...
 
__EXPORT int bson_encoder_fini (bson_encoder_t encoder)
 Finalise the encoded stream. More...
 
__EXPORT int bson_encoder_buf_size (bson_encoder_t encoder)
 Fetch the size of the encoded object; only valid for buffer operations.
 
__EXPORT void * bson_encoder_buf_data (bson_encoder_t encoder)
 Get a pointer to the encoded object buffer. More...
 
__EXPORT int bson_encoder_append_bool (bson_encoder_t encoder, const char *name, bool value)
 Append a boolean to the encoded stream. More...
 
__EXPORT int bson_encoder_append_int (bson_encoder_t encoder, const char *name, int64_t value)
 Append an integer to the encoded stream. More...
 
__EXPORT int bson_encoder_append_double (bson_encoder_t encoder, const char *name, double value)
 Append a double to the encoded stream. More...
 
__EXPORT int bson_encoder_append_string (bson_encoder_t encoder, const char *name, const char *string)
 Append a string to the encoded stream. More...
 
__EXPORT int bson_encoder_append_binary (bson_encoder_t encoder, const char *name, bson_binary_subtype_t subtype, size_t size, const void *data)
 Append a binary blob to the encoded stream. More...
 

Detailed Description

A simple subset SAX-style BSON parser and generator.

See http://bsonspec.org

Some types and defines taken from the standalone BSON parser/generator in the Mongo C connector.

Typedef Documentation

§ bson_decoder_callback

typedef int(* bson_decoder_callback) (bson_decoder_t decoder, void *priv, bson_node_t node)

Node callback.

The node callback function's return value is returned by bson_decoder_next.

Function Documentation

§ bson_decoder_copy_data()

__EXPORT int bson_decoder_copy_data ( bson_decoder_t  decoder,
void *  buf 
)

Copy node data.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.

§ bson_decoder_data_pending()

__EXPORT size_t bson_decoder_data_pending ( bson_decoder_t  decoder)

Report copyable data size.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.

§ bson_decoder_init_buf()

__EXPORT int bson_decoder_init_buf ( bson_decoder_t  decoder,
void *  buf,
unsigned  bufsize,
bson_decoder_callback  callback,
void *  priv 
)

Initialise the decoder to read from a buffer in memory.

Parameters
decoderDecoder state structure to be initialised.
bufBuffer to read from.
bufsizeSize of the buffer (BSON object may be smaller). May be passed as zero if the buffer size should be extracted from the BSON header only.
callbackCallback to be invoked by bson_decoder_next
privCallback private data, stored in node.
Returns
Zero on success.

§ bson_decoder_init_file()

__EXPORT int bson_decoder_init_file ( bson_decoder_t  decoder,
int  fd,
bson_decoder_callback  callback,
void *  priv 
)

Initialise the decoder to read from a file.

Parameters
decoderDecoder state structure to be initialised.
fdFile to read BSON data from.
callbackCallback to be invoked by bson_decoder_next
privCallback private data, stored in node.
Returns
Zero on success.

§ bson_decoder_next()

__EXPORT int bson_decoder_next ( bson_decoder_t  decoder)

Process the next node from the stream and invoke the callback.

Parameters
decoderDecoder state, must have been initialised with bson_decoder_init.
Returns
-1 if parsing encountered an error, 0 if the BSON stream has ended, otherwise the return value from the callback.

§ bson_encoder_append_binary()

__EXPORT int bson_encoder_append_binary ( bson_encoder_t  encoder,
const char *  name,
bson_binary_subtype_t  subtype,
size_t  size,
const void *  data 
)

Append a binary blob to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
subtypeBinary data subtype.
sizeData size.
dataBuffer containing data to be encoded.

§ bson_encoder_append_bool()

__EXPORT int bson_encoder_append_bool ( bson_encoder_t  encoder,
const char *  name,
bool  value 
)

Append a boolean to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

§ bson_encoder_append_double()

__EXPORT int bson_encoder_append_double ( bson_encoder_t  encoder,
const char *  name,
double  value 
)

Append a double to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

§ bson_encoder_append_int()

__EXPORT int bson_encoder_append_int ( bson_encoder_t  encoder,
const char *  name,
int64_t  value 
)

Append an integer to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
valueValue to be encoded.

§ bson_encoder_append_string()

__EXPORT int bson_encoder_append_string ( bson_encoder_t  encoder,
const char *  name,
const char *  string 
)

Append a string to the encoded stream.

Parameters
encoderEncoder state.
nameNode name.
stringNul-terminated C string.

§ bson_encoder_buf_data()

__EXPORT void* bson_encoder_buf_data ( bson_encoder_t  encoder)

Get a pointer to the encoded object buffer.

Note that if the buffer was allocated by the encoder, it is the caller's responsibility to free this buffer.

§ bson_encoder_fini()

__EXPORT int bson_encoder_fini ( bson_encoder_t  encoder)

Finalise the encoded stream.

Parameters
encoderThe encoder to finalise.

§ bson_encoder_init_buf()

__EXPORT int bson_encoder_init_buf ( bson_encoder_t  encoder,
void *  buf,
unsigned  bufsize 
)

Initialze the encoder for writing to a buffer.

Parameters
encoderEncoder state structure to be initialised.
bufBuffer pointer to use, or nullptr if the buffer should be allocated by the encoder.
bufsizeMaximum buffer size, or zero for no limit. If the buffer is supplied, the size of the supplied buffer.
Returns
Zero on success.

§ bson_encoder_init_buf_file()

__EXPORT int bson_encoder_init_buf_file ( bson_encoder_t  encoder,
int  fd,
void *  buf,
unsigned  bufsize 
)

Initialze the encoder for writing to a file.

Parameters
encoderEncoder state structure to be initialised.
fdFile to write to.
bufBuffer pointer to use, can't be nullptr
bufsizeSupplied buffer size
Returns
Zero on success.

§ bson_encoder_init_file()

__EXPORT int bson_encoder_init_file ( bson_encoder_t  encoder,
int  fd 
)

Initialze the encoder for writing to a file.

Parameters
encoderEncoder state structure to be initialised.
fdFile to write to.
Returns
Zero on success.