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