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