blob: 461eae78596e17dabc5fe02e1237ff0eafc9b814 [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 *
4 * @author: ucla-cs
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
480 /*int*/next;
481 /*int*/type = -1;
482 /*long*/val = 0;
483 /*boolean*/more = true;
484
485
486 //var savedOffset = this.offset;
487 var count = 0;
488
489 do {
490
491 var next = this.istream[this.offset ];
492
493
494 if (next < 0) {
495 return null;
496 }
497
498 if ((0 == next) && (0 == val)) {
499 return null;
500 }
501
502 more = (0 == (next & XML_TT_NO_MORE));
503
504 if (more) {
505 val = val << XML_REG_VAL_BITS;
506 val |= (next & XML_REG_VAL_MASK);
507 } else {
508
509 type = next & XML_TT_MASK;
510 val = val << XML_TT_VAL_BITS;
511 val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK);
512 }
513
514 this.offset++;
515
516 } while (more);
517
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700518 if(LOG>3)console.log('TYPE is '+ type + ' VAL is '+ val);
519
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700520 return new TypeAndVal(type, val);
521};
522
523
524
525//TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700526BinaryXMLDecoder.peekTypeAndVal = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700527 //TypeAndVal
528 tv = null;
529
Jeff Thompson1b196592012-10-14 17:07:40 -0700530 //this.istream.mark(LONG_BYTES*2);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700531
532 var previousOffset = this.offset;
533
534 try {
Jeff Thompson1b196592012-10-14 17:07:40 -0700535 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700536 } finally {
Jeff Thompson1b196592012-10-14 17:07:40 -0700537 //this.istream.reset();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700538 this.offset = previousOffset;
539 }
540
541 return tv;
542};
543
544
545//byte[]
546BinaryXMLDecoder.prototype.decodeBlob = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700547 //int
548 blobLength) {
549
550
551 if(null == blobLength){
552 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700553 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700554
555 var valval ;
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700556
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700557 if(typeof tv.val() == 'string'){
558 valval = (parseInt(tv.val()));
559 }
560 else
561 valval = (tv.val());
562
563 //console.log('valval here is ' + valval);
Jeff Thompson1b196592012-10-14 17:07:40 -0700564 return this.decodeBlob(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700565 }
566
567 //
568 //byte []
569
570 bytes = this.istream.slice(this.offset, this.offset+ blobLength);
571 this.offset += blobLength;
572
573 //int
574 return bytes;
575
576 count = 0;
577
578};
579
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700580var count =0;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700581
582//String
583BinaryXMLDecoder.prototype.decodeUString = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700584 //int
585 byteLength) {
586
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700587 /*
588 console.log('COUNT IS '+count);
589 console.log('INPUT BYTELENGTH IS '+byteLength);
590 count++;
591 if(null == byteLength|| undefined == byteLength){
592 console.log("!!!!");
Jeff Thompson1b196592012-10-14 17:07:40 -0700593 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700594 var valval ;
595 if(typeof tv.val() == 'string'){
596 valval = (parseInt(tv.val()));
597 }
598 else
599 valval = (tv.val());
600
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700601 if(LOG>4) console.log('valval is ' + valval);
Jeff Thompson1b196592012-10-14 17:07:40 -0700602 byteLength= this.decodeUString(valval);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700603
604 //if(LOG>4) console.log('byte Length found in type val is '+ byteLength.charCodeAt(0));
605 byteLength = parseInt(byteLength);
606
607
608 //byteLength = byteLength.charCodeAt(0);
609 //if(LOG>4) console.log('byte Length found in type val is '+ byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700610 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700611 if(LOG>4)console.log('byteLength is '+byteLength);
612 if(LOG>4)console.log('type of byteLength is '+typeof byteLength);
613
Jeff Thompson1b196592012-10-14 17:07:40 -0700614 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700615
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700616 //console.log('String bytes are '+ stringBytes);
617 //console.log('stringBytes);
618
619 if(LOG>4)console.log('byteLength is '+byteLength);
620 if(LOG>4)console.log('this.offset is '+this.offset);
621
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700622 tempBuffer = this.istream.slice(this.offset, this.offset+byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700623 if(LOG>4)console.log('TEMPBUFFER IS' + tempBuffer);
624 if(LOG>4)console.log( tempBuffer);
625
626 if(LOG>4)console.log('ADDING to offset value' + byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700627 this.offset+= byteLength;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700628 //if(LOG>3)console.log('read the String' + tempBuffer.toString('ascii'));
629 //return tempBuffer.toString('ascii');//
630
631
632 //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(stringBytes) ) ;
633 //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(tempBuffer) ) ;
634 //if(LOG>3)console.log(DataUtils.getUTF8StringFromBytes(tempBuffer) ) ;
635 //return DataUtils.getUTF8StringFromBytes(tempBuffer);
636
637 if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.toString(stringBytes) ) ;
638 if(LOG>3)console.log( 'TYPE OF STRING READ IS '+ typeof DataUtils.toString(stringBytes) ) ;
639
640 return DataUtils.toString(stringBytes);*/
641
642 if(null == byteLength ){
643 var tempStreamPosition = this.offset;
644
645 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700646 tv = this.decodeTypeAndVal();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700647
648 if(LOG>3)console.log('TV is '+tv);
649 if(LOG>3)console.log(tv);
650
651 if(LOG>3)console.log('Type of TV is '+typeof tv);
652
653 if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null
654 //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST))
655 //Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob.");
656
657 this.offset = tempStreamPosition;
658
659 return "";
660 }
661
Jeff Thompson1b196592012-10-14 17:07:40 -0700662 return this.decodeUString(tv.val());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700663 }
664 else{
665 //byte []
Jeff Thompson1b196592012-10-14 17:07:40 -0700666 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700667
668 //return DataUtils.getUTF8StringFromBytes(stringBytes);
669 return DataUtils.toString(stringBytes);
670
671 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700672};
673
674
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700675
676
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700677//OBject containg a pair of type and value
678var TypeAndVal = function TypeAndVal(_type,_val) {
679 this.t = _type;
680 this.v = _val;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700681};
682
683TypeAndVal.prototype.type = function(){
684 return this.t;
685};
686
687TypeAndVal.prototype.val = function(){
688 return this.v;
689};
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700690//TODO
691
692
693
694
695
696
697BinaryXMLDecoder.prototype.readIntegerElement =function(
698 //String
699 startTag) {
700
701 //String
702 if(LOG>4) console.log('READING INTEGER '+ startTag);
703 if(LOG>4) console.log('TYPE OF '+ typeof startTag);
704
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700705 strVal = this.readUTF8Element(startTag);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700706
707 return parseInt(strVal);
708};
709
710
711BinaryXMLDecoder.prototype.readUTF8Element =function(
712 //String
713 startTag,
714 //TreeMap<String, String>
715 attributes) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700716 //throws Error where name == "ContentDecodingException"
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700717
718 this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes
719 //String
720 strElementText = this.readUString();
721 return strElementText;
722};
Jeff Thompson1b196592012-10-14 17:07:40 -0700723
724/*
725 * Set the offset into the input, used for the next read.
726 */
727BinaryXMLDecoder.prototype.seek = function(
728 //int
729 offset) {
730 this.offset = offset;
731}
Jeff Thompsoncab74c32012-10-21 13:27:28 -0700732
733/*
734 * Call with: throw new ContentDecodingException(new Error("message")).
735 */
736function ContentDecodingException(error) {
737 this.message = error.message;
738 // Copy lineNumber, etc. from where new Error was called.
739 for (var prop in error)
740 this[prop] = error[prop];
741}
742ContentDecodingException.prototype = new Error();
743ContentDecodingException.prototype.name = "ContentDecodingException";
744
745