blob: 2805d8142a59b4b90dcc4c48ffabe366f0b60cdb [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompsonc8963652013-06-28 20:17:43 -07004 */
5
6#ifndef NDN_BINARYXMLENCODER_H
7#define NDN_BINARYXMLENCODER_H
8
9#include "../util/DynamicUCharArray.h"
Jeff Thompson5a984832013-07-01 19:28:27 -070010#include "BinaryXML.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070011
12#ifdef __cplusplus
13extern "C" {
14#endif
15
Jeff Thompson5a984832013-07-01 19:28:27 -070016/** An ndn_BinaryXMLEncoder struct is used by all the encoding functions. You should initialize it with
17 * ndn_BinaryXMLEncoder_init.
18 */
Jeff Thompsonc8963652013-06-28 20:17:43 -070019struct ndn_BinaryXMLEncoder {
20 struct ndn_DynamicUCharArray output; /**< receives the encoded output */
21 unsigned int offset; /**< the offset into output.array for the next encoding */
22};
23
24/**
25 * Initialize an ndn_BinaryXMLEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray.
26 * @param self pointer to the ndn_BinaryXMLEncoder struct
27 * @param outputArray the allocated array buffer to receive the encoding
28 * @param outputArrayLength the length of outputArray
29 * @param reallocFunction the realloc function used by ndn_DynamicUCharArray_ensureLength. If outputArrayLength
30 * is large enough to receive the entire encoding, this can be 0.
31 */
32static inline void ndn_BinaryXMLEncoder_init
33 (struct ndn_BinaryXMLEncoder *self, unsigned char *outputArray, unsigned int outputArrayLength,
Jeff Thompson5a984832013-07-01 19:28:27 -070034 unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
Jeff Thompsonc8963652013-06-28 20:17:43 -070035{
36 ndn_DynamicUCharArray_init(&self->output, outputArray, outputArrayLength, reallocFunction);
37 self->offset = 0;
38}
39
Jeff Thompson5a984832013-07-01 19:28:27 -070040/**
41 * Encode a header with the type and value and write it to self->output.
42 * @param self pointer to the ndn_BinaryXMLEncoder struct
43 * @param type the header type
44 * @param value the header value
45 * @return 0 for success, else an error string
46 */
Jeff Thompson433e6da2013-07-01 15:09:00 -070047char *ndn_BinaryXMLEncoder_encodeTypeAndValue(struct ndn_BinaryXMLEncoder *self, unsigned int type, unsigned int value);
48
Jeff Thompson5a984832013-07-01 19:28:27 -070049/**
50 * Write an element start header using DTAG with the tag to self->output.
51 * @param self pointer to the ndn_BinaryXMLEncoder struct
52 * @param tag the DTAG tag
53 * @return 0 for success, else an error string
54 */
55static inline char *ndn_BinaryXMLEncoder_writeElementStartDTag(struct ndn_BinaryXMLEncoder *self, unsigned int tag)
56{
57 return ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_DTAG, tag);
58}
59
60/**
61 * Write an element close to self->output.
62 * @param self pointer to the ndn_BinaryXMLEncoder struct
63 * @return 0 for success, else an error string
64 */
65char *ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self);
66
67/**
68 * Write a BLOB header, then the bytes of the blob value to self->output.
69 * @param self pointer to the ndn_BinaryXMLEncoder struct
70 * @param value an array of bytes for the blob value
71 * @param valueLength the length of the array
72 * @return 0 for success, else an error string
73 */
74char *ndn_BinaryXMLEncoder_writeBlob(struct ndn_BinaryXMLEncoder *self, unsigned char *value, unsigned int valueLength);
75
76/**
77 * Write an element start header using DTAG with the tag to self->output, then the blob, then an element close.
78 * (If you want to just write the blob, use ndn_BinaryXMLEncoder_writeBlob .)
79 * @param self pointer to the ndn_BinaryXMLEncoder struct
80 * @param tag the DTAG tag
81 * @param value an array of bytes for the blob value
82 * @param valueLength the length of the array
83 * @return 0 for success, else an error string
84 */
85char *ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength);
86
Jeff Thompsonc8963652013-06-28 20:17:43 -070087#ifdef __cplusplus
88}
89#endif
90
91#endif
92