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