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