blob: 4df49b46d75f6e54b4f2d2cbfab667b0e9836279 [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 Thompson53412192013-08-06 13:35:50 -070010#include "../util/dynamic-uchar-array.h"
11#include "binary-xml.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070012
13#ifdef __cplusplus
14extern "C" {
15#endif
16
Jeff Thompsonf0fea002013-07-30 17:22:42 -070017/** An ndn_BinaryXmlEncoder struct is used by all the encoding functions. You should initialize it with
18 * ndn_BinaryXmlEncoder_init.
Jeff Thompson5a984832013-07-01 19:28:27 -070019 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070020struct ndn_BinaryXmlEncoder {
Jeff Thompsonc8963652013-06-28 20:17:43 -070021 struct ndn_DynamicUCharArray output; /**< receives the encoded output */
22 unsigned int offset; /**< the offset into output.array for the next encoding */
23};
24
25/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070026 * Initialize an ndn_BinaryXmlEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray.
27 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonc8963652013-06-28 20:17:43 -070028 * @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 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070033static 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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070043 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070044 * @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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070053 * @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 Thompsonf0fea002013-07-30 17:22:42 -070056static inline ndn_Error ndn_BinaryXmlEncoder_writeElementStartDTag(struct ndn_BinaryXmlEncoder *self, unsigned int tag)
Jeff Thompson5a984832013-07-01 19:28:27 -070057{
Jeff Thompsonf0fea002013-07-30 17:22:42 -070058 return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag);
Jeff Thompson5a984832013-07-01 19:28:27 -070059}
60
61/**
62 * Write an element close to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070063 * @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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070070 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070071 * @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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070079 * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .)
80 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070081 * @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 Thompsonf0fea002013-07-30 17:22:42 -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 Thompsonf0fea002013-07-30 17:22:42 -070089 * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement.
90 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsona8749a52013-07-11 11:48:05 -070091 * @param tag the DTAG tag
92 * @param value an array of bytes for the blob value
93 * @param valueLength the length of the array
94 * @return 0 for success, else an error code
95 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070096static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
97 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength)
Jeff Thompsona8749a52013-07-11 11:48:05 -070098{
99 if (value && valueLength > 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700100 return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength);
Jeff Thompsona8749a52013-07-11 11:48:05 -0700101 else
Jeff Thompson2d62c932013-07-11 12:09:30 -0700102 return (ndn_Error)0;
Jeff Thompsona8749a52013-07-11 11:48:05 -0700103}
104
105/**
Jeff Thompson5b696e02013-07-08 15:04:22 -0700106 * Write a UDATA header, then the value as an unsigned decimal integer.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700107 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsone2276892013-07-08 02:44:18 -0700108 * @param value the unsigned int
109 * @return 0 for success, else an error code
110 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700111ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value);
Jeff Thompsone2276892013-07-08 02:44:18 -0700112
Jeff Thompson5b696e02013-07-08 15:04:22 -0700113/**
114 * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer,
115 * then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700116 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
117 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5b696e02013-07-08 15:04:22 -0700118 * @param tag the DTAG tag
119 * @param value the unsigned int
120 * @return 0 for success, else an error code
121 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700122ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned int value);
Jeff Thompson5b696e02013-07-08 15:04:22 -0700123
Jeff Thompsona259cc42013-07-08 17:14:09 -0700124/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700125 * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement.
126 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson45492a72013-07-11 12:02:47 -0700127 * @param tag the DTAG tag
128 * @param value negative for none, otherwise use (unsigned int)value
129 * @return 0 for success, else an error code
130 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700131static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, int value)
Jeff Thompson45492a72013-07-11 12:02:47 -0700132{
133 if (value >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700134 return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value);
Jeff Thompson45492a72013-07-11 12:02:47 -0700135 else
Jeff Thompson2d62c932013-07-11 12:09:30 -0700136 return (ndn_Error)0;
Jeff Thompson45492a72013-07-11 12:02:47 -0700137}
138
139/**
Jeff Thompsonedc22252013-07-11 18:05:44 -0700140 * Write a BLOB header, then the absolute value of value to self->output encoded as big endian.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700141 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700142 * @param value the double to encode as big endian. If value is 0, the big endian encoding has zero bytes.
143 * The value is converted to absolute value.
Jeff Thompsona259cc42013-07-08 17:14:09 -0700144 * @return 0 for success, else an error code
145 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700146ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700147
148/**
149 * Write an element start header using DTAG with the tag to self->output, then the absolute value of milliseconds
150 * as a big endian BLOB converted to 4096 ticks per second, then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700151 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
152 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700153 * @param tag the DTAG tag
154 * @param milliseconds the the number of milliseconds
155 * @return 0 for success, else an error code
156 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700157ndn_Error ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700158
159/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700160 * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement.
161 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700162 * @param tag the DTAG tag
163 * @param milliseconds negative for none, otherwise the number of milliseconds
164 * @return 0 for success, else an error code
165 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700166static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
167 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds)
Jeff Thompsonedc22252013-07-11 18:05:44 -0700168{
169 if (milliseconds >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700170 return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700171 else
172 return (ndn_Error)0;
173}
Jeff Thompsona259cc42013-07-08 17:14:09 -0700174
Jeff Thompsonc8963652013-06-28 20:17:43 -0700175#ifdef __cplusplus
176}
177#endif
178
179#endif
180