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