Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | e5b37a5 | 2013-07-08 15:39:01 -0700 | [diff] [blame] | 4 | * Derived from BinaryXMLDecoder.js by Meki Cheraoui. |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 8 | #include "binary-xml.h" |
| 9 | #include "binary-xml-decoder.h" |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Return the octet at self->offset, converting to unsigned int. Increment self->offset. |
| 13 | * This does not check for reading past the end of the input, so this is called "unsafe". |
| 14 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 15 | static inline unsigned int unsafeReadOctet(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 16 | { |
| 17 | return (unsigned int)(self->input[self->offset++] & 0xff); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Return the octet at self->offset, converting to unsigned int. Do not increment self->offset. |
| 22 | * This does not check for reading past the end of the input, so this is called "unsafe". |
| 23 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 24 | static inline unsigned int unsafeGetOctet(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 25 | { |
| 26 | return (unsigned int)(self->input[self->offset] & 0xff); |
| 27 | } |
| 28 | |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Parse the value as a decimal unsigned integer. This does not check for whitespace or + sign. |
| 31 | * If valueLength is 0, this succeeds with resultOut 0. |
| 32 | * @param value |
| 33 | * @param valueLength |
| 34 | * @param resultOut output the parsed integer. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 35 | * @return 0 for success, else an error code, including if an element of value is not a decimal digit. |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 36 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 37 | static ndn_Error parseUnsignedDecimalInt(uint8_t *value, size_t valueLength, unsigned int *resultOut) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 38 | { |
| 39 | unsigned int result = 0; |
| 40 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 41 | size_t i; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 42 | for (i = 0; i < valueLength; ++i) { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 43 | uint8_t digit = value[i]; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 44 | if (!(digit >= '0' && digit <= '9')) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 45 | return NDN_ERROR_element_of_value_is_not_a_decimal_digit; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 46 | |
| 47 | result *= 10; |
| 48 | result += (unsigned int)(digit - '0'); |
| 49 | } |
| 50 | |
| 51 | *resultOut = result; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 52 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 55 | ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *valueOut) |
Jeff Thompson | 82222e8 | 2013-06-26 19:32:59 -0700 | [diff] [blame] | 56 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 57 | unsigned int value = 0; |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 58 | int gotFirstOctet = 0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 60 | while (1) { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 61 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 62 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 63 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 64 | unsigned int octet = unsafeReadOctet(self); |
| 65 | |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 66 | if (!gotFirstOctet) { |
| 67 | if (octet == 0) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 68 | return NDN_ERROR_the_first_header_octet_may_not_be_zero; |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 69 | |
| 70 | gotFirstOctet = 1; |
| 71 | } |
| 72 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 73 | if (octet & ndn_BinaryXml_TT_FINAL) { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 74 | // Finished. |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 75 | *type = octet & ndn_BinaryXml_TT_MASK; |
| 76 | value = (value << ndn_BinaryXml_TT_VALUE_BITS) | ((octet >> ndn_BinaryXml_TT_BITS) & ndn_BinaryXml_TT_VALUE_MASK); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 77 | break; |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | value = (value << ndn_BinaryXml_REGULAR_VALUE_BITS) | (octet & ndn_BinaryXml_REGULAR_VALUE_MASK); |
| 81 | } |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 83 | *valueOut = value; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 84 | return NDN_ERROR_success; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 85 | } |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 86 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 87 | ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 88 | { |
Jeff Thompson | 5424ab3 | 2013-12-04 15:51:15 -0800 | [diff] [blame] | 89 | if (self->offset == self->previouslyPeekedDTagStartOffset) { |
| 90 | // peekDTag already decoded this DTag. |
| 91 | if (self->previouslyPeekedDTag != expectedTag) |
| 92 | return NDN_ERROR_did_not_get_the_expected_DTAG; |
| 93 | |
| 94 | // Fast forward past the header. |
| 95 | self->offset = self->previouslyPeekedDTagEndOffset; |
| 96 | } |
| 97 | else { |
| 98 | ndn_Error error; |
| 99 | unsigned int type; |
| 100 | unsigned int value; |
| 101 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value))) |
| 102 | return error; |
| 103 | |
| 104 | if (type != ndn_BinaryXml_DTAG) |
| 105 | return NDN_ERROR_header_type_is_not_a_DTAG; |
| 106 | |
| 107 | if (value != expectedTag) |
| 108 | return NDN_ERROR_did_not_get_the_expected_DTAG; |
| 109 | } |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 111 | return NDN_ERROR_success; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 114 | ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 115 | { |
| 116 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 117 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 118 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 119 | if (unsafeReadOctet(self) != ndn_BinaryXml_CLOSE) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 120 | return NDN_ERROR_did_not_get_the_expected_element_close; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 121 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 122 | return NDN_ERROR_success; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 125 | ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 126 | { |
Jeff Thompson | 5424ab3 | 2013-12-04 15:51:15 -0800 | [diff] [blame] | 127 | if (self->offset == self->previouslyPeekedDTagStartOffset) |
| 128 | // We already decoded this DTag. |
| 129 | *gotExpectedTag = (self->previouslyPeekedDTag == expectedTag ? 1 : 0); |
| 130 | else { |
| 131 | // Default to 0. |
| 132 | *gotExpectedTag = 0; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 133 | |
Jeff Thompson | 5424ab3 | 2013-12-04 15:51:15 -0800 | [diff] [blame] | 134 | // First check if it is an element close (which cannot be the expected tag). |
| 135 | if (self->offset >= self->inputLength) |
| 136 | return NDN_ERROR_read_past_the_end_of_the_input; |
| 137 | if (unsafeGetOctet(self) == ndn_BinaryXml_CLOSE) |
| 138 | return NDN_ERROR_success; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 139 | |
Jeff Thompson | 5424ab3 | 2013-12-04 15:51:15 -0800 | [diff] [blame] | 140 | unsigned int type; |
| 141 | unsigned int value; |
| 142 | size_t saveOffset = self->offset; |
| 143 | ndn_Error error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value); |
| 144 | // readElementStartDTag will use this to fast forward. |
| 145 | self->previouslyPeekedDTagEndOffset = self->offset; |
| 146 | // Restore offset. |
| 147 | self->offset = saveOffset; |
| 148 | |
| 149 | if (error) |
| 150 | return error; |
| 151 | |
| 152 | if (type == ndn_BinaryXml_DTAG) { |
| 153 | self->previouslyPeekedDTagStartOffset = saveOffset; |
| 154 | self->previouslyPeekedDTag = value; |
| 155 | |
| 156 | if (value == expectedTag) |
| 157 | *gotExpectedTag = 1; |
| 158 | } |
| 159 | } |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 160 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 161 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 164 | ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 165 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 166 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 167 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 168 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 169 | return error; |
| 170 | |
| 171 | if (allowNull) { |
| 172 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 173 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 174 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 175 | if (unsafeGetOctet(self) == ndn_BinaryXml_CLOSE) { |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 176 | // The binary item is missing, and this is allowed, so read the element close and return a null value. |
| 177 | ++self->offset; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 178 | value->value = 0; |
| 179 | value->length = 0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 180 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | unsigned int itemType; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 185 | unsigned int uintValueLength; |
| 186 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, &uintValueLength))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 187 | return error; |
| 188 | // Ignore itemType. |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 189 | value->value = self->input + self->offset; |
| 190 | value->length = (size_t)uintValueLength; |
| 191 | self->offset += value->length; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 192 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 193 | if ((error = ndn_BinaryXmlDecoder_readElementClose(self))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 194 | return error; |
| 195 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 196 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 197 | } |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 198 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 199 | ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 200 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 201 | { |
| 202 | ndn_Error error; |
| 203 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 204 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 205 | return error; |
| 206 | if (gotExpectedTag) { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 207 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, allowNull, value))) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 208 | return error; |
| 209 | } |
| 210 | else { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 211 | value->value = 0; |
| 212 | value->length = 0; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 215 | return NDN_ERROR_success; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 218 | ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 219 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 220 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 221 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 222 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 223 | return error; |
| 224 | |
| 225 | unsigned int itemType; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 226 | unsigned int uintValueLength; |
| 227 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, &uintValueLength))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 228 | return error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 229 | if (itemType != ndn_BinaryXml_UDATA) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 230 | return NDN_ERROR_item_is_not_UDATA; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 231 | value->value = self->input + self->offset; |
| 232 | value->length = uintValueLength; |
| 233 | self->offset += value->length; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 234 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 235 | if ((error = ndn_BinaryXmlDecoder_readElementClose(self))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 236 | return error; |
| 237 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 238 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 241 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 242 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value) |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 243 | { |
| 244 | ndn_Error error; |
| 245 | int gotExpectedTag; |
| 246 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
| 247 | return error; |
| 248 | if (gotExpectedTag) { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 249 | if ((error = ndn_BinaryXmlDecoder_readUDataDTagElement(self, expectedTag, value))) |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 250 | return error; |
| 251 | } |
| 252 | else { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 253 | value->value = 0; |
| 254 | value->length = 0; |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return NDN_ERROR_success; |
| 258 | } |
| 259 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 260 | ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement |
| 261 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 262 | { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 263 | struct ndn_Blob udataValue; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 264 | ndn_Error error; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 265 | if ((error = ndn_BinaryXmlDecoder_readUDataDTagElement(self, expectedTag, &udataValue))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 266 | return error; |
| 267 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 268 | if ((error = parseUnsignedDecimalInt(udataValue.value, udataValue.length, value))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 269 | return error; |
| 270 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 271 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 274 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 275 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 276 | { |
| 277 | int gotExpectedTag; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 278 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 279 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 280 | return error; |
| 281 | |
| 282 | if (!gotExpectedTag) { |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 283 | *value = -1; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 284 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | unsigned int unsignedValue; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 288 | if ((error = ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement(self, expectedTag, &unsignedValue))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 289 | return error; |
| 290 | |
| 291 | *value = (int)unsignedValue; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 292 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 293 | } |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 294 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 295 | ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 296 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, ndn_MillisecondsSince1970 *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 297 | { |
| 298 | ndn_Error error; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 299 | struct ndn_Blob bytes; |
| 300 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, 0, &bytes))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 301 | return error; |
| 302 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 303 | *milliseconds = 1000.0 * ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(bytes.value, bytes.length) / 4096.0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 304 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 307 | ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 308 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, ndn_MillisecondsSince1970 *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 309 | { |
| 310 | int gotExpectedTag; |
| 311 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 312 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 313 | return error; |
| 314 | |
| 315 | if (!gotExpectedTag) { |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 316 | *milliseconds = -1.0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 317 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 320 | if ((error = ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement(self, expectedTag, milliseconds))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 321 | return error; |
| 322 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 323 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 326 | double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t bytesLength) |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 327 | { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 328 | double result = 0.0; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 329 | size_t i; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 330 | for (i = 0; i < bytesLength; ++i) { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 331 | result *= 256.0; |
| 332 | result += (double)bytes[i]; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return result; |
| 336 | } |