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