aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/widget-zcat.c
diff options
context:
space:
mode:
authorDavid M. Syzdek <syzdek@bindlebinaries.com>2017-07-06 15:41:05 -0800
committerDavid M. Syzdek <syzdek@bindlebinaries.com>2017-07-06 15:41:05 -0800
commit6a12b9c6e0c7d459e8814cab72ea0ade6f0345a8 (patch)
tree0da64bcfc069f63c1ad7cd1705e145ab497cd3e8 /src/widget-zcat.c
parent1fb9caf12a30ef0bebc30336c527d79b61603414 (diff)
downloadsecurecoreutils-6a12b9c6e0c7d459e8814cab72ea0ade6f0345a8.tar.gz
securecoreutils-6a12b9c6e0c7d459e8814cab72ea0ade6f0345a8.tar.bz2
securecoreutils-6a12b9c6e0c7d459e8814cab72ea0ade6f0345a8.tar.xz
securecoreutils-6a12b9c6e0c7d459e8814cab72ea0ade6f0345a8.zip
Converting zcat to native xz library.
Diffstat (limited to 'src/widget-zcat.c')
-rw-r--r--src/widget-zcat.c162
1 files changed, 161 insertions, 1 deletions
diff --git a/src/widget-zcat.c b/src/widget-zcat.c
index f84a01a..dc36826 100644
--- a/src/widget-zcat.c
+++ b/src/widget-zcat.c
@@ -63,6 +63,10 @@
63#include <bzlib.h> 63#include <bzlib.h>
64#endif 64#endif
65 65
66#ifdef USE_XZ
67#include <lzma.h>
68#endif
69
66 70
67///////////////// 71/////////////////
68// // 72// //
@@ -78,6 +82,20 @@ const uint8_t scm_magic_z_lzh[2] = { 0x1f, 0xa0 }; // ta
78const uint8_t scm_magic_xz[6] = { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }; // tar.xz 82const uint8_t scm_magic_xz[6] = { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00 }; // tar.xz
79 83
80 84
85//////////////////
86// //
87// Prototypes //
88// //
89//////////////////
90#ifdef __TALLYMARK_PMARK
91#pragma mark -
92#endif
93
94#ifdef USE_XZ
95int scu_widget_zcat_xz_perror(scu_config * cnf, lzma_ret ret);
96#endif
97
98
81///////////////// 99/////////////////
82// // 100// //
83// Functions // 101// Functions //
@@ -210,7 +228,7 @@ int scu_widget_zcat(scu_config * cnf)
210 228
211#ifdef USE_XZ 229#ifdef USE_XZ
212 if (!( memcmp(magic, scm_magic_xz, sizeof(scm_magic_xz)) )) 230 if (!( memcmp(magic, scm_magic_xz, sizeof(scm_magic_xz)) ))
213 return(scu_widget_zcat_prog(cnf, "xz -cd")); 231 return(scu_widget_zcat_xz(cnf));
214#endif 232#endif
215 233
216#ifdef USE_Z 234#ifdef USE_Z
@@ -332,4 +350,146 @@ void scu_widget_zcat_usage(scu_config * cnf)
332 return; 350 return;
333} 351}
334 352
353
354#ifdef USE_XZ
355int scu_widget_zcat_xz(scu_config * cnf)
356{
357 int fd;
358 lzma_ret ret;
359 lzma_stream strm;
360 lzma_action action;
361 uint8_t in[16384];
362 uint8_t out[16384];
363 size_t len;
364
365 bzero(&strm, sizeof(strm));
366 action = LZMA_RUN;
367 strm.next_in = in;
368 strm.next_out = out;
369 strm.avail_in = 0;
370 strm.avail_out = sizeof(out);
371
372 if ((fd = open(cnf->argv[optind], O_RDONLY)) == -1)
373 {
374 fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
375 return(1);
376 };
377
378 if ((ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED)) != LZMA_OK)
379 {
380 scu_widget_zcat_xz_perror(cnf, ret);
381 close(fd);
382 return(1);
383 };
384
385
386 while (action == LZMA_RUN)
387 {
388 // read from compressed file
389 strm.next_in = in;
390 if (strm.avail_in != sizeof(in))
391 {
392 if ((len = read(fd, &in[strm.avail_in], (sizeof(in) - strm.avail_in))) == -1)
393 {
394 fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
395 close(fd);
396 return(1);
397 };
398 if (len == 0)
399 action = LZMA_FINISH;
400 else
401 strm.avail_in += len;
402 };
403
404 // uncompress file
405 if ((ret = lzma_code(&strm, action)) != LZMA_OK)
406 {
407 if (ret != LZMA_STREAM_END)
408 {
409 scu_widget_zcat_xz_perror(cnf, ret);
410 close(fd);
411 return(1);
412 };
413 };
414
415 // print uncompressed data
416 if (strm.avail_out == sizeof(out))
417 continue;
418 if ((len = write(STDOUT_FILENO, out, (sizeof(out) - strm.avail_out))) == -1)
419 {
420 fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
421 close(fd);
422 return(1);
423 };
424 strm.avail_out += len;
425 };
426
427 close(fd);
428
429 // finish uncompressing data
430 while ( (ret != LZMA_OK) && (ret != LZMA_STREAM_END))
431 {
432 // uncompress remaining data in buffer
433 if ((ret = lzma_code(&strm, action)) != LZMA_OK)
434 {
435 if (ret != LZMA_STREAM_END)
436 {
437 scu_widget_zcat_xz_perror(cnf, ret);
438 return(1);
439 };
440 };
441
442 // print uncompressed data
443 if (strm.avail_out == sizeof(out))
444 continue;
445 if ((len = write(STDOUT_FILENO, out, (sizeof(out) - strm.avail_out))) == -1)
446 {
447 fprintf(stderr, "%s: %s: %s\n", cnf->prog_name, cnf->widget->name, strerror(errno));
448 close(fd);
449 return(1);
450 };
451 strm.avail_out += len;
452 };
453
454 return(0);
455}
456
457
458int scu_widget_zcat_xz_perror(scu_config * cnf, lzma_ret ret)
459{
460 switch(ret)
461 {
462 case LZMA_BUF_ERROR:
463 fprintf(stderr, "%s: %s: truncated or corrupt file\n", cnf->prog_name, cnf->widget->name);
464 return(1);
465
466 case LZMA_DATA_ERROR:
467 fprintf(stderr, "%s: %s: compressed file is corrupt\n", cnf->prog_name, cnf->widget->name);
468 return(1);
469
470 case LZMA_FORMAT_ERROR:
471 fprintf(stderr, "%s: %s: invalid .xz format\n", cnf->prog_name, cnf->widget->name);
472 return(1);
473
474 case LZMA_MEM_ERROR:
475 fprintf(stderr, "%s: %s: out of virtual memory\n", cnf->prog_name, cnf->widget->name);
476 return(1);
477
478 case LZMA_OPTIONS_ERROR:
479 fprintf(stderr, "%s: %s: unsupported LZMA options\n", cnf->prog_name, cnf->widget->name);
480 return(1);
481
482 case LZMA_STREAM_END:
483 fprintf(stderr, "%s: %s: end of LZMA stream\n", cnf->prog_name, cnf->widget->name);
484 return(0);
485
486 default:
487 fprintf(stderr, "%s: %s: unknown LZMA error\n", cnf->prog_name, cnf->widget->name);
488 break;
489
490 };
491 return(1);
492}
493#endif
494
335/* end of source */ 495/* end of source */