Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #include "BinaryXML.h" |
| 8 | #include "BinaryXMLDecoder.h" |
| 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 | */ |
| 14 | static inline unsigned int unsafeReadOctet(struct ndn_BinaryXMLDecoder *self) |
| 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 | */ |
| 23 | static inline unsigned int unsafeGetOctet(struct ndn_BinaryXMLDecoder *self) |
| 24 | { |
| 25 | return (unsigned int)(self->input[self->offset] & 0xff); |
| 26 | } |
| 27 | |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 28 | char *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] | 29 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 30 | unsigned int value = 0; |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame^] | 31 | int gotFirstOctet = 0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 32 | |
| 33 | while (1) { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 34 | if (self->offset >= self->inputLength) |
Jeff Thompson | 04c0b6a | 2013-06-28 13:49:13 -0700 | [diff] [blame] | 35 | return "ndn_BinaryXMLDecoder_decodeTypeAndVal: read past the end of the input"; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 36 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 37 | unsigned int octet = unsafeReadOctet(self); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 38 | |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame^] | 39 | if (!gotFirstOctet) { |
| 40 | if (octet == 0) |
| 41 | return "ndn_BinaryXMLDecoder_decodeTypeAndVal: the first header octet may not be zero"; |
| 42 | |
| 43 | gotFirstOctet = 1; |
| 44 | } |
| 45 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 46 | if (octet & ndn_BinaryXML_TT_FINAL) { |
| 47 | // Finished. |
| 48 | *type = octet & ndn_BinaryXML_TT_MASK; |
| 49 | value = (value << ndn_BinaryXML_TT_VALUE_BITS) | ((octet >> ndn_BinaryXML_TT_BITS) & ndn_BinaryXML_TT_VALUE_MASK); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | value = (value << ndn_BinaryXML_REGULAR_VALUE_BITS) | (octet & ndn_BinaryXML_REGULAR_VALUE_MASK); |
| 54 | } |
| 55 | |
| 56 | *valueOut = value; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 57 | return 0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 58 | } |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 59 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 60 | char *ndn_BinaryXMLDecoder_readDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 61 | { |
| 62 | char *error; |
| 63 | unsigned int type; |
| 64 | unsigned int value; |
| 65 | if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value)) |
| 66 | return error; |
| 67 | |
| 68 | if (type != ndn_BinaryXML_DTAG) |
| 69 | return "ndn_BinaryXMLDecoder_readDTag: header type is not a DTAG"; |
| 70 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 71 | if (value != expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 72 | return "ndn_BinaryXMLDecoder_readDTag: did not get the expected DTAG"; |
| 73 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 74 | return 0; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self) |
| 78 | { |
| 79 | if (self->offset >= self->inputLength) |
Jeff Thompson | 04c0b6a | 2013-06-28 13:49:13 -0700 | [diff] [blame] | 80 | return "ndn_BinaryXMLDecoder_readElementClose: read past the end of the input"; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 82 | if (unsafeReadOctet(self) != ndn_BinaryXML_CLOSE) |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 83 | return "ndn_BinaryXMLDecoder_readDTag: did not get the expected element close"; |
| 84 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 85 | return 0; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | char *ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag) |
| 89 | { |
| 90 | // Default to 0. |
| 91 | *gotExpectedTag = 0; |
| 92 | |
| 93 | unsigned int type; |
| 94 | unsigned int value; |
| 95 | unsigned int saveOffset = self->offset; |
| 96 | char *error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 97 | // Restore offset. |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 98 | self->offset = saveOffset; |
| 99 | |
| 100 | if (error) |
| 101 | return error; |
| 102 | |
| 103 | if (type == ndn_BinaryXML_DTAG && value == expectedTag) |
| 104 | *gotExpectedTag = 1; |
| 105 | |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | char *ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 110 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen) |
| 111 | { |
| 112 | char *error; |
| 113 | if (error = ndn_BinaryXMLDecoder_readDTag(self, expectedTag)) |
| 114 | return error; |
| 115 | |
| 116 | if (allowNull) { |
| 117 | if (self->offset >= self->inputLength) |
Jeff Thompson | 04c0b6a | 2013-06-28 13:49:13 -0700 | [diff] [blame] | 118 | return "ndn_BinaryXMLDecoder_readBinaryDTagElement: read past the end of the input"; |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 119 | |
| 120 | if (unsafeGetOctet(self) == ndn_BinaryXML_CLOSE) { |
| 121 | // The binary item is missing, and this is allowed, so read the element close and return a null value. |
| 122 | ++self->offset; |
| 123 | *value = 0; |
| 124 | *valueLen = 0; |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | unsigned int itemType; |
| 130 | if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &itemType, valueLen)) |
| 131 | return error; |
| 132 | // Ignore itemType. |
| 133 | *value = self->input + self->offset; |
| 134 | self->offset += *valueLen; |
| 135 | |
| 136 | if (error = ndn_BinaryXMLDecoder_readElementClose(self)) |
| 137 | return error; |
| 138 | |
| 139 | return 0; |
| 140 | } |