Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BINARYXMLENCODER_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_BINARYXMLENCODER_H |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 10 | #include "../errors.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "../util/dynamic-uchar-array.h" |
| 12 | #include "binary-xml.h" |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 14 | #ifdef __cplusplus |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 15 | extern "C" { |
| 16 | #endif |
| 17 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 18 | /** 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] | 19 | * ndn_BinaryXmlEncoder_initialize. |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 20 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 21 | struct ndn_BinaryXmlEncoder { |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 22 | 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] | 23 | unsigned int offset; /**< the offset into output.array for the next encoding */ |
| 24 | }; |
| 25 | |
| 26 | /** |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 27 | * 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] | 28 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 29 | * @param output A pointer to a ndn_DynamicUCharArray struct which receives the encoded output. The struct must |
| 30 | * 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 Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 32 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 33 | 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] | 34 | { |
Jeff Thompson | c978a7b | 2013-08-12 13:17:17 -0700 | [diff] [blame] | 35 | self->output = output; |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 36 | self->offset = 0; |
| 37 | } |
| 38 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 39 | /** |
| 40 | * 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] | 41 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 42 | * @param type the header type |
| 43 | * @param value the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 44 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 45 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 46 | 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] | 47 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 48 | /** |
| 49 | * 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] | 50 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 51 | * @param tag the DTAG tag |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 52 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 53 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | 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] | 55 | { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 56 | return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Write an element close to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 61 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 62 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 63 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 64 | ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * 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] | 68 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 69 | * @param value an array of bytes for the blob value |
| 70 | * @param valueLength the length of the array |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 71 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 72 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 73 | 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] | 74 | |
| 75 | /** |
| 76 | * 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] | 77 | * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .) |
| 78 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 79 | * @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 Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 82 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 83 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 84 | 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] | 85 | |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 86 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 87 | * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement. |
| 88 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 89 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 94 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 95 | (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] | 96 | { |
| 97 | if (value && valueLength > 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 98 | return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength); |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 99 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 100 | return NDN_ERROR_success; |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Jeff Thompson | 214c26f | 2013-08-27 11:45:01 -0700 | [diff] [blame] | 104 | * 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 | */ |
| 110 | ndn_Error ndn_BinaryXmlEncoder_writeUData(struct ndn_BinaryXmlEncoder *self, unsigned char *value, unsigned int valueLength); |
| 111 | |
| 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 | */ |
| 121 | ndn_Error ndn_BinaryXmlEncoder_writeUDataDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength); |
| 122 | |
| 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 | */ |
| 131 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement |
| 132 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength) |
| 133 | { |
| 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 Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 141 | * Write a UDATA header, then the value as an unsigned decimal integer. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 142 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 143 | * @param value the unsigned int |
| 144 | * @return 0 for success, else an error code |
| 145 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 146 | ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value); |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 147 | |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 148 | /** |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 151 | * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .) |
| 152 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 153 | * @param tag the DTAG tag |
| 154 | * @param value the unsigned int |
| 155 | * @return 0 for success, else an error code |
| 156 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 157 | 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] | 158 | |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 159 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 160 | * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement. |
| 161 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 162 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 166 | 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] | 167 | { |
| 168 | if (value >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 169 | return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value); |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 170 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 171 | return NDN_ERROR_success; |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Jeff Thompson | 8f3bd99 | 2013-08-12 17:01:51 -0700 | [diff] [blame] | 175 | * 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] | 176 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 177 | * @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 Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 179 | * @return 0 for success, else an error code |
| 180 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 181 | ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 182 | |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 186 | * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .) |
| 187 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 188 | * @param tag the DTAG tag |
| 189 | * @param milliseconds the the number of milliseconds |
| 190 | * @return 0 for success, else an error code |
| 191 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 192 | 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] | 193 | |
| 194 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 195 | * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement. |
| 196 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 197 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 201 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement |
| 202 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds) |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 203 | { |
| 204 | if (milliseconds >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 205 | return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 206 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame] | 207 | return NDN_ERROR_success; |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 208 | } |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 209 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 210 | #ifdef __cplusplus |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 211 | } |
| 212 | #endif |
| 213 | |
| 214 | #endif |
| 215 | |