Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jeff Thompson | f418fe0 | 2013-06-27 17:28:55 -0700 | [diff] [blame] | 6 | #include "../util/ndn_memory.h" |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 7 | #include "BinaryXML.h" |
| 8 | #include "BinaryXMLDecoder.h" |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 9 | #include "BinaryXMLStructureDecoder.h" |
| 10 | |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 11 | void ndn_BinaryXMLStructureDecoder_init(struct ndn_BinaryXMLStructureDecoder *self) |
| 12 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 13 | self->gotElementEnd = 0; |
| 14 | self->offset = 0; |
| 15 | self->level = 0; |
| 16 | self->state = ndn_BinaryXMLStructureDecoder_READ_HEADER_OR_CLOSE; |
| 17 | self->headerLength = 0; |
| 18 | self->useHeaderBuffer = 0; |
| 19 | self->nBytesToRead = 0; |
| 20 | } |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Set the state to READ_HEADER_OR_CLOSE and set up to start reading the header. |
| 24 | */ |
Jeff Thompson | 7afc98e | 2013-06-27 14:33:53 -0700 | [diff] [blame] | 25 | static inline void startHeader(struct ndn_BinaryXMLStructureDecoder *self) |
| 26 | { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 27 | self->headerLength = 0; |
| 28 | self->useHeaderBuffer = 0; |
| 29 | self->state = ndn_BinaryXMLStructureDecoder_READ_HEADER_OR_CLOSE; |
| 30 | } |
| 31 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 32 | ndn_Error ndn_BinaryXMLStructureDecoder_findElementEnd |
Jeff Thompson | f418fe0 | 2013-06-27 17:28:55 -0700 | [diff] [blame] | 33 | (struct ndn_BinaryXMLStructureDecoder *self, unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 34 | { |
| 35 | if (self->gotElementEnd) |
| 36 | // Someone is calling when we already got the end. |
Jeff Thompson | 46b9416 | 2013-06-28 13:35:42 -0700 | [diff] [blame] | 37 | return 0; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 38 | |
| 39 | struct ndn_BinaryXMLDecoder decoder; |
| 40 | ndn_BinaryXMLDecoder_init(&decoder, input, inputLength); |
| 41 | |
| 42 | while (1) { |
| 43 | if (self->offset >= inputLength) |
| 44 | // All the cases assume we have some input. Return and wait for more. |
Jeff Thompson | 46b9416 | 2013-06-28 13:35:42 -0700 | [diff] [blame] | 45 | return 0; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 46 | |
| 47 | if (self->state == ndn_BinaryXMLStructureDecoder_READ_HEADER_OR_CLOSE) { |
| 48 | // First check for CLOSE. |
| 49 | if (self->headerLength == 0 && input[self->offset] == ndn_BinaryXML_CLOSE) { |
| 50 | ++self->offset; |
| 51 | // Close the level. |
| 52 | --self->level; |
| 53 | if (self->level == 0) { |
| 54 | // Finished. |
| 55 | self->gotElementEnd = 1; |
Jeff Thompson | 46b9416 | 2013-06-28 13:35:42 -0700 | [diff] [blame] | 56 | return 0; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 57 | } |
| 58 | if (self->level < 0) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 59 | return NDN_ERROR_findElementEnd_unexpected_close_tag; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 60 | |
| 61 | // Get ready for the next header. |
| 62 | startHeader(self); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | unsigned int startingHeaderLength = self->headerLength; |
| 67 | while (1) { |
| 68 | if (self->offset >= inputLength) { |
| 69 | // We can't get all of the header bytes from this input. Save in headerBuffer. |
| 70 | if (self->headerLength > sizeof(self->headerBuffer)) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 71 | return NDN_ERROR_cannot_store_more_header_bytes_than_the_size_of_headerBuffer; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 72 | self->useHeaderBuffer = 1; |
| 73 | unsigned int nNewBytes = self->headerLength - startingHeaderLength; |
Jeff Thompson | f418fe0 | 2013-06-27 17:28:55 -0700 | [diff] [blame] | 74 | ndn_memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes); |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | 46b9416 | 2013-06-28 13:35:42 -0700 | [diff] [blame] | 76 | return 0; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 77 | } |
| 78 | unsigned int headerByte = (unsigned int)input[self->offset++]; |
| 79 | ++self->headerLength; |
| 80 | if (headerByte & ndn_BinaryXML_TT_FINAL) |
| 81 | // Break and read the header. |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | unsigned int type; |
| 86 | unsigned int value; |
| 87 | if (self->useHeaderBuffer) { |
| 88 | // Copy the remaining bytes into headerBuffer. |
| 89 | if (self->headerLength > sizeof(self->headerBuffer)) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 90 | return NDN_ERROR_cannot_store_more_header_bytes_than_the_size_of_headerBuffer; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 91 | unsigned int nNewBytes = self->headerLength - startingHeaderLength; |
Jeff Thompson | f418fe0 | 2013-06-27 17:28:55 -0700 | [diff] [blame] | 92 | ndn_memcpy(self->headerBuffer + startingHeaderLength, input + (self->offset - nNewBytes), nNewBytes); |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 93 | |
| 94 | // Use a local decoder just for the headerBuffer. |
| 95 | struct ndn_BinaryXMLDecoder bufferDecoder; |
| 96 | ndn_BinaryXMLDecoder_init(&bufferDecoder, self->headerBuffer, sizeof(self->headerBuffer)); |
Jeff Thompson | 7afc98e | 2013-06-27 14:33:53 -0700 | [diff] [blame] | 97 | if (ndn_BinaryXMLDecoder_decodeTypeAndValue(&bufferDecoder, &type, &value)) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 98 | return NDN_ERROR_findElementEnd_cannot_read_header_type_and_value; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 99 | } |
| 100 | else { |
| 101 | // We didn't have to use the headerBuffer. |
| 102 | ndn_BinaryXMLDecoder_seek(&decoder, self->offset - self->headerLength); |
| 103 | if (ndn_BinaryXMLDecoder_decodeTypeAndValue(&decoder, &type, &value)) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 104 | return NDN_ERROR_findElementEnd_cannot_read_header_type_and_value; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Set the next state based on the type. |
| 108 | if (type == ndn_BinaryXML_DATTR) |
| 109 | // We already consumed the item. READ_HEADER_OR_CLOSE again. |
| 110 | // Binary XML has rules about what must follow an attribute, but we are just scanning. |
| 111 | startHeader(self); |
| 112 | else if (type == ndn_BinaryXML_DTAG || type == ndn_BinaryXML_EXT) { |
| 113 | // Start a new level and READ_HEADER_OR_CLOSE again. |
| 114 | ++self->level; |
| 115 | startHeader(self); |
| 116 | } |
| 117 | else if (type == ndn_BinaryXML_TAG || type == ndn_BinaryXML_ATTR) { |
| 118 | if (type == ndn_BinaryXML_TAG) |
| 119 | // Start a new level and read the tag. |
| 120 | ++self->level; |
| 121 | // Minimum tag or attribute length is 1. |
| 122 | self->nBytesToRead = value + 1; |
| 123 | self->state = ndn_BinaryXMLStructureDecoder_READ_BYTES; |
| 124 | // Binary XML has rules about what must follow an attribute, but we are just scanning. |
| 125 | } |
| 126 | else if (type == ndn_BinaryXML_BLOB || type == ndn_BinaryXML_UDATA) { |
| 127 | self->nBytesToRead = value; |
| 128 | self->state = ndn_BinaryXMLStructureDecoder_READ_BYTES; |
| 129 | } |
| 130 | else |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 131 | return NDN_ERROR_findElementEnd_unrecognized_header_type; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 132 | } |
| 133 | else if (self->state == ndn_BinaryXMLStructureDecoder_READ_BYTES) { |
| 134 | unsigned int nRemainingBytes = inputLength - self->offset; |
| 135 | if (nRemainingBytes < self->nBytesToRead) { |
| 136 | // Need more. |
| 137 | self->offset += nRemainingBytes; |
| 138 | self->nBytesToRead -= nRemainingBytes; |
Jeff Thompson | 46b9416 | 2013-06-28 13:35:42 -0700 | [diff] [blame] | 139 | return 0; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 140 | } |
| 141 | // Got the bytes. Read a new header or close. |
| 142 | self->offset += self->nBytesToRead; |
| 143 | startHeader(self); |
| 144 | } |
| 145 | else |
| 146 | // We don't expect this to happen. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 147 | return NDN_ERROR_findElementEnd_unrecognized_state; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 148 | } |
| 149 | } |