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 | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 10 | 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] | 11 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 12 | unsigned int value = 0; |
| 13 | |
| 14 | while (1) { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 15 | if (self->offset >= self->inputLength) |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 16 | return "ndn_BinaryXMLDecoder_decodeTypeAndVal read past the end of the input"; |
| 17 | |
| 18 | unsigned int octet = (unsigned int)(self->input[self->offset++] & 0xff); |
| 19 | |
| 20 | if (octet & ndn_BinaryXML_TT_FINAL) { |
| 21 | // Finished. |
| 22 | *type = octet & ndn_BinaryXML_TT_MASK; |
| 23 | value = (value << ndn_BinaryXML_TT_VALUE_BITS) | ((octet >> ndn_BinaryXML_TT_BITS) & ndn_BinaryXML_TT_VALUE_MASK); |
| 24 | break; |
| 25 | } |
| 26 | |
| 27 | value = (value << ndn_BinaryXML_REGULAR_VALUE_BITS) | (octet & ndn_BinaryXML_REGULAR_VALUE_MASK); |
| 28 | } |
| 29 | |
| 30 | *valueOut = value; |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 31 | return (char *)0; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 32 | } |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 34 | char *ndn_BinaryXMLDecoder_readDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 35 | { |
| 36 | char *error; |
| 37 | unsigned int type; |
| 38 | unsigned int value; |
| 39 | if (error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value)) |
| 40 | return error; |
| 41 | |
| 42 | if (type != ndn_BinaryXML_DTAG) |
| 43 | return "ndn_BinaryXMLDecoder_readDTag: header type is not a DTAG"; |
| 44 | |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 45 | if (value != expectedTag) |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 46 | return "ndn_BinaryXMLDecoder_readDTag: did not get the expected DTAG"; |
| 47 | |
| 48 | return (char *)0; |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self) |
| 52 | { |
| 53 | if (self->offset >= self->inputLength) |
| 54 | return "ndn_BinaryXMLDecoder_readElementClose read past the end of the input"; |
| 55 | |
| 56 | unsigned int octet = (unsigned int)(self->input[self->offset++] & 0xff); |
| 57 | |
| 58 | if (octet != ndn_BinaryXML_CLOSE) |
| 59 | return "ndn_BinaryXMLDecoder_readDTag: did not get the expected element close"; |
| 60 | |
| 61 | return (char *)0; |
| 62 | } |
| 63 | |
| 64 | char *ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag) |
| 65 | { |
| 66 | // Default to 0. |
| 67 | *gotExpectedTag = 0; |
| 68 | |
| 69 | unsigned int type; |
| 70 | unsigned int value; |
| 71 | unsigned int saveOffset = self->offset; |
| 72 | char *error = ndn_BinaryXMLDecoder_decodeTypeAndValue(self, &type, &value); |
| 73 | self->offset = saveOffset; |
| 74 | |
| 75 | if (error) |
| 76 | return error; |
| 77 | |
| 78 | if (type == ndn_BinaryXML_DTAG && value == expectedTag) |
| 79 | *gotExpectedTag = 1; |
| 80 | |
| 81 | return (char *)0; |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 82 | } |