encoding-tools 0.0.2 → 0.0.6

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/encoding-tools ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env node
2
+
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ /** ------------------------------------------------------------------
6
+ * Copyright ©
7
+ * Pellegrino Prevete
8
+ * 2024, 2025, 2026
9
+ *
10
+ * All rights reserved
11
+ * ------------------------------------------------------------------
12
+ *
13
+ * This program is free software: you can redistribute it and/or
14
+ * modify it under the terms of the GNU General Public License as
15
+ * published by the Free Software Foundation, either version 3 of
16
+ * the License, or (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program.
25
+ * If not, see <https://www.gnu.org/licenses/>.
26
+ */
27
+
28
+ const
29
+ _bin2txt_module =
30
+ require(
31
+ "./libbin2txt");
32
+ const
33
+ _bin2txt =
34
+ _bin2txt_module._bin2txt;
35
+ const
36
+ _txt2bin_module =
37
+ require(
38
+ "./libtxt2bin");
39
+ const
40
+ _txt2bin =
41
+ _txt2bin_module._txt2bin;
42
+
43
+ module.exports = {
44
+ _bin2txt:
45
+ _bin2txt,
46
+ _txt2bin:
47
+ _txt2bin
48
+ };
package/libbin2txt CHANGED
@@ -27,46 +27,107 @@
27
27
  * If not, see <https://www.gnu.org/licenses/>.
28
28
  */
29
29
 
30
+ /* eslint-disable
31
+ no-unused-vars */
32
+
30
33
  const
31
34
  _libcrash =
32
35
  require(
33
- 'crash-js');
36
+ "crash-js");
37
+ /* global
38
+ _cat */
39
+ const
40
+ _cat =
41
+ _libcrash._cat;
42
+ /* global
43
+ _dirname */
44
+ const
45
+ _dirname =
46
+ _libcrash._dirname;
47
+ /* global
48
+ _error_display */
34
49
  const
35
50
  _error_display =
36
51
  _libcrash._error_display;
52
+ /* global
53
+ _ext_rm */
37
54
  const
38
55
  _ext_rm =
39
56
  _libcrash._ext_rm;
57
+ /* global
58
+ _file_exists */
59
+ const
60
+ _file_exists =
61
+ _libcrash._file_exists;
62
+ /* global
63
+ _file_read */
40
64
  const
41
65
  _file_read =
42
66
  _libcrash._file_read;
67
+ /* global
68
+ _file_write */
43
69
  const
44
70
  _file_write =
45
71
  _libcrash._file_write;
72
+ /* global
73
+ _fs_worker_start */
46
74
  const
47
75
  _fs_worker_start =
48
76
  _libcrash._fs_worker_start;
77
+ /* global
78
+ _json_read */
49
79
  const
50
80
  _json_read =
51
81
  _libcrash._json_read;
82
+ /* global
83
+ _ls */
52
84
  const
53
85
  _ls =
54
86
  _libcrash._ls;
87
+ /* global
88
+ _mkdir */
55
89
  const
56
90
  _mkdir =
57
91
  _libcrash._mkdir;
92
+ /* global
93
+ _msg_info */
58
94
  const
59
95
  _msg_info =
60
96
  _libcrash._msg_info;
97
+ /* global
98
+ _msg_error */
61
99
  const
62
100
  _msg_error =
63
101
  _libcrash._msg_error;
102
+ /* global
103
+ _printf */
64
104
  const
65
- _split =
66
- _libcrash._split;
105
+ _printf =
106
+ _libcrash._printf;
107
+ /* global
108
+ _rm */
109
+ const
110
+ _rm =
111
+ _libcrash._rm;
112
+ /* global
113
+ _seq */
114
+ const
115
+ _seq =
116
+ _libcrash._seq;
117
+ /* global
118
+ _stat */
67
119
  const
68
120
  _stat =
69
121
  _libcrash._stat;
122
+ const
123
+ _tmcsplit =
124
+ require(
125
+ "tmcsplit");
126
+ /* global
127
+ _split */
128
+ const
129
+ _split =
130
+ _tmcsplit._split;
70
131
 
71
132
  async function
72
133
  _buffer_prepare(
@@ -74,39 +135,233 @@ async function
74
135
  _out,
75
136
  _buffer_size_bytes) {
76
137
  let
77
- _in_size_bytes,
78
- _split_opts,
79
138
  _msg;
80
- _split_opts =
81
- [];
82
- console.log(
83
- _stat);
84
- console.log(
85
- _in);
86
- console.log(
87
- _out);
88
- // _in_size_bytes="$( \
89
- // du \
90
- // -bs \
91
- // "${_in}" | \
92
- // awk \
93
- // '{print $1}')"
94
- // _msg=(
95
- // "File '${_in}' has size"
96
- // "'${_in_size_bytes}' bytes."
97
- // )
98
- // _msg_info \
99
- // "${_msg[*]}"
100
- // _blocks_total="$(( \
101
- // _in_size_bytes / _buffer_size_bytes + 1))"
102
- // split \
103
- // -n \
104
- // "l/${_blocks_total}" \
105
- // --numeric-suffixes="1" \
106
- // "${_in}" \
107
- // "${_out}.block."
108
- // _blocks_find \
109
- // "${_out}.block"
139
+ const
140
+ _size_max =
141
+ 0;
142
+ const
143
+ _in_size_bytes =
144
+ _stat(
145
+ _in).size;
146
+ _msg =
147
+ `File '${_in}' has size ` +
148
+ `'${_in_size_bytes}' bytes.`;
149
+ _msg_info(
150
+ _msg);
151
+ const
152
+ _blocks_total =
153
+ Math.ceil(
154
+ _in_size_bytes /
155
+ _buffer_size_bytes);
156
+ _msg =
157
+ `Splitting file '${_in}' in ` +
158
+ `'${_blocks_total}' blocks of ` +
159
+ `'${_buffer_size_bytes}' bytes.`;
160
+ _msg_info(
161
+ _msg);
162
+ const
163
+ _split_opts = [
164
+ _in,
165
+ `${_out}.block.`,
166
+ _blocks_total,
167
+ _size_max
168
+ ];
169
+ const
170
+ _output_files =
171
+ await _split.apply(
172
+ null,
173
+ _split_opts);
174
+ _msg =
175
+ "Done.";
176
+ _msg_info(
177
+ _msg);
178
+ return _output_files;
179
+ }
180
+
181
+ function
182
+ _base64_block_encode(
183
+ _file) {
184
+ let
185
+ _string;
186
+ const
187
+ _content =
188
+ _cat(
189
+ _file);
190
+ if ( typeof window === 'undefined' &&
191
+ ( typeof global !== 'undefined' &&
192
+ global.global === global ) &&
193
+ typeof __webpack_require__ !== 'function' ) {
194
+ const
195
+ _buffer =
196
+ Buffer.from(
197
+ _content);
198
+ _string =
199
+ _buffer.toString(
200
+ "base64");
201
+ }
202
+ else {
203
+ _string =
204
+ btoa(
205
+ _string);
206
+ }
207
+ return _string;
208
+ }
209
+
210
+ async function
211
+ _chunk_split(
212
+ _in,
213
+ _chunk_current,
214
+ _length,
215
+ _prefix,
216
+ _block_last) {
217
+ let
218
+ _chunk,
219
+ _chunk_start,
220
+ _content,
221
+ _cut,
222
+ _msg,
223
+ _out;
224
+ const
225
+ _total =
226
+ _in.length;
227
+ const
228
+ _chunks =
229
+ Math.ceil(
230
+ _chunk_current +
231
+ _total /
232
+ _length);
233
+ if ( _chunks < 2 ) {
234
+ _out =
235
+ _prefix;
236
+ _file_write(
237
+ _out,
238
+ _in);
239
+ return 0;
240
+ }
241
+ _msg =
242
+ `Chunking '${_total}' ` +
243
+ `letters into '${_chunks}' ` +
244
+ `chunks of length '${_length}'.`;
245
+ _msg_info(
246
+ _msg);
247
+ for ( _chunk of _seq(
248
+ _chunk_current + 1,
249
+ _chunks) ) {
250
+ _chunk_start =
251
+ ( ( _chunk -
252
+ _chunk_current ) *
253
+ _length ) -
254
+ _length;
255
+ _cut =
256
+ _in.substring(
257
+ _chunk_start,
258
+ _chunk_start + _length);
259
+ _out =
260
+ `${_prefix}.${_chunk}`;
261
+ _msg =
262
+ `Writing chunk ${_chunk} in file '${_out}' ` +
263
+ `of length ${_cut.length} out of ${_chunks}.`;
264
+ _msg_info(
265
+ _msg);
266
+ if ( _chunk < _chunks ) {
267
+ _printf(
268
+ _out,
269
+ _cut);
270
+ }
271
+ else if ( _chunk == _chunks ) {
272
+ if ( _block_last == "true" ) {
273
+ _file_write(
274
+ _out,
275
+ _cut);
276
+ }
277
+ else if ( _block_last == "false" ) {
278
+ _msg =
279
+ "Last chunk of non-last block, " +
280
+ "adding a newline character at the end (nope).";
281
+ _msg_info(
282
+ _msg);
283
+ _file_write(
284
+ _out,
285
+ _cut);
286
+ }
287
+ }
288
+ }
289
+ _chunk_current =
290
+ _chunks;
291
+ _msg=
292
+ "Finished encoding block.";
293
+ _msg_info(
294
+ _msg);
295
+ return _chunk_current;
296
+ }
297
+
298
+ async function
299
+ _base64_encode(
300
+ _block,
301
+ _chunk_current,
302
+ _length,
303
+ _out,
304
+ _block_last,
305
+ _amount_only) {
306
+ let
307
+ _msg;
308
+ const
309
+ _txt =
310
+ _base64_block_encode(
311
+ _block);
312
+ _msg =
313
+ `Block '${_block}' encoded ` +
314
+ `length: ${_txt.length}.`;
315
+ _msg_info(
316
+ _msg);
317
+ if ( _amount_only === "false" ) {
318
+ _chunk_current =
319
+ await _chunk_split(
320
+ _txt,
321
+ _chunk_current,
322
+ _length,
323
+ _out,
324
+ _block_last);
325
+ }
326
+ else if ( _amount_only === "true" ) {
327
+ const
328
+ _chunks_total =
329
+ Math.ceil(
330
+ _chunks_total +
331
+ _txt.length /
332
+ _length);
333
+ _chunk_current =
334
+ _chunks_total;
335
+ }
336
+ else {
337
+ _msg =
338
+ `Value of the 'amount_only' can ` +
339
+ `be true or false. Current: '${_amount_only}'.`;
340
+ _msg_error(
341
+ _msg,
342
+ 1);
343
+ }
344
+ _msg =
345
+ "Finished encoding block " +
346
+ `'${_block}'.`;
347
+ _msg_info(
348
+ _msg);
349
+ return _chunk_current;
350
+ }
351
+
352
+ async function
353
+ _input_data_check(
354
+ _in) {
355
+ if ( ! _file_exists(
356
+ _in) ) {
357
+ const
358
+ _msg =
359
+ `File '${_in}' does ` +
360
+ 'not exist.';
361
+ _msg_error(
362
+ _msg,
363
+ 1);
364
+ }
110
365
  }
111
366
 
112
367
  async function
@@ -118,71 +373,102 @@ async function
118
373
  _buffer_size_bytes,
119
374
  _amount_only) {
120
375
  let
121
- _blocks_total,
122
- _chunks_total,
376
+ _block,
123
377
  _chunk_current,
378
+ _chunks_total,
124
379
  _block_current,
125
380
  _block_last,
126
- _dir,
127
- _file,
128
381
  _msg,
382
+ _rm_args,
129
383
  _worker;
130
- _dir =
131
- "/test_dir";
132
- _file =
133
- `${_dir}/test_file`;
134
- _content =
135
- `${_in} ${_out}`;
136
384
  if ( typeof window !== 'undefined' ) {
137
- await _fs_worker_start(
138
- "fs-worker.js");
385
+ const
386
+ _worker =
387
+ await _fs_worker_start(
388
+ "fs-worker.js");
389
+ _msg =
390
+ `OPFS worker '${_worker}' started.`;
391
+ _msg_info(
392
+ _msg);
393
+ }
394
+ _input_data_check(
395
+ _in);
396
+ const
397
+ _blocks =
398
+ await _buffer_prepare(
399
+ _in,
400
+ _out,
401
+ _buffer_size_bytes);
402
+ const
403
+ _blocks_total =
404
+ _blocks.length;
405
+ _chunk_current =
406
+ 0;
407
+ _block_current =
408
+ 1;
409
+ for ( _block of _blocks ) {
410
+ _msg =
411
+ `Reading block '${_block}' ` +
412
+ `('${_block_current}' ` +
413
+ `of '${_blocks_total}').`;
414
+ _msg_info(
415
+ _msg);
416
+ if ( _block_current < _blocks_total ) {
417
+ _block_last =
418
+ "false";
419
+ }
420
+ else if ( _block_current == _blocks_total ) {
421
+ _block_last =
422
+ "true";
423
+ }
424
+ if ( _format === "base64" ) {
425
+ _chunk_current =
426
+ _base64_encode(
427
+ _block,
428
+ _chunk_current,
429
+ _length,
430
+ _out,
431
+ _block_last,
432
+ _amount_only);
433
+ _msg_info(
434
+ `Current chunk: '${_chunk_current}'.`);
435
+ _block_current =
436
+ _block_current + 1;
437
+ }
438
+ else {
439
+ _msg =
440
+ "Unknown encoding format " +
441
+ `${_format}.`;
442
+ _msg_error(
443
+ _msg,
444
+ 1);
445
+ }
446
+ }
447
+ for ( _block of _blocks ) {
448
+ _rm_args = [
449
+ _block
450
+ ];
451
+ _rm(
452
+ _rm_args);
453
+ }
454
+ if ( _amount_only === "true" ) {
455
+ _chunks_total =
456
+ _chunk_current;
457
+ _msg =
458
+ `File '${_in}' is base64 encoded and ` +
459
+ `split in '${_chunks_total}' ` +
460
+ `files of length '${_length}'.`;
461
+ _msg_info(
462
+ _msg);
463
+ return _chunks_total;
464
+ }
465
+ else if ( _amount_only === "false" ) {
466
+ _msg =
467
+ `Finished '${_format}' encoding ` +
468
+ `input file '${_in}'.`;
469
+ _msg_info(
470
+ _msg);
139
471
  }
140
- _buffer_prepare(
141
- _in,
142
- _out,
143
- _buffer_size_bytes);
144
- // _split(
145
- // "",
146
- // 2);
147
- // _split(
148
- // _input_file,
149
- // _output_prefix,
150
- // _chunks_amount,
151
- // _size_max) {
152
- // console.log(
153
- // _split
154
- // );
155
- // _msg =
156
- // `Creating directory '${_dir}':`;
157
- // _msg_info(
158
- // _msg);
159
- // _mkdir(
160
- // _dir);
161
- // _msg =
162
- // `Writing content '${_content}' to file '${_file}':`;
163
- // _msg_info(
164
- // _msg);
165
- // _file_write(
166
- // _file,
167
- // _content);
168
- // _msg =
169
- // `Content of directory '/':`;
170
- // _msg_info(
171
- // _msg);
172
- // _dir_content =
173
- // _ls(
174
- // "/").unwrap();
175
- // console.log(
176
- // _dir_content);
177
- // _msg =
178
- // `Content of directory '${_dir}':`;
179
- // _msg_info(
180
- // _msg);
181
- // _dir_content =
182
- // _ls(
183
- // _dir).unwrap();
184
- // console.log(
185
- // _dir_content);
186
472
  }
187
473
 
188
474
  module.exports = {