Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef NDN_BINARYXMLENCODER_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_BINARYXMLENCODER_H |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 9 | #include "../errors.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "../util/dynamic-uchar-array.h" |
| 11 | #include "binary-xml.h" |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 17 | /** An ndn_BinaryXmlEncoder struct is used by all the encoding functions. You should initialize it with |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 18 | * ndn_BinaryXmlEncoder_initialize. |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 19 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 20 | struct ndn_BinaryXmlEncoder { |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 21 | struct ndn_DynamicUCharArray *output; /**< A pointer to a ndn_DynamicUCharArray which receives the encoded output */ |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 22 | unsigned int offset; /**< the offset into output.array for the next encoding */ |
| 23 | }; |
| 24 | |
| 25 | /** |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 26 | * Initialize an ndn_BinaryXmlEncoder_initialize struct with the arguments for initializing the ndn_DynamicUCharArray. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 27 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 28 | * @param output A pointer to a ndn_DynamicUCharArray struct which receives the encoded output. The struct must |
| 29 | * remain valid during the entire life of this ndn_BinaryXmlEncoder. If the output->realloc |
| 30 | * function pointer is null, its array must be large enough to receive the entire encoding. |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 31 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 32 | static inline void ndn_BinaryXmlEncoder_initialize(struct ndn_BinaryXmlEncoder *self, struct ndn_DynamicUCharArray *output) |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 33 | { |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 34 | self->output = output; |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 35 | self->offset = 0; |
| 36 | } |
| 37 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Encode a header with the type and value and write it to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 40 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 41 | * @param type the header type |
| 42 | * @param value the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 43 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 44 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 45 | ndn_Error ndn_BinaryXmlEncoder_encodeTypeAndValue(struct ndn_BinaryXmlEncoder *self, unsigned int type, unsigned int value); |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 46 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Write an element start header using DTAG with the tag to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 49 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 50 | * @param tag the DTAG tag |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 51 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 52 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 53 | static inline ndn_Error ndn_BinaryXmlEncoder_writeElementStartDTag(struct ndn_BinaryXmlEncoder *self, unsigned int tag) |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 54 | { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 55 | return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Write an element close to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 60 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 61 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 62 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 63 | ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Write a BLOB header, then the bytes of the blob value to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 67 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 68 | * @param value an array of bytes for the blob value |
| 69 | * @param valueLength the length of the array |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 70 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 71 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 72 | ndn_Error ndn_BinaryXmlEncoder_writeBlob(struct ndn_BinaryXmlEncoder *self, unsigned char *value, unsigned int valueLength); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Write an element start header using DTAG with the tag to self->output, then the blob, then an element close. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 76 | * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .) |
| 77 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 78 | * @param tag the DTAG tag |
| 79 | * @param value an array of bytes for the blob value |
| 80 | * @param valueLength the length of the array |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 81 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 82 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 83 | ndn_Error ndn_BinaryXmlEncoder_writeBlobDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 84 | |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 85 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 86 | * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement. |
| 87 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 88 | * @param tag the DTAG tag |
| 89 | * @param value an array of bytes for the blob value |
| 90 | * @param valueLength the length of the array |
| 91 | * @return 0 for success, else an error code |
| 92 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 93 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 94 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength) |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 95 | { |
| 96 | if (value && valueLength > 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 97 | return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength); |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 98 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 99 | return NDN_ERROR_success; |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Jeff Thompson | 214c26f | 2013-08-27 11:45:01 -0700 | [diff] [blame] | 103 | * Write a UDATA header, then the bytes of the UDATA value to self->output. |
| 104 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
| 105 | * @param value an array of bytes for the value |
| 106 | * @param valueLength the length of the array |
| 107 | * @return 0 for success, else an error code |
| 108 | */ |
| 109 | ndn_Error ndn_BinaryXmlEncoder_writeUData(struct ndn_BinaryXmlEncoder *self, unsigned char *value, unsigned int valueLength); |
| 110 | |
| 111 | /** |
| 112 | * Write an element start header using DTAG with the tag to self->output, then the UDATA value, then an element close. |
| 113 | * (If you want to just write the UDATA value, use ndn_BinaryXmlEncoder_writeUData .) |
| 114 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
| 115 | * @param tag the DTAG tag |
| 116 | * @param value an array of bytes for the value |
| 117 | * @param valueLength the length of the array |
| 118 | * @return 0 for success, else an error code |
| 119 | */ |
| 120 | ndn_Error ndn_BinaryXmlEncoder_writeUDataDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength); |
| 121 | |
| 122 | /** |
| 123 | * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUDataDTagElement. |
| 124 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
| 125 | * @param tag the DTAG tag |
| 126 | * @param value an array of bytes for the value |
| 127 | * @param valueLength the length of the array |
| 128 | * @return 0 for success, else an error code |
| 129 | */ |
| 130 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement |
| 131 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength) |
| 132 | { |
| 133 | if (value && valueLength > 0) |
| 134 | return ndn_BinaryXmlEncoder_writeUDataDTagElement(self, tag, value, valueLength); |
| 135 | else |
| 136 | return NDN_ERROR_success; |
| 137 | } |
| 138 | |
| 139 | /** |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 140 | * Write a UDATA header, then the value as an unsigned decimal integer. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 141 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 142 | * @param value the unsigned int |
| 143 | * @return 0 for success, else an error code |
| 144 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 145 | ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value); |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer, |
| 149 | * then an element close. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 150 | * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .) |
| 151 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 152 | * @param tag the DTAG tag |
| 153 | * @param value the unsigned int |
| 154 | * @return 0 for success, else an error code |
| 155 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 156 | ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned int value); |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 157 | |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 158 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 159 | * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement. |
| 160 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 161 | * @param tag the DTAG tag |
| 162 | * @param value negative for none, otherwise use (unsigned int)value |
| 163 | * @return 0 for success, else an error code |
| 164 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 165 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, int value) |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 166 | { |
| 167 | if (value >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 168 | return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value); |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 169 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 170 | return NDN_ERROR_success; |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /** |
Jeff Thompson | 8f3bd99 | 2013-08-12 17:01:51 -0700 | [diff] [blame] | 174 | * Write a BLOB header, then the absolute value of value, rounded to an integer, to self->output encoded as big endian. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 175 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 176 | * @param value the double to encode as big endian. If value is 0, the big endian encoding has zero bytes. |
| 177 | * The value is converted to absolute value. |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 178 | * @return 0 for success, else an error code |
| 179 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 180 | ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Write an element start header using DTAG with the tag to self->output, then the absolute value of milliseconds |
| 184 | * as a big endian BLOB converted to 4096 ticks per second, then an element close. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 185 | * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .) |
| 186 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 187 | * @param tag the DTAG tag |
| 188 | * @param milliseconds the the number of milliseconds |
| 189 | * @return 0 for success, else an error code |
| 190 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 191 | ndn_Error ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 192 | |
| 193 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 194 | * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement. |
| 195 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 196 | * @param tag the DTAG tag |
| 197 | * @param milliseconds negative for none, otherwise the number of milliseconds |
| 198 | * @return 0 for success, else an error code |
| 199 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 200 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement |
| 201 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds) |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 202 | { |
| 203 | if (milliseconds >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 204 | return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 205 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 206 | return NDN_ERROR_success; |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 207 | } |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 209 | #ifdef __cplusplus |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 210 | } |
| 211 | #endif |
| 212 | |
| 213 | #endif |
| 214 | |