blob: c2df38964d9901a18757029774522ce92593d9a5 [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
Jeff Thompson8b666002013-07-08 01:16:26 -07009#include "../errors.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070010#include "../util/DynamicUCharArray.h"
Jeff Thompson5a984832013-07-01 19:28:27 -070011#include "BinaryXML.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070012
13#ifdef __cplusplus
14extern "C" {
15#endif
16
Jeff Thompson5a984832013-07-01 19:28:27 -070017/** An ndn_BinaryXMLEncoder struct is used by all the encoding functions. You should initialize it with
18 * ndn_BinaryXMLEncoder_init.
19 */
Jeff Thompsonc8963652013-06-28 20:17:43 -070020struct ndn_BinaryXMLEncoder {
21 struct ndn_DynamicUCharArray output; /**< receives the encoded output */
22 unsigned int offset; /**< the offset into output.array for the next encoding */
23};
24
25/**
26 * Initialize an ndn_BinaryXMLEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray.
27 * @param self pointer to the ndn_BinaryXMLEncoder struct
28 * @param outputArray the allocated array buffer to receive the encoding
29 * @param outputArrayLength the length of outputArray
30 * @param reallocFunction the realloc function used by ndn_DynamicUCharArray_ensureLength. If outputArrayLength
31 * is large enough to receive the entire encoding, this can be 0.
32 */
33static inline void ndn_BinaryXMLEncoder_init
34 (struct ndn_BinaryXMLEncoder *self, unsigned char *outputArray, unsigned int outputArrayLength,
Jeff Thompson5a984832013-07-01 19:28:27 -070035 unsigned char * (*reallocFunction)(unsigned char *, unsigned int))
Jeff Thompsonc8963652013-06-28 20:17:43 -070036{
37 ndn_DynamicUCharArray_init(&self->output, outputArray, outputArrayLength, reallocFunction);
38 self->offset = 0;
39}
40
Jeff Thompson5a984832013-07-01 19:28:27 -070041/**
42 * Encode a header with the type and value and write it to self->output.
43 * @param self pointer to the ndn_BinaryXMLEncoder struct
44 * @param type the header type
45 * @param value the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070046 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070047 */
Jeff Thompson8b666002013-07-08 01:16:26 -070048ndn_Error ndn_BinaryXMLEncoder_encodeTypeAndValue(struct ndn_BinaryXMLEncoder *self, unsigned int type, unsigned int value);
Jeff Thompson433e6da2013-07-01 15:09:00 -070049
Jeff Thompson5a984832013-07-01 19:28:27 -070050/**
51 * Write an element start header using DTAG with the tag to self->output.
52 * @param self pointer to the ndn_BinaryXMLEncoder struct
53 * @param tag the DTAG tag
Jeff Thompson8b666002013-07-08 01:16:26 -070054 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070055 */
Jeff Thompson8b666002013-07-08 01:16:26 -070056static inline ndn_Error ndn_BinaryXMLEncoder_writeElementStartDTag(struct ndn_BinaryXMLEncoder *self, unsigned int tag)
Jeff Thompson5a984832013-07-01 19:28:27 -070057{
58 return ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_DTAG, tag);
59}
60
61/**
62 * Write an element close to self->output.
63 * @param self pointer to the ndn_BinaryXMLEncoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070064 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070065 */
Jeff Thompson8b666002013-07-08 01:16:26 -070066ndn_Error ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self);
Jeff Thompson5a984832013-07-01 19:28:27 -070067
68/**
69 * Write a BLOB header, then the bytes of the blob value to self->output.
70 * @param self pointer to the ndn_BinaryXMLEncoder struct
71 * @param value an array of bytes for the blob value
72 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070073 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070074 */
Jeff Thompson8b666002013-07-08 01:16:26 -070075ndn_Error ndn_BinaryXMLEncoder_writeBlob(struct ndn_BinaryXMLEncoder *self, unsigned char *value, unsigned int valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070076
77/**
78 * Write an element start header using DTAG with the tag to self->output, then the blob, then an element close.
79 * (If you want to just write the blob, use ndn_BinaryXMLEncoder_writeBlob .)
80 * @param self pointer to the ndn_BinaryXMLEncoder struct
81 * @param tag the DTAG tag
82 * @param value an array of bytes for the blob value
83 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070084 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070085 */
Jeff Thompson8b666002013-07-08 01:16:26 -070086ndn_Error ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070087
Jeff Thompsone2276892013-07-08 02:44:18 -070088/**
Jeff Thompson5b696e02013-07-08 15:04:22 -070089 * Write a UDATA header, then the value as an unsigned decimal integer.
Jeff Thompsone2276892013-07-08 02:44:18 -070090 * @param self pointer to the ndn_BinaryXMLEncoder struct
91 * @param value the unsigned int
92 * @return 0 for success, else an error code
93 */
94ndn_Error ndn_BinaryXMLEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXMLEncoder *self, unsigned int value);
95
Jeff Thompson5b696e02013-07-08 15:04:22 -070096/**
97 * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer,
98 * then an element close.
99 * (If you want to just write the integer, use ndn_BinaryXMLEncoder_writeUnsignedDecimalInt .)
100 * @param self pointer to the ndn_BinaryXMLEncoder struct
101 * @param tag the DTAG tag
102 * @param value the unsigned int
103 * @return 0 for success, else an error code
104 */
105ndn_Error ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned int value);
106
Jeff Thompsona259cc42013-07-08 17:14:09 -0700107/**
108 * Write a BLOB header, then the value to self->output encoded as big endian.
109 * @param self pointer to the ndn_BinaryXMLEncoder struct
110 * @param value the unsigned int to encode as big endian. If value is 0, the big endian encoding has zero bytes.
111 * @return 0 for success, else an error code
112 */
113ndn_Error ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(struct ndn_BinaryXMLEncoder *self, unsigned int value);
114
Jeff Thompsonc8963652013-06-28 20:17:43 -0700115#ifdef __cplusplus
116}
117#endif
118
119#endif
120