#include <libnal/nal.h>
int NAL_decode_uint32(const unsigned char **bin, unsigned int *bin_len, unsigned long *val); int NAL_decode_uint16(const unsigned char **bin, unsigned int *bin_len, unsigned int *val); int NAL_decode_char(const unsigned char **bin, unsigned int *bin_len, unsigned char *val); int NAL_decode_bin(const unsigned char **bin, unsigned int *bin_len, unsigned char *val, unsigned int val_len);
int NAL_encode_uint32(unsigned char **bin, unsigned int *bin_len, const unsigned long val); int NAL_encode_uint16(unsigned char **bin, unsigned int *bin_len, const unsigned int val); int NAL_encode_char(unsigned char **bin, unsigned int *bin_len, const unsigned char val); int NAL_encode_bin(unsigned char **bin, unsigned int *bin_len, const unsigned char *val, const unsigned int val_len);
NAL_decode_bin() follows the semantics of the other decode functions except that it decodes a block of binary data of length val_len.
NAL_encode_uint32(), NAL_encode_uint16(), and NAL_encode_char() attempt to encode different sized integer values to the located pointed to by *bin (again, both bin and bin_len are passed by reference). If bin_len indicates there is sufficient room to successfully encode a value, val will be stored at *bin, *bin will be incremented to point to the next unused byte of storage, and *bin_len will be decremented to indicate how much unused storage remains.
NAL_encode_bin() follows the semantics of the other encode functions except that it encodes a block of binary data of length val_len.
#define MAX_DATA_SIZE 4096 typedef struct st_some_data_t { unsigned char is_active; /* boolean */ unsigned char buffer[MAX_DATA_SIZE]; unsigned int buffer_used; } some_data_t;
We could define two functions for encoding and decoding an object of this type such that they could be serialised and transferred over a connection. The most elegant way to build serialisation of objects is to create functions that use the same form of prototype as the libnal serialisation functions, this way serialisation of complex objects can be performed recursively by serialisation of aggregated types. Although the built-in libnal serialisation functions leave bin and bin_len unchanged on failure, it is generally not worth bothering to preserve this property at higher-levels - these examples do not attempt this.
An encoding function would thus look like;
int encode_some_data(unsigned char **bin, unsigned int *bin_len, const some_data_t *val) { if( /* Encode the "is_active" boolean */ !NAL_encode_char(bin, bin_len, val->is_active) || /* Encode the used data */ !NAL_encode_uint16(bin, bin_len, val->buffer_used) || ((val->buffer_used > 0) && !NAL_encode_bin(bin, bin_len, val->buffer, val->buffer_used))) return 0; return 1; }
Note that other types that include some_data_t objects could implement serialisation using encode_some_data() in the same way that encode_some_data() uses the lower-level libnal functions. A corresponding decode function follows.
int decode_some_data(const unsigned char **bin, unsigned int *bin_len, some_data_t *val) { if( /* Decode the "is_active" boolean */ !NAL_decode_char(bin, bin_len, &val->is_active) || /* Decode the used data */ !NAL_decode_uint16(bin, bin_len, &val->buffer_used) || /* [TODO: check 'val->buffer_used' is acceptable here] */ ((val->buffer_used > 0) && !NAL_decode_bin(bin, bin_len, val->buffer, val->buffer_used))) return 0; return 1; }
The above examples would be simpler still if a wrapper function were first written to serialise length-prefixed blocks of data. Such functions are not included in libnal because they can vary on what range of sizes are appropriate, what size encoding to use for a length-prefix, whether dynamic allocation should be used on decoding, etc. The above examples use a static buffer and encode the length prefix as 16-bits.
NAL_CONNECTION_new(2) - Functions for the NAL_CONNECTION type.
NAL_LISTENER_new(2) - Functions for the NAL_LISTENER type.
NAL_SELECTOR_new(2) - Functions for the NAL_SELECTOR type.
distcache(8) - Overview of the distcache architecture.
http://www.distcache.org/ - Distcache home page.
Home Page: http://www.distcache.org