]>
code.delx.au - monosys/blob - bin/hexhost
8 ...'0123456789'.split(''),
9 ...'abcdefghijklmnopqrstuvwxyz'.split(''),
13 const SHIFT
= BigInt(Math
.ceil(Math
.log2(CHARS1
.length
** BLOCKSIZE
)));
14 const MASK
= 2n
**SHIFT
- 1n
;
16 const CHARSN
= [...Array(BLOCKSIZE
- 1)].reduce((acc
) => acc
.map((v1
) => CHARS1
.map((v2
) => ''+v1
+v2
)).flat(), CHARS1
);
17 const FMAP
= new Map(CHARSN
.map((v
, i
) => [''+v
, BigInt(i
)]));
18 const RMAP
= new Map(CHARSN
.map((v
, i
) => [BigInt(i
), ''+v
]));
20 function main(arg1
, arg2
) {
22 console
.error('Usage: hexhost fdxx::4a59954e');
23 console
.error('Usage: hexhost fdxx:: hostname');
29 const suffix
= encode(arg2
).replaceAll(/(.{4})/g, '$1:').replace(/:$/, '');
30 console
.log(prefix
+ suffix
);
32 const [, suffix
] = arg1
.split(/::|:0:/);
33 console
.log(decode(suffix
));
37 function decode(input
) {
38 input
= input
&& input
.replaceAll(':', '');
40 throw new Error('No suffix found');
42 input
= BigInt('0x' + input
);
45 const encodedBlock
= input
& MASK
;
47 const block
= RMAP
.get(encodedBlock
);
48 if (block
!== undefined) {
52 return output
.reverse().join('').trim();
55 function encode(input
) {
56 if (input
.length
/ BLOCKSIZE
> (64n
/ SHIFT
)) {
57 throw new Error('Input is too long to fit in a /64!');
60 input
= input
.toLowerCase();
63 for (let i
= 0; i
< input
.length
; i
+= BLOCKSIZE
) {
64 const block
= input
.substring(i
, i
+ BLOCKSIZE
).padEnd(BLOCKSIZE
);
65 const encodedBlock
= FMAP
.get(block
);
66 if (encodedBlock
!== undefined) {
67 out
= (out
<< SHIFT
) + encodedBlock
;
70 return out
.toString(16);
73 main(process
.argv
[2], process
.argv
[3]);