ipaddress 0.2.5 → 0.2.6

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 (113) hide show
  1. package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
  2. package/dist/src/crunchy.d.ts.map +1 -0
  3. package/dist/src/crunchy.js +484 -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} +4 -5
  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} +18 -16
  14. package/dist/src/ipaddress.d.ts.map +1 -0
  15. package/dist/src/ipaddress.js +1228 -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 +176 -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 +35 -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 +25 -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.d.ts +2 -0
  44. package/dist/test/test_crunchy.d.ts.map +1 -0
  45. package/dist/test/test_crunchy.js +451 -0
  46. package/dist/test/test_ipaddress.d.ts +2 -0
  47. package/dist/test/test_ipaddress.d.ts.map +1 -0
  48. package/dist/test/test_ipaddress.js +182 -0
  49. package/dist/test/test_ipv4.d.ts +2 -0
  50. package/dist/test/test_ipv4.d.ts.map +1 -0
  51. package/dist/test/test_ipv4.js +561 -0
  52. package/dist/test/test_ipv6.d.ts +2 -0
  53. package/dist/test/test_ipv6.d.ts.map +1 -0
  54. package/dist/test/test_ipv6.js +313 -0
  55. package/dist/test/test_ipv6_loopback.d.ts +2 -0
  56. package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
  57. package/dist/test/test_ipv6_loopback.js +40 -0
  58. package/dist/test/test_ipv6_mapped.d.ts +2 -0
  59. package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
  60. package/dist/test/test_ipv6_mapped.js +89 -0
  61. package/dist/test/test_ipv6_unspec.d.ts +2 -0
  62. package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
  63. package/dist/test/test_ipv6_unspec.js +39 -0
  64. package/dist/test/test_prefix128.d.ts +2 -0
  65. package/dist/test/test_prefix128.d.ts.map +1 -0
  66. package/dist/test/test_prefix128.js +35 -0
  67. package/dist/test/test_prefix32.d.ts +2 -0
  68. package/dist/test/test_prefix32.d.ts.map +1 -0
  69. package/dist/test/test_prefix32.js +126 -0
  70. package/dist/test/test_rle.d.ts +2 -0
  71. package/dist/test/test_rle.d.ts.map +1 -0
  72. package/dist/test/test_rle.js +141 -0
  73. package/dist/vitest.config.d.ts +3 -0
  74. package/dist/vitest.config.d.ts.map +1 -0
  75. package/dist/vitest.config.js +11 -0
  76. package/package.json +40 -37
  77. package/src/crunchy.ts +546 -0
  78. package/src/index.ts +23 -0
  79. package/src/ip_bits.ts +148 -0
  80. package/src/ip_version.ts +6 -0
  81. package/src/ipaddress.ts +1331 -0
  82. package/src/ipv4.ts +190 -0
  83. package/src/ipv6.ts +202 -0
  84. package/src/ipv6_loopback.ts +55 -0
  85. package/src/ipv6_mapped.ts +163 -0
  86. package/src/ipv6_unspec.ts +59 -0
  87. package/src/prefix.ts +170 -0
  88. package/src/prefix128.ts +38 -0
  89. package/src/prefix32.ts +28 -0
  90. package/src/rle.ts +85 -0
  91. package/test/test_crunchy.ts +614 -0
  92. package/test/test_ipaddress.ts +208 -0
  93. package/test/test_ipv4.ts +596 -0
  94. package/test/test_ipv6.ts +325 -0
  95. package/test/test_ipv6_loopback.ts +52 -0
  96. package/test/test_ipv6_mapped.ts +101 -0
  97. package/test/test_ipv6_unspec.ts +51 -0
  98. package/test/test_prefix128.ts +39 -0
  99. package/test/test_prefix32.ts +130 -0
  100. package/test/test_rle.ts +144 -0
  101. package/LICENSE +0 -20
  102. package/README.md +0 -960
  103. package/index.d.ts +0 -12
  104. package/ipaddress.js +0 -2869
  105. package/ipaddress.js.map +0 -1
  106. package/ipv4.d.ts +0 -14
  107. package/ipv6.d.ts +0 -12
  108. package/ipv6_loopback.d.ts +0 -5
  109. package/ipv6_mapped.d.ts +0 -5
  110. package/ipv6_unspec.d.ts +0 -5
  111. package/prefix128.d.ts +0 -6
  112. package/prefix32.d.ts +0 -6
  113. package/rle.d.ts +0 -14
package/src/crunchy.ts ADDED
@@ -0,0 +1,546 @@
1
+ /**
2
+ * based
3
+ * Crunch - Arbitrary-precision integer arithmetic library
4
+ * Copyright (C) 2014 Nenad Vukicevic crunch.secureroom.net/license
5
+ *
6
+ */
7
+ /**
8
+ * @module Crunch
9
+ * Radix: 28 bits
10
+ * Endianness: Big
11
+ *
12
+ * @param {boolean} rawIn - expect 28-bit arrays
13
+ * @param {boolean} rawOut - return 28-bit arrays
14
+ */
15
+ // import Crunch from './crunch';
16
+
17
+ export class Crunchy {
18
+ num: number[] = [];
19
+ negative = false;
20
+
21
+ static zeroes: number[] = ((n: number): number[] => {
22
+ return new Array(n).fill(0);
23
+ })(60);
24
+
25
+ public clone(): Crunchy {
26
+ const ret = new Crunchy();
27
+ ret.num = this.num.slice();
28
+ ret.negative = this.negative;
29
+ return ret;
30
+ }
31
+
32
+ public static removeLeadingZeros(inn: number[]): number[] {
33
+ const out = inn.slice();
34
+ while (out[0] === 0 && out.length > 1) {
35
+ out.shift();
36
+ }
37
+ return out; // .transformOut();
38
+ }
39
+
40
+ public static from_14bit(a: number[]): Crunchy {
41
+ const ret = new Crunchy();
42
+ ret.num = a;
43
+ return ret;
44
+ }
45
+
46
+ public static from_8bit(a: number[]): Crunchy {
47
+ let x = [0, 0, 0, 0, 0, 0].slice((a.length - 1) % 7);
48
+ const z = new Crunchy();
49
+
50
+ if (a[0] < 0) {
51
+ a[0] *= -1;
52
+ z.negative = true;
53
+ } else {
54
+ z.negative = false;
55
+ }
56
+ x = x.concat(a);
57
+ for (let i = 0; i < x.length; i += 7) {
58
+ z.num.push(
59
+ x[i] * 1048576 + x[i + 1] * 4096 + x[i + 2] * 16 + (x[i + 3] >> 4),
60
+ (x[i + 3] & 15) * 16777216 + x[i + 4] * 65536 + x[i + 5] * 256 + x[i + 6],
61
+ );
62
+ }
63
+ z.num = Crunchy.removeLeadingZeros(z.num);
64
+ return z;
65
+ }
66
+
67
+ public static parse(val: string): Crunchy {
68
+ return Crunchy.from_string(val, 10);
69
+ }
70
+
71
+ public static from_number(val: number): Crunchy {
72
+ return Crunchy.parse("" + val);
73
+ }
74
+
75
+ public static from_string(val: string, radix = 10): Crunchy {
76
+ const x = val.split("");
77
+ let p = Crunchy.one();
78
+ let a = Crunchy.zero();
79
+ const b = Crunchy.from_8bit([radix]);
80
+ // console.log("fromstring:one:", p);
81
+ // console.log("fromstring:zero:", a);
82
+ // console.log("fromstring:radix:", b);
83
+ let n = false;
84
+
85
+ if (x[0] === "-") {
86
+ n = true;
87
+ x.shift();
88
+ }
89
+ while (x.length > 0) {
90
+ const c = parseInt(x.pop(), radix);
91
+ if (isNaN(c)) {
92
+ console.error("from_string:", val);
93
+ return null;
94
+ }
95
+ // console.log(a,p,b);
96
+ a = a.add(p.mul(Crunchy.from_8bit([c])));
97
+ p = p.mul(b);
98
+ }
99
+ a.negative = n;
100
+ return a;
101
+ }
102
+
103
+ public to_8bit(): number[] {
104
+ const x = [0].slice((this.num.length - 1) % 2).concat(this.num);
105
+ let z: number[] = [];
106
+ // z.num = [];
107
+
108
+ for (let i = 0; i < x.length; ) {
109
+ const u = x[i++];
110
+ const v = x[i++];
111
+
112
+ z.push(u >> 20, (u >> 12) & 255, (u >> 4) & 255, ((u << 4) | (v >> 24)) & 255, (v >> 16) & 255, (v >> 8) & 255, v & 255);
113
+ }
114
+ // console.log("co:", a, z);
115
+ z = Crunchy.removeLeadingZeros(z);
116
+
117
+ if (this.negative) {
118
+ z[0] *= -1;
119
+ }
120
+ return z;
121
+ }
122
+
123
+ public compare(y: Crunchy): number {
124
+ const xl = this.num.length;
125
+ const yl = y.num.length; // zero front pad problem
126
+
127
+ if (xl < yl) {
128
+ return -1;
129
+ } else if (xl > yl) {
130
+ return 1;
131
+ }
132
+
133
+ for (let i = 0; i < xl; i++) {
134
+ // console.log("x=y:", i, x, y);
135
+ if (this.num[i] < y.num[i]) return -1;
136
+ if (this.num[i] > y.num[i]) return 1;
137
+ }
138
+
139
+ return 0;
140
+ }
141
+
142
+ public eq(oth: Crunchy): boolean {
143
+ return this.compare(oth) == 0;
144
+ }
145
+
146
+ public lte(oth: Crunchy): boolean {
147
+ return this.compare(oth) <= 0;
148
+ }
149
+
150
+ public lt(oth: Crunchy): boolean {
151
+ return this.compare(oth) < 0;
152
+ }
153
+
154
+ public gt(oth: Crunchy): boolean {
155
+ return this.compare(oth) > 0;
156
+ }
157
+
158
+ public gte(oth: Crunchy): boolean {
159
+ return this.compare(oth) >= 0;
160
+ }
161
+
162
+ public add(y: Crunchy): Crunchy {
163
+ let z: Crunchy;
164
+ if (this.negative) {
165
+ if (y.negative) {
166
+ z = this.unsigned_add(y);
167
+ z.negative = true;
168
+ } else {
169
+ z = y.unsigned_sub(this, false).cut();
170
+ }
171
+ } else {
172
+ z = y.negative ? this.unsigned_sub(y, false).cut() : this.unsigned_add(y);
173
+ }
174
+ return z;
175
+ }
176
+
177
+ public unsigned_add(_y: Crunchy): Crunchy {
178
+ const n = this.num.length;
179
+ const t = _y.num.length;
180
+ let i = Math.max(n, t);
181
+ let c = 0;
182
+ const z = Crunchy.zeroes.slice(0, i);
183
+ // console.log("add:1:", new Date());
184
+ let x = this.num;
185
+ let y = _y.num;
186
+ if (n < t) {
187
+ x = Crunchy.zeroes.slice(0, t - n).concat(this.num);
188
+ } else if (n > t) {
189
+ y = Crunchy.zeroes.slice(0, n - t).concat(y);
190
+ }
191
+ // console.log("add:2:", new Date());
192
+ for (i -= 1; i >= 0; i--) {
193
+ z[i] = x[i] + y[i] + c;
194
+
195
+ if (z[i] > 268435455) {
196
+ c = 1;
197
+ z[i] -= 268435456;
198
+ } else {
199
+ c = 0;
200
+ }
201
+ }
202
+
203
+ // console.log("add:3:", new Date());
204
+ if (c === 1) {
205
+ z.unshift(c);
206
+ }
207
+ const ret = new Crunchy();
208
+ ret.num = z;
209
+ // console.log("add:4:", new Date());
210
+ return ret;
211
+ }
212
+
213
+ public sub(y: Crunchy): Crunchy {
214
+ let z: Crunchy;
215
+ if (this.negative) {
216
+ if (y.negative) {
217
+ // console.log("sub-c1");
218
+ z = y.unsigned_sub(this, false).cut();
219
+ } else {
220
+ // console.log("sub-c2");
221
+ z = this.unsigned_add(y);
222
+ z.negative = true;
223
+ }
224
+ } else {
225
+ // console.log("sub-c3", this, y);
226
+ z = y.negative ? this.unsigned_add(y) : this.unsigned_sub(y, false).cut();
227
+ }
228
+ return z;
229
+ }
230
+
231
+ public unsigned_sub(_y: Crunchy, internal = false): Crunchy {
232
+ const n = this.num.length;
233
+ const t = _y.num.length;
234
+ let i = Math.max(n, t);
235
+ let c = 0;
236
+ const z = Crunchy.zeroes.slice(0, i);
237
+ let x = this.num;
238
+ let y = _y.num;
239
+ if (n < t) {
240
+ x = Crunchy.zeroes.slice(0, t - n).concat(x);
241
+ } else if (n > t) {
242
+ y = Crunchy.zeroes.slice(0, n - t).concat(y);
243
+ }
244
+ for (i -= 1; i >= 0; i--) {
245
+ z[i] = x[i] - y[i] - c;
246
+ // console.log(z, x, y);
247
+
248
+ if (z[i] < 0) {
249
+ c = 1;
250
+ // console.log("pre:+", z[i], z);
251
+ z[i] += 268435456;
252
+ // console.log("pre:-", z[i], z);
253
+ } else {
254
+ c = 0;
255
+ }
256
+ }
257
+
258
+ let cry = new Crunchy();
259
+ cry.num = z;
260
+ if (c === 1 && !internal) {
261
+ const zero = new Crunchy();
262
+ zero.num = Crunchy.zeroes.slice(0, z.length);
263
+ cry = zero.unsigned_sub(cry, true);
264
+ cry.negative = true;
265
+ }
266
+ return cry;
267
+ }
268
+
269
+ public lsh(s: number): Crunchy {
270
+ const ss = s % 28;
271
+ const ls = Math.floor(s / 28);
272
+ let l = this.num.length;
273
+ const z: Crunchy = this.clone();
274
+ let t = 0;
275
+
276
+ if (ss) {
277
+ z.num = [];
278
+ while (l--) {
279
+ z.num[l] = ((this.num[l] << ss) + t) & 268435455;
280
+ t = this.num[l] >>> (28 - ss);
281
+ }
282
+
283
+ if (t !== 0) {
284
+ z.num.unshift(t);
285
+ }
286
+
287
+ z.negative = this.negative;
288
+ }
289
+ if (ls) {
290
+ z.num = z.num.concat(Crunchy.zeroes.slice(0, ls));
291
+ // return (ls) ? z.concat(zeroes.slice(0, ls)) : z;
292
+ }
293
+ // console.log(this, s, z);
294
+ return z;
295
+ }
296
+
297
+ // public leftShift(s: number): Crunchy {
298
+ // return this.transformIn()
299
+
300
+ // Crunchy.transformOut(Crunch.lsh(.pop(), s));
301
+ // }
302
+
303
+ // public shl(num: number): Crunchy {
304
+ // return Crunchy.from_8bit(Crunch.leftShift(this.num, num));
305
+ // }
306
+
307
+ public rsh(s: number): Crunchy {
308
+ const ss = s % 28;
309
+ const ls = Math.floor(s / 28);
310
+ let l = this.num.length - ls;
311
+ const z = this.clone();
312
+ z.num = this.num.slice(0, l);
313
+ if (ss) {
314
+ while (--l >= 0) {
315
+ z.num[l] = ((z.num[l] >> ss) | (z.num[l - 1] << (28 - ss))) & 268435455;
316
+ }
317
+
318
+ z.num[l] = z.num[l] >> ss;
319
+
320
+ if (z.num[0] === 0) {
321
+ z.num.shift();
322
+ }
323
+ }
324
+ return z;
325
+ }
326
+
327
+ // public shr(num: number): Crunchy {
328
+ // //console.log("shr<<", num, this.num);
329
+ // let res = this.rightShift(this.num, num);
330
+ // // console.log("shr>>", num, this.num, res);
331
+ // return Crunchy.from_8bit(res);
332
+ // }
333
+
334
+ public mul(y: Crunchy): Crunchy {
335
+ let yl: number;
336
+ let yh: number;
337
+ let c: number;
338
+ const n = this.num.length;
339
+ let i = y.num.length;
340
+ const z = Crunchy.zeroes.slice(0, n + i);
341
+
342
+ while (i--) {
343
+ c = 0;
344
+
345
+ yl = y.num[i] & 16383;
346
+ yh = y.num[i] >> 14;
347
+
348
+ for (let j = n - 1, xl: number, xh: number, t1: number, t2: number; j >= 0; j--) {
349
+ xl = this.num[j] & 16383;
350
+ xh = this.num[j] >> 14;
351
+
352
+ t1 = yh * xl + xh * yl;
353
+ t2 = yl * xl + ((t1 & 16383) << 14) + z[j + i + 1] + c;
354
+
355
+ z[j + i + 1] = t2 & 268435455;
356
+ c = yh * xh + (t1 >> 14) + (t2 >> 28);
357
+ }
358
+
359
+ z[i] = c;
360
+ }
361
+
362
+ if (z[0] === 0) {
363
+ z.shift();
364
+ }
365
+ const ret = new Crunchy();
366
+ ret.negative = this.negative !== y.negative;
367
+ ret.num = z;
368
+ return ret;
369
+ }
370
+
371
+ public static msb(x: number): number {
372
+ if (x !== 0) {
373
+ let z = 0;
374
+ for (let i = 134217728; i > x; z++) {
375
+ i /= 2;
376
+ }
377
+ return z;
378
+ }
379
+ }
380
+
381
+ // public transformIn(): Crunchy {
382
+ // // return Array.prototype.slice.call(a).map((v: Crunchy) => {
383
+ // // return Crunchy.from_8bit(v.num.slice())
384
+ // // });
385
+ // return null;
386
+ // }
387
+
388
+ // public transformOut(): Crunchy {
389
+ // console.log("in-transformOut:", this);
390
+ // let ret = Crunchy.co(this);
391
+ // console.log("out-transformOut:", this, ret);
392
+ // return ret;
393
+ // }
394
+
395
+ public shr(s: number): Crunchy {
396
+ const my = this.rsh(s);
397
+ return my.cut();
398
+ }
399
+
400
+ public shl(s: number): Crunchy {
401
+ return this.lsh(s).cut();
402
+
403
+ // Crunch.transformOut(Crunch.lsh(Crunch.transformIn([x]).pop(), s));
404
+ }
405
+
406
+ public cut(): Crunchy {
407
+ const out = this.clone();
408
+ // beasty hack
409
+ if (out.num.length == 0) {
410
+ out.num = [0];
411
+ return out;
412
+ }
413
+ while (out.num[0] === 0 && out.num.length > 1) {
414
+ out.num.shift();
415
+ }
416
+ return out; // .transformOut();
417
+ }
418
+ // return Crunchy.transformOut(this.num
419
+ // cut.apply(null, transformIn(arguments))
420
+ // );
421
+ // }
422
+
423
+ public div(y: Crunchy, internal = false): Crunchy {
424
+ if (y.num.length === 1 && y.num[0] === 0) {
425
+ return null;
426
+ }
427
+ // var u, v, xt, yt, d, q, k, i, z;
428
+ let u: Crunchy;
429
+ let v: Crunchy;
430
+ const s = Crunchy.msb(y.num[0]) - 1;
431
+ if (s > 0) {
432
+ u = this.lsh(s);
433
+ v = y.lsh(s);
434
+ } else {
435
+ u = this.clone();
436
+ v = y.clone();
437
+ }
438
+ const d = u.num.length - v.num.length;
439
+ const q = [0];
440
+ let k = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d)));
441
+ const yt = v.num[0] * 268435456 + v.num[1];
442
+
443
+ // only cmp as last resort
444
+ while (u.num[0] > k.num[0] || (u.num[0] === k.num[0] && u.compare(k) > -1)) {
445
+ q[0]++;
446
+ u = u.unsigned_sub(k, false);
447
+ }
448
+
449
+ for (let i = 1; i <= d; i++) {
450
+ q[i] = u.num[i - 1] === v.num[0] ? 268435455 : ~~((u.num[i - 1] * 268435456 + u.num[i]) / v.num[0]);
451
+
452
+ const xt = u.num[i - 1] * 72057594037927936 + u.num[i] * 268435456 + u.num[i + 1];
453
+ while (q[i] * yt > xt) {
454
+ // condition check can fail due to precision problem at 28-bit
455
+ q[i]--;
456
+ }
457
+
458
+ k = v.mul(Crunchy.from_14bit([q[i]]));
459
+ k.num = k.num.concat(Crunchy.zeroes.slice(0, d - i)); // concat after multiply, save cycles
460
+ u = u.unsigned_sub(k, false);
461
+
462
+ if (u.negative) {
463
+ u = Crunchy.from_14bit(v.num.concat(Crunchy.zeroes.slice(0, d - i))).unsigned_sub(u, false);
464
+ q[i]--;
465
+ }
466
+ }
467
+ let z: Crunchy;
468
+ if (internal) {
469
+ z = s > 0 ? u.cut().rsh(s) : u.cut();
470
+ } else {
471
+ z = Crunchy.from_14bit(Crunchy.removeLeadingZeros(q));
472
+ z.negative = this.negative !== y.negative;
473
+ }
474
+
475
+ return z;
476
+ }
477
+ // public sub(cry: Crunchy): Crunchy {
478
+ // return Crunchy.from_8bit(Crunch.sub(this.num, cry.num));
479
+ // }
480
+
481
+ public mod(y: Crunchy): Crunchy {
482
+ // For negative x, cmp doesn't work and result of div is negative
483
+ // so take result away from the modulus to get the correct result
484
+ if (this.negative) {
485
+ return y.sub(this.div(y, true));
486
+ }
487
+ switch (this.compare(y)) {
488
+ case -1:
489
+ return this;
490
+ case 0:
491
+ return Crunchy.from_8bit([0]);
492
+ default:
493
+ return this.div(y, true);
494
+ }
495
+ }
496
+
497
+ public mds(n: number): number {
498
+ let z = 0;
499
+ for (let i = 0, l = this.num.length; i < l; i++) {
500
+ z = ((this.num[i] >> 14) + (z << 14)) % n;
501
+ z = ((this.num[i] & 16383) + (z << 14)) % n;
502
+ }
503
+ return z;
504
+ }
505
+
506
+ public toString(radix = 10): string {
507
+ const a: string[] = [];
508
+ let i = 0;
509
+ let x = this.clone();
510
+ // console.log("toString:", new Date());
511
+ const cradix = Crunchy.from_8bit([radix]);
512
+ const zero = Crunchy.zero();
513
+ do {
514
+ const digit = x.mds(radix);
515
+ x = x.div(cradix);
516
+ a[i++] = "0123456789abcdef"[digit];
517
+ // console.log("1-toString:", x, radix, digit, a.join(""));
518
+ // console.log("2-toString:", x, radix, digit, a, Crunch.compare(x, Crunchy._zero.num));
519
+ } while (!x.eq(zero));
520
+ // console.log("Crunchy.tostring-1:", a);
521
+ const ret = a.reverse().join("");
522
+ // console.log("Crunchy.tostring-2:", ret, new Date());
523
+ return ret;
524
+ }
525
+
526
+ // public toNumber() : number {
527
+ // return null;
528
+ // }
529
+
530
+ static _zero = Crunchy.from_8bit([0]);
531
+ public static zero(): Crunchy {
532
+ return Crunchy._zero;
533
+ }
534
+
535
+ static _one = Crunchy.from_8bit([1]);
536
+ public static one(): Crunchy {
537
+ return Crunchy._one;
538
+ }
539
+
540
+ static _two = Crunchy.from_8bit([2]);
541
+ public static two(): Crunchy {
542
+ return Crunchy._two;
543
+ }
544
+ }
545
+
546
+ export default Crunchy;
package/src/index.ts ADDED
@@ -0,0 +1,23 @@
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
+
10
+ export * from "./crunchy.js";
11
+ export * from "./prefix.js";
12
+ export * from "./ip_bits.js";
13
+ export * from "./ip_version.js";
14
+ export * from "./ipaddress.js";
15
+ export * from "./ipv4.js";
16
+ export * from "./ipv6.js";
17
+ export * from "./ipv6_mapped.js";
18
+ export * from "./ipv6_loopback.js";
19
+ export * from "./ipv6_unspec.js";
20
+
21
+ // export = Crunchy;
22
+ export default IPAddress;
23
+ // declare let Ip: IPAddress;
package/src/ip_bits.ts ADDED
@@ -0,0 +1,148 @@
1
+ import Crunchy from "./crunchy.js";
2
+
3
+ import Rle from "./rle.js";
4
+
5
+ import IpVersion from "./ip_version.js";
6
+
7
+ export type ToString = (source: IpBits, num: Crunchy) => string;
8
+
9
+ // #[derive(Debug, Clone)]
10
+ export class IpBits {
11
+ version: IpVersion;
12
+ vt_as_compressed_string: ToString;
13
+ vt_as_uncompressed_string: ToString;
14
+ bits: number;
15
+ part_bits: number;
16
+ dns_bits: number;
17
+ rev_domain: string;
18
+ part_mod: number;
19
+ host_ofs: Crunchy; // ipv4=1, ipv6=0
20
+
21
+ public clone(): IpBits {
22
+ // let my = new IpBits();
23
+ // my.version = this.version;
24
+ // my.vt_as_compressed_string = this.vt_as_compressed_string;
25
+ // my.vt_as_uncompressed_string = this.vt_as_uncompressed_string;
26
+ // my.bits = this.bits;
27
+ // my.part_bits = this.part_bits;
28
+ // my.dns_bits = this.dns_bits;
29
+ // my.rev_domain = this.rev_domain;
30
+ // my.part_mod = this.part_mod;
31
+ // my.host_ofs = this.host_ofs.clone();
32
+ return this;
33
+ }
34
+
35
+ public parts(bu: Crunchy): number[] {
36
+ const vec: number[] = [];
37
+ let my = bu.clone();
38
+ const part_mod = Crunchy.one().shl(this.part_bits); // - Crunchy::one();
39
+ for (let i = 0; i < this.bits / this.part_bits; ++i) {
40
+ // console.log("parts-1:", my, part_mod, my.mod(part_mod), my.mod(part_mod).toString());
41
+ vec.push(+my.mod(part_mod).toString());
42
+ my = my.shr(this.part_bits);
43
+ }
44
+ // console.log("parts:", vec);
45
+ return vec.reverse();
46
+ }
47
+
48
+ public as_compressed_string(bu: Crunchy): string {
49
+ return this.vt_as_compressed_string(this, bu);
50
+ }
51
+
52
+ public as_uncompressed_string(bu: Crunchy): string {
53
+ return this.vt_as_uncompressed_string(this, bu);
54
+ }
55
+
56
+ public dns_part_format(i: number): string {
57
+ switch (this.version) {
58
+ case IpVersion.V4:
59
+ return `${i}`;
60
+ case IpVersion.V6:
61
+ return `${i.toString(16)}`;
62
+ }
63
+ }
64
+
65
+ public static _v4: IpBits = null;
66
+ public static v4(): IpBits {
67
+ if (IpBits._v4) {
68
+ return IpBits._v4;
69
+ }
70
+ const my = new IpBits();
71
+ IpBits._v4 = my;
72
+ my.version = IpVersion.V4;
73
+ my.vt_as_compressed_string = IpBits.ipv4_as_compressed;
74
+ my.vt_as_uncompressed_string = IpBits.ipv4_as_compressed;
75
+ my.bits = 32;
76
+ my.part_bits = 8;
77
+ my.dns_bits = 8;
78
+ my.rev_domain = "in-addr.arpa";
79
+ my.part_mod = 1 << 8;
80
+ my.host_ofs = Crunchy.one();
81
+ return my;
82
+ }
83
+
84
+ public static _v6: IpBits = null;
85
+ public static v6(): IpBits {
86
+ if (IpBits._v6) {
87
+ return IpBits._v6;
88
+ }
89
+ const my = new IpBits();
90
+ IpBits._v6 = my;
91
+ my.version = IpVersion.V6;
92
+ my.vt_as_compressed_string = IpBits.ipv6_as_compressed;
93
+ my.vt_as_uncompressed_string = IpBits.ipv6_as_uncompressed;
94
+ my.bits = 128;
95
+ my.part_bits = 16;
96
+ my.dns_bits = 4;
97
+ my.rev_domain = "ip6.arpa";
98
+ my.part_mod = 1 << 16;
99
+ my.host_ofs = Crunchy.zero();
100
+ return my;
101
+ }
102
+
103
+ public static ipv4_as_compressed(ip_bits: IpBits, host_address: Crunchy): string {
104
+ let ret = "";
105
+ let sep = "";
106
+ for (const part of ip_bits.parts(host_address)) {
107
+ ret += sep;
108
+ ret += `${part}`;
109
+ sep = ".";
110
+ }
111
+ return ret;
112
+ }
113
+
114
+ public static ipv6_as_compressed(ip_bits: IpBits, host_address: Crunchy): string {
115
+ // println!("ipv6_as_compressed:{}", host_address);
116
+ let ret = "";
117
+ let colon = "";
118
+ let done = false;
119
+ for (const rle of Rle.code(ip_bits.parts(host_address))) {
120
+ // console.log(rle.toString());
121
+ for (let _ = 0; _ < rle.cnt; _++) {
122
+ if (done || !(rle.part == 0 && rle.max)) {
123
+ ret += `${colon}${rle.part.toString(16)}`;
124
+ colon = ":";
125
+ } else if (rle.part == 0 && rle.max) {
126
+ ret += "::";
127
+ colon = "";
128
+ done = true;
129
+ break;
130
+ }
131
+ }
132
+ }
133
+ return ret;
134
+ }
135
+
136
+ public static ipv6_as_uncompressed(ip_bits: IpBits, host_address: Crunchy): string {
137
+ let ret = "";
138
+ let sep = "";
139
+ for (const part of ip_bits.parts(host_address)) {
140
+ ret += sep;
141
+ ret += (0x10000 + part).toString(16).slice(1);
142
+ sep = ":";
143
+ }
144
+ return ret;
145
+ }
146
+ }
147
+
148
+ export default IpBits;
@@ -0,0 +1,6 @@
1
+ enum IpVersion {
2
+ V4 = 4,
3
+ V6 = 6,
4
+ }
5
+
6
+ export default IpVersion;