@thi.ng/hex 2.3.23 → 2.3.24
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/index.js +75 -190
- package/package.json +6 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -1,191 +1,76 @@
|
|
|
1
|
-
const P32 =
|
|
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
|
-
* Returns hex string of 32bit uint, read in big-endian order from given byte
|
|
77
|
-
* array at index `i`.
|
|
78
|
-
*
|
|
79
|
-
* @param x -
|
|
80
|
-
* @param i -
|
|
81
|
-
*/
|
|
82
|
-
export const U32BE = (x, i) => U16BE(x, i) + U16BE(x, i + 2);
|
|
83
|
-
/**
|
|
84
|
-
* Returns hex string of 32bit uint, read in litte-endian order from given byte
|
|
85
|
-
* array at index `i`.
|
|
86
|
-
*
|
|
87
|
-
* @param x -
|
|
88
|
-
* @param i -
|
|
89
|
-
*/
|
|
90
|
-
export const U32LE = (x, i) => U16LE(x, i + 2) + U16LE(x, i);
|
|
91
|
-
/**
|
|
92
|
-
* Returns 48bit uint as hex string
|
|
93
|
-
*
|
|
94
|
-
* @param x -
|
|
95
|
-
*/
|
|
96
|
-
export const U48 = (x) => U48HL(x / P32, x % P32);
|
|
97
|
-
/**
|
|
98
|
-
* Similar to {@link U48}, but takes the 64bit arg as 2x 32bit values.
|
|
99
|
-
*
|
|
100
|
-
* @param hi -
|
|
101
|
-
* @param lo -
|
|
102
|
-
*/
|
|
103
|
-
export const U48HL = (hi, lo) => U16(hi) + U32(lo);
|
|
104
|
-
/**
|
|
105
|
-
* Returns hex string of 48bit uint, read in big-endian order from given byte
|
|
106
|
-
* array at index `i`.
|
|
107
|
-
*
|
|
108
|
-
* @param x -
|
|
109
|
-
* @param i -
|
|
110
|
-
*/
|
|
111
|
-
export const U48BE = (x, i) => U16BE(x, i) + U32BE(x, i + 2);
|
|
112
|
-
/**
|
|
113
|
-
* Returns hex string of 48bit uint, read in litte-endian order from given byte
|
|
114
|
-
* array at index `i`.
|
|
115
|
-
*
|
|
116
|
-
* @param x -
|
|
117
|
-
* @param i -
|
|
118
|
-
*/
|
|
119
|
-
export const U48LE = (x, i) => U16LE(x, i + 4) + U32LE(x, i);
|
|
120
|
-
/**
|
|
121
|
-
* Returns 64bit uint as hex string.
|
|
122
|
-
*
|
|
123
|
-
* @remarks
|
|
124
|
-
* Note: JS numbers are only integer precise up to `2**53 - 1`. Use
|
|
125
|
-
* {@link U64BE} or {@link U64LE} for byte array based values (full 64bit range
|
|
126
|
-
* supported). Alternatively, use `BigInt(x).toString(16)`.
|
|
127
|
-
*
|
|
128
|
-
* Also see {@link U64BIG} for `bigint` version.
|
|
129
|
-
*
|
|
130
|
-
* @param x -
|
|
131
|
-
*/
|
|
132
|
-
export const U64 = (x) => U64HL(x / P32, x % P32);
|
|
133
|
-
/**
|
|
134
|
-
* Returns 64bit bigint as hex string.
|
|
135
|
-
*
|
|
136
|
-
* @param x
|
|
137
|
-
*/
|
|
138
|
-
export const U64BIG = (x) => U64HL(Number(x >> BigInt(32)), Number(x & BigInt(P32 - 1)));
|
|
139
|
-
/**
|
|
140
|
-
* Similar to {@link U64}, but takes the 64bit arg as 2x 32bit values.
|
|
141
|
-
*
|
|
142
|
-
* @param hi -
|
|
143
|
-
* @param lo -
|
|
144
|
-
*/
|
|
145
|
-
export const U64HL = (hi, lo) => U32(hi) + U32(lo);
|
|
146
|
-
/**
|
|
147
|
-
* Returns hex string of 64bit uint, read in big-endian order from given byte
|
|
148
|
-
* array at index `i`.
|
|
149
|
-
*
|
|
150
|
-
* @param x -
|
|
151
|
-
* @param i -
|
|
152
|
-
*/
|
|
153
|
-
export const U64BE = (x, i) => U32BE(x, i) + U32BE(x, i + 4);
|
|
154
|
-
/**
|
|
155
|
-
* Returns hex string of 64bit uint, read in litte-endian order from given byte
|
|
156
|
-
* array at index `i`.
|
|
157
|
-
*
|
|
158
|
-
* @param x -
|
|
159
|
-
* @param i -
|
|
160
|
-
*/
|
|
161
|
-
export const U64LE = (x, i) => U32LE(x, i + 4) + U32LE(x, i);
|
|
162
|
-
/**
|
|
163
|
-
* Returns UUID formatted string of given byte array from optional start index
|
|
164
|
-
* `i` (default: 0). Array must have min. length 16 (starting from `i`).
|
|
165
|
-
*
|
|
166
|
-
* @param id -
|
|
167
|
-
* @param i -
|
|
168
|
-
*/
|
|
169
|
-
export const uuid = (id, i = 0) =>
|
|
170
|
-
// prettier-ignore
|
|
171
|
-
`${U32BE(id, i)}-${U16BE(id, i + 4)}-${U16BE(id, i + 6)}-${U16BE(id, i + 8)}-${U48BE(id, i + 10)}`;
|
|
172
|
-
export const hexdump = (bytes, addr, len, width, ascii) => hexdumpLines(bytes, addr, len, width, ascii).join("\n");
|
|
173
|
-
export const hexdumpLines = (bytes, addr, len, width = 16, ascii = true) => {
|
|
174
|
-
len = Math.min(len, bytes.length - addr);
|
|
175
|
-
let res = [];
|
|
176
|
-
while (len > 0) {
|
|
177
|
-
const row = [...bytes.subarray(addr, addr + Math.min(len, width))];
|
|
178
|
-
const pad = len < width && ascii ? new Array(width - len).fill(" ") : [];
|
|
179
|
-
res.push([
|
|
180
|
-
U32(addr),
|
|
181
|
-
...row.map(U8),
|
|
182
|
-
...pad,
|
|
183
|
-
row
|
|
184
|
-
.map((x) => x >= 0x20 && x < 0x80 ? String.fromCharCode(x) : ".")
|
|
185
|
-
.join(""),
|
|
186
|
-
].join(" "));
|
|
187
|
-
addr += width;
|
|
188
|
-
len -= width;
|
|
189
|
-
}
|
|
190
|
-
return res;
|
|
1
|
+
const P32 = 4294967296;
|
|
2
|
+
const HEX = "0123456789abcdef";
|
|
3
|
+
const U4 = (x) => HEX[x & 15];
|
|
4
|
+
const U8 = (x) => HEX[x >>> 4 & 15] + HEX[x & 15];
|
|
5
|
+
const U8A = (x, i) => U8(x[i]);
|
|
6
|
+
const U16 = (x) => U8(x >>> 8) + U8(x & 255);
|
|
7
|
+
const U16BE = (x, i) => U8(x[i]) + U8(x[i + 1]);
|
|
8
|
+
const U16LE = (x, i) => U8(x[i + 1]) + U8(x[i]);
|
|
9
|
+
const U24 = (x) => U8(x >>> 16) + U16(x);
|
|
10
|
+
const U24BE = (x, i) => U8(x[i]) + U16BE(x, i + 1);
|
|
11
|
+
const U24LE = (x, i) => U8(x[i + 2]) + U16LE(x, i);
|
|
12
|
+
const U32 = (x) => U16(x >>> 16) + U16(x);
|
|
13
|
+
const U32BE = (x, i) => U16BE(x, i) + U16BE(x, i + 2);
|
|
14
|
+
const U32LE = (x, i) => U16LE(x, i + 2) + U16LE(x, i);
|
|
15
|
+
const U48 = (x) => U48HL(x / P32, x % P32);
|
|
16
|
+
const U48HL = (hi, lo) => U16(hi) + U32(lo);
|
|
17
|
+
const U48BE = (x, i) => U16BE(x, i) + U32BE(x, i + 2);
|
|
18
|
+
const U48LE = (x, i) => U16LE(x, i + 4) + U32LE(x, i);
|
|
19
|
+
const U64 = (x) => U64HL(x / P32, x % P32);
|
|
20
|
+
const U64BIG = (x) => U64HL(Number(x >> BigInt(32)), Number(x & BigInt(P32 - 1)));
|
|
21
|
+
const U64HL = (hi, lo) => U32(hi) + U32(lo);
|
|
22
|
+
const U64BE = (x, i) => U32BE(x, i) + U32BE(x, i + 4);
|
|
23
|
+
const U64LE = (x, i) => U32LE(x, i + 4) + U32LE(x, i);
|
|
24
|
+
const uuid = (id, i = 0) => (
|
|
25
|
+
// prettier-ignore
|
|
26
|
+
`${U32BE(id, i)}-${U16BE(id, i + 4)}-${U16BE(id, i + 6)}-${U16BE(id, i + 8)}-${U48BE(id, i + 10)}`
|
|
27
|
+
);
|
|
28
|
+
const hexdump = (bytes, addr, len, width, ascii) => hexdumpLines(bytes, addr, len, width, ascii).join("\n");
|
|
29
|
+
const hexdumpLines = (bytes, addr, len, width = 16, ascii = true) => {
|
|
30
|
+
len = Math.min(len, bytes.length - addr);
|
|
31
|
+
let res = [];
|
|
32
|
+
while (len > 0) {
|
|
33
|
+
const row = [...bytes.subarray(addr, addr + Math.min(len, width))];
|
|
34
|
+
const pad = len < width && ascii ? new Array(width - len).fill(" ") : [];
|
|
35
|
+
res.push(
|
|
36
|
+
[
|
|
37
|
+
U32(addr),
|
|
38
|
+
...row.map(U8),
|
|
39
|
+
...pad,
|
|
40
|
+
row.map(
|
|
41
|
+
(x) => x >= 32 && x < 128 ? String.fromCharCode(x) : "."
|
|
42
|
+
).join("")
|
|
43
|
+
].join(" ")
|
|
44
|
+
);
|
|
45
|
+
addr += width;
|
|
46
|
+
len -= width;
|
|
47
|
+
}
|
|
48
|
+
return res;
|
|
49
|
+
};
|
|
50
|
+
export {
|
|
51
|
+
HEX,
|
|
52
|
+
U16,
|
|
53
|
+
U16BE,
|
|
54
|
+
U16LE,
|
|
55
|
+
U24,
|
|
56
|
+
U24BE,
|
|
57
|
+
U24LE,
|
|
58
|
+
U32,
|
|
59
|
+
U32BE,
|
|
60
|
+
U32LE,
|
|
61
|
+
U4,
|
|
62
|
+
U48,
|
|
63
|
+
U48BE,
|
|
64
|
+
U48HL,
|
|
65
|
+
U48LE,
|
|
66
|
+
U64,
|
|
67
|
+
U64BE,
|
|
68
|
+
U64BIG,
|
|
69
|
+
U64HL,
|
|
70
|
+
U64LE,
|
|
71
|
+
U8,
|
|
72
|
+
U8A,
|
|
73
|
+
hexdump,
|
|
74
|
+
hexdumpLines,
|
|
75
|
+
uuid
|
|
191
76
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/hex",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.24",
|
|
4
4
|
"description": "Hex string formatters for 4/8/16/24/32/48/64bit words",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -34,6 +36,7 @@
|
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
38
|
"@microsoft/api-extractor": "^7.38.3",
|
|
39
|
+
"esbuild": "^0.19.8",
|
|
37
40
|
"rimraf": "^5.0.5",
|
|
38
41
|
"tools": "^0.0.1",
|
|
39
42
|
"typedoc": "^0.25.4",
|
|
@@ -67,5 +70,5 @@
|
|
|
67
70
|
],
|
|
68
71
|
"year": 2020
|
|
69
72
|
},
|
|
70
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
71
74
|
}
|