Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This class is used to encode and decode binary elements ( blog, type/value pairs) |
| 3 | * |
| 4 | * @author: ucla-cs |
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 | |
| 68 | //console.log(stringToTag(64)); |
| 69 | var BinaryXMLDecoder = function BinaryXMLDecoder(istream){ |
| 70 | var MARK_LEN=512; |
| 71 | var DEBUG_MAX_LEN = 32768; |
| 72 | |
| 73 | this.istream = istream; |
| 74 | //console.log('istream is '+ this.istream); |
| 75 | this.offset = 0; |
| 76 | }; |
| 77 | |
| 78 | |
| 79 | BinaryXMLDecoder.prototype.readStartElement =function( |
| 80 | //String |
| 81 | startTag, |
| 82 | //TreeMap<String, String> |
| 83 | attributes) { |
| 84 | |
| 85 | try { |
| 86 | //this.TypeAndVal |
| 87 | tv = this.decodeTypeAndVal(this.istream); |
| 88 | |
| 89 | if (null == tv) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 90 | throw new Error("Expected start element: " + startTag + " got something not a tag."); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | //String |
| 94 | decodedTag = null; |
| 95 | |
| 96 | if (tv.type() == XML_TAG) { |
| 97 | |
| 98 | decodedTag = this.decodeUString(this.Istream, tv.val()+1); |
| 99 | |
| 100 | } else if (tv.type() == XML_DTAG) { |
| 101 | decodedTag = tagToString(tv.val()); |
| 102 | } |
| 103 | |
| 104 | if ((null == decodedTag) || decodedTag != startTag) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 105 | throw new Error("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (null != attributes) { |
| 109 | readAttributes(attributes); |
| 110 | } |
| 111 | |
| 112 | } catch (e) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 113 | throw new Error("readStartElement", e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
| 117 | BinaryXMLDecoder.prototype.readAttributes = function( |
| 118 | //TreeMap<String,String> |
| 119 | attributes){ |
| 120 | |
| 121 | if (null == attributes) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | try { |
| 126 | |
| 127 | //this.TypeAndVal |
| 128 | nextTV = this.peekTypeAndVal(this.istream); |
| 129 | |
| 130 | while ((null != nextTV) && ((XML_ATTR == nextTV.type()) || |
| 131 | (XML_DATTR == nextTV.type()))) { |
| 132 | |
| 133 | //this.TypeAndVal |
| 134 | thisTV = this.decodeTypeAndVal(this.Istream); |
| 135 | |
| 136 | var attributeName = null; |
| 137 | if (XML_ATTR == thisTV.type()) { |
| 138 | |
| 139 | attributeName = this.decodeUString(this.istream, thisTV.val()+1); |
| 140 | |
| 141 | } else if (XML_DATTR == thisTV.type()) { |
| 142 | // DKS TODO are attributes same or different dictionary? |
| 143 | attributeName = tagToString(thisTV.val()); |
| 144 | if (null == attributeName) { |
| 145 | throw new ContentDecodingException("Unknown DATTR value" + thisTV.val()); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | var attributeValue = this.decodeUString(this.istream); |
| 150 | |
| 151 | attributes.put(attributeName, attributeValue); |
| 152 | |
| 153 | nextTV = this.peekTypeAndVal(this.istream); |
| 154 | } |
| 155 | |
| 156 | } catch ( e) { |
| 157 | |
| 158 | throw new ContentDecodingException("readStartElement", e); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | |
| 163 | BinaryXMLDecoder.prototype.initializeDecoding = function() { |
| 164 | //if (!this.istream.markSupported()) { |
| 165 | //throw new IllegalArgumentException(this.getClass().getName() + ": input stream must support marking!"); |
| 166 | //} |
| 167 | } |
| 168 | |
| 169 | BinaryXMLDecoder.prototype.readStartDocument = function(){ |
| 170 | // Currently no start document in binary encoding. |
| 171 | } |
| 172 | |
| 173 | BinaryXMLDecoder.prototype.readEndDocument = function() { |
| 174 | // Currently no end document in binary encoding. |
| 175 | }; |
| 176 | |
| 177 | BinaryXMLDecoder.prototype.readStartElement = function( |
| 178 | //String |
| 179 | startTag, |
| 180 | //TreeMap<String, String> |
| 181 | attributes) { |
| 182 | |
| 183 | |
| 184 | //NOT SURE |
| 185 | //if(typeof startTag == 'number') |
| 186 | //startTag = tagToString(startTag); |
| 187 | |
| 188 | //try { |
| 189 | //TypeAndVal |
| 190 | tv = this.decodeTypeAndVal(this.istream); |
| 191 | |
| 192 | if (null == tv) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 193 | throw new Error("Expected start element: " + startTag + " got something not a tag."); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | //String |
| 197 | decodedTag = null; |
| 198 | //console.log(tv); |
| 199 | //console.log(typeof tv); |
| 200 | |
| 201 | //console.log(XML_TAG); |
| 202 | if (tv.type() == XML_TAG) { |
| 203 | //console.log('got here'); |
| 204 | //Log.info(Log.FAC_ENCODING, "Unexpected: got tag in readStartElement; looking for tag " + startTag + " got length: " + (int)tv.val()+1); |
| 205 | // Tag value represents length-1 as tags can never be empty. |
| 206 | var valval ; |
| 207 | if(typeof tv.val() == 'string'){ |
| 208 | valval = (parseInt(tv.val())) + 1; |
| 209 | } |
| 210 | else |
| 211 | valval = (tv.val())+ 1; |
| 212 | |
| 213 | //console.log('valval is ' +valval); |
| 214 | |
| 215 | decodedTag = this.decodeUString(this.istream, valval); |
| 216 | |
| 217 | } else if (tv.type() == XML_DTAG) { |
| 218 | //console.log('gothere'); |
| 219 | //console.log(tv.val()); |
| 220 | //decodedTag = tagToString(tv.val()); |
| 221 | //console.log() |
| 222 | decodedTag = tv.val(); |
| 223 | } |
| 224 | |
| 225 | //console.log(decodedTag); |
| 226 | //console.log('startTag is '+startTag); |
| 227 | |
| 228 | |
| 229 | if ((null == decodedTag) || decodedTag != startTag ) { |
| 230 | console.log('expecting '+ startag + ' but got '+ decodedTag); |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 231 | throw new Error("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // DKS: does not read attributes out of stream if caller doesn't |
| 235 | // ask for them. Should possibly peek and skip over them regardless. |
| 236 | // TODO: fix this |
| 237 | if (null != attributes) { |
| 238 | readAttributes(attributes); |
| 239 | } |
| 240 | |
| 241 | //} catch ( e) { |
| 242 | //console.log(e); |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 243 | //throw new Error("readStartElement", e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 244 | //} |
| 245 | } |
| 246 | |
| 247 | |
| 248 | BinaryXMLDecoder.prototype.readAttributes = function( |
| 249 | //TreeMap<String,String> |
| 250 | attributes) { |
| 251 | |
| 252 | if (null == attributes) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | try { |
| 257 | // Now need to get attributes. |
| 258 | //TypeAndVal |
| 259 | nextTV = this.peekTypeAndVal(this.istream); |
| 260 | |
| 261 | while ((null != nextTV) && ((XML_ATTR == nextTV.type()) || |
| 262 | (XML_DATTR == nextTV.type()))) { |
| 263 | |
| 264 | // Decode this attribute. First, really read the type and value. |
| 265 | //this.TypeAndVal |
| 266 | thisTV = this.decodeTypeAndVal(this.istream); |
| 267 | |
| 268 | //String |
| 269 | attributeName = null; |
| 270 | if (XML_ATTR == thisTV.type()) { |
| 271 | // Tag value represents length-1 as attribute names cannot be empty. |
| 272 | var valval ; |
| 273 | if(typeof tv.val() == 'string'){ |
| 274 | valval = (parseInt(tv.val())) + 1; |
| 275 | } |
| 276 | else |
| 277 | valval = (tv.val())+ 1; |
| 278 | |
| 279 | attributeName = this.decodeUString(this.istream,valval); |
| 280 | |
| 281 | } else if (XML_DATTR == thisTV.type()) { |
| 282 | // DKS TODO are attributes same or different dictionary? |
| 283 | attributeName = tagToString(thisTV.val()); |
| 284 | if (null == attributeName) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 285 | throw new Error("Unknown DATTR value" + thisTV.val()); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | // Attribute values are always UDATA |
| 289 | //String |
| 290 | attributeValue = this.decodeUString(this.istream); |
| 291 | |
| 292 | // |
| 293 | attributes.push([attributeName, attributeValue]); |
| 294 | |
| 295 | nextTV = this.peekTypeAndVal(this.istream); |
| 296 | } |
| 297 | |
| 298 | } catch ( e) { |
| 299 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 300 | throw new Error("readStartElement", e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 301 | } |
| 302 | }; |
| 303 | |
| 304 | //returns a string |
| 305 | BinaryXMLDecoder.prototype.peekStartElementAsString = function() { |
| 306 | //this.istream.mark(MARK_LEN); |
| 307 | |
| 308 | //String |
| 309 | decodedTag = null; |
| 310 | var previousOffset = this.offset; |
| 311 | try { |
| 312 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 313 | // a special exception subtype, or redo the work here. |
| 314 | //this.TypeAndVal |
| 315 | tv = this.decodeTypeAndVal(this.istream); |
| 316 | |
| 317 | if (null != tv) { |
| 318 | |
| 319 | if (tv.type() == XML_TAG) { |
| 320 | /*if (tv.val()+1 > DEBUG_MAX_LEN) { |
| 321 | throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!"); |
| 322 | }*/ |
| 323 | |
| 324 | // Tag value represents length-1 as tags can never be empty. |
| 325 | var valval ; |
| 326 | if(typeof tv.val() == 'string'){ |
| 327 | valval = (parseInt(tv.val())) + 1; |
| 328 | } |
| 329 | else |
| 330 | valval = (tv.val())+ 1; |
| 331 | |
| 332 | decodedTag = this.decodeUString(this.istream, valval); |
| 333 | |
| 334 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 335 | |
| 336 | } else if (tv.type() == XML_DTAG) { |
| 337 | decodedTag = tagToString(tv.val()); |
| 338 | } |
| 339 | |
| 340 | } // else, not a type and val, probably an end element. rewind and return false. |
| 341 | |
| 342 | } catch ( e) { |
| 343 | |
| 344 | } finally { |
| 345 | try { |
| 346 | this.offset = previousOffset; |
| 347 | } catch ( e) { |
| 348 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
| 349 | throw new ContentDecodingException("Cannot reset stream! " + e.getMessage(), e); |
| 350 | } |
| 351 | } |
| 352 | return decodedTag; |
| 353 | }; |
| 354 | |
| 355 | BinaryXMLDecoder.prototype.peekStartElement = function( |
| 356 | //String |
| 357 | startTag) { |
| 358 | //String |
| 359 | if(typeof startTag == 'string'){ |
| 360 | decodedTag = this.peekStartElementAsString(); |
| 361 | |
| 362 | if ((null != decodedTag) && decodedTag == startTag) { |
| 363 | return true; |
| 364 | } |
| 365 | return false; |
| 366 | } |
| 367 | else if(typeof startTag == 'number'){ |
| 368 | decodedTag = this.peekStartElementAsLong(); |
| 369 | if ((null != decodedTag) && decodedTag == startTag) { |
| 370 | return true; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | else{ |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 375 | throw new Error("SHOULD BE STRING OR NUMBER"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | //returns Long |
| 379 | BinaryXMLDecoder.prototype.peekStartElementAsLong = function() { |
| 380 | //this.istream.mark(MARK_LEN); |
| 381 | |
| 382 | //Long |
| 383 | decodedTag = null; |
| 384 | |
| 385 | var previousOffset = this.offset; |
| 386 | |
| 387 | try { |
| 388 | // Have to distinguish genuine errors from wrong tags. Could either use |
| 389 | // a special exception subtype, or redo the work here. |
| 390 | //this.TypeAndVal |
| 391 | tv = this.decodeTypeAndVal(this.istream); |
| 392 | |
| 393 | if (null != tv) { |
| 394 | |
| 395 | if (tv.type() == XML_TAG) { |
| 396 | if (tv.val()+1 > DEBUG_MAX_LEN) { |
| 397 | throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!"); |
| 398 | } |
| 399 | |
| 400 | var valval ; |
| 401 | if(typeof tv.val() == 'string'){ |
| 402 | valval = (parseInt(tv.val())) + 1; |
| 403 | } |
| 404 | else |
| 405 | valval = (tv.val())+ 1; |
| 406 | |
| 407 | // Tag value represents length-1 as tags can never be empty. |
| 408 | //String |
| 409 | strTag = this.decodeUString(this.istream, valval); |
| 410 | |
| 411 | decodedTag = stringToTag(strTag); |
| 412 | |
| 413 | //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag); |
| 414 | |
| 415 | } else if (tv.type() == XML_DTAG) { |
| 416 | decodedTag = tv.val(); |
| 417 | } |
| 418 | |
| 419 | } // else, not a type and val, probably an end element. rewind and return false. |
| 420 | |
| 421 | } catch ( e) { |
| 422 | |
| 423 | } finally { |
| 424 | try { |
| 425 | //this.istream.reset(); |
| 426 | this.offset = previousOffset; |
| 427 | } catch ( e) { |
| 428 | Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e); |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 429 | throw new Error("Cannot reset stream! " + e.getMessage(), e); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | return decodedTag; |
| 433 | }; |
| 434 | |
| 435 | |
| 436 | // returns a byte[] |
| 437 | BinaryXMLDecoder.prototype.readBinaryElement = function( |
| 438 | //long |
| 439 | startTag, |
| 440 | //TreeMap<String, String> |
| 441 | attributes){ |
| 442 | //byte [] |
| 443 | blob = null; |
| 444 | |
| 445 | this.readStartElement(startTag, attributes); |
| 446 | blob = this.readBlob(); |
| 447 | |
| 448 | |
| 449 | return blob; |
| 450 | |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | BinaryXMLDecoder.prototype.readEndElement = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 455 | //try { |
| 456 | if(LOG>4)console.log('this.offset is '+this.offset); |
| 457 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 458 | var next = this.istream[this.offset]; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 459 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 460 | this.offset++; |
| 461 | //read(); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 462 | |
| 463 | if(LOG>4)console.log('XML_CLOSE IS '+XML_CLOSE); |
| 464 | if(LOG>4)console.log('next is '+next); |
| 465 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 466 | if (next != XML_CLOSE) { |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 467 | console.log("Expected end element, got: " + next); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 468 | throw new ContentDecodingException("Expected end element, got: " + next); |
| 469 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 470 | //} catch ( e) { |
| 471 | //throw new ContentDecodingException(e); |
| 472 | //} |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | |
| 476 | //String |
| 477 | BinaryXMLDecoder.prototype.readUString = function(){ |
| 478 | //String |
| 479 | ustring = this.decodeUString(this.istream); |
| 480 | this.readEndElement(); |
| 481 | return ustring; |
| 482 | |
| 483 | }; |
| 484 | |
| 485 | |
| 486 | //returns a byte[] |
| 487 | BinaryXMLDecoder.prototype.readBlob = function() { |
| 488 | //byte [] |
| 489 | |
| 490 | blob = this.decodeBlob(this.istream); |
| 491 | this.readEndElement(); |
| 492 | return blob; |
| 493 | |
| 494 | }; |
| 495 | |
| 496 | |
| 497 | //CCNTime |
| 498 | BinaryXMLDecoder.prototype.readDateTime = function( |
| 499 | //long |
| 500 | startTag) { |
| 501 | //byte [] |
| 502 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 503 | var byteTimestamp = this.readBinaryElement(startTag); |
| 504 | |
| 505 | //var lontimestamp = DataUtils.byteArrayToUnsignedLong(byteTimestamp); |
| 506 | |
| 507 | var byteTimestamp = DataUtils.toHex(byteTimestamp); |
| 508 | |
| 509 | |
| 510 | var byteTimestamp = parseInt(byteTimestamp, 16); |
| 511 | |
| 512 | lontimestamp = (byteTimestamp/ 4096) * 1000; |
| 513 | |
| 514 | //if(lontimestamp<0) lontimestamp = - lontimestamp; |
| 515 | |
| 516 | if(LOG>3) console.log('DECODED DATE WITH VALUE'); |
| 517 | if(LOG>3) console.log(lontimestamp); |
| 518 | |
| 519 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 520 | //CCNTime |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 521 | timestamp = new CCNTime(lontimestamp); |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 522 | //timestamp.setDateBinary(byteTimestamp); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 523 | |
| 524 | if (null == timestamp) { |
| 525 | throw new ContentDecodingException("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp)); |
| 526 | } |
| 527 | return timestamp; |
| 528 | }; |
| 529 | |
| 530 | BinaryXMLDecoder.prototype.decodeTypeAndVal = function( |
| 531 | /*InputStream*/ |
| 532 | istream) { |
| 533 | |
| 534 | /*int*/next; |
| 535 | /*int*/type = -1; |
| 536 | /*long*/val = 0; |
| 537 | /*boolean*/more = true; |
| 538 | |
| 539 | |
| 540 | //var savedOffset = this.offset; |
| 541 | var count = 0; |
| 542 | |
| 543 | do { |
| 544 | |
| 545 | var next = this.istream[this.offset ]; |
| 546 | |
| 547 | |
| 548 | if (next < 0) { |
| 549 | return null; |
| 550 | } |
| 551 | |
| 552 | if ((0 == next) && (0 == val)) { |
| 553 | return null; |
| 554 | } |
| 555 | |
| 556 | more = (0 == (next & XML_TT_NO_MORE)); |
| 557 | |
| 558 | if (more) { |
| 559 | val = val << XML_REG_VAL_BITS; |
| 560 | val |= (next & XML_REG_VAL_MASK); |
| 561 | } else { |
| 562 | |
| 563 | type = next & XML_TT_MASK; |
| 564 | val = val << XML_TT_VAL_BITS; |
| 565 | val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK); |
| 566 | } |
| 567 | |
| 568 | this.offset++; |
| 569 | |
| 570 | } while (more); |
| 571 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 572 | if(LOG>3)console.log('TYPE is '+ type + ' VAL is '+ val); |
| 573 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 574 | return new TypeAndVal(type, val); |
| 575 | }; |
| 576 | |
| 577 | |
| 578 | |
| 579 | //TypeAndVal |
| 580 | BinaryXMLDecoder.peekTypeAndVal = function( |
| 581 | //InputStream |
| 582 | istream) { |
| 583 | //TypeAndVal |
| 584 | tv = null; |
| 585 | |
| 586 | //istream.mark(LONG_BYTES*2); |
| 587 | |
| 588 | var previousOffset = this.offset; |
| 589 | |
| 590 | try { |
| 591 | tv = this.decodeTypeAndVal(this.istream); |
| 592 | } finally { |
| 593 | //istream.reset(); |
| 594 | this.offset = previousOffset; |
| 595 | } |
| 596 | |
| 597 | return tv; |
| 598 | }; |
| 599 | |
| 600 | |
| 601 | //byte[] |
| 602 | BinaryXMLDecoder.prototype.decodeBlob = function( |
| 603 | //InputStream |
| 604 | istream, |
| 605 | //int |
| 606 | blobLength) { |
| 607 | |
| 608 | |
| 609 | if(null == blobLength){ |
| 610 | //TypeAndVal |
| 611 | tv = this.decodeTypeAndVal(this.istream); |
| 612 | |
| 613 | var valval ; |
Meki Cherkaoui | b21911b | 2012-05-18 16:54:37 -0700 | [diff] [blame] | 614 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 615 | if(typeof tv.val() == 'string'){ |
| 616 | valval = (parseInt(tv.val())); |
| 617 | } |
| 618 | else |
| 619 | valval = (tv.val()); |
| 620 | |
| 621 | //console.log('valval here is ' + valval); |
| 622 | return this.decodeBlob(this.istream, valval); |
| 623 | } |
| 624 | |
| 625 | // |
| 626 | //byte [] |
| 627 | |
| 628 | bytes = this.istream.slice(this.offset, this.offset+ blobLength); |
| 629 | this.offset += blobLength; |
| 630 | |
| 631 | //int |
| 632 | return bytes; |
| 633 | |
| 634 | count = 0; |
| 635 | |
| 636 | }; |
| 637 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 638 | var count =0; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 639 | |
| 640 | //String |
| 641 | BinaryXMLDecoder.prototype.decodeUString = function( |
| 642 | //InputStream |
| 643 | istream, |
| 644 | //int |
| 645 | byteLength) { |
| 646 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 647 | /* |
| 648 | console.log('COUNT IS '+count); |
| 649 | console.log('INPUT BYTELENGTH IS '+byteLength); |
| 650 | count++; |
| 651 | if(null == byteLength|| undefined == byteLength){ |
| 652 | console.log("!!!!"); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 653 | tv = this.decodeTypeAndVal(this.istream); |
| 654 | var valval ; |
| 655 | if(typeof tv.val() == 'string'){ |
| 656 | valval = (parseInt(tv.val())); |
| 657 | } |
| 658 | else |
| 659 | valval = (tv.val()); |
| 660 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 661 | if(LOG>4) console.log('valval is ' + valval); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 662 | byteLength= this.decodeUString(this.istream, valval); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 663 | |
| 664 | //if(LOG>4) console.log('byte Length found in type val is '+ byteLength.charCodeAt(0)); |
| 665 | byteLength = parseInt(byteLength); |
| 666 | |
| 667 | |
| 668 | //byteLength = byteLength.charCodeAt(0); |
| 669 | //if(LOG>4) console.log('byte Length found in type val is '+ byteLength); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 670 | } |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 671 | if(LOG>4)console.log('byteLength is '+byteLength); |
| 672 | if(LOG>4)console.log('type of byteLength is '+typeof byteLength); |
| 673 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 674 | stringBytes = this.decodeBlob(this.istream, byteLength); |
| 675 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 676 | //console.log('String bytes are '+ stringBytes); |
| 677 | //console.log('stringBytes); |
| 678 | |
| 679 | if(LOG>4)console.log('byteLength is '+byteLength); |
| 680 | if(LOG>4)console.log('this.offset is '+this.offset); |
| 681 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 682 | tempBuffer = this.istream.slice(this.offset, this.offset+byteLength); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 683 | if(LOG>4)console.log('TEMPBUFFER IS' + tempBuffer); |
| 684 | if(LOG>4)console.log( tempBuffer); |
| 685 | |
| 686 | if(LOG>4)console.log('ADDING to offset value' + byteLength); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 687 | this.offset+= byteLength; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 688 | //if(LOG>3)console.log('read the String' + tempBuffer.toString('ascii')); |
| 689 | //return tempBuffer.toString('ascii');// |
| 690 | |
| 691 | |
| 692 | //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(stringBytes) ) ; |
| 693 | //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(tempBuffer) ) ; |
| 694 | //if(LOG>3)console.log(DataUtils.getUTF8StringFromBytes(tempBuffer) ) ; |
| 695 | //return DataUtils.getUTF8StringFromBytes(tempBuffer); |
| 696 | |
| 697 | if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.toString(stringBytes) ) ; |
| 698 | if(LOG>3)console.log( 'TYPE OF STRING READ IS '+ typeof DataUtils.toString(stringBytes) ) ; |
| 699 | |
| 700 | return DataUtils.toString(stringBytes);*/ |
| 701 | |
| 702 | if(null == byteLength ){ |
| 703 | var tempStreamPosition = this.offset; |
| 704 | |
| 705 | //TypeAndVal |
| 706 | tv = this.decodeTypeAndVal(istream); |
| 707 | |
| 708 | if(LOG>3)console.log('TV is '+tv); |
| 709 | if(LOG>3)console.log(tv); |
| 710 | |
| 711 | if(LOG>3)console.log('Type of TV is '+typeof tv); |
| 712 | |
| 713 | if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null |
| 714 | //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST)) |
| 715 | //Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob."); |
| 716 | |
| 717 | this.offset = tempStreamPosition; |
| 718 | |
| 719 | return ""; |
| 720 | } |
| 721 | |
| 722 | return this.decodeUString(istream, tv.val()); |
| 723 | } |
| 724 | else{ |
| 725 | //byte [] |
| 726 | stringBytes = this.decodeBlob(istream, byteLength); |
| 727 | |
| 728 | //return DataUtils.getUTF8StringFromBytes(stringBytes); |
| 729 | return DataUtils.toString(stringBytes); |
| 730 | |
| 731 | } |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 735 | |
| 736 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 737 | //OBject containg a pair of type and value |
| 738 | var TypeAndVal = function TypeAndVal(_type,_val) { |
| 739 | this.t = _type; |
| 740 | this.v = _val; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 741 | }; |
| 742 | |
| 743 | TypeAndVal.prototype.type = function(){ |
| 744 | return this.t; |
| 745 | }; |
| 746 | |
| 747 | TypeAndVal.prototype.val = function(){ |
| 748 | return this.v; |
| 749 | }; |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 750 | //TODO |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | BinaryXMLDecoder.prototype.readIntegerElement =function( |
| 758 | //String |
| 759 | startTag) { |
| 760 | |
| 761 | //String |
| 762 | if(LOG>4) console.log('READING INTEGER '+ startTag); |
| 763 | if(LOG>4) console.log('TYPE OF '+ typeof startTag); |
| 764 | |
| 765 | //try { |
| 766 | |
| 767 | strVal = this.readUTF8Element(startTag); |
| 768 | |
| 769 | //} |
| 770 | //catch (e) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 771 | //throw new Error("Cannot parse " + startTag + ": " + strVal); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 772 | //} |
| 773 | |
| 774 | return parseInt(strVal); |
| 775 | }; |
| 776 | |
| 777 | |
| 778 | BinaryXMLDecoder.prototype.readUTF8Element =function( |
| 779 | //String |
| 780 | startTag, |
| 781 | //TreeMap<String, String> |
| 782 | attributes) { |
| 783 | //throws ContentDecodingException |
| 784 | |
| 785 | this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes |
| 786 | //String |
| 787 | strElementText = this.readUString(); |
| 788 | return strElementText; |
| 789 | }; |