-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
451 lines (367 loc) · 11.2 KB
/
main.cpp
File metadata and controls
451 lines (367 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* LZW variable width up to 16-bit data compression implementation.
* Copyright (c) 1996-2021 Yuriy Yakimenko
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation. No representations are made about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*/
/*------------------------------------------------------------*/
/* */
/* Includes test option (Linux and Windows only). */
/* Uses cksum or certUtil to verify results. */
/* Replace with custom cksum on other platforms. */
/* */
/*------------------------------------------------------------*/
#include "common.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define ONE_KILOBYTE 1024
enum ByteSequence { SEQ_CONSTANT = 0, SEQ_INCREASING, SEQ_RANDOM };
enum ArgOption { PARSE_ERROR = -1, SYNTHETIC_TEST = 0, FLAG_PACK = 1, FLAG_UNPACK = 2, FLAG_TEST = 3 };
struct progArguments
{
char *inputFile;
char *outputFile;
int flags;
int bits, kb256;
progArguments ()
{
inputFile = NULL;
outputFile = NULL;
flags = 0;
bits = 0;
kb256 = 0;
}
~progArguments ()
{
free (inputFile);
free (outputFile);
}
};
static void show_command (const char* cmd)
{
char buffer[128];
#if defined(__linux__)
FILE* pipe = popen(cmd, "r");
#elif defined(_WIN32)
FILE* pipe = _popen(cmd, "r");
#endif
if (!pipe)
{
fprintf(stderr, "popen() failed!");
return;
}
while (fgets(buffer, sizeof(buffer), pipe) != NULL)
{
printf ("%s", buffer);
}
#if defined(__linux__)
pclose(pipe);
#elif defined(_WIN32)
_pclose(pipe);
#endif
}
static void printSyntax (const char *prog)
{
printf ("syntax: %s -(p|u|t) [-v -f -k -t] [-bN] inputFile outputFile \n", prog);
printf (" %s -large [N] \n", prog);
printf ("\t -p - pack \n");
printf ("\t -u - unpack \n");
printf ("\t -v - verbose \n");
printf ("\t -f - force overwrite; applicable with -u option only \n");
printf ("\t -k - keep dirty/incomplete output file on failure \n");
printf ("\t -t - test option; requires only inputFile \n");
printf ("\t -bN - set maximum code bits. N from 12 to %d. Default is %d.\n", SUPPORTED_MAX_BITS, DEFAULT_MAX_BITS);
printf ("\t -large - synthetic data test; N is size in 256 Kb units. Default N is 32.\n");
}
static enum ArgOption parseArguments (int argc, char *argv[], progArguments & params)
{
int fileNameSet = false;
int flagPack = 0;
int flagUnpack = 0;
int flagForce = 0;
int flagVerbose = 0;
int flagKeepDirty = 0;
int flagTest = 0;
int flagDiagnostics = 0;
int bits = DEFAULT_MAX_BITS;
bool bits_set = false;
int i, j;
char combined_flags[32] = { 0 };
if (argc == 1)
{
return PARSE_ERROR;
}
for (i = 1; i < argc; i++)
{
if (strncmp (argv[i], "-", 1) == 0)
{
if (fileNameSet)
{
return PARSE_ERROR;
}
if (strncmp (argv[i], "-b", 2) == 0)
{
/* the Compress and Decompress supports bit values from 9 to 15 */
/* however values below 12 produce low compression and are not practical */
if (strcmp (argv[i], "-b12") == 0) bits = 12;
else if (strcmp (argv[i], "-b13") == 0) bits = 13;
else if (strcmp (argv[i], "-b14") == 0) bits = 14;
else if (strcmp (argv[i], "-b15") == 0) bits = 15;
else if (strcmp (argv[i], "-b16") == 0) bits = 16;
else
{
fprintf (stderr, "Invalid number of bits. Allowed range 12 to %d.\n", SUPPORTED_MAX_BITS);
return PARSE_ERROR;
}
bits_set = true;
continue;
}
if ((i == 1 || (i == 2 && bits_set)) && 0 == strcmp(argv[i], "-large"))
{
params.bits = DEFAULT_MAX_BITS;
params.kb256 = 32;
if (bits_set && bits > 0)
params.bits = bits;
if (i == argc - 2)
{
int k = atoi(argv[i + 1]);
if (k > 0)
{
params.kb256 = k;
}
}
return SYNTHETIC_TEST; /* large test */
}
memset (combined_flags, 0, sizeof (combined_flags));
strncpy (combined_flags, argv[i], sizeof (combined_flags) - 1);
for (j = 1; combined_flags[j]; j++)
{
char flag = combined_flags[j];
if (flag == 'p')
{
flagPack = true;
}
else if (flag == 'u')
{
flagUnpack = true;
}
else if (flag == 'f')
{
flagForce = true;
}
else if (flag == 'v')
{
flagVerbose = true;
}
else if (flag == 'k')
{
flagKeepDirty = true;
}
else if (flag == 't')
{
flagTest = true;
}
else if (flag == 'd')
{
flagDiagnostics = true;
}
else
{
fprintf (stderr, "Unknown flag -%c\n", flag);
return PARSE_ERROR;
}
}
}
else /* file names */
{
fileNameSet = true;
if (!params.inputFile)
{
params.inputFile = str_dup (argv[i]);
}
else if (!params.outputFile)
{
params.outputFile = str_dup (argv[i]);
}
}
}
if (flagTest + flagPack + flagUnpack > 1) /* inconsistent args */
{
fprintf (stderr, "Cannot combine -p, -u and -t flags.\n");
return PARSE_ERROR;
}
if (flagTest + flagPack + flagUnpack == 0)
{
fprintf (stderr, "No pack, unpack or test flags given.\n");
return PARSE_ERROR;
}
if (flagUnpack && bits_set)
{
fprintf (stderr, "Cannot cobine -u and -bit flag.\n");
return PARSE_ERROR;
}
if (flagTest)
{
if (NULL == params.inputFile)
return PARSE_ERROR;
}
else
{
if (NULL == params.inputFile || NULL == params.outputFile)
return PARSE_ERROR;
}
if (flagForce) params.flags |= OVERWRITE_FLAG;
if (flagVerbose) params.flags |= VERBOSE_OUTPUT;
if (flagKeepDirty) params.flags |= KEEP_ON_ERROR;
if (flagDiagnostics) params.flags |= DIAGNOSTIC_OUTPUT;
params.bits = bits;
ArgOption ret = PARSE_ERROR;
if (flagTest) ret = FLAG_TEST;
else if (flagPack) ret = FLAG_PACK;
else if (flagUnpack) ret = FLAG_UNPACK;
return ret;
}
static void run_cksum (const char *file)
{
size_t size = strlen (file) + 1;
char * command = (char *)malloc (size + 32);
if (!command)
{
perror ("Cannot allocate memory.");
return;
}
#if defined(__linux__)
sprintf (command, "cksum %s", file);
#elif defined(_WIN32)
sprintf(command, "certutil -hashfile %s", file);
#endif
show_command ( command );
free (command);
}
/*--------------------------------------------------------------------*/
/* For testing purposes only */
/*--------------------------------------------------------------------*/
static int syntheticDataTest (int kilobytes256, int bits = DEFAULT_MAX_BITS, ByteSequence option = SEQ_CONSTANT)
{
const char input [] = "synth.bin";
const char packed_input[] = "synth.lzw";
const char unpacked_input[] = "synth.out";
if (true)
{
FILE *fp = fopen (input, "w+b");
const int size = ONE_KILOBYTE;
char buffer[ONE_KILOBYTE];
memset (buffer, 0x0A, sizeof(buffer));
if (option == SEQ_INCREASING)
{
for (int i=0; i < size; i++) buffer[i] = (char)(i & 0xFF);
}
else if (option == SEQ_RANDOM)
{
for (int i=0; i < size; i++) buffer[i] = (char)(rand() & 0xFF);
}
for (int i=0; i < 256 * kilobytes256; i++)
{
fwrite (buffer, 1, sizeof(buffer), fp);
}
fclose (fp);
}
if (0 == Compress2 (input, packed_input, VERBOSE_OUTPUT, bits))
{
printf ("Synthetic input compression failed.\n");
return EXIT_FAILURE;
}
printf ("Synthetic input compression successful.\n");
if (0 == Decompress(packed_input, unpacked_input, OVERWRITE_FLAG))
{
printf ("Synthetic input decompression failed.\n");
return EXIT_FAILURE;
}
printf ("Synthetic input decompression successful.\n");
/* compare two files by running cksum */
run_cksum (input);
run_cksum (unpacked_input);
remove (unpacked_input);
remove (packed_input);
remove (input);
return EXIT_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////////////////
#ifndef LIBTEST_MAIN // add LIBTEST #define to compile the test with static library
int main(int argc, char *argv[])
{
progArguments params;
enum ArgOption option = parseArguments (argc, argv, params);
if (option == PARSE_ERROR)
{
printSyntax (argv[0]);
return EXIT_FAILURE;
}
else if (option == SYNTHETIC_TEST) /* synthetic test */
{
return syntheticDataTest (params.kb256, params.bits, SEQ_CONSTANT);
}
else if (option == FLAG_PACK)
{
if (0 == Compress2 (params.inputFile, params.outputFile, params.flags, params.bits))
{
printf ("Compression failed.\n");
return EXIT_FAILURE;
}
else
{
printf ("Compression successful.\n");
return EXIT_SUCCESS;
}
}
else if (option == FLAG_UNPACK)
{
if (0 == Decompress(params.inputFile, params.outputFile, params.flags))
{
printf ("Decompression failed.\n");
return EXIT_FAILURE;
}
else
{
printf ("Decompression successful.\n");
return EXIT_SUCCESS;
}
}
else if (option == FLAG_TEST)
{
char temp_name [PATH_MAX], out_name [PATH_MAX];
tmpnam_s (temp_name, sizeof(temp_name));
if (0 == Compress2 (params.inputFile, temp_name, params.flags, params.bits ))
{
printf ("Compression failed.\n");
return EXIT_FAILURE;
}
else
{
printf ("Compression successful.\n");
}
tmpnam_s (out_name, sizeof(out_name));
if (0 == Decompress(temp_name, out_name, params.flags | OVERWRITE_FLAG))
{
printf ("Decompression failed.\n");
return EXIT_FAILURE;
}
else
{
printf ("Decompression successful.\n");
}
/* compare 2 files. */
run_cksum (params.inputFile);
run_cksum (out_name);
remove (temp_name);
remove (out_name);
}
return EXIT_SUCCESS;
}
#endif // LIBTEST_MAIN