summaryrefslogtreecommitdiffstats
path: root/zutils.cc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--zutils.cc33
1 files changed, 19 insertions, 14 deletions
diff --git a/zutils.cc b/zutils.cc
index 54090ff..ad5cd3e 100644
--- a/zutils.cc
+++ b/zutils.cc
@@ -1,5 +1,5 @@
/* Zutils - Utilities dealing with compressed files
- Copyright (C) 2009-2021 Antonio Diaz Diaz.
+ Copyright (C) 2009-2022 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -46,8 +46,8 @@ inline bool isvalid_ds( const uint8_t ds ) // lzip valid dictionary_size
}
-/* Returns -1 if child not terminated, 2 in case of error, or exit status of
- child process 'pid'.
+/* Return -1 if child not terminated, 2 in case of error, or exit status of
+ child process 'pid'. Return 0 if child was terminated by SIGPIPE.
*/
int child_status( const pid_t pid, const char * const name )
{
@@ -73,8 +73,8 @@ int child_status( const pid_t pid, const char * const name )
} // end namespace
-/* Returns the number of bytes really read.
- If (returned value < size) and (errno == 0), means EOF was reached.
+/* Return the number of bytes really read.
+ If (value returned < size) and (errno == 0), means EOF was reached.
*/
int readblock( const int fd, uint8_t * const buf, const int size )
{
@@ -92,8 +92,8 @@ int readblock( const int fd, uint8_t * const buf, const int size )
}
-/* Returns the number of bytes really written.
- If (returned value < size), it is always an error.
+/* Return the number of bytes really written.
+ If (value returned < size), it is always an error.
*/
int writeblock( const int fd, const uint8_t * const buf, const int size )
{
@@ -146,7 +146,7 @@ bool good_status( const Children & children, const bool finished )
// even if compressor finished, trailing data may remain in data feeder
if( i == 0 || !finished )
{
- const int tmp = child_status( pid, name );
+ const int tmp = child_status( pid, name ); // 0 if SIGPIPE
if( tmp < 0 ) // child not terminated
{ kill( pid, SIGTERM ); wait_for_child( pid, name ); }
else if( tmp != 0 ) error = true; // child status != 0
@@ -246,7 +246,7 @@ bool set_data_feeder( const std::string & filename, int * const infdp,
}
-// Returns format index or -1 if uncompressed
+// Return format index, or -1 if uncompressed.
//
int test_format( const int infd, uint8_t magic_data[],
int * const magic_sizep )
@@ -254,7 +254,8 @@ int test_format( const int infd, uint8_t magic_data[],
enum { bzip2_magic_size = 3,
gzip_magic_size = 2,
lzip_magic_size = 5,
- xz_magic_size = 5 };
+ xz_magic_size = 5,
+ zstd_magic_size = 4 };
const uint8_t bzip2_magic[bzip2_magic_size] =
{ 0x42, 0x5A, 0x68 }; // "BZh"
const uint8_t gzip_magic[gzip_magic_size] =
@@ -263,19 +264,23 @@ int test_format( const int infd, uint8_t magic_data[],
{ 0x4C, 0x5A, 0x49, 0x50, 0x01 }; // "LZIP\001"
const uint8_t xz_magic[xz_magic_size] =
{ 0xFD, 0x37, 0x7A, 0x58, 0x5A }; // 0xFD, "7zXZ"
+ const uint8_t zstd_magic[zstd_magic_size] =
+ { 0x28, 0xB5, 0x2F, 0xFD }; // 0xFD2FB528 LE
*magic_sizep = readblock( infd, magic_data, magic_buf_size );
- if( *magic_sizep == magic_buf_size )
+ if( *magic_sizep == magic_buf_size ) // test formats in search order
{
+ if( std::memcmp( magic_data, lzip_magic, lzip_magic_size ) == 0 &&
+ isvalid_ds( magic_data[lzip_magic_size] ) )
+ return fmt_lz;
if( std::memcmp( magic_data, bzip2_magic, bzip2_magic_size ) == 0 &&
magic_data[3] >= '1' && magic_data[3] <= '9' &&
std::memcmp( magic_data + 4, "1AY&SY", 6 ) == 0 )
return fmt_bz2;
if( std::memcmp( magic_data, gzip_magic, gzip_magic_size ) == 0 )
return fmt_gz;
- if( std::memcmp( magic_data, lzip_magic, lzip_magic_size ) == 0 &&
- isvalid_ds( magic_data[lzip_magic_size] ) )
- return fmt_lz;
+ if( std::memcmp( magic_data, zstd_magic, zstd_magic_size ) == 0 )
+ return fmt_zst;
if( std::memcmp( magic_data, xz_magic, xz_magic_size ) == 0 )
return fmt_xz;
}