blob: 8c5047c4b478987346729223d390ce04c60aaa5f [file] [log] [blame]
/*
* Author: Jeff Thompson
*
* BSD license, See the LICENSE file for more information.
*/
#ifndef NDN_BINARYXMLENCODER_H
#define NDN_BINARYXMLENCODER_H
#include "../util/DynamicUCharArray.h"
#include "BinaryXML.h"
#ifdef __cplusplus
extern "C" {
#endif
/** An ndn_BinaryXMLEncoder struct is used by all the encoding functions. You should initialize it with
* ndn_BinaryXMLEncoder_init.
*/
struct ndn_BinaryXMLEncoder {
struct ndn_DynamicUCharArray output; /**< receives the encoded output */
unsigned int offset; /**< the offset into output.array for the next encoding */
};
/**
* Initialize an ndn_BinaryXMLEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param outputArray the allocated array buffer to receive the encoding
* @param outputArrayLength the length of outputArray
* @param reallocFunction the realloc function used by ndn_DynamicUCharArray_ensureLength. If outputArrayLength
* is large enough to receive the entire encoding, this can be 0.
*/
static inline void ndn_BinaryXMLEncoder_init
(struct ndn_BinaryXMLEncoder *self, unsigned char *outputArray, unsigned int outputArrayLength,
unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
{
ndn_DynamicUCharArray_init(&self->output, outputArray, outputArrayLength, reallocFunction);
self->offset = 0;
}
/**
* Encode a header with the type and value and write it to self->output.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param type the header type
* @param value the header value
* @return 0 for success, else an error string
*/
char *ndn_BinaryXMLEncoder_encodeTypeAndValue(struct ndn_BinaryXMLEncoder *self, unsigned int type, unsigned int value);
/**
* Write an element start header using DTAG with the tag to self->output.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param tag the DTAG tag
* @return 0 for success, else an error string
*/
static inline char *ndn_BinaryXMLEncoder_writeElementStartDTag(struct ndn_BinaryXMLEncoder *self, unsigned int tag)
{
return ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_DTAG, tag);
}
/**
* Write an element close to self->output.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @return 0 for success, else an error string
*/
char *ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self);
/**
* Write a BLOB header, then the bytes of the blob value to self->output.
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param value an array of bytes for the blob value
* @param valueLength the length of the array
* @return 0 for success, else an error string
*/
char *ndn_BinaryXMLEncoder_writeBlob(struct ndn_BinaryXMLEncoder *self, unsigned char *value, unsigned int valueLength);
/**
* Write an element start header using DTAG with the tag to self->output, then the blob, then an element close.
* (If you want to just write the blob, use ndn_BinaryXMLEncoder_writeBlob .)
* @param self pointer to the ndn_BinaryXMLEncoder struct
* @param tag the DTAG tag
* @param value an array of bytes for the blob value
* @param valueLength the length of the array
* @return 0 for success, else an error string
*/
char *ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength);
#ifdef __cplusplus
}
#endif
#endif