Coverage Report

Created: 2026-11-13 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/main/dounzip.c
Line
Count
Source
1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  file dounzip.c
4
 *  first part  Copyright (C) 2002-2026 The R Core Team
5
 *  second part Copyright (C) 1998-2010 Gilles Vollant
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, a copy is available at
19
 *  https://www.R-project.org/Licenses/
20
 */
21
22
#ifdef HAVE_CONFIG_H
23
#include <config.h>
24
#endif
25
26
#define HAVE_BZIP2
27
28
#define R_USE_SIGNALS 1
29
#include <Defn.h>
30
#include <Fileio.h> /* for R_fopen */
31
#include "unzip.h"
32
#ifdef HAVE_SYS_STAT_H
33
# include <sys/stat.h>
34
#endif
35
#include <errno.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
#ifdef Win32
40
#include <io.h> /* for mkdir */
41
#endif
42
43
/* cf do_dircreate in platform.c */
44
static int R_mkdir(char *path)
45
0
{
46
#ifdef Win32
47
    char *local = (char *)malloc(strlen(path) + 1);
48
    if (!local) {
49
  errno = ENOMEM;
50
  return -1;
51
    }
52
    strcpy(local, path);
53
    /* needed DOS paths on Win 9x */
54
    R_fixbackslash(local);
55
    int res = mkdir(local);
56
    free(local);
57
    return res;
58
#endif
59
0
#ifdef Unix
60
0
    return mkdir(path, 0777);
61
0
#endif
62
0
}
63
64
#ifdef Win32
65
#include <windows.h>
66
static void setFileTime(const char *fn, uLong dosdate)
67
{
68
    HANDLE hFile;
69
    FILETIME ftm, ftLocal;
70
71
    hFile = CreateFileA(fn, GENERIC_READ | GENERIC_WRITE,
72
      0, NULL, OPEN_EXISTING, 0, NULL);
73
    if (hFile == INVALID_HANDLE_VALUE) return;
74
    DosDateTimeToFileTime((WORD)(dosdate >> 16), (WORD)dosdate, &ftLocal);
75
    LocalFileTimeToFileTime(&ftLocal, &ftm);
76
    SetFileTime(hFile, &ftm, NULL, &ftm);
77
    CloseHandle(hFile);
78
}
79
#else
80
# include <time.h>
81
# ifdef HAVE_UTIMES
82
#  include <sys/time.h>
83
# endif
84
# ifdef HAVE_UTIME_H
85
#  include <utime.h>
86
# endif
87
static void setFileTime(const char *fn, tm_unz tmu_date)
88
0
{
89
0
    struct tm dt;
90
0
    dt.tm_sec = tmu_date.tm_sec;
91
0
    dt.tm_min = tmu_date.tm_min;
92
0
    dt.tm_hour = tmu_date.tm_hour;
93
0
    dt.tm_mday = tmu_date.tm_mday;
94
0
    dt.tm_mon = tmu_date.tm_mon;
95
0
    if (tmu_date.tm_year > 1900)
96
0
  dt.tm_year = tmu_date.tm_year - 1900;
97
0
    else
98
0
  dt.tm_year = tmu_date.tm_year;
99
0
    dt.tm_isdst = -1;
100
0
    time_t ftime = mktime(&dt);
101
0
#if defined(HAVE_UTIMES)
102
0
    struct timeval times[2];
103
104
0
    times[0].tv_sec = times[1].tv_sec = ftime;
105
0
    times[0].tv_usec = times[1].tv_usec = 0;
106
0
    utimes(fn, times);
107
#elif defined(HAVE_UTIME)
108
    struct utimbuf settime;
109
    settime.actime = settime.modtime = ftime;
110
    utime(fn, &settime);
111
#endif
112
0
}
113
#endif
114
115
0
#define UNZ_UNSAFE_LINK -201
116
117
/* The caller chooses 'dest' itself.  Below it, refuse to create anything
118
   through a symbolic link, whether an intermediate component or a
119
   pre-existing final component (which fopen() would follow).  Must run on
120
   the path as it will be used, i.e. after "../" components are removed. */
121
static int isExtractLink(const char *path)
122
110
{
123
#ifdef Win32
124
    DWORD attrs = GetFileAttributesA(path);
125
    return attrs != INVALID_FILE_ATTRIBUTES &&
126
  (attrs & FILE_ATTRIBUTE_REPARSE_POINT);
127
#else
128
110
    struct stat st;
129
110
    return lstat(path, &st) == 0 && S_ISLNK(st.st_mode);
130
110
#endif
131
110
}
132
133
static int hasExtractLink(const char *dest, const char *outname)
134
110
{
135
110
    char path[R_PATH_MAX];
136
110
    size_t len = strlen(outname), base = strlen(dest) + 1;
137
110
    strcpy(path, outname); /* extract_one has already checked its length */
138
18.3k
    for (size_t i = base; i < len; i++) {
139
18.2k
  if (path[i] == '/') {
140
0
      path[i] = '\0';
141
0
      int linked = isExtractLink(path);
142
0
      path[i] = '/';
143
0
      if (linked) return 1;
144
0
  }
145
18.2k
    }
146
110
    return len > base && isExtractLink(path);
147
110
}
148
149
/* Remove "../" components below 'dest', as unzip 6.00 does, warning once. */
150
static void stripDotDot(const char *dest, char *outname, const char *fn)
151
124
{
152
124
    char *pp = outname + strlen(dest) + 1, *p;
153
124
    bool warned = FALSE;
154
125
    while ((p = Rf_strchr(pp, '/'))) {
155
1
  if (p - pp == 2 && !strncmp(pp, "..", 2)) {
156
0
      if (!warned) {
157
0
    warning(_("skipped \"../\" path component(s) in '%s'"), fn);
158
0
    warned = true;
159
0
      }
160
0
      memmove(pp, p + 1, strlen(p + 1) + 1);
161
1
  } else {
162
1
      pp = p + 1;
163
1
  }
164
1
    }
165
124
}
166
167
/* Create missing parents of 'outname' below 'dest' (which exists).
168
   Returns 0, or -1 with errno set and the failing directory in 'dirs'. */
169
static int makeParents(const char *dest, const char *outname, char *dirs)
170
110
{
171
110
    const char *pp = outname + strlen(dest) + 1, *p;
172
110
    while ((p = Rf_strchr_const(pp, '/'))) {
173
0
  strcpy(dirs, outname);
174
0
  dirs[p - outname] = '\0';
175
0
  if (!R_FileExists(dirs) && R_mkdir(dirs)) return -1;
176
0
  pp = p + 1;
177
0
    }
178
110
    return 0;
179
110
}
180
181
430
#define BUF_SIZE 4096
182
static int
183
extract_one(unzFile uf, const char *const dest, const char * const filename,
184
      SEXP names, int *nnames, int overwrite, int junk, int setTime)
185
140
{
186
140
    int err = UNZ_OK;
187
140
    FILE *fout;
188
140
    char  outname[R_PATH_MAX], dirs[R_PATH_MAX], buf[BUF_SIZE], *p;
189
140
    char *fn, fn0[R_PATH_MAX];
190
191
140
    err = unzOpenCurrentFile(uf);
192
140
    if (err != UNZ_OK) return err;
193
124
    if (strlen(dest) > R_PATH_MAX - 2) {
194
0
  unzCloseCurrentFile(uf);
195
0
  return 1;
196
0
    }
197
124
    strcpy(outname, dest);
198
124
    strcat(outname, FILESEP);
199
124
    unz_file_info64 file_info;
200
124
    char filename_inzip[R_PATH_MAX];
201
124
    err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip,
202
124
          sizeof(filename_inzip), NULL, 0, NULL, 0);
203
124
    fn = filename_inzip; /* might be UTF-8 ... */
204
124
    if (filename) {
205
0
  if (strlen(filename) > R_PATH_MAX - 1) {
206
0
      unzCloseCurrentFile(uf);
207
0
      return 1;
208
0
  }
209
0
  strcpy(fn0, filename);
210
0
  fn = fn0;
211
124
    } else if (err != UNZ_OK
212
124
               || file_info.size_filename >= sizeof(filename_inzip)) {
213
0
  unzCloseCurrentFile(uf);
214
0
  return 1;
215
0
    }
216
#ifdef Win32
217
    R_fixslash(fn);
218
#endif
219
124
    if (junk && strlen(fn) >= 2) { /* need / and basename */
220
115
  p = Rf_strrchr(fn, '/');
221
115
  if (p) fn = p+1;
222
115
    }
223
124
    if (strlen(outname) + strlen(fn) > R_PATH_MAX - 1) {
224
0
  unzCloseCurrentFile(uf);
225
0
  return 1;
226
0
    }
227
124
    strcat(outname, fn);
228
229
#ifdef Win32
230
    R_fixslash(outname); /* ensure path separator is / */
231
#endif
232
    /* Normalize before looking at the file system, so that the symlink
233
       check and the directory creation see the same path. */
234
124
    stripDotDot(dest, outname, fn);
235
124
    p = outname + strlen(outname) - 1;
236
124
    bool isdir = (*p == '/'); /* Directories are stored with trailing slash */
237
124
    if (isdir) {
238
14
  if (junk) { /* nothing to create */
239
14
      unzCloseCurrentFile(uf);
240
14
      return UNZ_OK;
241
14
  }
242
0
  *p = '\0';
243
0
    }
244
110
    if (hasExtractLink(dest, outname)) {
245
0
  warning(_("cannot extract '%s' through a symbolic link"), fn);
246
0
  unzCloseCurrentFile(uf);
247
0
  return UNZ_UNSAFE_LINK;
248
0
    }
249
    /* make parents as required: have already checked dest exists */
250
110
    if (makeParents(dest, outname, dirs)) {
251
0
  int serrno = errno;
252
0
  unzCloseCurrentFile(uf);
253
0
  error(_("cannot create directory '%s': %s"), dirs, strerror(serrno));
254
0
    }
255
110
    if (isdir) {
256
0
  if (!R_FileExists(outname) && R_mkdir(outname)) {
257
0
      int serrno = errno;
258
0
      unzCloseCurrentFile(uf);
259
0
      error(_("cannot create directory '%s': %s"), outname,
260
0
      strerror(serrno));
261
0
  }
262
110
    } else {
263
  /* Rprintf("extracting %s\n", outname); */
264
110
  if (!overwrite && R_FileExists(outname)) {
265
0
      warning(_(" not overwriting file '%s"), outname);
266
0
      unzCloseCurrentFile(uf);
267
0
      return err;
268
0
  }
269
110
  fout = R_fopen(outname, "wb");
270
110
  int serrno = errno;
271
110
  if (!fout) {
272
58
      unzCloseCurrentFile(uf);
273
58
      error(_("cannot open file '%s': %s"), outname, strerror(serrno));
274
58
      return 3;   /* not reached */
275
58
  }
276
228
  while (1) {
277
228
      err = unzReadCurrentFile(uf, buf, BUF_SIZE);
278
      /* Rprintf("read %d bytes\n", err); */
279
228
      if (err <= 0) break;
280
202
      if (fwrite(buf, err, 1, fout) != 1) { err = -200; break; }
281
202
      if (err < BUF_SIZE) { err = 0; break; }
282
202
  }
283
52
  fclose(fout);
284
52
  SET_STRING_ELT(names, (*nnames)++, mkChar(outname));
285
52
    }
286
52
    unzCloseCurrentFile(uf);
287
#ifdef Win32
288
    if (setTime) setFileTime(outname, file_info.dosDate);
289
#else
290
52
    if (setTime) setFileTime(outname, file_info.tmu_date);
291
52
#endif
292
52
    return err;
293
110
}
294
295
296
static void unz_cleanup(void *data)
297
351
{
298
351
    unzClose((unzFile) data);
299
351
}
300
301
static int
302
zipunzip(const char *zipname, const char *dest, int nfiles, const char **files,
303
   SEXP *pnames, int *nnames, int overwrite, int junk, int setTime)
304
113
{
305
113
    int   i, err = UNZ_OK;
306
113
    unzFile uf;
307
113
    SEXP names = *pnames;
308
113
    RCNTXT cntxt;
309
310
113
    uf = unzOpen64(zipname);
311
113
    if (!uf) return 1;
312
    /* extract_one() raises an R error when an output file cannot be opened.
313
       => make sure we unwind at unzClose() below and don't leak with this context: */
314
113
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
315
113
     R_NilValue, R_NilValue);
316
113
    cntxt.cend = &unz_cleanup;
317
113
    cntxt.cenddata = uf;
318
113
    if (nfiles == 0) { /* all files */
319
113
  unz_global_info64 gi;
320
113
  unzGetGlobalInfo64(uf, &gi);
321
113
  PROTECT(names = allocVector(STRSXP, 5000));
322
228
  for (i = 0; i < gi.number_entry; i++) {
323
140
      if (i > 0) if ((err = unzGoToNextFile(uf)) != UNZ_OK) break;
324
140
      if (*nnames+1 >= LENGTH(names)) {
325
0
    SEXP onames = names;
326
0
    names = allocVector(STRSXP, 2*LENGTH(names));
327
0
    UNPROTECT(1);
328
0
    PROTECT(names);
329
0
    copyVector(names, onames);
330
0
      }
331
140
      if ((err = extract_one(uf, dest, NULL, names, nnames,
332
140
           overwrite, junk, setTime)) != UNZ_OK) break;
333
#ifdef Win32
334
      R_ProcessEvents();
335
#else
336
115
      R_CheckUserInterrupt();
337
115
#endif
338
115
  }
339
113
    } else {
340
0
  PROTECT(names = allocVector(STRSXP, nfiles));
341
0
  for (i = 0; i < nfiles; i++) {
342
0
      if ((err = unzLocateFile(uf, files[i], 1)) != UNZ_OK) break;
343
0
      if ((err = extract_one(uf, dest, files[i], names, nnames,
344
0
           overwrite, junk, setTime)) != UNZ_OK) break;
345
#ifdef Win32
346
      R_ProcessEvents();
347
#else
348
0
      R_CheckUserInterrupt();
349
0
#endif
350
0
  }
351
0
    }
352
113
    *pnames = names;
353
113
    endcontext(&cntxt);
354
113
    unzClose(uf);
355
113
    UNPROTECT(1); /* names */
356
113
    return err;
357
113
}
358
359
static SEXP ziplist(const char *zipname)
360
2.33k
{
361
2.33k
    unzFile uf = unzOpen64(zipname);
362
2.33k
    if (!uf) error(_("zip file '%s' cannot be opened"), zipname);
363
    /* allocVector() below raises an R error when the entry count read
364
       from the central directory is not a usable length.
365
       => open context, also covering explicit errors here. */
366
2.33k
    RCNTXT cntxt;
367
2.33k
    begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
368
2.33k
     R_NilValue, R_NilValue);
369
2.33k
    cntxt.cend = &unz_cleanup;
370
2.33k
    cntxt.cenddata = uf;
371
372
2.33k
    unz_global_info64 gi;
373
2.33k
    gi.number_entry = 0; /* =Wall */
374
2.33k
    int err = unzGetGlobalInfo64 (uf, &gi);
375
2.33k
    if (err != UNZ_OK)
376
0
  error("error %d with zipfile in unzGetGlobalInfo", err);
377
2.33k
    int nfiles = (int) gi.number_entry;
378
    /* name, length, datetime */
379
2.33k
    SEXP ans = PROTECT(allocVector(VECSXP, 3)), names, lengths, dates;
380
2.33k
    SET_VECTOR_ELT(ans, 0, names   = allocVector(STRSXP, nfiles));
381
2.33k
    SET_VECTOR_ELT(ans, 1, lengths = allocVector(REALSXP, nfiles));
382
2.33k
    SET_VECTOR_ELT(ans, 2, dates   = allocVector(STRSXP, nfiles));
383
5.40k
    for (uLong i = 0; i < nfiles; i++) {
384
3.07k
  char filename_inzip[R_PATH_MAX], date[50];
385
3.07k
  unz_file_info64 file_info;
386
387
3.07k
  err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip,
388
3.07k
              sizeof(filename_inzip), NULL, 0, NULL, 0);
389
3.07k
  if (err != UNZ_OK ||
390
2.89k
      file_info.size_filename >= sizeof(filename_inzip))
391
183
      error("error %d with zipfile in unzGetCurrentFileInfo\n", err);
392
  /* In theory at least bit 11 of the flag tells us that the
393
     filename is in UTF-8, so FIXME */
394
3.07k
  SET_STRING_ELT(names, i, mkChar(filename_inzip));
395
3.07k
  REAL(lengths)[i] = (double) file_info.uncompressed_size;
396
3.07k
  snprintf(date, 50, "%d-%02d-%02d %02d:%02d",
397
3.07k
     file_info.tmu_date.tm_year,
398
3.07k
     file_info.tmu_date.tm_mon + 1,
399
3.07k
     file_info.tmu_date.tm_mday,
400
3.07k
     file_info.tmu_date.tm_hour,
401
3.07k
     file_info.tmu_date.tm_min);
402
3.07k
  SET_STRING_ELT(dates, i, mkChar(date));
403
404
3.07k
  if (i < nfiles - 1) {
405
1.53k
      err = unzGoToNextFile(uf);
406
1.53k
      if (err != UNZ_OK)
407
75
    error("error %d with zipfile in unzGoToNextFile\n", err);
408
1.53k
  }
409
3.07k
    }
410
2.33k
    endcontext(&cntxt);
411
2.33k
    unzClose(uf);
412
413
2.33k
    UNPROTECT(1);
414
2.33k
    return ans;
415
2.33k
}
416
417
/* called from a .External in package 'utils', so managing
418
   the R_alloc stack here is prudence */
419
SEXP Runzip(SEXP args)
420
2.44k
{
421
2.44k
    SEXP  fn, ans, names = R_NilValue;
422
2.44k
    char  zipname[R_PATH_MAX], dest[R_PATH_MAX];
423
2.44k
    const char *p, **topics = NULL;
424
2.44k
    int   i, ntopics, list, overwrite, junk, setTime, rc, nnames = 0;
425
2.44k
    const void *vmax = vmaxget();
426
427
2.44k
    if (!isString(CAR(args)) || LENGTH(CAR(args)) != 1)
428
0
  error(_("invalid zip name argument"));
429
2.44k
    p = R_ExpandFileName(translateCharFP(STRING_ELT(CAR(args), 0)));
430
2.44k
    if (strlen(p) > R_PATH_MAX - 1)
431
0
  error(_("zip path is too long"));
432
2.44k
    strcpy(zipname, p);
433
2.44k
    args = CDR(args);
434
2.44k
    fn = CAR(args);
435
2.44k
    ntopics = length(fn);
436
2.44k
    if (ntopics > 0) {
437
0
  if (!isString(fn))
438
0
      error(_("invalid '%s' argument"), "files");
439
0
  topics = (const char **) R_alloc(ntopics, sizeof(char *));
440
0
  for (i = 0; i < ntopics; i++)
441
0
      topics[i] = translateCharFP(STRING_ELT(fn, i));
442
0
    }
443
2.44k
    args = CDR(args);
444
2.44k
    if (!isString(CAR(args)) || LENGTH(CAR(args)) != 1)
445
0
  error(_("invalid '%s' argument"), "exdir");
446
2.44k
    p = R_ExpandFileName(translateCharFP(STRING_ELT(CAR(args), 0)));
447
2.44k
    if (strlen(p) > R_PATH_MAX - 1)
448
0
  error(_("'exdir' is too long"));
449
2.44k
    strcpy(dest, p);
450
2.44k
    if (!R_FileExists(dest))
451
0
  error(_("'exdir' does not exist"));
452
2.44k
    args = CDR(args);
453
2.44k
    list = asLogical(CAR(args));
454
2.44k
    if (list == NA_LOGICAL)
455
0
  error(_("invalid '%s' argument"), "list");
456
2.44k
    if (list) return(ziplist(zipname));
457
113
    args = CDR(args);
458
113
    overwrite = asLogical(CAR(args));
459
113
    if (overwrite == NA_LOGICAL)
460
0
  error(_("invalid '%s' argument"), "overwrite");
461
113
    args = CDR(args);
462
113
    junk = asLogical(CAR(args));
463
113
    if (junk == NA_LOGICAL)
464
0
  error(_("invalid '%s' argument"), "junkpaths");
465
113
    args = CDR(args);
466
113
    setTime = asLogical(CAR(args));
467
113
    if (setTime == NA_LOGICAL)
468
0
  error(_("invalid '%s' argument"), "setTime");
469
470
113
    rc = zipunzip(zipname, dest, ntopics, topics, &names, &nnames,
471
113
      overwrite, junk, setTime);
472
113
    PROTECT(names);
473
113
    if (rc != UNZ_OK)
474
25
  switch(rc) {
475
0
  case UNZ_END_OF_LIST_OF_FILE:
476
0
      warning(_("requested file not found in the zip file"));
477
0
      break;
478
16
  case UNZ_BADZIPFILE:
479
16
      warning(_("zip file is corrupt"));
480
16
      break;
481
0
  case UNZ_CRCERROR:
482
0
      warning(_("CRC error in zip file"));
483
0
      break;
484
0
  case UNZ_PARAMERROR:
485
0
  case UNZ_INTERNALERROR:
486
0
      warning("internal error in 'unz' code");
487
0
      break;
488
0
  case -200:
489
0
      warning(_("write error in extracting from zip file"));
490
0
      break;
491
0
  case UNZ_UNSAFE_LINK: /* already warned, naming the member */
492
0
      break;
493
9
  default:
494
9
      warning(_("error %d in extracting from zip file"), rc);
495
25
  }
496
113
    PROTECT(ans = ScalarInteger(rc));
497
113
    PROTECT(names = lengthgets(names, nnames));
498
113
    setAttrib(ans, install("extracted"), names);
499
113
    UNPROTECT(3); /* old names, ans, names */
500
113
    vmaxset(vmax);
501
113
    return ans;
502
113
}
503
504
/* ------------------- unz connections --------------------- */
505
506
#include <Rconnections.h>
507
508
typedef struct unzconn {
509
    void *uf;
510
} *Runzconn;
511
512
static Rboolean unz_open(Rconnection con)
513
499
{
514
499
    unzFile uf;
515
499
    char path[2*R_PATH_MAX], *p;
516
499
    const char *tmp;
517
499
    int mlen;
518
519
499
    if(con->mode[0] != 'r') {
520
0
  warning(_("unz connections can only be opened for reading"));
521
0
  return FALSE;
522
0
    }
523
499
    tmp = R_ExpandFileName(con->description);
524
499
    if (strlen(tmp) > R_PATH_MAX - 1) {
525
0
  warning(_("zip path is too long"));
526
0
  return FALSE;
527
0
    }
528
499
    strcpy(path, tmp);
529
499
    p = Rf_strrchr(path, ':');
530
499
    if(!p) {
531
0
  warning(_("invalid description of 'unz' connection"));
532
0
  return FALSE;
533
0
    }
534
499
    *p = '\0';
535
499
    uf = unzOpen64(path);
536
499
    if(!uf) {
537
26
  warning(_("cannot open zip file '%s'"), path);
538
26
  return FALSE;
539
26
    }
540
473
    if (unzLocateFile(uf, p+1, 1) != UNZ_OK) {
541
  /* close first: the warning is an error under options(warn = 2) */
542
5
  unzClose(uf);
543
5
  warning(_("cannot locate file '%s' in zip file '%s'"), p+1, path);
544
5
  return FALSE;
545
5
    }
546
468
    unzOpenCurrentFile(uf);
547
468
    ((Runzconn)(con->private))->uf = uf;
548
468
    con->isopen = TRUE;
549
468
    con->canwrite = FALSE;
550
468
    con->canread = TRUE;
551
468
    mlen = (int) strlen(con->mode);
552
468
    if(mlen >= 2 && con->mode[mlen - 1] == 'b') con->text = FALSE;
553
238
    else con->text = TRUE;
554
    /* set_iconv(); not yet */
555
468
    con->save = -1000;
556
468
    return TRUE;
557
473
}
558
559
static void unz_close(Rconnection con)
560
468
{
561
468
    unzFile uf = ((Runzconn)(con->private))->uf;
562
468
    unzCloseCurrentFile(uf);
563
468
    unzClose(uf);
564
468
    con->isopen = FALSE;
565
468
}
566
567
static int unz_fgetc_internal(Rconnection con)
568
7.28M
{
569
7.28M
    unzFile uf = ((Runzconn)(con->private))->uf;
570
7.28M
    char buf[1];
571
7.28M
    int err, p;
572
573
7.28M
    err = unzReadCurrentFile(uf, buf, 1);
574
7.28M
    p = buf[0] % 256;
575
7.28M
    return (err < 1) ? R_EOF : p;
576
7.28M
}
577
578
static size_t unz_read(void *ptr, size_t size, size_t nitems,
579
           Rconnection con)
580
287
{
581
287
    unzFile uf = ((Runzconn)(con->private))->uf;
582
287
    return unzReadCurrentFile(uf, ptr, (unsigned int)(size*nitems))/size;
583
287
}
584
585
NORET static int null_vfprintf(Rconnection con, const char *format, va_list ap)
586
0
{
587
0
    error(_("printing not enabled for this connection"));
588
0
}
589
590
NORET static size_t null_write(const void *ptr, size_t size, size_t nitems,
591
       Rconnection con)
592
0
{
593
0
    error(_("write not enabled for this connection"));
594
0
}
595
596
NORET static double null_seek(Rconnection con, double where, int origin, int rw)
597
0
{
598
0
    error(_("seek not enabled for this connection"));
599
0
}
600
601
static int null_fflush(Rconnection con)
602
0
{
603
0
    return 0;
604
0
}
605
606
attribute_hidden Rconnection
607
R_newunz(const char *description, const char *const mode)
608
499
{
609
499
    Rconnection new;
610
499
    new = (Rconnection) malloc(sizeof(struct Rconn));
611
499
    if(!new) error(_("allocation of 'unz' connection failed"));
612
499
    new->class = (char *) malloc(strlen("unz") + 1);
613
499
    if(!new->class) {
614
0
  free(new);
615
0
  error(_("allocation of 'unz' connection failed"));
616
0
  /* for Solaris 12.5 */ new = NULL;
617
0
    }
618
499
    strcpy(new->class, "unz");
619
499
    new->description = (char *) malloc(strlen(description) + 1);
620
499
    if(!new->description) {
621
0
  free(new->class); free(new);
622
0
  error(_("allocation of 'unz' connection failed"));
623
0
  /* for Solaris 12.5 */ new = NULL;
624
0
    }
625
499
    init_con(new, description, CE_NATIVE, mode);
626
627
499
    new->canseek = TRUE;
628
499
    new->open = &unz_open;
629
499
    new->close = &unz_close;
630
499
    new->vfprintf = &null_vfprintf;
631
499
    new->fgetc_internal = &unz_fgetc_internal;
632
499
    new->fgetc = &dummy_fgetc;
633
499
    new->seek = &null_seek;
634
499
    new->fflush = &null_fflush;
635
499
    new->read = &unz_read;
636
499
    new->write = &null_write;
637
499
    new->private = (void *) malloc(sizeof(struct unzconn));
638
499
    if(!new->private) {
639
0
  free(new->description); free(new->class); free(new);
640
0
  error(_("allocation of 'unz' connection failed"));
641
0
  /* for Solaris 12.5 */ new = NULL;
642
0
    }
643
499
    return new;
644
499
}
645
646
       /* =================== second part ====================== */
647
648
/* From minizip contribution to zlib 1.2.3, updated for 1.2.5 */
649
/* cherry-picked fix for PR18390 */
650
651
/* unzip.c -- IO for uncompress .zip files using zlib
652
   Version 1.01e, February 12th, 2005
653
654
   Copyright (C) 1998-2005 Gilles Vollant
655
656
   Read unzip.h for more info
657
*/
658
659
#include <stdio.h>
660
#include <stdlib.h>
661
#include <string.h>
662
#include "zlib.h"
663
#include "unzip.h"
664
/*
665
#ifdef HAVE_ERRNO_H
666
#include <errno.h>
667
#else
668
extern int errno;
669
#endif
670
*/
671
672
#define local static
673
674
#ifndef CASESENSITIVITYDEFAULT_NO
675
#  if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES)
676
#    define CASESENSITIVITYDEFAULT_NO
677
#  endif
678
#endif
679
680
681
#ifndef UNZ_BUFSIZE
682
260
#define UNZ_BUFSIZE (16384)
683
#endif
684
685
#ifndef UNZ_MAXFILENAMEINZIP
686
941
#define UNZ_MAXFILENAMEINZIP (256)
687
#endif
688
689
#ifndef ALLOC
690
8.34k
# define ALLOC(size) (malloc(size))
691
#endif
692
#ifndef TRYFREE
693
8.34k
# define TRYFREE(p) {if (p) free(p);}
694
#endif
695
696
1.57k
#define SIZECENTRALDIRITEM (0x2e)
697
950
#define SIZEZIPLOCALHEADER (0x1e)
698
699
700
static const char unz_copyright[] =
701
   " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
702
703
/* unz_file_info_interntal contain internal info about a file in zipfile*/
704
typedef struct unz_file_info64_internal_s
705
{
706
    ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
707
} unz_file_info64_internal;
708
709
710
/* file_in_zip_read_info_s contain internal information about a file in zipfile,
711
    when reading and decompress it */
712
typedef struct
713
{
714
    char  *read_buffer;         /* internal buffer for compressed data */
715
    z_stream stream;            /* zLib stream structure for inflate */
716
717
#ifdef HAVE_BZIP2
718
    bz_stream bstream;          /* bzLib stream structure for bziped */
719
#endif
720
721
    ZPOS64_T pos_in_zipfile;       /* position in byte on the zipfile, for fseek*/
722
    uLong stream_initialised;   /* flag set if stream structure is initialised*/
723
724
    ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
725
    uInt  size_local_extrafield;  /* size of the local extra field */
726
    ZPOS64_T pos_local_extrafield;   /* position in the local extra field in read*/
727
    ZPOS64_T total_out_64;
728
729
    uLong crc32;                /* crc32 of all data uncompressed */
730
    uLong crc32_wait;           /* crc32 we must obtain after decompress all */
731
    ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
732
    ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
733
    voidpf filestream;        /* io structure of the zipfile */
734
    uLong compression_method;   /* compression method (0 == store) */
735
    ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
736
    int   raw;
737
} file_in_zip64_read_info_s;
738
739
740
/* unz64_s contain internal information about the zipfile
741
*/
742
typedef struct
743
{
744
    int is64bitOpenFunction;
745
    voidpf filestream;        /* io structure of the zipfile */
746
    unz_global_info64 gi;       /* public global information */
747
    ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
748
    ZPOS64_T num_file;             /* number of the current file in the zipfile*/
749
    ZPOS64_T pos_in_central_dir;   /* pos of the current file in the central dir*/
750
    ZPOS64_T current_file_ok;      /* flag about the usability of the current file*/
751
    ZPOS64_T central_pos;          /* position of the beginning of the central dir*/
752
753
    ZPOS64_T size_central_dir;     /* size of the central directory  */
754
    ZPOS64_T offset_central_dir;   /* offset of start of central directory with
755
           respect to the starting disk number */
756
757
    unz_file_info64 cur_file_info; /* public info about the current file in zip*/
758
    unz_file_info64_internal cur_file_info_internal; /* private info about it*/
759
    file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
760
          file if we are decompressing it */
761
    int encrypted;
762
763
    int isZip64;
764
} unz64_s;
765
766
767
/* ===========================================================================
768
     Read a byte from a gz_stream; update next_in and avail_in. Return EOF
769
   for end of file.
770
   IN assertion: the stream s has been successfully opened for reading.
771
*/
772
773
local int unz64local_getByte(voidpf filestream, int *pi)
774
19.2M
{
775
19.2M
    unsigned char c;
776
19.2M
    int err = (int) fread_func(filestream, &c, 1);
777
19.2M
    if (err == 1)
778
497k
    {
779
497k
  *pi = (int)c;
780
497k
  return UNZ_OK;
781
497k
    }
782
18.7M
    else
783
18.7M
    {
784
18.7M
  if (ferror_func(filestream))
785
0
      return UNZ_ERRNO;
786
18.7M
  else
787
18.7M
      return UNZ_EOF;
788
18.7M
    }
789
19.2M
}
790
791
792
/* ===========================================================================
793
   Reads a long in LSB order from the given gz_stream. Sets
794
*/
795
local int unz64local_getShort (voidpf filestream, uLong *pX)
796
9.48M
{
797
9.48M
    uLong x ;
798
9.48M
    int i = 0;
799
9.48M
    int err;
800
801
9.48M
    err = unz64local_getByte(filestream,&i);
802
9.48M
    x = (uLong)i;
803
804
9.48M
    if (err == UNZ_OK)
805
9.48M
  err = unz64local_getByte(filestream,&i);
806
9.48M
    x |= ((uLong)i)<<8;
807
808
9.48M
    if (err == UNZ_OK)
809
9.48M
  *pX = x;
810
0
    else
811
0
  *pX = 0;
812
9.48M
    return err;
813
9.48M
}
814
815
local int unz64local_getLong (voidpf filestream, uLong *pX)
816
71.4k
{
817
71.4k
    uLong x ;
818
71.4k
    int i = 0;
819
71.4k
    int err;
820
821
71.4k
    err = unz64local_getByte(filestream, &i);
822
71.4k
    x = (uLong)i;
823
824
71.4k
    if (err == UNZ_OK)
825
71.4k
  err = unz64local_getByte(filestream, &i);
826
71.4k
    x |= ((uLong)i)<<8;
827
828
71.4k
    if (err == UNZ_OK)
829
71.4k
  err = unz64local_getByte(filestream, &i);
830
71.4k
    x |= ((uLong)i)<<16;
831
832
71.4k
    if (err == UNZ_OK)
833
71.4k
  err = unz64local_getByte(filestream, &i);
834
71.4k
    x += ((uLong)i)<<24;
835
836
71.4k
    if (err == UNZ_OK)
837
71.4k
  *pX = x;
838
0
    else
839
0
  *pX = 0;
840
71.4k
    return err;
841
71.4k
}
842
843
local int unz64local_getLong64 (voidpf filestream, ZPOS64_T *pX)
844
4.27k
{
845
4.27k
    ZPOS64_T x ;
846
4.27k
    int i = 0;
847
4.27k
    int err;
848
849
4.27k
    err = unz64local_getByte(filestream,&i);
850
4.27k
    x = (ZPOS64_T)i;
851
852
4.27k
    if (err == UNZ_OK)
853
4.27k
  err = unz64local_getByte(filestream,&i);
854
4.27k
    x |= ((ZPOS64_T)i) << 8;
855
856
4.27k
    if (err == UNZ_OK)
857
4.27k
  err = unz64local_getByte(filestream,&i);
858
4.27k
    x |= ((ZPOS64_T)i) << 16;
859
860
4.27k
    if (err == UNZ_OK)
861
4.27k
  err = unz64local_getByte(filestream,&i);
862
4.27k
    x |= ((ZPOS64_T)i) << 24;
863
864
4.27k
    if (err == UNZ_OK)
865
4.27k
  err = unz64local_getByte(filestream,&i);
866
4.27k
    x |= ((ZPOS64_T)i) << 32;
867
868
4.27k
    if (err == UNZ_OK)
869
4.27k
  err = unz64local_getByte(filestream,&i);
870
4.27k
    x |= ((ZPOS64_T)i) << 40;
871
872
4.27k
    if (err == UNZ_OK)
873
4.27k
  err = unz64local_getByte(filestream,&i);
874
4.27k
    x |= ((ZPOS64_T)i) << 48;
875
876
4.27k
    if (err == UNZ_OK)
877
4.27k
  err = unz64local_getByte(filestream,&i);
878
4.27k
    x |= ((ZPOS64_T)i) << 56;
879
880
4.27k
    if (err == UNZ_OK)
881
4.27k
  *pX = x;
882
0
    else
883
0
  *pX = 0;
884
4.27k
    return err;
885
4.27k
}
886
887
/* My own strcmpi / strcasecmp */
888
local int
889
strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
890
0
{
891
0
    for (;;)
892
0
    {
893
0
  char c1 = *(fileName1++);
894
0
  char c2 = *(fileName2++);
895
0
  if ((c1 >= 'a') && (c1 <= 'z'))
896
0
      c1 -= 0x20;
897
0
  if ((c2 >= 'a') && (c2 <= 'z'))
898
0
      c2 -= 0x20;
899
0
  if (c1 == '\0')
900
0
      return ((c2 == '\0') ? 0 : -1);
901
0
  if (c2 == '\0')
902
0
      return 1;
903
0
  if (c1 < c2)
904
0
      return -1;
905
0
  if (c1 > c2)
906
0
      return 1;
907
0
    }
908
0
}
909
910
911
#ifdef  CASESENSITIVITYDEFAULT_NO
912
#define CASESENSITIVITYDEFAULTVALUE 2
913
#else
914
0
#define CASESENSITIVITYDEFAULTVALUE 1
915
#endif
916
917
#ifndef STRCMPCASENOSENTIVEFUNCTION
918
0
#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
919
#endif
920
921
/*
922
   Compare two filename (fileName1,fileName2).
923
   If iCaseSenisivity = 1, comparison is case sensitivity (like strcmp)
924
   If iCaseSenisivity = 2, comparison is not case sensitivity (like strcmpi
925
                or strcasecmp)
926
   If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
927
  (like 1 on Unix, 2 on Windows)
928
929
*/
930
extern int ZEXPORT unzStringFileNameCompare (const char*  fileName1,
931
               const char*  fileName2,
932
               int iCaseSensitivity)
933
934
468
{
935
468
    if (iCaseSensitivity == 0)
936
0
  iCaseSensitivity = CASESENSITIVITYDEFAULTVALUE;
937
938
468
    if (iCaseSensitivity == 1)
939
468
  return strcmp(fileName1, fileName2);
940
941
0
    return STRCMPCASENOSENTIVEFUNCTION(fileName1, fileName2);
942
468
}
943
944
#ifndef BUFREADCOMMENT
945
23.2k
#define BUFREADCOMMENT (0x400)
946
#endif
947
948
/*
949
  Locate the Central directory of a zipfile (at the end, just before
950
    the global comment)
951
*/
952
local ZPOS64_T unz64local_SearchCentralDir(voidpf filestream)
953
2.39k
{
954
2.39k
    unsigned char* buf;
955
2.39k
    ZPOS64_T uSizeFile;
956
2.39k
    ZPOS64_T uBackRead;
957
2.39k
    ZPOS64_T uMaxBack = 0xffff; /* maximum size of global comment */
958
2.39k
    ZPOS64_T uPosFound = 0;
959
960
2.39k
    if (fseek_func(filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
961
0
  return 0;
962
963
964
2.39k
    uSizeFile = ftell_func(filestream);
965
966
2.39k
    if (uMaxBack>uSizeFile)
967
2.39k
  uMaxBack = uSizeFile;
968
969
2.39k
    buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
970
2.39k
    if (buf == NULL)
971
0
  return 0;
972
973
2.39k
    uBackRead = 4;
974
4.03k
    while (uBackRead < uMaxBack)
975
3.68k
    {
976
3.68k
  size_t uReadSize;
977
3.68k
  ZPOS64_T uReadPos ;
978
3.68k
  int i;
979
3.68k
  if (uBackRead+BUFREADCOMMENT > uMaxBack)
980
2.22k
      uBackRead = uMaxBack;
981
1.45k
  else
982
1.45k
      uBackRead += BUFREADCOMMENT;
983
3.68k
  uReadPos = uSizeFile - uBackRead ;
984
985
3.68k
  uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
986
2.34k
         (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
987
3.68k
  if (fseek_func(filestream, uReadPos, ZLIB_FILEFUNC_SEEK_SET) != 0)
988
0
      break;
989
990
3.68k
  if (fread_func(filestream, buf, uReadSize) != uReadSize)
991
0
      break;
992
993
1.50M
  for (i = (int)uReadSize-3; (i--) > 0;)
994
1.50M
      if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) &&
995
15.7k
    ((*(buf+i+2)) == 0x05) && ((*(buf+i+3)) == 0x06))
996
2.04k
      {
997
2.04k
    uPosFound = uReadPos+i;
998
2.04k
    break;
999
2.04k
      }
1000
1001
3.68k
  if (uPosFound != 0)
1002
2.04k
      break;
1003
3.68k
    }
1004
2.39k
    TRYFREE(buf);
1005
2.39k
    return uPosFound;
1006
2.39k
}
1007
1008
1009
/*
1010
  Locate the Central directory 64 of a zipfile (at the end, just before
1011
    the global comment)
1012
*/
1013
local ZPOS64_T
1014
unz64local_SearchCentralDir64(voidpf filestream)
1015
2.91k
{
1016
2.91k
    unsigned char* buf;
1017
2.91k
    ZPOS64_T uSizeFile;
1018
2.91k
    ZPOS64_T uBackRead;
1019
2.91k
    ZPOS64_T uMaxBack = 0xffff; /* maximum size of global comment */
1020
2.91k
    ZPOS64_T uPosFound = 0;
1021
2.91k
    uLong uL;
1022
2.91k
    ZPOS64_T relativeOffset;
1023
1024
2.91k
    if (fseek_func(filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
1025
0
  return 0;
1026
1027
1028
2.91k
    uSizeFile = ftell_func(filestream);
1029
1030
2.91k
    if (uMaxBack > uSizeFile)
1031
2.91k
  uMaxBack = uSizeFile;
1032
1033
2.91k
    buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
1034
2.91k
    if (buf == NULL)
1035
0
  return 0;
1036
1037
2.91k
    uBackRead = 4;
1038
6.73k
    while (uBackRead<uMaxBack)
1039
4.69k
    {
1040
4.69k
  size_t uReadSize;
1041
4.69k
  ZPOS64_T uReadPos;
1042
4.69k
  int i;
1043
4.69k
  if (uBackRead+BUFREADCOMMENT>uMaxBack)
1044
2.81k
      uBackRead = uMaxBack;
1045
1.87k
  else
1046
1.87k
      uBackRead += BUFREADCOMMENT;
1047
4.69k
  uReadPos = uSizeFile - uBackRead ;
1048
1049
4.69k
  uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
1050
2.87k
         (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
1051
4.69k
  if (fseek_func(filestream, uReadPos, ZLIB_FILEFUNC_SEEK_SET) != 0)
1052
0
      break;
1053
1054
4.69k
  if (fread_func(filestream, buf, uReadSize) != uReadSize)
1055
0
      break;
1056
1057
2.36M
  for (i = (int)uReadSize-3; (i--) > 0;)
1058
2.36M
      if (((*(buf+i)) == 0x50) && ((*(buf+i+1)) == 0x4b) &&
1059
30.5k
    ((*(buf+i+2)) == 0x06) && ((*(buf+i+3)) == 0x07))
1060
873
      {
1061
873
    uPosFound = uReadPos+i;
1062
873
    break;
1063
873
      }
1064
1065
4.69k
  if (uPosFound != 0)
1066
872
      break;
1067
4.69k
    }
1068
2.91k
    TRYFREE(buf);
1069
2.91k
    if (uPosFound == 0)
1070
2.04k
  return 0;
1071
1072
    /* Zip64 end of central directory locator */
1073
872
    if (fseek_func(filestream, uPosFound, ZLIB_FILEFUNC_SEEK_SET) != 0)
1074
0
  return 0;
1075
1076
    /* the signature, already checked */
1077
872
    if (unz64local_getLong(filestream, &uL) != UNZ_OK)
1078
0
  return 0;
1079
1080
    /* number of the disk with the start of the zip64 end of  central directory */
1081
872
    if (unz64local_getLong(filestream, &uL) != UNZ_OK)
1082
0
  return 0;
1083
872
    if (uL != 0)
1084
182
  return 0;
1085
1086
    /* relative offset of the zip64 end of central directory record */
1087
690
    if (unz64local_getLong64(filestream, &relativeOffset) != UNZ_OK)
1088
0
  return 0;
1089
1090
    /* total number of disks */
1091
690
    if (unz64local_getLong(filestream, &uL) != UNZ_OK)
1092
0
  return 0;
1093
690
    if (uL != 1)
1094
90
  return 0;
1095
1096
    /* Goto end of central directory record */
1097
600
    if (fseek_func(filestream, relativeOffset, ZLIB_FILEFUNC_SEEK_SET) != 0)
1098
4
  return 0;
1099
1100
     /* the signature */
1101
596
    if (unz64local_getLong(filestream, &uL) != UNZ_OK)
1102
0
  return 0;
1103
1104
596
    if (uL != 0x06064b50)
1105
70
  return 0;
1106
1107
526
    return relativeOffset;
1108
596
}
1109
1110
/*
1111
  Open a Zip file. path contain the full pathname (by example,
1112
     on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer
1113
     "zlib/zlib114.zip".
1114
     If the zipfile cannot be opened (file doesn't exist or in not valid), the
1115
       return value is NULL.
1116
     Else, the return value is a unzFile Handle, usable with other function
1117
       of this unzip package.
1118
*/
1119
local unzFile unzOpenInternal (const void *path, int is64bitOpenFunction)
1120
2.94k
{
1121
2.94k
    unz64_s us;
1122
2.94k
    unz64_s *s;
1123
2.94k
    ZPOS64_T central_pos;
1124
2.94k
    uLong   uL;
1125
1126
2.94k
    uLong number_disk;          /* number of the current dist, used for
1127
           spaning ZIP, unsupported, always 0*/
1128
2.94k
    uLong number_disk_with_CD;  /* number the disk with central dir, used
1129
           for spaning ZIP, unsupported, always 0*/
1130
2.94k
    ZPOS64_T number_entry_CD;      /* total number of entries in
1131
           the central dir
1132
           (same than number_entry on nospan) */
1133
1134
2.94k
    int err=UNZ_OK;
1135
1136
2.94k
    if (unz_copyright[0] != ' ')
1137
0
  return NULL;
1138
1139
2.94k
    us.is64bitOpenFunction = is64bitOpenFunction;
1140
1141
1142
1143
2.94k
    us.filestream = fopen_func(path,
1144
2.94k
             ZLIB_FILEFUNC_MODE_READ |
1145
2.94k
             ZLIB_FILEFUNC_MODE_EXISTING);
1146
2.94k
    if (us.filestream == NULL)
1147
26
  return NULL;
1148
1149
2.91k
    central_pos = unz64local_SearchCentralDir64(us.filestream);
1150
2.91k
    if (central_pos)
1151
526
    {
1152
526
  uLong uS;
1153
526
  ZPOS64_T uL64;
1154
1155
526
  us.isZip64 = 1;
1156
1157
526
  if (fseek_func(us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
1158
0
  err = UNZ_ERRNO;
1159
1160
  /* the signature, already checked */
1161
526
  if (unz64local_getLong(us.filestream, &uL) != UNZ_OK)
1162
0
      err = UNZ_ERRNO;
1163
1164
  /* size of zip64 end of central directory record */
1165
526
  if (unz64local_getLong64(us.filestream, &uL64) != UNZ_OK)
1166
0
      err = UNZ_ERRNO;
1167
1168
  /* version made by */
1169
526
  if (unz64local_getShort(us.filestream, &uS) != UNZ_OK)
1170
0
      err = UNZ_ERRNO;
1171
1172
  /* version needed to extract */
1173
526
  if (unz64local_getShort(us.filestream, &uS) != UNZ_OK)
1174
0
      err = UNZ_ERRNO;
1175
1176
  /* number of this disk */
1177
526
  if (unz64local_getLong(us.filestream, &number_disk) != UNZ_OK)
1178
0
      err = UNZ_ERRNO;
1179
1180
  /* number of the disk with the start of the central directory */
1181
526
  if (unz64local_getLong(us.filestream, &number_disk_with_CD) != UNZ_OK)
1182
0
      err = UNZ_ERRNO;
1183
1184
  /* total number of entries in the central directory on this disk */
1185
526
  if (unz64local_getLong64(us.filestream, &us.gi.number_entry) != UNZ_OK)
1186
0
      err = UNZ_ERRNO;
1187
1188
  /* total number of entries in the central directory */
1189
526
  if (unz64local_getLong64(us.filestream, &number_entry_CD) != UNZ_OK)
1190
0
      err = UNZ_ERRNO;
1191
1192
526
  if ((number_entry_CD != us.gi.number_entry) ||
1193
353
      (number_disk_with_CD != 0) ||
1194
329
      (number_disk != 0))
1195
228
      err = UNZ_BADZIPFILE;
1196
1197
  /* size of the central directory */
1198
526
  if (unz64local_getLong64(us.filestream, &us.size_central_dir) != UNZ_OK)
1199
0
      err = UNZ_ERRNO;
1200
1201
  /* offset of start of central directory with respect to the
1202
    starting disk number */
1203
526
  if (unz64local_getLong64(us.filestream, &us.offset_central_dir) != UNZ_OK)
1204
0
      err = UNZ_ERRNO;
1205
1206
526
  us.gi.size_comment = 0;
1207
526
    }
1208
2.39k
    else
1209
2.39k
    {
1210
2.39k
  central_pos = unz64local_SearchCentralDir(us.filestream);
1211
2.39k
  if (central_pos == 0)
1212
350
      err = UNZ_ERRNO;
1213
1214
2.39k
  us.isZip64 = 0;
1215
1216
2.39k
  if (fseek_func(us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
1217
0
      err = UNZ_ERRNO;
1218
1219
  /* the signature, already checked */
1220
2.39k
  if (unz64local_getLong(us.filestream, &uL) != UNZ_OK)
1221
0
      err = UNZ_ERRNO;
1222
1223
  /* number of this disk */
1224
2.39k
  if (unz64local_getShort(us.filestream, &number_disk) != UNZ_OK)
1225
0
      err = UNZ_ERRNO;
1226
1227
  /* number of the disk with the start of the central directory */
1228
2.39k
  if (unz64local_getShort(us.filestream, &number_disk_with_CD) != UNZ_OK)
1229
0
      err = UNZ_ERRNO;
1230
1231
  /* total number of entries in the central dir on this disk */
1232
2.39k
  if (unz64local_getShort(us.filestream, &uL) != UNZ_OK)
1233
0
      err = UNZ_ERRNO;
1234
2.39k
  us.gi.number_entry = uL;
1235
1236
  /* total number of entries in the central dir */
1237
2.39k
  if (unz64local_getShort(us.filestream ,&uL) != UNZ_OK)
1238
0
      err = UNZ_ERRNO;
1239
2.39k
  number_entry_CD = uL;
1240
1241
2.39k
  if ((number_entry_CD != us.gi.number_entry) ||
1242
2.19k
      (number_disk_with_CD != 0) ||
1243
2.15k
      (number_disk != 0))
1244
299
      err = UNZ_BADZIPFILE;
1245
1246
  /* size of the central directory */
1247
2.39k
  if (unz64local_getLong(us.filestream, &uL) != UNZ_OK)
1248
0
      err = UNZ_ERRNO;
1249
2.39k
  us.size_central_dir = uL;
1250
1251
  /* offset of start of central directory with respect to the
1252
      starting disk number */
1253
2.39k
  if (unz64local_getLong(us.filestream, &uL) != UNZ_OK)
1254
0
      err = UNZ_ERRNO;
1255
2.39k
  us.offset_central_dir = uL;
1256
1257
  /* zipfile comment length */
1258
2.39k
  if (unz64local_getShort(us.filestream, &us.gi.size_comment) != UNZ_OK)
1259
0
      err = UNZ_ERRNO;
1260
2.39k
    }
1261
1262
2.91k
    if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
1263
337
  (err == UNZ_OK))
1264
6
  err = UNZ_BADZIPFILE;
1265
1266
2.91k
    if (err != UNZ_OK)
1267
593
    {
1268
593
  fclose_func(us.filestream);
1269
593
  return NULL;
1270
593
    }
1271
1272
2.32k
    us.byte_before_the_zipfile = central_pos -
1273
2.32k
          (us.offset_central_dir+us.size_central_dir);
1274
2.32k
    us.central_pos = central_pos;
1275
2.32k
    us.pfile_in_zip_read = NULL;
1276
2.32k
    us.encrypted = 0;
1277
1278
1279
2.32k
    s=(unz64_s*)ALLOC(sizeof(unz64_s));
1280
2.32k
    if( s != NULL)
1281
2.32k
    {
1282
2.32k
  *s = us;
1283
2.32k
  unzGoToFirstFile((unzFile)s);
1284
2.32k
    }
1285
2.32k
    return (unzFile)s;
1286
2.91k
}
1287
1288
1289
#ifdef UNUSED
1290
extern unzFile ZEXPORT unzOpen (const char *path)
1291
{
1292
    return unzOpenInternal(path, 0);
1293
}
1294
#endif
1295
1296
extern unzFile ZEXPORT unzOpen64 (const void *path)
1297
2.94k
{
1298
2.94k
    return unzOpenInternal(path, 1);
1299
2.94k
}
1300
1301
/*
1302
  Close a ZipFile opened with unzipOpen.
1303
  If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
1304
    these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
1305
  return UNZ_OK if there is no problem. */
1306
extern int ZEXPORT unzClose (unzFile file)
1307
2.32k
{
1308
2.32k
    unz64_s* s;
1309
2.32k
    if (file == NULL)
1310
0
  return UNZ_PARAMERROR;
1311
2.32k
    s = (unz64_s*) file;
1312
1313
2.32k
    if (s->pfile_in_zip_read != NULL)
1314
0
  unzCloseCurrentFile(file);
1315
1316
2.32k
    fclose_func(s->filestream);
1317
2.32k
    TRYFREE(s);
1318
2.32k
    return UNZ_OK;
1319
2.32k
}
1320
1321
1322
/*
1323
  Write info about the ZipFile in the *pglobal_info structure.
1324
  No preparation of the structure is needed
1325
  return UNZ_OK if there is no problem. */
1326
extern int ZEXPORT
1327
unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info)
1328
1.85k
{
1329
1.85k
    unz64_s* s;
1330
1.85k
    if (file == NULL)
1331
0
  return UNZ_PARAMERROR;
1332
1.85k
    s = (unz64_s*)file;
1333
1.85k
    *pglobal_info = s->gi;
1334
1.85k
    return UNZ_OK;
1335
1.85k
}
1336
1337
/*
1338
   Translate date/time from Dos format to tm_unz (readable more easily)
1339
*/
1340
local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm)
1341
8.03k
{
1342
8.03k
    ZPOS64_T uDate;
1343
8.03k
    uDate = (ZPOS64_T)(ulDosDate>>16);
1344
8.03k
    ptm->tm_mday = (uInt)(uDate&0x1f) ;
1345
8.03k
    ptm->tm_mon =  (uInt)((((uDate)&0x1E0)/0x20)-1) ;
1346
8.03k
    ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
1347
1348
8.03k
    ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
1349
8.03k
    ptm->tm_min =  (uInt) ((ulDosDate&0x7E0)/0x20) ;
1350
8.03k
    ptm->tm_sec =  (uInt) (2*(ulDosDate&0x1f)) ;
1351
8.03k
}
1352
1353
/*
1354
  Get Info about the current file in the zipfile, with internal only info
1355
*/
1356
local int unz64local_GetCurrentFileInfoInternal (unzFile file,
1357
              unz_file_info64 *pfile_info,
1358
              unz_file_info64_internal
1359
              *pfile_info_internal,
1360
              char *szFileName,
1361
              uLong fileNameBufferSize,
1362
              void *extraField,
1363
              uLong extraFieldBufferSize,
1364
              char *szComment,
1365
              uLong commentBufferSize)
1366
8.03k
{
1367
8.03k
    unz64_s* s;
1368
8.03k
    unz_file_info64 file_info;
1369
8.03k
    unz_file_info64_internal file_info_internal;
1370
8.03k
    int err = UNZ_OK;
1371
8.03k
    uLong uMagic;
1372
8.03k
    long lSeek = 0;
1373
8.03k
    uLong uL;
1374
1375
8.03k
    if (file == NULL)
1376
0
  return UNZ_PARAMERROR;
1377
8.03k
    s = (unz64_s*)file;
1378
8.03k
    if (fseek_func(s->filestream,
1379
8.03k
       s->pos_in_central_dir+s->byte_before_the_zipfile,
1380
8.03k
       ZLIB_FILEFUNC_SEEK_SET) != 0)
1381
2
  err = UNZ_ERRNO;
1382
1383
1384
    /* we check the magic */
1385
8.03k
    if (err == UNZ_OK)
1386
8.03k
    {
1387
8.03k
  if (unz64local_getLong(s->filestream,&uMagic) != UNZ_OK)
1388
0
      err = UNZ_ERRNO;
1389
8.03k
  else if (uMagic != 0x02014b50)
1390
405
      err = UNZ_BADZIPFILE;
1391
8.03k
    }
1392
1393
8.03k
    if (unz64local_getShort(s->filestream,&file_info.version) != UNZ_OK)
1394
0
  err = UNZ_ERRNO;
1395
1396
8.03k
    if (unz64local_getShort(s->filestream,&file_info.version_needed) != UNZ_OK)
1397
0
  err = UNZ_ERRNO;
1398
1399
8.03k
    if (unz64local_getShort(s->filestream,&file_info.flag) != UNZ_OK)
1400
0
  err = UNZ_ERRNO;
1401
1402
8.03k
    if (unz64local_getShort(s->filestream,&file_info.compression_method) != UNZ_OK)
1403
0
  err = UNZ_ERRNO;
1404
1405
8.03k
    if (unz64local_getLong(s->filestream,&file_info.dosDate) != UNZ_OK)
1406
0
  err = UNZ_ERRNO;
1407
1408
8.03k
    unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
1409
1410
8.03k
    if (unz64local_getLong(s->filestream,&file_info.crc) != UNZ_OK)
1411
0
  err = UNZ_ERRNO;
1412
1413
8.03k
    if (unz64local_getLong(s->filestream,&uL) != UNZ_OK)
1414
0
  err = UNZ_ERRNO;
1415
8.03k
    file_info.compressed_size = uL;
1416
1417
8.03k
    if (unz64local_getLong(s->filestream,&uL) != UNZ_OK)
1418
0
  err = UNZ_ERRNO;
1419
8.03k
    file_info.uncompressed_size = uL;
1420
1421
8.03k
    if (unz64local_getShort(s->filestream,&file_info.size_filename) != UNZ_OK)
1422
0
  err = UNZ_ERRNO;
1423
1424
8.03k
    if (unz64local_getShort(s->filestream,&file_info.size_file_extra) != UNZ_OK)
1425
0
  err = UNZ_ERRNO;
1426
1427
8.03k
    if (unz64local_getShort(s->filestream,&file_info.size_file_comment) != UNZ_OK)
1428
0
  err = UNZ_ERRNO;
1429
1430
8.03k
    if (unz64local_getShort(s->filestream,&file_info.disk_num_start) != UNZ_OK)
1431
0
  err = UNZ_ERRNO;
1432
1433
8.03k
    if (unz64local_getShort(s->filestream,&file_info.internal_fa) != UNZ_OK)
1434
0
  err = UNZ_ERRNO;
1435
1436
8.03k
    if (unz64local_getLong(s->filestream,&file_info.external_fa) != UNZ_OK)
1437
0
  err = UNZ_ERRNO;
1438
1439
    // relative offset of local header
1440
8.03k
    if (unz64local_getLong(s->filestream,&uL) != UNZ_OK)
1441
0
  err = UNZ_ERRNO;
1442
8.03k
    file_info_internal.offset_curfile = uL;
1443
1444
8.03k
    lSeek += file_info.size_filename;
1445
8.03k
    if ((err == UNZ_OK) && (szFileName != NULL))
1446
3.55k
    {
1447
3.55k
  size_t uSizeRead ;
1448
3.55k
  if (file_info.size_filename<fileNameBufferSize)
1449
3.49k
  {
1450
3.49k
      *(szFileName+file_info.size_filename)='\0';
1451
3.49k
      uSizeRead = file_info.size_filename;
1452
3.49k
  }
1453
62
  else
1454
62
      uSizeRead = fileNameBufferSize;
1455
1456
3.55k
  if ((file_info.size_filename>0) && (fileNameBufferSize>0))
1457
3.35k
      if (fread_func(s->filestream, szFileName, uSizeRead) != uSizeRead)
1458
67
    err = UNZ_ERRNO;
1459
3.55k
  lSeek -= uSizeRead;
1460
3.55k
    }
1461
1462
    // Read extrafield
1463
8.03k
    if ((err == UNZ_OK) && (extraField != NULL))
1464
0
    {
1465
0
  size_t uSizeRead;
1466
0
  if (file_info.size_file_extra < extraFieldBufferSize)
1467
0
      uSizeRead = file_info.size_file_extra;
1468
0
  else
1469
0
      uSizeRead = extraFieldBufferSize;
1470
1471
0
  if (lSeek != 0)
1472
0
  {
1473
0
      if (fseek_func(s->filestream, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
1474
0
    lSeek = 0;
1475
0
      else
1476
0
    err = UNZ_ERRNO;
1477
0
  }
1478
1479
0
  if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
1480
0
      if (fread_func(s->filestream, extraField, (size_t) uSizeRead)
1481
0
    != uSizeRead) err = UNZ_ERRNO;
1482
1483
0
  lSeek += file_info.size_file_extra - (uLong)uSizeRead;
1484
0
    }
1485
8.03k
    else
1486
8.03k
  lSeek += file_info.size_file_extra;
1487
1488
1489
8.03k
    if ((err == UNZ_OK) && (file_info.size_file_extra != 0))
1490
2.28k
    {
1491
2.28k
  uLong acc = 0;
1492
1493
  // since lSeek now points to after the extra field we need to move back
1494
2.28k
  lSeek -= file_info.size_file_extra;
1495
1496
2.28k
  if (lSeek != 0)
1497
1.07k
  {
1498
1.07k
      if (fseek_func(s->filestream, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
1499
1.07k
    lSeek = 0;
1500
0
      else
1501
0
    err = UNZ_ERRNO;
1502
1.07k
  }
1503
1504
4.69M
  while(acc < file_info.size_file_extra)
1505
4.69M
  {
1506
4.69M
      uLong headerId;
1507
4.69M
      uLong dataSize;
1508
1509
4.69M
      if (unz64local_getShort(s->filestream,&headerId) != UNZ_OK)
1510
0
    err = UNZ_ERRNO;
1511
1512
4.69M
      if (unz64local_getShort(s->filestream,&dataSize) != UNZ_OK)
1513
0
    err = UNZ_ERRNO;
1514
1515
      /* ZIP64 extra fields */
1516
4.69M
      if (headerId == 0x0001)
1517
1.56k
      {
1518
1.56k
    uLong uL;
1519
1520
1.56k
    if(file_info.uncompressed_size == MAXU32)
1521
385
    {
1522
385
        if (unz64local_getLong64(s->filestream,
1523
385
               &file_info.uncompressed_size) != UNZ_OK)
1524
0
      err = UNZ_ERRNO;
1525
385
    }
1526
1527
1.56k
    if(file_info.compressed_size == MAXU32)
1528
327
    {
1529
327
        if (unz64local_getLong64(s->filestream,
1530
327
               &file_info.compressed_size) != UNZ_OK)
1531
0
      err = UNZ_ERRNO;
1532
327
    }
1533
1534
1.56k
    if(file_info_internal.offset_curfile == MAXU32)
1535
238
    {
1536
        /* Relative Header offset */
1537
238
        if (unz64local_getLong64(s->filestream,
1538
238
               &file_info_internal.offset_curfile) != UNZ_OK)
1539
0
      err = UNZ_ERRNO;
1540
238
    }
1541
1542
1.56k
    if(file_info.disk_num_start == 0xffff)
1543
        /* minizip checks against MAXU32, but the specification
1544
           says 0xffff, see also PR18390 */
1545
470
    {
1546
        /* Disk Start Number */
1547
470
        if (unz64local_getLong(s->filestream,&uL) != UNZ_OK)
1548
0
      err = UNZ_ERRNO;
1549
470
    }
1550
1551
1.56k
      }
1552
4.69M
      else
1553
4.69M
      {
1554
4.69M
    if (fseek_func(s->filestream, dataSize, ZLIB_FILEFUNC_SEEK_CUR) != 0)
1555
0
        err = UNZ_ERRNO;
1556
4.69M
      }
1557
1558
4.69M
      acc += 2 + 2 + dataSize;
1559
4.69M
  }
1560
2.28k
    }
1561
1562
8.03k
    if ((err == UNZ_OK) && (szComment != NULL))
1563
0
    {
1564
0
  size_t uSizeRead ;
1565
0
  if (file_info.size_file_comment<commentBufferSize)
1566
0
  {
1567
0
      *(szComment+file_info.size_file_comment) = '\0';
1568
0
      uSizeRead = file_info.size_file_comment;
1569
0
  }
1570
0
  else
1571
0
      uSizeRead = commentBufferSize;
1572
1573
0
  if (lSeek != 0)
1574
0
  {
1575
0
      if (fseek_func(s->filestream, lSeek, ZLIB_FILEFUNC_SEEK_CUR) ==  0)
1576
0
    lSeek = 0;
1577
0
      else
1578
0
    err = UNZ_ERRNO;
1579
0
  }
1580
1581
0
  if ((file_info.size_file_comment>0) && (commentBufferSize>0))
1582
0
      if (fread_func(s->filestream,szComment,uSizeRead) != uSizeRead)
1583
0
    err = UNZ_ERRNO;
1584
0
  lSeek += file_info.size_file_comment - uSizeRead;
1585
0
    }
1586
8.03k
    else
1587
8.03k
  lSeek += file_info.size_file_comment;
1588
1589
1590
8.03k
    if ((err == UNZ_OK) && (pfile_info != NULL))
1591
7.09k
  *pfile_info = file_info;
1592
1593
8.03k
    if ((err == UNZ_OK) && (pfile_info_internal != NULL))
1594
4.07k
  *pfile_info_internal = file_info_internal;
1595
1596
8.03k
    return err;
1597
8.03k
}
1598
1599
1600
1601
/*
1602
  Write info about the ZipFile in the *pglobal_info structure.
1603
  No preparation of the structure is needed
1604
  return UNZ_OK if there is no problem.
1605
*/
1606
extern int ZEXPORT
1607
unzGetCurrentFileInfo64 (unzFile file,
1608
       unz_file_info64 * pfile_info,
1609
       char * szFileName, uLong fileNameBufferSize,
1610
       void *extraField, uLong extraFieldBufferSize,
1611
       char* szComment,  uLong commentBufferSize)
1612
3.66k
{
1613
3.66k
    return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
1614
3.66k
            szFileName,fileNameBufferSize,
1615
3.66k
            extraField,extraFieldBufferSize,
1616
3.66k
            szComment,commentBufferSize);
1617
3.66k
}
1618
1619
/*
1620
  Set the current file of the zipfile to the first file.
1621
  return UNZ_OK if there is no problem
1622
*/
1623
extern int ZEXPORT unzGoToFirstFile (unzFile file)
1624
2.79k
{
1625
2.79k
    int err = UNZ_OK;
1626
2.79k
    unz64_s* s;
1627
2.79k
    if (file == NULL)
1628
0
  return UNZ_PARAMERROR;
1629
2.79k
    s = (unz64_s*)file;
1630
2.79k
    s->pos_in_central_dir = s->offset_central_dir;
1631
2.79k
    s->num_file = 0;
1632
2.79k
    err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
1633
2.79k
            &s->cur_file_info_internal,
1634
2.79k
            NULL,0,NULL,0,NULL,0);
1635
2.79k
    s->current_file_ok = (err == UNZ_OK);
1636
2.79k
    return err;
1637
2.79k
}
1638
1639
/*
1640
  Set the current file of the zipfile to the next file.
1641
  return UNZ_OK if there is no problem
1642
  return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
1643
*/
1644
extern int ZEXPORT unzGoToNextFile (unzFile  file)
1645
1.57k
{
1646
1.57k
    unz64_s* s;
1647
1.57k
    int err;
1648
1649
1.57k
    if (file == NULL)
1650
0
  return UNZ_PARAMERROR;
1651
1.57k
    s = (unz64_s*)file;
1652
1.57k
    if (!s->current_file_ok)
1653
0
  return UNZ_END_OF_LIST_OF_FILE;
1654
1.57k
    if (s->gi.number_entry != 0xffff)    /* 2^16 files overflow hack */
1655
1.47k
      if (s->num_file+1 == s->gi.number_entry)
1656
0
  return UNZ_END_OF_LIST_OF_FILE;
1657
1658
1.57k
    s->pos_in_central_dir +=
1659
1.57k
  SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
1660
1.57k
  s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
1661
1.57k
    s->num_file++;
1662
1.57k
    err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
1663
1.57k
                 &s->cur_file_info_internal,
1664
1.57k
                 NULL,0,NULL,0,NULL,0);
1665
1.57k
    s->current_file_ok = (err == UNZ_OK);
1666
1.57k
    return err;
1667
1.57k
}
1668
1669
1670
/*
1671
  Try locate the file szFileName in the zipfile.
1672
  For the iCaseSensitivity signification, see unzipStringFileNameCompare
1673
1674
  return value :
1675
  UNZ_OK if the file is found. It becomes the current file.
1676
  UNZ_END_OF_LIST_OF_FILE if the file is not found
1677
*/
1678
extern int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
1679
473
{
1680
473
    unz64_s* s;
1681
473
    int err;
1682
1683
    /* We remember the 'current' position in the file so that we can jump
1684
     * back there if we fail.
1685
     */
1686
473
    unz_file_info64 cur_file_infoSaved;
1687
473
    unz_file_info64_internal cur_file_info_internalSaved;
1688
473
    ZPOS64_T num_fileSaved;
1689
473
    ZPOS64_T pos_in_central_dirSaved;
1690
1691
1692
473
    if (file == NULL)
1693
0
  return UNZ_PARAMERROR;
1694
1695
473
    if (strlen(szFileName) >= UNZ_MAXFILENAMEINZIP)
1696
5
  return UNZ_PARAMERROR;
1697
1698
468
    s = (unz64_s*)file;
1699
468
    if (!s->current_file_ok)
1700
0
  return UNZ_END_OF_LIST_OF_FILE;
1701
1702
    /* Save the current state */
1703
468
    num_fileSaved = s->num_file;
1704
468
    pos_in_central_dirSaved = s->pos_in_central_dir;
1705
468
    cur_file_infoSaved = s->cur_file_info;
1706
468
    cur_file_info_internalSaved = s->cur_file_info_internal;
1707
1708
468
    err = unzGoToFirstFile(file);
1709
1710
468
    while (err == UNZ_OK)
1711
468
    {
1712
468
  char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
1713
468
  szCurrentFileName[UNZ_MAXFILENAMEINZIP] = '\0';
1714
468
  err = unzGetCurrentFileInfo64(file, NULL,
1715
468
              szCurrentFileName,
1716
468
              sizeof(szCurrentFileName)-1,
1717
468
              NULL, 0, NULL, 0);
1718
468
  if (err == UNZ_OK)
1719
468
  {
1720
468
      if (unzStringFileNameCompare(szCurrentFileName,
1721
468
           szFileName,iCaseSensitivity) == 0)
1722
468
    return UNZ_OK;
1723
0
      err = unzGoToNextFile(file);
1724
0
  }
1725
468
    }
1726
1727
    /* We failed, so restore the state of the 'current file' to where we
1728
     * were.
1729
     */
1730
0
    s->num_file = num_fileSaved ;
1731
0
    s->pos_in_central_dir = pos_in_central_dirSaved ;
1732
0
    s->cur_file_info = cur_file_infoSaved;
1733
0
    s->cur_file_info_internal = cur_file_info_internalSaved;
1734
0
    return err;
1735
468
}
1736
1737
1738
/*
1739
///////////////////////////////////////////
1740
// Contributed by Ryan Haksi (mailto://cryogen@infoserve.net)
1741
// I need random access
1742
//
1743
// Further optimization could be realized by adding an ability
1744
// to cache the directory in memory. The goal being a single
1745
// comprehensive file read to put the file I need in a memory.
1746
*/
1747
1748
1749
/*
1750
// Unzip Helper Functions - should be here?
1751
///////////////////////////////////////////
1752
*/
1753
1754
/*
1755
  Read the local header of the current zipfile
1756
  Check the coherency of the local header and info in the end of central
1757
  directory about this file
1758
  store in *piSizeVar the size of extra info in local header
1759
  (filename and size of extra field data)
1760
*/
1761
local int
1762
unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar,
1763
              ZPOS64_T * poffset_local_extrafield,
1764
              uInt  * psize_local_extrafield)
1765
608
{
1766
608
    uLong uMagic,uData,uFlags;
1767
608
    uLong size_filename;
1768
608
    uLong size_extra_field;
1769
608
    int err = UNZ_OK;
1770
1771
608
    *piSizeVar = 0;
1772
608
    *poffset_local_extrafield = 0;
1773
608
    *psize_local_extrafield = 0;
1774
1775
608
    if (fseek_func(s->filestream,s->cur_file_info_internal.offset_curfile +
1776
608
       s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
1777
12
  return UNZ_ERRNO;
1778
1779
1780
596
    if (err == UNZ_OK)
1781
596
    {
1782
596
  if (unz64local_getLong(s->filestream,&uMagic) != UNZ_OK)
1783
0
      err = UNZ_ERRNO;
1784
596
  else if (uMagic!=0x04034b50)
1785
151
      err = UNZ_BADZIPFILE;
1786
596
    }
1787
1788
596
    if (unz64local_getShort(s->filestream,&uData) != UNZ_OK)
1789
0
  err = UNZ_ERRNO;
1790
/*
1791
    else if ((err == UNZ_OK) && (uData!=s->cur_file_info.wVersion))
1792
  err=UNZ_BADZIPFILE;
1793
*/
1794
596
    if (unz64local_getShort(s->filestream,&uFlags)  !=  UNZ_OK)
1795
0
  err = UNZ_ERRNO;
1796
1797
596
    if (unz64local_getShort(s->filestream,&uData) != UNZ_OK)
1798
0
  err = UNZ_ERRNO;
1799
596
    else if ((err == UNZ_OK) && (uData != s->cur_file_info.compression_method))
1800
21
  err = UNZ_BADZIPFILE;
1801
1802
596
    if ((err == UNZ_OK) && (s->cur_file_info.compression_method != 0) &&
1803
/* #ifdef HAVE_BZIP2 */
1804
233
       (s->cur_file_info.compression_method != Z_BZIP2ED) &&
1805
/* #endif */
1806
146
       (s->cur_file_info.compression_method != Z_DEFLATED))
1807
4
  err = UNZ_BADZIPFILE;
1808
1809
596
    if (unz64local_getLong(s->filestream,&uData) != UNZ_OK) /* date/time */
1810
0
  err = UNZ_ERRNO;
1811
1812
596
    if (unz64local_getLong(s->filestream,&uData) != UNZ_OK) /* crc */
1813
0
  err = UNZ_ERRNO;
1814
596
    else if ((err == UNZ_OK) && (uData != s->cur_file_info.crc) &&
1815
205
       ((uFlags & 8) == 0))
1816
15
  err = UNZ_BADZIPFILE;
1817
1818
596
    if (unz64local_getLong(s->filestream,&uData) != UNZ_OK) /* size compr */
1819
0
  err = UNZ_ERRNO;
1820
596
    else if (uData != 0xFFFFFFFF && (err == UNZ_OK) &&
1821
399
       (uData != s->cur_file_info.compressed_size) && ((uFlags & 8) == 0))
1822
6
  err = UNZ_BADZIPFILE;
1823
1824
596
    if (unz64local_getLong(s->filestream,&uData) != UNZ_OK) /* size uncompr */
1825
0
  err = UNZ_ERRNO;
1826
596
    else if (uData != 0xFFFFFFFF && (err == UNZ_OK) &&
1827
378
       (uData != s->cur_file_info.uncompressed_size) && ((uFlags & 8) == 0))
1828
7
  err = UNZ_BADZIPFILE;
1829
1830
596
    if (unz64local_getShort(s->filestream,&size_filename) != UNZ_OK)
1831
0
  err = UNZ_ERRNO;
1832
596
    else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename))
1833
38
  err = UNZ_BADZIPFILE;
1834
1835
596
    *piSizeVar += (uInt)size_filename;
1836
1837
596
    if (unz64local_getShort(s->filestream,&size_extra_field) != UNZ_OK)
1838
0
  err=UNZ_ERRNO;
1839
596
    *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
1840
596
            SIZEZIPLOCALHEADER + size_filename;
1841
596
    *psize_local_extrafield = (uInt)size_extra_field;
1842
1843
596
    *piSizeVar += (uInt)size_extra_field;
1844
1845
596
    return err;
1846
608
}
1847
1848
/*
1849
  Open for reading data the current file in the zipfile.
1850
  If there is no error and the file is opened, the return value is UNZ_OK.
1851
*/
1852
static int unzOpenCurrentFile3 (unzFile file, int* method,
1853
        int* level, int raw, const char* password)
1854
608
{
1855
608
    int err = UNZ_OK;
1856
608
    uInt iSizeVar;
1857
608
    unz64_s* s;
1858
608
    file_in_zip64_read_info_s* pfile_in_zip_read_info;
1859
608
    ZPOS64_T offset_local_extrafield;  /* offset of the local extra field */
1860
608
    uInt  size_local_extrafield;    /* size of the local extra field */
1861
608
    if (password != NULL) return UNZ_PARAMERROR;
1862
1863
608
    if (file == NULL)
1864
0
  return UNZ_PARAMERROR;
1865
608
    s = (unz64_s*)file;
1866
608
    if (!s->current_file_ok)
1867
0
  return UNZ_PARAMERROR;
1868
1869
608
    if (s->pfile_in_zip_read != NULL)
1870
0
  unzCloseCurrentFile(file);
1871
1872
608
    if (unz64local_CheckCurrentFileCoherencyHeader(s, &iSizeVar,
1873
608
               &offset_local_extrafield,&size_local_extrafield) != UNZ_OK)
1874
254
  return UNZ_BADZIPFILE;
1875
1876
354
    pfile_in_zip_read_info =
1877
354
  (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
1878
354
    if (pfile_in_zip_read_info == NULL)
1879
0
  return UNZ_INTERNALERROR;
1880
1881
354
    pfile_in_zip_read_info->read_buffer = (char*)ALLOC(UNZ_BUFSIZE);
1882
354
    pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1883
354
    pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1884
354
    pfile_in_zip_read_info->pos_local_extrafield = 0;
1885
354
    pfile_in_zip_read_info->raw = raw;
1886
1887
354
    if (pfile_in_zip_read_info->read_buffer == NULL)
1888
0
    {
1889
0
  TRYFREE(pfile_in_zip_read_info);
1890
0
  return UNZ_INTERNALERROR;
1891
0
    }
1892
1893
354
    pfile_in_zip_read_info->stream_initialised = 0;
1894
1895
354
    if (method != NULL)
1896
0
  *method = (int)s->cur_file_info.compression_method;
1897
1898
354
    if (level != NULL)
1899
0
    {
1900
0
  *level = 6;
1901
0
  switch (s->cur_file_info.flag & 0x06)
1902
0
  {
1903
0
    case 6 : *level = 1; break;
1904
0
    case 4 : *level = 2; break;
1905
0
    case 2 : *level = 9; break;
1906
0
  }
1907
0
    }
1908
1909
354
    if ((s->cur_file_info.compression_method != 0) &&
1910
/* #ifdef HAVE_BZIP2 */
1911
217
  (s->cur_file_info.compression_method != Z_BZIP2ED) &&
1912
/* #endif */
1913
131
  (s->cur_file_info.compression_method != Z_DEFLATED))
1914
1915
0
  err=UNZ_BADZIPFILE;
1916
1917
354
    pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc;
1918
354
    pfile_in_zip_read_info->crc32 = 0;
1919
354
    pfile_in_zip_read_info->total_out_64 = 0;
1920
354
    pfile_in_zip_read_info->compression_method =
1921
354
  s->cur_file_info.compression_method;
1922
354
    pfile_in_zip_read_info->filestream = s->filestream;
1923
354
    pfile_in_zip_read_info->byte_before_the_zipfile =
1924
354
  s->byte_before_the_zipfile;
1925
1926
354
    pfile_in_zip_read_info->stream.total_out = 0;
1927
1928
354
    if ((s->cur_file_info.compression_method == Z_BZIP2ED) && (!raw))
1929
86
    {
1930
86
#ifdef HAVE_BZIP2
1931
86
      pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
1932
86
      pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
1933
86
      pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
1934
86
      pfile_in_zip_read_info->bstream.state = (voidpf)0;
1935
1936
86
      pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1937
86
      pfile_in_zip_read_info->stream.zfree = (free_func)0;
1938
86
      pfile_in_zip_read_info->stream.opaque = (voidpf)0;
1939
86
      pfile_in_zip_read_info->stream.next_in = (voidpf)0;
1940
86
      pfile_in_zip_read_info->stream.avail_in = 0;
1941
1942
86
      err = BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
1943
86
      if (err == Z_OK)
1944
86
  pfile_in_zip_read_info->stream_initialised = Z_BZIP2ED;
1945
0
      else
1946
0
      {
1947
0
  TRYFREE(pfile_in_zip_read_info);
1948
0
  return err;
1949
0
      }
1950
#else
1951
      pfile_in_zip_read_info->raw = 1;
1952
#endif
1953
86
    }
1954
268
    else if ((s->cur_file_info.compression_method == Z_DEFLATED) && (!raw))
1955
131
    {
1956
131
      pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1957
131
      pfile_in_zip_read_info->stream.zfree = (free_func)0;
1958
131
      pfile_in_zip_read_info->stream.opaque = (voidpf)0;
1959
131
      pfile_in_zip_read_info->stream.next_in = 0;
1960
131
      pfile_in_zip_read_info->stream.avail_in = 0;
1961
1962
131
      err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1963
131
      if (err == Z_OK)
1964
131
  pfile_in_zip_read_info->stream_initialised = Z_DEFLATED;
1965
0
      else
1966
0
      {
1967
0
  TRYFREE(pfile_in_zip_read_info);
1968
0
  return err;
1969
0
      }
1970
  /* windowBits is passed < 0 to tell that there is no zlib header.
1971
   * Note that in this case inflate *requires* an extra "dummy" byte
1972
   * after the compressed stream in order to complete decompression and
1973
   * return Z_STREAM_END.
1974
   * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1975
   * size of both compressed and uncompressed data
1976
   */
1977
131
    }
1978
354
    pfile_in_zip_read_info->rest_read_compressed =
1979
354
      s->cur_file_info.compressed_size ;
1980
354
    pfile_in_zip_read_info->rest_read_uncompressed =
1981
354
      s->cur_file_info.uncompressed_size ;
1982
1983
1984
354
    pfile_in_zip_read_info->pos_in_zipfile =
1985
354
      s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
1986
354
        iSizeVar;
1987
1988
354
    pfile_in_zip_read_info->stream.avail_in = (uInt)0;
1989
1990
354
    s->pfile_in_zip_read = pfile_in_zip_read_info;
1991
354
    s->encrypted = 0;
1992
1993
354
    return UNZ_OK;
1994
354
}
1995
1996
static int unzOpenCurrentFile (unzFile file)
1997
608
{
1998
608
    return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
1999
608
}
2000
2001
/*
2002
  Read bytes from the current file.
2003
  buf contain buffer where data must be copied
2004
  len the size of buf.
2005
2006
  return the number of byte copied if somes bytes are copied
2007
  return 0 if the end of file was reached
2008
  return <0 with error code if there is an error
2009
    (UNZ_ERRNO for IO error, or zLib error for uncompress error)
2010
*/
2011
static int unzReadCurrentFile  (unzFile file, voidp buf, unsigned len)
2012
7.28M
{
2013
7.28M
    int err = UNZ_OK;
2014
7.28M
    uInt iRead = 0;
2015
7.28M
    unz64_s* s;
2016
7.28M
    file_in_zip64_read_info_s* pfile_in_zip_read_info;
2017
7.28M
    if (file == NULL)
2018
0
  return UNZ_PARAMERROR;
2019
7.28M
    s = (unz64_s*)file;
2020
7.28M
    pfile_in_zip_read_info = s->pfile_in_zip_read;
2021
2022
7.28M
    if (pfile_in_zip_read_info == NULL)
2023
238
  return UNZ_PARAMERROR;
2024
2025
2026
7.28M
    if (pfile_in_zip_read_info->read_buffer == NULL)
2027
0
  return UNZ_END_OF_LIST_OF_FILE;
2028
7.28M
    if (len == 0)
2029
0
  return 0;
2030
2031
7.28M
    pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
2032
2033
7.28M
    pfile_in_zip_read_info->stream.avail_out = (uInt)len;
2034
2035
7.28M
    if ((len>pfile_in_zip_read_info->rest_read_uncompressed) &&
2036
128
  (!(pfile_in_zip_read_info->raw)))
2037
128
  pfile_in_zip_read_info->stream.avail_out =
2038
128
      (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
2039
2040
7.28M
    if ((len>pfile_in_zip_read_info->rest_read_compressed+
2041
7.28M
     pfile_in_zip_read_info->stream.avail_in) &&
2042
1.15k
   (pfile_in_zip_read_info->raw))
2043
0
  pfile_in_zip_read_info->stream.avail_out =
2044
0
      (uInt)pfile_in_zip_read_info->rest_read_compressed+
2045
0
      pfile_in_zip_read_info->stream.avail_in;
2046
2047
14.5M
    while (pfile_in_zip_read_info->stream.avail_out>0)
2048
7.28M
    {
2049
7.28M
  if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
2050
1.11k
      (pfile_in_zip_read_info->rest_read_compressed>0))
2051
260
  {
2052
260
      size_t uReadThis = UNZ_BUFSIZE;
2053
260
      if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
2054
208
    uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
2055
260
      if (uReadThis == 0)
2056
0
    return UNZ_EOF;
2057
260
      if (fseek_func(pfile_in_zip_read_info->filestream,
2058
260
         pfile_in_zip_read_info->pos_in_zipfile +
2059
260
         pfile_in_zip_read_info->byte_before_the_zipfile,
2060
260
         ZLIB_FILEFUNC_SEEK_SET) != 0)
2061
0
    return UNZ_ERRNO;
2062
260
      if (fread_func(pfile_in_zip_read_info->filestream,
2063
260
         pfile_in_zip_read_info->read_buffer,
2064
260
         uReadThis) != uReadThis)
2065
58
    return UNZ_ERRNO;
2066
2067
202
      pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
2068
2069
202
      pfile_in_zip_read_info->rest_read_compressed -= uReadThis;
2070
2071
202
      pfile_in_zip_read_info->stream.next_in =
2072
202
    (Bytef*)pfile_in_zip_read_info->read_buffer;
2073
202
      pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
2074
202
  }
2075
2076
7.28M
  if ((pfile_in_zip_read_info->compression_method == 0) ||
2077
7.28M
      (pfile_in_zip_read_info->raw))
2078
492
  {
2079
492
      uInt uDoCopy,i ;
2080
2081
492
      if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
2082
24
    (pfile_in_zip_read_info->rest_read_compressed == 0))
2083
24
    return (iRead == 0) ? UNZ_EOF : iRead;
2084
2085
468
      if (pfile_in_zip_read_info->stream.avail_out <
2086
468
          pfile_in_zip_read_info->stream.avail_in)
2087
423
    uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
2088
45
      else
2089
45
    uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
2090
2091
124k
      for (i = 0; i < uDoCopy; i++)
2092
123k
    *(pfile_in_zip_read_info->stream.next_out+i) =
2093
123k
      *(pfile_in_zip_read_info->stream.next_in+i);
2094
2095
468
      pfile_in_zip_read_info->total_out_64 =
2096
468
    pfile_in_zip_read_info->total_out_64 + uDoCopy;
2097
2098
468
      pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
2099
468
        pfile_in_zip_read_info->stream.next_out,
2100
468
        uDoCopy);
2101
468
      pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
2102
468
      pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
2103
468
      pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
2104
468
      pfile_in_zip_read_info->stream.next_out += uDoCopy;
2105
468
      pfile_in_zip_read_info->stream.next_in += uDoCopy;
2106
468
      pfile_in_zip_read_info->stream.total_out += uDoCopy;
2107
468
      iRead += uDoCopy;
2108
468
  }
2109
7.28M
  else if (pfile_in_zip_read_info->compression_method == Z_BZIP2ED)
2110
7.26M
  {
2111
7.26M
#ifdef HAVE_BZIP2
2112
7.26M
      uLong uTotalOutBefore,uTotalOutAfter;
2113
7.26M
      const Bytef *bufBefore;
2114
7.26M
      uLong uOutThis;
2115
2116
7.26M
      pfile_in_zip_read_info->bstream.next_in        =
2117
7.26M
    (char*)pfile_in_zip_read_info->stream.next_in;
2118
7.26M
      pfile_in_zip_read_info->bstream.avail_in       =
2119
7.26M
    pfile_in_zip_read_info->stream.avail_in;
2120
7.26M
      pfile_in_zip_read_info->bstream.total_in_lo32  =
2121
7.26M
    (unsigned int) pfile_in_zip_read_info->stream.total_in;
2122
7.26M
      pfile_in_zip_read_info->bstream.total_in_hi32  = 0;
2123
7.26M
      pfile_in_zip_read_info->bstream.next_out       =
2124
7.26M
    (char*)pfile_in_zip_read_info->stream.next_out;
2125
7.26M
      pfile_in_zip_read_info->bstream.avail_out      =
2126
7.26M
    pfile_in_zip_read_info->stream.avail_out;
2127
7.26M
      pfile_in_zip_read_info->bstream.total_out_lo32 =
2128
7.26M
    (unsigned int) pfile_in_zip_read_info->stream.total_out;
2129
7.26M
      pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
2130
2131
7.26M
      uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
2132
7.26M
      bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
2133
2134
7.26M
      err = BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
2135
2136
7.26M
      uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
2137
7.26M
      uOutThis = uTotalOutAfter-uTotalOutBefore;
2138
2139
7.26M
      pfile_in_zip_read_info->total_out_64 =
2140
7.26M
    pfile_in_zip_read_info->total_out_64 + uOutThis;
2141
2142
7.26M
      pfile_in_zip_read_info->crc32 =
2143
7.26M
    crc32(pfile_in_zip_read_info->crc32,bufBefore,
2144
7.26M
          (uInt)(uOutThis));
2145
7.26M
      pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
2146
7.26M
      iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
2147
2148
7.26M
      pfile_in_zip_read_info->stream.next_in   =
2149
7.26M
    (Bytef*)pfile_in_zip_read_info->bstream.next_in;
2150
7.26M
      pfile_in_zip_read_info->stream.avail_in  =
2151
7.26M
    pfile_in_zip_read_info->bstream.avail_in;
2152
7.26M
      pfile_in_zip_read_info->stream.total_in  =
2153
7.26M
    pfile_in_zip_read_info->bstream.total_in_lo32;
2154
7.26M
      pfile_in_zip_read_info->stream.next_out  =
2155
7.26M
    (Bytef*)pfile_in_zip_read_info->bstream.next_out;
2156
7.26M
      pfile_in_zip_read_info->stream.avail_out =
2157
7.26M
    pfile_in_zip_read_info->bstream.avail_out;
2158
7.26M
      pfile_in_zip_read_info->stream.total_out =
2159
7.26M
    pfile_in_zip_read_info->bstream.total_out_lo32;
2160
2161
7.26M
      if (err == BZ_STREAM_END)
2162
1
        return (iRead == 0) ? UNZ_EOF : iRead;
2163
7.26M
      if (err != BZ_OK)
2164
40
        break;
2165
      /* BZ_OK with no output means the decoder wants more input.
2166
         With the entry's compressed bytes all consumed there is
2167
         none, so the stream is truncated and this loop would never
2168
         end.  (inflate() reports Z_BUF_ERROR in that state.) */
2169
7.26M
      if (uOutThis == 0 &&
2170
1
    pfile_in_zip_read_info->stream.avail_in == 0 &&
2171
1
    pfile_in_zip_read_info->rest_read_compressed == 0)
2172
1
      {
2173
1
    err = BZ_UNEXPECTED_EOF;
2174
1
    break;
2175
1
      }
2176
7.26M
#endif
2177
7.26M
  } // end Z_BZIP2ED
2178
14.2k
  else
2179
14.2k
  {
2180
14.2k
      ZPOS64_T uTotalOutBefore,uTotalOutAfter;
2181
14.2k
      const Bytef *bufBefore;
2182
14.2k
      ZPOS64_T uOutThis;
2183
14.2k
      int flush = Z_SYNC_FLUSH;
2184
2185
14.2k
      uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
2186
14.2k
      bufBefore = pfile_in_zip_read_info->stream.next_out;
2187
2188
      /*
2189
      if ((pfile_in_zip_read_info->rest_read_uncompressed ==
2190
         pfile_in_zip_read_info->stream.avail_out) &&
2191
    (pfile_in_zip_read_info->rest_read_compressed == 0))
2192
    flush = Z_FINISH;
2193
      */
2194
14.2k
      err = inflate(&pfile_in_zip_read_info->stream, flush);
2195
2196
14.2k
      if ((err >= 0) && (pfile_in_zip_read_info->stream.msg != NULL))
2197
0
        err = Z_DATA_ERROR;
2198
2199
14.2k
      uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
2200
14.2k
      uOutThis = uTotalOutAfter-uTotalOutBefore;
2201
2202
14.2k
      pfile_in_zip_read_info->total_out_64 =
2203
14.2k
    pfile_in_zip_read_info->total_out_64 + uOutThis;
2204
2205
14.2k
      pfile_in_zip_read_info->crc32 =
2206
14.2k
    crc32(pfile_in_zip_read_info->crc32,bufBefore,
2207
14.2k
      (uInt)(uOutThis));
2208
2209
14.2k
      pfile_in_zip_read_info->rest_read_uncompressed -=
2210
14.2k
    uOutThis;
2211
2212
14.2k
      iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
2213
      // R addition for 3.2.0
2214
14.2k
      if(iRead < len && uTotalOutAfter == 4294967295U)
2215
0
    warning("possible truncation of >= 4GB file");
2216
2217
14.2k
      if (err == Z_STREAM_END)
2218
32
    return (iRead == 0) ? UNZ_EOF : iRead;
2219
14.2k
      if (err != Z_OK)
2220
29
    break;
2221
14.2k
  }
2222
7.28M
    }
2223
2224
7.28M
    if (err == Z_OK)
2225
7.28M
  return iRead;
2226
70
    return err;
2227
7.28M
}
2228
2229
/*
2230
  Close the file in zip opened with unzipOpenCurrentFile
2231
  Return UNZ_CRCERROR if all the file was read but the CRC is not good
2232
*/
2233
static int unzCloseCurrentFile (unzFile file)
2234
592
{
2235
592
    int err = UNZ_OK;
2236
2237
592
    unz64_s* s;
2238
592
    file_in_zip64_read_info_s* pfile_in_zip_read_info;
2239
592
    if (file == NULL)
2240
0
  return UNZ_PARAMERROR;
2241
592
    s = (unz64_s*)file;
2242
592
    pfile_in_zip_read_info = s->pfile_in_zip_read;
2243
2244
592
    if (pfile_in_zip_read_info == NULL)
2245
238
  return UNZ_PARAMERROR;
2246
2247
2248
354
    if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
2249
118
  (!pfile_in_zip_read_info->raw))
2250
118
    {
2251
118
  if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
2252
77
      err = UNZ_CRCERROR;
2253
118
    }
2254
2255
2256
354
    TRYFREE(pfile_in_zip_read_info->read_buffer);
2257
354
    pfile_in_zip_read_info->read_buffer = NULL;
2258
354
    if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
2259
131
  inflateEnd(&pfile_in_zip_read_info->stream);
2260
223
#ifdef HAVE_BZIP2
2261
223
    else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
2262
86
  BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
2263
354
#endif
2264
2265
2266
354
    pfile_in_zip_read_info->stream_initialised = 0;
2267
354
    TRYFREE(pfile_in_zip_read_info);
2268
2269
354
    s->pfile_in_zip_read = NULL;
2270
2271
354
    return err;
2272
592
}
2273
2274
2275
#ifdef Win32
2276
# define f_seek fseeko64
2277
# define f_tell ftello64
2278
#elif defined(HAVE_OFF_T) && defined(HAVE_FSEEKO)
2279
4.72M
# define f_seek fseeko
2280
5.31k
# define f_tell ftello
2281
#else
2282
# define f_seek fseek
2283
# define f_tell ftell
2284
#endif
2285
2286
static voidpf fopen_func(const void* filename, int mode)
2287
2.94k
{
2288
2.94k
    FILE* file = NULL;
2289
2.94k
    const char* mode_fopen = NULL;
2290
2.94k
    if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
2291
2.94k
  mode_fopen = "rb";
2292
0
    else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
2293
0
  mode_fopen = "r+b";
2294
0
    else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
2295
0
  mode_fopen = "wb";
2296
2297
2.94k
    if ((filename != NULL) && (mode_fopen != NULL))
2298
2.94k
  file = fopen(filename, mode_fopen);
2299
2.94k
    return file;
2300
2.94k
}
2301
2302
2303
static ZPOS64_T ftell_func (voidpf stream)
2304
5.31k
{
2305
5.31k
    return f_tell((FILE *)stream);
2306
5.31k
}
2307
2308
static int fseek_func (voidpf stream, ZPOS64_T offset, int origin)
2309
4.72M
{
2310
4.72M
    int fseek_origin = 0;
2311
4.72M
    int ret;
2312
4.72M
    switch (origin)
2313
4.72M
    {
2314
4.69M
    case ZLIB_FILEFUNC_SEEK_CUR :
2315
4.69M
  fseek_origin = SEEK_CUR;
2316
4.69M
  break;
2317
5.31k
    case ZLIB_FILEFUNC_SEEK_END :
2318
5.31k
  fseek_origin = SEEK_END;
2319
5.31k
  break;
2320
21.6k
    case ZLIB_FILEFUNC_SEEK_SET :
2321
21.6k
  fseek_origin = SEEK_SET;
2322
21.6k
  break;
2323
0
    default: return -1;
2324
4.72M
    }
2325
4.72M
    ret = 0;
2326
4.72M
    if (f_seek((FILE *)stream, offset, fseek_origin) != 0) ret = -1;
2327
4.72M
    return ret;
2328
4.72M
}
2329
2330
static size_t fread_func (voidpf stream, void* buf, size_t size)
2331
19.2M
{
2332
19.2M
    uLong ret;
2333
19.2M
    ret = (uLong)fread(buf, 1, size, (FILE *) stream);
2334
19.2M
    return ret;
2335
19.2M
}
2336
2337
static int fclose_func (voidpf stream)
2338
2.91k
{
2339
2.91k
    int ret;
2340
2.91k
    ret = fclose((FILE *)stream);
2341
2.91k
    return ret;
2342
2.91k
}
2343
2344
static int ferror_func (voidpf stream)
2345
18.7M
{
2346
18.7M
    int ret;
2347
18.7M
    ret = ferror((FILE *)stream);
2348
18.7M
    return ret;
2349
18.7M
}