-
Notifications
You must be signed in to change notification settings - Fork 54
/
pvutils_bundle.js
791 lines (676 loc) · 26.3 KB
/
pvutils_bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pvutils = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getUTCDate = getUTCDate;
exports.getParametersValue = getParametersValue;
exports.bufferToHexCodes = bufferToHexCodes;
exports.checkBufferParams = checkBufferParams;
exports.utilFromBase = utilFromBase;
exports.utilToBase = utilToBase;
exports.utilConcatBuf = utilConcatBuf;
exports.utilConcatView = utilConcatView;
exports.utilDecodeTC = utilDecodeTC;
exports.utilEncodeTC = utilEncodeTC;
exports.isEqualBuffer = isEqualBuffer;
exports.padNumber = padNumber;
exports.toBase64 = toBase64;
exports.fromBase64 = fromBase64;
exports.arrayBufferToString = arrayBufferToString;
exports.stringToArrayBuffer = stringToArrayBuffer;
exports.nearestPowerOf2 = nearestPowerOf2;
exports.clearProps = clearProps;
//**************************************************************************************
/**
* Making UTC date from local date
* @param {Date} date Date to convert from
* @returns {Date}
*/
function getUTCDate(date) {
// noinspection NestedFunctionCallJS, MagicNumberJS
return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
}
//**************************************************************************************
// noinspection FunctionWithMultipleReturnPointsJS
/**
* Get value for input parameters, or set a default value
* @param {Object} parameters
* @param {string} name
* @param defaultValue
*/
function getParametersValue(parameters, name, defaultValue) {
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS
if (parameters instanceof Object === false) return defaultValue;
// noinspection NonBlockStatementBodyJS
if (name in parameters) return parameters[name];
return defaultValue;
}
//**************************************************************************************
/**
* Converts "ArrayBuffer" into a hexdecimal string
* @param {ArrayBuffer} inputBuffer
* @param {number} [inputOffset=0]
* @param {number} [inputLength=inputBuffer.byteLength]
* @param {boolean} [insertSpace=false]
* @returns {string}
*/
function bufferToHexCodes(inputBuffer, inputOffset = 0, inputLength = inputBuffer.byteLength - inputOffset, insertSpace = false) {
let result = "";
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = new Uint8Array(inputBuffer, inputOffset, inputLength)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
const item = _step.value;
// noinspection ChainedFunctionCallJS
const str = item.toString(16).toUpperCase();
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS
if (str.length === 1) result += "0";
result += str;
// noinspection NonBlockStatementBodyJS
if (insertSpace) result += " ";
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return result.trim();
}
//**************************************************************************************
// noinspection JSValidateJSDoc, FunctionWithMultipleReturnPointsJS
/**
* Check input "ArrayBuffer" for common functions
* @param {LocalBaseBlock} baseBlock
* @param {ArrayBuffer} inputBuffer
* @param {number} inputOffset
* @param {number} inputLength
* @returns {boolean}
*/
function checkBufferParams(baseBlock, inputBuffer, inputOffset, inputLength) {
// noinspection ConstantOnRightSideOfComparisonJS
if (inputBuffer instanceof ArrayBuffer === false) {
// noinspection JSUndefinedPropertyAssignment
baseBlock.error = "Wrong parameter: inputBuffer must be \"ArrayBuffer\"";
return false;
}
// noinspection ConstantOnRightSideOfComparisonJS
if (inputBuffer.byteLength === 0) {
// noinspection JSUndefinedPropertyAssignment
baseBlock.error = "Wrong parameter: inputBuffer has zero length";
return false;
}
// noinspection ConstantOnRightSideOfComparisonJS
if (inputOffset < 0) {
// noinspection JSUndefinedPropertyAssignment
baseBlock.error = "Wrong parameter: inputOffset less than zero";
return false;
}
// noinspection ConstantOnRightSideOfComparisonJS
if (inputLength < 0) {
// noinspection JSUndefinedPropertyAssignment
baseBlock.error = "Wrong parameter: inputLength less than zero";
return false;
}
// noinspection ConstantOnRightSideOfComparisonJS
if (inputBuffer.byteLength - inputOffset - inputLength < 0) {
// noinspection JSUndefinedPropertyAssignment
baseBlock.error = "End of input reached before message was fully decoded (inconsistent offset and length values)";
return false;
}
return true;
}
//**************************************************************************************
// noinspection FunctionWithMultipleReturnPointsJS
/**
* Convert number from 2^base to 2^10
* @param {Uint8Array} inputBuffer
* @param {number} inputBase
* @returns {number}
*/
function utilFromBase(inputBuffer, inputBase) {
let result = 0;
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS
if (inputBuffer.length === 1) return inputBuffer[0];
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS
for (let i = inputBuffer.length - 1; i >= 0; i--) result += inputBuffer[inputBuffer.length - 1 - i] * Math.pow(2, inputBase * i);
return result;
}
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS, FunctionWithMultipleReturnPointsJS
/**
* Convert number from 2^10 to 2^base
* @param {!number} value The number to convert
* @param {!number} base The base for 2^base
* @param {number} [reserved=0] Pre-defined number of bytes in output array (-1 = limited by function itself)
* @returns {ArrayBuffer}
*/
function utilToBase(value, base, reserved = -1) {
const internalReserved = reserved;
let internalValue = value;
let result = 0;
let biggest = Math.pow(2, base);
// noinspection ConstantOnRightSideOfComparisonJS
for (let i = 1; i < 8; i++) {
if (value < biggest) {
let retBuf;
// noinspection ConstantOnRightSideOfComparisonJS
if (internalReserved < 0) {
retBuf = new ArrayBuffer(i);
result = i;
} else {
// noinspection NonBlockStatementBodyJS
if (internalReserved < i) return new ArrayBuffer(0);
retBuf = new ArrayBuffer(internalReserved);
result = internalReserved;
}
const retView = new Uint8Array(retBuf);
// noinspection ConstantOnRightSideOfComparisonJS
for (let j = i - 1; j >= 0; j--) {
const basis = Math.pow(2, j * base);
retView[result - j - 1] = Math.floor(internalValue / basis);
internalValue -= retView[result - j - 1] * basis;
}
return retBuf;
}
biggest *= Math.pow(2, base);
}
return new ArrayBuffer(0);
}
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS
/**
* Concatenate two ArrayBuffers
* @param {...ArrayBuffer} buffers Set of ArrayBuffer
*/
function utilConcatBuf(...buffers) {
//region Initial variables
let outputLength = 0;
let prevLength = 0;
//endregion
//region Calculate output length
// noinspection NonBlockStatementBodyJS
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = buffers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
const buffer = _step2.value;
outputLength += buffer.byteLength;
} //endregion
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
const retBuf = new ArrayBuffer(outputLength);
const retView = new Uint8Array(retBuf);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = buffers[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
const buffer = _step3.value;
// noinspection NestedFunctionCallJS
retView.set(new Uint8Array(buffer), prevLength);
prevLength += buffer.byteLength;
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return retBuf;
}
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS
/**
* Concatenate two Uint8Array
* @param {...Uint8Array} views Set of Uint8Array
*/
function utilConcatView(...views) {
//region Initial variables
let outputLength = 0;
let prevLength = 0;
//endregion
//region Calculate output length
// noinspection NonBlockStatementBodyJS
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = views[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
const view = _step4.value;
outputLength += view.length;
} //endregion
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
const retBuf = new ArrayBuffer(outputLength);
const retView = new Uint8Array(retBuf);
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = views[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
const view = _step5.value;
retView.set(view, prevLength);
prevLength += view.length;
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
return retView;
}
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS
/**
* Decoding of "two complement" values
* The function must be called in scope of instance of "hexBlock" class ("valueHex" and "warnings" properties must be present)
* @returns {number}
*/
function utilDecodeTC() {
const buf = new Uint8Array(this.valueHex);
// noinspection ConstantOnRightSideOfComparisonJS
if (this.valueHex.byteLength >= 2) {
//noinspection JSBitwiseOperatorUsage, ConstantOnRightSideOfComparisonJS, LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
const condition1 = buf[0] === 0xFF && buf[1] & 0x80;
// noinspection ConstantOnRightSideOfComparisonJS, LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
const condition2 = buf[0] === 0x00 && (buf[1] & 0x80) === 0x00;
// noinspection NonBlockStatementBodyJS
if (condition1 || condition2) this.warnings.push("Needlessly long format");
}
//region Create big part of the integer
const bigIntBuffer = new ArrayBuffer(this.valueHex.byteLength);
const bigIntView = new Uint8Array(bigIntBuffer);
// noinspection NonBlockStatementBodyJS
for (let i = 0; i < this.valueHex.byteLength; i++) bigIntView[i] = 0;
// noinspection MagicNumberJS, NonShortCircuitBooleanExpressionJS
bigIntView[0] = buf[0] & 0x80; // mask only the biggest bit
const bigInt = utilFromBase(bigIntView, 8);
//endregion
//region Create small part of the integer
const smallIntBuffer = new ArrayBuffer(this.valueHex.byteLength);
const smallIntView = new Uint8Array(smallIntBuffer);
// noinspection NonBlockStatementBodyJS
for (let j = 0; j < this.valueHex.byteLength; j++) smallIntView[j] = buf[j];
// noinspection MagicNumberJS
smallIntView[0] &= 0x7F; // mask biggest bit
const smallInt = utilFromBase(smallIntView, 8);
//endregion
return smallInt - bigInt;
}
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS, FunctionWithMultipleReturnPointsJS
/**
* Encode integer value to "two complement" format
* @param {number} value Value to encode
* @returns {ArrayBuffer}
*/
function utilEncodeTC(value) {
// noinspection ConstantOnRightSideOfComparisonJS, ConditionalExpressionJS
const modValue = value < 0 ? value * -1 : value;
let bigInt = 128;
// noinspection ConstantOnRightSideOfComparisonJS
for (let i = 1; i < 8; i++) {
if (modValue <= bigInt) {
// noinspection ConstantOnRightSideOfComparisonJS
if (value < 0) {
const smallInt = bigInt - modValue;
const retBuf = utilToBase(smallInt, 8, i);
const retView = new Uint8Array(retBuf);
// noinspection MagicNumberJS
retView[0] |= 0x80;
return retBuf;
}
let retBuf = utilToBase(modValue, 8, i);
let retView = new Uint8Array(retBuf);
//noinspection JSBitwiseOperatorUsage, MagicNumberJS, NonShortCircuitBooleanExpressionJS
if (retView[0] & 0x80) {
//noinspection JSCheckFunctionSignatures
const tempBuf = retBuf.slice(0);
const tempView = new Uint8Array(tempBuf);
retBuf = new ArrayBuffer(retBuf.byteLength + 1);
// noinspection ReuseOfLocalVariableJS
retView = new Uint8Array(retBuf);
// noinspection NonBlockStatementBodyJS
for (let k = 0; k < tempBuf.byteLength; k++) retView[k + 1] = tempView[k];
// noinspection MagicNumberJS
retView[0] = 0x00;
}
return retBuf;
}
bigInt *= Math.pow(2, 8);
}
return new ArrayBuffer(0);
}
//**************************************************************************************
// noinspection FunctionWithMultipleReturnPointsJS, ParameterNamingConventionJS
/**
* Compare two array buffers
* @param {!ArrayBuffer} inputBuffer1
* @param {!ArrayBuffer} inputBuffer2
* @returns {boolean}
*/
function isEqualBuffer(inputBuffer1, inputBuffer2) {
// noinspection NonBlockStatementBodyJS
if (inputBuffer1.byteLength !== inputBuffer2.byteLength) return false;
// noinspection LocalVariableNamingConventionJS
const view1 = new Uint8Array(inputBuffer1);
// noinspection LocalVariableNamingConventionJS
const view2 = new Uint8Array(inputBuffer2);
for (let i = 0; i < view1.length; i++) {
// noinspection NonBlockStatementBodyJS
if (view1[i] !== view2[i]) return false;
}
return true;
}
//**************************************************************************************
// noinspection FunctionWithMultipleReturnPointsJS
/**
* Pad input number with leade "0" if needed
* @returns {string}
* @param {number} inputNumber
* @param {number} fullLength
*/
function padNumber(inputNumber, fullLength) {
const str = inputNumber.toString(10);
// noinspection NonBlockStatementBodyJS
if (fullLength < str.length) return "";
const dif = fullLength - str.length;
const padding = new Array(dif);
// noinspection NonBlockStatementBodyJS
for (let i = 0; i < dif; i++) padding[i] = "0";
const paddingString = padding.join("");
return paddingString.concat(str);
}
//**************************************************************************************
const base64Template = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const base64UrlTemplate = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
//**************************************************************************************
// noinspection FunctionWithMultipleLoopsJS, OverlyComplexFunctionJS, FunctionTooLongJS, FunctionNamingConventionJS
/**
* Encode string into BASE64 (or "base64url")
* @param {string} input
* @param {boolean} useUrlTemplate If "true" then output would be encoded using "base64url"
* @param {boolean} skipPadding Skip BASE-64 padding or not
* @param {boolean} skipLeadingZeros Skip leading zeros in input data or not
* @returns {string}
*/
function toBase64(input, useUrlTemplate = false, skipPadding = false, skipLeadingZeros = false) {
let i = 0;
// noinspection LocalVariableNamingConventionJS
let flag1 = 0;
// noinspection LocalVariableNamingConventionJS
let flag2 = 0;
let output = "";
// noinspection ConditionalExpressionJS
const template = useUrlTemplate ? base64UrlTemplate : base64Template;
if (skipLeadingZeros) {
let nonZeroPosition = 0;
for (let i = 0; i < input.length; i++) {
// noinspection ConstantOnRightSideOfComparisonJS
if (input.charCodeAt(i) !== 0) {
nonZeroPosition = i;
// noinspection BreakStatementJS
break;
}
}
// noinspection AssignmentToFunctionParameterJS
input = input.slice(nonZeroPosition);
}
while (i < input.length) {
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS
const chr1 = input.charCodeAt(i++);
// noinspection NonBlockStatementBodyJS
if (i >= input.length) flag1 = 1;
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS
const chr2 = input.charCodeAt(i++);
// noinspection NonBlockStatementBodyJS
if (i >= input.length) flag2 = 1;
// noinspection LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS
const chr3 = input.charCodeAt(i++);
// noinspection LocalVariableNamingConventionJS
const enc1 = chr1 >> 2;
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
const enc2 = (chr1 & 0x03) << 4 | chr2 >> 4;
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
let enc3 = (chr2 & 0x0F) << 2 | chr3 >> 6;
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
let enc4 = chr3 & 0x3F;
// noinspection ConstantOnRightSideOfComparisonJS
if (flag1 === 1) {
// noinspection NestedAssignmentJS, AssignmentResultUsedJS, MagicNumberJS
enc3 = enc4 = 64;
} else {
// noinspection ConstantOnRightSideOfComparisonJS
if (flag2 === 1) {
// noinspection MagicNumberJS
enc4 = 64;
}
}
// noinspection NonBlockStatementBodyJS
if (skipPadding) {
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS
if (enc3 === 64) output += `${template.charAt(enc1)}${template.charAt(enc2)}`;else {
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS
if (enc4 === 64) output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}`;else output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`;
}
} else output += `${template.charAt(enc1)}${template.charAt(enc2)}${template.charAt(enc3)}${template.charAt(enc4)}`;
}
return output;
}
//**************************************************************************************
// noinspection FunctionWithMoreThanThreeNegationsJS, FunctionWithMultipleLoopsJS, OverlyComplexFunctionJS, FunctionNamingConventionJS
/**
* Decode string from BASE64 (or "base64url")
* @param {string} input
* @param {boolean} [useUrlTemplate=false] If "true" then output would be encoded using "base64url"
* @param {boolean} [cutTailZeros=false] If "true" then cut tailing zeroz from function result
* @returns {string}
*/
function fromBase64(input, useUrlTemplate = false, cutTailZeros = false) {
// noinspection ConditionalExpressionJS
const template = useUrlTemplate ? base64UrlTemplate : base64Template;
//region Aux functions
// noinspection FunctionWithMultipleReturnPointsJS, NestedFunctionJS
function indexof(toSearch) {
// noinspection ConstantOnRightSideOfComparisonJS, MagicNumberJS
for (let i = 0; i < 64; i++) {
// noinspection NonBlockStatementBodyJS
if (template.charAt(i) === toSearch) return i;
}
// noinspection MagicNumberJS
return 64;
}
// noinspection NestedFunctionJS
function test(incoming) {
// noinspection ConstantOnRightSideOfComparisonJS, ConditionalExpressionJS, MagicNumberJS
return incoming === 64 ? 0x00 : incoming;
}
//endregion
let i = 0;
let output = "";
while (i < input.length) {
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, IncrementDecrementResultUsedJS
const enc1 = indexof(input.charAt(i++));
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS
const enc2 = i >= input.length ? 0x00 : indexof(input.charAt(i++));
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS
const enc3 = i >= input.length ? 0x00 : indexof(input.charAt(i++));
// noinspection NestedFunctionCallJS, LocalVariableNamingConventionJS, ConditionalExpressionJS, MagicNumberJS, IncrementDecrementResultUsedJS
const enc4 = i >= input.length ? 0x00 : indexof(input.charAt(i++));
// noinspection LocalVariableNamingConventionJS, NonShortCircuitBooleanExpressionJS
const chr1 = test(enc1) << 2 | test(enc2) >> 4;
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
const chr2 = (test(enc2) & 0x0F) << 4 | test(enc3) >> 2;
// noinspection LocalVariableNamingConventionJS, MagicNumberJS, NonShortCircuitBooleanExpressionJS
const chr3 = (test(enc3) & 0x03) << 6 | test(enc4);
output += String.fromCharCode(chr1);
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS
if (enc3 !== 64) output += String.fromCharCode(chr2);
// noinspection ConstantOnRightSideOfComparisonJS, NonBlockStatementBodyJS, MagicNumberJS
if (enc4 !== 64) output += String.fromCharCode(chr3);
}
if (cutTailZeros) {
const outputLength = output.length;
let nonZeroStart = -1;
// noinspection ConstantOnRightSideOfComparisonJS
for (let i = outputLength - 1; i >= 0; i--) {
// noinspection ConstantOnRightSideOfComparisonJS
if (output.charCodeAt(i) !== 0) {
nonZeroStart = i;
// noinspection BreakStatementJS
break;
}
}
// noinspection NonBlockStatementBodyJS, NegatedIfStatementJS
if (nonZeroStart !== -1) output = output.slice(0, nonZeroStart + 1);else output = "";
}
return output;
}
//**************************************************************************************
function arrayBufferToString(buffer) {
let resultString = "";
const view = new Uint8Array(buffer);
// noinspection NonBlockStatementBodyJS
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = view[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
const element = _step6.value;
resultString += String.fromCharCode(element);
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
return resultString;
}
//**************************************************************************************
function stringToArrayBuffer(str) {
const stringLength = str.length;
const resultBuffer = new ArrayBuffer(stringLength);
const resultView = new Uint8Array(resultBuffer);
// noinspection NonBlockStatementBodyJS
for (let i = 0; i < stringLength; i++) resultView[i] = str.charCodeAt(i);
return resultBuffer;
}
//**************************************************************************************
const log2 = Math.log(2);
//**************************************************************************************
// noinspection FunctionNamingConventionJS
/**
* Get nearest to input length power of 2
* @param {number} length Current length of existing array
* @returns {number}
*/
function nearestPowerOf2(length) {
const base = Math.log(length) / log2;
const floor = Math.floor(base);
const round = Math.round(base);
// noinspection ConditionalExpressionJS
return floor === round ? floor : round;
}
//**************************************************************************************
/**
* Delete properties by name from specified object
* @param {Object} object Object to delete properties from
* @param {Array.<string>} propsArray Array of properties names
*/
function clearProps(object, propsArray) {
var _iteratorNormalCompletion7 = true;
var _didIteratorError7 = false;
var _iteratorError7 = undefined;
try {
for (var _iterator7 = propsArray[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
const prop = _step7.value;
delete object[prop];
}
} catch (err) {
_didIteratorError7 = true;
_iteratorError7 = err;
} finally {
try {
if (!_iteratorNormalCompletion7 && _iterator7.return) {
_iterator7.return();
}
} finally {
if (_didIteratorError7) {
throw _iteratorError7;
}
}
}
}
//**************************************************************************************
},{}],2:[function(require,module,exports){
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const pvutils = require("pvutils"); // version 1.0.17
module.exports = {
pvutils,
};
},{"pvutils":1}]},{},[2])(2)
});