Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 2 | * This class is used to decode ccnb binary elements (blob, type/value pairs). |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 3 | * |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 4 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | var XML_EXT = 0x00; |
| 9 | |
| 10 | var XML_TAG = 0x01; |
| 11 | |
| 12 | var XML_DTAG = 0x02; |
| 13 | |
| 14 | var XML_ATTR = 0x03; |
| 15 | |
| 16 | var XML_DATTR = 0x04; |
| 17 | |
| 18 | var XML_BLOB = 0x05; |
| 19 | |
| 20 | var XML_UDATA = 0x06; |
| 21 | |
| 22 | var XML_CLOSE = 0x0; |
| 23 | |
| 24 | var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16; |
| 25 | |
| 26 | |
| 27 | var XML_TT_BITS = 3; |
| 28 | var XML_TT_MASK = ((1 << XML_TT_BITS) - 1); |
| 29 | var XML_TT_VAL_BITS = XML_TT_BITS + 1; |
| 30 | var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1); |
| 31 | var XML_REG_VAL_BITS = 7; |
| 32 | var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1); |
| 33 | var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80 |
| 34 | var BYTE_MASK = 0xFF; |
| 35 | var LONG_BYTES = 8; |
| 36 | var LONG_BITS = 64; |
| 37 | |
| 38 | var bits_11 = 0x0000007FF; |
| 39 | var bits_18 = 0x00003FFFF; |
| 40 | var bits_32 = 0x0FFFFFFFF; |
| 41 | |
| 42 | |
| 43 | |
| 44 | //returns a string |
| 45 | tagToString = function(/*long*/ tagVal) { |
| 46 | if ((tagVal >= 0) && (tagVal < CCNProtocolDTagsStrings.length)) { |
| 47 | return CCNProtocolDTagsStrings[tagVal]; |
| 48 | } else if (tagVal == CCNProtocolDTags.CCNProtocolDataUnit) { |
| 49 | return CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT; |
| 50 | } |
| 51 | return null; |
| 52 | }; |
| 53 | |
| 54 | //returns a Long |
| 55 | stringToTag = function(/*String*/ tagName) { |
| 56 | // the slow way, but right now we don't care.... want a static lookup for the forward direction |
| 57 | for (var i=0; i < CCNProtocolDTagsStrings.length; ++i) { |
| 58 | if ((null != CCNProtocolDTagsStrings[i]) && (CCNProtocolDTagsStrings[i] == tagName)) { |
| 59 | return i; |
| 60 | } |
| 61 | } |
| 62 | if (CCNProtocolDTags.CCNPROTOCOL_DATA_UNIT == tagName) { |
| 63 | return CCNProtocolDTags.CCNProtocolDataUnit; |
| 64 | } |
| 65 | return null; |
| 66 | }; |
| 67 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame^] | 68 | /** |
| 69 | * @constructor |
| 70 | */ |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 71 | var BinaryXMLDecoder = function BinaryXMLDecoder(input){ |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 72 | var MARK_LEN=512; |
| 73 | var DEBUG_MAX_LEN = 32768; |
| 74 | |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 75 | this.input = input; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 76 | this.offset = 0; |
| 77 | }; |
| 78 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 79 | BinaryXMLDecoder.prototype.initializeDecoding = function() { |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 80 | //if (!this.input.markSupported()) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 81 | //throw new IllegalArgumentException(this.getClass().getName() + ": input stream must support marking!"); |
| 82 | //} |
| 83 | } |
| 84 | |
| 85 | BinaryXMLDecoder.prototype.readStartDocument = function(){ |
| 86 | // Currently no start document in binary encoding. |
| 87 | } |
| 88 | |
| 89 | BinaryXMLDecoder.prototype.readEndDocument = function() { |
| 90 | // Currently no end document in binary encoding. |
| 91 | }; |
| 92 | |
| 93 | BinaryXMLDecoder.prototype.readStartElement = function( |
| 94 | //String |
| 95 | startTag, |
| 96 | //TreeMap<String, String> |
| 97 | attributes) { |
| 98 | |
| 99 | |
| 100 | //NOT SURE |
| 101 | //if(typeof startTag == 'number') |
| 102 | //startTag = tagToString(startTag); |
| 103 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 104 | //TypeAndVal |
Jeff Thompson | c3c611b | 2013-02-18 22:52:03 -0800 | [diff] [blame] | 105 | var tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 106 | |
| 107 | if (null == tv) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 108 | throw new ContentDecodingException(new Error("Expected start element: " + startTag + " got something not a tag.")); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 112 | var decodedTag = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 113 | //console.log(tv); |
| 114 | //console.log(typeof tv); |
| 115 | |
| 116 | //console.log(XML_TAG); |
| 117 | if (tv.type() == XML_TAG) { |
| 118 | //console.log('got here'); |
| 119 | //Log.info(Log.FAC_ENCODING, "Unexpected: got tag in readStartElement; looking for tag " + startTag + " got length: " + (int)tv.val()+1); |
| 120 | // Tag value represents length-1 as tags can never be empty. |
| 121 | var valval ; |
| 122 | if(typeof tv.val() == 'string'){ |
| 123 | valval = (parseInt(tv.val())) + 1; |
| 124 | } |
| 125 | else |
| 126 | valval = (tv.val())+ 1; |
| 127 | |
| 128 | //console.log('valval is ' +valval); |
| 129 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 130 | decodedTag = this.decodeUString(valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 131 | |
| 132 | } else if (tv.type() == XML_DTAG) { |
| 133 | //console.log('gothere'); |
| 134 | //console.log(tv.val()); |
| 135 | //decodedTag = tagToString(tv.val()); |
| 136 | //console.log() |
| 137 | decodedTag = tv.val(); |
| 138 | } |
| 139 | |
| 140 | //console.log(decodedTag); |
| 141 | //console.log('startTag is '+startTag); |
| 142 | |
| 143 | |
| 144 | if ((null == decodedTag) || decodedTag != startTag ) { |
Jeff Thompson | b7e190b | 2012-11-28 18:38:15 -0800 | [diff] [blame] | 145 | console.log('expecting '+ startTag + ' but got '+ decodedTag); |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 146 | throw new ContentDecodingException(new Error("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")")); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // DKS: does not read attributes out of stream if caller doesn't |
| 150 | // ask for them. Should possibly peek and skip over them regardless. |
| 151 | // TODO: fix this |
| 152 | if (null != attributes) { |
| 153 | readAttributes(attributes); |
| 154 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | BinaryXMLDecoder.prototype.readAttributes = function( |
Jeff Thompson | c3c611b | 2013-02-18 22:52:03 -0800 | [diff] [blame] | 159 | // array of [attributeName, attributeValue] |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 160 | attributes) { |
| 161 | |
| 162 | if (null == attributes) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | try { |
| 167 | // Now need to get attributes. |
| 168 | //TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 169 | var nextTV = this.peekTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 170 | |
| 171 | while ((null != nextTV) && ((XML_ATTR == nextTV.type()) || |
| 172 | (XML_DATTR == nextTV.type()))) { |
| 173 | |
| 174 | // Decode this attribute. First, really read the type and value. |
| 175 | //this.TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 176 | var thisTV = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 177 | |
| 178 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 179 | var attributeName = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 180 | if (XML_ATTR == thisTV.type()) { |
| 181 | // Tag value represents length-1 as attribute names cannot be empty. |
| 182 | var valval ; |
Jeff Thompson | c3c611b | 2013-02-18 22:52:03 -0800 | [diff] [blame] | 183 | if(typeof thisTV.val() == 'string'){ |
| 184 | valval = (parseInt(thisTV.val())) + 1; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 185 | } |
| 186 | else |
Jeff Thompson | c3c611b | 2013-02-18 22:52:03 -0800 | [diff] [blame] | 187 | valval = (thisTV.val())+ 1; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 189 | attributeName = this.decodeUString(valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 190 | |
| 191 | } else if (XML_DATTR == thisTV.type()) { |
| 192 | // DKS TODO are attributes same or different dictionary? |
| 193 | attributeName = tagToString(thisTV.val()); |
| 194 | if (null == attributeName) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 195 | throw new ContentDecodingException(new Error("Unknown DATTR value" + thisTV.val())); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | // Attribute values are always UDATA |
| 199 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 200 | var attributeValue = this.decodeUString(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 201 | |
| 202 | // |
| 203 | attributes.push([attributeName, attributeValue]); |
| 204 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 205 | nextTV = this.peekTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 206 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 207 | } catch ( e) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 208 | throw new ContentDecodingException(new Error("readStartElement", e)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 209 | } |
| 210 | }; |
| 211 | |
| 212 | //returns a string |
| 213 | BinaryXMLDecoder.prototype.peekStartElementAsString = function() { |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 214 | //this.input.mark(MARK_LEN); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 215 | |
| 216 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 217 | var decodedTag = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 218 | var previousOffset = this.offset; |
| 219 | try { |
| 220 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 221 | // a special exception subtype, or redo the work here. |
| 222 | //this.TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 223 | var tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 224 | |
| 225 | if (null != tv) { |
| 226 | |
| 227 | if (tv.type() == XML_TAG) { |
| 228 | /*if (tv.val()+1 > DEBUG_MAX_LEN) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 229 | throw new ContentDecodingException(new Error("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!")(; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 230 | }*/ |
| 231 | |
| 232 | // Tag value represents length-1 as tags can never be empty. |
| 233 | var valval ; |
| 234 | if(typeof tv.val() == 'string'){ |
| 235 | valval = (parseInt(tv.val())) + 1; |
| 236 | } |
| 237 | else |
| 238 | valval = (tv.val())+ 1; |
| 239 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 240 | decodedTag = this.decodeUString(valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 241 | |
| 242 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 243 | |
| 244 | } else if (tv.type() == XML_DTAG) { |
| 245 | decodedTag = tagToString(tv.val()); |
| 246 | } |
| 247 | |
| 248 | } // else, not a type and val, probably an end element. rewind and return false. |
| 249 | |
| 250 | } catch ( e) { |
| 251 | |
| 252 | } finally { |
| 253 | try { |
| 254 | this.offset = previousOffset; |
| 255 | } catch ( e) { |
| 256 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 257 | throw new ContentDecodingException(new Error("Cannot reset stream! " + e.getMessage(), e)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | return decodedTag; |
| 261 | }; |
| 262 | |
| 263 | BinaryXMLDecoder.prototype.peekStartElement = function( |
| 264 | //String |
| 265 | startTag) { |
| 266 | //String |
| 267 | if(typeof startTag == 'string'){ |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 268 | var decodedTag = this.peekStartElementAsString(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 269 | |
| 270 | if ((null != decodedTag) && decodedTag == startTag) { |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | else if(typeof startTag == 'number'){ |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 276 | var decodedTag = this.peekStartElementAsLong(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 277 | if ((null != decodedTag) && decodedTag == startTag) { |
| 278 | return true; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | else{ |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 283 | throw new ContentDecodingException(new Error("SHOULD BE STRING OR NUMBER")); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | //returns Long |
| 287 | BinaryXMLDecoder.prototype.peekStartElementAsLong = function() { |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 288 | //this.input.mark(MARK_LEN); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 289 | |
| 290 | //Long |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 291 | var decodedTag = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 292 | |
| 293 | var previousOffset = this.offset; |
| 294 | |
| 295 | try { |
| 296 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 297 | // a special exception subtype, or redo the work here. |
| 298 | //this.TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 299 | var tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 300 | |
| 301 | if (null != tv) { |
| 302 | |
| 303 | if (tv.type() == XML_TAG) { |
| 304 | if (tv.val()+1 > DEBUG_MAX_LEN) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 305 | throw new ContentDecodingException(new Error("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!")); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | var valval ; |
| 309 | if(typeof tv.val() == 'string'){ |
| 310 | valval = (parseInt(tv.val())) + 1; |
| 311 | } |
| 312 | else |
| 313 | valval = (tv.val())+ 1; |
| 314 | |
| 315 | // Tag value represents length-1 as tags can never be empty. |
| 316 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 317 | var strTag = this.decodeUString(valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 318 | |
| 319 | decodedTag = stringToTag(strTag); |
| 320 | |
| 321 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 322 | |
| 323 | } else if (tv.type() == XML_DTAG) { |
| 324 | decodedTag = tv.val(); |
| 325 | } |
| 326 | |
| 327 | } // else, not a type and val, probably an end element. rewind and return false. |
| 328 | |
| 329 | } catch ( e) { |
| 330 | |
| 331 | } finally { |
| 332 | try { |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 333 | //this.input.reset(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 334 | this.offset = previousOffset; |
| 335 | } catch ( e) { |
| 336 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 337 | throw new Error("Cannot reset stream! " + e.getMessage(), e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | return decodedTag; |
| 341 | }; |
| 342 | |
| 343 | |
Jeff Thompson | efff43b | 2013-03-10 17:10:26 -0700 | [diff] [blame] | 344 | // Returns a Uint8Array. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 345 | BinaryXMLDecoder.prototype.readBinaryElement = function( |
| 346 | //long |
| 347 | startTag, |
| 348 | //TreeMap<String, String> |
Jeff Thompson | d4a1f86 | 2013-03-10 17:30:31 -0700 | [diff] [blame] | 349 | attributes, |
| 350 | //boolean |
| 351 | allowNull){ |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 352 | this.readStartElement(startTag, attributes); |
Jeff Thompson | d4a1f86 | 2013-03-10 17:30:31 -0700 | [diff] [blame] | 353 | return this.readBlob(allowNull); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | |
| 357 | BinaryXMLDecoder.prototype.readEndElement = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 358 | if(LOG>4)console.log('this.offset is '+this.offset); |
| 359 | |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 360 | var next = this.input[this.offset]; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 361 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 362 | this.offset++; |
| 363 | //read(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 364 | |
| 365 | if(LOG>4)console.log('XML_CLOSE IS '+XML_CLOSE); |
| 366 | if(LOG>4)console.log('next is '+next); |
| 367 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 368 | if (next != XML_CLOSE) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 369 | console.log("Expected end element, got: " + next); |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 370 | throw new ContentDecodingException(new Error("Expected end element, got: " + next)); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 371 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | |
| 375 | //String |
| 376 | BinaryXMLDecoder.prototype.readUString = function(){ |
| 377 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 378 | var ustring = this.decodeUString(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 379 | this.readEndElement(); |
| 380 | return ustring; |
| 381 | |
| 382 | }; |
| 383 | |
| 384 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame^] | 385 | /** |
Jeff Thompson | d4a1f86 | 2013-03-10 17:30:31 -0700 | [diff] [blame] | 386 | * Read a blob as well as the end element. Returns a Uint8Array (or null for missing blob). |
| 387 | * If the blob is missing and allowNull is false (default), throw an exception. Otherwise, |
| 388 | * just read the end element and return null. |
| 389 | */ |
| 390 | BinaryXMLDecoder.prototype.readBlob = function(allowNull) { |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 391 | if (this.input[this.offset] == XML_CLOSE && allowNull) { |
Jeff Thompson | d4a1f86 | 2013-03-10 17:30:31 -0700 | [diff] [blame] | 392 | this.readEndElement(); |
| 393 | return null; |
| 394 | } |
| 395 | |
Jeff Thompson | efff43b | 2013-03-10 17:10:26 -0700 | [diff] [blame] | 396 | var blob = this.decodeBlob(); |
| 397 | this.readEndElement(); |
| 398 | return blob; |
| 399 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 400 | |
| 401 | |
| 402 | //CCNTime |
| 403 | BinaryXMLDecoder.prototype.readDateTime = function( |
| 404 | //long |
| 405 | startTag) { |
| 406 | //byte [] |
| 407 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 408 | var byteTimestamp = this.readBinaryElement(startTag); |
| 409 | |
| 410 | //var lontimestamp = DataUtils.byteArrayToUnsignedLong(byteTimestamp); |
| 411 | |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 412 | byteTimestamp = DataUtils.toHex(byteTimestamp); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 413 | |
| 414 | |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 415 | byteTimestamp = parseInt(byteTimestamp, 16); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 416 | |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 417 | var lontimestamp = (byteTimestamp/ 4096) * 1000; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 418 | |
| 419 | //if(lontimestamp<0) lontimestamp = - lontimestamp; |
| 420 | |
Jeff Thompson | 0da6561 | 2013-03-10 16:30:27 -0700 | [diff] [blame] | 421 | if(LOG>4) console.log('DECODED DATE WITH VALUE'); |
| 422 | if(LOG>4) console.log(lontimestamp); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 423 | |
| 424 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 425 | //CCNTime |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 426 | var timestamp = new CCNTime(lontimestamp); |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 427 | //timestamp.setDateBinary(byteTimestamp); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 428 | |
| 429 | if (null == timestamp) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 430 | throw new ContentDecodingException(new Error("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp))); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 431 | } |
| 432 | return timestamp; |
| 433 | }; |
| 434 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 435 | BinaryXMLDecoder.prototype.decodeTypeAndVal = function() { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 436 | |
Jeff Thompson | f72d7ed | 2012-10-25 20:57:07 -0700 | [diff] [blame] | 437 | /*int*/var type = -1; |
| 438 | /*long*/var val = 0; |
| 439 | /*boolean*/var more = true; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 440 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 441 | do { |
| 442 | |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 443 | var next = this.input[this.offset ]; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 444 | |
| 445 | |
| 446 | if (next < 0) { |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | if ((0 == next) && (0 == val)) { |
| 451 | return null; |
| 452 | } |
| 453 | |
| 454 | more = (0 == (next & XML_TT_NO_MORE)); |
| 455 | |
| 456 | if (more) { |
| 457 | val = val << XML_REG_VAL_BITS; |
| 458 | val |= (next & XML_REG_VAL_MASK); |
| 459 | } else { |
| 460 | |
| 461 | type = next & XML_TT_MASK; |
| 462 | val = val << XML_TT_VAL_BITS; |
| 463 | val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK); |
| 464 | } |
| 465 | |
| 466 | this.offset++; |
| 467 | |
| 468 | } while (more); |
| 469 | |
Jeff Thompson | 0da6561 | 2013-03-10 16:30:27 -0700 | [diff] [blame] | 470 | if(LOG>4)console.log('TYPE is '+ type + ' VAL is '+ val); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 471 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 472 | return new TypeAndVal(type, val); |
| 473 | }; |
| 474 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 475 | //TypeAndVal |
Jeff Thompson | 7480aca | 2013-03-10 16:01:33 -0700 | [diff] [blame] | 476 | BinaryXMLDecoder.prototype.peekTypeAndVal = function() { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 477 | //TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 478 | var tv = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 479 | var previousOffset = this.offset; |
| 480 | |
| 481 | try { |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 482 | tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 483 | } finally { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 484 | this.offset = previousOffset; |
| 485 | } |
| 486 | |
| 487 | return tv; |
| 488 | }; |
| 489 | |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 490 | //Uint8Array |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 491 | BinaryXMLDecoder.prototype.decodeBlob = function( |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 492 | //int |
| 493 | blobLength) { |
| 494 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 495 | if(null == blobLength){ |
| 496 | //TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 497 | var tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 498 | |
| 499 | var valval ; |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 500 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 501 | if(typeof tv.val() == 'string'){ |
| 502 | valval = (parseInt(tv.val())); |
| 503 | } |
| 504 | else |
| 505 | valval = (tv.val()); |
| 506 | |
| 507 | //console.log('valval here is ' + valval); |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 508 | return this.decodeBlob(valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | // |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 512 | //Uint8Array |
Jeff Thompson | 14afeea | 2013-06-24 16:23:30 -0700 | [diff] [blame] | 513 | var bytes = this.input.subarray(this.offset, this.offset+ blobLength); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 514 | this.offset += blobLength; |
| 515 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 516 | return bytes; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 517 | }; |
| 518 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 519 | //String |
| 520 | BinaryXMLDecoder.prototype.decodeUString = function( |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 521 | //int |
| 522 | byteLength) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 523 | if(null == byteLength ){ |
| 524 | var tempStreamPosition = this.offset; |
| 525 | |
| 526 | //TypeAndVal |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 527 | var tv = this.decodeTypeAndVal(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 528 | |
Jeff Thompson | 0da6561 | 2013-03-10 16:30:27 -0700 | [diff] [blame] | 529 | if(LOG>4)console.log('TV is '+tv); |
| 530 | if(LOG>4)console.log(tv); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 531 | |
Jeff Thompson | 0da6561 | 2013-03-10 16:30:27 -0700 | [diff] [blame] | 532 | if(LOG>4)console.log('Type of TV is '+typeof tv); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 533 | |
| 534 | if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null |
| 535 | //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST)) |
| 536 | //Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob."); |
| 537 | |
| 538 | this.offset = tempStreamPosition; |
| 539 | |
| 540 | return ""; |
| 541 | } |
| 542 | |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 543 | return this.decodeUString(tv.val()); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 544 | } |
| 545 | else{ |
Wentao Shang | 882e34e | 2013-01-05 02:49:51 -0800 | [diff] [blame] | 546 | //uint8array |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 547 | var stringBytes = this.decodeBlob(byteLength); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 548 | |
| 549 | //return DataUtils.getUTF8StringFromBytes(stringBytes); |
| 550 | return DataUtils.toString(stringBytes); |
| 551 | |
| 552 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 553 | }; |
| 554 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 555 | //OBject containg a pair of type and value |
| 556 | var TypeAndVal = function TypeAndVal(_type,_val) { |
| 557 | this.t = _type; |
| 558 | this.v = _val; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 559 | }; |
| 560 | |
| 561 | TypeAndVal.prototype.type = function(){ |
| 562 | return this.t; |
| 563 | }; |
| 564 | |
| 565 | TypeAndVal.prototype.val = function(){ |
| 566 | return this.v; |
| 567 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 568 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 569 | BinaryXMLDecoder.prototype.readIntegerElement =function( |
| 570 | //String |
| 571 | startTag) { |
| 572 | |
| 573 | //String |
| 574 | if(LOG>4) console.log('READING INTEGER '+ startTag); |
| 575 | if(LOG>4) console.log('TYPE OF '+ typeof startTag); |
| 576 | |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 577 | var strVal = this.readUTF8Element(startTag); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 578 | |
| 579 | return parseInt(strVal); |
| 580 | }; |
| 581 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 582 | BinaryXMLDecoder.prototype.readUTF8Element =function( |
| 583 | //String |
| 584 | startTag, |
| 585 | //TreeMap<String, String> |
| 586 | attributes) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 587 | //throws Error where name == "ContentDecodingException" |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 588 | |
| 589 | this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes |
| 590 | //String |
Jeff Thompson | 41ef0f9 | 2012-11-24 09:22:55 -0800 | [diff] [blame] | 591 | var strElementText = this.readUString(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 592 | return strElementText; |
| 593 | }; |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 594 | |
Jeff Thompson | 2b14c7e | 2013-07-29 15:09:56 -0700 | [diff] [blame^] | 595 | /** |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 596 | * Set the offset into the input, used for the next read. |
| 597 | */ |
| 598 | BinaryXMLDecoder.prototype.seek = function( |
| 599 | //int |
| 600 | offset) { |
Jeff Thompson | c92706b | 2012-11-11 19:12:58 -0800 | [diff] [blame] | 601 | this.offset = offset; |
Jeff Thompson | 1b19659 | 2012-10-14 17:07:40 -0700 | [diff] [blame] | 602 | } |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 603 | |
| 604 | /* |
| 605 | * Call with: throw new ContentDecodingException(new Error("message")). |
| 606 | */ |
| 607 | function ContentDecodingException(error) { |
| 608 | this.message = error.message; |
| 609 | // Copy lineNumber, etc. from where new Error was called. |
| 610 | for (var prop in error) |
| 611 | this[prop] = error[prop]; |
| 612 | } |
| 613 | ContentDecodingException.prototype = new Error(); |
| 614 | ContentDecodingException.prototype.name = "ContentDecodingException"; |
| 615 | |