ipaddress 0.2.5 → 0.2.7

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.
Files changed (118) hide show
  1. package/dist/src/crunchy.d.ts +130 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +616 -0
  4. package/dist/src/index.d.ts +13 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +21 -0
  7. package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -7
  8. package/dist/src/ip_bits.d.ts.map +1 -0
  9. package/dist/src/ip_bits.js +133 -0
  10. package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
  11. package/dist/src/ip_version.d.ts.map +1 -0
  12. package/dist/src/ip_version.js +6 -0
  13. package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +38 -35
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1243 -0
  16. package/dist/src/ipv4.d.ts +15 -0
  17. package/dist/src/ipv4.d.ts.map +1 -0
  18. package/dist/src/ipv4.js +187 -0
  19. package/dist/src/ipv6.d.ts +13 -0
  20. package/dist/src/ipv6.d.ts.map +1 -0
  21. package/dist/src/ipv6.js +190 -0
  22. package/dist/src/ipv6_loopback.d.ts +6 -0
  23. package/dist/src/ipv6_loopback.d.ts.map +1 -0
  24. package/dist/src/ipv6_loopback.js +51 -0
  25. package/dist/src/ipv6_mapped.d.ts +6 -0
  26. package/dist/src/ipv6_mapped.d.ts.map +1 -0
  27. package/dist/src/ipv6_mapped.js +159 -0
  28. package/dist/src/ipv6_unspec.d.ts +6 -0
  29. package/dist/src/ipv6_unspec.d.ts.map +1 -0
  30. package/dist/src/ipv6_unspec.js +57 -0
  31. package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
  32. package/dist/src/prefix.d.ts.map +1 -0
  33. package/dist/src/prefix.js +142 -0
  34. package/dist/src/prefix128.d.ts +7 -0
  35. package/dist/src/prefix128.d.ts.map +1 -0
  36. package/dist/src/prefix128.js +39 -0
  37. package/dist/src/prefix32.d.ts +7 -0
  38. package/dist/src/prefix32.d.ts.map +1 -0
  39. package/dist/src/prefix32.js +29 -0
  40. package/dist/src/rle.d.ts +19 -0
  41. package/dist/src/rle.d.ts.map +1 -0
  42. package/dist/src/rle.js +73 -0
  43. package/dist/test/test_crunchy_impls.d.ts +2 -0
  44. package/dist/test/test_crunchy_impls.d.ts.map +1 -0
  45. package/dist/test/test_crunchy_impls.js +150 -0
  46. package/dist/test/test_crunchy_with32bit.d.ts +2 -0
  47. package/dist/test/test_crunchy_with32bit.d.ts.map +1 -0
  48. package/dist/test/test_crunchy_with32bit.js +382 -0
  49. package/dist/test/test_ipaddress.d.ts +2 -0
  50. package/dist/test/test_ipaddress.d.ts.map +1 -0
  51. package/dist/test/test_ipaddress.js +182 -0
  52. package/dist/test/test_ipv4.d.ts +2 -0
  53. package/dist/test/test_ipv4.d.ts.map +1 -0
  54. package/dist/test/test_ipv4.js +561 -0
  55. package/dist/test/test_ipv6.d.ts +2 -0
  56. package/dist/test/test_ipv6.d.ts.map +1 -0
  57. package/dist/test/test_ipv6.js +313 -0
  58. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  59. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_loopback.js +40 -0
  61. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  62. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_mapped.js +89 -0
  64. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  65. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  66. package/dist/test/test_ipv6_unspec.js +39 -0
  67. package/dist/test/test_prefix128.d.ts +2 -0
  68. package/dist/test/test_prefix128.d.ts.map +1 -0
  69. package/dist/test/test_prefix128.js +35 -0
  70. package/dist/test/test_prefix32.d.ts +2 -0
  71. package/dist/test/test_prefix32.d.ts.map +1 -0
  72. package/dist/test/test_prefix32.js +126 -0
  73. package/dist/test/test_rle.d.ts +2 -0
  74. package/dist/test/test_rle.d.ts.map +1 -0
  75. package/dist/test/test_rle.js +141 -0
  76. package/dist/vitest.config.d.ts +3 -0
  77. package/dist/vitest.config.d.ts.map +1 -0
  78. package/dist/vitest.config.js +11 -0
  79. package/package.json +41 -37
  80. package/src/crunchy.ts +731 -0
  81. package/src/index.ts +23 -0
  82. package/src/ip_bits.ts +148 -0
  83. package/src/ip_version.ts +6 -0
  84. package/src/ipaddress.ts +1347 -0
  85. package/src/ipv4.ts +203 -0
  86. package/src/ipv6.ts +202 -0
  87. package/src/ipv6_loopback.ts +55 -0
  88. package/src/ipv6_mapped.ts +163 -0
  89. package/src/ipv6_unspec.ts +59 -0
  90. package/src/prefix.ts +170 -0
  91. package/src/prefix128.ts +42 -0
  92. package/src/prefix32.ts +32 -0
  93. package/src/rle.ts +85 -0
  94. package/test/test_crunchy_impls.ts +179 -0
  95. package/test/test_crunchy_with32bit.ts +510 -0
  96. package/test/test_ipaddress.ts +208 -0
  97. package/test/test_ipv4.ts +596 -0
  98. package/test/test_ipv6.ts +325 -0
  99. package/test/test_ipv6_loopback.ts +52 -0
  100. package/test/test_ipv6_mapped.ts +101 -0
  101. package/test/test_ipv6_unspec.ts +51 -0
  102. package/test/test_prefix128.ts +39 -0
  103. package/test/test_prefix32.ts +130 -0
  104. package/test/test_rle.ts +144 -0
  105. package/LICENSE +0 -20
  106. package/README.md +0 -960
  107. package/crunchy.d.ts +0 -55
  108. package/index.d.ts +0 -12
  109. package/ipaddress.js +0 -2869
  110. package/ipaddress.js.map +0 -1
  111. package/ipv4.d.ts +0 -14
  112. package/ipv6.d.ts +0 -12
  113. package/ipv6_loopback.d.ts +0 -5
  114. package/ipv6_mapped.d.ts +0 -5
  115. package/ipv6_unspec.d.ts +0 -5
  116. package/prefix128.d.ts +0 -6
  117. package/prefix32.d.ts +0 -6
  118. package/rle.d.ts +0 -14
@@ -0,0 +1,616 @@
1
+ /**
2
+ * Arbitrary-precision integer used throughout ip address math.
3
+ *
4
+ * Historically this was always the hand-rolled 28-bit-limb
5
+ * implementation below (CrunchyWith32Bit), ported from
6
+ * Crunch - Arbitrary-precision integer arithmetic library
7
+ * Copyright (C) 2014 Nenad Vukicevic crunch.secureroom.net/license
8
+ *
9
+ * Native BigInt is now available in every runtime this library
10
+ * targets, so there are two implementations of CrunchyBase -
11
+ * CrunchyWithBigInt (native BigInt, used whenever available) and
12
+ * CrunchyWith32Bit (the original limb-array implementation, kept for
13
+ * environments without BigInt support). Which one backs `Crunchy` is
14
+ * decided once, at module load, by feature-detecting BigInt.
15
+ *
16
+ * `Crunchy` is a value (not a class), so `new Crunchy()` is not
17
+ * possible - use `Crunchy.create()` (or any of the other factories:
18
+ * `Crunchy.zero()`, `Crunchy.parse()`, ...).
19
+ */
20
+ /**
21
+ * Native BigInt-backed implementation. Used whenever the runtime
22
+ * supports BigInt (i.e. essentially always: Node 10.4+, every
23
+ * evergreen browser, Deno).
24
+ */
25
+ export class CrunchyWithBigInt {
26
+ value;
27
+ // Only ever constructed from within this class (its own methods, or
28
+ // the bootstrap() below used once to seed the module-level
29
+ // singleton) - real values always arrive as number|string, through
30
+ // from_number()/from_string()/parse(), never as a raw bigint.
31
+ constructor(value) {
32
+ this.value = value;
33
+ }
34
+ static bootstrap() {
35
+ return new CrunchyWithBigInt(0n);
36
+ }
37
+ static valueOf(y) {
38
+ if (!(y instanceof CrunchyWithBigInt)) {
39
+ throw new Error("CrunchyWithBigInt operation received a non-CrunchyWithBigInt operand");
40
+ }
41
+ return y.value;
42
+ }
43
+ clone() {
44
+ return new CrunchyWithBigInt(this.value);
45
+ }
46
+ compare(y) {
47
+ const yv = CrunchyWithBigInt.valueOf(y);
48
+ if (this.value < yv)
49
+ return -1;
50
+ if (this.value > yv)
51
+ return 1;
52
+ return 0;
53
+ }
54
+ eq(oth) {
55
+ return this.compare(oth) == 0;
56
+ }
57
+ lt(oth) {
58
+ return this.compare(oth) < 0;
59
+ }
60
+ lte(oth) {
61
+ return this.compare(oth) <= 0;
62
+ }
63
+ gt(oth) {
64
+ return this.compare(oth) > 0;
65
+ }
66
+ gte(oth) {
67
+ return this.compare(oth) >= 0;
68
+ }
69
+ add(y) {
70
+ return new CrunchyWithBigInt(this.value + CrunchyWithBigInt.valueOf(y));
71
+ }
72
+ sub(y) {
73
+ return new CrunchyWithBigInt(this.value - CrunchyWithBigInt.valueOf(y));
74
+ }
75
+ // shl/shr shift the magnitude only and leave the sign untouched,
76
+ // matching CrunchyWith32Bit (native BigInt `>>` on a negative value
77
+ // performs an arithmetic/floor shift instead).
78
+ shl(s) {
79
+ const neg = this.value < 0n;
80
+ const abs = neg ? -this.value : this.value;
81
+ const shifted = abs << BigInt(s);
82
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
83
+ }
84
+ shr(s) {
85
+ const neg = this.value < 0n;
86
+ const abs = neg ? -this.value : this.value;
87
+ const shifted = abs >> BigInt(s);
88
+ return new CrunchyWithBigInt(neg ? -shifted : shifted);
89
+ }
90
+ // Floored modulo: result always has the same sign as `y` (or is
91
+ // zero), which matches the only path CrunchyWith32Bit.mod() is
92
+ // actually exercised with (non-negative operands).
93
+ mod(y) {
94
+ const yv = CrunchyWithBigInt.valueOf(y);
95
+ let r = this.value % yv;
96
+ if (r !== 0n && r < 0n !== yv < 0n) {
97
+ r += yv;
98
+ }
99
+ return new CrunchyWithBigInt(r);
100
+ }
101
+ // Never emits a sign, matching CrunchyWith32Bit.toString(), which
102
+ // only ever produces magnitude digits.
103
+ toString(radix = 10) {
104
+ const abs = this.value < 0n ? -this.value : this.value;
105
+ return abs.toString(radix);
106
+ }
107
+ create(value) {
108
+ if (value === undefined) {
109
+ return this.zero();
110
+ }
111
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
112
+ }
113
+ zero() {
114
+ return new CrunchyWithBigInt(0n);
115
+ }
116
+ one() {
117
+ return new CrunchyWithBigInt(1n);
118
+ }
119
+ two() {
120
+ return new CrunchyWithBigInt(2n);
121
+ }
122
+ parse(val) {
123
+ const ret = this.from_string(val, 10);
124
+ if (ret === null) {
125
+ throw new Error(`invalid decimal number: ${val}`);
126
+ }
127
+ return ret;
128
+ }
129
+ from_number(val) {
130
+ return new CrunchyWithBigInt(BigInt(Math.trunc(val)));
131
+ }
132
+ from_string(val, radix = 10) {
133
+ let s = val;
134
+ let neg = false;
135
+ if (s.startsWith("-")) {
136
+ neg = true;
137
+ s = s.slice(1);
138
+ }
139
+ let result = 0n;
140
+ const bigRadix = BigInt(radix);
141
+ for (const ch of s) {
142
+ const digit = parseInt(ch, radix);
143
+ if (isNaN(digit)) {
144
+ return null;
145
+ }
146
+ result = result * bigRadix + BigInt(digit);
147
+ }
148
+ return new CrunchyWithBigInt(neg ? -result : result);
149
+ }
150
+ from_string_or_throw(val, radix = 10) {
151
+ const ret = this.from_string(val, radix);
152
+ if (ret === null) {
153
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
154
+ }
155
+ return ret;
156
+ }
157
+ }
158
+ /**
159
+ * @module Crunch
160
+ * Radix: 28 bits
161
+ * Endianness: Big
162
+ *
163
+ * @param {boolean} rawIn - expect 28-bit arrays
164
+ * @param {boolean} rawOut - return 28-bit arrays
165
+ */
166
+ export class CrunchyWith32Bit {
167
+ num = [];
168
+ negative = false;
169
+ static zeroes = ((n) => {
170
+ return new Array(n).fill(0);
171
+ })(60);
172
+ clone() {
173
+ const ret = new CrunchyWith32Bit();
174
+ ret.num = this.num.slice();
175
+ ret.negative = this.negative;
176
+ return ret;
177
+ }
178
+ static removeLeadingZeros(inn) {
179
+ const out = inn.slice();
180
+ while (out[0] === 0 && out.length > 1) {
181
+ out.shift();
182
+ }
183
+ return out; // .transformOut();
184
+ }
185
+ static from_14bit(a) {
186
+ const ret = new CrunchyWith32Bit();
187
+ ret.num = a;
188
+ return ret;
189
+ }
190
+ static from_8bit(a) {
191
+ let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
192
+ const z = new CrunchyWith32Bit();
193
+ if (a[0] < 0) {
194
+ a[0] *= -1;
195
+ z.negative = true;
196
+ }
197
+ else {
198
+ z.negative = false;
199
+ }
200
+ x = x.concat(a);
201
+ for (let i = 0; i < x.length; i += 7) {
202
+ z.num.push(x[i] * 1048576 + x[i + 1] * 4096 + x[i + 2] * 16 + (x[i + 3] >> 4), (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6]);
203
+ }
204
+ z.num = CrunchyWith32Bit.removeLeadingZeros(z.num);
205
+ return z;
206
+ }
207
+ static _zero = CrunchyWith32Bit.from_8bit([0]);
208
+ static _one = CrunchyWith32Bit.from_8bit([1]);
209
+ static _two = CrunchyWith32Bit.from_8bit([2]);
210
+ create(value) {
211
+ if (value === undefined) {
212
+ return this.zero();
213
+ }
214
+ return typeof value === "number" ? this.from_number(value) : this.parse(value);
215
+ }
216
+ zero() {
217
+ return CrunchyWith32Bit._zero;
218
+ }
219
+ one() {
220
+ return CrunchyWith32Bit._one;
221
+ }
222
+ two() {
223
+ return CrunchyWith32Bit._two;
224
+ }
225
+ parse(val) {
226
+ const ret = this.from_string(val, 10);
227
+ if (ret === null) {
228
+ throw new Error(`invalid decimal number: ${val}`);
229
+ }
230
+ return ret;
231
+ }
232
+ from_number(val) {
233
+ return this.parse("" + val);
234
+ }
235
+ // Same as from_string(), but for literals that are known to be valid
236
+ // numbers (e.g. hardcoded hex constants), so the caller doesn't need
237
+ // to deal with a nullable result it can never actually observe.
238
+ from_string_or_throw(val, radix = 10) {
239
+ const ret = this.from_string(val, radix);
240
+ if (ret === null) {
241
+ throw new Error(`invalid number: ${val} (radix ${radix})`);
242
+ }
243
+ return ret;
244
+ }
245
+ from_string(val, radix = 10) {
246
+ const x = val.split("");
247
+ let p = CrunchyWith32Bit._one;
248
+ let a = CrunchyWith32Bit._zero;
249
+ const b = CrunchyWith32Bit.from_8bit([radix]);
250
+ let n = false;
251
+ if (x[0] === "-") {
252
+ n = true;
253
+ x.shift();
254
+ }
255
+ while (x.length > 0) {
256
+ const popped = x.pop();
257
+ if (popped === undefined) {
258
+ break;
259
+ }
260
+ const c = parseInt(popped, radix);
261
+ if (isNaN(c)) {
262
+ console.error("from_string:", val);
263
+ return null;
264
+ }
265
+ a = a.add(p.mul(CrunchyWith32Bit.from_8bit([c])));
266
+ p = p.mul(b);
267
+ }
268
+ a.negative = n;
269
+ return a;
270
+ }
271
+ to_8bit() {
272
+ const x = [0].slice((this.num.length - 1) % 2).concat(this.num);
273
+ let z = [];
274
+ for (let i = 0; i < x.length;) {
275
+ const u = x[i++];
276
+ const v = x[i++];
277
+ z.push(u >> 20, (u >> 12) & 255, (u >> 4) & 255, ((u << 4) | (v >> 24)) & 255, (v >> 16) & 255, (v >> 8) & 255, v & 255);
278
+ }
279
+ z = CrunchyWith32Bit.removeLeadingZeros(z);
280
+ if (this.negative) {
281
+ z[0] *= -1;
282
+ }
283
+ return z;
284
+ }
285
+ compare(y) {
286
+ const yy = y;
287
+ const xl = this.num.length;
288
+ const yl = yy.num.length; // zero front pad problem
289
+ if (xl < yl) {
290
+ return -1;
291
+ }
292
+ else if (xl > yl) {
293
+ return 1;
294
+ }
295
+ for (let i = 0; i < xl; i++) {
296
+ if (this.num[i] < yy.num[i])
297
+ return -1;
298
+ if (this.num[i] > yy.num[i])
299
+ return 1;
300
+ }
301
+ return 0;
302
+ }
303
+ eq(oth) {
304
+ return this.compare(oth) == 0;
305
+ }
306
+ lt(oth) {
307
+ return this.compare(oth) < 0;
308
+ }
309
+ lte(oth) {
310
+ return this.compare(oth) <= 0;
311
+ }
312
+ gt(oth) {
313
+ return this.compare(oth) > 0;
314
+ }
315
+ gte(oth) {
316
+ return this.compare(oth) >= 0;
317
+ }
318
+ add(y) {
319
+ const yy = y;
320
+ let z;
321
+ if (this.negative) {
322
+ if (yy.negative) {
323
+ z = this.unsigned_add(yy);
324
+ z.negative = true;
325
+ }
326
+ else {
327
+ z = yy.unsigned_sub(this, false).cut();
328
+ }
329
+ }
330
+ else {
331
+ z = yy.negative ? this.unsigned_sub(yy, false).cut() : this.unsigned_add(yy);
332
+ }
333
+ return z;
334
+ }
335
+ unsigned_add(_y) {
336
+ const n = this.num.length;
337
+ const t = _y.num.length;
338
+ let i = Math.max(n, t);
339
+ let c = 0;
340
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
341
+ let x = this.num;
342
+ let y = _y.num;
343
+ if (n < t) {
344
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(this.num);
345
+ }
346
+ else if (n > t) {
347
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
348
+ }
349
+ for (i -= 1; i >= 0; i--) {
350
+ z[i] = x[i] + y[i] + c;
351
+ if (z[i] > 268435455) {
352
+ c = 1;
353
+ z[i] -= 268435456;
354
+ }
355
+ else {
356
+ c = 0;
357
+ }
358
+ }
359
+ if (c === 1) {
360
+ z.unshift(c);
361
+ }
362
+ const ret = new CrunchyWith32Bit();
363
+ ret.num = z;
364
+ return ret;
365
+ }
366
+ sub(y) {
367
+ const yy = y;
368
+ let z;
369
+ if (this.negative) {
370
+ if (yy.negative) {
371
+ z = yy.unsigned_sub(this, false).cut();
372
+ }
373
+ else {
374
+ z = this.unsigned_add(yy);
375
+ z.negative = true;
376
+ }
377
+ }
378
+ else {
379
+ z = yy.negative ? this.unsigned_add(yy) : this.unsigned_sub(yy, false).cut();
380
+ }
381
+ return z;
382
+ }
383
+ unsigned_sub(_y, internal = false) {
384
+ const n = this.num.length;
385
+ const t = _y.num.length;
386
+ let i = Math.max(n, t);
387
+ let c = 0;
388
+ const z = CrunchyWith32Bit.zeroes.slice(0, i);
389
+ let x = this.num;
390
+ let y = _y.num;
391
+ if (n < t) {
392
+ x = CrunchyWith32Bit.zeroes.slice(0, t - n).concat(x);
393
+ }
394
+ else if (n > t) {
395
+ y = CrunchyWith32Bit.zeroes.slice(0, n - t).concat(y);
396
+ }
397
+ for (i -= 1; i >= 0; i--) {
398
+ z[i] = x[i] - y[i] - c;
399
+ if (z[i] < 0) {
400
+ c = 1;
401
+ z[i] += 268435456;
402
+ }
403
+ else {
404
+ c = 0;
405
+ }
406
+ }
407
+ let cry = new CrunchyWith32Bit();
408
+ cry.num = z;
409
+ if (c === 1 && !internal) {
410
+ const zero = new CrunchyWith32Bit();
411
+ zero.num = CrunchyWith32Bit.zeroes.slice(0, z.length);
412
+ cry = zero.unsigned_sub(cry, true);
413
+ cry.negative = true;
414
+ }
415
+ return cry;
416
+ }
417
+ lsh(s) {
418
+ const ss = s % 28;
419
+ const ls = Math.floor(s / 28);
420
+ let l = this.num.length;
421
+ const z = this.clone();
422
+ let t = 0;
423
+ if (ss) {
424
+ z.num = [];
425
+ while (l--) {
426
+ z.num[l] = ((this.num[l] << ss) + t) & 268435455;
427
+ t = this.num[l] >>> (28 - ss);
428
+ }
429
+ if (t !== 0) {
430
+ z.num.unshift(t);
431
+ }
432
+ z.negative = this.negative;
433
+ }
434
+ if (ls) {
435
+ z.num = z.num.concat(CrunchyWith32Bit.zeroes.slice(0, ls));
436
+ }
437
+ return z;
438
+ }
439
+ rsh(s) {
440
+ const ss = s % 28;
441
+ const ls = Math.floor(s / 28);
442
+ let l = this.num.length - ls;
443
+ const z = this.clone();
444
+ z.num = this.num.slice(0, l);
445
+ if (ss) {
446
+ while (--l >= 0) {
447
+ z.num[l] = ((z.num[l] >> ss) | (z.num[l - 1] << (28 - ss))) & 268435455;
448
+ }
449
+ z.num[l] = z.num[l] >> ss;
450
+ if (z.num[0] === 0) {
451
+ z.num.shift();
452
+ }
453
+ }
454
+ return z;
455
+ }
456
+ mul(y) {
457
+ const yy = y;
458
+ let yl;
459
+ let yh;
460
+ let c;
461
+ const n = this.num.length;
462
+ let i = yy.num.length;
463
+ const z = CrunchyWith32Bit.zeroes.slice(0, n + i);
464
+ while (i--) {
465
+ c = 0;
466
+ yl = yy.num[i] & 16383;
467
+ yh = yy.num[i] >> 14;
468
+ for (let j = n - 1, xl, xh, t1, t2; j >= 0; j--) {
469
+ xl = this.num[j] & 16383;
470
+ xh = this.num[j] >> 14;
471
+ t1 = yh * xl + xh * yl;
472
+ t2 = yl * xl + ((t1 & 16383) << 14) + z[j + i + 1] + c;
473
+ z[j + i + 1] = t2 & 268435455;
474
+ c = yh * xh + (t1 >> 14) + (t2 >> 28);
475
+ }
476
+ z[i] = c;
477
+ }
478
+ if (z[0] === 0) {
479
+ z.shift();
480
+ }
481
+ const ret = new CrunchyWith32Bit();
482
+ ret.negative = this.negative !== yy.negative;
483
+ ret.num = z;
484
+ return ret;
485
+ }
486
+ static msb(x) {
487
+ if (x !== 0) {
488
+ let z = 0;
489
+ for (let i = 134217728; i > x; z++) {
490
+ i /= 2;
491
+ }
492
+ return z;
493
+ }
494
+ }
495
+ shr(s) {
496
+ const my = this.rsh(s);
497
+ return my.cut();
498
+ }
499
+ shl(s) {
500
+ return this.lsh(s).cut();
501
+ }
502
+ cut() {
503
+ const out = this.clone();
504
+ // beasty hack
505
+ if (out.num.length == 0) {
506
+ out.num = [0];
507
+ return out;
508
+ }
509
+ while (out.num[0] === 0 && out.num.length > 1) {
510
+ out.num.shift();
511
+ }
512
+ return out;
513
+ }
514
+ div(y, internal = false) {
515
+ const yy = y;
516
+ if (yy.num.length === 1 && yy.num[0] === 0) {
517
+ return null;
518
+ }
519
+ let u;
520
+ let v;
521
+ const s = (CrunchyWith32Bit.msb(yy.num[0]) ?? 0) - 1;
522
+ if (s > 0) {
523
+ u = this.lsh(s);
524
+ v = yy.lsh(s);
525
+ }
526
+ else {
527
+ u = this.clone();
528
+ v = yy.clone();
529
+ }
530
+ const d = u.num.length - v.num.length;
531
+ const q = [0];
532
+ let k = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d)));
533
+ const yt = v.num[0] * 268435456 + v.num[1];
534
+ // only cmp as last resort
535
+ while (u.num[0] > k.num[0] || (u.num[0] === k.num[0] && u.compare(k) > -1)) {
536
+ q[0]++;
537
+ u = u.unsigned_sub(k, false);
538
+ }
539
+ for (let i = 1; i <= d; i++) {
540
+ q[i] = u.num[i - 1] === v.num[0] ? 268435455 : ~~((u.num[i - 1] * 268435456 + u.num[i]) / v.num[0]);
541
+ const xt = u.num[i - 1] * 72057594037927936 + u.num[i] * 268435456 + u.num[i + 1];
542
+ while (q[i] * yt > xt) {
543
+ // condition check can fail due to precision problem at 28-bit
544
+ q[i]--;
545
+ }
546
+ k = v.mul(CrunchyWith32Bit.from_14bit([q[i]]));
547
+ k.num = k.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i)); // concat after multiply, save cycles
548
+ u = u.unsigned_sub(k, false);
549
+ if (u.negative) {
550
+ u = CrunchyWith32Bit.from_14bit(v.num.concat(CrunchyWith32Bit.zeroes.slice(0, d - i))).unsigned_sub(u, false);
551
+ q[i]--;
552
+ }
553
+ }
554
+ let z;
555
+ if (internal) {
556
+ z = s > 0 ? u.cut().rsh(s) : u.cut();
557
+ }
558
+ else {
559
+ z = CrunchyWith32Bit.from_14bit(CrunchyWith32Bit.removeLeadingZeros(q));
560
+ z.negative = this.negative !== yy.negative;
561
+ }
562
+ return z;
563
+ }
564
+ // Same as div(), but for divisors that are known to be non-zero, so the
565
+ // caller doesn't need to deal with a nullable result it can never
566
+ // actually observe.
567
+ div_or_throw(y, internal = false) {
568
+ const ret = this.div(y, internal);
569
+ if (ret === null) {
570
+ throw new Error("division by zero");
571
+ }
572
+ return ret;
573
+ }
574
+ mod(y) {
575
+ // For negative x, cmp doesn't work and result of div is negative
576
+ // so take result away from the modulus to get the correct result
577
+ const yy = y;
578
+ if (this.negative) {
579
+ return yy.sub(this.div_or_throw(yy, true));
580
+ }
581
+ switch (this.compare(yy)) {
582
+ case -1:
583
+ return this;
584
+ case 0:
585
+ return CrunchyWith32Bit.from_8bit([0]);
586
+ default:
587
+ return this.div_or_throw(yy, true);
588
+ }
589
+ }
590
+ mds(n) {
591
+ let z = 0;
592
+ for (let i = 0, l = this.num.length; i < l; i++) {
593
+ z = ((this.num[i] >> 14) + (z << 14)) % n;
594
+ z = ((this.num[i] & 16383) + (z << 14)) % n;
595
+ }
596
+ return z;
597
+ }
598
+ toString(radix = 10) {
599
+ const a = [];
600
+ let i = 0;
601
+ let x = this.clone();
602
+ const cradix = CrunchyWith32Bit.from_8bit([radix]);
603
+ const zero = CrunchyWith32Bit._zero;
604
+ do {
605
+ const digit = x.mds(radix);
606
+ x = x.div_or_throw(cradix);
607
+ a[i++] = "0123456789abcdef"[digit];
608
+ } while (!x.eq(zero));
609
+ return a.reverse().join("");
610
+ }
611
+ }
612
+ // Feature detection, computed once at module load.
613
+ export const hasBigInt = typeof BigInt !== "undefined";
614
+ const crunchySingleton = hasBigInt ? CrunchyWithBigInt.bootstrap() : new CrunchyWith32Bit();
615
+ export const Crunchy = crunchySingleton;
616
+ export default Crunchy;
@@ -0,0 +1,13 @@
1
+ import IPAddress from "./ipaddress.js";
2
+ export * from "./crunchy.js";
3
+ export * from "./prefix.js";
4
+ export * from "./ip_bits.js";
5
+ export * from "./ip_version.js";
6
+ export * from "./ipaddress.js";
7
+ export * from "./ipv4.js";
8
+ export * from "./ipv6.js";
9
+ export * from "./ipv6_mapped.js";
10
+ export * from "./ipv6_loopback.js";
11
+ export * from "./ipv6_unspec.js";
12
+ export default IPAddress;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AAGjC,eAAe,SAAS,CAAC"}
@@ -0,0 +1,21 @@
1
+ // import Crunchy from './crunchy.js'
2
+ // import Ipv4 from './ipv4.js'
3
+ // import Ipv6 from './ipv6.js'
4
+ // import Prefix from './prefix.js'
5
+ // import Ipv6Mapped from './ipv6_mapped.js'
6
+ // import Ipv6Loopback from './ipv6_loopback.js'
7
+ // import Ipv6Unspec from './ipv6_unspec.js'
8
+ import IPAddress from "./ipaddress.js";
9
+ export * from "./crunchy.js";
10
+ export * from "./prefix.js";
11
+ export * from "./ip_bits.js";
12
+ export * from "./ip_version.js";
13
+ export * from "./ipaddress.js";
14
+ export * from "./ipv4.js";
15
+ export * from "./ipv6.js";
16
+ export * from "./ipv6_mapped.js";
17
+ export * from "./ipv6_loopback.js";
18
+ export * from "./ipv6_unspec.js";
19
+ // export = Crunchy;
20
+ export default IPAddress;
21
+ // declare let Ip: IPAddress;
@@ -1,8 +1,6 @@
1
- import Crunchy from './crunchy';
2
- import IpVersion from './ip_version';
3
- export interface ToString {
4
- (source: IpBits, num: Crunchy): string;
5
- }
1
+ import Crunchy from "./crunchy.js";
2
+ import IpVersion from "./ip_version.js";
3
+ export type ToString = (source: IpBits, num: Crunchy) => string;
6
4
  export declare class IpBits {
7
5
  version: IpVersion;
8
6
  vt_as_compressed_string: ToString;
@@ -18,12 +16,13 @@ export declare class IpBits {
18
16
  as_compressed_string(bu: Crunchy): string;
19
17
  as_uncompressed_string(bu: Crunchy): string;
20
18
  dns_part_format(i: number): string;
21
- static _v4: IpBits;
19
+ static _v4: IpBits | null;
22
20
  static v4(): IpBits;
23
- static _v6: IpBits;
21
+ static _v6: IpBits | null;
24
22
  static v6(): IpBits;
25
23
  static ipv4_as_compressed(ip_bits: IpBits, host_address: Crunchy): string;
26
24
  static ipv6_as_compressed(ip_bits: IpBits, host_address: Crunchy): string;
27
25
  static ipv6_as_uncompressed(ip_bits: IpBits, host_address: Crunchy): string;
28
26
  }
29
27
  export default IpBits;
28
+ //# sourceMappingURL=ip_bits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ip_bits.d.ts","sourceRoot":"","sources":["../../src/ip_bits.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,cAAc,CAAC;AAInC,OAAO,SAAS,MAAM,iBAAiB,CAAC;AAExC,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,KAAK,MAAM,CAAC;AAGhE,qBAAa,MAAM;IACjB,OAAO,EAAG,SAAS,CAAC;IACpB,uBAAuB,EAAG,QAAQ,CAAC;IACnC,yBAAyB,EAAG,QAAQ,CAAC;IACrC,IAAI,EAAG,MAAM,CAAC;IACd,SAAS,EAAG,MAAM,CAAC;IACnB,QAAQ,EAAG,MAAM,CAAC;IAClB,UAAU,EAAG,MAAM,CAAC;IACpB,QAAQ,EAAG,MAAM,CAAC;IAClB,QAAQ,EAAG,OAAO,CAAC;IAEZ,KAAK,IAAI,MAAM;IAcf,KAAK,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM,EAAE;IAa5B,oBAAoB,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM;IAIzC,sBAAsB,CAAC,EAAE,EAAE,OAAO,GAAG,MAAM;IAI3C,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IASzC,OAAc,GAAG,EAAE,MAAM,GAAG,IAAI,CAAQ;WAC1B,EAAE,IAAI,MAAM;IAkB1B,OAAc,GAAG,EAAE,MAAM,GAAG,IAAI,CAAQ;WAC1B,EAAE,IAAI,MAAM;WAkBZ,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;WAWlE,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;WAsBlE,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,GAAG,MAAM;CAUnF;AAED,eAAe,MAAM,CAAC"}