Repository navigation
Expand file tree
/
Copy pathutils_native.js
More file actions
181 lines (170 loc) · 5 KB
/
Copy pathutils_native.js
File metadata and controls
181 lines (170 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* global BigInt */
import * as Scalar from "./scalar.js";
export function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || o.eq !== undefined) {
return o.toString(10);
} else if (o instanceof Uint8Array) {
return Scalar.fromRprLE(o, 0);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = stringifyBigInts(o[k]);
});
return res;
} else {
return o;
}
}
export function unstringifyBigInts(o) {
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
return BigInt(o);
} else if ((typeof(o) == "string") && (/^0x[0-9a-fA-F]+$/.test(o) )) {
return BigInt(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
} else if (typeof o == "object") {
if (o===null) return null;
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = unstringifyBigInts(o[k]);
});
return res;
} else {
return o;
}
}
export function beBuff2int(buff) {
let res = BigInt(0);
let i = buff.length;
let offset = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i>0) {
if (i >= 4) {
i -= 4;
res += BigInt(buffV.getUint32(i)) << BigInt(offset*8);
offset += 4;
} else if (i >= 2) {
i -= 2;
res += BigInt(buffV.getUint16(i)) << BigInt(offset*8);
offset += 2;
} else {
i -= 1;
res += BigInt(buffV.getUint8(i)) << BigInt(offset*8);
offset += 1;
}
}
return res;
}
export function beInt2Buff(n, len) {
let r = n;
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = len;
while (o > 0) {
if (o-4 >= 0) {
o -= 4;
buffV.setUint32(o, Number(r & BigInt(0xFFFFFFFF)));
r = r >> BigInt(32);
} else if (o-2 >= 0) {
o -= 2;
buffV.setUint16(o, Number(r & BigInt(0xFFFF)));
r = r >> BigInt(16);
} else {
o -= 1;
buffV.setUint8(o, Number(r & BigInt(0xFF)));
r = r >> BigInt(8);
}
}
if (r) {
throw new Error("Number does not fit in this length");
}
return buff;
}
export function leBuff2int(buff) {
let res = BigInt(0);
let i = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i<buff.length) {
if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint32(i, true)) << BigInt( i*8);
i += 4;
} else if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint16(i, true)) << BigInt( i*8);
i += 2;
} else {
res += BigInt(buffV.getUint8(i, true)) << BigInt( i*8);
i += 1;
}
}
return res;
}
export function leInt2Buff(n, len) {
let r = n;
if (typeof len === "undefined") {
len = Math.floor((Scalar.bitLength(n) - 1) / 8) +1;
if (len==0) len = 1;
}
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = 0;
while (o < len) {
if (o+4 <= len) {
buffV.setUint32(o, Number(r & BigInt(0xFFFFFFFF)), true );
o += 4;
r = r >> BigInt(32);
} else if (o+2 <= len) {
buffV.setUint16(Number(o, r & BigInt(0xFFFF)), true );
o += 2;
r = r >> BigInt(16);
} else {
buffV.setUint8(Number(o, r & BigInt(0xFF)), true );
o += 1;
r = r >> BigInt(8);
}
}
if (r) {
throw new Error("Number does not fit in this length");
}
return buff;
}
export function stringifyFElements(F, o) {
if ((typeof(o) == "bigint") || o.eq !== undefined) {
return o.toString(10);
} else if (o instanceof Uint8Array) {
return F.toString(F.e(o));
} else if (Array.isArray(o)) {
return o.map(stringifyFElements.bind(this,F));
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = stringifyFElements(F, o[k]);
});
return res;
} else {
return o;
}
}
export function unstringifyFElements(F, o) {
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
return F.e(o);
} else if ((typeof(o) == "string") && (/^0x[0-9a-fA-F]+$/.test(o) )) {
return F.e(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyFElements.bind(this,F));
} else if (typeof o == "object") {
if (o===null) return null;
const res = {};
const keys = Object.keys(o);
keys.forEach( (k) => {
res[k] = unstringifyFElements(F, o[k]);
});
return res;
} else {
return o;
}
}