Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
Jeff Thompson | e5b37a5 | 2013-07-08 15:39:01 -0700 | [diff] [blame] | 3 | * Derived from BinaryXMLEncoder.js by Meki Cheraoui. |
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 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 7 | #include "../util/ndn_memory.h" |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 8 | #include "BinaryXML.h" |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 9 | #include "BinaryXMLEncoder.h" |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 10 | |
| 11 | enum { |
| 12 | ENCODING_LIMIT_1_BYTE = ((1 << ndn_BinaryXML_TT_VALUE_BITS) - 1), |
| 13 | ENCODING_LIMIT_2_BYTES = ((1 << (ndn_BinaryXML_TT_VALUE_BITS + ndn_BinaryXML_REGULAR_VALUE_BITS)) - 1), |
| 14 | ENCODING_LIMIT_3_BYTES = ((1 << (ndn_BinaryXML_TT_VALUE_BITS + 2 * ndn_BinaryXML_REGULAR_VALUE_BITS)) - 1) |
| 15 | }; |
| 16 | |
| 17 | /** |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 18 | * Call ndn_DynamicUCharArray_ensureLength to ensure that there is enough room in the output, and copy |
| 19 | * array to the output. This does not write a header. |
| 20 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 21 | * @param array the array to copy |
| 22 | * @param arrayLength the length of the array |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 23 | * @return 0 for success, else an error code |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 24 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 25 | static ndn_Error writeArray(struct ndn_BinaryXMLEncoder *self, unsigned char *array, unsigned int arrayLength) |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 26 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 27 | ndn_Error error; |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 28 | if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + arrayLength)) |
| 29 | return error; |
| 30 | |
| 31 | ndn_memcpy(self->output.array + self->offset, array, arrayLength); |
| 32 | self->offset += arrayLength; |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | /** |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 38 | * Return the number of bytes to encode a header of value x. |
| 39 | */ |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 40 | static unsigned int getNHeaderEncodingBytes(unsigned int x) |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 41 | { |
| 42 | // Do a quick check for pre-compiled results. |
| 43 | if (x <= ENCODING_LIMIT_1_BYTE) |
| 44 | return 1; |
| 45 | if (x <= ENCODING_LIMIT_2_BYTES) |
| 46 | return 2; |
| 47 | if (x <= ENCODING_LIMIT_3_BYTES) |
| 48 | return 3; |
| 49 | |
| 50 | unsigned int nBytes = 1; |
| 51 | |
| 52 | // Last byte gives you TT_VALUE_BITS. |
| 53 | // Remainder each gives you REGULAR_VALUE_BITS. |
| 54 | x >>= ndn_BinaryXML_TT_VALUE_BITS; |
| 55 | while (x != 0) { |
| 56 | ++nBytes; |
| 57 | x >>= ndn_BinaryXML_REGULAR_VALUE_BITS; |
| 58 | } |
| 59 | |
| 60 | return nBytes; |
| 61 | } |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 62 | |
Jeff Thompson | 2c1d921 | 2013-07-08 02:10:03 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Reverse the length bytes in array starting at offset. |
| 65 | * @param array |
| 66 | * @param offset |
| 67 | * @param length |
| 68 | */ |
| 69 | static void reverse(unsigned char *array, unsigned int offset, unsigned int length) |
| 70 | { |
| 71 | if (length == 0) |
| 72 | return; |
| 73 | |
| 74 | unsigned char *left = array + offset; |
| 75 | unsigned char *right = array + offset + length - 1; |
| 76 | while (left < right) { |
| 77 | // Swap. |
| 78 | unsigned char temp = *left; |
| 79 | *left = *right; |
| 80 | *right = temp; |
| 81 | |
| 82 | ++left; |
| 83 | --right; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Write x as an unsigned decimal integer to the output, using ndn_DynamicUCharArray_ensureLength. |
| 89 | * This does not write a header. |
| 90 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 91 | * @param x the unsigned int to write |
| 92 | * @return 0 for success, else an error code |
| 93 | */ |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 94 | static ndn_Error encodeUnsignedDecimalInt(struct ndn_BinaryXMLEncoder *self, unsigned int x) |
Jeff Thompson | 2c1d921 | 2013-07-08 02:10:03 -0700 | [diff] [blame] | 95 | { |
| 96 | // We write the value backwards, then reverse it. |
| 97 | unsigned int startOffset = self->offset; |
| 98 | |
| 99 | while (1) { |
| 100 | ndn_Error error; |
| 101 | if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + 1)) |
| 102 | return error; |
| 103 | |
| 104 | self->output.array[self->offset++] = (unsigned char)(x % 10 + '0'); |
| 105 | x /= 10; |
| 106 | |
| 107 | if (x == 0) |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | // Now reverse. |
| 112 | reverse(self->output.array, startOffset, self->offset - startOffset); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 116 | 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] | 117 | { |
| 118 | if (type > ndn_BinaryXML_UDATA) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 119 | return NDN_ERROR_header_type_is_out_of_range; |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 120 | |
| 121 | // Encode backwards. Calculate how many bytes we need. |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 122 | unsigned int nEncodingBytes = getNHeaderEncodingBytes(value); |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 123 | ndn_Error error; |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 124 | if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + nEncodingBytes)) |
| 125 | return error; |
| 126 | |
| 127 | // Bottom 4 bits of value go in last byte with tag. |
| 128 | self->output.array[self->offset + nEncodingBytes - 1] = |
| 129 | (ndn_BinaryXML_TT_MASK & type | |
| 130 | ((ndn_BinaryXML_TT_VALUE_MASK & value) << ndn_BinaryXML_TT_BITS)) | |
| 131 | ndn_BinaryXML_TT_FINAL; // set top bit for last byte |
| 132 | value >>= ndn_BinaryXML_TT_VALUE_BITS; |
| 133 | |
| 134 | // Rest of value goes into preceding bytes, 7 bits per byte. (Zero top bit is "more" flag.) |
| 135 | unsigned int i = self->offset + nEncodingBytes - 2; |
| 136 | while (value != 0 && i >= self->offset) { |
| 137 | self->output.array[i] = (value & ndn_BinaryXML_REGULAR_VALUE_MASK); |
| 138 | value >>= ndn_BinaryXML_REGULAR_VALUE_BITS; |
| 139 | --i; |
| 140 | } |
| 141 | if (value != 0) |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 142 | // This should not happen if getNHeaderEncodingBytes is correct. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 143 | return NDN_ERROR_encodeTypeAndValue_miscalculated_N_encoding_bytes; |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 144 | |
| 145 | self->offset+= nEncodingBytes; |
| 146 | |
| 147 | return 0; |
| 148 | } |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 149 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 150 | ndn_Error ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self) |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 151 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 152 | ndn_Error error; |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 153 | if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + 1)) |
| 154 | return error; |
| 155 | |
| 156 | self->output.array[self->offset] = ndn_BinaryXML_CLOSE; |
| 157 | self->offset += 1; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 162 | 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] | 163 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 164 | ndn_Error error; |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 165 | if (error = ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_BLOB, valueLength)) |
| 166 | return error; |
| 167 | |
| 168 | if (error = writeArray(self, value, valueLength)) |
| 169 | return error; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 174 | 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] | 175 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 176 | ndn_Error error; |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame] | 177 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(self, tag)) |
| 178 | return error; |
| 179 | |
| 180 | if (error = ndn_BinaryXMLEncoder_writeBlob(self, value, valueLength)) |
| 181 | return error; |
| 182 | |
| 183 | if (error = ndn_BinaryXMLEncoder_writeElementClose(self)) |
| 184 | return error; |
| 185 | |
| 186 | return 0; |
| 187 | } |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 188 | |
| 189 | ndn_Error ndn_BinaryXMLEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXMLEncoder *self, unsigned int value) |
| 190 | { |
| 191 | // First write the decimal int (to find out how many bytes it is), then shift it forward to make room for the header. |
| 192 | unsigned int startOffset = self->offset; |
| 193 | |
| 194 | ndn_Error error; |
| 195 | if (error = encodeUnsignedDecimalInt(self, value)) |
| 196 | return error; |
| 197 | |
| 198 | unsigned int nIntegerBytes = self->offset - startOffset; |
| 199 | unsigned int nHeaderBytes = getNHeaderEncodingBytes(nIntegerBytes); |
| 200 | if (error = ndn_DynamicUCharArray_ensureLength(&self->output, self->offset + nHeaderBytes)) |
| 201 | return error; |
| 202 | |
| 203 | // Don't use memcpy to shift because its behavior is not guaranteed when the buffers overlap. |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 204 | // We are shifting forward, so start from the end of the buffer. |
Jeff Thompson | e227689 | 2013-07-08 02:44:18 -0700 | [diff] [blame] | 205 | unsigned char *source = self->output.array + startOffset + nIntegerBytes - 1; |
| 206 | unsigned char *dest = source + nHeaderBytes; |
| 207 | unsigned char *sourceFinal = self->output.array + startOffset; |
| 208 | while (source >= sourceFinal) |
| 209 | *(dest--) = *(source--); |
| 210 | |
| 211 | // Override the offset to force encodeTypeAndValue to encode at startOffset, then fix the offset. |
| 212 | self->offset = startOffset; |
| 213 | if (error = ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_UDATA, nIntegerBytes)) |
| 214 | // We don't really expect to get an error, since we have already ensured the length. |
| 215 | return error; |
| 216 | self->offset = startOffset + nHeaderBytes + nIntegerBytes; |
| 217 | |
| 218 | return 0; |
| 219 | } |
Jeff Thompson | 5b696e0 | 2013-07-08 15:04:22 -0700 | [diff] [blame] | 220 | |
| 221 | ndn_Error ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned int value) |
| 222 | { |
| 223 | ndn_Error error; |
| 224 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(self, tag)) |
| 225 | return error; |
| 226 | |
| 227 | if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalInt(self, value)) |
| 228 | return error; |
| 229 | |
| 230 | if (error = ndn_BinaryXMLEncoder_writeElementClose(self)) |
| 231 | return error; |
| 232 | |
| 233 | return 0; |
| 234 | } |