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 | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #include "binary-xml.h" |
| 8 | #include "binary-xml-decoder.h" |
| 9 | #include "binary-xml-structure-decoder.h" |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 11 | void ndn_BinaryXmlStructureDecoder_init(struct ndn_BinaryXmlStructureDecoder *self) |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 16 | self->state = ndn_BinaryXmlStructureDecoder_READ_HEADER_OR_CLOSE; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 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 | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 25 | static inline void startHeader(struct ndn_BinaryXmlStructureDecoder *self) |
Jeff Thompson | 7afc98e | 2013-06-27 14:33:53 -0700 | [diff] [blame] | 26 | { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 27 | self->headerLength = 0; |
| 28 | self->useHeaderBuffer = 0; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 29 | self->state = ndn_BinaryXmlStructureDecoder_READ_HEADER_OR_CLOSE; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 32 | ndn_Error ndn_BinaryXmlStructureDecoder_findElementEnd |
| 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 | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame^] | 37 | return NDN_ERROR_success; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 38 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 39 | struct ndn_BinaryXmlDecoder decoder; |
| 40 | ndn_BinaryXmlDecoder_init(&decoder, input, inputLength); |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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 | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame^] | 45 | return NDN_ERROR_success; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 46 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 47 | if (self->state == ndn_BinaryXmlStructureDecoder_READ_HEADER_OR_CLOSE) { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 48 | // First check for CLOSE. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 49 | if (self->headerLength == 0 && input[self->offset] == ndn_BinaryXml_CLOSE) { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 50 | ++self->offset; |
| 51 | // Close the level. |
| 52 | --self->level; |
| 53 | if (self->level == 0) { |
| 54 | // Finished. |
| 55 | self->gotElementEnd = 1; |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame^] | 56 | return NDN_ERROR_success; |
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 | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame^] | 76 | return NDN_ERROR_success; |
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; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 80 | if (headerByte & ndn_BinaryXml_TT_FINAL) |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 95 | struct ndn_BinaryXmlDecoder bufferDecoder; |
| 96 | ndn_BinaryXmlDecoder_init(&bufferDecoder, self->headerBuffer, sizeof(self->headerBuffer)); |
| 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. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 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. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 108 | if (type == ndn_BinaryXml_DATTR) |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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); |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 112 | else if (type == ndn_BinaryXml_DTAG || type == ndn_BinaryXml_EXT) { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 113 | // Start a new level and READ_HEADER_OR_CLOSE again. |
| 114 | ++self->level; |
| 115 | startHeader(self); |
| 116 | } |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 117 | else if (type == ndn_BinaryXml_TAG || type == ndn_BinaryXml_ATTR) { |
| 118 | if (type == ndn_BinaryXml_TAG) |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 123 | self->state = ndn_BinaryXmlStructureDecoder_READ_BYTES; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 124 | // Binary XML has rules about what must follow an attribute, but we are just scanning. |
| 125 | } |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 126 | else if (type == ndn_BinaryXml_BLOB || type == ndn_BinaryXml_UDATA) { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 127 | self->nBytesToRead = value; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 128 | self->state = ndn_BinaryXmlStructureDecoder_READ_BYTES; |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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 | } |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 133 | else if (self->state == ndn_BinaryXmlStructureDecoder_READ_BYTES) { |
Jeff Thompson | 9dc1073 | 2013-06-26 21:40:32 -0700 | [diff] [blame] | 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 | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame^] | 139 | return NDN_ERROR_success; |
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 | } |