blob: 718f898b007ca5f157c56b9d5023960ac0e1d51c [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
Jeff Thompson1b196592012-10-14 17:07:40 -07002 * This class is used to decode ccnb binary elements (blob, type/value pairs).
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07003 *
Jeff Thompson146d7de2012-11-17 16:15:28 -08004 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07005 * See COPYING for copyright and distribution information.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07006 */
7
8var XML_EXT = 0x00;
9
10var XML_TAG = 0x01;
11
12var XML_DTAG = 0x02;
13
14var XML_ATTR = 0x03;
15
16var XML_DATTR = 0x04;
17
18var XML_BLOB = 0x05;
19
20var XML_UDATA = 0x06;
21
22var XML_CLOSE = 0x0;
23
24var XML_SUBTYPE_PROCESSING_INSTRUCTIONS = 16;
25
26
27var XML_TT_BITS = 3;
28var XML_TT_MASK = ((1 << XML_TT_BITS) - 1);
29var XML_TT_VAL_BITS = XML_TT_BITS + 1;
30var XML_TT_VAL_MASK = ((1 << (XML_TT_VAL_BITS)) - 1);
31var XML_REG_VAL_BITS = 7;
32var XML_REG_VAL_MASK = ((1 << XML_REG_VAL_BITS) - 1);
33var XML_TT_NO_MORE = (1 << XML_REG_VAL_BITS); // 0x80
34var BYTE_MASK = 0xFF;
35var LONG_BYTES = 8;
36var LONG_BITS = 64;
37
38var bits_11 = 0x0000007FF;
39var bits_18 = 0x00003FFFF;
40var bits_32 = 0x0FFFFFFFF;
41
42
43
44//returns a string
45tagToString = 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
55stringToTag = 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));
69var BinaryXMLDecoder = function BinaryXMLDecoder(istream){
70 var MARK_LEN=512;
71 var DEBUG_MAX_LEN = 32768;
72
73 this.istream = istream;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070074 this.offset = 0;
75};
76
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070077BinaryXMLDecoder.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 Thompson1b196592012-10-14 17:07:40 -070088 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070089
90 while ((null != nextTV) && ((XML_ATTR == nextTV.type()) ||
91 (XML_DATTR == nextTV.type()))) {
92
93 //this.TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -070094 thisTV = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070095
96 var attributeName = null;
97 if (XML_ATTR == thisTV.type()) {
98
Jeff Thompson1b196592012-10-14 17:07:40 -070099 attributeName = this.decodeUString(thisTV.val()+1);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700100
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 Thompsoncab74c32012-10-21 13:27:28 -0700105 throw new ContentDecodingException(new Error("Unknown DATTR value" + thisTV.val()));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700106 }
107 }
108
Jeff Thompson1b196592012-10-14 17:07:40 -0700109 var attributeValue = this.decodeUString();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700110
111 attributes.put(attributeName, attributeValue);
112
Jeff Thompson1b196592012-10-14 17:07:40 -0700113 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700114 }
115
116 } catch ( e) {
117
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700118 throw new ContentDecodingException(new Error("readStartElement", e));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700119 }
120};
121
122
123BinaryXMLDecoder.prototype.initializeDecoding = function() {
124 //if (!this.istream.markSupported()) {
125 //throw new IllegalArgumentException(this.getClass().getName() + ": input stream must support marking!");
126 //}
127}
128
129BinaryXMLDecoder.prototype.readStartDocument = function(){
130 // Currently no start document in binary encoding.
131 }
132
133BinaryXMLDecoder.prototype.readEndDocument = function() {
134 // Currently no end document in binary encoding.
135 };
136
137BinaryXMLDecoder.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 Cherkaouif441d3a2012-04-22 15:17:52 -0700148 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700149 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700150
151 if (null == tv) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700152 throw new ContentDecodingException(new Error("Expected start element: " + startTag + " got something not a tag."));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700153 }
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 Thompson1b196592012-10-14 17:07:40 -0700174 decodedTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700175
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 Thompsoncab74c32012-10-21 13:27:28 -0700190 throw new ContentDecodingException(new Error("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")"));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700191 }
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 Cherkaouif441d3a2012-04-22 15:17:52 -0700199 }
200
201
202BinaryXMLDecoder.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 Thompson1b196592012-10-14 17:07:40 -0700213 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700214
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 Thompson1b196592012-10-14 17:07:40 -0700220 thisTV = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700221
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 Thompson1b196592012-10-14 17:07:40 -0700233 attributeName = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700234
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 Thompsoncab74c32012-10-21 13:27:28 -0700239 throw new ContentDecodingException(new Error("Unknown DATTR value" + thisTV.val()));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700240 }
241 }
242 // Attribute values are always UDATA
243 //String
Jeff Thompson1b196592012-10-14 17:07:40 -0700244 attributeValue = this.decodeUString();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700245
246 //
247 attributes.push([attributeName, attributeValue]);
248
Jeff Thompson1b196592012-10-14 17:07:40 -0700249 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700250 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700251 } catch ( e) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700252 throw new ContentDecodingException(new Error("readStartElement", e));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700253 }
254};
255
256//returns a string
257BinaryXMLDecoder.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 Thompson1b196592012-10-14 17:07:40 -0700267 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700268
269 if (null != tv) {
270
271 if (tv.type() == XML_TAG) {
272 /*if (tv.val()+1 > DEBUG_MAX_LEN) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700273 throw new ContentDecodingException(new Error("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!")(;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700274 }*/
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 Thompson1b196592012-10-14 17:07:40 -0700284 decodedTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700285
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 Thompsoncab74c32012-10-21 13:27:28 -0700301 throw new ContentDecodingException(new Error("Cannot reset stream! " + e.getMessage(), e));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700302 }
303 }
304 return decodedTag;
305};
306
307BinaryXMLDecoder.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 Thompsoncab74c32012-10-21 13:27:28 -0700327 throw new ContentDecodingException(new Error("SHOULD BE STRING OR NUMBER"));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700328 }
329}
330//returns Long
331BinaryXMLDecoder.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 Thompson1b196592012-10-14 17:07:40 -0700343 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700344
345 if (null != tv) {
346
347 if (tv.type() == XML_TAG) {
348 if (tv.val()+1 > DEBUG_MAX_LEN) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700349 throw new ContentDecodingException(new Error("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!"));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700350 }
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 Thompson1b196592012-10-14 17:07:40 -0700361 strTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700362
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 Thompson34a2ec02012-09-29 21:47:05 -0700381 throw new Error("Cannot reset stream! " + e.getMessage(), e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700382 }
383 }
384 return decodedTag;
385 };
386
387
388// returns a byte[]
389BinaryXMLDecoder.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
406BinaryXMLDecoder.prototype.readEndElement = function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700407 if(LOG>4)console.log('this.offset is '+this.offset);
408
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700409 var next = this.istream[this.offset];
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700410
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700411 this.offset++;
412 //read();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700413
414 if(LOG>4)console.log('XML_CLOSE IS '+XML_CLOSE);
415 if(LOG>4)console.log('next is '+next);
416
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700417 if (next != XML_CLOSE) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700418 console.log("Expected end element, got: " + next);
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700419 throw new ContentDecodingException(new Error("Expected end element, got: " + next));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700420 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700421 };
422
423
424//String
425BinaryXMLDecoder.prototype.readUString = function(){
426 //String
Jeff Thompson1b196592012-10-14 17:07:40 -0700427 ustring = this.decodeUString();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700428 this.readEndElement();
429 return ustring;
430
431 };
432
433
434//returns a byte[]
435BinaryXMLDecoder.prototype.readBlob = function() {
436 //byte []
437
Jeff Thompson1b196592012-10-14 17:07:40 -0700438 blob = this.decodeBlob();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700439 this.readEndElement();
440 return blob;
441
442 };
443
444
445//CCNTime
446BinaryXMLDecoder.prototype.readDateTime = function(
447 //long
448 startTag) {
449 //byte []
450
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700451 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 Cherkaouif441d3a2012-04-22 15:17:52 -0700468 //CCNTime
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700469 timestamp = new CCNTime(lontimestamp);
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700470 //timestamp.setDateBinary(byteTimestamp);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700471
472 if (null == timestamp) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700473 throw new ContentDecodingException(new Error("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp)));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700474 }
475 return timestamp;
476};
477
Jeff Thompson1b196592012-10-14 17:07:40 -0700478BinaryXMLDecoder.prototype.decodeTypeAndVal = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700479
Jeff Thompsonf72d7ed2012-10-25 20:57:07 -0700480 /*int*/var type = -1;
481 /*long*/var val = 0;
482 /*boolean*/var more = true;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700483
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700484 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 Cherkaoui8f173612012-06-06 01:05:40 -0700513 if(LOG>3)console.log('TYPE is '+ type + ' VAL is '+ val);
514
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700515 return new TypeAndVal(type, val);
516};
517
518
519
520//TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700521BinaryXMLDecoder.peekTypeAndVal = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700522 //TypeAndVal
523 tv = null;
524
Jeff Thompson1b196592012-10-14 17:07:40 -0700525 //this.istream.mark(LONG_BYTES*2);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700526
527 var previousOffset = this.offset;
528
529 try {
Jeff Thompson1b196592012-10-14 17:07:40 -0700530 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700531 } finally {
Jeff Thompson1b196592012-10-14 17:07:40 -0700532 //this.istream.reset();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700533 this.offset = previousOffset;
534 }
535
536 return tv;
537};
538
539
Jeff Thompsonc92706b2012-11-11 19:12:58 -0800540//Uint8Array
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700541BinaryXMLDecoder.prototype.decodeBlob = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700542 //int
543 blobLength) {
544
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700545 if(null == blobLength){
546 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700547 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700548
549 var valval ;
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700550
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700551 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 Thompson1b196592012-10-14 17:07:40 -0700558 return this.decodeBlob(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700559 }
560
561 //
Jeff Thompsonc92706b2012-11-11 19:12:58 -0800562 //Uint8Array
563 var bytes = this.istream.subarray(this.offset, this.offset+ blobLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700564 this.offset += blobLength;
565
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700566 return bytes;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700567};
568
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700569var count =0;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700570
571//String
572BinaryXMLDecoder.prototype.decodeUString = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700573 //int
574 byteLength) {
575
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700576 /*
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 Thompson1b196592012-10-14 17:07:40 -0700582 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700583 var valval ;
584 if(typeof tv.val() == 'string'){
585 valval = (parseInt(tv.val()));
586 }
587 else
588 valval = (tv.val());
589
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700590 if(LOG>4) console.log('valval is ' + valval);
Jeff Thompson1b196592012-10-14 17:07:40 -0700591 byteLength= this.decodeUString(valval);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700592
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 Cherkaouif441d3a2012-04-22 15:17:52 -0700599 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700600 if(LOG>4)console.log('byteLength is '+byteLength);
601 if(LOG>4)console.log('type of byteLength is '+typeof byteLength);
602
Jeff Thompson1b196592012-10-14 17:07:40 -0700603 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700604
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700605 //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 Cherkaouif441d3a2012-04-22 15:17:52 -0700611 tempBuffer = this.istream.slice(this.offset, this.offset+byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700612 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 Cherkaouif441d3a2012-04-22 15:17:52 -0700616 this.offset+= byteLength;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700617 //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 Thompson1b196592012-10-14 17:07:40 -0700635 tv = this.decodeTypeAndVal();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700636
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 Thompson1b196592012-10-14 17:07:40 -0700651 return this.decodeUString(tv.val());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700652 }
653 else{
654 //byte []
Jeff Thompson1b196592012-10-14 17:07:40 -0700655 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700656
657 //return DataUtils.getUTF8StringFromBytes(stringBytes);
658 return DataUtils.toString(stringBytes);
659
660 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700661};
662
663
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700664
665
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700666//OBject containg a pair of type and value
667var TypeAndVal = function TypeAndVal(_type,_val) {
668 this.t = _type;
669 this.v = _val;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700670};
671
672TypeAndVal.prototype.type = function(){
673 return this.t;
674};
675
676TypeAndVal.prototype.val = function(){
677 return this.v;
678};
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700679
680
681
682
683BinaryXMLDecoder.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 Cherkaoui8f173612012-06-06 01:05:40 -0700691 strVal = this.readUTF8Element(startTag);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700692
693 return parseInt(strVal);
694};
695
696
697BinaryXMLDecoder.prototype.readUTF8Element =function(
698 //String
699 startTag,
700 //TreeMap<String, String>
701 attributes) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700702 //throws Error where name == "ContentDecodingException"
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700703
704 this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes
705 //String
706 strElementText = this.readUString();
707 return strElementText;
708};
Jeff Thompson1b196592012-10-14 17:07:40 -0700709
Jeff Thompsonc92706b2012-11-11 19:12:58 -0800710
Jeff Thompson1b196592012-10-14 17:07:40 -0700711/*
712 * Set the offset into the input, used for the next read.
713 */
714BinaryXMLDecoder.prototype.seek = function(
715 //int
716 offset) {
Jeff Thompsonc92706b2012-11-11 19:12:58 -0800717 this.offset = offset;
Jeff Thompson1b196592012-10-14 17:07:40 -0700718}
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700719
720/*
721 * Call with: throw new ContentDecodingException(new Error("message")).
722 */
723function 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}
729ContentDecodingException.prototype = new Error();
730ContentDecodingException.prototype.name = "ContentDecodingException";
731