blob: 609bd996d75d4a2005ab6fa981cd329cc18a165e [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) {
105 throw new ContentDecodingException("Unknown DATTR value" + thisTV.val());
106 }
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
118 throw new ContentDecodingException("readStartElement", e);
119 }
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
148 //try {
149 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700150 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700151
152 if (null == tv) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700153 throw new Error("Expected start element: " + startTag + " got something not a tag.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700154 }
155
156 //String
157 decodedTag = null;
158 //console.log(tv);
159 //console.log(typeof tv);
160
161 //console.log(XML_TAG);
162 if (tv.type() == XML_TAG) {
163 //console.log('got here');
164 //Log.info(Log.FAC_ENCODING, "Unexpected: got tag in readStartElement; looking for tag " + startTag + " got length: " + (int)tv.val()+1);
165 // Tag value represents length-1 as tags can never be empty.
166 var valval ;
167 if(typeof tv.val() == 'string'){
168 valval = (parseInt(tv.val())) + 1;
169 }
170 else
171 valval = (tv.val())+ 1;
172
173 //console.log('valval is ' +valval);
174
Jeff Thompson1b196592012-10-14 17:07:40 -0700175 decodedTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700176
177 } else if (tv.type() == XML_DTAG) {
178 //console.log('gothere');
179 //console.log(tv.val());
180 //decodedTag = tagToString(tv.val());
181 //console.log()
182 decodedTag = tv.val();
183 }
184
185 //console.log(decodedTag);
186 //console.log('startTag is '+startTag);
187
188
189 if ((null == decodedTag) || decodedTag != startTag ) {
190 console.log('expecting '+ startag + ' but got '+ decodedTag);
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700191 throw new Error("Expected start element: " + startTag + " got: " + decodedTag + "(" + tv.val() + ")");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700192 }
193
194 // DKS: does not read attributes out of stream if caller doesn't
195 // ask for them. Should possibly peek and skip over them regardless.
196 // TODO: fix this
197 if (null != attributes) {
198 readAttributes(attributes);
199 }
200
201 //} catch ( e) {
202 //console.log(e);
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700203 //throw new Error("readStartElement", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700204 //}
205 }
206
207
208BinaryXMLDecoder.prototype.readAttributes = function(
209 //TreeMap<String,String>
210 attributes) {
211
212 if (null == attributes) {
213 return;
214 }
215
216 try {
217 // Now need to get attributes.
218 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700219 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700220
221 while ((null != nextTV) && ((XML_ATTR == nextTV.type()) ||
222 (XML_DATTR == nextTV.type()))) {
223
224 // Decode this attribute. First, really read the type and value.
225 //this.TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700226 thisTV = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700227
228 //String
229 attributeName = null;
230 if (XML_ATTR == thisTV.type()) {
231 // Tag value represents length-1 as attribute names cannot be empty.
232 var valval ;
233 if(typeof tv.val() == 'string'){
234 valval = (parseInt(tv.val())) + 1;
235 }
236 else
237 valval = (tv.val())+ 1;
238
Jeff Thompson1b196592012-10-14 17:07:40 -0700239 attributeName = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700240
241 } else if (XML_DATTR == thisTV.type()) {
242 // DKS TODO are attributes same or different dictionary?
243 attributeName = tagToString(thisTV.val());
244 if (null == attributeName) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700245 throw new Error("Unknown DATTR value" + thisTV.val());
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700246 }
247 }
248 // Attribute values are always UDATA
249 //String
Jeff Thompson1b196592012-10-14 17:07:40 -0700250 attributeValue = this.decodeUString();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700251
252 //
253 attributes.push([attributeName, attributeValue]);
254
Jeff Thompson1b196592012-10-14 17:07:40 -0700255 nextTV = this.peekTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700256 }
257
258 } catch ( e) {
259 Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700260 throw new Error("readStartElement", e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700261 }
262};
263
264//returns a string
265BinaryXMLDecoder.prototype.peekStartElementAsString = function() {
266 //this.istream.mark(MARK_LEN);
267
268 //String
269 decodedTag = null;
270 var previousOffset = this.offset;
271 try {
272 // Have to distinguish genuine errors from wrong tags. Could either use
273 // a special exception subtype, or redo the work here.
274 //this.TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700275 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700276
277 if (null != tv) {
278
279 if (tv.type() == XML_TAG) {
280 /*if (tv.val()+1 > DEBUG_MAX_LEN) {
281 throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!");
282 }*/
283
284 // Tag value represents length-1 as tags can never be empty.
285 var valval ;
286 if(typeof tv.val() == 'string'){
287 valval = (parseInt(tv.val())) + 1;
288 }
289 else
290 valval = (tv.val())+ 1;
291
Jeff Thompson1b196592012-10-14 17:07:40 -0700292 decodedTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700293
294 //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag);
295
296 } else if (tv.type() == XML_DTAG) {
297 decodedTag = tagToString(tv.val());
298 }
299
300 } // else, not a type and val, probably an end element. rewind and return false.
301
302 } catch ( e) {
303
304 } finally {
305 try {
306 this.offset = previousOffset;
307 } catch ( e) {
308 Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
309 throw new ContentDecodingException("Cannot reset stream! " + e.getMessage(), e);
310 }
311 }
312 return decodedTag;
313};
314
315BinaryXMLDecoder.prototype.peekStartElement = function(
316 //String
317 startTag) {
318 //String
319 if(typeof startTag == 'string'){
320 decodedTag = this.peekStartElementAsString();
321
322 if ((null != decodedTag) && decodedTag == startTag) {
323 return true;
324 }
325 return false;
326 }
327 else if(typeof startTag == 'number'){
328 decodedTag = this.peekStartElementAsLong();
329 if ((null != decodedTag) && decodedTag == startTag) {
330 return true;
331 }
332 return false;
333 }
334 else{
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700335 throw new Error("SHOULD BE STRING OR NUMBER");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700336 }
337}
338//returns Long
339BinaryXMLDecoder.prototype.peekStartElementAsLong = function() {
340 //this.istream.mark(MARK_LEN);
341
342 //Long
343 decodedTag = null;
344
345 var previousOffset = this.offset;
346
347 try {
348 // Have to distinguish genuine errors from wrong tags. Could either use
349 // a special exception subtype, or redo the work here.
350 //this.TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700351 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700352
353 if (null != tv) {
354
355 if (tv.type() == XML_TAG) {
356 if (tv.val()+1 > DEBUG_MAX_LEN) {
357 throw new ContentDecodingException("Decoding error: length " + tv.val()+1 + " longer than expected maximum length!");
358 }
359
360 var valval ;
361 if(typeof tv.val() == 'string'){
362 valval = (parseInt(tv.val())) + 1;
363 }
364 else
365 valval = (tv.val())+ 1;
366
367 // Tag value represents length-1 as tags can never be empty.
368 //String
Jeff Thompson1b196592012-10-14 17:07:40 -0700369 strTag = this.decodeUString(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700370
371 decodedTag = stringToTag(strTag);
372
373 //Log.info(Log.FAC_ENCODING, "Unexpected: got text tag in peekStartElement; length: " + valval + " decoded tag = " + decodedTag);
374
375 } else if (tv.type() == XML_DTAG) {
376 decodedTag = tv.val();
377 }
378
379 } // else, not a type and val, probably an end element. rewind and return false.
380
381 } catch ( e) {
382
383 } finally {
384 try {
385 //this.istream.reset();
386 this.offset = previousOffset;
387 } catch ( e) {
388 Log.logStackTrace(Log.FAC_ENCODING, Level.WARNING, e);
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700389 throw new Error("Cannot reset stream! " + e.getMessage(), e);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700390 }
391 }
392 return decodedTag;
393 };
394
395
396// returns a byte[]
397BinaryXMLDecoder.prototype.readBinaryElement = function(
398 //long
399 startTag,
400 //TreeMap<String, String>
401 attributes){
402 //byte []
403 blob = null;
404
405 this.readStartElement(startTag, attributes);
406 blob = this.readBlob();
407
408
409 return blob;
410
411};
412
413
414BinaryXMLDecoder.prototype.readEndElement = function(){
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700415 //try {
416 if(LOG>4)console.log('this.offset is '+this.offset);
417
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700418 var next = this.istream[this.offset];
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700419
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700420 this.offset++;
421 //read();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700422
423 if(LOG>4)console.log('XML_CLOSE IS '+XML_CLOSE);
424 if(LOG>4)console.log('next is '+next);
425
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700426 if (next != XML_CLOSE) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700427 console.log("Expected end element, got: " + next);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700428 throw new ContentDecodingException("Expected end element, got: " + next);
429 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700430 //} catch ( e) {
431 //throw new ContentDecodingException(e);
432 //}
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700433 };
434
435
436//String
437BinaryXMLDecoder.prototype.readUString = function(){
438 //String
Jeff Thompson1b196592012-10-14 17:07:40 -0700439 ustring = this.decodeUString();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700440 this.readEndElement();
441 return ustring;
442
443 };
444
445
446//returns a byte[]
447BinaryXMLDecoder.prototype.readBlob = function() {
448 //byte []
449
Jeff Thompson1b196592012-10-14 17:07:40 -0700450 blob = this.decodeBlob();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700451 this.readEndElement();
452 return blob;
453
454 };
455
456
457//CCNTime
458BinaryXMLDecoder.prototype.readDateTime = function(
459 //long
460 startTag) {
461 //byte []
462
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700463 var byteTimestamp = this.readBinaryElement(startTag);
464
465 //var lontimestamp = DataUtils.byteArrayToUnsignedLong(byteTimestamp);
466
467 var byteTimestamp = DataUtils.toHex(byteTimestamp);
468
469
470 var byteTimestamp = parseInt(byteTimestamp, 16);
471
472 lontimestamp = (byteTimestamp/ 4096) * 1000;
473
474 //if(lontimestamp<0) lontimestamp = - lontimestamp;
475
476 if(LOG>3) console.log('DECODED DATE WITH VALUE');
477 if(LOG>3) console.log(lontimestamp);
478
479
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700480 //CCNTime
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700481 timestamp = new CCNTime(lontimestamp);
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700482 //timestamp.setDateBinary(byteTimestamp);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700483
484 if (null == timestamp) {
485 throw new ContentDecodingException("Cannot parse timestamp: " + DataUtils.printHexBytes(byteTimestamp));
486 }
487 return timestamp;
488};
489
Jeff Thompson1b196592012-10-14 17:07:40 -0700490BinaryXMLDecoder.prototype.decodeTypeAndVal = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700491
492 /*int*/next;
493 /*int*/type = -1;
494 /*long*/val = 0;
495 /*boolean*/more = true;
496
497
498 //var savedOffset = this.offset;
499 var count = 0;
500
501 do {
502
503 var next = this.istream[this.offset ];
504
505
506 if (next < 0) {
507 return null;
508 }
509
510 if ((0 == next) && (0 == val)) {
511 return null;
512 }
513
514 more = (0 == (next & XML_TT_NO_MORE));
515
516 if (more) {
517 val = val << XML_REG_VAL_BITS;
518 val |= (next & XML_REG_VAL_MASK);
519 } else {
520
521 type = next & XML_TT_MASK;
522 val = val << XML_TT_VAL_BITS;
523 val |= ((next >>> XML_TT_BITS) & XML_TT_VAL_MASK);
524 }
525
526 this.offset++;
527
528 } while (more);
529
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700530 if(LOG>3)console.log('TYPE is '+ type + ' VAL is '+ val);
531
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700532 return new TypeAndVal(type, val);
533};
534
535
536
537//TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700538BinaryXMLDecoder.peekTypeAndVal = function() {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700539 //TypeAndVal
540 tv = null;
541
Jeff Thompson1b196592012-10-14 17:07:40 -0700542 //this.istream.mark(LONG_BYTES*2);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700543
544 var previousOffset = this.offset;
545
546 try {
Jeff Thompson1b196592012-10-14 17:07:40 -0700547 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700548 } finally {
Jeff Thompson1b196592012-10-14 17:07:40 -0700549 //this.istream.reset();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700550 this.offset = previousOffset;
551 }
552
553 return tv;
554};
555
556
557//byte[]
558BinaryXMLDecoder.prototype.decodeBlob = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700559 //int
560 blobLength) {
561
562
563 if(null == blobLength){
564 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700565 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700566
567 var valval ;
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700568
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700569 if(typeof tv.val() == 'string'){
570 valval = (parseInt(tv.val()));
571 }
572 else
573 valval = (tv.val());
574
575 //console.log('valval here is ' + valval);
Jeff Thompson1b196592012-10-14 17:07:40 -0700576 return this.decodeBlob(valval);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700577 }
578
579 //
580 //byte []
581
582 bytes = this.istream.slice(this.offset, this.offset+ blobLength);
583 this.offset += blobLength;
584
585 //int
586 return bytes;
587
588 count = 0;
589
590};
591
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700592var count =0;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700593
594//String
595BinaryXMLDecoder.prototype.decodeUString = function(
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700596 //int
597 byteLength) {
598
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700599 /*
600 console.log('COUNT IS '+count);
601 console.log('INPUT BYTELENGTH IS '+byteLength);
602 count++;
603 if(null == byteLength|| undefined == byteLength){
604 console.log("!!!!");
Jeff Thompson1b196592012-10-14 17:07:40 -0700605 tv = this.decodeTypeAndVal();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700606 var valval ;
607 if(typeof tv.val() == 'string'){
608 valval = (parseInt(tv.val()));
609 }
610 else
611 valval = (tv.val());
612
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700613 if(LOG>4) console.log('valval is ' + valval);
Jeff Thompson1b196592012-10-14 17:07:40 -0700614 byteLength= this.decodeUString(valval);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700615
616 //if(LOG>4) console.log('byte Length found in type val is '+ byteLength.charCodeAt(0));
617 byteLength = parseInt(byteLength);
618
619
620 //byteLength = byteLength.charCodeAt(0);
621 //if(LOG>4) console.log('byte Length found in type val is '+ byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700622 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700623 if(LOG>4)console.log('byteLength is '+byteLength);
624 if(LOG>4)console.log('type of byteLength is '+typeof byteLength);
625
Jeff Thompson1b196592012-10-14 17:07:40 -0700626 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700627
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700628 //console.log('String bytes are '+ stringBytes);
629 //console.log('stringBytes);
630
631 if(LOG>4)console.log('byteLength is '+byteLength);
632 if(LOG>4)console.log('this.offset is '+this.offset);
633
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700634 tempBuffer = this.istream.slice(this.offset, this.offset+byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700635 if(LOG>4)console.log('TEMPBUFFER IS' + tempBuffer);
636 if(LOG>4)console.log( tempBuffer);
637
638 if(LOG>4)console.log('ADDING to offset value' + byteLength);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700639 this.offset+= byteLength;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700640 //if(LOG>3)console.log('read the String' + tempBuffer.toString('ascii'));
641 //return tempBuffer.toString('ascii');//
642
643
644 //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(stringBytes) ) ;
645 //if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.getUTF8StringFromBytes(tempBuffer) ) ;
646 //if(LOG>3)console.log(DataUtils.getUTF8StringFromBytes(tempBuffer) ) ;
647 //return DataUtils.getUTF8StringFromBytes(tempBuffer);
648
649 if(LOG>3)console.log( 'STRING READ IS '+ DataUtils.toString(stringBytes) ) ;
650 if(LOG>3)console.log( 'TYPE OF STRING READ IS '+ typeof DataUtils.toString(stringBytes) ) ;
651
652 return DataUtils.toString(stringBytes);*/
653
654 if(null == byteLength ){
655 var tempStreamPosition = this.offset;
656
657 //TypeAndVal
Jeff Thompson1b196592012-10-14 17:07:40 -0700658 tv = this.decodeTypeAndVal();
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700659
660 if(LOG>3)console.log('TV is '+tv);
661 if(LOG>3)console.log(tv);
662
663 if(LOG>3)console.log('Type of TV is '+typeof tv);
664
665 if ((null == tv) || (XML_UDATA != tv.type())) { // if we just have closers left, will get back null
666 //if (Log.isLoggable(Log.FAC_ENCODING, Level.FINEST))
667 //Log.finest(Log.FAC_ENCODING, "Expected UDATA, got " + ((null == tv) ? " not a tag " : tv.type()) + ", assuming elided 0-length blob.");
668
669 this.offset = tempStreamPosition;
670
671 return "";
672 }
673
Jeff Thompson1b196592012-10-14 17:07:40 -0700674 return this.decodeUString(tv.val());
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700675 }
676 else{
677 //byte []
Jeff Thompson1b196592012-10-14 17:07:40 -0700678 stringBytes = this.decodeBlob(byteLength);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700679
680 //return DataUtils.getUTF8StringFromBytes(stringBytes);
681 return DataUtils.toString(stringBytes);
682
683 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700684};
685
686
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700687
688
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700689//OBject containg a pair of type and value
690var TypeAndVal = function TypeAndVal(_type,_val) {
691 this.t = _type;
692 this.v = _val;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700693};
694
695TypeAndVal.prototype.type = function(){
696 return this.t;
697};
698
699TypeAndVal.prototype.val = function(){
700 return this.v;
701};
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700702//TODO
703
704
705
706
707
708
709BinaryXMLDecoder.prototype.readIntegerElement =function(
710 //String
711 startTag) {
712
713 //String
714 if(LOG>4) console.log('READING INTEGER '+ startTag);
715 if(LOG>4) console.log('TYPE OF '+ typeof startTag);
716
717 //try {
718
719 strVal = this.readUTF8Element(startTag);
720
721 //}
722 //catch (e) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -0700723 //throw new Error("Cannot parse " + startTag + ": " + strVal);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700724 //}
725
726 return parseInt(strVal);
727};
728
729
730BinaryXMLDecoder.prototype.readUTF8Element =function(
731 //String
732 startTag,
733 //TreeMap<String, String>
734 attributes) {
735 //throws ContentDecodingException
736
737 this.readStartElement(startTag, attributes); // can't use getElementText, can't get attributes
738 //String
739 strElementText = this.readUString();
740 return strElementText;
741};
Jeff Thompson1b196592012-10-14 17:07:40 -0700742
743/*
744 * Set the offset into the input, used for the next read.
745 */
746BinaryXMLDecoder.prototype.seek = function(
747 //int
748 offset) {
749 this.offset = offset;
750}