@sswroom/sswr 1.5.5 → 1.6.1

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/parser.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as cert from "./cert.js";
2
+ import { ASN1Util } from "./certutil.js";
1
3
  import * as data from "./data.js";
2
4
  import * as geometry from "./geometry.js";
3
5
  import * as kml from "./kml.js";
@@ -793,7 +795,7 @@ function parseKMLNode(kmlNode, doc)
793
795
  }
794
796
  }
795
797
 
796
- async function parseJpg(reader)
798
+ async function parseJpg(reader, sourceName)
797
799
  {
798
800
  if (!(reader instanceof data.ByteReader))
799
801
  return null;
@@ -923,7 +925,7 @@ async function parseJpg(reader)
923
925
  let buff = reader.getArrayBuffer();
924
926
  let b = new Blob([buff], {type: "image/jpeg"});
925
927
  let img = await media.loadImageFromBlob(b);
926
- let simg = new media.StaticImage(img);
928
+ let simg = new media.StaticImage(img, sourceName, "image/jpeg");
927
929
  if (exif)
928
930
  simg.setExif(exif);
929
931
  return simg;
@@ -931,7 +933,7 @@ async function parseJpg(reader)
931
933
  return null;
932
934
  }
933
935
 
934
- async function parseWebp(reader)
936
+ async function parseWebp(reader, sourceName)
935
937
  {
936
938
  if (!(reader instanceof data.ByteReader))
937
939
  return null;
@@ -942,11 +944,238 @@ async function parseWebp(reader)
942
944
  let buff = reader.getArrayBuffer();
943
945
  let b = new Blob([buff], {type: "image/webp"});
944
946
  let img = await media.loadImageFromBlob(b);
945
- let simg = new media.StaticImage(img);
947
+ let simg = new media.StaticImage(img, sourceName, "image/webp");
946
948
  return simg;
947
949
  }
948
950
 
949
- export function parseXML(txt)
951
+ function parseX509(reader, fileName, mime)
952
+ {
953
+ if (!(reader instanceof data.ByteReader))
954
+ return null;
955
+ let initTxt;
956
+ let initOfst = 0;
957
+ if (reader.readUInt8(0) == 0xef && reader.readUInt8(1) == 0xbb && reader.readUInt8(2) == 0xbf)
958
+ {
959
+ initOfst = 3;
960
+ }
961
+ initTxt = reader.readUTF8(initOfst, 5);
962
+ if (initTxt == "-----")
963
+ {
964
+ let files = [];
965
+ let lines = text.splitLines(reader.readUTF8(initOfst, reader.getLength() - initOfst));
966
+ let i = 0;
967
+ let j = lines.length;
968
+ while (i < j)
969
+ {
970
+ if (lines[i] == "-----BEGIN CERTIFICATE-----")
971
+ {
972
+ let b64 = [];
973
+ i++;
974
+ while (true)
975
+ {
976
+ if (i >= j)
977
+ return null;
978
+ if (lines[i] == "-----END CERTIFICATE-----")
979
+ {
980
+ break;
981
+ }
982
+ else
983
+ {
984
+ b64.push(lines[i]);
985
+ }
986
+ i++;
987
+ }
988
+ files.push(new cert.X509Cert(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer));
989
+ }
990
+ else if (lines[i] == "-----BEGIN RSA PRIVATE KEY-----")
991
+ {
992
+ let b64 = [];
993
+ let enc = false;
994
+ i++;
995
+ while (true)
996
+ {
997
+ if (i >= j)
998
+ return null;
999
+ if (lines[i] == "-----END RSA PRIVATE KEY-----")
1000
+ {
1001
+ break;
1002
+ }
1003
+ else if (lines[i].startsWith("Proc-Type:"))
1004
+ {
1005
+ enc = true;
1006
+ }
1007
+ else
1008
+ {
1009
+ b64.push(lines[i]);
1010
+ }
1011
+ i++;
1012
+ }
1013
+ files.push(new cert.X509Key(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer, cert.KeyType.RSA));
1014
+ }
1015
+ else if (lines[i] == "-----BEGIN DSA PRIVATE KEY-----")
1016
+ {
1017
+ let b64 = [];
1018
+ i++;
1019
+ while (true)
1020
+ {
1021
+ if (i >= j)
1022
+ return null;
1023
+ if (lines[i] == "-----END DSA PRIVATE KEY-----")
1024
+ {
1025
+ break;
1026
+ }
1027
+ else
1028
+ {
1029
+ b64.push(lines[i]);
1030
+ }
1031
+ i++;
1032
+ }
1033
+ files.push(new cert.X509Key(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer, cert.KeyType.DSA));
1034
+ }
1035
+ else if (lines[i] == "-----BEGIN EC PRIVATE KEY-----")
1036
+ {
1037
+ let b64 = [];
1038
+ i++;
1039
+ while (true)
1040
+ {
1041
+ if (i >= j)
1042
+ return null;
1043
+ if (lines[i] == "-----END EC PRIVATE KEY-----")
1044
+ {
1045
+ break;
1046
+ }
1047
+ else
1048
+ {
1049
+ b64.push(lines[i]);
1050
+ }
1051
+ i++;
1052
+ }
1053
+ files.push(new cert.X509Key(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer, cert.KeyType.ECDSA));
1054
+ }
1055
+ else if (lines[i] == "-----BEGIN PRIVATE KEY-----")
1056
+ {
1057
+ let b64 = [];
1058
+ i++;
1059
+ while (true)
1060
+ {
1061
+ if (i >= j)
1062
+ return null;
1063
+ if (lines[i] == "-----END PRIVATE KEY-----")
1064
+ {
1065
+ break;
1066
+ }
1067
+ else
1068
+ {
1069
+ b64.push(lines[i]);
1070
+ }
1071
+ i++;
1072
+ }
1073
+ files.push(new cert.X509PrivKey(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer));
1074
+ }
1075
+ else if (lines[i] == "-----BEGIN PUBLIC KEY-----")
1076
+ {
1077
+ let b64 = [];
1078
+ i++;
1079
+ while (true)
1080
+ {
1081
+ if (i >= j)
1082
+ return null;
1083
+ if (lines[i] == "-----END PUBLIC KEY-----")
1084
+ {
1085
+ break;
1086
+ }
1087
+ else
1088
+ {
1089
+ b64.push(lines[i]);
1090
+ }
1091
+ i++;
1092
+ }
1093
+ files.push(new cert.X509PubKey(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer));
1094
+ }
1095
+ else if (lines[i] == "-----BEGIN CERTIFICATE REQUEST-----")
1096
+ {
1097
+ let b64 = [];
1098
+ i++;
1099
+ while (true)
1100
+ {
1101
+ if (i >= j)
1102
+ return null;
1103
+ if (lines[i] == "-----END CERTIFICATE REQUEST-----")
1104
+ {
1105
+ break;
1106
+ }
1107
+ else
1108
+ {
1109
+ b64.push(lines[i]);
1110
+ }
1111
+ i++;
1112
+ }
1113
+ files.push(new cert.X509CertReq(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer));
1114
+ }
1115
+ else if (lines[i] == "-----BEGIN NEW CERTIFICATE REQUEST-----")
1116
+ {
1117
+ let b64 = [];
1118
+ i++;
1119
+ while (true)
1120
+ {
1121
+ if (i >= j)
1122
+ return null;
1123
+ if (lines[i] == "-----END NEW CERTIFICATE REQUEST-----")
1124
+ {
1125
+ break;
1126
+ }
1127
+ else
1128
+ {
1129
+ b64.push(lines[i]);
1130
+ }
1131
+ i++;
1132
+ }
1133
+ files.push(new cert.X509CertReq(fileName, new text.Base64Enc().decodeBin(b64.join("")).buffer));
1134
+ }
1135
+ i++;
1136
+ }
1137
+ if (files.length == 1)
1138
+ {
1139
+ return files[0];
1140
+ }
1141
+ else if (files.length > 1)
1142
+ {
1143
+ let fileList = new cert.X509FileList(fileName, files[0]);
1144
+ let i = 1;
1145
+ while (i < files.length)
1146
+ {
1147
+ fileList.addFile(files[i]);
1148
+ i++;
1149
+ }
1150
+ return fileList;
1151
+ }
1152
+ console.log("Unsupported file", fileName, mime);
1153
+ }
1154
+ else if (reader.readUInt8(0) == 0x30 && ASN1Util.pduIsValid(reader, 0, reader.getLength()))
1155
+ {
1156
+ fileName = fileName.toUpperCase();
1157
+ console.log(mime);
1158
+ if (mime == "application/x-pkcs12" || fileName.endsWith(".P12") || fileName.endsWith(".PFX"))
1159
+ {
1160
+ return new cert.X509PKCS12(fileName, reader.getArrayBuffer());
1161
+ }
1162
+ else if (mime == "application/x-x509-ca-cert" || mime == "application/x-x509-user-cert" || mime == "application/pkix-cert" || fileName.endsWith(".DER") || fileName.endsWith(".CER") || fileName.endsWith(".CRT"))
1163
+ {
1164
+ return new cert.X509Cert(fileName, reader.getArrayBuffer());
1165
+ }
1166
+ else if (mime == "application/x-pkcs7-certificates" || fileName.endsWith(".P7B") || fileName.endsWith(".P7S"))
1167
+ {
1168
+ return new cert.X509PKCS7(fileName, reader.getArrayBuffer());
1169
+ }
1170
+ else if (mime == "application/pkix-crl" || fileName.endsWith(".CRL"))
1171
+ {
1172
+ return new cert.X509CRL(fileName, reader.getArrayBuffer());
1173
+ }
1174
+ }
1175
+ return null;
1176
+ }
1177
+
1178
+ export function parseXML(txt, sourceName)
950
1179
  {
951
1180
  let parser = new DOMParser();
952
1181
  let xmlDoc = parser.parseFromString(txt, "application/xml");
@@ -964,7 +1193,17 @@ export function parseXML(txt)
964
1193
  for (kmlNode of xmlRoot.childNodes)
965
1194
  {
966
1195
  if (kmlNode.nodeName != "#text")
967
- return parseKMLNode(kmlNode);
1196
+ {
1197
+ let feature = parseKMLNode(kmlNode);
1198
+ if (feature)
1199
+ {
1200
+ return new kml.KMLFile(feature, sourceName);
1201
+ }
1202
+ else
1203
+ {
1204
+ return null;
1205
+ }
1206
+ }
968
1207
  }
969
1208
  }
970
1209
  else
@@ -988,14 +1227,15 @@ export async function parseFile(file)
988
1227
 
989
1228
  if (t == "application/vnd.google-earth.kml+xml")
990
1229
  {
991
- return parseXML(await file.text());
1230
+ return parseXML(await file.text(), file.name);
992
1231
  }
993
1232
  else
994
1233
  {
995
1234
  let view = new data.ByteReader(await file.arrayBuffer());
996
1235
  let obj;
997
- if (obj = await parseJpg(view)) return obj;
998
- if (obj = await parseWebp(view)) return obj;
1236
+ if (obj = await parseJpg(view, file.name)) return obj;
1237
+ if (obj = await parseWebp(view, file.name)) return obj;
1238
+ if (obj = parseX509(view, file.name, t)) return obj;
999
1239
  console.log("Unsupported file type", t);
1000
1240
  return null;
1001
1241
  }
package/text.d.ts CHANGED
@@ -10,10 +10,11 @@ export enum LineBreakType
10
10
  CR,
11
11
  LF,
12
12
  CRLF
13
- };
13
+ }
14
14
 
15
15
  export function zeroPad(val: string | number, ndigits: number): string;
16
16
  export function isInteger(s: string): boolean;
17
+ export function isUInteger(s: string): boolean;
17
18
  export function toJSText(s: string): string;
18
19
  export function toXMLText(s: string): string;
19
20
  export function toAttrText(s: string): string;
@@ -24,6 +25,14 @@ export function toHex8(v: number): string;
24
25
  export function toHex16(v: number): string;
25
26
  export function toHex32(v: number): string;
26
27
  export function u8Arr2Hex(buff: Uint8Array, byteSep: string, rowSep: string): string;
28
+ export function splitLines(txt: string): string[];
29
+ export function isEmailAddress(s: string): boolean;
30
+ export function toUTF32Length(s: string): number;
31
+ export function isHKID(s: string): boolean;
32
+ export function charIsAlphaNumeric(s: string, index: number): boolean;
33
+ export function charIsDigit(s: string, index: number): boolean;
34
+ export function charIsUpperCase(s: string, index: number): boolean;
35
+ export function charIsLowerCase(s: string, index: number): boolean;
27
36
  export function getEncList(): TextBinEnc[];
28
37
 
29
38
  export class TextBinEnc
package/text.js CHANGED
@@ -42,6 +42,25 @@ export function isInteger(s)
42
42
  return true;
43
43
  }
44
44
 
45
+ export function isUInteger(s)
46
+ {
47
+ if (s === null)
48
+ return false;
49
+ let j = s.length;
50
+ if (j == 0)
51
+ return false;
52
+ let i = 0;
53
+ let c;
54
+ while (i < j)
55
+ {
56
+ c = s.charCodeAt(i);
57
+ if (c < 0x30 || c > 0x39)
58
+ return false;
59
+ i++;
60
+ }
61
+ return true;
62
+ }
63
+
45
64
  export function toJSText(s)
46
65
  {
47
66
  let out = "\"";
@@ -216,8 +235,7 @@ export function toHex16(v)
216
235
 
217
236
  export function toHex32(v)
218
237
  {
219
- let s = (v & 0xffffffff).toString(16);
220
- return "0".repeat(8 - s.length)+s;
238
+ return toHex16(v >> 16) + toHex16(v);
221
239
  }
222
240
 
223
241
  export function u8Arr2Hex(buff, byteSep, rowSep)
@@ -247,6 +265,270 @@ export function u8Arr2Hex(buff, byteSep, rowSep)
247
265
  return rows.join(rowSep);
248
266
  }
249
267
 
268
+ export function splitLines(txt)
269
+ {
270
+ let lines = [];
271
+ let pos = 0;
272
+ let i;
273
+ let j;
274
+ while (true)
275
+ {
276
+ i = txt.indexOf("\r", pos);
277
+ j = txt.indexOf("\n", pos);
278
+ if (i == -1 && j == -1)
279
+ {
280
+ lines.push(txt.substring(pos));
281
+ break;
282
+ }
283
+ else if (i == -1)
284
+ {
285
+ lines.push(txt.substring(pos, j));
286
+ pos = j + 1;
287
+ }
288
+ else if (j == -1)
289
+ {
290
+ lines.push(txt.substring(pos, i));
291
+ pos = i + 1;
292
+ }
293
+ else if (i < j)
294
+ {
295
+ lines.push(txt.substring(pos, i));
296
+ if (j == i + 1)
297
+ pos = j + 1;
298
+ else
299
+ pos = i + 1;
300
+ }
301
+ else
302
+ {
303
+ lines.push(txt.substring(pos, j));
304
+ pos = j + 1;
305
+ }
306
+ }
307
+ return lines;
308
+ }
309
+
310
+ export function isEmailAddress(s)
311
+ {
312
+ let atPos = -1;
313
+ let dotIndex = -1;
314
+ let c;
315
+ let i = 0;
316
+ let j = s.length;
317
+ while (i < j)
318
+ {
319
+ c = s.charAt(i);
320
+ if (charIsAlphaNumeric(s, i) || c == '-')
321
+ {
322
+
323
+ }
324
+ else if (c == '.')
325
+ {
326
+ if (atPos != -1)
327
+ {
328
+ dotIndex = i;
329
+ }
330
+ }
331
+ else if (c == '@')
332
+ {
333
+ if (atPos != -1)
334
+ {
335
+ return false;
336
+ }
337
+ atPos = i;
338
+ dotIndex = -1;
339
+ }
340
+ else
341
+ {
342
+ return false;
343
+ }
344
+ i++;
345
+ }
346
+ if (atPos == -1 || atPos == 0 || dotIndex == -1 || (j - dotIndex) < 2)
347
+ {
348
+ return false;
349
+ }
350
+ return true;
351
+ }
352
+
353
+ export function toUTF32Length(s)
354
+ {
355
+ let i = 0;
356
+ let j = s.length;
357
+ let ret = 0;
358
+ while (i < j)
359
+ {
360
+ if (i + 1 < j)
361
+ {
362
+ let c1 = s.charCodeAt(i);
363
+ let c2 = s.charCodeAt(i + 1);
364
+ if (c1 >= 0xd800 && c1 < 0xdc00 && c2 >= 0xdc00 && c2 < 0xe000)
365
+ {
366
+ i += 2;
367
+ }
368
+ else
369
+ {
370
+ i++;
371
+ }
372
+ }
373
+ else
374
+ {
375
+ i++;
376
+ }
377
+ ret++;
378
+ }
379
+ return ret;
380
+ }
381
+
382
+ export function isHKID(s)
383
+ {
384
+ let hkid;
385
+ let chk;
386
+ let ichk;
387
+ if (s.endsWith(')'))
388
+ {
389
+ if (s.length == 10)
390
+ {
391
+ if (s.charAt(7) == '(')
392
+ {
393
+ hkid = s.substring(0, 7);
394
+ chk = s.charAt(8);
395
+ }
396
+ else
397
+ {
398
+ return false;
399
+ }
400
+ }
401
+ else if (s.length == 11)
402
+ {
403
+ if (s.charAt(8) == '(')
404
+ {
405
+ hkid = s.substring(0, 8);
406
+ chk = s.charAt(9);
407
+ }
408
+ else
409
+ {
410
+ return false;
411
+ }
412
+ }
413
+ else
414
+ {
415
+ return false;
416
+ }
417
+ }
418
+ else
419
+ {
420
+ if (s.length == 8)
421
+ {
422
+ hkid = s.substring(0, 7);
423
+ chk = s.charAt(7);
424
+ }
425
+ else if (s.length == 9)
426
+ {
427
+ hkid = s.substring(0, 8);
428
+ chk = s.charAt(8);
429
+ }
430
+ else
431
+ {
432
+ return false;
433
+ }
434
+ }
435
+
436
+ if (charIsDigit(chk, 0))
437
+ ichk = Number.parseInt(chk);
438
+ else if (chk == 'A')
439
+ ichk = 10;
440
+ else
441
+ {
442
+ return false;
443
+ }
444
+
445
+ let thisChk;
446
+ if (hkid.length == 8)
447
+ {
448
+ if (!charIsUpperCase(hkid, 0) ||
449
+ !charIsUpperCase(hkid, 1) ||
450
+ !charIsDigit(hkid, 2) ||
451
+ !charIsDigit(hkid, 3) ||
452
+ !charIsDigit(hkid, 4) ||
453
+ !charIsDigit(hkid, 5) ||
454
+ !charIsDigit(hkid, 6) ||
455
+ !charIsDigit(hkid, 7))
456
+ return false;
457
+
458
+ thisChk = 0;
459
+ thisChk += (hkid.charCodeAt(0) - 0x41 + 10) * 9;
460
+ thisChk += (hkid.charCodeAt(1) - 0x41 + 10) * 8;
461
+ thisChk += (hkid.charCodeAt(2) - 0x30) * 7;
462
+ thisChk += (hkid.charCodeAt(3) - 0x30) * 6;
463
+ thisChk += (hkid.charCodeAt(4) - 0x30) * 5;
464
+ thisChk += (hkid.charCodeAt(5) - 0x30) * 4;
465
+ thisChk += (hkid.charCodeAt(6) - 0x30) * 3;
466
+ thisChk += (hkid.charCodeAt(7) - 0x30) * 2;
467
+ if (ichk != (thisChk % 11))
468
+ return false;
469
+ return true;
470
+ }
471
+ else
472
+ {
473
+ if (!charIsUpperCase(hkid, 0) ||
474
+ !charIsDigit(hkid, 1) ||
475
+ !charIsDigit(hkid, 2) ||
476
+ !charIsDigit(hkid, 3) ||
477
+ !charIsDigit(hkid, 4) ||
478
+ !charIsDigit(hkid, 5) ||
479
+ !charIsDigit(hkid, 6))
480
+ return false;
481
+
482
+ thisChk = 36 * 9;
483
+ thisChk += (hkid.charCodeAt(0) - 0x41 + 10) * 8;
484
+ thisChk += (hkid.charCodeAt(1) - 0x30) * 7;
485
+ thisChk += (hkid.charCodeAt(2) - 0x30) * 6;
486
+ thisChk += (hkid.charCodeAt(3) - 0x30) * 5;
487
+ thisChk += (hkid.charCodeAt(4) - 0x30) * 4;
488
+ thisChk += (hkid.charCodeAt(5) - 0x30) * 3;
489
+ thisChk += (hkid.charCodeAt(6) - 0x30) * 2;
490
+ if (ichk != (thisChk % 11))
491
+ return false;
492
+ return true;
493
+ }
494
+ }
495
+
496
+ export function charIsAlphaNumeric(s, index)
497
+ {
498
+ let c = s.charCodeAt(index);
499
+ if (c >= 0x30 && c <= 0x39)
500
+ return true;
501
+ if (c >= 0x41 && c <= 0x5A)
502
+ return true;
503
+ if (c >= 0x61 && c <= 0x7A)
504
+ return true;
505
+ return false;
506
+ }
507
+
508
+ export function charIsDigit(s, index)
509
+ {
510
+ let c = s.charCodeAt(index);
511
+ if (c >= 0x30 && c <= 0x39)
512
+ return true;
513
+ return false;
514
+ }
515
+
516
+ export function charIsUpperCase(s, index)
517
+ {
518
+ let c = s.charCodeAt(index);
519
+ if (c >= 0x41 && c <= 0x5A)
520
+ return true;
521
+ return false;
522
+ }
523
+
524
+ export function charIsLowerCase(s, index)
525
+ {
526
+ let c = s.charCodeAt(index);
527
+ if (c >= 0x61 && c <= 0x7A)
528
+ return true;
529
+ return false;
530
+ }
531
+
250
532
  export function getEncList()
251
533
  {
252
534
  let ret = [];
package/unit.d.ts CHANGED
@@ -83,6 +83,8 @@ export class Count
83
83
  static getUnitInfo(u: Count.Unit): UnitInfo<Count.Unit> | null;
84
84
  static getUnitRatio(u: Count.Unit) : number;
85
85
  static convert(fromUnit: Count.Unit, toUnit: Count.Unit, fromValue: number): number;
86
+ static wellFormat(val: number, nDecimal?: number): string;
87
+ static wellFormatBin(val: number, nDecimal?: number): string;
86
88
  }
87
89
 
88
90
  module Distance