summaryrefslogtreecommitdiff
path: root/frontend/delta/js/Clipperz/ByteArray.js
Unidiff
Diffstat (limited to 'frontend/delta/js/Clipperz/ByteArray.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/Clipperz/ByteArray.js1459
1 files changed, 1459 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/ByteArray.js b/frontend/delta/js/Clipperz/ByteArray.js
new file mode 100644
index 0000000..22c7c6e
--- a/dev/null
+++ b/frontend/delta/js/Clipperz/ByteArray.js
@@ -0,0 +1,1459 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
25
26//=============================================================================
27
28Clipperz.ByteArray_abstract = function(args) {
29 return this;
30}
31
32Clipperz.ByteArray_abstract.prototype = MochiKit.Base.update(null, {
33
34 //-------------------------------------------------------------------------
35
36 'toString': function() {
37 return "Clipperz.ByteArray_abstract";
38 },
39
40 //-------------------------------------------------------------------------
41
42 'equals': function(aValue) {
43 return (this.compare(aValue) == 0);
44 },
45
46 //-------------------------------------------------------------------------
47
48 'compare': function(aValue) {
49 var result;
50 var i;
51
52 result = MochiKit.Base.compare(this.length(), aValue.length());
53 i = this.length();
54
55 while ((result == 0) && (i>0)) {
56 i--;
57 result = MochiKit.Base.compare(this.byteAtIndex(i), aValue.byteAtIndex(i));
58 }
59
60 return result;
61 },
62
63 //-------------------------------------------------------------------------
64
65 'clone': function() {
66 throw Clipperz.Base.exception.AbstractMethod;
67 },
68
69 //-------------------------------------------------------------------------
70
71 'newInstance': function() {
72 throw Clipperz.Base.exception.AbstractMethod;
73 },
74
75 //-------------------------------------------------------------------------
76
77 'reset': function() {
78 throw Clipperz.Base.exception.AbstractMethod;
79 },
80
81 //-------------------------------------------------------------------------
82
83 'length': function() {
84 throw Clipperz.Base.exception.AbstractMethod;
85 },
86
87 //-------------------------------------------------------------------------
88
89 'checkByteValue': function(aValue) {
90 if ((aValue & 0xff) != aValue) {
91 Clipperz.logError("Clipperz.ByteArray.appendByte: the provided value (0x" + aValue.toString(16) + ") is not a byte value.");
92 throw Clipperz.ByteArray.exception.InvalidValue;
93 }
94 },
95
96 //-------------------------------------------------------------------------
97
98 'xorMergeWithBlock': function(aBlock, anAllignment, paddingMode) {
99 var result;
100 var a, b;
101 var aLength;
102 var bLength;
103 var i, c;
104
105 if (this.length() > aBlock.length()) {
106 a = this;
107 b = aBlock;
108 } else {
109 a = aBlock;
110 b = this;
111 }
112
113 aLength = a.length();
114 bLength = b.length();
115
116 if (aLength != bLength) {
117 if (paddingMode == 'truncate') {
118 if (anAllignment == 'left') {
119 a = a.split(0, bLength);
120 } else {
121 a = a.split(aLength - bLength);
122 }
123 } else {
124 var ii, cc;
125 var padding;
126
127 // padding = new Clipperz.ByteArray();
128 padding = this.newInstance();
129 cc = aLength - bLength;
130 for (ii=0; ii<cc; ii++) {
131 padding.appendByte(0);
132 }
133
134 if (anAllignment == 'left') {
135 b = b.appendBlock(padding);
136 } else {
137 b = padding.appendBlock(b);
138 }
139 }
140 }
141
142 result = this.newInstance();
143 c = a.length();
144 for (i=0; i<c; i++) {
145 result.appendByte(a.byteAtIndex(i) ^ b.byteAtIndex(i));
146 }
147
148 return result;
149 },
150
151 //-------------------------------------------------------------------------
152
153 'appendBlock': function(aBlock) {
154 throw Clipperz.Base.exception.AbstractMethod;
155 },
156
157 //-------------------------------------------------------------------------
158
159 'appendByte': function(aValue) {
160 throw Clipperz.Base.exception.AbstractMethod;
161 },
162
163 'appendBytes': function(args) {
164 varvalues;
165 vari,c;
166
167 if (args.constructor == Array) {
168 values = args;
169 } else {
170 values = arguments;
171 }
172
173 c = values.length;
174 for (i=0; i<c; i++) {
175 this.appendByte(values[i]);
176 }
177
178 return this;
179 },
180
181 //-------------------------------------------------------------------------
182
183 'appendWord': function(aValue, isLittleEndian) {
184 var result;
185 var processAsLittleEndian;
186
187 processAsLittleEndian = isLittleEndian === true ? true : false;
188
189 if (processAsLittleEndian) {
190 result = this.appendBytes( (aValue) & 0xff, (aValue >> 8) & 0xff, (aValue >> 16) & 0xff, (aValue >> 24) & 0xff ); //little endian
191 } else {
192 result = this.appendBytes( (aValue >> 24) & 0xff, (aValue >> 16) & 0xff, (aValue >> 8) & 0xff, (aValue) & 0xff ); //big endian - DEFAULT
193 }
194
195 return result;
196 },
197
198 'appendWords': function(args) {
199 varvalues;
200 vari,c;
201
202 if (args.constructor == Array) {
203 values = args;
204 } else {
205 values = arguments;
206 }
207
208 c = values.length;
209 for (i=0; i<c; i++) {
210 this.appendWord(values[i], false);
211 }
212
213 return this;
214 },
215
216 //-------------------------------------------------------------------------
217
218 'appendBigEndianWords': function(args) {
219 varvalues;
220 vari,c;
221
222 if (args.constructor == Array) {
223 values = args;
224 } else {
225 values = arguments;
226 }
227
228 c = values.length;
229 for (i=0; i<c; i++) {
230 this.appendWord(values[i], true);
231 }
232
233 return this;
234 },
235
236 //-------------------------------------------------------------------------
237
238 'appendBinaryString': function (aBinaryString) {
239 var i,c;
240
241 c = aBinaryString.length;
242 for (i=0; i<c; i++) {
243 this.appendByte(aBinaryString.charCodeAt(i));
244 };
245
246 return this;
247 },
248
249 //-------------------------------------------------------------------------
250
251 'byteAtIndex': function(anIndex) {
252 throw Clipperz.Base.exception.AbstractMethod;
253 },
254
255 'setByteAtIndex': function(aValue, anIndex) {
256 throw Clipperz.Base.exception.AbstractMethod;
257 },
258
259 //-------------------------------------------------------------------------
260
261 'bitAtIndex': function(aBitPosition) {
262 var result;
263 varbytePosition;
264 var bitPositionInSelectedByte;
265 var selectedByte;
266 var selectedByteMask;
267
268 bytePosition = this.length() - Math.ceil((aBitPosition + 1)/ 8);
269 bitPositionInSelectedByte = aBitPosition % 8;
270 selectedByte = this.byteAtIndex(bytePosition);
271
272 if (bitPositionInSelectedByte > 0) {
273 selectedByteMask = (1 << bitPositionInSelectedByte);
274 } else {
275 selectedByteMask = 1;
276 }
277 result = selectedByte & selectedByteMask ? 1 : 0;
278
279 return result;
280 },
281
282 //-------------------------------------------------------------------------
283
284 'bitBlockAtIndexWithSize': function(aBitPosition, aSize) {
285 var result;
286 var bitValue;
287 var i,c;
288
289 result = 0;
290 c = aSize;
291 for (i=0; i<c; i++) {
292 bitValue = this.bitAtIndex(aBitPosition + i);
293 result = result | bitValue << i;
294 }
295
296 return result;
297 },
298
299 //-------------------------------------------------------------------------
300
301 'asString': function() {
302 varresult;
303 varlength;
304 vari;
305
306 result = [];
307
308 i = 0;
309 length = this.length();
310
311 while (i < length) {
312 varcurrentCharacter;
313 varcurrentByte;
314 varunicode;
315
316 currentByte = this.byteAtIndex(i);
317
318 if ((currentByte & 0x80) == 0x00 ) { //0xxxxxxx
319 unicode = currentByte;
320 currentCharacter = String.fromCharCode(unicode);
321 } else if ((currentByte & 0xe0) == 0xc0 ) { //110xxxxx 10xxxxxx
322 unicode = (currentByte & 0x1f) << 6;
323 i++; currentByte = this.byteAtIndex(i);
324 unicode = unicode | (currentByte & 0x3f);
325
326 currentCharacter = String.fromCharCode(unicode);
327 } else if ((currentByte & 0xf0) == 0xe0 ) { //1110xxxx 10xxxxxx 10xxxxxx
328 unicode = (currentByte & 0x0f) << (6+6);
329 i++; currentByte = this.byteAtIndex(i);
330 unicode = unicode | ((currentByte & 0x3f) << 6);
331 i++; currentByte = this.byteAtIndex(i);
332 unicode = unicode | (currentByte & 0x3f);
333
334 currentCharacter = String.fromCharCode(unicode);
335 } else { //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
336 unicode = (currentByte & 0x07) << (6+6+6);
337 i++; currentByte = this.byteAtIndex(i);
338 unicode = unicode | ((currentByte & 0x3f) << (6+6));
339 i++; currentByte = this.byteAtIndex(i);
340 unicode = unicode | ((currentByte & 0x3f) << 6);
341 i++; currentByte = this.byteAtIndex(i);
342 unicode = unicode | (currentByte & 0x3f);
343
344 currentCharacter = String.fromCharCode(unicode);
345 }
346
347 result.push(currentCharacter);
348 i++;
349 }
350
351 return result.join("");
352 },
353
354 //-------------------------------------------------------------------------
355
356 'toHexString': function() {
357 throw Clipperz.Base.exception.AbstractMethod;
358 },
359
360 //-------------------------------------------------------------------------
361
362 'base64map': "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
363 'base64mapIndex': "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(''),
364
365 //-------------------------------------------------------------------------
366
367 'appendBase64String': function(aValue) {
368 var i;
369 var length;
370
371 length = aValue.length;
372
373 if ((length % 4) != 0) {
374 Clipperz.logError("the value passed to the 'ByteArray.setBase64Value' is not correct");
375 throw Clipperz.ByteArray.exception.InvalidValue;
376 }
377
378 i = 0;
379 while (i<length) {
380 var value1, value2, value3, value4;
381 var byte1, byte2, byte3;
382
383 value1 = this.base64map.indexOf(aValue.charAt(i));
384 value2 = this.base64map.indexOf(aValue.charAt(i+1));
385 value3 = this.base64map.indexOf(aValue.charAt(i+2));
386 value4 = this.base64map.indexOf(aValue.charAt(i+3));
387
388 byte1 = (value1 << 2) | ((value2 & 0x30) >> 4);
389 if (value3 != -1) {
390 byte2 = ((value2 & 0x0f) << 4) | ((value3 & 0x3c) >> 2);
391
392 if (value4 != -1) {
393 byte3 = ((value3 & 0x03) << 6) | (value4);
394 } else {
395 byte3 = null;
396 }
397 } else {
398 byte2 = null;
399 byte3 = null;
400 }
401
402 this.appendByte(byte1);
403 this.appendByte(byte2);
404 this.appendByte(byte3);
405
406 i += 4;
407 }
408
409 return this;
410 },
411
412 //-------------------------------------------------------------------------
413
414 'toBase64String': function() {
415 var result;
416 var length;
417 var i;
418 var byte1, byte2, byte3;
419 var char1, char2, char3, char4;
420
421 i = 0;
422 length = this.length();
423 result = new Array(Math.ceil(length/3));
424
425 while (i < length) {
426 byte1 = this.byteAtIndex(i);
427 if ((i+2) < length) {
428 byte2 = this.byteAtIndex(i+1);
429 byte3 = this.byteAtIndex(i+2);
430 } else if ((i+2) == length) {
431 byte2 = this.byteAtIndex(i+1);
432 byte3 = null;
433 } else {
434 byte2 = null;
435 byte3 = null;
436 }
437
438 char1 = this.base64mapIndex[byte1 >> 2];
439 if (byte2 != null) {
440 char2 = this.base64mapIndex[((byte1 & 0x03) << 4) | ((byte2 & 0xf0) >> 4)];
441 if (byte3 != null) {
442 char3 = this.base64mapIndex[((byte2 & 0x0f) << 2) | ((byte3 & 0xc0) >> 6)];
443 char4 = this.base64mapIndex[(byte3 & 0x3f)];
444 } else {
445 char3 = this.base64mapIndex[(byte2 & 0x0f) << 2];
446 char4 = "=";
447 }
448 } else {
449 char2 = this.base64mapIndex[(byte1 & 0x03) << 4];
450 char3 = "=";
451 char4 = "=";
452 }
453
454 result.push(char1 + char2 + char3 + char4);
455
456 i += 3;
457 }
458
459 return result.join("");
460 },
461
462 //-------------------------------------------------------------------------
463
464 'base32map': "0123456789abcdefghjkmnpqrstvwxyz",
465 'base32mapIndex': "0123456789abcdefghjkmnpqrstvwxyz".split(''),
466
467 //-------------------------------------------------------------------------
468
469 'appendBase32String': function(aValue) {
470 var value;
471 var i;
472 var length;
473 var value1, value2, value3, value4, value5, value6, value7, value8;
474 var byte1, byte2, byte3, byte4, byte5;
475
476 value = aValue.toLowerCase();
477 value = value.replace(/[\s\-]/g, '');
478 value = value.replace(/[0o]/g, '0');
479 value = value.replace(/[1il]/g, '1');
480
481 length = value.length;
482
483 if ((length % 8) != 0) {
484 Clipperz.logError("the value passed to the 'ByteArray.setBase32Value' is not correct");
485 throw Clipperz.ByteArray.exception.InvalidValue;
486 }
487
488 i = 0;
489 while (i<length) {
490 value1 = this.base32map.indexOf(value.charAt(i));
491 value2 = this.base32map.indexOf(value.charAt(i+1));
492 value3 = this.base32map.indexOf(value.charAt(i+2));
493 value4 = this.base32map.indexOf(value.charAt(i+3));
494 value5 = this.base32map.indexOf(value.charAt(i+4));
495 value6 = this.base32map.indexOf(value.charAt(i+5));
496 value7 = this.base32map.indexOf(value.charAt(i+6));
497 value8 = this.base32map.indexOf(value.charAt(i+7));
498
499 byte1 = byte2 = byte3 = byte4 = byte5 = null;
500
501 byte1 = (value1 << 3) | ((value2 & 0x1c) >> 2);
502 if (value3 != -1) {
503 byte2 = ((value2 & 0x03) << 6) | (value3 << 1) | ((value4 & 0x10) >> 4);
504 if (value5 != -1) {
505 byte3 = ((value4 & 0x0f) << 4) | ((value5 & 0x1e) >> 1);
506 if (value6 != -1) {
507 byte4 = ((value5 & 0x01) << 7) | (value6 << 2) | ((value7 & 0x18) >> 3);
508 if (value8 != -1) {
509 byte5 = ((value7 & 0x07) << 5) | (value8);
510 }
511 }
512 }
513 }
514
515 this.appendByte(byte1);
516 this.appendByte(byte2);
517 this.appendByte(byte3);
518 this.appendByte(byte4);
519 this.appendByte(byte5);
520
521 i += 8;
522 }
523
524 return this;
525 },
526
527 //-------------------------------------------------------------------------
528
529 'toBase32String': function() {
530 var result;
531 var length;
532 var i;
533 var byte1, byte2, byte3, byte4, byte5;
534 var char1, char2, char3, char4, char5, char6, char7, char8;
535
536 i = 0;
537 length = this.length();
538 result = new Array(Math.ceil(length/5));
539
540 while (i < length) {
541 byte1 = this.byteAtIndex(i);
542
543 if ((i+4) < length) {
544 byte2 = this.byteAtIndex(i+1);
545 byte3 = this.byteAtIndex(i+2);
546 byte4 = this.byteAtIndex(i+3);
547 byte5 = this.byteAtIndex(i+4);
548 } else if ((i+4) == length) {
549 byte2 = this.byteAtIndex(i+1);
550 byte3 = this.byteAtIndex(i+2);
551 byte4 = this.byteAtIndex(i+3);
552 byte5 = null;
553 } else if ((i+3) == length) {
554 byte2 = this.byteAtIndex(i+1);
555 byte3 = this.byteAtIndex(i+2);
556 byte4 = null;
557 byte5 = null;
558 } else if ((i+2) == length) {
559 byte2 = this.byteAtIndex(i+1);
560 byte3 = null;
561 byte4 = null;
562 byte5 = null;
563 } else {
564 byte2 = null;
565 byte3 = null;
566 byte4 = null;
567 byte5 = null;
568 }
569
570
571 char1 = this.base32mapIndex[byte1 >> 3];
572 char2 = char3 = char4 = char5 = char6 = char7 = char8 = "=";
573
574 if (byte2 != null) {
575 char2 = this.base32mapIndex[((byte1 & 0x07) << 2) | ((byte2 & 0xc0) >> 6)];
576 char3 = this.base32mapIndex[((byte2 & 0x3e) >> 1)];
577 if (byte3 != null) {
578 char4 = this.base32mapIndex[((byte2 & 0x01) << 4) | ((byte3 & 0xf0) >> 4)];
579 if (byte4 != null) {
580 char5 = this.base32mapIndex[((byte3 & 0x0f) << 1) | ((byte4 & 0x80) >> 7)];
581 char6 = this.base32mapIndex[(byte4 & 0x7c) >> 2];
582 if (byte5 != null) {
583 char7 = this.base32mapIndex[((byte4 & 0x03) << 3) | ((byte5 & 0xe0) >> 5)];
584 char8 = this.base32mapIndex[(byte5 & 0x1f)];
585 } else {
586 char7 = this.base32mapIndex[(byte4 & 0x03) << 3];
587 }
588 } else {
589 char5 = this.base32mapIndex[(byte3 & 0x0f) << 1];
590 }
591
592 } else {
593 char4 = this.base32mapIndex[(byte2 & 0x01) << 4];
594 }
595 } else {
596 char2 = this.base32mapIndex[(byte1 & 0x07) << 2];
597 }
598
599 result.push(char1 + char2 + char3 + char4 + char5 + char6 + char7 + char8);
600 i += 5;
601 }
602
603 return result.join("");
604 },
605
606 //-------------------------------------------------------------------------
607
608 'toBinaryString': function () {
609 vari, c;
610 var result;
611
612 result = '';
613
614 c = this.length();
615 for (i=0; i<c; i++) {
616 result += String.fromCharCode(this.byteAtIndex(i));
617 }
618
619 return result;
620 },
621
622
623 //-------------------------------------------------------------------------
624
625 'split': function(aStartingIndex, anEndingIndex) {
626 throw Clipperz.Base.exception.AbstractMethod;
627 },
628
629 //-------------------------------------------------------------------------
630
631 'increment': function() {
632 var i;
633 var done;
634
635 done = false;
636 i = this.length() - 1;
637
638 while ((i>=0) && (done == false)) {
639 var currentByteValue;
640
641 currentByteValue = this.byteAtIndex(i);
642
643 if (currentByteValue == 0xff) {
644 this.setByteAtIndex(0, i);
645 if (i>= 0) {
646 i --;
647 } else {
648 done = true;
649 }
650 } else {
651 this.setByteAtIndex(currentByteValue + 1, i);
652 done = true;
653 }
654 }
655 },
656
657 //-------------------------------------------------------------------------
658
659 'arrayValues': function() {
660 throw Clipperz.Base.exception.AbstractMethod;
661 },
662
663 //-------------------------------------------------------------------------
664 __syntaxFix__: "syntax fix"
665
666});
667
668//=============================================================================
669//
670 //Clipperz.ByteArray_hex
671//
672//=============================================================================
673/*
674Clipperz.ByteArray_hex = function (args) {
675 this._value = "";
676
677 if (typeof(args) != 'undefined') {
678 if (args.constructor == Array) {
679 this.appendBytes(args);
680 } else if (args.constructor == String) {
681 if (args.indexOf("0x") == 0) {
682 varvalue;
683
684 value = args.substring(2).toLowerCase();
685 if (/[0123456789abcdef]* /.test(value)) { the space in the regexp shoud be removed if the code is activate
686 if ((value.length % 2) == 0) {
687 this._value = value;
688 } else {
689 this._value = "0" + value;
690 }
691 } else {
692Clipperz.logError("Clipperz.ByteArray should be inizialized with an hex string.");
693 throw Clipperz.ByteArray.exception.InvalidValue;
694 }
695 } else {
696 varvalue;
697 vari,c;
698
699 c = args.length;
700 value = new Array(c);
701 for (i=0; i<c; i++) {
702 value.push(Clipperz.ByteArray.unicodeToUtf8HexString(args.charCodeAt(i)));
703 }
704
705 this._value = value.join("");
706 }
707 } else {
708 this.appendBytes(MochiKit.Base.extend(null, arguments));
709 }
710 }
711 return this;
712}
713
714Clipperz.ByteArray_hex.prototype = MochiKit.Base.update(new Clipperz.ByteArray_abstract(), {
715
716 //-------------------------------------------------------------------------
717
718 'toString': function() {
719 return "Clipperz.ByteArray_hex";
720 },
721
722 //-------------------------------------------------------------------------
723
724 'clone': function() {
725 var result;
726
727 result = this.newInstance();
728 result._value = this._value;
729
730 return result;
731 },
732
733 //-------------------------------------------------------------------------
734
735 'newInstance': function() {
736 return new Clipperz.ByteArray_hex();
737 },
738
739 //-------------------------------------------------------------------------
740
741 'reset': function() {
742 this._value = "";
743 },
744
745 //-------------------------------------------------------------------------
746
747 'length': function() {
748 return (this._value.length / 2);
749 },
750
751 //-------------------------------------------------------------------------
752
753 'appendBlock': function(aBlock) {
754 this._value = this._value += aBlock.toHexString().substring(2);
755
756 return this;
757 },
758
759 //-------------------------------------------------------------------------
760
761 'appendByte': function(aValue) {
762 if (aValue != null) {
763 this.checkByteValue(aValue);
764 this._value += Clipperz.ByteArray.byteToHex(aValue);
765 }
766
767 return this;
768 },
769
770 //-------------------------------------------------------------------------
771
772 'byteAtIndex': function(anIndex) {
773 return parseInt(this._value.substr(anIndex*2, 2), 16);
774 },
775
776 'setByteAtIndex': function(aValue, anIndex) {
777 varmissingBytes;
778
779 this.checkByteValue(aValue);
780
781 missingBytes = anIndex - this.length();
782
783 if (missingBytes < 0) {
784 varcurrentValue;
785 varfirstCutIndex;
786 var secondCutIndex;
787
788 firstCutIndex = anIndex * 2;
789 secondCutIndex = firstCutIndex + 2;
790 currentValue = this._value;
791 this._value =currentValue.substring(0, firstCutIndex) +
792 Clipperz.ByteArray.byteToHex(aValue) +
793 currentValue.substring(secondCutIndex);
794 } else if (missingBytes == 0) {
795 this.appendByte(aValue);
796 } else {
797 var i,c;
798
799 c = missingBytes;
800 for (i=0; i<c; i++) {
801 this.appendByte(0);
802 }
803
804 this.appendByte(aValue);
805 }
806 },
807
808 //-------------------------------------------------------------------------
809
810 'toHexString': function() {
811 return "0x" + this._value;
812 },
813
814 //-------------------------------------------------------------------------
815
816 'split': function(aStartingIndex, anEndingIndex) {
817 var result;
818 varstartingIndex;
819 var endingIndex;
820
821 result = this.newInstance();
822
823 startingIndex = aStartingIndex * 2;
824 if (typeof(anEndingIndex) != 'undefined') {
825 endingIndex = anEndingIndex * 2;
826 result._value = this._value.substring(startingIndex, endingIndex);
827 } else {
828 result._value = this._value.substring(startingIndex);
829 }
830
831 return result;
832 },
833
834 //-------------------------------------------------------------------------
835
836 'arrayValues': function() {
837 var result;
838 var i,c;
839
840 c = this.length();
841
842 result = new Array(c);
843 for (i=0; i<c; i++) {
844 result[i] = this.byteAtIndex(i);
845 }
846
847 return result;
848 },
849
850 //-------------------------------------------------------------------------
851 __syntaxFix__: "syntax fix"
852});
853*/
854
855//=============================================================================
856//
857 //Clipperz.ByteArray_array
858//
859//=============================================================================
860
861Clipperz.ByteArray_array = function (args) {
862 if (typeof(args) != 'undefined') {
863 if (args.constructor == Array) {
864 this._value = args.slice(0);
865 } else if (args.constructor == String) {
866 var result;
867 varvalue;
868 var i, c;
869
870 if (args.indexOf("0x") == 0) {
871
872 value = args.substring(2).toLowerCase();
873 if (/[0123456789abcdef]*/.test(value)) {
874 if ((value.length % 2) != 0) {
875 value = "0" + value;
876 }
877 } else {
878 Clipperz.logError("Clipperz.ByteArray should be inizialized with an hex string.");
879 throw Clipperz.ByteArray.exception.InvalidValue;
880 }
881
882 c = value.length / 2
883 result = new Array(c);
884 for (i=0; i<c; i++) {
885 result[i] = parseInt(value.substr(i*2, 2), 16);
886 }
887
888 } else {
889 var unicode;
890 result = [];
891 c = args.length;
892 for (i=0; i<c; i++) {
893 // Clipperz.ByteArray.pushUtf8BytesOfUnicodeChar(result, args.charCodeAt(i));
894
895 unicode = args.charCodeAt(i);
896 if (unicode <= 0x7f) { //0x00000000 - 0x0000007f -> 0xxxxxxx
897 result.push(unicode);
898 // } else if ((unicode >= 0x80) && (unicode <= 0x7ff)) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
899 } else if (unicode <= 0x7ff) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
900 result.push((unicode >> 6) | 0xc0);
901 result.push((unicode & 0x3F) | 0x80);
902 // } else if ((unicode >= 0x0800) && (unicode <= 0xffff)) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
903 } else if (unicode <= 0xffff) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
904 result.push((unicode >> 12) | 0xe0);
905 result.push(((unicode >> 6) & 0x3f) | 0x80);
906 result.push((unicode & 0x3f) | 0x80);
907 } else { //0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
908 result.push((unicode >> 18) | 0xf0);
909 result.push(((unicode >> 12) & 0x3f) | 0x80);
910 result.push(((unicode >> 6) & 0x3f) | 0x80);
911 result.push((unicode & 0x3f) | 0x80);
912 }
913 }
914 }
915
916
917 this._value = result;
918 } else {
919 this._value = [];
920 this.appendBytes(MochiKit.Base.extend(null, arguments));
921 }
922 } else {
923 this._value = [];
924 }
925
926 return this;
927}
928
929Clipperz.ByteArray_array.prototype = MochiKit.Base.update(new Clipperz.ByteArray_abstract(), {
930
931 //-------------------------------------------------------------------------
932
933 'toString': function() {
934 return "Clipperz.ByteArray_array";
935 },
936
937 //-------------------------------------------------------------------------
938
939 'clone': function() {
940 var result;
941
942 result = this.newInstance();
943 result.appendBytes(this._value);
944
945 return result;
946 },
947
948 //-------------------------------------------------------------------------
949
950 'newInstance': function() {
951 return new Clipperz.ByteArray_array();
952 },
953
954 //-------------------------------------------------------------------------
955
956 'reset': function() {
957 this._value = [];
958 },
959
960 //-------------------------------------------------------------------------
961
962 'length': function() {
963 return (this._value.length);
964 },
965
966 //-------------------------------------------------------------------------
967
968 'appendBlock': function(aBlock) {
969 MochiKit.Base.extend(this._value, aBlock._value);
970
971 return this;
972 },
973
974 //-------------------------------------------------------------------------
975
976 'appendByte': function(aValue) {
977 if (aValue != null) {
978 this.checkByteValue(aValue);
979 this._value.push(aValue);
980 }
981
982 return this;
983 },
984
985 //-------------------------------------------------------------------------
986
987 'byteAtIndex': function(anIndex) {
988 return this._value[anIndex];
989 },
990
991 'setByteAtIndex': function(aValue, anIndex) {
992 varmissingBytes;
993
994 this.checkByteValue(aValue);
995
996 missingBytes = anIndex - this.length();
997
998 if (missingBytes < 0) {
999 this._value[anIndex] = aValue;
1000 } else if (missingBytes == 0) {
1001 this._value.push(aValue);
1002 } else {
1003 var i,c;
1004
1005 c = missingBytes;
1006 for (i=0; i<c; i++) {
1007 this._value.push(0);
1008 }
1009
1010 this._value.push(aValue);
1011 }
1012 },
1013
1014 //-------------------------------------------------------------------------
1015
1016 'toHexString': function() {
1017 var result;
1018 var i, c;
1019
1020 result = "0x";
1021 c = this.length();
1022 for (i=0; i<c; i++) {
1023 result += Clipperz.ByteArray.byteToHex(this._value[i]);
1024 }
1025
1026 return result;
1027 },
1028
1029 //-------------------------------------------------------------------------
1030
1031 'split': function(aStartingIndex, anEndingIndex) {
1032 var result;
1033
1034 result = this.newInstance();
1035 result._value = this._value.slice(aStartingIndex, anEndingIndex ? anEndingIndex : this.length());
1036
1037 return result;
1038 },
1039
1040 //-------------------------------------------------------------------------
1041
1042 'arrayValues': function() {
1043 return this._value.slice(0);
1044 },
1045
1046 //-------------------------------------------------------------------------
1047 __syntaxFix__: "syntax fix"
1048});
1049
1050
1051
1052
1053
1054//=============================================================================
1055//
1056 //Clipperz.ByteArray_string
1057//
1058//=============================================================================
1059/*
1060Clipperz.ByteArray_string = function (args) {
1061 this._value = "";
1062
1063 if (typeof(args) != 'undefined') {
1064 if (args.constructor == Array) {
1065 this.appendBytes(args);
1066 } else if (args.constructor == String) {
1067 var result;
1068 varvalue;
1069 var i, c;
1070
1071 if (args.indexOf("0x") == 0) {
1072
1073 value = args.substring(2).toLowerCase();
1074 if (/[0123456789abcdef]* /.test(value)) { the space in the regexp shoud be removed if the code is activated
1075 if ((value.length % 2) != 0) {
1076 value = "0" + value;
1077 }
1078 } else {
1079Clipperz.logError("Clipperz.ByteArray should be inizialized with an hex string.");
1080 throw Clipperz.ByteArray.exception.InvalidValue;
1081 }
1082 } else {
1083 value = "";
1084 c = args.length;
1085 for (i=0; i<c; i++) {
1086 value += Clipperz.ByteArray.unicodeToUtf8HexString(args.charCodeAt(i));
1087 }
1088 }
1089
1090 c = value.length / 2
1091 for (i=0; i<c; i++) {
1092 this.appendByte(parseInt(value.substr(i*2, 2), 16));
1093 }
1094 } else {
1095 this.appendBytes(MochiKit.Base.extend(null, arguments));
1096 }
1097 }
1098
1099 return this;
1100}
1101
1102Clipperz.ByteArray_string.prototype = MochiKit.Base.update(new Clipperz.ByteArray_abstract(), {
1103
1104 //-------------------------------------------------------------------------
1105
1106 'toString': function() {
1107 return "Clipperz.ByteArray_string";
1108 },
1109
1110 //-------------------------------------------------------------------------
1111
1112 'clone': function() {
1113 var result;
1114
1115 result = this.newInstance();
1116 result._value = this._value;
1117
1118 return result;
1119 },
1120
1121 //-------------------------------------------------------------------------
1122
1123 'newInstance': function() {
1124 return new Clipperz.ByteArray_string();
1125 },
1126
1127 //-------------------------------------------------------------------------
1128
1129 'reset': function() {
1130 this._value = "";
1131 },
1132
1133 //-------------------------------------------------------------------------
1134
1135 'length': function() {
1136 return (this._value.length);
1137 },
1138
1139 //-------------------------------------------------------------------------
1140
1141 'appendBlock': function(aBlock) {
1142 this._value += aBlock._value;
1143
1144 return this;
1145 },
1146
1147 //-------------------------------------------------------------------------
1148
1149 'appendByte': function(aValue) {
1150 if (aValue != null) {
1151 this.checkByteValue(aValue);
1152 this._value += String.fromCharCode(aValue);
1153 }
1154
1155 return this;
1156 },
1157
1158 //-------------------------------------------------------------------------
1159
1160 'byteAtIndex': function(anIndex) {
1161 return this._value.charCodeAt(anIndex);
1162 },
1163
1164 'setByteAtIndex': function(aValue, anIndex) {
1165 varmissingBytes;
1166
1167 this.checkByteValue(aValue);
1168
1169 missingBytes = anIndex - this.length();
1170
1171 if (missingBytes < 0) {
1172 this._value = this._value.substring(0, anIndex) + String.fromCharCode(aValue) + this._value.substring(anIndex + 1);
1173 } else if (missingBytes == 0) {
1174 this.appendByte(aValue);
1175 } else {
1176 var i,c;
1177
1178 c = missingBytes;
1179 for (i=0; i<c; i++) {
1180 this.appendByte(0);
1181 }
1182
1183 this.appendByte(aValue);
1184 }
1185 },
1186
1187 //-------------------------------------------------------------------------
1188
1189 'toHexString': function() {
1190 var result;
1191 var i, c;
1192
1193 result = "0x";
1194 c = this.length();
1195 for (i=0; i<c; i++) {
1196 result += Clipperz.ByteArray.byteToHex(this.byteAtIndex(i));
1197 }
1198
1199 return result;
1200 },
1201
1202 //-------------------------------------------------------------------------
1203
1204 'split': function(aStartingIndex, anEndingIndex) {
1205 var result;
1206 result = this.newInstance();
1207 result._value = this._value.substring(aStartingIndex, anEndingIndex ? anEndingIndex : this.length());
1208
1209 return result;
1210 },
1211
1212 //-------------------------------------------------------------------------
1213
1214 'arrayValues': function() {
1215 var result;
1216 var i,c;
1217
1218 c = this.length();
1219
1220 result = new Array(c);
1221 for (i=0; i<c; i++) {
1222 result[i] = this.byteAtIndex(i);
1223 }
1224
1225 return result;
1226 },
1227
1228 //-------------------------------------------------------------------------
1229 __syntaxFix__: "syntax fix"
1230});
1231*/
1232
1233//=============================================================================
1234//
1235 //Clipperz.ByteArray
1236//
1237//=============================================================================
1238
1239Clipperz.ByteArray = Clipperz.ByteArray_array;
1240//Clipperz.ByteArray = Clipperz.ByteArray_string;
1241//Clipperz.ByteArray = Clipperz.ByteArray_hex;
1242
1243//#############################################################################
1244
1245Clipperz.ByteArray.byteToHex = function(aByte) {
1246 return ((aByte < 16) ? "0" : "") + aByte.toString(16);
1247}
1248
1249
1250Clipperz.ByteArray.unicodeToUtf8HexString = function(aUnicode) {
1251 var result;
1252 varself;
1253
1254 self = Clipperz.ByteArray;
1255
1256 if (aUnicode <= 0x7f) { //0x00000000 - 0x0000007f -> 0xxxxxxx
1257 result = self.byteToHex(aUnicode);
1258 // } else if ((aUnicode >= 0x80) && (aUnicode <= 0x7ff)) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
1259 } else if (aUnicode <= 0x7ff) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
1260 result = self.byteToHex((aUnicode >> 6) | 0xc0);
1261 result += self.byteToHex((aUnicode & 0x3F) | 0x80);
1262 // } else if ((aUnicode >= 0x0800) && (aUnicode <= 0xffff)) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
1263 } else if (aUnicode <= 0xffff) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
1264 result = self.byteToHex((aUnicode >> 12) | 0xe0);
1265 result += self.byteToHex(((aUnicode >> 6) & 0x3f) | 0x80);
1266 result += self.byteToHex((aUnicode & 0x3f) | 0x80);
1267 } else { //0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1268 result = self.byteToHex((aUnicode >> 18) | 0xf0);
1269 result += self.byteToHex(((aUnicode >> 12) & 0x3f) | 0x80);
1270 result += self.byteToHex(((aUnicode >> 6) & 0x3f) | 0x80);
1271 result += self.byteToHex((aUnicode & 0x3f) | 0x80);
1272 }
1273
1274 return result;
1275}
1276
1277Clipperz.ByteArray.pushUtf8BytesOfUnicodeChar = function(anArray, aUnicode) {
1278 varself;
1279
1280 self = Clipperz.ByteArray;
1281
1282 if (aUnicode <= 0x7f) { //0x00000000 - 0x0000007f -> 0xxxxxxx
1283 anArray.push(aUnicode);
1284 // } else if ((aUnicode >= 0x80) && (aUnicode <= 0x7ff)) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
1285 } else if (aUnicode <= 0x7ff) { //0x00000080 - 0x000007ff -> 110xxxxx 10xxxxxx
1286 anArray.push((aUnicode >> 6) | 0xc0);
1287 anArray.push((aUnicode & 0x3F) | 0x80);
1288 // } else if ((aUnicode >= 0x0800) && (aUnicode <= 0xffff)) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
1289 } else if (aUnicode <= 0xffff) { //0x00000800 - 0x0000ffff -> 1110xxxx 10xxxxxx 10xxxxxx
1290 anArray.push((aUnicode >> 12) | 0xe0);
1291 anArray.push(((aUnicode >> 6) & 0x3f) | 0x80);
1292 anArray.push((aUnicode & 0x3f) | 0x80);
1293 } else { //0x00010000 - 0x001fffff -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1294 anArray.push((aUnicode >> 18) | 0xf0);
1295 anArray.push(((aUnicode >> 12) & 0x3f) | 0x80);
1296 anArray.push(((aUnicode >> 6) & 0x3f) | 0x80);
1297 anArray.push((aUnicode & 0x3f) | 0x80);
1298 }
1299}
1300
1301Clipperz.ByteArray.prefixMatchingBits = function (aValue, bValue) {
1302 varresult;
1303 var i,c;
1304
1305 result = 0;
1306
1307 c = Math.min(aValue.length(), bValue.length());
1308 i = 0;
1309 while (i<c && (aValue.byteAtIndex(i) == bValue.byteAtIndex(i))) {
1310 result += 8;
1311 i++;
1312 }
1313
1314 if (i<c) {
1315 varxorValue;
1316
1317 xorValue = (aValue.byteAtIndex(i) ^ bValue.byteAtIndex(i));
1318
1319 if (xorValue >= 128) {
1320 result += 0;
1321 } else if (xorValue >= 64) {
1322 result += 1;
1323 } else if (xorValue >= 32) {
1324 result += 2;
1325 } else if (xorValue >= 16) {
1326 result += 3;
1327 } else if (xorValue >= 8) {
1328 result += 4;
1329 } else if (xorValue >= 4) {
1330 result += 5;
1331 } else if (xorValue >= 2) {
1332 result += 6;
1333 } else if (xorValue >= 1) {
1334 result += 7;
1335 }
1336 }
1337
1338 return result;
1339};
1340
1341Clipperz.ByteArray.exception = {
1342 InvalidValue: new MochiKit.Base.NamedError("Clipperz.ByteArray.exception.InvalidValue")
1343};
1344
1345//#############################################################################
1346
1347Clipperz.ByteArrayIterator = function(args) {
1348 args = args || {};
1349
1350 this._byteArray = args.byteArray;
1351 this._blockSize = args.blockSize;
1352 this._finalPadding = args.finalPadding || false;
1353
1354 this._currentPosition = 0;
1355
1356 return this;
1357}
1358
1359Clipperz.ByteArrayIterator.prototype = MochiKit.Base.update(null, {
1360
1361 //-------------------------------------------------------------------------
1362
1363 'toString': function() {
1364 return "Clipperz.ByteArrayIterator";
1365 },
1366
1367 //-------------------------------------------------------------------------
1368
1369 'blockSize': function() {
1370 var result;
1371
1372 result = this._blockSize;
1373
1374 return result;
1375 },
1376
1377 //-------------------------------------------------------------------------
1378
1379 'currentPosition': function() {
1380 var result;
1381
1382 result = this._currentPosition;
1383
1384 return result;
1385 },
1386
1387 //-------------------------------------------------------------------------
1388
1389 'byteArray': function() {
1390 var result;
1391
1392 result = this._byteArray;
1393
1394 return result;
1395 },
1396
1397 //-------------------------------------------------------------------------
1398
1399 'finalPadding': function() {
1400 var result;
1401
1402 result = this._finalPadding;
1403
1404 return result;
1405 },
1406
1407 //-------------------------------------------------------------------------
1408
1409 'nextBlock': function() {
1410 var result;
1411 var currentPosition;
1412 varbyteArrayLength;
1413
1414 currentPosition = this._currentPosition;
1415 byteArrayLength = this.byteArray().length();
1416
1417 if (currentPosition < byteArrayLength) {
1418 var i,c;
1419
1420 c = this.blockSize();
1421 result = new Array(c);
1422 for (i=0; i<c; i++) {
1423 if (currentPosition < byteArrayLength) {
1424 result[i] = this.byteArray().byteAtIndex(currentPosition);
1425 currentPosition++;
1426 } else if (this.finalPadding() == true) {
1427 result[i] = 0;
1428 }
1429 }
1430
1431 this._currentPosition = currentPosition;
1432 } else {
1433 result = null;
1434 }
1435
1436 return result;
1437 },
1438
1439 //-------------------------------------------------------------------------
1440
1441 'nextBlockArray': function() {
1442 var result;
1443 var nextBlock;
1444
1445 nextBlock = this.nextBlock();
1446
1447 if (nextBlock != null) {
1448 result = new Clipperz.ByteArray(nextBlock);
1449 } else {
1450 result = null;
1451 }
1452
1453 return result;
1454 },
1455
1456 //-----------------------------------------------------------------------------
1457 __syntaxFix__: "syntax fix"
1458
1459});