blob: bbf5e55b750a730ee227eaac55866be505431cfb [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson76317aa2013-06-25 19:11:48 -07004 */
5
6#include "BinaryXML.h"
7#include "BinaryXMLDecoder.h"
8
Jeff Thompsonb4ee4002013-06-28 13:41:43 -07009/**
10 * Return the octet at self->offset, converting to unsigned int. Increment self->offset.
11 * This does not check for reading past the end of the input, so this is called "unsafe".
12 */
13static inline unsigned int unsafeReadOctet(struct ndn_BinaryXMLDecoder *self)
14{
15 return (unsigned int)(self->input[self->offset++] & 0xff);
16}
17
18/**
19 * Return the octet at self->offset, converting to unsigned int. Do not increment self->offset.
20 * This does not check for reading past the end of the input, so this is called "unsafe".
21 */
22static inline unsigned int unsafeGetOctet(struct ndn_BinaryXMLDecoder *self)
23{
24 return (unsigned int)(self->input[self->offset] & 0xff);
25}
26
Jeff Thompson167ca5a2013-07-07 20:45:43 -070027/**
28 * Parse the value as a decimal unsigned integer. This does not check for whitespace or + sign.
29 * If valueLength is 0, this succeeds with resultOut 0.
30 * @param value
31 * @param valueLength
32 * @param resultOut output the parsed integer.
Jeff Thompson8b666002013-07-08 01:16:26 -070033 * @return 0 for success, else an error code, including if an element of value is not a decimal digit.
Jeff Thompson167ca5a2013-07-07 20:45:43 -070034 */
Jeff Thompson8b666002013-07-08 01:16:26 -070035static ndn_Error parseUnsignedDecimalInt(unsigned char *value, unsigned int valueLength, unsigned int *resultOut)
Jeff Thompson167ca5a2013-07-07 20:45:43 -070036{
37 unsigned int result = 0;
38
39 unsigned int i;
40 for (i = 0; i < valueLength; ++i) {
41 unsigned char digit = value[i];
42 if (!(digit >= '0' && digit <= '9'))
Jeff Thompson8b666002013-07-08 01:16:26 -070043 return NDN_ERROR_element_of_value_is_not_a_decimal_digit;
Jeff Thompson167ca5a2013-07-07 20:45:43 -070044
45 result *= 10;
46 result += (unsigned int)(digit - '0');
47 }
48
49 *resultOut = result;
50 return 0;
51}
52
Jeff Thompson8b666002013-07-08 01:16:26 -070053ndn_Error ndn_BinaryXMLDecoder_decodeTypeAndValue(struct ndn_BinaryXMLDecoder *self, unsigned int *type, unsigned int *valueOut)
Jeff Thompson82222e82013-06-26 19:32:59 -070054{
Jeff Thompson76317aa2013-06-25 19:11:48 -070055 unsigned int value = 0;
Jeff Thompson38cbd572013-06-28 14:01:10 -070056 int gotFirstOctet = 0;
Jeff Thompson76317aa2013-06-25 19:11:48 -070057
58 while (1) {
Jeff Thompsonf7316692013-06-26 21:31:42 -070059 if (self->offset >= self->inputLength)
Jeff Thompson8b666002013-07-08 01:16:26 -070060 return NDN_ERROR_read_past_the_end_of_the_input;
Jeff Thompson76317aa2013-06-25 19:11:48 -070061
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070062 unsigned int octet = unsafeReadOctet(self);
Jeff Thompson76317aa2013-06-25 19:11:48 -070063
Jeff Thompson38cbd572013-06-28 14:01:10 -070064 if (!gotFirstOctet) {
65 if (octet == 0)
Jeff Thompson8b666002013-07-08 01:16:26 -070066 return NDN_ERROR_the_first_header_octet_may_not_be_zero;
Jeff Thompson38cbd572013-06-28 14:01:10 -070067
68 gotFirstOctet = 1;
69 }
70
Jeff Thompson76317aa2013-06-25 19:11:48 -070071 if (octet & ndn_BinaryXML_TT_FINAL) {
72 // Finished.
73 *type = octet & ndn_BinaryXML_TT_MASK;
74 value = (value << ndn_BinaryXML_TT_VALUE_BITS) | ((octet >> ndn_BinaryXML_TT_BITS) & ndn_BinaryXML_TT_VALUE_MASK);
75 break;
76 }
77
78 value = (value << ndn_BinaryXML_REGULAR_VALUE_BITS) | (octet & ndn_BinaryXML_REGULAR_VALUE_MASK);
79 }
80
81 *valueOut = value;
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070082 return 0;
Jeff Thompson76317aa2013-06-25 19:11:48 -070083}
Jeff Thompson179d0502013-06-28 11:36:00 -070084
Jeff Thompson8b666002013-07-08 01:16:26 -070085ndn_Error ndn_BinaryXMLDecoder_readElementStartDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag)
Jeff Thompson179d0502013-06-28 11:36:00 -070086{
Jeff Thompson8b666002013-07-08 01:16:26 -070087 ndn_Error error;
Jeff Thompson179d0502013-06-28 11:36:00 -070088 unsigned int type;
89 unsigned int value;
90 if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value))
91 return error;
92
93 if (type != ndn_BinaryXML_DTAG)
Jeff Thompson8b666002013-07-08 01:16:26 -070094 return NDN_ERROR_header_type_is_not_a_DTAG;
Jeff Thompson179d0502013-06-28 11:36:00 -070095
Jeff Thompson74ab0812013-06-28 12:25:04 -070096 if (value != expectedTag)
Jeff Thompson8b666002013-07-08 01:16:26 -070097 return NDN_ERROR_did_not_get_the_expected_DTAG;
Jeff Thompson179d0502013-06-28 11:36:00 -070098
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070099 return 0;
Jeff Thompson74ab0812013-06-28 12:25:04 -0700100}
101
Jeff Thompson8b666002013-07-08 01:16:26 -0700102ndn_Error ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self)
Jeff Thompson74ab0812013-06-28 12:25:04 -0700103{
104 if (self->offset >= self->inputLength)
Jeff Thompson8b666002013-07-08 01:16:26 -0700105 return NDN_ERROR_read_past_the_end_of_the_input;
Jeff Thompson74ab0812013-06-28 12:25:04 -0700106
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700107 if (unsafeReadOctet(self) != ndn_BinaryXML_CLOSE)
Jeff Thompson8b666002013-07-08 01:16:26 -0700108 return NDN_ERROR_did_not_get_the_expected_element_close;
Jeff Thompson74ab0812013-06-28 12:25:04 -0700109
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700110 return 0;
Jeff Thompson74ab0812013-06-28 12:25:04 -0700111}
112
Jeff Thompson8b666002013-07-08 01:16:26 -0700113ndn_Error ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag)
Jeff Thompson74ab0812013-06-28 12:25:04 -0700114{
115 // Default to 0.
116 *gotExpectedTag = 0;
Jeff Thompson6d17b6e2013-06-28 14:04:01 -0700117
118 // First check if it is an element close (which cannot be the expected tag).
119 if (self->offset >= self->inputLength)
Jeff Thompson8b666002013-07-08 01:16:26 -0700120 return NDN_ERROR_read_past_the_end_of_the_input;
Jeff Thompson6d17b6e2013-06-28 14:04:01 -0700121 if (unsafeGetOctet(self) == 0)
122 return 0;
123
Jeff Thompson74ab0812013-06-28 12:25:04 -0700124 unsigned int type;
125 unsigned int value;
126 unsigned int saveOffset = self->offset;
Jeff Thompson8b666002013-07-08 01:16:26 -0700127 ndn_Error error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700128 // Restore offset.
Jeff Thompson74ab0812013-06-28 12:25:04 -0700129 self->offset = saveOffset;
130
131 if (error)
132 return error;
133
134 if (type == ndn_BinaryXML_DTAG && value == expectedTag)
135 *gotExpectedTag = 1;
136
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700137 return 0;
138}
139
Jeff Thompson8b666002013-07-08 01:16:26 -0700140ndn_Error ndn_BinaryXMLDecoder_readBinaryDTagElement
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700141 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen)
142{
Jeff Thompson8b666002013-07-08 01:16:26 -0700143 ndn_Error error;
Jeff Thompson1156ed12013-07-01 16:13:56 -0700144 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(self, expectedTag))
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700145 return error;
146
147 if (allowNull) {
148 if (self->offset >= self->inputLength)
Jeff Thompson8b666002013-07-08 01:16:26 -0700149 return NDN_ERROR_read_past_the_end_of_the_input;
Jeff Thompsonb4ee4002013-06-28 13:41:43 -0700150
151 if (unsafeGetOctet(self) == ndn_BinaryXML_CLOSE) {
152 // The binary item is missing, and this is allowed, so read the element close and return a null value.
153 ++self->offset;
154 *value = 0;
155 *valueLen = 0;
156 return 0;
157 }
158 }
159
160 unsigned int itemType;
161 if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLen))
162 return error;
163 // Ignore itemType.
164 *value = self->input + self->offset;
165 self->offset += *valueLen;
166
167 if (error = ndn_BinaryXMLDecoder_readElementClose(self))
168 return error;
169
170 return 0;
171}
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700172
Jeff Thompson8b666002013-07-08 01:16:26 -0700173ndn_Error ndn_BinaryXMLDecoder_readUDataDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700174 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen)
175{
Jeff Thompson8b666002013-07-08 01:16:26 -0700176 ndn_Error error;
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700177 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(self, expectedTag))
178 return error;
179
180 unsigned int itemType;
181 if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLen))
182 return error;
183 if (itemType != ndn_BinaryXML_UDATA)
Jeff Thompson8b666002013-07-08 01:16:26 -0700184 return NDN_ERROR_item_is_not_UDATA;
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700185 *value = self->input + self->offset;
186 self->offset += *valueLen;
187
188 if (error = ndn_BinaryXMLDecoder_readElementClose(self))
189 return error;
190
191 return 0;
192}
193
Jeff Thompson8b666002013-07-08 01:16:26 -0700194ndn_Error ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700195 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned int *value)
196{
197 unsigned char *udataValue;
198 unsigned int udataValueLength;
Jeff Thompson8b666002013-07-08 01:16:26 -0700199 ndn_Error error;
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700200 if (error = ndn_BinaryXMLDecoder_readUDataDTagElement(self, expectedTag, &udataValue, &udataValueLength))
201 return error;
202
203 if (error = parseUnsignedDecimalInt(udataValue, udataValueLength, value))
204 return error;
205
206 return 0;
207}
208
Jeff Thompson8b666002013-07-08 01:16:26 -0700209ndn_Error ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700210 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value)
211{
212 int gotExpectedTag;
Jeff Thompson8b666002013-07-08 01:16:26 -0700213 ndn_Error error;
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700214 if (error = ndn_BinaryXMLDecoder_peekDTag(self, expectedTag, &gotExpectedTag))
215 return error;
216
217 if (!gotExpectedTag) {
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700218 *value = -1;
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700219 return 0;
220 }
221
222 unsigned int unsignedValue;
223 if (error = ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement(self, expectedTag, &unsignedValue))
224 return error;
225
226 *value = (int)unsignedValue;
227 return 0;
228}
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700229
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700230unsigned int ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(unsigned char *bytes, unsigned int bytesLength)
231{
232 unsigned int result = 0;
233 unsigned int i;
234 for (i = 0; i < bytesLength; ++i) {
235 result <<= 8;
236 result += (unsigned int)bytes[i];
237 }
238
239 return result;
240}