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