Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 2 | * This class uses BinaryXMLDecoder to follow the structure of a ccnb binary element to |
| 3 | * determine its end. |
| 4 | * |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 5 | * @author: Jeff Thompson |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 9 | /** |
| 10 | * @constructor |
| 11 | */ |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 12 | var BinaryXMLStructureDecoder = function BinaryXMLDecoder() { |
| 13 | this.gotElementEnd = false; |
Jeff Thompson | f5c5612 | 2013-06-26 19:52:06 -0700 | [diff] [blame] | 14 | this.offset = 0; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 15 | this.level = 0; |
| 16 | this.state = BinaryXMLStructureDecoder.READ_HEADER_OR_CLOSE; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 17 | this.headerLength = 0; |
| 18 | this.useHeaderBuffer = false; |
Jeff Thompson | 942022d | 2012-12-29 21:57:47 -0800 | [diff] [blame] | 19 | this.headerBuffer = new DynamicUint8Array(5); |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 20 | this.nBytesToRead = 0; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | BinaryXMLStructureDecoder.READ_HEADER_OR_CLOSE = 0; |
| 24 | BinaryXMLStructureDecoder.READ_BYTES = 1; |
| 25 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 26 | /** |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 27 | * Continue scanning input starting from this.offset. If found the end of the element |
| 28 | * which started at offset 0 then return true, else false. |
| 29 | * If this returns false, you should read more into input and call again. |
| 30 | * You have to pass in input each time because the array could be reallocated. |
| 31 | * This throws an exception for badly formed ccnb. |
| 32 | */ |
| 33 | BinaryXMLStructureDecoder.prototype.findElementEnd = function( |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 34 | // Uint8Array |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 35 | input) |
| 36 | { |
| 37 | if (this.gotElementEnd) |
| 38 | // Someone is calling when we already got the end. |
| 39 | return true; |
| 40 | |
| 41 | var decoder = new BinaryXMLDecoder(input); |
| 42 | |
| 43 | while (true) { |
| 44 | if (this.offset >= input.length) |
| 45 | // All the cases assume we have some input. |
| 46 | return false; |
Wentao Shang | 2b740e6 | 2012-12-07 00:02:53 -0800 | [diff] [blame] | 47 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 48 | switch (this.state) { |
| 49 | case BinaryXMLStructureDecoder.READ_HEADER_OR_CLOSE: |
| 50 | // First check for XML_CLOSE. |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 51 | if (this.headerLength == 0 && input[this.offset] == XML_CLOSE) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 52 | ++this.offset; |
| 53 | // Close the level. |
| 54 | --this.level; |
Jeff Thompson | f5c5612 | 2013-06-26 19:52:06 -0700 | [diff] [blame] | 55 | if (this.level == 0) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 56 | // Finished. |
Jeff Thompson | f5c5612 | 2013-06-26 19:52:06 -0700 | [diff] [blame] | 57 | this.gotElementEnd = true; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 58 | return true; |
Jeff Thompson | f5c5612 | 2013-06-26 19:52:06 -0700 | [diff] [blame] | 59 | } |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 60 | if (this.level < 0) |
Jeff Thompson | f5c5612 | 2013-06-26 19:52:06 -0700 | [diff] [blame] | 61 | throw new Error("BinaryXMLStructureDecoder: Unexpected close tag at offset " + |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 62 | (this.offset - 1)); |
| 63 | |
| 64 | // Get ready for the next header. |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 65 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 66 | break; |
| 67 | } |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 68 | |
| 69 | var startingHeaderLength = this.headerLength; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 70 | while (true) { |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 71 | if (this.offset >= input.length) { |
| 72 | // We can't get all of the header bytes from this input. Save in headerBuffer. |
| 73 | this.useHeaderBuffer = true; |
| 74 | var nNewBytes = this.headerLength - startingHeaderLength; |
Jeff Thompson | 942022d | 2012-12-29 21:57:47 -0800 | [diff] [blame] | 75 | this.headerBuffer.set |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 76 | (input.subarray(this.offset - nNewBytes, nNewBytes), startingHeaderLength); |
| 77 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 78 | return false; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 79 | } |
| 80 | var headerByte = input[this.offset++]; |
| 81 | ++this.headerLength; |
| 82 | if (headerByte & XML_TT_NO_MORE) |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 83 | // Break and read the header. |
| 84 | break; |
| 85 | } |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 86 | |
| 87 | var typeAndVal; |
| 88 | if (this.useHeaderBuffer) { |
| 89 | // Copy the remaining bytes into headerBuffer. |
| 90 | nNewBytes = this.headerLength - startingHeaderLength; |
Jeff Thompson | 942022d | 2012-12-29 21:57:47 -0800 | [diff] [blame] | 91 | this.headerBuffer.set |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 92 | (input.subarray(this.offset - nNewBytes, nNewBytes), startingHeaderLength); |
| 93 | |
Jeff Thompson | 942022d | 2012-12-29 21:57:47 -0800 | [diff] [blame] | 94 | typeAndVal = new BinaryXMLDecoder(this.headerBuffer.array).decodeTypeAndVal(); |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 95 | } |
| 96 | else { |
| 97 | // We didn't have to use the headerBuffer. |
| 98 | decoder.seek(this.offset - this.headerLength); |
| 99 | typeAndVal = decoder.decodeTypeAndVal(); |
| 100 | } |
| 101 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 102 | if (typeAndVal == null) |
| 103 | throw new Error("BinaryXMLStructureDecoder: Can't read header starting at offset " + |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 104 | (this.offset - this.headerLength)); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 105 | |
| 106 | // Set the next state based on the type. |
| 107 | var type = typeAndVal.t; |
| 108 | if (type == XML_DATTR) |
| 109 | // We already consumed the item. READ_HEADER_OR_CLOSE again. |
| 110 | // ccnb has rules about what must follow an attribute, but we are just scanning. |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 111 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 112 | else if (type == XML_DTAG || type == XML_EXT) { |
| 113 | // Start a new level and READ_HEADER_OR_CLOSE again. |
| 114 | ++this.level; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 115 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 116 | } |
| 117 | else if (type == XML_TAG || type == XML_ATTR) { |
| 118 | if (type == XML_TAG) |
| 119 | // Start a new level and read the tag. |
| 120 | ++this.level; |
| 121 | // Minimum tag or attribute length is 1. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 122 | this.nBytesToRead = typeAndVal.v + 1; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 123 | this.state = BinaryXMLStructureDecoder.READ_BYTES; |
| 124 | // ccnb has rules about what must follow an attribute, but we are just scanning. |
| 125 | } |
| 126 | else if (type == XML_BLOB || type == XML_UDATA) { |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 127 | this.nBytesToRead = typeAndVal.v; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 128 | this.state = BinaryXMLStructureDecoder.READ_BYTES; |
| 129 | } |
| 130 | else |
| 131 | throw new Error("BinaryXMLStructureDecoder: Unrecognized header type " + type); |
| 132 | break; |
| 133 | |
| 134 | case BinaryXMLStructureDecoder.READ_BYTES: |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 135 | var nRemainingBytes = input.length - this.offset; |
| 136 | if (nRemainingBytes < this.nBytesToRead) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 137 | // Need more. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 138 | this.offset += nRemainingBytes; |
| 139 | this.nBytesToRead -= nRemainingBytes; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 140 | return false; |
| 141 | } |
| 142 | // Got the bytes. Read a new header or close. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 143 | this.offset += this.nBytesToRead; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 144 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 145 | break; |
| 146 | |
| 147 | default: |
| 148 | // We don't expect this to happen. |
| 149 | throw new Error("BinaryXMLStructureDecoder: Unrecognized state " + this.state); |
| 150 | } |
| 151 | } |
| 152 | }; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 153 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 154 | /** |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 155 | * Set the state to READ_HEADER_OR_CLOSE and set up to start reading the header |
| 156 | */ |
| 157 | BinaryXMLStructureDecoder.prototype.startHeader = function() { |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 158 | this.headerLength = 0; |
| 159 | this.useHeaderBuffer = false; |
| 160 | this.state = BinaryXMLStructureDecoder.READ_HEADER_OR_CLOSE; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 163 | /** |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 164 | * Set the offset into the input, used for the next read. |
| 165 | */ |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame] | 166 | BinaryXMLStructureDecoder.prototype.seek = function(offset) { |
| 167 | this.offset = offset; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 168 | } |