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