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 |
| 7 | #define NDN_BINARYXMLENCODER_H |
| 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 | |
| 13 | #ifdef __cplusplus |
| 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 |
| 18 | * ndn_BinaryXmlEncoder_init. |
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 | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 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 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 26 | * Initialize an ndn_BinaryXmlEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray. |
| 27 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 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 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 33 | static inline void ndn_BinaryXmlEncoder_init |
| 34 | (struct ndn_BinaryXmlEncoder *self, unsigned char *outputArray, unsigned int outputArrayLength, |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 35 | unsigned char * (*reallocFunction)(unsigned char *, unsigned int)) |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 36 | { |
| 37 | ndn_DynamicUCharArray_init(&self->output, outputArray, outputArrayLength, reallocFunction); |
| 38 | self->offset = 0; |
| 39 | } |
| 40 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 41 | /** |
| 42 | * 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] | 43 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 44 | * @param type the header type |
| 45 | * @param value the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 46 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 47 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 48 | 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] | 49 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 50 | /** |
| 51 | * 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] | 52 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 53 | * @param tag the DTAG tag |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 54 | * @return 0 for success, else an error code |
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 | 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] | 57 | { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 58 | return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Write an element close to self->output. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 63 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 64 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 65 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 66 | ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self); |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * 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] | 70 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 71 | * @param value an array of bytes for the blob value |
| 72 | * @param valueLength the length of the array |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 73 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 74 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 75 | 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] | 76 | |
| 77 | /** |
| 78 | * 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] | 79 | * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .) |
| 80 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 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 Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 84 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 85 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 86 | 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] | 87 | |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 88 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 89 | * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement. |
| 90 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 91 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 96 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 97 | (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] | 98 | { |
| 99 | if (value && valueLength > 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 100 | return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength); |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 101 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame^] | 102 | return NDN_ERROR_success; |
Jeff Thompson | a8749a5 | 2013-07-11 11:48:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 106 | * Write a UDATA header, then the value as an unsigned decimal integer. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 107 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 108 | * @param value the unsigned int |
| 109 | * @return 0 for success, else an error code |
| 110 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 111 | ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value); |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 112 | |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 113 | /** |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 116 | * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .) |
| 117 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 118 | * @param tag the DTAG tag |
| 119 | * @param value the unsigned int |
| 120 | * @return 0 for success, else an error code |
| 121 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 122 | 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] | 123 | |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 124 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 125 | * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement. |
| 126 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 127 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 131 | 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] | 132 | { |
| 133 | if (value >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 134 | return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value); |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 135 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame^] | 136 | return NDN_ERROR_success; |
Jeff Thompson | 45492a7 | 2013-07-11 12:02:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 140 | * Write a BLOB header, then the absolute value of value to self->output encoded as big endian. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 141 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 142 | * @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 Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 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_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 147 | |
| 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 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 | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 153 | * @param tag the DTAG tag |
| 154 | * @param milliseconds the the number of milliseconds |
| 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_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 158 | |
| 159 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 160 | * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement. |
| 161 | * @param self pointer to the ndn_BinaryXmlEncoder struct |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 162 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 166 | static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement |
| 167 | (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds) |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 168 | { |
| 169 | if (milliseconds >= 0) |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 170 | return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds); |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 171 | else |
Jeff Thompson | f2349af | 2013-08-08 14:05:37 -0700 | [diff] [blame^] | 172 | return NDN_ERROR_success; |
Jeff Thompson | edc2225 | 2013-07-11 18:05:44 -0700 | [diff] [blame] | 173 | } |
Jeff Thompson | a259cc4 | 2013-07-08 17:14:09 -0700 | [diff] [blame] | 174 | |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif |
| 180 | |