Skip to content

Commit 5d76861

Browse files
committed
Progress commit
1 parent 2c5d373 commit 5d76861

File tree

2 files changed

+128
-168
lines changed

2 files changed

+128
-168
lines changed

demo_unishox2.js

Lines changed: 38 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -64,84 +64,12 @@ const USX_PSETS = [
6464
const usx = require('./unishox2');
6565
var fs = require("fs");
6666

67-
function unishox2_compress_preset_lines(input, len, out, pset, prev_lines) {
68-
switch (pset) {
69-
case 0:
70-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
71-
case 1:
72-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
73-
case 2:
74-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
75-
case 3:
76-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
77-
case 4:
78-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
79-
case 5:
80-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
81-
case 6:
82-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
83-
case 7:
84-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
85-
case 8:
86-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
87-
case 9:
88-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
89-
case 10:
90-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
91-
case 11:
92-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
93-
case 12:
94-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
95-
case 13:
96-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
97-
case 14:
98-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
99-
case 15:
100-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
101-
case 16:
102-
return usx.unishox2_compress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
103-
}
104-
return 0;
67+
function unishox2_compress_preset(input, len, out, pset, prev_lines_arr) {
68+
return usx.unishox2_compress_array(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines_arr);
10569
}
10670

107-
function unishox2_decompress_preset_lines(input, len, out, pset, prev_lines) {
108-
switch (preset) {
109-
case 0:
110-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
111-
case 1:
112-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
113-
case 2:
114-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
115-
case 3:
116-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
117-
case 4:
118-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
119-
case 5:
120-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
121-
case 6:
122-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
123-
case 7:
124-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
125-
case 8:
126-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
127-
case 9:
128-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
129-
case 10:
130-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
131-
case 11:
132-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
133-
case 12:
134-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
135-
case 13:
136-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
137-
case 14:
138-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
139-
case 15:
140-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
141-
case 16:
142-
return usx.unishox2_decompress_lines(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES, prev_lines);
143-
}
144-
return 0;
71+
function unishox2_decompress_preset(input, len, out, pset) {
72+
return usx.unishox2_decompress(input, len, out, USX_PSETS[pset][0], USX_PSETS[pset][1], USX_PSETS[pset][2], USX_TEMPLATES);
14573
}
14674

14775
var args = process.argv.slice(1);
@@ -176,7 +104,7 @@ if (argv >= 4 && args[1] == "-c") {
176104
do {
177105
bytes_read = fs.readSync(fp, cbuf, 0, cbuf.length, null);
178106
if (bytes_read > 0) {
179-
clen = unishox2_compress_preset_lines(cbuf, bytes_read, dbuf, preset, null);
107+
clen = unishox2_compress_preset(cbuf, bytes_read, dbuf, preset);
180108
ctot += clen;
181109
tot_len += bytes_read;
182110
if (clen > 0) {
@@ -219,7 +147,7 @@ if (argv >= 4 && args[1] == "-d") {
219147
len_to_read += dbuf[1];
220148
bytes_read = fs.readSync(fp, dbuf, 0, len_to_read, null);
221149
if (bytes_read > 0) {
222-
dlen = unishox2_decompress_preset_lines(dbuf, bytes_read, cbuf, preset, null);
150+
dlen = unishox2_decompress_preset(dbuf, bytes_read, cbuf, preset);
223151
if (dlen > 0) {
224152
if (dlen != fs.writeSync(wfp, cbuf, 0, dlen, null)) {
225153
console.log("error writing file");
@@ -241,59 +169,64 @@ if (argv >= 4 && (args[1] == "-g" || args[1] == "-G")) {
241169
console.log(e);
242170
return;
243171
}
244-
sprintf(cbuf, "%s.h", args[3]);
245172
try {
246-
wfp = fs.openSync(args[3], "w+");
173+
wfp = fs.openSync(args[3] + ".js", "w+");
247174
} catch (e) {
248175
console.log(e);
249176
return;
250177
}
251178
tot_len = 0;
252179
ctot = 0;
253-
var cur_line = null;
254-
fs.writeSync(wfp, "// _UNISHOX2_COMPRESSED__", 0);
255-
fs.writeSync(wfp, args[3], 0);
256-
fs.writeSync(wfp, "__\n", 0);
180+
var prev_lines = [];
181+
var prev_lines_compressed = [];
182+
fs.writeSync(wfp, "// _UNISHOX2_COMPRESSED__");
183+
fs.writeSync(wfp, args[3]);
184+
fs.writeSync(wfp, "__\n");
257185
var line_ctr = 0;
258186
var max_len = 0;
259187
var cur_pos = 0;
260188
var max_line_length = 1024;
189+
fs.writeSync(wfp, "module.exports = [\n");
261190
while (fs.readSync(fp, cbuf, 0, max_line_length, cur_pos) != 0) {
262191
len = 0;
263-
while (cbuf[len] != '\r' && cbuf[len != '\n'])
192+
while (cbuf[len] !== 13 && cbuf[len] !== 10)
264193
len++;
265-
cur_pos = len + 1;
194+
cur_pos += len;
195+
cur_pos++;
266196
// compress the line and look in previous lines
267197
// add to linked list
268198
if (len > 0) {
269-
cur_line[line_ctr] = cbuf.slice(0, len);
270-
clen = unishox2_compress_preset_lines(cbuf, bytes_read, dbuf, preset, cur_line);
199+
prev_lines[line_ctr] = cbuf.slice(0, len);
200+
clen = unishox2_compress_preset(cbuf, len, dbuf, preset, prev_lines);
271201
if (clen > 0) {
272202
perc = (len-clen);
273203
perc /= len;
274204
perc *= 100;
275205
//print_compressed(dbuf, clen);
276206
console.log("len: %ld/%ld=", clen, len);
277-
console.log("%.2f %s", perc, cbuf);
207+
console.log("%.2f", perc);
278208
tot_len += len;
279209
ctot += clen;
280-
fs.writeSync(wfp, "var " + args[3] + "_" + line_ctr + " = new Uint8Array([", 0);
210+
if (line_ctr > 0)
211+
fs.writeSync(wfp, ", \n");
212+
fs.writeSync(wfp, "new Uint8Array([");
281213
for (var i = 0; i < clen; i++)
282-
fs.writeSync(wfp, (i == 0 ? "" : ", ") + dbuf[i], 0);
283-
fs.writeSync(wfp, "]);", 0);
214+
fs.writeSync(wfp, (i == 0 ? "" : ", ") + dbuf[i]);
215+
fs.writeSync(wfp, "])");
216+
if (len > max_len)
217+
max_len = len;
218+
prev_lines_compressed[line_ctr] = dbuf.slice(0, clen);
219+
console.log(unishox2_decompress_preset(prev_lines_compressed, line_ctr, null, preset));
284220
}
285-
if (len > max_len)
286-
max_len = len;
287-
dlen = unishox2_decompress_preset_lines(dbuf, clen, cbuf, preset, cur_line);
288-
cbuf[dlen] = 0;
289-
console.log("\n%s", cbuf);
221+
line_ctr++;
290222
}
291-
}
292-
perc = (tot_len-ctot);
293-
perc /= tot_len;
294-
perc *= 100;
295-
printf("\nBytes (Compressed/Original=Savings%%): %ld/%ld=", ctot, tot_len);
296-
console.log("%.2f%%", perc);
223+
}
224+
fs.writeSync(wfp, "];\n");
225+
perc = (tot_len-ctot);
226+
perc /= tot_len;
227+
perc *= 100;
228+
console.log("\nBytes (Compressed/Original=Savings%%): %ld/%ld=", ctot, tot_len);
229+
console.log("%.2f%%", perc);
297230
} else
298231
if (argv == 2 || (argv == 3 && parseInt(args[2], 10) > 0)) {
299232
var pset = 0;
@@ -322,8 +255,8 @@ if (argv == 2 || (argv == 3 && parseInt(args[2], 10) > 0)) {
322255
console.log(" -t run tests");
323256
console.log(" -c compress");
324257
console.log(" -d decompress");
325-
console.log(" -g generate C header file");
326-
console.log(" -G generate C header file using additional compression (slower)");
258+
console.log(" -g generate Uint8Array file");
259+
console.log(" -G generate Uint8Array file using additional compression (slower)");
327260
console.log("");
328261
console.log(" [preset_number]:");
329262
console.log(" 0 Optimum - favors all including JSON, XML, URL and HTML (default)");

0 commit comments

Comments
 (0)