blob: c457185f62a1c469f5d3f67f1fa2e7abe57ec360 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonc8963652013-06-28 20:17:43 -07005 */
6
7#ifndef NDN_BINARYXMLENCODER_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_BINARYXMLENCODER_H
Jeff Thompsonc8963652013-06-28 20:17:43 -07009
Jeff Thompson8b666002013-07-08 01:16:26 -070010#include "../errors.h"
Jeff Thompson10ad12a2013-09-24 16:19:11 -070011#include "../util/dynamic-uint8-array.h"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "binary-xml.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070013
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070014#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -070015extern "C" {
16#endif
17
Jeff Thompsonf0fea002013-07-30 17:22:42 -070018/** An ndn_BinaryXmlEncoder struct is used by all the encoding functions. You should initialize it with
Jeff Thompsond1427fb2013-08-29 17:20:32 -070019 * ndn_BinaryXmlEncoder_initialize.
Jeff Thompson5a984832013-07-01 19:28:27 -070020 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070021struct ndn_BinaryXmlEncoder {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070022 struct ndn_DynamicUInt8Array *output; /**< A pointer to a ndn_DynamicUInt8Array which receives the encoded output */
Jeff Thompson97223af2013-09-24 17:01:27 -070023 size_t offset; /**< the offset into output.array for the next encoding */
Jeff Thompsonc8963652013-06-28 20:17:43 -070024};
25
26/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070027 * Initialize an ndn_BinaryXmlEncoder_initialize struct with the arguments for initializing the ndn_DynamicUInt8Array.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070028 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson10ad12a2013-09-24 16:19:11 -070029 * @param output A pointer to a ndn_DynamicUInt8Array struct which receives the encoded output. The struct must
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070030 * remain valid during the entire life of this ndn_BinaryXmlEncoder. If the output->realloc
31 * function pointer is null, its array must be large enough to receive the entire encoding.
Jeff Thompsonc8963652013-06-28 20:17:43 -070032 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070033static inline void ndn_BinaryXmlEncoder_initialize(struct ndn_BinaryXmlEncoder *self, struct ndn_DynamicUInt8Array *output)
Jeff Thompsonc8963652013-06-28 20:17:43 -070034{
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070035 self->output = output;
Jeff Thompsonc8963652013-06-28 20:17:43 -070036 self->offset = 0;
37}
38
Jeff Thompson5a984832013-07-01 19:28:27 -070039/**
40 * Encode a header with the type and value and write it to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070041 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070042 * @param type the header type
43 * @param value the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070044 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070045 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070046ndn_Error ndn_BinaryXmlEncoder_encodeTypeAndValue(struct ndn_BinaryXmlEncoder *self, unsigned int type, unsigned int value);
Jeff Thompson433e6da2013-07-01 15:09:00 -070047
Jeff Thompson5a984832013-07-01 19:28:27 -070048/**
49 * Write an element start header using DTAG with the tag to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070050 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070051 * @param tag the DTAG tag
Jeff Thompson8b666002013-07-08 01:16:26 -070052 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070053 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070054static inline ndn_Error ndn_BinaryXmlEncoder_writeElementStartDTag(struct ndn_BinaryXmlEncoder *self, unsigned int tag)
Jeff Thompson5a984832013-07-01 19:28:27 -070055{
Jeff Thompsonf0fea002013-07-30 17:22:42 -070056 return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag);
Jeff Thompson5a984832013-07-01 19:28:27 -070057}
58
59/**
60 * Write an element close to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070061 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070062 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070063 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070064ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self);
Jeff Thompson5a984832013-07-01 19:28:27 -070065
66/**
67 * Write a BLOB header, then the bytes of the blob value to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070068 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070069 * @param value an array of bytes for the blob value
70 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070071 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070072 */
Jeff Thompson97223af2013-09-24 17:01:27 -070073ndn_Error ndn_BinaryXmlEncoder_writeBlob(struct ndn_BinaryXmlEncoder *self, uint8_t *value, size_t valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070074
75/**
76 * 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 -070077 * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .)
78 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070079 * @param tag the DTAG tag
80 * @param value an array of bytes for the blob value
81 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070082 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070083 */
Jeff Thompson97223af2013-09-24 17:01:27 -070084ndn_Error ndn_BinaryXmlEncoder_writeBlobDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, uint8_t *value, size_t valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070085
Jeff Thompsone2276892013-07-08 02:44:18 -070086/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070087 * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement.
88 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsona8749a52013-07-11 11:48:05 -070089 * @param tag the DTAG tag
90 * @param value an array of bytes for the blob value
91 * @param valueLength the length of the array
92 * @return 0 for success, else an error code
93 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070094static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -070095 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, uint8_t *value, size_t valueLength)
Jeff Thompsona8749a52013-07-11 11:48:05 -070096{
97 if (value && valueLength > 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -070098 return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength);
Jeff Thompsona8749a52013-07-11 11:48:05 -070099 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700100 return NDN_ERROR_success;
Jeff Thompsona8749a52013-07-11 11:48:05 -0700101}
102
103/**
Jeff Thompson214c26f2013-08-27 11:45:01 -0700104 * Write a UDATA header, then the bytes of the UDATA value to self->output.
105 * @param self pointer to the ndn_BinaryXmlEncoder struct
106 * @param value an array of bytes for the value
107 * @param valueLength the length of the array
108 * @return 0 for success, else an error code
109 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700110ndn_Error ndn_BinaryXmlEncoder_writeUData(struct ndn_BinaryXmlEncoder *self, uint8_t *value, size_t valueLength);
Jeff Thompson214c26f2013-08-27 11:45:01 -0700111
112/**
113 * Write an element start header using DTAG with the tag to self->output, then the UDATA value, then an element close.
114 * (If you want to just write the UDATA value, use ndn_BinaryXmlEncoder_writeUData .)
115 * @param self pointer to the ndn_BinaryXmlEncoder struct
116 * @param tag the DTAG tag
117 * @param value an array of bytes for the value
118 * @param valueLength the length of the array
119 * @return 0 for success, else an error code
120 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700121ndn_Error ndn_BinaryXmlEncoder_writeUDataDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, uint8_t *value, size_t valueLength);
Jeff Thompson214c26f2013-08-27 11:45:01 -0700122
123/**
124 * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUDataDTagElement.
125 * @param self pointer to the ndn_BinaryXmlEncoder struct
126 * @param tag the DTAG tag
127 * @param value an array of bytes for the value
128 * @param valueLength the length of the array
129 * @return 0 for success, else an error code
130 */
131static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -0700132 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, uint8_t *value, size_t valueLength)
Jeff Thompson214c26f2013-08-27 11:45:01 -0700133{
134 if (value && valueLength > 0)
135 return ndn_BinaryXmlEncoder_writeUDataDTagElement(self, tag, value, valueLength);
136 else
137 return NDN_ERROR_success;
138}
139
140/**
Jeff Thompson5b696e02013-07-08 15:04:22 -0700141 * Write a UDATA header, then the value as an unsigned decimal integer.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700142 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsone2276892013-07-08 02:44:18 -0700143 * @param value the unsigned int
144 * @return 0 for success, else an error code
145 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700146ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value);
Jeff Thompsone2276892013-07-08 02:44:18 -0700147
Jeff Thompson5b696e02013-07-08 15:04:22 -0700148/**
149 * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer,
150 * 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 Thompson5b696e02013-07-08 15:04:22 -0700153 * @param tag the DTAG tag
154 * @param value the unsigned int
155 * @return 0 for success, else an error code
156 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700157ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned int value);
Jeff Thompson5b696e02013-07-08 15:04:22 -0700158
Jeff Thompsona259cc42013-07-08 17:14:09 -0700159/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700160 * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement.
161 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson45492a72013-07-11 12:02:47 -0700162 * @param tag the DTAG tag
163 * @param value negative for none, otherwise use (unsigned int)value
164 * @return 0 for success, else an error code
165 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700166static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, int value)
Jeff Thompson45492a72013-07-11 12:02:47 -0700167{
168 if (value >= 0)
Jeff Thompson97223af2013-09-24 17:01:27 -0700169 return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (size_t)value);
Jeff Thompson45492a72013-07-11 12:02:47 -0700170 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700171 return NDN_ERROR_success;
Jeff Thompson45492a72013-07-11 12:02:47 -0700172}
173
174/**
Jeff Thompson8f3bd992013-08-12 17:01:51 -0700175 * Write a BLOB header, then the absolute value of value, rounded to an integer, to self->output encoded as big endian.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700176 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700177 * @param value the double to encode as big endian. If value is 0, the big endian encoding has zero bytes.
178 * The value is converted to absolute value.
Jeff Thompsona259cc42013-07-08 17:14:09 -0700179 * @return 0 for success, else an error code
180 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700181ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700182
183/**
184 * Write an element start header using DTAG with the tag to self->output, then the absolute value of milliseconds
185 * as a big endian BLOB converted to 4096 ticks per second, then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700186 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
187 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700188 * @param tag the DTAG tag
189 * @param milliseconds the the number of milliseconds
190 * @return 0 for success, else an error code
191 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700192ndn_Error ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700193
194/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700195 * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement.
196 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700197 * @param tag the DTAG tag
198 * @param milliseconds negative for none, otherwise the number of milliseconds
199 * @return 0 for success, else an error code
200 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700201static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
202 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds)
Jeff Thompsonedc22252013-07-11 18:05:44 -0700203{
204 if (milliseconds >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700205 return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700206 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700207 return NDN_ERROR_success;
Jeff Thompsonedc22252013-07-11 18:05:44 -0700208}
Jeff Thompsona259cc42013-07-08 17:14:09 -0700209
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700210#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -0700211}
212#endif
213
214#endif
215