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 BinaryXMLDecoder.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 | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame^] | 7 | #include "binary-xml.h" |
| 8 | #include "binary-xml-decoder.h" |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 10 | /** |
| 11 | * Return the octet at self->offset, converting to unsigned int. Increment self->offset. |
| 12 | * This does not check for reading past the end of the input, so this is called "unsafe". |
| 13 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 14 | static inline unsigned int unsafeReadOctet(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 15 | { |
| 16 | return (unsigned int)(self->input[self->offset++] & 0xff); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Return the octet at self->offset, converting to unsigned int. Do not increment self->offset. |
| 21 | * This does not check for reading past the end of the input, so this is called "unsafe". |
| 22 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 23 | static inline unsigned int unsafeGetOctet(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 24 | { |
| 25 | return (unsigned int)(self->input[self->offset] & 0xff); |
| 26 | } |
| 27 | |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Parse the value as a decimal unsigned integer. This does not check for whitespace or + sign. |
| 30 | * If valueLength is 0, this succeeds with resultOut 0. |
| 31 | * @param value |
| 32 | * @param valueLength |
| 33 | * @param resultOut output the parsed integer. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 34 | * @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] | 35 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 36 | static ndn_Error parseUnsignedDecimalInt(unsigned char *value, unsigned int valueLength, unsigned int *resultOut) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 37 | { |
| 38 | unsigned int result = 0; |
| 39 | |
| 40 | unsigned int i; |
| 41 | for (i = 0; i < valueLength; ++i) { |
| 42 | unsigned char digit = value[i]; |
| 43 | if (!(digit >= '0' && digit <= '9')) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 44 | return NDN_ERROR_element_of_value_is_not_a_decimal_digit; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 45 | |
| 46 | result *= 10; |
| 47 | result += (unsigned int)(digit - '0'); |
| 48 | } |
| 49 | |
| 50 | *resultOut = result; |
| 51 | return 0; |
| 52 | } |
| 53 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | 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] | 55 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 56 | unsigned int value = 0; |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 57 | int gotFirstOctet = 0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 58 | |
| 59 | while (1) { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 60 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 61 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 62 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 63 | unsigned int octet = unsafeReadOctet(self); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 64 | |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 65 | if (!gotFirstOctet) { |
| 66 | if (octet == 0) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 67 | return NDN_ERROR_the_first_header_octet_may_not_be_zero; |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 68 | |
| 69 | gotFirstOctet = 1; |
| 70 | } |
| 71 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 72 | if (octet & ndn_BinaryXml_TT_FINAL) { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 73 | // Finished. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 74 | *type = octet & ndn_BinaryXml_TT_MASK; |
| 75 | 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] | 76 | break; |
| 77 | } |
| 78 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 79 | value = (value << ndn_BinaryXml_REGULAR_VALUE_BITS) | (octet & ndn_BinaryXml_REGULAR_VALUE_MASK); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | *valueOut = value; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 83 | return 0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 84 | } |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 85 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 86 | ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 87 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 88 | ndn_Error error; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 89 | unsigned int type; |
| 90 | unsigned int value; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 91 | if (error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value)) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 92 | return error; |
| 93 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 94 | if (type != ndn_BinaryXml_DTAG) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 95 | return NDN_ERROR_header_type_is_not_a_DTAG; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 96 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 97 | if (value != expectedTag) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 98 | return NDN_ERROR_did_not_get_the_expected_DTAG; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 99 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 100 | return 0; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 103 | ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 104 | { |
| 105 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 106 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 107 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 108 | if (unsafeReadOctet(self) != ndn_BinaryXml_CLOSE) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 109 | return NDN_ERROR_did_not_get_the_expected_element_close; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 111 | return 0; |
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_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 115 | { |
| 116 | // Default to 0. |
| 117 | *gotExpectedTag = 0; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 118 | |
| 119 | // First check if it is an element close (which cannot be the expected tag). |
| 120 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 121 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 122 | if (unsafeGetOctet(self) == 0) |
| 123 | return 0; |
| 124 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 125 | unsigned int type; |
| 126 | unsigned int value; |
| 127 | unsigned int saveOffset = self->offset; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 128 | ndn_Error error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 129 | // Restore offset. |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 130 | self->offset = saveOffset; |
| 131 | |
| 132 | if (error) |
| 133 | return error; |
| 134 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 135 | if (type == ndn_BinaryXml_DTAG && value == expectedTag) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 136 | *gotExpectedTag = 1; |
| 137 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 141 | ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 142 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 143 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 144 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 145 | if (error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag)) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 146 | return error; |
| 147 | |
| 148 | if (allowNull) { |
| 149 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 150 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 151 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 152 | if (unsafeGetOctet(self) == ndn_BinaryXml_CLOSE) { |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 153 | // The binary item is missing, and this is allowed, so read the element close and return a null value. |
| 154 | ++self->offset; |
| 155 | *value = 0; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 156 | *valueLength = 0; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | unsigned int itemType; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 162 | if (error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, valueLength)) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 163 | return error; |
| 164 | // Ignore itemType. |
| 165 | *value = self->input + self->offset; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 166 | self->offset += *valueLength; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 167 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 168 | if (error = ndn_BinaryXmlDecoder_readElementClose(self)) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 169 | return error; |
| 170 | |
| 171 | return 0; |
| 172 | } |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 173 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 174 | ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 175 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 176 | { |
| 177 | ndn_Error error; |
| 178 | int gotExpectedTag; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 179 | if (error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag)) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 180 | return error; |
| 181 | if (gotExpectedTag) { |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 182 | if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, allowNull, value, valueLength)) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 183 | return error; |
| 184 | } |
| 185 | else { |
| 186 | *value = 0; |
| 187 | *valueLength = 0; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 193 | ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement |
| 194 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 195 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 196 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 197 | if (error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 198 | return error; |
| 199 | |
| 200 | unsigned int itemType; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 201 | if (error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, valueLength)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 202 | return error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 203 | if (itemType != ndn_BinaryXml_UDATA) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 204 | return NDN_ERROR_item_is_not_UDATA; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 205 | *value = self->input + self->offset; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 206 | self->offset += *valueLength; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 207 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 208 | if (error = ndn_BinaryXmlDecoder_readElementClose(self)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 209 | return error; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 214 | ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement |
| 215 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 216 | { |
| 217 | unsigned char *udataValue; |
| 218 | unsigned int udataValueLength; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 219 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 220 | if (error = ndn_BinaryXmlDecoder_readUDataDTagElement(self, expectedTag, &udataValue, &udataValueLength)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 221 | return error; |
| 222 | |
| 223 | if (error = parseUnsignedDecimalInt(udataValue, udataValueLength, value)) |
| 224 | return error; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 229 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 230 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 231 | { |
| 232 | int gotExpectedTag; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 233 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 234 | if (error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 235 | return error; |
| 236 | |
| 237 | if (!gotExpectedTag) { |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 238 | *value = -1; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | unsigned int unsignedValue; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 243 | if (error = ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement(self, expectedTag, &unsignedValue)) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 244 | return error; |
| 245 | |
| 246 | *value = (int)unsignedValue; |
| 247 | return 0; |
| 248 | } |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 249 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 250 | ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement |
| 251 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 252 | { |
| 253 | ndn_Error error; |
| 254 | unsigned char *bytes; |
| 255 | unsigned int bytesLength; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 256 | if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, 0, &bytes, &bytesLength)) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 257 | return error; |
| 258 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 259 | *milliseconds = 1000.0 * ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(bytes, bytesLength) / 4096.0; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 263 | ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 264 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 265 | { |
| 266 | int gotExpectedTag; |
| 267 | ndn_Error error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 268 | if (error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag)) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 269 | return error; |
| 270 | |
| 271 | if (!gotExpectedTag) { |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 272 | *milliseconds = -1.0; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 276 | if (error = ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement(self, expectedTag, milliseconds)) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 277 | return error; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 282 | double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength) |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 283 | { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 284 | double result = 0.0; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 285 | unsigned int i; |
| 286 | for (i = 0; i < bytesLength; ++i) { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 287 | result *= 256.0; |
| 288 | result += (double)bytes[i]; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return result; |
| 292 | } |