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