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; |
| 11 | this.offset = 0; |
| 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; |
| 16 | this.headerBuffer = new Uint8Array(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; |
| 44 | |
| 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; |
| 52 | if (this.level == 0) |
| 53 | // Finished. |
| 54 | return true; |
| 55 | if (this.level < 0) |
| 56 | throw new Error("BinaryXMLStructureDecoder: Unexepected close tag at offset " + |
| 57 | (this.offset - 1)); |
| 58 | |
| 59 | // Get ready for the next header. |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 60 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 61 | break; |
| 62 | } |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 63 | |
| 64 | var startingHeaderLength = this.headerLength; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 65 | while (true) { |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 66 | if (this.offset >= input.length) { |
| 67 | // We can't get all of the header bytes from this input. Save in headerBuffer. |
| 68 | this.useHeaderBuffer = true; |
| 69 | var nNewBytes = this.headerLength - startingHeaderLength; |
| 70 | this.setHeaderBuffer |
| 71 | (input.subarray(this.offset - nNewBytes, nNewBytes), startingHeaderLength); |
| 72 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 73 | return false; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 74 | } |
| 75 | var headerByte = input[this.offset++]; |
| 76 | ++this.headerLength; |
| 77 | if (headerByte & XML_TT_NO_MORE) |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 78 | // Break and read the header. |
| 79 | break; |
| 80 | } |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 81 | |
| 82 | var typeAndVal; |
| 83 | if (this.useHeaderBuffer) { |
| 84 | // Copy the remaining bytes into headerBuffer. |
| 85 | nNewBytes = this.headerLength - startingHeaderLength; |
| 86 | this.setHeaderBuffer |
| 87 | (input.subarray(this.offset - nNewBytes, nNewBytes), startingHeaderLength); |
| 88 | |
| 89 | typeAndVal = new BinaryXMLDecoder(this.headerBuffer).decodeTypeAndVal(); |
| 90 | } |
| 91 | else { |
| 92 | // We didn't have to use the headerBuffer. |
| 93 | decoder.seek(this.offset - this.headerLength); |
| 94 | typeAndVal = decoder.decodeTypeAndVal(); |
| 95 | } |
| 96 | |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 97 | if (typeAndVal == null) |
| 98 | throw new Error("BinaryXMLStructureDecoder: Can't read header starting at offset " + |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 99 | (this.offset - this.headerLength)); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 100 | |
| 101 | // Set the next state based on the type. |
| 102 | var type = typeAndVal.t; |
| 103 | if (type == XML_DATTR) |
| 104 | // We already consumed the item. READ_HEADER_OR_CLOSE again. |
| 105 | // 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] | 106 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 107 | else if (type == XML_DTAG || type == XML_EXT) { |
| 108 | // Start a new level and READ_HEADER_OR_CLOSE again. |
| 109 | ++this.level; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 110 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 111 | } |
| 112 | else if (type == XML_TAG || type == XML_ATTR) { |
| 113 | if (type == XML_TAG) |
| 114 | // Start a new level and read the tag. |
| 115 | ++this.level; |
| 116 | // Minimum tag or attribute length is 1. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 117 | this.nBytesToRead = typeAndVal.v + 1; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 118 | this.state = BinaryXMLStructureDecoder.READ_BYTES; |
| 119 | // ccnb has rules about what must follow an attribute, but we are just scanning. |
| 120 | } |
| 121 | else if (type == XML_BLOB || type == XML_UDATA) { |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 122 | this.nBytesToRead = typeAndVal.v; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 123 | this.state = BinaryXMLStructureDecoder.READ_BYTES; |
| 124 | } |
| 125 | else |
| 126 | throw new Error("BinaryXMLStructureDecoder: Unrecognized header type " + type); |
| 127 | break; |
| 128 | |
| 129 | case BinaryXMLStructureDecoder.READ_BYTES: |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 130 | var nRemainingBytes = input.length - this.offset; |
| 131 | if (nRemainingBytes < this.nBytesToRead) { |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 132 | // Need more. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 133 | this.offset += nRemainingBytes; |
| 134 | this.nBytesToRead -= nRemainingBytes; |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | // Got the bytes. Read a new header or close. |
Jeff Thompson | 11dc9b6 | 2012-11-28 19:21:20 -0800 | [diff] [blame] | 138 | this.offset += this.nBytesToRead; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 139 | this.startHeader(); |
Jeff Thompson | dad617b | 2012-10-14 17:11:41 -0700 | [diff] [blame] | 140 | break; |
| 141 | |
| 142 | default: |
| 143 | // We don't expect this to happen. |
| 144 | throw new Error("BinaryXMLStructureDecoder: Unrecognized state " + this.state); |
| 145 | } |
| 146 | } |
| 147 | }; |
Jeff Thompson | 8dfa291 | 2012-11-28 21:21:56 -0800 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Set the state to READ_HEADER_OR_CLOSE and set up to start reading the header |
| 151 | */ |
| 152 | BinaryXMLStructureDecoder.prototype.startHeader = function() { |
| 153 | this.headerLength = 0; |
| 154 | this.useHeaderBuffer = false; |
| 155 | this.state = BinaryXMLStructureDecoder.READ_HEADER_OR_CLOSE; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Set the offset into the input, used for the next read. |
| 160 | */ |
| 161 | BinaryXMLStructureDecoder.prototype.seek = function( |
| 162 | //int |
| 163 | offset) { |
| 164 | this.offset = offset; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Set call this.headerBuffer.set(subarray, bufferOffset), an reallocate the headerBuffer if needed. |
| 169 | */ |
| 170 | BinaryXMLStructureDecoder.prototype.setHeaderBuffer = function(subarray, bufferOffset) { |
| 171 | var size = subarray.length + bufferOffset; |
| 172 | if (size > this.headerBuffer.length) { |
| 173 | // Reallocate the buffer. |
| 174 | var newHeaderBuffer = new Uint8Array(size + 5); |
| 175 | newHeaderBuffer.set(this.headerBuffer); |
| 176 | this.headerBuffer = newHeaderBuffer; |
| 177 | } |
| 178 | this.headerBuffer.set(subarray, bufferOffset); |
| 179 | } |