1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var forge = require('./forge');
|
9 | var baseN = require('./baseN');
|
10 |
|
11 |
|
12 | var util = module.exports = forge.util = forge.util || {};
|
13 |
|
14 |
|
15 | (function() {
|
16 |
|
17 |
|
18 |
|
19 | if(typeof process !== 'undefined' && process.nextTick && !process.browser) {
|
20 | util.nextTick = process.nextTick;
|
21 | if(typeof setImmediate === 'function') {
|
22 | util.setImmediate = setImmediate;
|
23 | } else {
|
24 |
|
25 |
|
26 | util.setImmediate = util.nextTick;
|
27 | }
|
28 | return;
|
29 | }
|
30 |
|
31 |
|
32 | if(typeof setImmediate === 'function') {
|
33 | util.setImmediate = function() { return setImmediate.apply(undefined, arguments); };
|
34 | util.nextTick = function(callback) {
|
35 | return setImmediate(callback);
|
36 | };
|
37 | return;
|
38 | }
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | util.setImmediate = function(callback) {
|
47 | setTimeout(callback, 0);
|
48 | };
|
49 |
|
50 |
|
51 | if(typeof window !== 'undefined' &&
|
52 | typeof window.postMessage === 'function') {
|
53 | var msg = 'forge.setImmediate';
|
54 | var callbacks = [];
|
55 | util.setImmediate = function(callback) {
|
56 | callbacks.push(callback);
|
57 |
|
58 |
|
59 | if(callbacks.length === 1) {
|
60 | window.postMessage(msg, '*');
|
61 | }
|
62 | };
|
63 | function handler(event) {
|
64 | if(event.source === window && event.data === msg) {
|
65 | event.stopPropagation();
|
66 | var copy = callbacks.slice();
|
67 | callbacks.length = 0;
|
68 | copy.forEach(function(callback) {
|
69 | callback();
|
70 | });
|
71 | }
|
72 | }
|
73 | window.addEventListener('message', handler, true);
|
74 | }
|
75 |
|
76 |
|
77 | if(typeof MutationObserver !== 'undefined') {
|
78 |
|
79 | var now = Date.now();
|
80 | var attr = true;
|
81 | var div = document.createElement('div');
|
82 | var callbacks = [];
|
83 | new MutationObserver(function() {
|
84 | var copy = callbacks.slice();
|
85 | callbacks.length = 0;
|
86 | copy.forEach(function(callback) {
|
87 | callback();
|
88 | });
|
89 | }).observe(div, {attributes: true});
|
90 | var oldSetImmediate = util.setImmediate;
|
91 | util.setImmediate = function(callback) {
|
92 | if(Date.now() - now > 15) {
|
93 | now = Date.now();
|
94 | oldSetImmediate(callback);
|
95 | } else {
|
96 | callbacks.push(callback);
|
97 |
|
98 |
|
99 | if(callbacks.length === 1) {
|
100 | div.setAttribute('a', attr = !attr);
|
101 | }
|
102 | }
|
103 | };
|
104 | }
|
105 |
|
106 | util.nextTick = util.setImmediate;
|
107 | })();
|
108 |
|
109 |
|
110 | util.isNodejs =
|
111 | typeof process !== 'undefined' && process.versions && process.versions.node;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | util.globalScope = (function() {
|
119 | if(util.isNodejs) {
|
120 | return global;
|
121 | }
|
122 |
|
123 | return typeof self === 'undefined' ? window : self;
|
124 | })();
|
125 |
|
126 |
|
127 | util.isArray = Array.isArray || function(x) {
|
128 | return Object.prototype.toString.call(x) === '[object Array]';
|
129 | };
|
130 |
|
131 |
|
132 | util.isArrayBuffer = function(x) {
|
133 | return typeof ArrayBuffer !== 'undefined' && x instanceof ArrayBuffer;
|
134 | };
|
135 |
|
136 |
|
137 | util.isArrayBufferView = function(x) {
|
138 | return x && util.isArrayBuffer(x.buffer) && x.byteLength !== undefined;
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | function _checkBitsParam(n) {
|
151 | if(!(n === 8 || n === 16 || n === 24 || n === 32)) {
|
152 | throw new Error('Only 8, 16, 24, or 32 bits supported: ' + n);
|
153 | }
|
154 | }
|
155 |
|
156 |
|
157 | util.ByteBuffer = ByteStringBuffer;
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function ByteStringBuffer(b) {
|
168 |
|
169 |
|
170 |
|
171 | this.data = '';
|
172 |
|
173 | this.read = 0;
|
174 |
|
175 | if(typeof b === 'string') {
|
176 | this.data = b;
|
177 | } else if(util.isArrayBuffer(b) || util.isArrayBufferView(b)) {
|
178 | if(typeof Buffer !== 'undefined' && b instanceof Buffer) {
|
179 | this.data = b.toString('binary');
|
180 | } else {
|
181 |
|
182 |
|
183 | var arr = new Uint8Array(b);
|
184 | try {
|
185 | this.data = String.fromCharCode.apply(null, arr);
|
186 | } catch(e) {
|
187 | for(var i = 0; i < arr.length; ++i) {
|
188 | this.putByte(arr[i]);
|
189 | }
|
190 | }
|
191 | }
|
192 | } else if(b instanceof ByteStringBuffer ||
|
193 | (typeof b === 'object' && typeof b.data === 'string' &&
|
194 | typeof b.read === 'number')) {
|
195 |
|
196 | this.data = b.data;
|
197 | this.read = b.read;
|
198 | }
|
199 |
|
200 |
|
201 | this._constructedStringLength = 0;
|
202 | }
|
203 | util.ByteStringBuffer = ByteStringBuffer;
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | var _MAX_CONSTRUCTED_STRING_LENGTH = 4096;
|
216 | util.ByteStringBuffer.prototype._optimizeConstructedString = function(x) {
|
217 | this._constructedStringLength += x;
|
218 | if(this._constructedStringLength > _MAX_CONSTRUCTED_STRING_LENGTH) {
|
219 |
|
220 | this.data.substr(0, 1);
|
221 | this._constructedStringLength = 0;
|
222 | }
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | util.ByteStringBuffer.prototype.length = function() {
|
231 | return this.data.length - this.read;
|
232 | };
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | util.ByteStringBuffer.prototype.isEmpty = function() {
|
240 | return this.length() <= 0;
|
241 | };
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | util.ByteStringBuffer.prototype.putByte = function(b) {
|
251 | return this.putBytes(String.fromCharCode(b));
|
252 | };
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | util.ByteStringBuffer.prototype.fillWithByte = function(b, n) {
|
263 | b = String.fromCharCode(b);
|
264 | var d = this.data;
|
265 | while(n > 0) {
|
266 | if(n & 1) {
|
267 | d += b;
|
268 | }
|
269 | n >>>= 1;
|
270 | if(n > 0) {
|
271 | b += b;
|
272 | }
|
273 | }
|
274 | this.data = d;
|
275 | this._optimizeConstructedString(n);
|
276 | return this;
|
277 | };
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | util.ByteStringBuffer.prototype.putBytes = function(bytes) {
|
287 | this.data += bytes;
|
288 | this._optimizeConstructedString(bytes.length);
|
289 | return this;
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | util.ByteStringBuffer.prototype.putString = function(str) {
|
300 | return this.putBytes(util.encodeUtf8(str));
|
301 | };
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | util.ByteStringBuffer.prototype.putInt16 = function(i) {
|
311 | return this.putBytes(
|
312 | String.fromCharCode(i >> 8 & 0xFF) +
|
313 | String.fromCharCode(i & 0xFF));
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | util.ByteStringBuffer.prototype.putInt24 = function(i) {
|
324 | return this.putBytes(
|
325 | String.fromCharCode(i >> 16 & 0xFF) +
|
326 | String.fromCharCode(i >> 8 & 0xFF) +
|
327 | String.fromCharCode(i & 0xFF));
|
328 | };
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 | util.ByteStringBuffer.prototype.putInt32 = function(i) {
|
338 | return this.putBytes(
|
339 | String.fromCharCode(i >> 24 & 0xFF) +
|
340 | String.fromCharCode(i >> 16 & 0xFF) +
|
341 | String.fromCharCode(i >> 8 & 0xFF) +
|
342 | String.fromCharCode(i & 0xFF));
|
343 | };
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | util.ByteStringBuffer.prototype.putInt16Le = function(i) {
|
353 | return this.putBytes(
|
354 | String.fromCharCode(i & 0xFF) +
|
355 | String.fromCharCode(i >> 8 & 0xFF));
|
356 | };
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 | util.ByteStringBuffer.prototype.putInt24Le = function(i) {
|
366 | return this.putBytes(
|
367 | String.fromCharCode(i & 0xFF) +
|
368 | String.fromCharCode(i >> 8 & 0xFF) +
|
369 | String.fromCharCode(i >> 16 & 0xFF));
|
370 | };
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | util.ByteStringBuffer.prototype.putInt32Le = function(i) {
|
380 | return this.putBytes(
|
381 | String.fromCharCode(i & 0xFF) +
|
382 | String.fromCharCode(i >> 8 & 0xFF) +
|
383 | String.fromCharCode(i >> 16 & 0xFF) +
|
384 | String.fromCharCode(i >> 24 & 0xFF));
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | util.ByteStringBuffer.prototype.putInt = function(i, n) {
|
396 | _checkBitsParam(n);
|
397 | var bytes = '';
|
398 | do {
|
399 | n -= 8;
|
400 | bytes += String.fromCharCode((i >> n) & 0xFF);
|
401 | } while(n > 0);
|
402 | return this.putBytes(bytes);
|
403 | };
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 | util.ByteStringBuffer.prototype.putSignedInt = function(i, n) {
|
415 |
|
416 | if(i < 0) {
|
417 | i += 2 << (n - 1);
|
418 | }
|
419 | return this.putInt(i, n);
|
420 | };
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | util.ByteStringBuffer.prototype.putBuffer = function(buffer) {
|
430 | return this.putBytes(buffer.getBytes());
|
431 | };
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 | util.ByteStringBuffer.prototype.getByte = function() {
|
439 | return this.data.charCodeAt(this.read++);
|
440 | };
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | util.ByteStringBuffer.prototype.getInt16 = function() {
|
449 | var rval = (
|
450 | this.data.charCodeAt(this.read) << 8 ^
|
451 | this.data.charCodeAt(this.read + 1));
|
452 | this.read += 2;
|
453 | return rval;
|
454 | };
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 | util.ByteStringBuffer.prototype.getInt24 = function() {
|
463 | var rval = (
|
464 | this.data.charCodeAt(this.read) << 16 ^
|
465 | this.data.charCodeAt(this.read + 1) << 8 ^
|
466 | this.data.charCodeAt(this.read + 2));
|
467 | this.read += 3;
|
468 | return rval;
|
469 | };
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 | util.ByteStringBuffer.prototype.getInt32 = function() {
|
478 | var rval = (
|
479 | this.data.charCodeAt(this.read) << 24 ^
|
480 | this.data.charCodeAt(this.read + 1) << 16 ^
|
481 | this.data.charCodeAt(this.read + 2) << 8 ^
|
482 | this.data.charCodeAt(this.read + 3));
|
483 | this.read += 4;
|
484 | return rval;
|
485 | };
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | util.ByteStringBuffer.prototype.getInt16Le = function() {
|
494 | var rval = (
|
495 | this.data.charCodeAt(this.read) ^
|
496 | this.data.charCodeAt(this.read + 1) << 8);
|
497 | this.read += 2;
|
498 | return rval;
|
499 | };
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | util.ByteStringBuffer.prototype.getInt24Le = function() {
|
508 | var rval = (
|
509 | this.data.charCodeAt(this.read) ^
|
510 | this.data.charCodeAt(this.read + 1) << 8 ^
|
511 | this.data.charCodeAt(this.read + 2) << 16);
|
512 | this.read += 3;
|
513 | return rval;
|
514 | };
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 | util.ByteStringBuffer.prototype.getInt32Le = function() {
|
523 | var rval = (
|
524 | this.data.charCodeAt(this.read) ^
|
525 | this.data.charCodeAt(this.read + 1) << 8 ^
|
526 | this.data.charCodeAt(this.read + 2) << 16 ^
|
527 | this.data.charCodeAt(this.read + 3) << 24);
|
528 | this.read += 4;
|
529 | return rval;
|
530 | };
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | util.ByteStringBuffer.prototype.getInt = function(n) {
|
541 | _checkBitsParam(n);
|
542 | var rval = 0;
|
543 | do {
|
544 |
|
545 | rval = (rval << 8) + this.data.charCodeAt(this.read++);
|
546 | n -= 8;
|
547 | } while(n > 0);
|
548 | return rval;
|
549 | };
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 | util.ByteStringBuffer.prototype.getSignedInt = function(n) {
|
560 |
|
561 | var x = this.getInt(n);
|
562 | var max = 2 << (n - 2);
|
563 | if(x >= max) {
|
564 | x -= max << 1;
|
565 | }
|
566 | return x;
|
567 | };
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 | util.ByteStringBuffer.prototype.getBytes = function(count) {
|
579 | var rval;
|
580 | if(count) {
|
581 |
|
582 | count = Math.min(this.length(), count);
|
583 | rval = this.data.slice(this.read, this.read + count);
|
584 | this.read += count;
|
585 | } else if(count === 0) {
|
586 | rval = '';
|
587 | } else {
|
588 |
|
589 | rval = (this.read === 0) ? this.data : this.data.slice(this.read);
|
590 | this.clear();
|
591 | }
|
592 | return rval;
|
593 | };
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 |
|
601 |
|
602 |
|
603 | util.ByteStringBuffer.prototype.bytes = function(count) {
|
604 | return (typeof(count) === 'undefined' ?
|
605 | this.data.slice(this.read) :
|
606 | this.data.slice(this.read, this.read + count));
|
607 | };
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 | util.ByteStringBuffer.prototype.at = function(i) {
|
617 | return this.data.charCodeAt(this.read + i);
|
618 | };
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 |
|
627 |
|
628 | util.ByteStringBuffer.prototype.setAt = function(i, b) {
|
629 | this.data = this.data.substr(0, this.read + i) +
|
630 | String.fromCharCode(b) +
|
631 | this.data.substr(this.read + i + 1);
|
632 | return this;
|
633 | };
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 | util.ByteStringBuffer.prototype.last = function() {
|
641 | return this.data.charCodeAt(this.data.length - 1);
|
642 | };
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 | util.ByteStringBuffer.prototype.copy = function() {
|
650 | var c = util.createBuffer(this.data);
|
651 | c.read = this.read;
|
652 | return c;
|
653 | };
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 | util.ByteStringBuffer.prototype.compact = function() {
|
661 | if(this.read > 0) {
|
662 | this.data = this.data.slice(this.read);
|
663 | this.read = 0;
|
664 | }
|
665 | return this;
|
666 | };
|
667 |
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 | util.ByteStringBuffer.prototype.clear = function() {
|
674 | this.data = '';
|
675 | this.read = 0;
|
676 | return this;
|
677 | };
|
678 |
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 | util.ByteStringBuffer.prototype.truncate = function(count) {
|
687 | var len = Math.max(0, this.length() - count);
|
688 | this.data = this.data.substr(this.read, len);
|
689 | this.read = 0;
|
690 | return this;
|
691 | };
|
692 |
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 | util.ByteStringBuffer.prototype.toHex = function() {
|
699 | var rval = '';
|
700 | for(var i = this.read; i < this.data.length; ++i) {
|
701 | var b = this.data.charCodeAt(i);
|
702 | if(b < 16) {
|
703 | rval += '0';
|
704 | }
|
705 | rval += b.toString(16);
|
706 | }
|
707 | return rval;
|
708 | };
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 | util.ByteStringBuffer.prototype.toString = function() {
|
716 | return util.decodeUtf8(this.bytes());
|
717 | };
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 | function DataBuffer(b, options) {
|
752 |
|
753 | options = options || {};
|
754 |
|
755 |
|
756 | this.read = options.readOffset || 0;
|
757 | this.growSize = options.growSize || 1024;
|
758 |
|
759 | var isArrayBuffer = util.isArrayBuffer(b);
|
760 | var isArrayBufferView = util.isArrayBufferView(b);
|
761 | if(isArrayBuffer || isArrayBufferView) {
|
762 |
|
763 | if(isArrayBuffer) {
|
764 | this.data = new DataView(b);
|
765 | } else {
|
766 |
|
767 |
|
768 |
|
769 | this.data = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
770 | }
|
771 | this.write = ('writeOffset' in options ?
|
772 | options.writeOffset : this.data.byteLength);
|
773 | return;
|
774 | }
|
775 |
|
776 |
|
777 | this.data = new DataView(new ArrayBuffer(0));
|
778 | this.write = 0;
|
779 |
|
780 | if(b !== null && b !== undefined) {
|
781 | this.putBytes(b);
|
782 | }
|
783 |
|
784 | if('writeOffset' in options) {
|
785 | this.write = options.writeOffset;
|
786 | }
|
787 | }
|
788 | util.DataBuffer = DataBuffer;
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 | util.DataBuffer.prototype.length = function() {
|
796 | return this.write - this.read;
|
797 | };
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 | util.DataBuffer.prototype.isEmpty = function() {
|
805 | return this.length() <= 0;
|
806 | };
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | util.DataBuffer.prototype.accommodate = function(amount, growSize) {
|
820 | if(this.length() >= amount) {
|
821 | return this;
|
822 | }
|
823 | growSize = Math.max(growSize || this.growSize, amount);
|
824 |
|
825 |
|
826 | var src = new Uint8Array(
|
827 | this.data.buffer, this.data.byteOffset, this.data.byteLength);
|
828 | var dst = new Uint8Array(this.length() + growSize);
|
829 | dst.set(src);
|
830 | this.data = new DataView(dst.buffer);
|
831 |
|
832 | return this;
|
833 | };
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 | util.DataBuffer.prototype.putByte = function(b) {
|
843 | this.accommodate(1);
|
844 | this.data.setUint8(this.write++, b);
|
845 | return this;
|
846 | };
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 |
|
855 |
|
856 | util.DataBuffer.prototype.fillWithByte = function(b, n) {
|
857 | this.accommodate(n);
|
858 | for(var i = 0; i < n; ++i) {
|
859 | this.data.setUint8(b);
|
860 | }
|
861 | return this;
|
862 | };
|
863 |
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 | util.DataBuffer.prototype.putBytes = function(bytes, encoding) {
|
875 | if(util.isArrayBufferView(bytes)) {
|
876 | var src = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
877 | var len = src.byteLength - src.byteOffset;
|
878 | this.accommodate(len);
|
879 | var dst = new Uint8Array(this.data.buffer, this.write);
|
880 | dst.set(src);
|
881 | this.write += len;
|
882 | return this;
|
883 | }
|
884 |
|
885 | if(util.isArrayBuffer(bytes)) {
|
886 | var src = new Uint8Array(bytes);
|
887 | this.accommodate(src.byteLength);
|
888 | var dst = new Uint8Array(this.data.buffer);
|
889 | dst.set(src, this.write);
|
890 | this.write += src.byteLength;
|
891 | return this;
|
892 | }
|
893 |
|
894 |
|
895 | if(bytes instanceof util.DataBuffer ||
|
896 | (typeof bytes === 'object' &&
|
897 | typeof bytes.read === 'number' && typeof bytes.write === 'number' &&
|
898 | util.isArrayBufferView(bytes.data))) {
|
899 | var src = new Uint8Array(bytes.data.byteLength, bytes.read, bytes.length());
|
900 | this.accommodate(src.byteLength);
|
901 | var dst = new Uint8Array(bytes.data.byteLength, this.write);
|
902 | dst.set(src);
|
903 | this.write += src.byteLength;
|
904 | return this;
|
905 | }
|
906 |
|
907 | if(bytes instanceof util.ByteStringBuffer) {
|
908 |
|
909 | bytes = bytes.data;
|
910 | encoding = 'binary';
|
911 | }
|
912 |
|
913 |
|
914 | encoding = encoding || 'binary';
|
915 | if(typeof bytes === 'string') {
|
916 | var view;
|
917 |
|
918 |
|
919 | if(encoding === 'hex') {
|
920 | this.accommodate(Math.ceil(bytes.length / 2));
|
921 | view = new Uint8Array(this.data.buffer, this.write);
|
922 | this.write += util.binary.hex.decode(bytes, view, this.write);
|
923 | return this;
|
924 | }
|
925 | if(encoding === 'base64') {
|
926 | this.accommodate(Math.ceil(bytes.length / 4) * 3);
|
927 | view = new Uint8Array(this.data.buffer, this.write);
|
928 | this.write += util.binary.base64.decode(bytes, view, this.write);
|
929 | return this;
|
930 | }
|
931 |
|
932 |
|
933 | if(encoding === 'utf8') {
|
934 |
|
935 | bytes = util.encodeUtf8(bytes);
|
936 | encoding = 'binary';
|
937 | }
|
938 |
|
939 |
|
940 | if(encoding === 'binary' || encoding === 'raw') {
|
941 |
|
942 | this.accommodate(bytes.length);
|
943 | view = new Uint8Array(this.data.buffer, this.write);
|
944 | this.write += util.binary.raw.decode(view);
|
945 | return this;
|
946 | }
|
947 |
|
948 |
|
949 | if(encoding === 'utf16') {
|
950 |
|
951 | this.accommodate(bytes.length * 2);
|
952 | view = new Uint16Array(this.data.buffer, this.write);
|
953 | this.write += util.text.utf16.encode(view);
|
954 | return this;
|
955 | }
|
956 |
|
957 | throw new Error('Invalid encoding: ' + encoding);
|
958 | }
|
959 |
|
960 | throw Error('Invalid parameter: ' + bytes);
|
961 | };
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 | util.DataBuffer.prototype.putBuffer = function(buffer) {
|
971 | this.putBytes(buffer);
|
972 | buffer.clear();
|
973 | return this;
|
974 | };
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 | util.DataBuffer.prototype.putString = function(str) {
|
985 | return this.putBytes(str, 'utf16');
|
986 | };
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 | util.DataBuffer.prototype.putInt16 = function(i) {
|
996 | this.accommodate(2);
|
997 | this.data.setInt16(this.write, i);
|
998 | this.write += 2;
|
999 | return this;
|
1000 | };
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 | util.DataBuffer.prototype.putInt24 = function(i) {
|
1010 | this.accommodate(3);
|
1011 | this.data.setInt16(this.write, i >> 8 & 0xFFFF);
|
1012 | this.data.setInt8(this.write, i >> 16 & 0xFF);
|
1013 | this.write += 3;
|
1014 | return this;
|
1015 | };
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 |
|
1024 | util.DataBuffer.prototype.putInt32 = function(i) {
|
1025 | this.accommodate(4);
|
1026 | this.data.setInt32(this.write, i);
|
1027 | this.write += 4;
|
1028 | return this;
|
1029 | };
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 | util.DataBuffer.prototype.putInt16Le = function(i) {
|
1039 | this.accommodate(2);
|
1040 | this.data.setInt16(this.write, i, true);
|
1041 | this.write += 2;
|
1042 | return this;
|
1043 | };
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 |
|
1050 |
|
1051 |
|
1052 | util.DataBuffer.prototype.putInt24Le = function(i) {
|
1053 | this.accommodate(3);
|
1054 | this.data.setInt8(this.write, i >> 16 & 0xFF);
|
1055 | this.data.setInt16(this.write, i >> 8 & 0xFFFF, true);
|
1056 | this.write += 3;
|
1057 | return this;
|
1058 | };
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 | util.DataBuffer.prototype.putInt32Le = function(i) {
|
1068 | this.accommodate(4);
|
1069 | this.data.setInt32(this.write, i, true);
|
1070 | this.write += 4;
|
1071 | return this;
|
1072 | };
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 | util.DataBuffer.prototype.putInt = function(i, n) {
|
1083 | _checkBitsParam(n);
|
1084 | this.accommodate(n / 8);
|
1085 | do {
|
1086 | n -= 8;
|
1087 | this.data.setInt8(this.write++, (i >> n) & 0xFF);
|
1088 | } while(n > 0);
|
1089 | return this;
|
1090 | };
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 |
|
1097 |
|
1098 |
|
1099 |
|
1100 |
|
1101 | util.DataBuffer.prototype.putSignedInt = function(i, n) {
|
1102 | _checkBitsParam(n);
|
1103 | this.accommodate(n / 8);
|
1104 | if(i < 0) {
|
1105 | i += 2 << (n - 1);
|
1106 | }
|
1107 | return this.putInt(i, n);
|
1108 | };
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 | util.DataBuffer.prototype.getByte = function() {
|
1116 | return this.data.getInt8(this.read++);
|
1117 | };
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 | util.DataBuffer.prototype.getInt16 = function() {
|
1126 | var rval = this.data.getInt16(this.read);
|
1127 | this.read += 2;
|
1128 | return rval;
|
1129 | };
|
1130 |
|
1131 |
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 | util.DataBuffer.prototype.getInt24 = function() {
|
1138 | var rval = (
|
1139 | this.data.getInt16(this.read) << 8 ^
|
1140 | this.data.getInt8(this.read + 2));
|
1141 | this.read += 3;
|
1142 | return rval;
|
1143 | };
|
1144 |
|
1145 |
|
1146 |
|
1147 |
|
1148 |
|
1149 |
|
1150 |
|
1151 | util.DataBuffer.prototype.getInt32 = function() {
|
1152 | var rval = this.data.getInt32(this.read);
|
1153 | this.read += 4;
|
1154 | return rval;
|
1155 | };
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 | util.DataBuffer.prototype.getInt16Le = function() {
|
1164 | var rval = this.data.getInt16(this.read, true);
|
1165 | this.read += 2;
|
1166 | return rval;
|
1167 | };
|
1168 |
|
1169 |
|
1170 |
|
1171 |
|
1172 |
|
1173 |
|
1174 |
|
1175 | util.DataBuffer.prototype.getInt24Le = function() {
|
1176 | var rval = (
|
1177 | this.data.getInt8(this.read) ^
|
1178 | this.data.getInt16(this.read + 1, true) << 8);
|
1179 | this.read += 3;
|
1180 | return rval;
|
1181 | };
|
1182 |
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 | util.DataBuffer.prototype.getInt32Le = function() {
|
1190 | var rval = this.data.getInt32(this.read, true);
|
1191 | this.read += 4;
|
1192 | return rval;
|
1193 | };
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 |
|
1199 |
|
1200 |
|
1201 |
|
1202 |
|
1203 | util.DataBuffer.prototype.getInt = function(n) {
|
1204 | _checkBitsParam(n);
|
1205 | var rval = 0;
|
1206 | do {
|
1207 |
|
1208 | rval = (rval << 8) + this.data.getInt8(this.read++);
|
1209 | n -= 8;
|
1210 | } while(n > 0);
|
1211 | return rval;
|
1212 | };
|
1213 |
|
1214 |
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 | util.DataBuffer.prototype.getSignedInt = function(n) {
|
1223 |
|
1224 | var x = this.getInt(n);
|
1225 | var max = 2 << (n - 2);
|
1226 | if(x >= max) {
|
1227 | x -= max << 1;
|
1228 | }
|
1229 | return x;
|
1230 | };
|
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 | util.DataBuffer.prototype.getBytes = function(count) {
|
1241 |
|
1242 |
|
1243 |
|
1244 | var rval;
|
1245 | if(count) {
|
1246 |
|
1247 | count = Math.min(this.length(), count);
|
1248 | rval = this.data.slice(this.read, this.read + count);
|
1249 | this.read += count;
|
1250 | } else if(count === 0) {
|
1251 | rval = '';
|
1252 | } else {
|
1253 |
|
1254 | rval = (this.read === 0) ? this.data : this.data.slice(this.read);
|
1255 | this.clear();
|
1256 | }
|
1257 | return rval;
|
1258 | };
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 | util.DataBuffer.prototype.bytes = function(count) {
|
1269 |
|
1270 | return (typeof(count) === 'undefined' ?
|
1271 | this.data.slice(this.read) :
|
1272 | this.data.slice(this.read, this.read + count));
|
1273 | };
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 | util.DataBuffer.prototype.at = function(i) {
|
1283 | return this.data.getUint8(this.read + i);
|
1284 | };
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 | util.DataBuffer.prototype.setAt = function(i, b) {
|
1295 | this.data.setUint8(i, b);
|
1296 | return this;
|
1297 | };
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 | util.DataBuffer.prototype.last = function() {
|
1305 | return this.data.getUint8(this.write - 1);
|
1306 | };
|
1307 |
|
1308 |
|
1309 |
|
1310 |
|
1311 |
|
1312 |
|
1313 | util.DataBuffer.prototype.copy = function() {
|
1314 | return new util.DataBuffer(this);
|
1315 | };
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 |
|
1322 | util.DataBuffer.prototype.compact = function() {
|
1323 | if(this.read > 0) {
|
1324 | var src = new Uint8Array(this.data.buffer, this.read);
|
1325 | var dst = new Uint8Array(src.byteLength);
|
1326 | dst.set(src);
|
1327 | this.data = new DataView(dst);
|
1328 | this.write -= this.read;
|
1329 | this.read = 0;
|
1330 | }
|
1331 | return this;
|
1332 | };
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 | util.DataBuffer.prototype.clear = function() {
|
1340 | this.data = new DataView(new ArrayBuffer(0));
|
1341 | this.read = this.write = 0;
|
1342 | return this;
|
1343 | };
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 | util.DataBuffer.prototype.truncate = function(count) {
|
1353 | this.write = Math.max(0, this.length() - count);
|
1354 | this.read = Math.min(this.read, this.write);
|
1355 | return this;
|
1356 | };
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 | util.DataBuffer.prototype.toHex = function() {
|
1364 | var rval = '';
|
1365 | for(var i = this.read; i < this.data.byteLength; ++i) {
|
1366 | var b = this.data.getUint8(i);
|
1367 | if(b < 16) {
|
1368 | rval += '0';
|
1369 | }
|
1370 | rval += b.toString(16);
|
1371 | }
|
1372 | return rval;
|
1373 | };
|
1374 |
|
1375 |
|
1376 |
|
1377 |
|
1378 |
|
1379 |
|
1380 |
|
1381 |
|
1382 |
|
1383 |
|
1384 | util.DataBuffer.prototype.toString = function(encoding) {
|
1385 | var view = new Uint8Array(this.data, this.read, this.length());
|
1386 | encoding = encoding || 'utf8';
|
1387 |
|
1388 |
|
1389 | if(encoding === 'binary' || encoding === 'raw') {
|
1390 | return util.binary.raw.encode(view);
|
1391 | }
|
1392 | if(encoding === 'hex') {
|
1393 | return util.binary.hex.encode(view);
|
1394 | }
|
1395 | if(encoding === 'base64') {
|
1396 | return util.binary.base64.encode(view);
|
1397 | }
|
1398 |
|
1399 |
|
1400 | if(encoding === 'utf8') {
|
1401 | return util.text.utf8.decode(view);
|
1402 | }
|
1403 | if(encoding === 'utf16') {
|
1404 | return util.text.utf16.decode(view);
|
1405 | }
|
1406 |
|
1407 | throw new Error('Invalid encoding: ' + encoding);
|
1408 | };
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 |
|
1422 | util.createBuffer = function(input, encoding) {
|
1423 |
|
1424 | encoding = encoding || 'raw';
|
1425 | if(input !== undefined && encoding === 'utf8') {
|
1426 | input = util.encodeUtf8(input);
|
1427 | }
|
1428 | return new util.ByteBuffer(input);
|
1429 | };
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 |
|
1436 |
|
1437 |
|
1438 |
|
1439 |
|
1440 |
|
1441 | util.fillString = function(c, n) {
|
1442 | var s = '';
|
1443 | while(n > 0) {
|
1444 | if(n & 1) {
|
1445 | s += c;
|
1446 | }
|
1447 | n >>>= 1;
|
1448 | if(n > 0) {
|
1449 | c += c;
|
1450 | }
|
1451 | }
|
1452 | return s;
|
1453 | };
|
1454 |
|
1455 |
|
1456 |
|
1457 |
|
1458 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 | util.xorBytes = function(s1, s2, n) {
|
1466 | var s3 = '';
|
1467 | var b = '';
|
1468 | var t = '';
|
1469 | var i = 0;
|
1470 | var c = 0;
|
1471 | for(; n > 0; --n, ++i) {
|
1472 | b = s1.charCodeAt(i) ^ s2.charCodeAt(i);
|
1473 | if(c >= 10) {
|
1474 | s3 += t;
|
1475 | t = '';
|
1476 | c = 0;
|
1477 | }
|
1478 | t += String.fromCharCode(b);
|
1479 | ++c;
|
1480 | }
|
1481 | s3 += t;
|
1482 | return s3;
|
1483 | };
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 |
|
1492 | util.hexToBytes = function(hex) {
|
1493 |
|
1494 | var rval = '';
|
1495 | var i = 0;
|
1496 | if(hex.length & 1 == 1) {
|
1497 |
|
1498 | i = 1;
|
1499 | rval += String.fromCharCode(parseInt(hex[0], 16));
|
1500 | }
|
1501 |
|
1502 | for(; i < hex.length; i += 2) {
|
1503 | rval += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
1504 | }
|
1505 | return rval;
|
1506 | };
|
1507 |
|
1508 |
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 | util.bytesToHex = function(bytes) {
|
1516 |
|
1517 | return util.createBuffer(bytes).toHex();
|
1518 | };
|
1519 |
|
1520 |
|
1521 |
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 | util.int32ToBytes = function(i) {
|
1528 | return (
|
1529 | String.fromCharCode(i >> 24 & 0xFF) +
|
1530 | String.fromCharCode(i >> 16 & 0xFF) +
|
1531 | String.fromCharCode(i >> 8 & 0xFF) +
|
1532 | String.fromCharCode(i & 0xFF));
|
1533 | };
|
1534 |
|
1535 |
|
1536 | var _base64 =
|
1537 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
1538 | var _base64Idx = [
|
1539 |
|
1540 |
|
1541 | 62, -1, -1, -1, 63,
|
1542 |
|
1543 |
|
1544 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
|
1545 |
|
1546 |
|
1547 | -1, -1, -1, 64, -1, -1, -1,
|
1548 |
|
1549 |
|
1550 |
|
1551 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
|
1552 |
|
1553 |
|
1554 | 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
|
1555 |
|
1556 |
|
1557 |
|
1558 | -1, -1, -1, -1, -1, -1,
|
1559 |
|
1560 |
|
1561 |
|
1562 | 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
|
1563 |
|
1564 |
|
1565 | 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
|
1566 | ];
|
1567 |
|
1568 |
|
1569 | var _base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 |
|
1577 |
|
1578 |
|
1579 |
|
1580 | util.encode64 = function(input, maxline) {
|
1581 |
|
1582 | var line = '';
|
1583 | var output = '';
|
1584 | var chr1, chr2, chr3;
|
1585 | var i = 0;
|
1586 | while(i < input.length) {
|
1587 | chr1 = input.charCodeAt(i++);
|
1588 | chr2 = input.charCodeAt(i++);
|
1589 | chr3 = input.charCodeAt(i++);
|
1590 |
|
1591 |
|
1592 | line += _base64.charAt(chr1 >> 2);
|
1593 | line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4));
|
1594 | if(isNaN(chr2)) {
|
1595 | line += '==';
|
1596 | } else {
|
1597 | line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6));
|
1598 | line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
|
1599 | }
|
1600 |
|
1601 | if(maxline && line.length > maxline) {
|
1602 | output += line.substr(0, maxline) + '\r\n';
|
1603 | line = line.substr(maxline);
|
1604 | }
|
1605 | }
|
1606 | output += line;
|
1607 | return output;
|
1608 | };
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 | util.decode64 = function(input) {
|
1618 |
|
1619 |
|
1620 |
|
1621 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
|
1622 |
|
1623 | var output = '';
|
1624 | var enc1, enc2, enc3, enc4;
|
1625 | var i = 0;
|
1626 |
|
1627 | while(i < input.length) {
|
1628 | enc1 = _base64Idx[input.charCodeAt(i++) - 43];
|
1629 | enc2 = _base64Idx[input.charCodeAt(i++) - 43];
|
1630 | enc3 = _base64Idx[input.charCodeAt(i++) - 43];
|
1631 | enc4 = _base64Idx[input.charCodeAt(i++) - 43];
|
1632 |
|
1633 | output += String.fromCharCode((enc1 << 2) | (enc2 >> 4));
|
1634 | if(enc3 !== 64) {
|
1635 |
|
1636 | output += String.fromCharCode(((enc2 & 15) << 4) | (enc3 >> 2));
|
1637 | if(enc4 !== 64) {
|
1638 |
|
1639 | output += String.fromCharCode(((enc3 & 3) << 6) | enc4);
|
1640 | }
|
1641 | }
|
1642 | }
|
1643 |
|
1644 | return output;
|
1645 | };
|
1646 |
|
1647 |
|
1648 |
|
1649 |
|
1650 |
|
1651 |
|
1652 |
|
1653 |
|
1654 |
|
1655 |
|
1656 |
|
1657 | util.encodeUtf8 = function(str) {
|
1658 | return unescape(encodeURIComponent(str));
|
1659 | };
|
1660 |
|
1661 |
|
1662 |
|
1663 |
|
1664 |
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 |
|
1670 | util.decodeUtf8 = function(str) {
|
1671 | return decodeURIComponent(escape(str));
|
1672 | };
|
1673 |
|
1674 |
|
1675 |
|
1676 | util.binary = {
|
1677 | raw: {},
|
1678 | hex: {},
|
1679 | base64: {},
|
1680 | base58: {},
|
1681 | baseN : {
|
1682 | encode: baseN.encode,
|
1683 | decode: baseN.decode
|
1684 | }
|
1685 | };
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 | util.binary.raw.encode = function(bytes) {
|
1696 | return String.fromCharCode.apply(null, bytes);
|
1697 | };
|
1698 |
|
1699 |
|
1700 |
|
1701 |
|
1702 |
|
1703 |
|
1704 |
|
1705 |
|
1706 |
|
1707 |
|
1708 |
|
1709 |
|
1710 | util.binary.raw.decode = function(str, output, offset) {
|
1711 | var out = output;
|
1712 | if(!out) {
|
1713 | out = new Uint8Array(str.length);
|
1714 | }
|
1715 | offset = offset || 0;
|
1716 | var j = offset;
|
1717 | for(var i = 0; i < str.length; ++i) {
|
1718 | out[j++] = str.charCodeAt(i);
|
1719 | }
|
1720 | return output ? (j - offset) : out;
|
1721 | };
|
1722 |
|
1723 |
|
1724 |
|
1725 |
|
1726 |
|
1727 |
|
1728 |
|
1729 |
|
1730 |
|
1731 | util.binary.hex.encode = util.bytesToHex;
|
1732 |
|
1733 |
|
1734 |
|
1735 |
|
1736 |
|
1737 |
|
1738 |
|
1739 |
|
1740 |
|
1741 |
|
1742 |
|
1743 | util.binary.hex.decode = function(hex, output, offset) {
|
1744 | var out = output;
|
1745 | if(!out) {
|
1746 | out = new Uint8Array(Math.ceil(hex.length / 2));
|
1747 | }
|
1748 | offset = offset || 0;
|
1749 | var i = 0, j = offset;
|
1750 | if(hex.length & 1) {
|
1751 |
|
1752 | i = 1;
|
1753 | out[j++] = parseInt(hex[0], 16);
|
1754 | }
|
1755 |
|
1756 | for(; i < hex.length; i += 2) {
|
1757 | out[j++] = parseInt(hex.substr(i, 2), 16);
|
1758 | }
|
1759 | return output ? (j - offset) : out;
|
1760 | };
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 |
|
1768 |
|
1769 |
|
1770 |
|
1771 | util.binary.base64.encode = function(input, maxline) {
|
1772 | var line = '';
|
1773 | var output = '';
|
1774 | var chr1, chr2, chr3;
|
1775 | var i = 0;
|
1776 | while(i < input.byteLength) {
|
1777 | chr1 = input[i++];
|
1778 | chr2 = input[i++];
|
1779 | chr3 = input[i++];
|
1780 |
|
1781 |
|
1782 | line += _base64.charAt(chr1 >> 2);
|
1783 | line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4));
|
1784 | if(isNaN(chr2)) {
|
1785 | line += '==';
|
1786 | } else {
|
1787 | line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6));
|
1788 | line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
|
1789 | }
|
1790 |
|
1791 | if(maxline && line.length > maxline) {
|
1792 | output += line.substr(0, maxline) + '\r\n';
|
1793 | line = line.substr(maxline);
|
1794 | }
|
1795 | }
|
1796 | output += line;
|
1797 | return output;
|
1798 | };
|
1799 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 |
|
1806 |
|
1807 |
|
1808 |
|
1809 |
|
1810 | util.binary.base64.decode = function(input, output, offset) {
|
1811 | var out = output;
|
1812 | if(!out) {
|
1813 | out = new Uint8Array(Math.ceil(input.length / 4) * 3);
|
1814 | }
|
1815 |
|
1816 |
|
1817 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
|
1818 |
|
1819 | offset = offset || 0;
|
1820 | var enc1, enc2, enc3, enc4;
|
1821 | var i = 0, j = offset;
|
1822 |
|
1823 | while(i < input.length) {
|
1824 | enc1 = _base64Idx[input.charCodeAt(i++) - 43];
|
1825 | enc2 = _base64Idx[input.charCodeAt(i++) - 43];
|
1826 | enc3 = _base64Idx[input.charCodeAt(i++) - 43];
|
1827 | enc4 = _base64Idx[input.charCodeAt(i++) - 43];
|
1828 |
|
1829 | out[j++] = (enc1 << 2) | (enc2 >> 4);
|
1830 | if(enc3 !== 64) {
|
1831 |
|
1832 | out[j++] = ((enc2 & 15) << 4) | (enc3 >> 2);
|
1833 | if(enc4 !== 64) {
|
1834 |
|
1835 | out[j++] = ((enc3 & 3) << 6) | enc4;
|
1836 | }
|
1837 | }
|
1838 | }
|
1839 |
|
1840 |
|
1841 | return output ? (j - offset) : out.subarray(0, j);
|
1842 | };
|
1843 |
|
1844 |
|
1845 | util.binary.base58.encode = function(input, maxline) {
|
1846 | return util.binary.baseN.encode(input, _base58, maxline);
|
1847 | };
|
1848 | util.binary.base58.decode = function(input, maxline) {
|
1849 | return util.binary.baseN.decode(input, _base58, maxline);
|
1850 | };
|
1851 |
|
1852 |
|
1853 |
|
1854 | util.text = {
|
1855 | utf8: {},
|
1856 | utf16: {}
|
1857 | };
|
1858 |
|
1859 |
|
1860 |
|
1861 |
|
1862 |
|
1863 |
|
1864 |
|
1865 |
|
1866 |
|
1867 |
|
1868 |
|
1869 | util.text.utf8.encode = function(str, output, offset) {
|
1870 | str = util.encodeUtf8(str);
|
1871 | var out = output;
|
1872 | if(!out) {
|
1873 | out = new Uint8Array(str.length);
|
1874 | }
|
1875 | offset = offset || 0;
|
1876 | var j = offset;
|
1877 | for(var i = 0; i < str.length; ++i) {
|
1878 | out[j++] = str.charCodeAt(i);
|
1879 | }
|
1880 | return output ? (j - offset) : out;
|
1881 | };
|
1882 |
|
1883 |
|
1884 |
|
1885 |
|
1886 |
|
1887 |
|
1888 |
|
1889 |
|
1890 | util.text.utf8.decode = function(bytes) {
|
1891 | return util.decodeUtf8(String.fromCharCode.apply(null, bytes));
|
1892 | };
|
1893 |
|
1894 |
|
1895 |
|
1896 |
|
1897 |
|
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 | util.text.utf16.encode = function(str, output, offset) {
|
1905 | var out = output;
|
1906 | if(!out) {
|
1907 | out = new Uint8Array(str.length * 2);
|
1908 | }
|
1909 | var view = new Uint16Array(out.buffer);
|
1910 | offset = offset || 0;
|
1911 | var j = offset;
|
1912 | var k = offset;
|
1913 | for(var i = 0; i < str.length; ++i) {
|
1914 | view[k++] = str.charCodeAt(i);
|
1915 | j += 2;
|
1916 | }
|
1917 | return output ? (j - offset) : out;
|
1918 | };
|
1919 |
|
1920 |
|
1921 |
|
1922 |
|
1923 |
|
1924 |
|
1925 |
|
1926 |
|
1927 | util.text.utf16.decode = function(bytes) {
|
1928 | return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
|
1929 | };
|
1930 |
|
1931 |
|
1932 |
|
1933 |
|
1934 |
|
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 |
|
1941 | util.deflate = function(api, bytes, raw) {
|
1942 | bytes = util.decode64(api.deflate(util.encode64(bytes)).rval);
|
1943 |
|
1944 |
|
1945 | if(raw) {
|
1946 |
|
1947 |
|
1948 |
|
1949 | var start = 2;
|
1950 | var flg = bytes.charCodeAt(1);
|
1951 | if(flg & 0x20) {
|
1952 | start = 6;
|
1953 | }
|
1954 |
|
1955 | bytes = bytes.substring(start, bytes.length - 4);
|
1956 | }
|
1957 |
|
1958 | return bytes;
|
1959 | };
|
1960 |
|
1961 |
|
1962 |
|
1963 |
|
1964 |
|
1965 |
|
1966 |
|
1967 |
|
1968 |
|
1969 |
|
1970 |
|
1971 | util.inflate = function(api, bytes, raw) {
|
1972 |
|
1973 | var rval = api.inflate(util.encode64(bytes)).rval;
|
1974 | return (rval === null) ? null : util.decode64(rval);
|
1975 | };
|
1976 |
|
1977 |
|
1978 |
|
1979 |
|
1980 |
|
1981 |
|
1982 |
|
1983 |
|
1984 | var _setStorageObject = function(api, id, obj) {
|
1985 | if(!api) {
|
1986 | throw new Error('WebStorage not available.');
|
1987 | }
|
1988 |
|
1989 | var rval;
|
1990 | if(obj === null) {
|
1991 | rval = api.removeItem(id);
|
1992 | } else {
|
1993 |
|
1994 | obj = util.encode64(JSON.stringify(obj));
|
1995 | rval = api.setItem(id, obj);
|
1996 | }
|
1997 |
|
1998 |
|
1999 | if(typeof(rval) !== 'undefined' && rval.rval !== true) {
|
2000 | var error = new Error(rval.error.message);
|
2001 | error.id = rval.error.id;
|
2002 | error.name = rval.error.name;
|
2003 | throw error;
|
2004 | }
|
2005 | };
|
2006 |
|
2007 |
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 | var _getStorageObject = function(api, id) {
|
2016 | if(!api) {
|
2017 | throw new Error('WebStorage not available.');
|
2018 | }
|
2019 |
|
2020 |
|
2021 | var rval = api.getItem(id);
|
2022 |
|
2023 | |
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 | if(api.init) {
|
2030 | if(rval.rval === null) {
|
2031 | if(rval.error) {
|
2032 | var error = new Error(rval.error.message);
|
2033 | error.id = rval.error.id;
|
2034 | error.name = rval.error.name;
|
2035 | throw error;
|
2036 | }
|
2037 |
|
2038 | rval = null;
|
2039 | } else {
|
2040 | rval = rval.rval;
|
2041 | }
|
2042 | }
|
2043 |
|
2044 |
|
2045 | if(rval !== null) {
|
2046 |
|
2047 | rval = JSON.parse(util.decode64(rval));
|
2048 | }
|
2049 |
|
2050 | return rval;
|
2051 | };
|
2052 |
|
2053 |
|
2054 |
|
2055 |
|
2056 |
|
2057 |
|
2058 |
|
2059 |
|
2060 |
|
2061 | var _setItem = function(api, id, key, data) {
|
2062 |
|
2063 | var obj = _getStorageObject(api, id);
|
2064 | if(obj === null) {
|
2065 |
|
2066 | obj = {};
|
2067 | }
|
2068 |
|
2069 | obj[key] = data;
|
2070 |
|
2071 |
|
2072 | _setStorageObject(api, id, obj);
|
2073 | };
|
2074 |
|
2075 |
|
2076 |
|
2077 |
|
2078 |
|
2079 |
|
2080 |
|
2081 |
|
2082 |
|
2083 |
|
2084 | var _getItem = function(api, id, key) {
|
2085 |
|
2086 | var rval = _getStorageObject(api, id);
|
2087 | if(rval !== null) {
|
2088 |
|
2089 | rval = (key in rval) ? rval[key] : null;
|
2090 | }
|
2091 |
|
2092 | return rval;
|
2093 | };
|
2094 |
|
2095 |
|
2096 |
|
2097 |
|
2098 |
|
2099 |
|
2100 |
|
2101 |
|
2102 | var _removeItem = function(api, id, key) {
|
2103 |
|
2104 | var obj = _getStorageObject(api, id);
|
2105 | if(obj !== null && key in obj) {
|
2106 |
|
2107 | delete obj[key];
|
2108 |
|
2109 |
|
2110 | var empty = true;
|
2111 | for(var prop in obj) {
|
2112 | empty = false;
|
2113 | break;
|
2114 | }
|
2115 | if(empty) {
|
2116 |
|
2117 | obj = null;
|
2118 | }
|
2119 |
|
2120 |
|
2121 | _setStorageObject(api, id, obj);
|
2122 | }
|
2123 | };
|
2124 |
|
2125 |
|
2126 |
|
2127 |
|
2128 |
|
2129 |
|
2130 |
|
2131 | var _clearItems = function(api, id) {
|
2132 | _setStorageObject(api, id, null);
|
2133 | };
|
2134 |
|
2135 |
|
2136 |
|
2137 |
|
2138 |
|
2139 |
|
2140 |
|
2141 |
|
2142 |
|
2143 |
|
2144 | var _callStorageFunction = function(func, args, location) {
|
2145 | var rval = null;
|
2146 |
|
2147 |
|
2148 | if(typeof(location) === 'undefined') {
|
2149 | location = ['web', 'flash'];
|
2150 | }
|
2151 |
|
2152 |
|
2153 | var type;
|
2154 | var done = false;
|
2155 | var exception = null;
|
2156 | for(var idx in location) {
|
2157 | type = location[idx];
|
2158 | try {
|
2159 | if(type === 'flash' || type === 'both') {
|
2160 | if(args[0] === null) {
|
2161 | throw new Error('Flash local storage not available.');
|
2162 | }
|
2163 | rval = func.apply(this, args);
|
2164 | done = (type === 'flash');
|
2165 | }
|
2166 | if(type === 'web' || type === 'both') {
|
2167 | args[0] = localStorage;
|
2168 | rval = func.apply(this, args);
|
2169 | done = true;
|
2170 | }
|
2171 | } catch(ex) {
|
2172 | exception = ex;
|
2173 | }
|
2174 | if(done) {
|
2175 | break;
|
2176 | }
|
2177 | }
|
2178 |
|
2179 | if(!done) {
|
2180 | throw exception;
|
2181 | }
|
2182 |
|
2183 | return rval;
|
2184 | };
|
2185 |
|
2186 |
|
2187 |
|
2188 |
|
2189 |
|
2190 |
|
2191 |
|
2192 |
|
2193 |
|
2194 |
|
2195 |
|
2196 |
|
2197 |
|
2198 |
|
2199 |
|
2200 |
|
2201 |
|
2202 |
|
2203 |
|
2204 |
|
2205 |
|
2206 |
|
2207 |
|
2208 |
|
2209 |
|
2210 |
|
2211 |
|
2212 |
|
2213 |
|
2214 | util.setItem = function(api, id, key, data, location) {
|
2215 | _callStorageFunction(_setItem, arguments, location);
|
2216 | };
|
2217 |
|
2218 |
|
2219 |
|
2220 |
|
2221 |
|
2222 |
|
2223 |
|
2224 |
|
2225 |
|
2226 |
|
2227 |
|
2228 |
|
2229 |
|
2230 | util.getItem = function(api, id, key, location) {
|
2231 | return _callStorageFunction(_getItem, arguments, location);
|
2232 | };
|
2233 |
|
2234 |
|
2235 |
|
2236 |
|
2237 |
|
2238 |
|
2239 |
|
2240 |
|
2241 |
|
2242 |
|
2243 |
|
2244 | util.removeItem = function(api, id, key, location) {
|
2245 | _callStorageFunction(_removeItem, arguments, location);
|
2246 | };
|
2247 |
|
2248 |
|
2249 |
|
2250 |
|
2251 |
|
2252 |
|
2253 |
|
2254 |
|
2255 |
|
2256 |
|
2257 | util.clearItems = function(api, id, location) {
|
2258 | _callStorageFunction(_clearItems, arguments, location);
|
2259 | };
|
2260 |
|
2261 |
|
2262 |
|
2263 |
|
2264 |
|
2265 |
|
2266 |
|
2267 |
|
2268 | util.parseUrl = function(str) {
|
2269 |
|
2270 | var regex = /^(https?):\/\/([^:&^\/]*):?(\d*)(.*)$/g;
|
2271 | regex.lastIndex = 0;
|
2272 | var m = regex.exec(str);
|
2273 | var url = (m === null) ? null : {
|
2274 | full: str,
|
2275 | scheme: m[1],
|
2276 | host: m[2],
|
2277 | port: m[3],
|
2278 | path: m[4]
|
2279 | };
|
2280 | if(url) {
|
2281 | url.fullHost = url.host;
|
2282 | if(url.port) {
|
2283 | if(url.port !== 80 && url.scheme === 'http') {
|
2284 | url.fullHost += ':' + url.port;
|
2285 | } else if(url.port !== 443 && url.scheme === 'https') {
|
2286 | url.fullHost += ':' + url.port;
|
2287 | }
|
2288 | } else if(url.scheme === 'http') {
|
2289 | url.port = 80;
|
2290 | } else if(url.scheme === 'https') {
|
2291 | url.port = 443;
|
2292 | }
|
2293 | url.full = url.scheme + '://' + url.fullHost;
|
2294 | }
|
2295 | return url;
|
2296 | };
|
2297 |
|
2298 |
|
2299 | var _queryVariables = null;
|
2300 |
|
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 |
|
2308 |
|
2309 |
|
2310 |
|
2311 |
|
2312 |
|
2313 |
|
2314 |
|
2315 |
|
2316 |
|
2317 |
|
2318 |
|
2319 |
|
2320 |
|
2321 |
|
2322 |
|
2323 |
|
2324 |
|
2325 |
|
2326 |
|
2327 |
|
2328 |
|
2329 |
|
2330 |
|
2331 |
|
2332 |
|
2333 |
|
2334 |
|
2335 |
|
2336 |
|
2337 |
|
2338 |
|
2339 |
|
2340 | util.getQueryVariables = function(query) {
|
2341 | var parse = function(q) {
|
2342 | var rval = {};
|
2343 | var kvpairs = q.split('&');
|
2344 | for(var i = 0; i < kvpairs.length; i++) {
|
2345 | var pos = kvpairs[i].indexOf('=');
|
2346 | var key;
|
2347 | var val;
|
2348 | if(pos > 0) {
|
2349 | key = kvpairs[i].substring(0, pos);
|
2350 | val = kvpairs[i].substring(pos + 1);
|
2351 | } else {
|
2352 | key = kvpairs[i];
|
2353 | val = null;
|
2354 | }
|
2355 | if(!(key in rval)) {
|
2356 | rval[key] = [];
|
2357 | }
|
2358 |
|
2359 | if(!(key in Object.prototype) && val !== null) {
|
2360 | rval[key].push(unescape(val));
|
2361 | }
|
2362 | }
|
2363 | return rval;
|
2364 | };
|
2365 |
|
2366 | var rval;
|
2367 | if(typeof(query) === 'undefined') {
|
2368 |
|
2369 | if(_queryVariables === null) {
|
2370 | if(typeof(window) !== 'undefined' && window.location && window.location.search) {
|
2371 |
|
2372 | _queryVariables = parse(window.location.search.substring(1));
|
2373 | } else {
|
2374 |
|
2375 | _queryVariables = {};
|
2376 | }
|
2377 | }
|
2378 | rval = _queryVariables;
|
2379 | } else {
|
2380 |
|
2381 | rval = parse(query);
|
2382 | }
|
2383 | return rval;
|
2384 | };
|
2385 |
|
2386 |
|
2387 |
|
2388 |
|
2389 |
|
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 |
|
2395 |
|
2396 |
|
2397 |
|
2398 | util.parseFragment = function(fragment) {
|
2399 |
|
2400 | var fp = fragment;
|
2401 | var fq = '';
|
2402 |
|
2403 | var pos = fragment.indexOf('?');
|
2404 | if(pos > 0) {
|
2405 | fp = fragment.substring(0, pos);
|
2406 | fq = fragment.substring(pos + 1);
|
2407 | }
|
2408 |
|
2409 | var path = fp.split('/');
|
2410 | if(path.length > 0 && path[0] === '') {
|
2411 | path.shift();
|
2412 | }
|
2413 |
|
2414 | var query = (fq === '') ? {} : util.getQueryVariables(fq);
|
2415 |
|
2416 | return {
|
2417 | pathString: fp,
|
2418 | queryString: fq,
|
2419 | path: path,
|
2420 | query: query
|
2421 | };
|
2422 | };
|
2423 |
|
2424 |
|
2425 |
|
2426 |
|
2427 |
|
2428 |
|
2429 |
|
2430 |
|
2431 |
|
2432 |
|
2433 |
|
2434 |
|
2435 |
|
2436 |
|
2437 |
|
2438 |
|
2439 |
|
2440 |
|
2441 |
|
2442 |
|
2443 | util.makeRequest = function(reqString) {
|
2444 | var frag = util.parseFragment(reqString);
|
2445 | var req = {
|
2446 |
|
2447 | path: frag.pathString,
|
2448 |
|
2449 | query: frag.queryString,
|
2450 | |
2451 |
|
2452 |
|
2453 |
|
2454 |
|
2455 |
|
2456 |
|
2457 | getPath: function(i) {
|
2458 | return (typeof(i) === 'undefined') ? frag.path : frag.path[i];
|
2459 | },
|
2460 | |
2461 |
|
2462 |
|
2463 |
|
2464 |
|
2465 |
|
2466 |
|
2467 |
|
2468 | getQuery: function(k, i) {
|
2469 | var rval;
|
2470 | if(typeof(k) === 'undefined') {
|
2471 | rval = frag.query;
|
2472 | } else {
|
2473 | rval = frag.query[k];
|
2474 | if(rval && typeof(i) !== 'undefined') {
|
2475 | rval = rval[i];
|
2476 | }
|
2477 | }
|
2478 | return rval;
|
2479 | },
|
2480 | getQueryLast: function(k, _default) {
|
2481 | var rval;
|
2482 | var vals = req.getQuery(k);
|
2483 | if(vals) {
|
2484 | rval = vals[vals.length - 1];
|
2485 | } else {
|
2486 | rval = _default;
|
2487 | }
|
2488 | return rval;
|
2489 | }
|
2490 | };
|
2491 | return req;
|
2492 | };
|
2493 |
|
2494 |
|
2495 |
|
2496 |
|
2497 |
|
2498 |
|
2499 |
|
2500 |
|
2501 |
|
2502 |
|
2503 |
|
2504 |
|
2505 | util.makeLink = function(path, query, fragment) {
|
2506 |
|
2507 | path = jQuery.isArray(path) ? path.join('/') : path;
|
2508 |
|
2509 | var qstr = jQuery.param(query || {});
|
2510 | fragment = fragment || '';
|
2511 | return path +
|
2512 | ((qstr.length > 0) ? ('?' + qstr) : '') +
|
2513 | ((fragment.length > 0) ? ('#' + fragment) : '');
|
2514 | };
|
2515 |
|
2516 |
|
2517 |
|
2518 |
|
2519 |
|
2520 |
|
2521 |
|
2522 |
|
2523 |
|
2524 | util.isEmpty = function(obj) {
|
2525 | for(var prop in obj) {
|
2526 | if(obj.hasOwnProperty(prop)) {
|
2527 | return false;
|
2528 | }
|
2529 | }
|
2530 | return true;
|
2531 | };
|
2532 |
|
2533 |
|
2534 |
|
2535 |
|
2536 |
|
2537 |
|
2538 |
|
2539 |
|
2540 |
|
2541 |
|
2542 | util.format = function(format) {
|
2543 | var re = /%./g;
|
2544 |
|
2545 | var match;
|
2546 |
|
2547 | var part;
|
2548 |
|
2549 | var argi = 0;
|
2550 |
|
2551 | var parts = [];
|
2552 |
|
2553 | var last = 0;
|
2554 |
|
2555 | while((match = re.exec(format))) {
|
2556 | part = format.substring(last, re.lastIndex - 2);
|
2557 |
|
2558 | if(part.length > 0) {
|
2559 | parts.push(part);
|
2560 | }
|
2561 | last = re.lastIndex;
|
2562 |
|
2563 | var code = match[0][1];
|
2564 | switch(code) {
|
2565 | case 's':
|
2566 | case 'o':
|
2567 |
|
2568 | if(argi < arguments.length) {
|
2569 | parts.push(arguments[argi++ + 1]);
|
2570 | } else {
|
2571 | parts.push('<?>');
|
2572 | }
|
2573 | break;
|
2574 |
|
2575 |
|
2576 |
|
2577 | case '%':
|
2578 | parts.push('%');
|
2579 | break;
|
2580 | default:
|
2581 | parts.push('<%' + code + '?>');
|
2582 | }
|
2583 | }
|
2584 |
|
2585 | parts.push(format.substring(last));
|
2586 | return parts.join('');
|
2587 | };
|
2588 |
|
2589 |
|
2590 |
|
2591 |
|
2592 |
|
2593 |
|
2594 | util.formatNumber = function(number, decimals, dec_point, thousands_sep) {
|
2595 |
|
2596 |
|
2597 |
|
2598 |
|
2599 |
|
2600 |
|
2601 |
|
2602 |
|
2603 |
|
2604 |
|
2605 | var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
|
2606 | var d = dec_point === undefined ? ',' : dec_point;
|
2607 | var t = thousands_sep === undefined ?
|
2608 | '.' : thousands_sep, s = n < 0 ? '-' : '';
|
2609 | var i = parseInt((n = Math.abs(+n || 0).toFixed(c)), 10) + '';
|
2610 | var j = (i.length > 3) ? i.length % 3 : 0;
|
2611 | return s + (j ? i.substr(0, j) + t : '') +
|
2612 | i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) +
|
2613 | (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
|
2614 | };
|
2615 |
|
2616 |
|
2617 |
|
2618 |
|
2619 |
|
2620 |
|
2621 | util.formatSize = function(size) {
|
2622 | if(size >= 1073741824) {
|
2623 | size = util.formatNumber(size / 1073741824, 2, '.', '') + ' GiB';
|
2624 | } else if(size >= 1048576) {
|
2625 | size = util.formatNumber(size / 1048576, 2, '.', '') + ' MiB';
|
2626 | } else if(size >= 1024) {
|
2627 | size = util.formatNumber(size / 1024, 0) + ' KiB';
|
2628 | } else {
|
2629 | size = util.formatNumber(size, 0) + ' bytes';
|
2630 | }
|
2631 | return size;
|
2632 | };
|
2633 |
|
2634 |
|
2635 |
|
2636 |
|
2637 |
|
2638 |
|
2639 |
|
2640 |
|
2641 |
|
2642 | util.bytesFromIP = function(ip) {
|
2643 | if(ip.indexOf('.') !== -1) {
|
2644 | return util.bytesFromIPv4(ip);
|
2645 | }
|
2646 | if(ip.indexOf(':') !== -1) {
|
2647 | return util.bytesFromIPv6(ip);
|
2648 | }
|
2649 | return null;
|
2650 | };
|
2651 |
|
2652 |
|
2653 |
|
2654 |
|
2655 |
|
2656 |
|
2657 |
|
2658 |
|
2659 | util.bytesFromIPv4 = function(ip) {
|
2660 | ip = ip.split('.');
|
2661 | if(ip.length !== 4) {
|
2662 | return null;
|
2663 | }
|
2664 | var b = util.createBuffer();
|
2665 | for(var i = 0; i < ip.length; ++i) {
|
2666 | var num = parseInt(ip[i], 10);
|
2667 | if(isNaN(num)) {
|
2668 | return null;
|
2669 | }
|
2670 | b.putByte(num);
|
2671 | }
|
2672 | return b.getBytes();
|
2673 | };
|
2674 |
|
2675 |
|
2676 |
|
2677 |
|
2678 |
|
2679 |
|
2680 |
|
2681 |
|
2682 | util.bytesFromIPv6 = function(ip) {
|
2683 | var blanks = 0;
|
2684 | ip = ip.split(':').filter(function(e) {
|
2685 | if(e.length === 0) ++blanks;
|
2686 | return true;
|
2687 | });
|
2688 | var zeros = (8 - ip.length + blanks) * 2;
|
2689 | var b = util.createBuffer();
|
2690 | for(var i = 0; i < 8; ++i) {
|
2691 | if(!ip[i] || ip[i].length === 0) {
|
2692 | b.fillWithByte(0, zeros);
|
2693 | zeros = 0;
|
2694 | continue;
|
2695 | }
|
2696 | var bytes = util.hexToBytes(ip[i]);
|
2697 | if(bytes.length < 2) {
|
2698 | b.putByte(0);
|
2699 | }
|
2700 | b.putBytes(bytes);
|
2701 | }
|
2702 | return b.getBytes();
|
2703 | };
|
2704 |
|
2705 |
|
2706 |
|
2707 |
|
2708 |
|
2709 |
|
2710 |
|
2711 |
|
2712 |
|
2713 |
|
2714 | util.bytesToIP = function(bytes) {
|
2715 | if(bytes.length === 4) {
|
2716 | return util.bytesToIPv4(bytes);
|
2717 | }
|
2718 | if(bytes.length === 16) {
|
2719 | return util.bytesToIPv6(bytes);
|
2720 | }
|
2721 | return null;
|
2722 | };
|
2723 |
|
2724 |
|
2725 |
|
2726 |
|
2727 |
|
2728 |
|
2729 |
|
2730 |
|
2731 |
|
2732 | util.bytesToIPv4 = function(bytes) {
|
2733 | if(bytes.length !== 4) {
|
2734 | return null;
|
2735 | }
|
2736 | var ip = [];
|
2737 | for(var i = 0; i < bytes.length; ++i) {
|
2738 | ip.push(bytes.charCodeAt(i));
|
2739 | }
|
2740 | return ip.join('.');
|
2741 | };
|
2742 |
|
2743 |
|
2744 |
|
2745 |
|
2746 |
|
2747 |
|
2748 |
|
2749 |
|
2750 |
|
2751 | util.bytesToIPv6 = function(bytes) {
|
2752 | if(bytes.length !== 16) {
|
2753 | return null;
|
2754 | }
|
2755 | var ip = [];
|
2756 | var zeroGroups = [];
|
2757 | var zeroMaxGroup = 0;
|
2758 | for(var i = 0; i < bytes.length; i += 2) {
|
2759 | var hex = util.bytesToHex(bytes[i] + bytes[i + 1]);
|
2760 |
|
2761 | while(hex[0] === '0' && hex !== '0') {
|
2762 | hex = hex.substr(1);
|
2763 | }
|
2764 | if(hex === '0') {
|
2765 | var last = zeroGroups[zeroGroups.length - 1];
|
2766 | var idx = ip.length;
|
2767 | if(!last || idx !== last.end + 1) {
|
2768 | zeroGroups.push({start: idx, end: idx});
|
2769 | } else {
|
2770 | last.end = idx;
|
2771 | if((last.end - last.start) >
|
2772 | (zeroGroups[zeroMaxGroup].end - zeroGroups[zeroMaxGroup].start)) {
|
2773 | zeroMaxGroup = zeroGroups.length - 1;
|
2774 | }
|
2775 | }
|
2776 | }
|
2777 | ip.push(hex);
|
2778 | }
|
2779 | if(zeroGroups.length > 0) {
|
2780 | var group = zeroGroups[zeroMaxGroup];
|
2781 |
|
2782 | if(group.end - group.start > 0) {
|
2783 | ip.splice(group.start, group.end - group.start + 1, '');
|
2784 | if(group.start === 0) {
|
2785 | ip.unshift('');
|
2786 | }
|
2787 | if(group.end === 7) {
|
2788 | ip.push('');
|
2789 | }
|
2790 | }
|
2791 | }
|
2792 | return ip.join(':');
|
2793 | };
|
2794 |
|
2795 |
|
2796 |
|
2797 |
|
2798 |
|
2799 |
|
2800 |
|
2801 |
|
2802 |
|
2803 |
|
2804 | util.estimateCores = function(options, callback) {
|
2805 | if(typeof options === 'function') {
|
2806 | callback = options;
|
2807 | options = {};
|
2808 | }
|
2809 | options = options || {};
|
2810 | if('cores' in util && !options.update) {
|
2811 | return callback(null, util.cores);
|
2812 | }
|
2813 | if(typeof navigator !== 'undefined' &&
|
2814 | 'hardwareConcurrency' in navigator &&
|
2815 | navigator.hardwareConcurrency > 0) {
|
2816 | util.cores = navigator.hardwareConcurrency;
|
2817 | return callback(null, util.cores);
|
2818 | }
|
2819 | if(typeof Worker === 'undefined') {
|
2820 |
|
2821 | util.cores = 1;
|
2822 | return callback(null, util.cores);
|
2823 | }
|
2824 | if(typeof Blob === 'undefined') {
|
2825 |
|
2826 | util.cores = 2;
|
2827 | return callback(null, util.cores);
|
2828 | }
|
2829 |
|
2830 |
|
2831 | var blobUrl = URL.createObjectURL(new Blob(['(',
|
2832 | function() {
|
2833 | self.addEventListener('message', function(e) {
|
2834 |
|
2835 | var st = Date.now();
|
2836 | var et = st + 4;
|
2837 | while(Date.now() < et);
|
2838 | self.postMessage({st: st, et: et});
|
2839 | });
|
2840 | }.toString(),
|
2841 | ')()'], {type: 'application/javascript'}));
|
2842 |
|
2843 |
|
2844 | sample([], 5, 16);
|
2845 |
|
2846 | function sample(max, samples, numWorkers) {
|
2847 | if(samples === 0) {
|
2848 |
|
2849 | var avg = Math.floor(max.reduce(function(avg, x) {
|
2850 | return avg + x;
|
2851 | }, 0) / max.length);
|
2852 | util.cores = Math.max(1, avg);
|
2853 | URL.revokeObjectURL(blobUrl);
|
2854 | return callback(null, util.cores);
|
2855 | }
|
2856 | map(numWorkers, function(err, results) {
|
2857 | max.push(reduce(numWorkers, results));
|
2858 | sample(max, samples - 1, numWorkers);
|
2859 | });
|
2860 | }
|
2861 |
|
2862 | function map(numWorkers, callback) {
|
2863 | var workers = [];
|
2864 | var results = [];
|
2865 | for(var i = 0; i < numWorkers; ++i) {
|
2866 | var worker = new Worker(blobUrl);
|
2867 | worker.addEventListener('message', function(e) {
|
2868 | results.push(e.data);
|
2869 | if(results.length === numWorkers) {
|
2870 | for(var i = 0; i < numWorkers; ++i) {
|
2871 | workers[i].terminate();
|
2872 | }
|
2873 | callback(null, results);
|
2874 | }
|
2875 | });
|
2876 | workers.push(worker);
|
2877 | }
|
2878 | for(var i = 0; i < numWorkers; ++i) {
|
2879 | workers[i].postMessage(i);
|
2880 | }
|
2881 | }
|
2882 |
|
2883 | function reduce(numWorkers, results) {
|
2884 |
|
2885 | var overlaps = [];
|
2886 | for(var n = 0; n < numWorkers; ++n) {
|
2887 | var r1 = results[n];
|
2888 | var overlap = overlaps[n] = [];
|
2889 | for(var i = 0; i < numWorkers; ++i) {
|
2890 | if(n === i) {
|
2891 | continue;
|
2892 | }
|
2893 | var r2 = results[i];
|
2894 | if((r1.st > r2.st && r1.st < r2.et) ||
|
2895 | (r2.st > r1.st && r2.st < r1.et)) {
|
2896 | overlap.push(i);
|
2897 | }
|
2898 | }
|
2899 | }
|
2900 |
|
2901 |
|
2902 |
|
2903 | return overlaps.reduce(function(max, overlap) {
|
2904 | return Math.max(max, overlap.length);
|
2905 | }, 0);
|
2906 | }
|
2907 | };
|