@ddunigma/node 1.0.26 → 1.0.27
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/README.md +4 -0
- package/dist/cjs/Custom64.d.ts +29 -0
- package/dist/cjs/Custom64.js +205 -0
- package/dist/cjs/Ddu1024.d.ts +26 -0
- package/dist/cjs/Ddu1024.js +262 -0
- package/dist/cjs/Ddu128.d.ts +26 -0
- package/dist/cjs/Ddu128.js +150 -0
- package/dist/cjs/Ddu512.d.ts +26 -0
- package/dist/cjs/Ddu512.js +198 -0
- package/dist/cjs/Ddu64.d.ts +29 -0
- package/dist/cjs/Ddu64.js +149 -0
- package/dist/cjs/index.d.ts +5 -58
- package/dist/cjs/index.js +11 -329
- package/dist/mjs/Custom64.d.ts +29 -0
- package/dist/mjs/Custom64.js +203 -0
- package/dist/mjs/Ddu1024.d.ts +26 -0
- package/dist/mjs/Ddu1024.js +261 -0
- package/dist/mjs/Ddu128.d.ts +26 -0
- package/dist/mjs/Ddu128.js +149 -0
- package/dist/mjs/Ddu512.d.ts +26 -0
- package/dist/mjs/Ddu512.js +197 -0
- package/dist/mjs/Ddu64.d.ts +29 -0
- package/dist/mjs/Ddu64.js +147 -0
- package/dist/mjs/index.d.ts +5 -58
- package/dist/mjs/index.js +5 -330
- package/package.json +2 -3
- package/dist/cjs/test/test2.d.ts +0 -1
- package/dist/cjs/test/test2.js +0 -44
- package/dist/cjs/test/test3.d.ts +0 -1
- package/dist/cjs/test/test3.js +0 -20
- package/dist/cjs/test/tester2.d.ts +0 -1
- package/dist/cjs/test/tester2.js +0 -46
- package/dist/mjs/test/test2.d.ts +0 -1
- package/dist/mjs/test/test2.js +0 -42
- package/dist/mjs/test/test3.d.ts +0 -1
- package/dist/mjs/test/test3.js +0 -18
- package/dist/mjs/test/tester2.d.ts +0 -1
- package/dist/mjs/test/tester2.js +0 -44
package/dist/cjs/index.js
CHANGED
|
@@ -1,331 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Custom64 = exports.Ddu64 = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
".",
|
|
15
|
-
];
|
|
16
|
-
this.paddingCharKr = "뭐";
|
|
17
|
-
this.defaultEncoding = "utf-8";
|
|
18
|
-
this.bitLengthMap = new Map();
|
|
19
|
-
this.binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
20
|
-
this.dduBinaryLookup = new Map();
|
|
21
|
-
this.dduBinaryLookupKr = new Map();
|
|
22
|
-
this.paddingRegex = new Map();
|
|
23
|
-
if (typeof dduChar === "string") {
|
|
24
|
-
const removeDuplicatesString = [...new Set(dduChar.trim())].join("");
|
|
25
|
-
this.dduChar = removeDuplicatesString.trim().split("");
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
this.dduChar = dduChar || this.dduCharKr;
|
|
29
|
-
}
|
|
30
|
-
this.paddingChar = paddingChar || this.paddingCharKr;
|
|
31
|
-
this.dduChar.forEach((char, index) => this.dduBinaryLookup.set(char, index));
|
|
32
|
-
this.dduCharKr.forEach((char, index) => this.dduBinaryLookupKr.set(char, index));
|
|
33
|
-
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
34
|
-
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
35
|
-
}
|
|
36
|
-
getBitLength(setLength) {
|
|
37
|
-
let cached = this.bitLengthMap.get(setLength);
|
|
38
|
-
if (cached === undefined) {
|
|
39
|
-
cached = Math.ceil(Math.log2(setLength));
|
|
40
|
-
this.bitLengthMap.set(setLength, cached);
|
|
41
|
-
}
|
|
42
|
-
return cached;
|
|
43
|
-
}
|
|
44
|
-
getLargestPowerOfTwo(n) {
|
|
45
|
-
return Math.pow(2, Math.floor(Math.log2(n)));
|
|
46
|
-
}
|
|
47
|
-
*splitString(s, length) {
|
|
48
|
-
for (let i = 0; i < s.length; i += length) {
|
|
49
|
-
yield s.slice(i, Math.min(i + length, s.length));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
getSelectedSets(dduSetSymbol, usePowerOfTwo) {
|
|
53
|
-
const isKR = dduSetSymbol === "KR";
|
|
54
|
-
const dduSet = isKR ? this.dduCharKr : this.dduChar;
|
|
55
|
-
const padChar = isKR ? this.paddingCharKr : this.paddingChar;
|
|
56
|
-
let dduLength = dduSet.length;
|
|
57
|
-
if (usePowerOfTwo) {
|
|
58
|
-
const powerOfTwoLength = this.getLargestPowerOfTwo(dduLength);
|
|
59
|
-
dduLength = powerOfTwoLength;
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
dduSet: usePowerOfTwo ? dduSet.slice(0, dduLength) : dduSet,
|
|
63
|
-
padChar,
|
|
64
|
-
dduLength,
|
|
65
|
-
bitLength: this.getBitLength(dduLength),
|
|
66
|
-
lookupTable: isKR ? this.dduBinaryLookupKr : this.dduBinaryLookup,
|
|
67
|
-
paddingRegExp: this.paddingRegex.get(isKR ? "KR" : "default"),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
bufferToDduBinary(input, bitLength) {
|
|
71
|
-
const encodedBin = input.reduce((acc, byte) => acc + this.binaryLookup[byte], "");
|
|
72
|
-
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
73
|
-
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
74
|
-
if (padding > 0) {
|
|
75
|
-
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
76
|
-
}
|
|
77
|
-
return { dduBinary, padding };
|
|
78
|
-
}
|
|
79
|
-
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
80
|
-
if (paddingBits > 0) {
|
|
81
|
-
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
82
|
-
}
|
|
83
|
-
const buffer = [];
|
|
84
|
-
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
85
|
-
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
86
|
-
}
|
|
87
|
-
return Buffer.from(buffer);
|
|
88
|
-
}
|
|
89
|
-
encode(input, options = {}) {
|
|
90
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
91
|
-
const bufferInput = typeof input === "string" ? Buffer.from(input, encoding) : input;
|
|
92
|
-
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
93
|
-
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
94
|
-
let resultString = "";
|
|
95
|
-
for (const binaryChunk of dduBinary) {
|
|
96
|
-
const charInt = parseInt(binaryChunk, 2);
|
|
97
|
-
if (usePowerOfTwo) {
|
|
98
|
-
resultString += dduSet[charInt];
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
const quotient = Math.floor(charInt / dduLength);
|
|
102
|
-
const remainder = charInt % dduLength;
|
|
103
|
-
resultString += dduSet[quotient] + dduSet[remainder];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return padding > 0
|
|
107
|
-
? resultString + padChar.repeat(Math.floor(padding / 2))
|
|
108
|
-
: resultString;
|
|
109
|
-
}
|
|
110
|
-
decode(input, options = {}) {
|
|
111
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
112
|
-
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
113
|
-
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
114
|
-
const paddingBits = paddingCount * 2;
|
|
115
|
-
input = input.replace(paddingRegExp, "");
|
|
116
|
-
let dduBinary = "";
|
|
117
|
-
for (let i = 0; i < input.length; i += usePowerOfTwo ? 1 : 2) {
|
|
118
|
-
const firstChar = input[i];
|
|
119
|
-
const secondChar = input[i + 1];
|
|
120
|
-
if (usePowerOfTwo) {
|
|
121
|
-
const charIndex = lookupTable.get(firstChar);
|
|
122
|
-
if (charIndex === undefined)
|
|
123
|
-
throw new Error(`Invalid character: ${firstChar}`);
|
|
124
|
-
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
const firstIndex = lookupTable.get(firstChar);
|
|
128
|
-
const secondIndex = lookupTable.get(secondChar);
|
|
129
|
-
if (firstIndex === undefined || secondIndex === undefined)
|
|
130
|
-
throw new Error(`Invalid character: ${firstChar} or ${secondChar}`);
|
|
131
|
-
const value = firstIndex * dduLength + secondIndex;
|
|
132
|
-
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
const decoded = this.dduBinaryToBuffer(dduBinary, usePowerOfTwo ? paddingBits : paddingCount * bitLength);
|
|
136
|
-
return decoded.toString(encoding);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
exports.Ddu64 = Ddu64;
|
|
140
|
-
class Custom64 {
|
|
141
|
-
constructor(dduChar, paddingChar) {
|
|
142
|
-
this.dduCharKr = [
|
|
143
|
-
"A",
|
|
144
|
-
"s",
|
|
145
|
-
"q",
|
|
146
|
-
"r",
|
|
147
|
-
"0",
|
|
148
|
-
"z",
|
|
149
|
-
"3",
|
|
150
|
-
"t",
|
|
151
|
-
"y",
|
|
152
|
-
"1",
|
|
153
|
-
"5",
|
|
154
|
-
"2",
|
|
155
|
-
"4",
|
|
156
|
-
"E",
|
|
157
|
-
"B",
|
|
158
|
-
"C",
|
|
159
|
-
"Q",
|
|
160
|
-
"F",
|
|
161
|
-
"R",
|
|
162
|
-
"T",
|
|
163
|
-
"U",
|
|
164
|
-
"W",
|
|
165
|
-
"V",
|
|
166
|
-
"X",
|
|
167
|
-
"Y",
|
|
168
|
-
"Z",
|
|
169
|
-
"b",
|
|
170
|
-
"a",
|
|
171
|
-
"c",
|
|
172
|
-
"d",
|
|
173
|
-
"D",
|
|
174
|
-
"G",
|
|
175
|
-
"L",
|
|
176
|
-
"H",
|
|
177
|
-
"I",
|
|
178
|
-
"+",
|
|
179
|
-
"J",
|
|
180
|
-
"K",
|
|
181
|
-
"M",
|
|
182
|
-
"O",
|
|
183
|
-
"N",
|
|
184
|
-
"f",
|
|
185
|
-
"e",
|
|
186
|
-
"h",
|
|
187
|
-
"g",
|
|
188
|
-
"P",
|
|
189
|
-
"i",
|
|
190
|
-
"S",
|
|
191
|
-
"k",
|
|
192
|
-
"l",
|
|
193
|
-
"m",
|
|
194
|
-
"u",
|
|
195
|
-
"j",
|
|
196
|
-
"v",
|
|
197
|
-
"n",
|
|
198
|
-
"o",
|
|
199
|
-
"p",
|
|
200
|
-
"9",
|
|
201
|
-
"w",
|
|
202
|
-
"6",
|
|
203
|
-
"7",
|
|
204
|
-
"8",
|
|
205
|
-
"x",
|
|
206
|
-
"/",
|
|
207
|
-
];
|
|
208
|
-
this.paddingCharKr = "=";
|
|
209
|
-
this.defaultEncoding = "utf-8";
|
|
210
|
-
this.bitLengthMap = new Map();
|
|
211
|
-
this.binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
212
|
-
this.dduBinaryLookup = new Map();
|
|
213
|
-
this.dduBinaryLookupKr = new Map();
|
|
214
|
-
this.paddingRegex = new Map();
|
|
215
|
-
if (typeof dduChar === "string") {
|
|
216
|
-
const removeDuplicatesString = [...new Set(dduChar.trim())].join("");
|
|
217
|
-
this.dduChar = removeDuplicatesString.trim().split("");
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
this.dduChar = dduChar || this.dduCharKr;
|
|
221
|
-
}
|
|
222
|
-
this.paddingChar = paddingChar || this.paddingCharKr;
|
|
223
|
-
this.dduChar.forEach((char, index) => this.dduBinaryLookup.set(char, index));
|
|
224
|
-
this.dduCharKr.forEach((char, index) => this.dduBinaryLookupKr.set(char, index));
|
|
225
|
-
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
226
|
-
this.paddingRegex.set("KR", new RegExp(this.paddingCharKr, "g"));
|
|
227
|
-
}
|
|
228
|
-
getBitLength(setLength) {
|
|
229
|
-
let cached = this.bitLengthMap.get(setLength);
|
|
230
|
-
if (cached === undefined) {
|
|
231
|
-
cached = Math.ceil(Math.log2(setLength));
|
|
232
|
-
this.bitLengthMap.set(setLength, cached);
|
|
233
|
-
}
|
|
234
|
-
return cached;
|
|
235
|
-
}
|
|
236
|
-
getLargestPowerOfTwo(n) {
|
|
237
|
-
return Math.pow(2, Math.floor(Math.log2(n)));
|
|
238
|
-
}
|
|
239
|
-
*splitString(s, length) {
|
|
240
|
-
for (let i = 0; i < s.length; i += length) {
|
|
241
|
-
yield s.slice(i, Math.min(i + length, s.length));
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
getSelectedSets(dduSetSymbol, usePowerOfTwo) {
|
|
245
|
-
const isKR = dduSetSymbol === "KR";
|
|
246
|
-
const dduSet = isKR ? this.dduCharKr : this.dduChar;
|
|
247
|
-
const padChar = isKR ? this.paddingCharKr : this.paddingChar;
|
|
248
|
-
let dduLength = dduSet.length;
|
|
249
|
-
if (usePowerOfTwo) {
|
|
250
|
-
const powerOfTwoLength = this.getLargestPowerOfTwo(dduLength);
|
|
251
|
-
dduLength = powerOfTwoLength;
|
|
252
|
-
}
|
|
253
|
-
return {
|
|
254
|
-
dduSet: usePowerOfTwo ? dduSet.slice(0, dduLength) : dduSet,
|
|
255
|
-
padChar,
|
|
256
|
-
dduLength,
|
|
257
|
-
bitLength: this.getBitLength(dduLength),
|
|
258
|
-
lookupTable: isKR ? this.dduBinaryLookupKr : this.dduBinaryLookup,
|
|
259
|
-
paddingRegExp: this.paddingRegex.get(isKR ? "KR" : "default"),
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
bufferToDduBinary(input, bitLength) {
|
|
263
|
-
const encodedBin = input.reduce((acc, byte) => acc + this.binaryLookup[byte], "");
|
|
264
|
-
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
265
|
-
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
266
|
-
if (padding > 0) {
|
|
267
|
-
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
268
|
-
}
|
|
269
|
-
return { dduBinary, padding };
|
|
270
|
-
}
|
|
271
|
-
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
272
|
-
if (paddingBits > 0) {
|
|
273
|
-
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
274
|
-
}
|
|
275
|
-
const buffer = [];
|
|
276
|
-
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
277
|
-
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
278
|
-
}
|
|
279
|
-
return Buffer.from(buffer);
|
|
280
|
-
}
|
|
281
|
-
encode(input, options = {}) {
|
|
282
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
283
|
-
const bufferInput = typeof input === "string" ? Buffer.from(input, encoding) : input;
|
|
284
|
-
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
285
|
-
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
286
|
-
let resultString = "";
|
|
287
|
-
for (const binaryChunk of dduBinary) {
|
|
288
|
-
const charInt = parseInt(binaryChunk, 2);
|
|
289
|
-
if (usePowerOfTwo) {
|
|
290
|
-
resultString += dduSet[charInt];
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
const quotient = Math.floor(charInt / dduLength);
|
|
294
|
-
const remainder = charInt % dduLength;
|
|
295
|
-
resultString += dduSet[quotient] + dduSet[remainder];
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return padding > 0
|
|
299
|
-
? resultString + padChar.repeat(Math.floor(padding / 2))
|
|
300
|
-
: resultString;
|
|
301
|
-
}
|
|
302
|
-
decode(input, options = {}) {
|
|
303
|
-
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
304
|
-
const { dduSet, dduLength, bitLength, lookupTable, paddingRegExp } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
305
|
-
const paddingCount = (input.match(paddingRegExp) || []).length;
|
|
306
|
-
const paddingBits = paddingCount * 2;
|
|
307
|
-
input = input.replace(paddingRegExp, "");
|
|
308
|
-
let dduBinary = "";
|
|
309
|
-
for (let i = 0; i < input.length; i += usePowerOfTwo ? 1 : 2) {
|
|
310
|
-
const firstChar = input[i];
|
|
311
|
-
const secondChar = input[i + 1];
|
|
312
|
-
if (usePowerOfTwo) {
|
|
313
|
-
const charIndex = lookupTable.get(firstChar);
|
|
314
|
-
if (charIndex === undefined)
|
|
315
|
-
throw new Error(`Invalid character: ${firstChar}`);
|
|
316
|
-
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
317
|
-
}
|
|
318
|
-
else {
|
|
319
|
-
const firstIndex = lookupTable.get(firstChar);
|
|
320
|
-
const secondIndex = lookupTable.get(secondChar);
|
|
321
|
-
if (firstIndex === undefined || secondIndex === undefined)
|
|
322
|
-
throw new Error(`Invalid character: ${firstChar} or ${secondChar}`);
|
|
323
|
-
const value = firstIndex * dduLength + secondIndex;
|
|
324
|
-
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
const decoded = this.dduBinaryToBuffer(dduBinary, usePowerOfTwo ? paddingBits : paddingCount * bitLength);
|
|
328
|
-
return decoded.toString(encoding);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
exports.Custom64 = Custom64;
|
|
3
|
+
exports.Ddu1024 = exports.Ddu512 = exports.Ddu128 = exports.Custom64 = exports.Ddu64 = void 0;
|
|
4
|
+
var Ddu64_js_1 = require("./Ddu64.js");
|
|
5
|
+
Object.defineProperty(exports, "Ddu64", { enumerable: true, get: function () { return Ddu64_js_1.Ddu64; } });
|
|
6
|
+
var Custom64_js_1 = require("./Custom64.js");
|
|
7
|
+
Object.defineProperty(exports, "Custom64", { enumerable: true, get: function () { return Custom64_js_1.Custom64; } });
|
|
8
|
+
var Ddu128_js_1 = require("./Ddu128.js");
|
|
9
|
+
Object.defineProperty(exports, "Ddu128", { enumerable: true, get: function () { return Ddu128_js_1.Ddu128; } });
|
|
10
|
+
var Ddu512_js_1 = require("./Ddu512.js");
|
|
11
|
+
Object.defineProperty(exports, "Ddu512", { enumerable: true, get: function () { return Ddu512_js_1.Ddu512; } });
|
|
12
|
+
var Ddu1024_js_1 = require("./Ddu1024.js");
|
|
13
|
+
Object.defineProperty(exports, "Ddu1024", { enumerable: true, get: function () { return Ddu1024_js_1.Ddu1024; } });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare class Custom64 {
|
|
2
|
+
private readonly dduChar;
|
|
3
|
+
private readonly paddingChar;
|
|
4
|
+
private readonly dduCharDefault;
|
|
5
|
+
private readonly paddingCharDefault;
|
|
6
|
+
private readonly defaultEncoding;
|
|
7
|
+
private readonly bitLengthMap;
|
|
8
|
+
private readonly binaryLookup;
|
|
9
|
+
private readonly dduBinaryLookup;
|
|
10
|
+
private readonly dduBinaryLookupKr;
|
|
11
|
+
private readonly paddingRegex;
|
|
12
|
+
constructor(dduChar?: string[] | string, paddingChar?: string);
|
|
13
|
+
private getBitLength;
|
|
14
|
+
private getLargestPowerOfTwo;
|
|
15
|
+
private splitString;
|
|
16
|
+
private getSelectedSets;
|
|
17
|
+
private bufferToDduBinary;
|
|
18
|
+
private dduBinaryToBuffer;
|
|
19
|
+
encode(input: Buffer | string, options?: {
|
|
20
|
+
dduSetSymbol?: string;
|
|
21
|
+
encoding?: BufferEncoding;
|
|
22
|
+
usePowerOfTwo?: boolean;
|
|
23
|
+
}): string;
|
|
24
|
+
decode(input: string, options?: {
|
|
25
|
+
dduSetSymbol?: string;
|
|
26
|
+
encoding?: BufferEncoding;
|
|
27
|
+
usePowerOfTwo?: boolean;
|
|
28
|
+
}): string;
|
|
29
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
export class Custom64 {
|
|
2
|
+
dduChar;
|
|
3
|
+
paddingChar;
|
|
4
|
+
dduCharDefault = [
|
|
5
|
+
"A",
|
|
6
|
+
"s",
|
|
7
|
+
"q",
|
|
8
|
+
"r",
|
|
9
|
+
"0",
|
|
10
|
+
"z",
|
|
11
|
+
"3",
|
|
12
|
+
"t",
|
|
13
|
+
"y",
|
|
14
|
+
"1",
|
|
15
|
+
"5",
|
|
16
|
+
"2",
|
|
17
|
+
"4",
|
|
18
|
+
"E",
|
|
19
|
+
"B",
|
|
20
|
+
"C",
|
|
21
|
+
"Q",
|
|
22
|
+
"F",
|
|
23
|
+
"R",
|
|
24
|
+
"T",
|
|
25
|
+
"U",
|
|
26
|
+
"W",
|
|
27
|
+
"V",
|
|
28
|
+
"X",
|
|
29
|
+
"Y",
|
|
30
|
+
"Z",
|
|
31
|
+
"b",
|
|
32
|
+
"a",
|
|
33
|
+
"c",
|
|
34
|
+
"d",
|
|
35
|
+
"D",
|
|
36
|
+
"G",
|
|
37
|
+
"L",
|
|
38
|
+
"H",
|
|
39
|
+
"I",
|
|
40
|
+
"-",
|
|
41
|
+
"J",
|
|
42
|
+
"K",
|
|
43
|
+
"M",
|
|
44
|
+
"O",
|
|
45
|
+
"N",
|
|
46
|
+
"f",
|
|
47
|
+
"e",
|
|
48
|
+
"h",
|
|
49
|
+
"g",
|
|
50
|
+
"P",
|
|
51
|
+
"i",
|
|
52
|
+
"S",
|
|
53
|
+
"k",
|
|
54
|
+
"l",
|
|
55
|
+
"m",
|
|
56
|
+
"u",
|
|
57
|
+
"j",
|
|
58
|
+
"v",
|
|
59
|
+
"n",
|
|
60
|
+
"o",
|
|
61
|
+
"p",
|
|
62
|
+
"9",
|
|
63
|
+
"w",
|
|
64
|
+
"6",
|
|
65
|
+
"7",
|
|
66
|
+
"8",
|
|
67
|
+
"x",
|
|
68
|
+
"_",
|
|
69
|
+
];
|
|
70
|
+
paddingCharDefault = "=";
|
|
71
|
+
defaultEncoding = "utf-8";
|
|
72
|
+
bitLengthMap = new Map();
|
|
73
|
+
binaryLookup = Array.from({ length: 256 }, (_, i) => i.toString(2).padStart(8, "0"));
|
|
74
|
+
dduBinaryLookup = new Map();
|
|
75
|
+
dduBinaryLookupKr = new Map();
|
|
76
|
+
paddingRegex = new Map();
|
|
77
|
+
constructor(dduChar, paddingChar) {
|
|
78
|
+
if (typeof dduChar === "string") {
|
|
79
|
+
const removeDuplicatesString = [...new Set(dduChar.trim())].join("");
|
|
80
|
+
this.dduChar = removeDuplicatesString.trim().split("");
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.dduChar = dduChar || this.dduCharDefault;
|
|
84
|
+
}
|
|
85
|
+
this.paddingChar = paddingChar || this.paddingCharDefault;
|
|
86
|
+
this.dduChar.forEach((char, index) => this.dduBinaryLookup.set(char, index));
|
|
87
|
+
this.dduCharDefault.forEach((char, index) => this.dduBinaryLookupKr.set(char, index));
|
|
88
|
+
this.paddingRegex.set("default", new RegExp(this.paddingChar, "g"));
|
|
89
|
+
this.paddingRegex.set("KR", new RegExp(this.paddingCharDefault, "g"));
|
|
90
|
+
}
|
|
91
|
+
getBitLength(setLength) {
|
|
92
|
+
let cached = this.bitLengthMap.get(setLength);
|
|
93
|
+
if (cached === undefined) {
|
|
94
|
+
cached = Math.ceil(Math.log2(setLength));
|
|
95
|
+
this.bitLengthMap.set(setLength, cached);
|
|
96
|
+
}
|
|
97
|
+
return cached;
|
|
98
|
+
}
|
|
99
|
+
getLargestPowerOfTwo(n) {
|
|
100
|
+
return 2 ** Math.floor(Math.log2(n));
|
|
101
|
+
}
|
|
102
|
+
*splitString(s, length) {
|
|
103
|
+
for (let i = 0; i < s.length; i += length) {
|
|
104
|
+
yield s.slice(i, Math.min(i + length, s.length));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
getSelectedSets(dduSetSymbol, usePowerOfTwo) {
|
|
108
|
+
const isKR = dduSetSymbol === "KR";
|
|
109
|
+
const dduSet = isKR ? this.dduCharDefault : this.dduChar;
|
|
110
|
+
const padChar = isKR ? this.paddingCharDefault : this.paddingChar;
|
|
111
|
+
let dduLength = dduSet.length;
|
|
112
|
+
if (usePowerOfTwo) {
|
|
113
|
+
const powerOfTwoLength = this.getLargestPowerOfTwo(dduLength);
|
|
114
|
+
dduLength = powerOfTwoLength;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
dduSet: usePowerOfTwo ? dduSet.slice(0, dduLength) : dduSet,
|
|
118
|
+
padChar,
|
|
119
|
+
dduLength,
|
|
120
|
+
bitLength: this.getBitLength(dduLength),
|
|
121
|
+
lookupTable: isKR ? this.dduBinaryLookupKr : this.dduBinaryLookup,
|
|
122
|
+
paddingRegExp: this.paddingRegex.get(isKR ? "KR" : "default"),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
bufferToDduBinary(input, bitLength) {
|
|
126
|
+
const encodedBin = input.reduce((acc, byte) => acc + this.binaryLookup[byte], "");
|
|
127
|
+
if (encodedBin.length === 0) {
|
|
128
|
+
return { dduBinary: [], padding: 0 };
|
|
129
|
+
}
|
|
130
|
+
const dduBinary = Array.from(this.splitString(encodedBin, bitLength));
|
|
131
|
+
const padding = bitLength - dduBinary[dduBinary.length - 1].length;
|
|
132
|
+
if (padding > 0) {
|
|
133
|
+
dduBinary[dduBinary.length - 1] += "0".repeat(padding);
|
|
134
|
+
}
|
|
135
|
+
return { dduBinary, padding };
|
|
136
|
+
}
|
|
137
|
+
dduBinaryToBuffer(decodedBin, paddingBits) {
|
|
138
|
+
if (paddingBits > 0) {
|
|
139
|
+
decodedBin = decodedBin.slice(0, -paddingBits);
|
|
140
|
+
}
|
|
141
|
+
const buffer = [];
|
|
142
|
+
for (let i = 0; i < decodedBin.length; i += 8) {
|
|
143
|
+
buffer.push(parseInt(decodedBin.slice(i, i + 8), 2));
|
|
144
|
+
}
|
|
145
|
+
return Buffer.from(buffer);
|
|
146
|
+
}
|
|
147
|
+
encode(input, options = {}) {
|
|
148
|
+
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
149
|
+
const bufferInput = typeof input === "string" ? Buffer.from(input, encoding) : input;
|
|
150
|
+
const { dduSet, padChar, dduLength, bitLength } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
151
|
+
const { dduBinary, padding } = this.bufferToDduBinary(bufferInput, bitLength);
|
|
152
|
+
let resultString = "";
|
|
153
|
+
for (const binaryChunk of dduBinary) {
|
|
154
|
+
const charInt = parseInt(binaryChunk, 2);
|
|
155
|
+
if (usePowerOfTwo) {
|
|
156
|
+
resultString += dduSet[charInt];
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const quotient = Math.floor(charInt / dduLength);
|
|
160
|
+
const remainder = charInt % dduLength;
|
|
161
|
+
resultString += dduSet[quotient] + dduSet[remainder];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// 패딩 비트 수를 padChar + 숫자로 명시 (모든 모드)
|
|
165
|
+
if (padding > 0) {
|
|
166
|
+
resultString += padChar + padding;
|
|
167
|
+
}
|
|
168
|
+
return resultString;
|
|
169
|
+
}
|
|
170
|
+
decode(input, options = {}) {
|
|
171
|
+
const { dduSetSymbol = "default", encoding = this.defaultEncoding, usePowerOfTwo = true, } = options;
|
|
172
|
+
const { dduSet, dduLength, bitLength, lookupTable, padChar, paddingRegExp } = this.getSelectedSets(dduSetSymbol, usePowerOfTwo);
|
|
173
|
+
// 패딩 정보 추출: padChar 이후의 숫자가 패딩 비트 수 (모든 모드)
|
|
174
|
+
let paddingBits = 0;
|
|
175
|
+
const padCharIndex = input.indexOf(padChar);
|
|
176
|
+
if (padCharIndex >= 0) {
|
|
177
|
+
const paddingStr = input.substring(padCharIndex + padChar.length);
|
|
178
|
+
paddingBits = parseInt(paddingStr) || 0;
|
|
179
|
+
input = input.substring(0, padCharIndex);
|
|
180
|
+
}
|
|
181
|
+
let dduBinary = "";
|
|
182
|
+
for (let i = 0; i < input.length; i += usePowerOfTwo ? 1 : 2) {
|
|
183
|
+
const firstChar = input[i];
|
|
184
|
+
const secondChar = input[i + 1];
|
|
185
|
+
if (usePowerOfTwo) {
|
|
186
|
+
const charIndex = lookupTable.get(firstChar);
|
|
187
|
+
if (charIndex === undefined)
|
|
188
|
+
throw new Error(`Invalid character: ${firstChar}`);
|
|
189
|
+
dduBinary += charIndex.toString(2).padStart(bitLength, "0");
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
const firstIndex = lookupTable.get(firstChar);
|
|
193
|
+
const secondIndex = lookupTable.get(secondChar);
|
|
194
|
+
if (firstIndex === undefined || secondIndex === undefined)
|
|
195
|
+
throw new Error(`Invalid character: ${firstChar} or ${secondChar}`);
|
|
196
|
+
const value = firstIndex * dduLength + secondIndex;
|
|
197
|
+
dduBinary += value.toString(2).padStart(bitLength, "0");
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const decoded = this.dduBinaryToBuffer(dduBinary, paddingBits);
|
|
201
|
+
return decoded.toString(encoding);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class Ddu1024 {
|
|
2
|
+
private readonly dduChar;
|
|
3
|
+
private readonly paddingChar;
|
|
4
|
+
private readonly charLength;
|
|
5
|
+
private readonly dduCharDefault;
|
|
6
|
+
private readonly paddingCharDefault;
|
|
7
|
+
private readonly defaultEncoding;
|
|
8
|
+
private readonly binaryLookup;
|
|
9
|
+
private readonly dduBinaryLookup;
|
|
10
|
+
private readonly dduBinaryLookupKr;
|
|
11
|
+
private readonly paddingRegex;
|
|
12
|
+
constructor(dduChar?: string[], paddingChar?: string);
|
|
13
|
+
private escapeRegExp;
|
|
14
|
+
private splitString;
|
|
15
|
+
private getSelectedSets;
|
|
16
|
+
private bufferToDduBinary;
|
|
17
|
+
private dduBinaryToBuffer;
|
|
18
|
+
encode(input: Buffer | string, options?: {
|
|
19
|
+
dduSetSymbol?: string;
|
|
20
|
+
encoding?: BufferEncoding;
|
|
21
|
+
}): string;
|
|
22
|
+
decode(input: string, options?: {
|
|
23
|
+
dduSetSymbol?: string;
|
|
24
|
+
encoding?: BufferEncoding;
|
|
25
|
+
}): string;
|
|
26
|
+
}
|