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