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