summaryrefslogtreecommitdiffstats
path: root/zutils.cc
blob: 5451534440ad2f842049e7b87f711ef280a63e92 (plain)
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
/*  Zutils - Utilities dealing with compressed files
    Copyright (C) 2009, 2010 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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _FILE_OFFSET_BITS 64

#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stdint.h>
#include <unistd.h>
#include <sys/wait.h>

#include "zutils.h"


const char * invocation_name = 0;
const char * util_name = program_name;

int verbosity = 0;


// Returns the number of bytes really read.
// If (returned value < size) and (errno == 0), means EOF was reached.
//
int readblock( const int fd, uint8_t * const buf, const int size ) throw()
  {
  int rest = size;
  errno = 0;
  while( rest > 0 )
    {
    errno = 0;
    const int n = read( fd, buf + size - rest, rest );
    if( n > 0 ) rest -= n;
    else if( n == 0 ) break;
    else if( errno != EINTR && errno != EAGAIN ) break;
    }
  return ( rest > 0 ) ? size - rest : size;
  }


// Returns the number of bytes really written.
// If (returned value < size), it is always an error.
//
int writeblock( const int fd, const uint8_t * const buf, const int size ) throw()
  {
  int rest = size;
  errno = 0;
  while( rest > 0 )
    {
    errno = 0;
    const int n = write( fd, buf + size - rest, rest );
    if( n > 0 ) rest -= n;
    else if( errno && errno != EINTR && errno != EAGAIN ) break;
    }
  return ( rest > 0 ) ? size - rest : size;
  }


bool feed_data( const int infd, const int outfd,
                const uint8_t * magic_data, const int magic_size )
  {
  if( writeblock( outfd, magic_data, magic_size ) != magic_size )
    { show_error( "Write error", errno ); return false; }
  enum { buffer_size = 4096 };
  uint8_t buffer[buffer_size];
  while( true )
    {
    const int size = readblock( infd, buffer, buffer_size );
    if( size != buffer_size && errno )
      { show_error( "Read error", errno ); return false; }
    if( size > 0 && writeblock( outfd, buffer, size ) != size )
      { show_error( "Write error", errno ); return false; }
    if( size < buffer_size ) break;
    }
  return true;
  }


bool set_data_feeder( int * const infdp, pid_t * const pidp )
  {
  std::string file_type;
  const uint8_t * magic_data;
  int magic_size;
  const bool compressed =
    test_format( *infdp, file_type, &magic_data, &magic_size );

  if( compressed )			// compressed with `file_type'
    {
    int fda[2];				// pipe from feeder
    int fda2[2];			// pipe from decompressor
    if( pipe( fda ) < 0 || pipe( fda2 ) < 0 )
      { show_error( "Can't create pipe", errno ); return false; }
    const int old_infd = *infdp;
    *infdp = fda2[0];
    const pid_t pid = fork();
    if( pid == 0 )			// child (decompressor feeder)
      {
      const pid_t pid2 = fork();
      if( pid2 == 0 )			// grandchild (decompressor)
        {
        if( dup2( fda[0], STDIN_FILENO ) >= 0 &&
            dup2( fda2[1], STDOUT_FILENO ) >= 0 &&
            close( fda[0] ) == 0 && close( fda[1] ) == 0 &&
            close( fda2[0] ) == 0 && close( fda2[1] ) == 0 )
          execlp( file_type.c_str(), file_type.c_str(), "-cdfq", (char *)0 );
        if( verbosity >= 0 )
          std::fprintf( stderr, "%s: Can't exec `%s': %s.\n",
                        util_name, file_type.c_str(), std::strerror( errno ) );
        _exit( 2 );
        }
      if( pid2 < 0 )
        {
        if( verbosity >= 0 )
          std::fprintf( stderr, "%s: Can't fork `%s': %s.\n",
                        util_name, file_type.c_str(), std::strerror( errno ) );
        _exit( 2 );
        }

      if( close( fda[0] ) != 0 ||
          close( fda2[0] ) != 0 || close( fda2[1] ) != 0 ||
          !feed_data( old_infd, fda[1], magic_data, magic_size ) )
        _exit( 2 );
      if( close( fda[1] ) != 0 )
        { show_error( "Can't close output of decompressor feeder", errno );
          _exit( 2 ); }
      _exit( wait_for_child( pid2, file_type.c_str() ) );
      }
					// parent
    close( fda[0] ); close( fda[1] ); close( fda2[1] );
    if( pid < 0 )
      { show_error( "Can't fork decompressor feeder", errno ); return false; }
    *pidp = pid;
    }
  else					// not compressed
    {
    int fda[2];				// pipe from feeder
    if( pipe( fda ) < 0 )
      { show_error( "Can't create pipe", errno ); return false; }
    const int old_infd = *infdp;
    *infdp = fda[0];
    const pid_t pid = fork();
    if( pid == 0 )			// child (feeder)
      {
      if( close( fda[0] ) != 0 ||
          !feed_data( old_infd, fda[1], magic_data, magic_size ) )
        _exit( 2 );
      if( close( fda[1] ) != 0 )
        { show_error( "Can't close output of data feeder", errno );
          _exit( 2 ); }
      _exit( 0 );
      }
					// parent
    close( fda[1] );
    if( pid < 0 )
      { show_error( "Can't fork data feeder", errno ); return false; }
    *pidp = pid;
    }
  return true;
  }


void show_help_addr() throw()
  {
  std::printf( "\nReport bugs to zutils-bug@nongnu.org\n" );
  std::printf( "Zutils home page: http://www.nongnu.org/zutils/zutils.html\n" );
  }


void show_version( const char * const Util_name ) throw()
  {
  if( !Util_name || !*Util_name )
    std::printf( "%s %s\n", Program_name, PROGVERSION );
  else
    std::printf( "%s (%s) %s\n", Util_name, program_name, PROGVERSION );
  std::printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
  std::printf( "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" );
  std::printf( "This is free software: you are free to change and redistribute it.\n" );
  std::printf( "There is NO WARRANTY, to the extent permitted by law.\n" );
  }


void show_error( const char * const msg, const int errcode,
                 const bool help ) throw()
  {
  if( verbosity >= 0 )
    {
    if( msg && msg[0] )
      {
      std::fprintf( stderr, "%s: %s", util_name, msg );
      if( errcode > 0 )
        std::fprintf( stderr, ": %s", std::strerror( errcode ) );
      std::fprintf( stderr, "\n" );
      }
    if( help && invocation_name && invocation_name[0] )
      std::fprintf( stderr, "Try `%s --help' for more information.\n",
                    invocation_name );
    }
  }


void internal_error( const char * const msg )
  {
  if( verbosity >= 0 )
    std::fprintf( stderr, "%s: internal error: %s.\n", util_name, msg );
  std::exit( 3 );
  }


unsigned char xdigit( const int value ) throw()
  {
  if( value >= 0 && value <= 9 ) return '0' + value;
  if( value >= 10 && value <= 15 ) return 'A' + ( value - 10 );
  return 0;
  }


bool test_format( const int infd, std::string & file_type,
                  const uint8_t ** const magic_datap, int * const magic_sizep )
  {
  enum { buf_size = 5 };
  static uint8_t buf[buf_size];
  int i = 0;
  file_type.clear();
  if( readblock( infd, buf, 1 ) == 1 )
    {
    ++i;
    if( buf[0] == bzip2_magic[0] )
      {
      if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[1] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == bzip2_magic[2] )
        { file_type = "bzip2";
          *magic_datap = bzip2_magic; *magic_sizep = bzip2_magic_size; }
      }
    else if( buf[0] == gzip_magic[0] )
      {
      if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == gzip_magic[1] )
        { file_type = "gzip";
          *magic_datap = gzip_magic; *magic_sizep = gzip_magic_size; }
      }
    else if( buf[0] == lzip_magic[0] )
      {
      if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[1] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[2] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == lzip_magic[3] )
        { file_type = "lzip";
          *magic_datap = lzip_magic; *magic_sizep = lzip_magic_size; }
      }
    else if( buf[0] == xz_magic[0] )
      {
      if( readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[1] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[2] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[3] &&
          readblock( infd, &buf[i], 1 ) == 1 && buf[i++] == xz_magic[4] )
        { file_type = "xz";
          *magic_datap = xz_magic; *magic_sizep = xz_magic_size; }
      }
    }
  if( file_type.size() ) return true;
  for( int j = 0; j < i; ++j )
    {
    file_type += xdigit( buf[j] >> 4 );
    file_type += xdigit( buf[j] & 0x0F );
    }
  *magic_datap = buf; *magic_sizep = i;
  return false;
  }


int wait_for_child( const pid_t pid, const char * const name )
  {
  int status;
  while( waitpid( pid, &status, 0 ) == -1 )
    {
    if( errno != EINTR )
      {
      if( verbosity >= 0 )
        std::fprintf( stderr, "%s: Error waiting termination of `%s': %s.\n",
                  util_name, name, std::strerror( errno ) );
      _exit( 1 );
      }
    }

  if( WIFEXITED( status ) ) return WEXITSTATUS( status );
  else return 1;
  }