@sswroom/sswr 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/text.js CHANGED
@@ -1,6 +1,21 @@
1
+ import * as data from "./data.js";
2
+ import * as net from "./net.js";
3
+
4
+ export const Base64Charset = {
5
+ Normal: 1,
6
+ URL: 2
7
+ };
8
+
9
+ export const LineBreakType = {
10
+ None: "",
11
+ CR: "\r",
12
+ LF: "\n",
13
+ CRLF: "\r\n"
14
+ };
15
+
1
16
  export function zeroPad(val, ndigits)
2
17
  {
3
- var s = "" + val;
18
+ let s = "" + val;
4
19
  while (s.length < ndigits)
5
20
  s = "0"+s;
6
21
  return s;
@@ -12,11 +27,11 @@ export function isInteger(s)
12
27
  return false;
13
28
  if (s.startsWith("-"))
14
29
  s = s.substring(1);
15
- var j = s.length;
30
+ let j = s.length;
16
31
  if (j == 0)
17
32
  return false;
18
- var i = 0;
19
- var c;
33
+ let i = 0;
34
+ let c;
20
35
  while (i < j)
21
36
  {
22
37
  c = s.charCodeAt(i);
@@ -29,11 +44,11 @@ export function isInteger(s)
29
44
 
30
45
  export function toJSText(s)
31
46
  {
32
- var out = "\"";
33
- var i = 0;
34
- var j = s.length;
35
- var c;
36
- var ccode;
47
+ let out = "\"";
48
+ let i = 0;
49
+ let j = s.length;
50
+ let c;
51
+ let ccode;
37
52
  while (i < j)
38
53
  {
39
54
  c = s.charAt(i);
@@ -73,10 +88,10 @@ export function toJSText(s)
73
88
  export function toXMLText(s)
74
89
  {
75
90
  if (s == null) return "";
76
- var out = "";
77
- var i = 0;
78
- var j = s.length;
79
- var c;
91
+ let out = "";
92
+ let i = 0;
93
+ let j = s.length;
94
+ let c;
80
95
  while (i < j)
81
96
  {
82
97
  c = s.charAt(i);
@@ -121,10 +136,10 @@ export function toAttrText(s)
121
136
  export function toHTMLText(s)
122
137
  {
123
138
  if (s == null) return "";
124
- var out = "";
125
- var i = 0;
126
- var j = s.length;
127
- var c;
139
+ let out = "";
140
+ let i = 0;
141
+ let j = s.length;
142
+ let c;
128
143
  while (i < j)
129
144
  {
130
145
  c = s.charAt(i);
@@ -161,8 +176,8 @@ export function toHTMLText(s)
161
176
 
162
177
  export function bracketToHTML(s)
163
178
  {
164
- var i;
165
- var j;
179
+ let i;
180
+ let j;
166
181
  while (true)
167
182
  {
168
183
  i = s.indexOf("[i]");
@@ -178,11 +193,1117 @@ export function bracketToHTML(s)
178
193
 
179
194
  export function arrayToNumbers(arr)
180
195
  {
181
- var ret = [];
182
- var i;
196
+ let ret = [];
197
+ let i;
183
198
  for (i in arr)
184
199
  {
185
200
  ret.push(Number.parseFloat(arr[i]));
186
201
  }
187
202
  return ret;
188
203
  }
204
+
205
+ export function toHex8(v)
206
+ {
207
+ let s = (v & 255).toString(16);
208
+ return "0".repeat(2 - s.length)+s;
209
+ }
210
+
211
+ export function toHex16(v)
212
+ {
213
+ let s = (v & 0xffff).toString(16);
214
+ return "0".repeat(4 - s.length)+s;
215
+ }
216
+
217
+ export function toHex32(v)
218
+ {
219
+ let s = (v & 0xffffffff).toString(16);
220
+ return "0".repeat(8 - s.length)+s;
221
+ }
222
+
223
+ export function getEncList()
224
+ {
225
+ let ret = [];
226
+ ret.push(new Base64Enc());
227
+ ret.push(new UTF8TextBinEnc());
228
+ ret.push(new UTF8LCaseTextBinEnc());
229
+ ret.push(new UTF8UCaseTextBinEnc());
230
+ ret.push(new CPPByteArrBinEnc());
231
+ ret.push(new CPPTextBinEnc());
232
+ ret.push(new HexTextBinEnc());
233
+ ret.push(new QuotedPrintableEnc());
234
+ ret.push(new UTF16LETextBinEnc());
235
+ ret.push(new UTF16BETextBinEnc());
236
+ ret.push(new ASN1OIDBinEnc());
237
+ ret.push(new FormEncoding());
238
+ ret.push(new URIEncoding());
239
+ ret.push(new ASCII85Enc());
240
+ return ret;
241
+ }
242
+
243
+ export class TextBinEnc
244
+ {
245
+ constructor(name)
246
+ {
247
+ this.name = name;
248
+ }
249
+
250
+ getName()
251
+ {
252
+ return this.name;
253
+ }
254
+
255
+ getClassName()
256
+ {
257
+ return this.constructor.name;
258
+ }
259
+ }
260
+
261
+ export class UTF8TextBinEnc extends TextBinEnc
262
+ {
263
+ constructor()
264
+ {
265
+ super("UTF-8 Text");
266
+ }
267
+
268
+ encodeBin(buff)
269
+ {
270
+ let enc = new TextDecoder();
271
+ return enc.decode(buff);
272
+ }
273
+
274
+ decodeBin(str)
275
+ {
276
+ let enc = new TextEncoder();
277
+ return enc.encode(str);
278
+ }
279
+ }
280
+
281
+ export class UTF8LCaseTextBinEnc extends TextBinEnc
282
+ {
283
+ constructor()
284
+ {
285
+ super("UTF-8 LCase Text");
286
+ }
287
+
288
+ encodeBin(buff)
289
+ {
290
+ let enc = new TextDecoder();
291
+ return enc.decode(buff).toLowerCase();
292
+ }
293
+
294
+ decodeBin(str)
295
+ {
296
+ let enc = new TextEncoder();
297
+ return enc.encode(str);
298
+ }
299
+ }
300
+
301
+ export class UTF8UCaseTextBinEnc extends TextBinEnc
302
+ {
303
+ constructor()
304
+ {
305
+ super("UTF-8 UCase Text");
306
+ }
307
+
308
+ encodeBin(buff)
309
+ {
310
+ let enc = new TextDecoder();
311
+ return enc.decode(buff).toUpperCase();
312
+ }
313
+
314
+ decodeBin(str)
315
+ {
316
+ let enc = new TextEncoder();
317
+ return enc.encode(str);
318
+ }
319
+ }
320
+
321
+ export class HexTextBinEnc extends TextBinEnc
322
+ {
323
+ constructor()
324
+ {
325
+ super("Hex");
326
+ }
327
+
328
+ encodeBin(buff)
329
+ {
330
+ let arr = new Uint8Array(buff);
331
+ let ret = [];
332
+ let i;
333
+ for (i in arr)
334
+ {
335
+ ret.push(toHex8(arr[i]));
336
+ }
337
+ return ret.join(" ");
338
+ }
339
+
340
+ decodeBin(str)
341
+ {
342
+ let arr = [];
343
+ let i = 0;
344
+ let j = str.length;
345
+ let v = 0;
346
+ let found = 0;
347
+ let c;
348
+ while (i < j)
349
+ {
350
+ c = str.charCodeAt(i);
351
+ if (c >= 0x30 && c <= 0x39)
352
+ {
353
+ v = (v << 4) + (c - 0x30);
354
+ found++;
355
+ }
356
+ else if (c >= 0x41 && c <= 0x46)
357
+ {
358
+ v = (v << 4) + (c - 0x37);
359
+ found++;
360
+ }
361
+ else if (c >= 0x61 && c <= 0x66)
362
+ {
363
+ v = (v << 4) + (c - 0x57);
364
+ found++;
365
+ }
366
+ if (found == 2)
367
+ {
368
+ arr.push(v);
369
+ v = 0;
370
+ found = 0;
371
+ }
372
+
373
+ i++;
374
+ }
375
+ return new Uint8Array(arr);
376
+ }
377
+ }
378
+
379
+ const Base64Enc_decArr = [
380
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
381
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
382
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0x3e, 0xff, 0x3f,
383
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
384
+ 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
385
+ 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0x3f,
386
+ 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
387
+ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
388
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
389
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
390
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
391
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
392
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
393
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
394
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
395
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
396
+ ];
397
+ export class Base64Enc extends TextBinEnc
398
+ {
399
+ static getEncArr(charset)
400
+ {
401
+ if (charset == Base64Charset.URL)
402
+ {
403
+ return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
404
+ }
405
+ else
406
+ {
407
+ return "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
408
+ }
409
+
410
+ }
411
+
412
+ constructor(charset, noPadding)
413
+ {
414
+ super("Base64");
415
+ this.encArr = Base64Enc.getEncArr(charset);
416
+ this.noPadding = noPadding || null;
417
+ }
418
+
419
+ encodeBin(buff, lbt, charsPerLine)
420
+ {
421
+ let arr = new Uint8Array(buff);
422
+ if (lbt == null)
423
+ lbt = LineBreakType.None;
424
+ let tmp1 = arr.length % 3;
425
+ let tmp2 = Math.floor(arr.length / 3);
426
+ if (lbt == LineBreakType.None || !charsPerLine)
427
+ {
428
+ let ret = [];
429
+ let i = 0;
430
+ while (tmp2-- > 0)
431
+ {
432
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
433
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
434
+ ret.push(this.encArr.charAt(((arr[i + 1] << 2) | (arr[i + 2] >> 6)) & 0x3f));
435
+ ret.push(this.encArr.charAt(arr[i + 2] & 0x3f));
436
+ i += 3;
437
+ }
438
+ if (tmp1 == 1)
439
+ {
440
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
441
+ ret.push(this.encArr.charAt((arr[i + 0] << 4) & 0x3f));
442
+ if (this.noPadding)
443
+ {
444
+ }
445
+ else
446
+ {
447
+ ret.push("==");
448
+ }
449
+ }
450
+ else if (tmp1 == 2)
451
+ {
452
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
453
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
454
+ ret.push(this.encArr.charAt((arr[i + 1] << 2) & 0x3f));
455
+ if (this.noPadding)
456
+ {
457
+ }
458
+ else
459
+ {
460
+ ret.push("=");
461
+ }
462
+ }
463
+ return ret.join("");
464
+ }
465
+ else
466
+ {
467
+ let ret = [];
468
+ let i = 0;
469
+ let lineLeft = charsPerLine;
470
+ while (tmp2-- > 0)
471
+ {
472
+ if (lineLeft >= 4)
473
+ {
474
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
475
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
476
+ ret.push(this.encArr.charAt(((arr[i + 1] << 2) | (arr[i + 2] >> 6)) & 0x3f));
477
+ ret.push(this.encArr.charAt(arr[i + 2] & 0x3f));
478
+ if (lineLeft == 0)
479
+ {
480
+ if (tmp2 > 0 || tmp1 != 0)
481
+ ret.push(lbt);
482
+ lineLeft = charsPerLine;
483
+ }
484
+ }
485
+ else if (lineLeft == 3)
486
+ {
487
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
488
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
489
+ ret.push(this.encArr.charAt(((arr[i + 1] << 2) | (arr[i + 2] >> 6)) & 0x3f));
490
+ ret.push(lbt);
491
+ ret.push(this.encArr.charAt(arr[i + 2] & 0x3f));
492
+ lineLeft = charsPerLine - 1;
493
+ }
494
+ else if (lineLeft == 2)
495
+ {
496
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
497
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
498
+ ret.push(lbt);
499
+ ret.push(this.encArr.charAt(((arr[i + 1] << 2) | (arr[i + 2] >> 6)) & 0x3f));
500
+ ret.push(this.encArr.charAt(arr[i + 2] & 0x3f));
501
+ lineLeft = charsPerLine - 2;
502
+ }
503
+ else
504
+ {
505
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
506
+ ret.push(lbt);
507
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
508
+ ret.push(this.encArr.charAt(((arr[i + 1] << 2) | (arr[i + 2] >> 6)) & 0x3f));
509
+ ret.push(this.encArr.charAt(arr[i + 2] & 0x3f));
510
+ lineLeft = charsPerLine - 3;
511
+ }
512
+ i += 3;
513
+ }
514
+ if (tmp1 == 1)
515
+ {
516
+ if (lineLeft >= 2)
517
+ {
518
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
519
+ ret.push(this.encArr.charAt((arr[i + 0] << 4) & 0x3f));
520
+ if (this.noPadding)
521
+ {
522
+ }
523
+ else if (lineLeft == 2)
524
+ {
525
+ ret.push(lbt);
526
+ ret.push("==");
527
+ }
528
+ else if (lineLeft == 3)
529
+ {
530
+ ret.push("=");
531
+ ret.push(lbt);
532
+ ret.push("=");
533
+ }
534
+ else
535
+ {
536
+ ret.push("==");
537
+ }
538
+ }
539
+ else
540
+ {
541
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
542
+ ret.push(lbt);
543
+ ret.push(this.encArr.charAt((arr[i + 0] << 4) & 0x3f));
544
+ if (this.noPadding)
545
+ {
546
+ }
547
+ else
548
+ {
549
+ ret.push("==");
550
+ }
551
+ }
552
+ }
553
+ else if (tmp1 == 2)
554
+ {
555
+ if (lineLeft >= 3)
556
+ {
557
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
558
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
559
+ ret.push(this.encArr.charAt((arr[i + 1] << 2) & 0x3f));
560
+ if (this.noPadding)
561
+ {
562
+ }
563
+ else if (lineLeft == 3)
564
+ {
565
+ ret.push(lbt);
566
+ ret.push("=");
567
+ }
568
+ else
569
+ {
570
+ ret.push("=");
571
+ }
572
+ }
573
+ else
574
+ {
575
+ if (lineLeft == 2)
576
+ {
577
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
578
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
579
+ ret.push(lbt);
580
+ ret.push(this.encArr.charAt((arr[i + 1] << 2) & 0x3f));
581
+ }
582
+ else
583
+ {
584
+ ret.push(this.encArr.charAt(arr[i + 0] >> 2));
585
+ ret.push(lbt);
586
+ ret.push(this.encArr.charAt(((arr[i + 0] << 4) | (arr[i + 1] >> 4)) & 0x3f));
587
+ ret.push(this.encArr.charAt((arr[i + 1] << 2) & 0x3f));
588
+ }
589
+ if (this.noPadding)
590
+ {
591
+ }
592
+ else
593
+ {
594
+ ret.push("=");
595
+ }
596
+ }
597
+ }
598
+ return ret.join("");
599
+ }
600
+ }
601
+
602
+ decodeBin(str)
603
+ {
604
+ let ret = [];
605
+ let i = 0;
606
+ let j = str.length;
607
+ let b = 0;
608
+ let b2;
609
+ let c;
610
+ let code;
611
+ while (i < j)
612
+ {
613
+ c = str.charCodeAt(i);
614
+ if (c < 0x80)
615
+ {
616
+ code = Base64Enc_decArr[c];
617
+ if (code != 0xff)
618
+ {
619
+ switch (b)
620
+ {
621
+ case 0:
622
+ b2 = (code << 2);
623
+ b = 1;
624
+ break;
625
+ case 1:
626
+ ret.push(b2 | (code >> 4));
627
+ b2 = (code << 4);
628
+ b = 2;
629
+ break;
630
+ case 2:
631
+ ret.push(b2 | (code >> 2));
632
+ b2 = (code << 6);
633
+ b = 3;
634
+ break;
635
+ case 3:
636
+ ret.push(b2 | code);
637
+ b = 0;
638
+ break;
639
+ }
640
+ }
641
+ }
642
+ i++;
643
+ }
644
+ return new Uint8Array(ret);
645
+ }
646
+ }
647
+
648
+ export class UTF16LETextBinEnc extends TextBinEnc
649
+ {
650
+ constructor()
651
+ {
652
+ super("UTF16LE Text");
653
+ }
654
+
655
+ encodeBin(buff)
656
+ {
657
+ let ret = [];
658
+ let i = 0;
659
+ let j = buff.length;
660
+ if (j & 1)
661
+ j--;
662
+ while (i < j)
663
+ {
664
+ ret.push(String.fromCharCode(buff[i] + (buff[i + 1] << 8)));
665
+ i += 2;
666
+ }
667
+ return ret.join("");
668
+ }
669
+
670
+ decodeBin(str)
671
+ {
672
+ let ret = [];
673
+ let c;
674
+ let i = 0;
675
+ let j = str.length;
676
+ while (i < j)
677
+ {
678
+ c = str.charCodeAt(i);
679
+ ret.push(c & 255);
680
+ ret.push(c >> 8);
681
+ i++;
682
+ }
683
+ return new Uint8Array(ret);
684
+ }
685
+ }
686
+
687
+ export class UTF16BETextBinEnc extends TextBinEnc
688
+ {
689
+ constructor()
690
+ {
691
+ super("UTF16BE Text");
692
+ }
693
+
694
+ encodeBin(buff)
695
+ {
696
+ let ret = [];
697
+ let i = 0;
698
+ let j = buff.length;
699
+ if (j & 1)
700
+ j--;
701
+ while (i < j)
702
+ {
703
+ ret.push(String.fromCharCode(buff[i + 1] + (buff[i] << 8)));
704
+ i += 2;
705
+ }
706
+ return ret.join("");
707
+ }
708
+
709
+ decodeBin(str)
710
+ {
711
+ let ret = [];
712
+ let c;
713
+ let i = 0;
714
+ let j = str.length;
715
+ while (i < j)
716
+ {
717
+ c = str.charCodeAt(i);
718
+ ret.push(c >> 8);
719
+ ret.push(c & 255);
720
+ i++;
721
+ }
722
+ return new Uint8Array(ret);
723
+ }
724
+ }
725
+
726
+ export class CPPByteArrBinEnc extends TextBinEnc
727
+ {
728
+ constructor()
729
+ {
730
+ super("CPP Byte Arr");
731
+ }
732
+
733
+ encodeBin(buff)
734
+ {
735
+ let lines = [];
736
+ let cols = [];
737
+ let arr = new Uint8Array(buff);
738
+ let i = 0;
739
+ let j = arr.length;
740
+ while (i < j)
741
+ {
742
+ if (i > 0 && (i & 15) == 0)
743
+ {
744
+ lines.push(cols.join(", "));
745
+ cols = [];
746
+ }
747
+ cols.push("0x"+toHex8(arr[i]));
748
+ i++;
749
+ }
750
+ if (cols.length > 0)
751
+ {
752
+ lines.push(cols.join(", "));
753
+ }
754
+ return lines.join(",\r\n");
755
+ }
756
+
757
+ decodeBin(str)
758
+ {
759
+ let ret = [];
760
+ let i = -1;
761
+ let j;
762
+ let s;
763
+ while (true)
764
+ {
765
+ j = str.indexOf(",", i + 1);
766
+ if (j < 0)
767
+ {
768
+ s = str.substring(i + 1).trim();
769
+ }
770
+ else
771
+ {
772
+ s = str.substring(i + 1, j).trim();
773
+ }
774
+ if (s.startsWith("0x"))
775
+ ret.push(Number.parseInt(s.substring(2), 16));
776
+ else
777
+ ret.push(Number.parseInt(s));
778
+ if (j < 0)
779
+ break;
780
+ else
781
+ i = j;
782
+ }
783
+ return new Uint8Array(ret);
784
+ }
785
+ }
786
+
787
+ export class CPPTextBinEnc extends TextBinEnc
788
+ {
789
+ constructor()
790
+ {
791
+ super("CPP String");
792
+ }
793
+
794
+ encodeBin(buff)
795
+ {
796
+ let arr = new Uint8Array(buff);
797
+ let ret = [];
798
+ let lineStart = true;
799
+ let code;
800
+ let b;
801
+ let i = 0;
802
+ let j = arr.length;
803
+ while (i < j)
804
+ {
805
+ b = arr[i];
806
+ if (lineStart)
807
+ {
808
+ ret.push("\"");
809
+ lineStart = false;
810
+ }
811
+ if (b == 0)
812
+ {
813
+ ret.push("\\0");
814
+ }
815
+ else if (b == 13)
816
+ {
817
+ ret.push("\\r");
818
+ }
819
+ else if (b == 10)
820
+ {
821
+ ret.push("\\n\"\r\n");
822
+ lineStart = true;
823
+ }
824
+ else if (b == 0x5c)
825
+ {
826
+ ret.push("\\\\");
827
+ }
828
+ else if (b == 0x22)
829
+ {
830
+ ret.push("\\\"");
831
+ }
832
+ else if (b < 0x80)
833
+ {
834
+ ret.push(String.fromCharCode(b));
835
+ }
836
+ else if ((b & 0xe0) == 0xc0)
837
+ {
838
+ ret.push(String.fromCharCode((((b & 0x1f) << 6) | (arr[i + 1] & 0x3f))));
839
+ i++;
840
+ }
841
+ else if ((b & 0xf0) == 0xe0)
842
+ {
843
+ ret.push(String.fromCharCode(((b & 0x0f) << 12) | ((arr[i + 1] & 0x3f) << 6) | (arr[i + 2] & 0x3f)));
844
+ i += 2;
845
+ }
846
+ else if ((b & 0xf8) == 0xf0)
847
+ {
848
+ code = (((b & 0x7) << 18) | ((arr[i + 1] & 0x3f) << 12) | ((arr[i + 2] & 0x3f) << 6) | (arr[i + 3] & 0x3f));
849
+ if (code >= 0x10000)
850
+ {
851
+ ret.push(String.fromCharCode(((code - 0x10000) >> 10) + 0xd800, (code & 0x3ff) + 0xdc00));
852
+ }
853
+ else
854
+ {
855
+ ret.push(String.fromCharCode(code));
856
+ }
857
+ i += 3;
858
+ }
859
+ else if ((b & 0xfc) == 0xf8)
860
+ {
861
+ code = (((b & 0x3) << 24) | ((arr[i + 1] & 0x3f) << 18) | ((arr[i + 2] & 0x3f) << 12) | ((arr[i + 3] & 0x3f) << 6) | (arr[i + 4] & 0x3f));
862
+ if (code >= 0x10000)
863
+ {
864
+ ret.push(String.fromCharCode(((code - 0x10000) >> 10) + 0xd800, (code & 0x3ff) + 0xdc00));
865
+ }
866
+ else
867
+ {
868
+ ret.push(String.fromCharCode(code));
869
+ }
870
+ i += 4;
871
+ }
872
+ else if ((b & 0xfe) == 0xfc)
873
+ {
874
+ code = (UInt32)(((b & 0x1) << 30) | ((arr[i + 1] & 0x3f) << 24) | ((arr[i + 2] & 0x3f) << 18) | ((arr[i + 3] & 0x3f) << 12) | ((arr[i + 4] & 0x3f) << 6) | (arr[i + 5] & 0x3f));
875
+ if (code >= 0x10000)
876
+ {
877
+ ret.push(String.fromCharCode(((code - 0x10000) >> 10) + 0xd800, (code & 0x3ff) + 0xdc00));
878
+ }
879
+ else
880
+ {
881
+ ret.push(String.fromCharCode(code));
882
+ }
883
+ i += 5;
884
+ }
885
+ i++;
886
+ }
887
+ if (!lineStart)
888
+ {
889
+ ret.push('"');
890
+ }
891
+ return ret.join("");
892
+ }
893
+
894
+ decodeBin(str)
895
+ {
896
+ let isQuote = false;
897
+ let ret = [];
898
+ let i = 0;
899
+ let j = str.length;
900
+ let c;
901
+ while (true)
902
+ {
903
+ if (i >= j)
904
+ {
905
+ if (isQuote)
906
+ {
907
+ throw new Error("Unexpected end of string");
908
+ }
909
+ break;
910
+ }
911
+ c = str.charCodeAt(i++);
912
+ if (!isQuote)
913
+ {
914
+ if (c == 0x22)
915
+ {
916
+ isQuote = true;
917
+ }
918
+ else if (c == 0x20 || c == '\t'.charCodeAt(0) || c == 13 || c == 10)
919
+ {
920
+ }
921
+ else
922
+ {
923
+ throw new Error("Only allow white space characters when it is not quoted");
924
+ }
925
+ }
926
+ else if (c == 0x5c)
927
+ {
928
+ if (i >= j)
929
+ throw new Error("Unexpected end of string after \\");
930
+ c = str.charAt(i++);
931
+ if (c == 'r')
932
+ {
933
+ ret.push(13);
934
+ }
935
+ else if (c == 'n')
936
+ {
937
+ ret.push(10);
938
+ }
939
+ else if (c == 't')
940
+ {
941
+ ret.push(9);
942
+ }
943
+ else if (c == '\\')
944
+ {
945
+ ret.push(0x5c);
946
+ }
947
+ else if (c == '"')
948
+ {
949
+ ret.push(0x22);
950
+ }
951
+ else if (c == '0')
952
+ {
953
+ ret.push(0);
954
+ }
955
+ else
956
+ {
957
+ throw new Error("Unsupported escape sequence \\"+c);
958
+ }
959
+ }
960
+ else if (c == 0x22)
961
+ {
962
+ isQuote = false;
963
+ }
964
+ else if (c < 0x80)
965
+ {
966
+ ret.push(c);
967
+ }
968
+ else if (c < 0x800)
969
+ {
970
+ ret.push(0xc0 | (c >> 6));
971
+ ret.push(0x80 | (c & 0x3f));
972
+ }
973
+ else if (c < 0x10000)
974
+ {
975
+ ret.push(0xe0 | (c >> 12));
976
+ ret.push(0x80 | ((c >> 6) & 0x3f));
977
+ ret.push(0x80 | (c & 0x3f));
978
+ }
979
+ else if (c < 0x200000)
980
+ {
981
+ ret.push(0xf0 | (c >> 18));
982
+ ret.push(0x80 | ((c >> 12) & 0x3f));
983
+ ret.push(0x80 | ((c >> 6) & 0x3f));
984
+ ret.push(0x80 | (c & 0x3f));
985
+ }
986
+ else if (c < 0x4000000)
987
+ {
988
+ ret.push(0xf8 | (c >> 24));
989
+ ret.push(0x80 | ((c >> 18) & 0x3f));
990
+ ret.push(0x80 | ((c >> 12) & 0x3f));
991
+ ret.push(0x80 | ((c >> 6) & 0x3f));
992
+ ret.push(0x80 | (c & 0x3f));
993
+ }
994
+ else
995
+ {
996
+ ret.push(0xfc | (c >> 30));
997
+ ret.push(0x80 | ((c >> 24) & 0x3f));
998
+ ret.push(0x80 | ((c >> 18) & 0x3f));
999
+ ret.push(0x80 | ((c >> 12) & 0x3f));
1000
+ ret.push(0x80 | ((c >> 6) & 0x3f));
1001
+ ret.push(0x80 | (c & 0x3f));
1002
+ }
1003
+ }
1004
+ return new Uint8Array(ret);
1005
+ }
1006
+ }
1007
+
1008
+ export class QuotedPrintableEnc extends TextBinEnc
1009
+ {
1010
+ constructor()
1011
+ {
1012
+ super("QuotedPrintable");
1013
+ }
1014
+
1015
+ encodeBin(buff)
1016
+ {
1017
+ let hexStr = "0123456789ABCDEF";
1018
+ let arr = new Uint8Array(buff);
1019
+ let ret = [];
1020
+ let lineCnt = 0;
1021
+ let b;
1022
+ let i = 0;
1023
+ let j = arr.length;
1024
+ while (i < j)
1025
+ {
1026
+ b = arr[i++];
1027
+ if (b == 13 || b == 10)
1028
+ {
1029
+ ret.push(b);
1030
+ lineCnt = 0;
1031
+ }
1032
+ else if (b >= 32 && b <= 126)
1033
+ {
1034
+ if (lineCnt < 75)
1035
+ {
1036
+ ret.push(b);
1037
+ lineCnt++;
1038
+ }
1039
+ else if (arr[i] == 13 || arr[i] == 10)
1040
+ {
1041
+ ret.push(b);
1042
+ lineCnt++;
1043
+ }
1044
+ else
1045
+ {
1046
+ ret.push('='.charCodeAt(0));
1047
+ ret.push(13);
1048
+ ret.push(10);
1049
+ ret.push(b);
1050
+ lineCnt = 1;
1051
+ }
1052
+ }
1053
+ else if (lineCnt < 73)
1054
+ {
1055
+ ret.push('='.charCodeAt(0));
1056
+ ret.push(hexStr.charCodeAt(b >> 4));
1057
+ ret.push(hexStr.charCodeAt(b & 15));
1058
+ lineCnt += 3;
1059
+ }
1060
+ else if (arr[i] == 13 || arr[i] == 10)
1061
+ {
1062
+ ret.push('='.charCodeAt(0));
1063
+ ret.push(hexStr.charCodeAt(b >> 4));
1064
+ ret.push(hexStr.charCodeAt(b & 15));
1065
+ lineCnt += 3;
1066
+ }
1067
+ else
1068
+ {
1069
+ ret.push('='.charCodeAt(0));
1070
+ ret.push(13);
1071
+ ret.push(10);
1072
+ ret.push('='.charCodeAt(0));
1073
+ ret.push(hexStr.charCodeAt(b >> 4));
1074
+ ret.push(hexStr.charCodeAt(b & 15));
1075
+ lineCnt = 3;
1076
+ }
1077
+ }
1078
+
1079
+ return new TextDecoder().decode(new Uint8Array(ret));
1080
+ }
1081
+
1082
+ decodeBin(str)
1083
+ {
1084
+ let arr = new TextEncoder().encode(str);
1085
+ let ret = [];
1086
+ let c;
1087
+ let i = 0;
1088
+ let j = arr.length;
1089
+ while (i < j)
1090
+ {
1091
+ c = arr[i++];
1092
+ if (c == '='.charCodeAt(0))
1093
+ {
1094
+ if (i + 2 <= j && arr[i + 0] == 13 && arr[i + 1] == 10)
1095
+ {
1096
+ i += 2;
1097
+ }
1098
+ else if (i + 1 <= j && (arr[i] == 13 || arr[i] == 10))
1099
+ {
1100
+ i += 1;
1101
+ }
1102
+ else if (i + 2 <= j)
1103
+ {
1104
+ ret.push(Number.parseInt(String.fromCharCode(arr[i], arr[i + 1]), 16));
1105
+ i += 2;
1106
+ }
1107
+ }
1108
+ else
1109
+ {
1110
+ ret.push(c);
1111
+ }
1112
+ }
1113
+ return new Uint8Array(ret);
1114
+ }
1115
+ }
1116
+
1117
+ export class ASN1OIDBinEnc extends TextBinEnc
1118
+ {
1119
+ constructor()
1120
+ {
1121
+ super("SNMP OID");
1122
+ }
1123
+
1124
+ encodeBin(buff)
1125
+ {
1126
+ return net.oidToString(buff);
1127
+ }
1128
+
1129
+ decodeBin(str)
1130
+ {
1131
+ return net.oidText2PDU(str);
1132
+ }
1133
+ }
1134
+
1135
+ export class URIEncoding extends TextBinEnc
1136
+ {
1137
+ constructor()
1138
+ {
1139
+ super("URI Encoding");
1140
+ }
1141
+
1142
+ encodeBin(buff)
1143
+ {
1144
+ return encodeURI(new TextDecoder().decode(buff));
1145
+ }
1146
+
1147
+ decodeBin(str)
1148
+ {
1149
+ return new TextEncoder().encode(decodeURI(str));
1150
+ }
1151
+ }
1152
+
1153
+ export class FormEncoding extends TextBinEnc
1154
+ {
1155
+ constructor()
1156
+ {
1157
+ super("Form Encoding");
1158
+ }
1159
+
1160
+ encodeBin(buff)
1161
+ {
1162
+ return encodeURIComponent(new TextDecoder().decode(buff));
1163
+ }
1164
+
1165
+ decodeBin(str)
1166
+ {
1167
+ return new TextEncoder().encode(decodeURIComponent(str));
1168
+ }
1169
+ }
1170
+
1171
+ export class ASCII85Enc extends TextBinEnc
1172
+ {
1173
+ constructor()
1174
+ {
1175
+ super("ASCII85");
1176
+ }
1177
+
1178
+ encodeBin(buff)
1179
+ {
1180
+ let ret = [];
1181
+ let arr = new Uint8Array(buff);
1182
+ let i = 0;
1183
+ let j = arr.length;
1184
+ let v;
1185
+ while (i + 4 <= j)
1186
+ {
1187
+ v = data.readMUInt32(arr, i);
1188
+ i += 4;
1189
+ ret.push(String.fromCharCode(Math.floor(v / 52200625) + 33));
1190
+ v %= 52200625;
1191
+ ret.push(String.fromCharCode(Math.floor(v / 614125) + 33));
1192
+ v %= 614125;
1193
+ ret.push(String.fromCharCode(Math.floor(v / 7225) + 33));
1194
+ v %= 7225;
1195
+ ret.push(String.fromCharCode(Math.floor(v / 85) + 33));
1196
+ v %= 85;
1197
+ ret.push(String.fromCharCode(v + 33));
1198
+ }
1199
+ switch (j - i)
1200
+ {
1201
+ case 1:
1202
+ v = arr[i] << 24;
1203
+ ret.push(String.fromCharCode(Math.floor(v / 52200625) + 33));
1204
+ v %= 52200625;
1205
+ ret.push(String.fromCharCode(Math.floor(v / 614125) + 33));
1206
+ break;
1207
+ case 2:
1208
+ v = data.readMUInt16(arr, i) << 16;
1209
+ ret.push(String.fromCharCode(Math.floor(v / 52200625) + 33));
1210
+ v %= 52200625;
1211
+ ret.push(String.fromCharCode(Math.floor(v / 614125) + 33));
1212
+ v %= 614125;
1213
+ ret.push(String.fromCharCode(Math.floor(v / 7225) + 33));
1214
+ break;
1215
+ case 3:
1216
+ v = data.readMUInt24(arr, i) << 8;
1217
+ ret.push(String.fromCharCode(Math.floor(v / 52200625) + 33));
1218
+ v %= 52200625;
1219
+ ret.push(String.fromCharCode(Math.floor(v / 614125) + 33));
1220
+ v %= 614125;
1221
+ ret.push(String.fromCharCode(Math.floor(v / 7225) + 33));
1222
+ v %= 7225;
1223
+ ret.push(String.fromCharCode(Math.floor(v / 85) + 33));
1224
+ break;
1225
+ }
1226
+ return ret.join("");
1227
+ }
1228
+
1229
+ decodeBin(str)
1230
+ {
1231
+ let i = 0;
1232
+ let j = str.length;
1233
+ let validCnt = 0;
1234
+ const mulVals = [52200625, 614125, 7225, 85, 1];
1235
+ let v = 0;
1236
+ let lastU = 0;
1237
+ let c;
1238
+ let ret = [];
1239
+ while (i < j)
1240
+ {
1241
+ c = str.charCodeAt(i++);
1242
+ if (c == 'z'.charCodeAt(0))
1243
+ {
1244
+ ret.push(0);
1245
+ ret.push(0);
1246
+ ret.push(0);
1247
+ ret.push(0);
1248
+ lastU = 0;
1249
+ }
1250
+ else if (c == '~'.charCodeAt(0))
1251
+ {
1252
+ if (i < j && str.charAt(i) == '>')
1253
+ {
1254
+ lastU = 0;
1255
+ break;
1256
+ }
1257
+ }
1258
+ else if (c == 'u'.charCodeAt(0))
1259
+ {
1260
+ lastU++;
1261
+ v += (c - 33) * mulVals[validCnt];
1262
+ validCnt++;
1263
+ }
1264
+ else if (c < 33 || c > 'u'.charCodeAt(0))
1265
+ {
1266
+ }
1267
+ else
1268
+ {
1269
+ lastU = 0;
1270
+ v += (c - 33) * mulVals[validCnt];
1271
+ validCnt++;
1272
+ }
1273
+ if (validCnt == 5)
1274
+ {
1275
+ ret.push((v >> 24) & 0xff);
1276
+ ret.push((v >> 16) & 0xff);
1277
+ ret.push((v >> 8) & 0xff);
1278
+ ret.push(v & 0xff);
1279
+ validCnt = 0;
1280
+ v = 0;
1281
+ }
1282
+ }
1283
+ if (validCnt > 0)
1284
+ {
1285
+ i = validCnt;
1286
+ while (i < 5)
1287
+ {
1288
+ v += ('u'.charCodeAt(0) - 33) * mulVals[i];
1289
+ i++;
1290
+ }
1291
+ validCnt--;
1292
+ i = 0;
1293
+ while (i < validCnt)
1294
+ {
1295
+ ret.push((v >> 24) & 0xff);
1296
+ v = v << 8;
1297
+ i++;
1298
+ }
1299
+ }
1300
+ else if (lastU > 0)
1301
+ {
1302
+ while (lastU-- > 0)
1303
+ {
1304
+ ret.pop();
1305
+ }
1306
+ }
1307
+ return new Uint8Array(ret);
1308
+ }
1309
+ }