|
6 | 6 | * it under the terms of copyleft-next 0.3.1. See copyleft-next-0.3.1.txt. |
7 | 7 | */ |
8 | 8 |
|
9 | | -'use strict'; |
10 | | - |
11 | | -(function (factory) { |
12 | | - const imports = ['require']; |
13 | | - |
14 | | - if (typeof define === 'function' && define.amd) { |
15 | | - define(imports, factory); |
16 | | - } else if (typeof module === 'object' && module.exports) { |
17 | | - module.exports = factory(require); |
18 | | - } else { |
19 | | - console.log('Module system not recognized, please use AMD or CommonJS'); |
20 | | - } |
21 | | -})(function (require) { |
22 | | - const MNET32 = 'ybndrfg8ejkmcpqxot1uwisza345h769'; |
23 | | - const MNET32Reverse = {}; |
24 | | - for (let i = 0; i < MNET32.length; i++) { |
25 | | - MNET32Reverse[MNET32[i]] = i; |
26 | | - } |
27 | | - |
28 | | - function IntegerTooLargeException(message) { |
29 | | - this.message = message; |
30 | | - this.name = 'IntegerTooLargeException'; |
31 | | - } |
32 | | - |
33 | | - function getBit(byte, offset) { |
34 | | - return (byte >> offset) & 0x01; |
35 | | - } |
36 | | - |
37 | | - function to5bit(ab) { |
38 | | - const bytes = new Uint8Array(ab); |
39 | | - |
40 | | - let bitCount = 0; |
41 | | - let chunks = []; |
42 | | - let currentChunk = 0; |
43 | | - for (let i = bytes.length - 1; i >= 0; i--) { |
44 | | - for (let j = 0; j < 8; j++) { |
45 | | - currentChunk = (currentChunk >> 1) | (getBit(bytes[i], j) << 4); |
46 | | - if (++bitCount > 4) { |
47 | | - bitCount = 0; |
48 | | - chunks.push(currentChunk); |
49 | | - currentChunk = 0; |
50 | | - } |
| 9 | +const MNET32 = 'ybndrfg8ejkmcpqxot1uwisza345h769'; |
| 10 | +const MNET32Reverse = {}; |
| 11 | +for (let i = 0; i < MNET32.length; i++) { |
| 12 | + MNET32Reverse[MNET32[i]] = i; |
| 13 | +} |
| 14 | + |
| 15 | +function IntegerTooLargeException(message) { |
| 16 | + this.message = message; |
| 17 | + this.name = 'IntegerTooLargeException'; |
| 18 | +} |
| 19 | + |
| 20 | +function getBit(byte, offset) { |
| 21 | + return (byte >> offset) & 0x01; |
| 22 | +} |
| 23 | + |
| 24 | +export function to5bit(ab) { |
| 25 | + const bytes = new Uint8Array(ab); |
| 26 | + |
| 27 | + let bitCount = 0; |
| 28 | + let chunks = []; |
| 29 | + let currentChunk = 0; |
| 30 | + for (let i = bytes.length - 1; i >= 0; i--) { |
| 31 | + for (let j = 0; j < 8; j++) { |
| 32 | + currentChunk = (currentChunk >> 1) | (getBit(bytes[i], j) << 4); |
| 33 | + if (++bitCount > 4) { |
| 34 | + bitCount = 0; |
| 35 | + chunks.push(currentChunk); |
| 36 | + currentChunk = 0; |
51 | 37 | } |
52 | 38 | } |
53 | | - |
54 | | - if (bitCount) { |
55 | | - chunks.push(currentChunk >> (5 - bitCount)); |
56 | | - } |
57 | | - |
58 | | - chunks.reverse(); |
59 | | - return chunks; |
60 | 39 | } |
61 | 40 |
|
62 | | - function from5bit(ab) { |
63 | | - const chunks = new Uint8Array(ab); |
64 | | - |
65 | | - let bitCount = 0; |
66 | | - let bytes = []; |
67 | | - let currentByte = 0; |
68 | | - for (let i = chunks.length - 1; i >= 0; i--) { |
69 | | - for (let j = 0; j < 5; j++) { |
70 | | - currentByte = (currentByte >> 1) | (getBit(chunks[i], j) << 7); |
71 | | - if (++bitCount > 7) { |
72 | | - bitCount = 0; |
73 | | - bytes.push(currentByte); |
74 | | - currentByte = 0; |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - bytes.reverse(); |
80 | | - return new Uint8Array(bytes); |
81 | | - } |
82 | | - |
83 | | - function toNumber(ab) { |
84 | | - return ( |
85 | | - (ab[0] << 30) | |
86 | | - (ab[1] << 25) | |
87 | | - (ab[2] << 20) | |
88 | | - (ab[3] << 15) | |
89 | | - (ab[4] << 10) | |
90 | | - (ab[5] << 5) | |
91 | | - ab[6] |
92 | | - ); |
| 41 | + if (bitCount) { |
| 42 | + chunks.push(currentChunk >> (5 - bitCount)); |
93 | 43 | } |
94 | 44 |
|
95 | | - function fromNumber(num) { |
96 | | - if (num < 0 || num > 0x7fffffff) { |
97 | | - throw new IntegerTooLargeException('fromNumber expects a 32-bit signed integer'); |
| 45 | + chunks.reverse(); |
| 46 | + return chunks; |
| 47 | +} |
| 48 | + |
| 49 | +export function from5bit(ab) { |
| 50 | + const chunks = new Uint8Array(ab); |
| 51 | + |
| 52 | + let bitCount = 0; |
| 53 | + let bytes = []; |
| 54 | + let currentByte = 0; |
| 55 | + for (let i = chunks.length - 1; i >= 0; i--) { |
| 56 | + for (let j = 0; j < 5; j++) { |
| 57 | + currentByte = (currentByte >> 1) | (getBit(chunks[i], j) << 7); |
| 58 | + if (++bitCount > 7) { |
| 59 | + bitCount = 0; |
| 60 | + bytes.push(currentByte); |
| 61 | + currentByte = 0; |
| 62 | + } |
98 | 63 | } |
99 | | - |
100 | | - // Although javascript Numbers can safely represent integers up to (2^53)-1, the |
101 | | - // bitwise operators operate on them as 32-bit signed integers. |
102 | | - // FIXME: look for a popular bigint library which can easily convert big integers as |
103 | | - // ArrayBuffers. |
104 | | - return new Uint8Array([ |
105 | | - (num >>> 30) & 0x03, |
106 | | - (num >>> 25) & 0x1f, |
107 | | - (num >>> 20) & 0x1f, |
108 | | - (num >>> 15) & 0x1f, |
109 | | - (num >>> 10) & 0x1f, |
110 | | - (num >>> 5) & 0x1f, |
111 | | - (num >>> 0) & 0x1f, |
112 | | - ]); |
113 | | - } |
114 | | - |
115 | | - function decode32bitNumber(str) { |
116 | | - return toNumber(decode(str)); |
117 | | - } |
118 | | - |
119 | | - function encode32bitNumber(int) { |
120 | | - return encode(fromNumber(int)); |
121 | | - } |
122 | | - |
123 | | - function decode(x) { |
124 | | - return from5bit(x.split('').map((chr) => MNET32Reverse[chr])); |
125 | 64 | } |
126 | 65 |
|
127 | | - function encode(x) { |
128 | | - return to5bit(x) |
129 | | - .map((value) => MNET32[value]) |
130 | | - .join(''); |
| 66 | + bytes.reverse(); |
| 67 | + return new Uint8Array(bytes); |
| 68 | +} |
| 69 | + |
| 70 | +export function toNumber(ab) { |
| 71 | + return ( |
| 72 | + (ab[0] << 30) | |
| 73 | + (ab[1] << 25) | |
| 74 | + (ab[2] << 20) | |
| 75 | + (ab[3] << 15) | |
| 76 | + (ab[4] << 10) | |
| 77 | + (ab[5] << 5) | |
| 78 | + ab[6] |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +export function fromNumber(num) { |
| 83 | + if (num < 0 || num > 0x7fffffff) { |
| 84 | + throw new IntegerTooLargeException('fromNumber expects a 32-bit signed integer'); |
131 | 85 | } |
132 | 86 |
|
133 | | - return { |
134 | | - decode: decode, |
135 | | - encode: encode, |
136 | | - from5bit: from5bit, |
137 | | - to5bit: to5bit, |
138 | | - fromNumber: fromNumber, |
139 | | - toNumber: toNumber, |
140 | | - decode32bitNumber: decode32bitNumber, |
141 | | - encode32bitNumber: encode32bitNumber, |
142 | | - }; |
143 | | -}); |
| 87 | + // Although javascript Numbers can safely represent integers up to (2^53)-1, the |
| 88 | + // bitwise operators operate on them as 32-bit signed integers. |
| 89 | + // FIXME: look for a popular bigint library which can easily convert big integers as |
| 90 | + // ArrayBuffers. |
| 91 | + return new Uint8Array([ |
| 92 | + (num >>> 30) & 0x03, |
| 93 | + (num >>> 25) & 0x1f, |
| 94 | + (num >>> 20) & 0x1f, |
| 95 | + (num >>> 15) & 0x1f, |
| 96 | + (num >>> 10) & 0x1f, |
| 97 | + (num >>> 5) & 0x1f, |
| 98 | + (num >>> 0) & 0x1f, |
| 99 | + ]); |
| 100 | +} |
| 101 | + |
| 102 | +export function decode32bitNumber(str) { |
| 103 | + return toNumber(decode(str)); |
| 104 | +} |
| 105 | + |
| 106 | +export function encode32bitNumber(int) { |
| 107 | + return encode(fromNumber(int)); |
| 108 | +} |
| 109 | + |
| 110 | +export function decode(x) { |
| 111 | + return from5bit(x.split('').map((chr) => MNET32Reverse[chr])); |
| 112 | +} |
| 113 | + |
| 114 | +export function encode(x) { |
| 115 | + return to5bit(x) |
| 116 | + .map((value) => MNET32[value]) |
| 117 | + .join(''); |
| 118 | +} |
| 119 | + |
| 120 | +const zbase32 = { |
| 121 | + decode: decode, |
| 122 | + encode: encode, |
| 123 | + from5bit: from5bit, |
| 124 | + to5bit: to5bit, |
| 125 | + fromNumber: fromNumber, |
| 126 | + toNumber: toNumber, |
| 127 | + decode32bitNumber: decode32bitNumber, |
| 128 | + encode32bitNumber: encode32bitNumber, |
| 129 | +}; |
| 130 | + |
| 131 | +export default zbase32; |
0 commit comments