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