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 | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 37 | 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] | 38 | { |
| 39 | unsigned int result = 0; |
| 40 | |
| 41 | unsigned int i; |
| 42 | for (i = 0; i < valueLength; ++i) { |
| 43 | unsigned char digit = value[i]; |
| 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 | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 89 | ndn_Error error; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 90 | unsigned int type; |
| 91 | unsigned int value; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 92 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value))) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 93 | return error; |
| 94 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 95 | if (type != ndn_BinaryXml_DTAG) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 96 | return NDN_ERROR_header_type_is_not_a_DTAG; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 97 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 98 | if (value != expectedTag) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 99 | return NDN_ERROR_did_not_get_the_expected_DTAG; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 100 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 101 | return NDN_ERROR_success; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 104 | ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 105 | { |
| 106 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 107 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 108 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 109 | if (unsafeReadOctet(self) != ndn_BinaryXml_CLOSE) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 110 | return NDN_ERROR_did_not_get_the_expected_element_close; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 111 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 112 | return NDN_ERROR_success; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | // Default to 0. |
| 118 | *gotExpectedTag = 0; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 119 | |
| 120 | // First check if it is an element close (which cannot be the expected tag). |
| 121 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 122 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 123 | if (unsafeGetOctet(self) == 0) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 124 | return NDN_ERROR_success; |
Jeff Thompson | 6d17b6e | 2013-06-28 14:04:01 -0700 | [diff] [blame] | 125 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 126 | unsigned int type; |
| 127 | unsigned int value; |
| 128 | unsigned int saveOffset = self->offset; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 129 | ndn_Error error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &type, &value); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 130 | // Restore offset. |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 131 | self->offset = saveOffset; |
| 132 | |
| 133 | if (error) |
| 134 | return error; |
| 135 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 136 | if (type == ndn_BinaryXml_DTAG && value == expectedTag) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 137 | *gotExpectedTag = 1; |
| 138 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 139 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 142 | ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 143 | (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] | 144 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 145 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 146 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 147 | return error; |
| 148 | |
| 149 | if (allowNull) { |
| 150 | if (self->offset >= self->inputLength) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 151 | return NDN_ERROR_read_past_the_end_of_the_input; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 152 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 153 | if (unsafeGetOctet(self) == ndn_BinaryXml_CLOSE) { |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 154 | // The binary item is missing, and this is allowed, so read the element close and return a null value. |
| 155 | ++self->offset; |
| 156 | *value = 0; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 157 | *valueLength = 0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 158 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | unsigned int itemType; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 163 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, valueLength))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 164 | return error; |
| 165 | // Ignore itemType. |
| 166 | *value = self->input + self->offset; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 167 | self->offset += *valueLength; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 168 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 169 | if ((error = ndn_BinaryXmlDecoder_readElementClose(self))) |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 170 | return error; |
| 171 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 172 | return NDN_ERROR_success; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 173 | } |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 174 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 175 | ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 176 | (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] | 177 | { |
| 178 | ndn_Error error; |
| 179 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 180 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 181 | return error; |
| 182 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 183 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, allowNull, value, valueLength))) |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 184 | return error; |
| 185 | } |
| 186 | else { |
| 187 | *value = 0; |
| 188 | *valueLength = 0; |
| 189 | } |
| 190 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 191 | return NDN_ERROR_success; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 194 | ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement |
| 195 | (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] | 196 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 197 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 198 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(self, expectedTag))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 199 | return error; |
| 200 | |
| 201 | unsigned int itemType; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 202 | if ((error = ndn_BinaryXmlDecoder_decodeTypeAndValue(self, &itemType, valueLength))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 203 | return error; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 204 | if (itemType != ndn_BinaryXml_UDATA) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 205 | return NDN_ERROR_item_is_not_UDATA; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 206 | *value = self->input + self->offset; |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 207 | self->offset += *valueLength; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 209 | if ((error = ndn_BinaryXmlDecoder_readElementClose(self))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 210 | return error; |
| 211 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 212 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 215 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement |
| 216 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength) |
| 217 | { |
| 218 | ndn_Error error; |
| 219 | int gotExpectedTag; |
| 220 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
| 221 | return error; |
| 222 | if (gotExpectedTag) { |
| 223 | if ((error = ndn_BinaryXmlDecoder_readUDataDTagElement(self, expectedTag, value, valueLength))) |
| 224 | return error; |
| 225 | } |
| 226 | else { |
| 227 | *value = 0; |
| 228 | *valueLength = 0; |
| 229 | } |
| 230 | |
| 231 | return NDN_ERROR_success; |
| 232 | } |
| 233 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 234 | ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement |
| 235 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 236 | { |
| 237 | unsigned char *udataValue; |
| 238 | unsigned int udataValueLength; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 239 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 240 | if ((error = ndn_BinaryXmlDecoder_readUDataDTagElement(self, expectedTag, &udataValue, &udataValueLength))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 241 | return error; |
| 242 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 243 | if ((error = parseUnsignedDecimalInt(udataValue, udataValueLength, value))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 244 | return error; |
| 245 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 246 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 249 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 250 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 251 | { |
| 252 | int gotExpectedTag; |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 253 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 254 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 255 | return error; |
| 256 | |
| 257 | if (!gotExpectedTag) { |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 258 | *value = -1; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 259 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | unsigned int unsignedValue; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 263 | if ((error = ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement(self, expectedTag, &unsignedValue))) |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 264 | return error; |
| 265 | |
| 266 | *value = (int)unsignedValue; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 267 | return NDN_ERROR_success; |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 268 | } |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 269 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 270 | ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement |
| 271 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 272 | { |
| 273 | ndn_Error error; |
| 274 | unsigned char *bytes; |
| 275 | unsigned int bytesLength; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 276 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(self, expectedTag, 0, &bytes, &bytesLength))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 277 | return error; |
| 278 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 279 | *milliseconds = 1000.0 * ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(bytes, bytesLength) / 4096.0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 280 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 283 | ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 284 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 285 | { |
| 286 | int gotExpectedTag; |
| 287 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 288 | if ((error = ndn_BinaryXmlDecoder_peekDTag(self, expectedTag, &gotExpectedTag))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 289 | return error; |
| 290 | |
| 291 | if (!gotExpectedTag) { |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 292 | *milliseconds = -1.0; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 293 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 296 | if ((error = ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement(self, expectedTag, milliseconds))) |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 297 | return error; |
| 298 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 299 | return NDN_ERROR_success; |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 302 | double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength) |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 303 | { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 304 | double result = 0.0; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 305 | unsigned int i; |
| 306 | for (i = 0; i < bytesLength; ++i) { |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 307 | result *= 256.0; |
| 308 | result += (double)bytes[i]; |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | return result; |
| 312 | } |