aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/widget-zcat.c
blob: 720e25462761e8cec6a7a1ebfcf635d69cae3304 (plain) (blame)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 *  Secure Core Utilities
 *  Copyright (C) 2015, 2017 David M. Syzdek <david@syzdek.net>.
 *
 *  @SYZDEK_BSD_LICENSE_START@
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of David M. Syzdek nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 *  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M SYZDEK BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 *
 *  @SYZDEK_BSD_LICENSE_END@
 */
#include "widget-zcat.h"

///////////////
//           //
//  Headers  //
//           //
///////////////
#ifdef __SECURECOREUTILS_PMARK
#pragma mark - Headers
#endif

#include <assert.h>
#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>

#ifdef USE_ZLIB
#include <zlib.h>
#endif

#ifdef USE_BZIP2
#include <bzlib.h>
#endif

#ifdef USE_LZMA
#include <lzma.h>
#endif

#include "lzw/lzw.h"


/////////////////
//             //
//  Variables  //
//             //
/////////////////
#ifdef __SECURECOREUTILS_PMARK
#pragma mark - Variables
#endif

const uint8_t scm_magic_bz2[3]   = { 0x42, 0x5a, 0x68 };                   // tar.bz2
const uint8_t scm_magic_gz[2]    = { 0x1f, 0x8b };                         // tar.gz
const uint8_t scm_magic_lz4[4]   = { 0x04, 0x22, 0x4d, 0x18 };             // tar.lz4
const uint8_t scm_magic_z_lzw[2] = { 0x1f, 0x9d };                         // tar.Z
const uint8_t scm_magic_z_lzh[2] = { 0x1f, 0xa0 };                         // tar.Z
const uint8_t scm_magic_lzma[6]  = { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }; // tar.xz


//////////////////
//              //
//  Prototypes  //
//              //
//////////////////
#ifdef __SECURECOREUTILS_PMARK
#pragma mark - Prototypes
#endif

int scu_widget_zcat_bz2(scu_config * cnf);
int scu_widget_zcat_gz(scu_config * cnf);
int scu_widget_zcat_lzma(scu_config * cnf);
#ifdef USE_LZMA
int scu_widget_zcat_lzma_perror(scu_config * cnf, lzma_ret ret);
#endif
int scu_widget_zcat_lzw(scu_config * cnf);
void scu_widget_zcat_usage(scu_config * cnf);


/////////////////
//             //
//  Functions  //
//             //
/////////////////
#ifdef __SECURECOREUTILS_PMARK
#pragma mark - Functions
#endif

int scu_widget_zcat(scu_config * cnf)
{
   int            fd;
   int            c;
   int            opt_index;
   int            rc;
   uint8_t        magic[16];
   ssize_t        len;

   // getopt options
   static char   short_opt[] = "+hqVv";
   static struct option long_opt[] =
   {
      {"help",             no_argument,       NULL, 'h' },
      {"quiet",            no_argument,       NULL, 'q' },
      {"silent",           no_argument,       NULL, 'q' },
      {"version",          no_argument,       NULL, 'V' },
      {"verbose",          no_argument,       NULL, 'v' },
      { NULL, 0, NULL, 0 }
   };

   assert(cnf != NULL);
   cnf->short_opt = short_opt;

   bzero(magic, sizeof(magic));

   while((c = getopt_long(cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1)
   {
      switch(c)
      {
         case -1:	/* no more arguments */
         case 0:	/* long options toggles */
         break;

         case 'h':
         scu_widget_zcat_usage(cnf);
         return(0);

         case 's':
         cnf->quiet = 1;
         if ((cnf->verbose))
         {
            fprintf(stderr, "%s: %s: incompatible options\n", PROGRAM_NAME, cnf->widget->name);
            fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
            return(1);
         };
         break;

         case 'V':
         printf("%s widget\n", cnf->widget->name);
         scu_version();
         return(0);

         case 'v':
         cnf->verbose++;
         if ((cnf->quiet))
         {
            fprintf(stderr, "%s: %s: incompatible options\n", PROGRAM_NAME, cnf->widget->name);
            fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name);
            return(1);
         };
         break;

         case '?':
         fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name);
         return(1);

         default:
         fprintf(stderr, "%s: %s: unrecognized option `--%c'\n", PROGRAM_NAME, cnf->widget->name, c);
         fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name);
         return(1);
      };
   };

   if ((cnf->argc - optind) < 1)
   {
      fprintf(stderr, "%s: %s: missing required argument\n", PROGRAM_NAME, cnf->widget->name);
      fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name);
      return(1);
   };
   if ((cnf->argc - optind) > 1)
   {
      fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->argv[optind+1]);
      fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget->name);
      return(1);
   };

   // checks file for restriction validations
   if ((rc = scu_pathcheck(cnf->argv[optind], 0)) != 0)
   {
      fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, scu_strerror(rc));
      return(1);
   };

   // read magic number
   if ((fd = open(cnf->argv[optind], O_RDONLY)) == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno));
      return(1);
   };
   if ((len = read(fd, magic, sizeof(magic))) == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno));
      close(fd);
      return(1);
   };
   close(fd);


   // processes gzip file
#ifdef USE_ZLIB
   if (!( memcmp(magic, scm_magic_gz, sizeof(scm_magic_gz)) ))
      return(scu_widget_zcat_gz(cnf));
#endif

#ifdef USE_BZIP2
   if (!( memcmp(magic, scm_magic_bz2, sizeof(scm_magic_bz2)) ))
      return(scu_widget_zcat_bz2(cnf));
#endif

#ifdef USE_LZMA
   if (!( memcmp(magic, scm_magic_lzma, sizeof(scm_magic_lzma)) ))
      return(scu_widget_zcat_lzma(cnf));
#endif

   if (!( memcmp(magic, scm_magic_z_lzw, sizeof(scm_magic_z_lzw)) ))
      return(scu_widget_zcat_lzw(cnf));

//   if (!( memcmp(magic, scm_magic_z_lzh, sizeof(scm_magic_z_lzh)) ))
//      return(scu_widget_zcat_lzh(cnf));

   fprintf(stderr, "%s: %s: unable to determine compression algorithms\n", PROGRAM_NAME, cnf->widget->name);
   return(1);
}


#ifdef USE_BZIP2
int scu_widget_zcat_bz2(scu_config * cnf)
{
   int         bzerror;
   char        buff[16384];
   ssize_t     len;
   FILE      * fs;
   BZFILE    * bz2;

   if ((fs = fopen(cnf->argv[optind], "r")) == NULL)
   {
      fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno));
      return(1);
   };

   bz2 = BZ2_bzReadOpen(&bzerror, fs, 0, 1, NULL, 0);
   if (bzerror != BZ_OK)
   {
      fprintf(stderr, "%s: %s: error with decompression\n", PROGRAM_NAME, cnf->widget->name);
      BZ2_bzReadClose(&bzerror, bz2);
      fclose(fs);
      return(1);
   };

   while ((len = BZ2_bzRead(&bzerror, bz2, buff, sizeof(buff))) > 0)
   {
      if ((len = write(STDOUT_FILENO, buff, len)) == -1)
      {
         fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno));
         BZ2_bzReadClose(&bzerror, bz2);
         fclose(fs);
         return(1);
      };
   };

   BZ2_bzReadClose(&bzerror, bz2);
   fclose(fs);

   return(0);
}
#endif


#ifdef USE_ZLIB
int scu_widget_zcat_gz(scu_config * cnf)
{
   gzFile   gz;
   char     buff[4096];
   int      gzlen;
   ssize_t  len;

   errno = 0;

   if ((gz = gzopen(cnf->argv[optind], "rb")) == NULL)
   {
      if (errno == 0)
      {
         fprintf(stderr, "%s: %s: insufficient memory\n", PROGRAM_NAME, cnf->widget->name);
         return(1);
      };
   };

   while((gzlen = gzread(gz, buff, sizeof(buff))) > 0)
   {
      if ((len = write(STDOUT_FILENO, buff, gzlen)) == -1)
      {
         fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, strerror(errno));
         gzclose(gz);
         return(1);
      };
   };
   if (gzlen == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, gzerror(gz, NULL));
      gzclose(gz);
      return(1);
   };

   gzclose(gz);

   return(0);
}
#endif


int scu_widget_zcat_lzw(scu_config * cnf)
{
   int            fd;
   lzwFile      * lzw;
   char           buff[4096];
   ssize_t        len;

   if ((fd = open(cnf->argv[optind], O_RDONLY)) == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
      return(1);
   };

   if ((lzw = lzw_fdopen(fd)) == NULL)
   {
      fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
      close(fd);
      return(1);
   };

   while((len = lzw_read(lzw, buff, sizeof(buff))) > 0)
   {
      if ((len = write(STDOUT_FILENO, buff, len)) == -1)
      {
         fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
         lzw_close(lzw);
         return(1);
      };
   };
   if (len == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
      lzw_close(lzw);
      return(1);
   };

   lzw_close(lzw);

   return(0);
}


void scu_widget_zcat_usage(scu_config * cnf)
{
   scu_usage_summary(cnf, " [OPTIONS] file");
   printf("\n");
   scu_usage_options(cnf);
   printf("\n");
   scu_usage_restrictions();
   printf("\n");
   return;
}


#ifdef USE_LZMA
int scu_widget_zcat_lzma(scu_config * cnf)
{
   int            fd;
   lzma_ret       ret;
   lzma_stream    strm;
   lzma_action    action;
   uint8_t        in[16384];
   uint8_t        out[16384];
   ssize_t        len;
   int            eof;

   bzero(&strm, sizeof(strm));
   action         = LZMA_RUN;
   strm.next_in   = in;
   strm.next_out  = out;
   strm.avail_in  = 0;
   strm.avail_out = sizeof(out);

   if ((fd = open(cnf->argv[optind], O_RDONLY)) == -1)
   {
      fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
      return(1);
   };

   if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED)) != LZMA_OK)
   {
      scu_widget_zcat_lzma_perror(cnf, ret);
      close(fd);
      return(1);
   };

   ret = LZMA_OK;
   eof = 0;
   while (ret == LZMA_OK)
   {
      // read from compressed file if input buffer is empty
      if ((strm.avail_in == 0) && (!(eof)))
      {
         if ((len = read(fd, in, sizeof(in))) == -1)
         {
            fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
            close(fd);
            return(1);
         };
         strm.next_in   = in;
         strm.avail_in  = len;
         if (len < (ssize_t)sizeof(in))
         {
            action = LZMA_FINISH;
            eof = 1;
         };
      };

      // uncompress file
      if ((ret = lzma_code(&strm, action)) != LZMA_OK)
      {
         if (ret != LZMA_STREAM_END)
         {
            scu_widget_zcat_lzma_perror(cnf, ret);
            close(fd);
            return(1);
         };
      };

      // print uncompressed data if buffer is full or done inflating data
      if ((strm.avail_out == 0) || (ret == LZMA_STREAM_END))
      {
         if ((len = write(STDOUT_FILENO, out, (sizeof(out) - strm.avail_out))) == -1)
         {
            fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
            close(fd);
            return(1);
         };
         strm.avail_out = sizeof(out);
         strm.next_out  = out;
      };
   };

   close(fd);

   return(0);
}


int scu_widget_zcat_lzma_perror(scu_config * cnf, lzma_ret ret)
{
   switch(ret)
   {
      case LZMA_BUF_ERROR:
      fprintf(stderr, "%s: %s: truncated or corrupt file\n", cnf->prog_name, cnf->widget->name);
      return(1);

      case LZMA_DATA_ERROR:
      fprintf(stderr, "%s: %s: compressed file is corrupt\n", cnf->prog_name, cnf->widget->name);
      return(1);

      case LZMA_FORMAT_ERROR:
      fprintf(stderr, "%s: %s: invalid LZMA format\n", cnf->prog_name, cnf->widget->name);
      return(1);

      case LZMA_MEM_ERROR:
      fprintf(stderr, "%s: %s: out of virtual memory\n", cnf->prog_name, cnf->widget->name);
      return(1);

      case LZMA_OPTIONS_ERROR:
      fprintf(stderr, "%s: %s: unsupported LZMA options\n", cnf->prog_name, cnf->widget->name);
      return(1);

      case LZMA_STREAM_END:
      fprintf(stderr, "%s: %s: end of LZMA stream\n", cnf->prog_name, cnf->widget->name);
      return(0);

      default:
      fprintf(stderr, "%s: %s: unknown LZMA error\n", cnf->prog_name, cnf->widget->name);
      break;

   };
   return(1);
}
#endif

/* end of source */