summaryrefslogtreecommitdiffstats
path: root/file_index.c
blob: 74adf952b412674aab36de85f835ab9974465f30 (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
/*  Clzip - LZMA lossless data compressor
    Copyright (C) 2010-2017 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 2 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 <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

#include "lzip.h"
#include "file_index.h"


static int seek_read( const int fd, uint8_t * const buf, const int size,
                      const long long pos )
  {
  if( lseek( fd, pos, SEEK_SET ) == pos )
    return readblock( fd, buf, size );
  return 0;
  }


static bool add_error( struct File_index * const fi, const char * const msg )
  {
  const int len = strlen( msg );
  void * tmp = resize_buffer( fi->error, fi->error_size + len + 1 );
  if( !tmp ) return false;
  fi->error = (char *)tmp;
  strncpy( fi->error + fi->error_size, msg, len + 1 );
  fi->error_size += len;
  return true;
  }


static bool push_back_member( struct File_index * const fi,
                              const long long dp, const long long ds,
                              const long long mp, const long long ms,
                              const unsigned dict_size )
  {
  struct Member * p;
  void * tmp = resize_buffer( fi->member_vector,
               ( fi->members + 1 ) * sizeof fi->member_vector[0] );
  if( !tmp )
    { add_error( fi, "Not enough memory." ); fi->retval = 1; return false; }
  fi->member_vector = (struct Member *)tmp;
  p = &(fi->member_vector[fi->members]);
  init_member( p, dp, ds, mp, ms, dict_size );
  ++fi->members;
  return true;
  }


static void Fi_free_member_vector( struct File_index * const fi )
  {
  if( fi->member_vector )
    { free( fi->member_vector ); fi->member_vector = 0; }
  fi->members = 0;
  }


static void Fi_reverse_member_vector( struct File_index * const fi )
  {
  struct Member tmp;
  long i;
  for( i = 0; i < fi->members / 2; ++i )
    {
    tmp = fi->member_vector[i];
    fi->member_vector[i] = fi->member_vector[fi->members-i-1];
    fi->member_vector[fi->members-i-1] = tmp;
    }
  }


static void Fi_set_errno_error( struct File_index * const fi,
                                const char * const msg )
  {
  add_error( fi, msg ); add_error( fi, strerror( errno ) );
  fi->retval = 1;
  }

static void Fi_set_num_error( struct File_index * const fi,
                              const char * const msg, unsigned long long num )
  {
  char buf[80];
  snprintf( buf, sizeof buf, "%s%llu", msg, num );
  add_error( fi, buf );
  fi->retval = 2;
  }


/* If successful, push last member and set pos to member header. */
static bool Fi_skip_trailing_data( struct File_index * const fi,
                                   const int fd, long long * const pos )
  {
  enum { block_size = 16384,
         buffer_size = block_size + Ft_size - 1 + Fh_size };
  uint8_t buffer[buffer_size];
  int bsize = *pos % block_size;		/* total bytes in buffer */
  int search_size, rd_size;
  unsigned long long ipos;
  int i;
  if( bsize <= buffer_size - block_size ) bsize += block_size;
  search_size = bsize;				/* bytes to search for trailer */
  rd_size = bsize;				/* bytes to read from file */
  ipos = *pos - rd_size;			/* aligned to block_size */
  if( *pos < min_member_size ) return false;

  while( true )
    {
    const uint8_t max_msb = ( ipos + search_size ) >> 56;
    if( seek_read( fd, buffer, rd_size, ipos ) != rd_size )
      { Fi_set_errno_error( fi, "Error seeking member trailer: " );
        return false; }
    for( i = search_size; i >= Ft_size; --i )
      if( buffer[i-1] <= max_msb )	/* most significant byte of member_size */
        {
        File_header header;
        File_trailer * trailer = (File_trailer *)( buffer + i - Ft_size );
        const unsigned long long member_size = Ft_get_member_size( *trailer );
        unsigned dictionary_size;
        if( member_size == 0 )
          { while( i > Ft_size && buffer[i-9] == 0 ) --i; continue; }
        if( member_size < min_member_size || member_size > ipos + i )
          continue;
        if( seek_read( fd, header, Fh_size,
                       ipos + i - member_size ) != Fh_size )
          { Fi_set_errno_error( fi, "Error reading member header: " );
            return false; }
        dictionary_size = Fh_get_dictionary_size( header );
        if( !Fh_verify_magic( header ) || !Fh_verify_version( header ) ||
            !isvalid_ds( dictionary_size ) ) continue;
        if( Fh_verify_prefix( buffer + i, bsize - i ) )
          {
          add_error( fi, "Last member in input file is truncated or corrupt." );
          fi->retval = 2; return false;
          }
        *pos = ipos + i - member_size;
        return push_back_member( fi, 0, Ft_get_data_size( *trailer ), *pos,
                                 member_size, dictionary_size );
        }
    if( ipos <= 0 )
      { Fi_set_num_error( fi, "Member size in trailer is corrupt at pos ",
                          *pos - 8 );
        return false; }
    bsize = buffer_size;
    search_size = bsize - Fh_size;
    rd_size = block_size;
    ipos -= rd_size;
    memcpy( buffer + rd_size, buffer, buffer_size - rd_size );
    }
  }


bool Fi_init( struct File_index * const fi, const int infd,
              const bool ignore_trailing )
  {
  File_header header;
  long long pos;
  long i;
  fi->member_vector = 0;
  fi->error = 0;
  fi->isize = lseek( infd, 0, SEEK_END );
  fi->members = 0;
  fi->error_size = 0;
  fi->retval = 0;
  if( fi->isize < 0 )
    { Fi_set_errno_error( fi, "Input file is not seekable: " ); return false; }
  if( fi->isize < min_member_size )
    { add_error( fi, "Input file is too short." ); fi->retval = 2;
      return false; }
  if( fi->isize > INT64_MAX )
    { add_error( fi, "Input file is too long (2^63 bytes or more)." );
      fi->retval = 2; return false; }

  if( seek_read( infd, header, Fh_size, 0 ) != Fh_size )
    { Fi_set_errno_error( fi, "Error reading member header: " ); return false; }
  if( !Fh_verify_magic( header ) )
    { add_error( fi, bad_magic_msg ); fi->retval = 2; return false; }
  if( !Fh_verify_version( header ) )
    { add_error( fi, bad_version( Fh_version( header ) ) ); fi->retval = 2;
      return false; }
  if( !isvalid_ds( Fh_get_dictionary_size( header ) ) )
    { add_error( fi, bad_dict_msg ); fi->retval = 2; return false; }

  pos = fi->isize;		/* always points to a header or to EOF */
  while( pos >= min_member_size )
    {
    File_trailer trailer;
    unsigned long long member_size;
    unsigned dictionary_size;
    if( seek_read( infd, trailer, Ft_size, pos - Ft_size ) != Ft_size )
      { Fi_set_errno_error( fi, "Error reading member trailer: " ); break; }
    member_size = Ft_get_member_size( trailer );
    if( member_size < min_member_size || member_size > (unsigned long long)pos )
      {
      if( fi->members > 0 )
        Fi_set_num_error( fi, "Member size in trailer is corrupt at pos ",
                          pos - 8 );
      else if( Fi_skip_trailing_data( fi, infd, &pos ) )
        { if( ignore_trailing ) continue;
          add_error( fi, trailing_msg ); fi->retval = 2; return false; }
      break;
      }
    if( seek_read( infd, header, Fh_size, pos - member_size ) != Fh_size )
      { Fi_set_errno_error( fi, "Error reading member header: " ); break; }
    dictionary_size = Fh_get_dictionary_size( header );
    if( !Fh_verify_magic( header ) || !Fh_verify_version( header ) ||
        !isvalid_ds( dictionary_size ) )
      {
      if( fi->members > 0 )
        Fi_set_num_error( fi, "Bad header at pos ", pos - member_size );
      else if( Fi_skip_trailing_data( fi, infd, &pos ) )
        { if( ignore_trailing ) continue;
          add_error( fi, trailing_msg ); fi->retval = 2; return false; }
      break;
      }
    pos -= member_size;
    if( !push_back_member( fi, 0, Ft_get_data_size( trailer ), pos,
                           member_size, dictionary_size ) )
      return false;
    }
  if( pos != 0 || fi->members <= 0 )
    {
    Fi_free_member_vector( fi );
    if( fi->retval == 0 )
      { add_error( fi, "Can't create file index." ); fi->retval = 2; }
    return false;
    }
  Fi_reverse_member_vector( fi );
  for( i = 0; i < fi->members - 1; ++i )
    {
    const long long end = block_end( fi->member_vector[i].dblock );
    if( end < 0 || end > INT64_MAX )
      {
      Fi_free_member_vector( fi );
      add_error( fi, "Data in input file is too long (2^63 bytes or more)." );
      fi->retval = 2; return false;
      }
    fi->member_vector[i+1].dblock.pos = end;
    }
  return true;
  }


void Fi_free( struct File_index * const fi )
  {
  Fi_free_member_vector( fi );
  if( fi->error ) { free( fi->error ); fi->error = 0; }
  fi->error_size = 0;
  }