blob: ad7a5d185ff2b7fd9037a36fc0e04a6d2a222ce1 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
Jeff Thompsone5b37a52013-07-08 15:39:01 -07003 * Derived from BinaryXMLEncoder.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonc8963652013-06-28 20:17:43 -07005 */
6
Jeff Thompson5a984832013-07-01 19:28:27 -07007#include "../util/ndn_memory.h"
Jeff Thompson590b8692013-06-28 22:07:41 -07008#include "BinaryXML.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -07009#include "BinaryXMLEncoder.h"
Jeff Thompson590b8692013-06-28 22:07:41 -070010
11enum {
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 Thompson5a984832013-07-01 19:28:27 -070018 * 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 Thompson8b666002013-07-08 01:16:26 -070023 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070024 */
Jeff Thompson8b666002013-07-08 01:16:26 -070025static ndn_Error writeArray(struct ndn_BinaryXMLEncoder *self, unsigned char *array, unsigned int arrayLength)
Jeff Thompson5a984832013-07-01 19:28:27 -070026{
Jeff Thompson8b666002013-07-08 01:16:26 -070027 ndn_Error error;
Jeff Thompson5a984832013-07-01 19:28:27 -070028 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 Thompson590b8692013-06-28 22:07:41 -070038 * Return the number of bytes to encode a header of value x.
39 */
Jeff Thompsone2276892013-07-08 02:44:18 -070040static unsigned int getNHeaderEncodingBytes(unsigned int x)
Jeff Thompson590b8692013-06-28 22:07:41 -070041{
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 Thompson433e6da2013-07-01 15:09:00 -070062
Jeff Thompson2c1d9212013-07-08 02:10:03 -070063/**
64 * Reverse the length bytes in array starting at offset.
65 * @param array
66 * @param offset
67 * @param length
68 */
69static 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 Thompsone2276892013-07-08 02:44:18 -070094static ndn_Error encodeUnsignedDecimalInt(struct ndn_BinaryXMLEncoder *self, unsigned int x)
Jeff Thompson2c1d9212013-07-08 02:10:03 -070095{
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 Thompson8b666002013-07-08 01:16:26 -0700116ndn_Error ndn_BinaryXMLEncoder_encodeTypeAndValue(struct ndn_BinaryXMLEncoder *self, unsigned int type, unsigned int value)
Jeff Thompson433e6da2013-07-01 15:09:00 -0700117{
118 if (type > ndn_BinaryXML_UDATA)
Jeff Thompson8b666002013-07-08 01:16:26 -0700119 return NDN_ERROR_header_type_is_out_of_range;
Jeff Thompson433e6da2013-07-01 15:09:00 -0700120
121 // Encode backwards. Calculate how many bytes we need.
Jeff Thompsone2276892013-07-08 02:44:18 -0700122 unsigned int nEncodingBytes = getNHeaderEncodingBytes(value);
Jeff Thompson8b666002013-07-08 01:16:26 -0700123 ndn_Error error;
Jeff Thompson433e6da2013-07-01 15:09:00 -0700124 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 Thompsone2276892013-07-08 02:44:18 -0700142 // This should not happen if getNHeaderEncodingBytes is correct.
Jeff Thompson8b666002013-07-08 01:16:26 -0700143 return NDN_ERROR_encodeTypeAndValue_miscalculated_N_encoding_bytes;
Jeff Thompson433e6da2013-07-01 15:09:00 -0700144
145 self->offset+= nEncodingBytes;
146
147 return 0;
148}
Jeff Thompson5a984832013-07-01 19:28:27 -0700149
Jeff Thompson8b666002013-07-08 01:16:26 -0700150ndn_Error ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self)
Jeff Thompson5a984832013-07-01 19:28:27 -0700151{
Jeff Thompson8b666002013-07-08 01:16:26 -0700152 ndn_Error error;
Jeff Thompson5a984832013-07-01 19:28:27 -0700153 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 Thompson8b666002013-07-08 01:16:26 -0700162ndn_Error ndn_BinaryXMLEncoder_writeBlob(struct ndn_BinaryXMLEncoder *self, unsigned char *value, unsigned int valueLength)
Jeff Thompson5a984832013-07-01 19:28:27 -0700163{
Jeff Thompson8b666002013-07-08 01:16:26 -0700164 ndn_Error error;
Jeff Thompson5a984832013-07-01 19:28:27 -0700165 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 Thompson8b666002013-07-08 01:16:26 -0700174ndn_Error ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength)
Jeff Thompson5a984832013-07-01 19:28:27 -0700175{
Jeff Thompson8b666002013-07-08 01:16:26 -0700176 ndn_Error error;
Jeff Thompson5a984832013-07-01 19:28:27 -0700177 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 Thompsone2276892013-07-08 02:44:18 -0700188
189ndn_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 Thompson5b696e02013-07-08 15:04:22 -0700204 // We are shifting forward, so start from the end of the buffer.
Jeff Thompsone2276892013-07-08 02:44:18 -0700205 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 Thompson5b696e02013-07-08 15:04:22 -0700220
221ndn_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}