~ubuntu-branches/ubuntu/karmic/fastjar/karmic-security

« back to all changes in this revision

Viewing changes to jartool.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-11-11 05:58:19 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20081111055819-y7rp49bl4rjmmlge
Tags: upstream-0.97
ImportĀ upstreamĀ versionĀ 0.97

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "config.h"
24
24
#endif
25
25
 
 
26
#include <inttypes.h>
 
27
#include <stdint.h>
 
28
 
26
29
#include <zlib.h>
27
30
 
28
31
#ifdef HAVE_STDLIB_H
102
105
 
103
106
typedef struct ziplistentry ziplistentry;
104
107
 
 
108
static void exit_on_error(const char* message);
105
109
static void usage(const char*);
106
110
static void help(const char *);
107
111
static void version(void);
354
358
      if ((jarfd = open (jarfile, O_RDWR | O_BINARY)) < 0)
355
359
        {
356
360
          fprintf (stderr, "Error opening %s for reading!\n", jarfile);
357
 
          perror (jarfile);
358
 
          exit(EXIT_FAILURE);
 
361
          exit_on_error (jarfile);
359
362
        }
360
363
 
361
364
      /* Assert that jarfd is seekable. */
425
428
    /* Check if the file shrunk when we updated it. */
426
429
    if (action == ACTION_UPDATE)
427
430
#ifdef HAVE_FTRUNCATE
428
 
      ftruncate (jarfd, lseek (jarfd, 0, SEEK_CUR));
 
431
      if (-1 == ftruncate (jarfd, lseek (jarfd, 0, SEEK_CUR)))
 
432
        exit_on_error("ftruncate");
429
433
#else
430
434
      _chsize (jarfd, lseek (jarfd, 0, SEEK_CUR));
431
435
#endif
462
466
    create_central_header(jarfd);
463
467
 
464
468
#ifdef HAVE_FTRUNCATE
465
 
    ftruncate (jarfd, lseek (jarfd, 0, SEEK_CUR));
 
469
    if (-1 == ftruncate (jarfd, lseek (jarfd, 0, SEEK_CUR)))
 
470
      exit_on_error("ftruncate");
466
471
#else
467
472
    _chsize (jarfd, lseek (jarfd, 0, SEEK_CUR));
468
473
#endif
478
483
  exit(EXIT_SUCCESS);
479
484
}
480
485
 
 
486
static void
 
487
exit_on_error(const char* message)
 
488
{
 
489
  perror(message);
 
490
  exit(EXIT_FAILURE);
 
491
}
 
492
 
481
493
static int args_current_g;
482
494
static const char **args_g;
483
495
 
521
533
 
522
534
      s = (char *) malloc (len);
523
535
      if (s == NULL)
524
 
        {
525
 
          perror ("malloc");
526
 
          exit(EXIT_FAILURE);
527
 
        }
 
536
        exit_on_error("malloc");
528
537
 
529
538
      /* Get rid of '\n' and '\r' first. */
530
539
      while (1)
555
564
              len *= 2;
556
565
              s = (char *) realloc (s, len);
557
566
              if (s == NULL)
558
 
                {
559
 
                  perror ("realloc");
560
 
                  exit(EXIT_FAILURE);
561
 
                }
 
567
                  exit_on_error ("realloc");
562
568
            }
563
569
        }
564
570
 
672
678
/*
673
679
 * Read the zip entries of an existing file, building `ziplist' as we go.
674
680
 */
 
681
static
675
682
int read_entries (int fd)
676
683
{
677
684
  struct zipentry *ze;
812
819
  size_t nlen;   /* length of file name */
813
820
  int mod_time; /* file modification time */
814
821
  struct zipentry *ze;
815
 
  
816
 
  nlen = 9;  /* trust me on this one */
817
 
 
818
 
  memset((file_header + 12), '\0', 16); /*clear mod time, crc, size fields*/
819
 
  
820
 
  current_time = time(NULL);
821
 
  if(current_time == (time_t)-1){
822
 
    perror("time");
823
 
    exit(EXIT_FAILURE);
824
 
  }
825
 
 
826
 
  mod_time = unix2dostime(&current_time);
827
 
  
828
 
  PACK_UB2(file_header, LOC_EXTRA, 0);
829
 
  PACK_UB2(file_header, LOC_COMP, 0);
830
 
  PACK_UB2(file_header, LOC_FNLEN, nlen);
831
 
  PACK_UB4(file_header, LOC_MODTIME, mod_time);
832
 
  
833
 
  if(verbose)
834
 
    printf("adding: META-INF/ (in=0) (out=0) (stored 0%%)\n");
835
 
  
836
 
  ze = (zipentry*)malloc(sizeof(zipentry));
837
 
  if(ze == NULL){
838
 
    perror("malloc");
839
 
    exit(EXIT_FAILURE);
840
 
  }
841
 
  
842
 
  memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
843
 
  ze->filename = strdup("META-INF/");
844
 
  if (NULL == ze->filename) {
845
 
     perror("strdup");
846
 
     exit(EXIT_FAILURE);
847
 
  }
848
 
 
849
 
  ze->offset = lseek(jfd, 0, SEEK_CUR);
850
 
  ze->mod_time = (ub2)(mod_time & 0x0000ffff);
851
 
  ze->mod_date = (ub2)((mod_time & 0xffff0000) >> 16);
852
 
  ze->compressed = FALSE;
853
 
 
854
 
  add_entry(ze);
855
 
  
856
 
  write(jfd, file_header, 30);
857
 
  write(jfd, "META-INF/", nlen);
 
822
 
 
823
  mod_time = unix2dostime(&current_time);  
 
824
  
 
825
  /* If we are creating a new manifest, create a META-INF directory entry */
 
826
  if (0 == updating) {
 
827
    nlen = 9;  /* trust me on this one */
 
828
 
 
829
    memset((file_header + 12), '\0', 16); /*clear mod time, crc, size fields*/
 
830
  
 
831
    current_time = time(NULL);
 
832
    if(current_time == (time_t)-1)
 
833
      exit_on_error("time");
 
834
 
 
835
    PACK_UB2(file_header, LOC_EXTRA, 0);
 
836
    PACK_UB2(file_header, LOC_COMP, 0);
 
837
    PACK_UB2(file_header, LOC_FNLEN, nlen);
 
838
    PACK_UB4(file_header, LOC_MODTIME, mod_time);
 
839
  
 
840
    if(verbose)
 
841
      printf("adding: META-INF/ (in=0) (out=0) (stored 0%%)\n");
 
842
  
 
843
    ze = (zipentry*)malloc(sizeof(zipentry));
 
844
    if(ze == NULL)
 
845
      exit_on_error("malloc");
 
846
  
 
847
    memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
 
848
    ze->filename = strdup("META-INF/");
 
849
    if (NULL == ze->filename)
 
850
      exit_on_error("strdup");
 
851
 
 
852
    ze->offset = lseek(jfd, 0, SEEK_CUR);
 
853
    ze->mod_time = (ub2)(mod_time & 0x0000ffff);
 
854
    ze->mod_date = (ub2)((mod_time & 0xffff0000) >> 16);
 
855
    ze->compressed = FALSE;
 
856
 
 
857
    add_entry(ze);
 
858
 
 
859
    if (-1 == write(jfd, file_header, 30))
 
860
      exit_on_error("write");
 
861
    if (-1 == write(jfd, "META-INF/", nlen))
 
862
      exit_on_error("write");
 
863
  }
858
864
 
859
865
  /* if the user didn't specify an external manifest file... */
860
866
  if(mf_name == NULL){
886
892
      printf("adding: META-INF/MANIFEST.MF (in=56) (out=56) (stored 0%%)\n");
887
893
    
888
894
    ze = (zipentry*)malloc(sizeof(zipentry));
889
 
    if(ze == NULL){
890
 
      perror("malloc");
891
 
      exit(EXIT_FAILURE);
892
 
    }
 
895
    if(ze == NULL)
 
896
      exit_on_error("malloc");
893
897
    
894
898
    memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
895
899
    ze->filename = strdup("META-INF/MANIFEST.MF");
896
 
    if (NULL == ze->filename) {
897
 
      perror("strdup");
898
 
      exit(EXIT_FAILURE);
899
 
    }
 
900
    if (NULL == ze->filename)
 
901
      exit_on_error("strdup");
900
902
    
901
903
    ze->offset = lseek(jfd, 0, SEEK_CUR);
902
904
    ze->mod_time = (ub2)(mod_time & 0x0000ffff);
908
910
    
909
911
    add_entry(ze);
910
912
    
911
 
    write(jfd, file_header, 30);
912
 
    write(jfd, "META-INF/MANIFEST.MF", nlen);
913
 
    write(jfd, mf, mf_len);
 
913
    if (-1 == write(jfd, file_header, 30))
 
914
      exit_on_error("write");
 
915
    if (-1 == write(jfd, "META-INF/MANIFEST.MF", nlen))
 
916
      exit_on_error("write");
 
917
    if (-1 == write(jfd, mf, mf_len))
 
918
      exit_on_error("write");
914
919
    free(mf);
915
920
    }
916
921
    else {
935
940
      exit(EXIT_FAILURE);
936
941
    }
937
942
 
938
 
    if(add_file_to_jar(jfd, mfd, "META-INF/MANIFEST.MF", &statbuf, updating)){
939
 
      perror("error writing to jar");
940
 
      exit(EXIT_FAILURE);
941
 
    }
942
 
 
 
943
    if(add_file_to_jar(jfd, mfd, "META-INF/MANIFEST.MF", &statbuf, updating))
 
944
      exit_on_error("error writing to jar");
943
945
  }
944
946
 
945
947
  return 0;
963
965
  
964
966
  old_dir = (char *) malloc (old_dir_len);
965
967
  if (old_dir == NULL)
966
 
    {
967
 
      perror ("malloc");
968
 
      exit(EXIT_FAILURE);
969
 
    }
 
968
      exit_on_error ("malloc");
970
969
 
971
970
  while (1)
972
971
    {
980
979
      old_dir_len *= 2;
981
980
      old_dir = (char *) realloc (old_dir, old_dir_len); 
982
981
      if (old_dir == NULL)
983
 
        {
984
 
          perror ("realloc");
985
 
          exit(EXIT_FAILURE);
986
 
        }
 
982
          exit_on_error ("realloc");
987
983
    }
988
984
 
989
985
  if (chdir(new_dir) == -1) {
1074
1070
    PACK_UB4(file_header, LOC_MODTIME, mod_time);
1075
1071
 
1076
1072
    ze = (zipentry*)malloc(sizeof(zipentry));
1077
 
    if(ze == NULL){
1078
 
      perror("malloc");
1079
 
      exit(EXIT_FAILURE);
1080
 
    }
 
1073
    if(ze == NULL)
 
1074
      exit_on_error("malloc");
1081
1075
 
1082
1076
    memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
1083
1077
    ze->filename = strdup(fullname);
1110
1104
    if (!existing)
1111
1105
      {
1112
1106
        add_entry (ze);
1113
 
        write (fd, file_header, 30);
1114
 
        write (fd, fullname, nlen);
 
1107
        if (-1 == write (fd, file_header, 30))
 
1108
          exit_on_error("write");
 
1109
        if (-1 == write (fd, fullname, nlen))
 
1110
          exit_on_error("write");
1115
1111
        end_of_entries = lseek (fd, 0, SEEK_CUR);
1116
1112
 
1117
1113
        if (verbose)
1218
1214
    memset((file_header + LOC_CRC), '\0', 12); /* clear crc/usize/csize */
1219
1215
  
1220
1216
  ze = (zipentry*)malloc(sizeof(zipentry));
1221
 
  if(ze == NULL){
1222
 
    perror("malloc");
1223
 
    exit(EXIT_FAILURE);
1224
 
  }
1225
 
  
 
1217
  if(ze == NULL)
 
1218
    exit_on_error("malloc");
 
1219
 
1226
1220
  memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
1227
1221
  ze->filename = strdup(fname);
1228
1222
 
1259
1253
     as before */
1260
1254
  
1261
1255
  /* Write the local header */
1262
 
  write(jfd, file_header, 30);
1263
 
    
 
1256
  if (-1 == write(jfd, file_header, 30))
 
1257
      exit_on_error("write");
 
1258
 
1264
1259
  /* write the file name to the zip file */
1265
 
  write(jfd, fname, file_name_length);
1266
 
 
 
1260
  if (1 == write(jfd, fname, file_name_length))
 
1261
    exit_on_error("write");
1267
1262
 
1268
1263
  if(verbose){
1269
1264
    if (existing)
1314
1309
  if(seekable){
1315
1310
    offset = (ze->csize + strlen(ze->filename) + 16);
1316
1311
    
1317
 
    if(lseek(jfd, -offset, SEEK_CUR) == (off_t)-1){
1318
 
      perror("lseek");
1319
 
      exit(EXIT_FAILURE);
1320
 
    }
 
1312
    if(lseek(jfd, -offset, SEEK_CUR) == (off_t)-1)
 
1313
      exit_on_error("lseek");
1321
1314
 
1322
1315
    if(write(jfd, (data_descriptor + 4), 12) != 12){
1323
1316
      perror("write");
1326
1319
    
1327
1320
    offset -= 12;
1328
1321
 
1329
 
    if(lseek(jfd, offset, SEEK_CUR) == (off_t)-1){
1330
 
      perror("lseek");
1331
 
      exit(EXIT_FAILURE);
1332
 
    }
 
1322
    if(lseek(jfd, offset, SEEK_CUR) == (off_t)-1)
 
1323
      exit_on_error("lseek");
 
1324
 
1333
1325
  } else if(do_compress){
1334
1326
    /* Sun's jar tool will only allow a data descriptor if the entry is
1335
1327
       compressed, but we'll save 16 bytes/entry if we only use it when
1474
1466
    PACK_UB2(header, CEN_FNLEN, strlen(ze->filename));
1475
1467
    PACK_UB4(header, CEN_OFFSET, ze->offset);
1476
1468
 
1477
 
    write(fd, header, 46);
 
1469
    if (-1 == write(fd, header, 46))
 
1470
      exit_on_error("write");
1478
1471
 
1479
 
    write(fd, ze->filename, strlen(ze->filename));
 
1472
    if (-1 == write(fd, ze->filename, strlen(ze->filename)))
 
1473
      exit_on_error("write");
1480
1474
  }
1481
1475
 
1482
1476
  dir_size = lseek(fd, 0, SEEK_CUR) - start_offset;
1504
1498
  end_header[20] = 0;
1505
1499
  end_header[21] = 0;
1506
1500
 
1507
 
  write(fd, end_header, 22);
1508
 
  
 
1501
  if (-1 == write(fd, end_header, 22))
 
1502
    exit_on_error("write");
 
1503
 
1509
1504
  if(verbose)
1510
1505
    printf("Total:\n------\n(in = %d) (out = %d) (%s %d%%)\n", 
1511
1506
           total_in, 
1520
1515
int extract_jar(int fd, const char **files, int file_num){
1521
1516
  size_t rdamt;
1522
1517
  int out_a, in_a;
1523
 
  ub4 signature;
 
1518
  uint32_t signature;
1524
1519
  ub4 csize;
1525
1520
  ub4 crc;
1526
1521
  ub2 fnlen;
1580
1575
#endif
1581
1576
      break;
1582
1577
    }else if(signature != 0x04034b50){
1583
 
      printf("Ick! %#x\n", signature);
 
1578
      printf("Ick! %#" PRIx32 "\n", signature);
1584
1579
      break;
1585
1580
    }
1586
1581
    
1698
1693
            /* Add 10 more spots to subdir_list */
1699
1694
            if (subdir_list_index == subdir_list_size) {
1700
1695
              subdir_list_size = subdir_list_size + 10;
1701
 
              subdir_list = realloc(subdir_list, (subdir_list_size) * sizeof(char));
 
1696
              subdir_list = realloc(subdir_list, (subdir_list_size) * sizeof(char *));
1702
1697
 
1703
1698
              if (subdir_list == NULL) {
1704
1699
                fprintf(stderr, "error realloc-ing subdir_list_size\n");
1763
1758
        } else if (strcmp(tmp_buff, ".") != 0)
1764
1759
          ++depth;
1765
1760
        if(stat(tmp_buff, &sbuf) < 0){
1766
 
          if(errno != ENOENT){
1767
 
            perror("stat");
1768
 
            exit(EXIT_FAILURE);
1769
 
          }
 
1761
          if(errno != ENOENT)
 
1762
            exit_on_error("stat");
1770
1763
 
1771
1764
        } else if(S_ISDIR(sbuf.st_mode)){
1772
1765
#ifdef DEBUG    
1782
1775
#ifdef DEBUG    
1783
1776
        printf("Making directory..\n");
1784
1777
#endif
1785
 
        if(mkdir(tmp_buff, 0755) < 0){
1786
 
          perror("mkdir");
1787
 
          exit(EXIT_FAILURE);
1788
 
        }
 
1778
        if(mkdir(tmp_buff, 0755) < 0)
 
1779
          exit_on_error("mkdir");
 
1780
 
1789
1781
        if(verbose && handle)
1790
1782
          printf("%10s: %s/\n", "created", tmp_buff);
1791
1783
 
1812
1804
 
1813
1805
      if(f_fd < 0){
1814
1806
        fprintf(stderr, "Error extracting JAR archive!\n");
1815
 
        perror((const char *)filename);
1816
 
        exit(EXIT_FAILURE);
 
1807
        exit_on_error((const char *)filename);
1817
1808
      }
1818
1809
    }
1819
1810
 
1841
1832
 
1842
1833
      while(out_a < (int)csize){
1843
1834
        rdamt = (in_a > RDSZ ? RDSZ : in_a);
1844
 
        if(pb_read(&pbf, rd_buff, rdamt) != rdamt){
1845
 
          perror("read");
1846
 
          exit(EXIT_FAILURE);
1847
 
        }
 
1835
        if(pb_read(&pbf, rd_buff, rdamt) != rdamt)
 
1836
          exit_on_error("read");
1848
1837
        
1849
1838
        ze.crc = crc32(ze.crc, (Bytef*)rd_buff, rdamt);
1850
1839
 
1851
1840
        if(f_fd >= 0) {
1852
1841
          ssize_t num_written;
1853
1842
          num_written = write(f_fd, rd_buff, rdamt);
1854
 
          if (num_written == -1) {
1855
 
            perror("write");
1856
 
            exit(EXIT_FAILURE);
1857
 
          }
 
1843
          if (num_written == -1)
 
1844
            exit_on_error("write");
1858
1845
        }
1859
1846
 
1860
1847
        out_a += rdamt;
1869
1856
    /* if there is a data descriptor left, compare the CRC */
1870
1857
    if(flags & 0x0008){
1871
1858
 
1872
 
      if(pb_read(&pbf, scratch, 16) != 16){
1873
 
        perror("read");
1874
 
        exit(EXIT_FAILURE);
1875
 
      }
 
1859
      if(pb_read(&pbf, scratch, 16) != 16)
 
1860
        exit_on_error("read");
1876
1861
 
1877
1862
      signature = UNPACK_UB4(scratch, 0);
1878
1863
 
1902
1887
  for (j = 0; j < subdir_list_index; j++) {
1903
1888
    free(subdir_list[j]);
1904
1889
  }
 
1890
  free(subdir_list); 
1905
1891
  return 0;
1906
1892
}
1907
1893
 
1936
1922
 
1937
1923
  /* This should be the start of the central-header-end section */
1938
1924
  if(seekable){
1939
 
    if(lseek(fd, -22, SEEK_END) == (off_t)-1){
1940
 
      perror("lseek");
1941
 
      exit(EXIT_FAILURE);
1942
 
    }
 
1925
    if(lseek(fd, -22, SEEK_END) == (off_t)-1)
 
1926
      exit_on_error("lseek");
1943
1927
    
1944
 
    if(read(fd, &tmp, sizeof(ub4)) != 4){
1945
 
      perror("read");
1946
 
      exit(EXIT_FAILURE);
1947
 
    }
 
1928
    if(read(fd, &tmp, sizeof(ub4)) != 4)
 
1929
      exit_on_error("read");
1948
1930
 
1949
1931
#ifdef WORDS_BIGENDIAN
1950
1932
    tmp = L2BI(tmp);
1958
1940
         position, so we have to fall back on the method used for
1959
1941
         non-seekable files. */
1960
1942
      seekable = FALSE;
1961
 
      if(lseek(fd, 0, SEEK_SET) == (off_t)-1){
1962
 
        perror("lseek");
1963
 
        exit(EXIT_FAILURE);
1964
 
      }
 
1943
      if(lseek(fd, 0, SEEK_SET) == (off_t)-1)
 
1944
        exit_on_error("lseek");
1965
1945
    } else {
1966
 
      if(lseek(fd, 6, SEEK_CUR) == (off_t)-1){
1967
 
        perror("lseek");
1968
 
        exit(EXIT_FAILURE);
1969
 
      }
 
1946
      if(lseek(fd, 6, SEEK_CUR) == (off_t)-1)
 
1947
        exit_on_error("lseek");
1970
1948
  
1971
 
      if(read(fd, &cen_size, 2) != 2){
1972
 
        perror("read");
1973
 
        exit(EXIT_FAILURE);
1974
 
      }
 
1949
      if(read(fd, &cen_size, 2) != 2)
 
1950
        exit_on_error("read");
1975
1951
 
1976
1952
#ifdef WORDS_BIGENDIAN
1977
1953
      cen_size = L2BS(cen_size);
1979
1955
 
1980
1956
      /*   printf("%hu entries in central header\n", cen_size); */
1981
1957
 
1982
 
      if(lseek(fd, 4, SEEK_CUR) == (off_t)-1){
1983
 
        perror("lseek");
1984
 
        exit(EXIT_FAILURE);
1985
 
      }
1986
 
 
1987
 
      if(read(fd, &tmp, 4) != 4){
1988
 
        perror("read");
1989
 
        exit(EXIT_FAILURE);
1990
 
      }
 
1958
      if(lseek(fd, 4, SEEK_CUR) == (off_t)-1)
 
1959
        exit_on_error("lseek");
 
1960
 
 
1961
      if(read(fd, &tmp, 4) != 4)
 
1962
        exit_on_error("read");
 
1963
 
1991
1964
 
1992
1965
#ifdef WORDS_BIGENDIAN
1993
1966
      tmp = L2BI(tmp);
1997
1970
 
1998
1971
      /*   printf("Central header offset = %d\n", central_header_offset); */
1999
1972
 
2000
 
      if(lseek(fd, central_header_offset, SEEK_SET) != central_header_offset){
2001
 
        perror("lseek");
2002
 
        exit(EXIT_FAILURE);
2003
 
      }
 
1973
      if(lseek(fd, central_header_offset, SEEK_SET) != central_header_offset)
 
1974
        exit_on_error("lseek");
2004
1975
 
2005
1976
      /* Loop through the entries in the central header */
2006
1977
      for(i = 0; i < cen_size; i++){
2008
1979
 
2009
1980
        read_amt = read(fd, &cen_header, 46);
2010
1981
    
2011
 
        if(read_amt == -1 || read_amt != 46){
2012
 
          perror("read");
2013
 
          exit(EXIT_FAILURE);
2014
 
        }
 
1982
        if(read_amt == -1 || read_amt != 46)
 
1983
          exit_on_error("read");
2015
1984
 
2016
1985
        signature = UNPACK_UB4(cen_header, 0);
2017
1986
        if(signature != 0x02014b50){
2033
2002
          tdate = dos2unixtime(mdate);
2034
2003
          s_tm = localtime(&tdate);
2035
2004
          time_string_length = strftime(ascii_date, 30, "%a %b %d %H:%M:%S %Z %Y", s_tm);
2036
 
          if (0 == time_string_length) {
2037
 
            perror("strftime");
2038
 
            exit(EXIT_FAILURE);
2039
 
          }
 
2005
          if (0 == time_string_length)
 
2006
            exit_on_error("strftime");
 
2007
 
2040
2008
          ascii_date[30] = '\0';
2041
2009
        }
2042
2010
 
2045
2013
            free(filename);
2046
2014
 
2047
2015
          filename = malloc(sizeof(ub1) * (fnlen + 1));
2048
 
          if (NULL == filename) {
2049
 
            perror("malloc");
2050
 
            exit(EXIT_FAILURE);
2051
 
          }
 
2016
          if (NULL == filename)
 
2017
            exit_on_error("malloc");
 
2018
 
2052
2019
          filename_len = fnlen + 1;
2053
2020
        }
2054
2021
    
2055
2022
        read_amt = read(fd, filename, fnlen);
2056
 
        if(read_amt == -1 || read_amt != (ssize_t) fnlen){
2057
 
          perror("read");
2058
 
          exit(EXIT_FAILURE);
2059
 
        }
 
2023
        if(read_amt == -1 || read_amt != (ssize_t) fnlen)
 
2024
          exit_on_error("read");
 
2025
 
2060
2026
        filename[fnlen] = (ub1) '\0';
2061
2027
    
2062
2028
        /* if the user specified a list of files on the command line,
2079
2045
      
2080
2046
        size = eflen + clen;
2081
2047
        if(size > 0){
2082
 
          if(lseek(fd, size, SEEK_CUR) == (off_t)-1){
2083
 
            perror("lseek");
2084
 
            exit(EXIT_FAILURE);
2085
 
          }
 
2048
          if(lseek(fd, size, SEEK_CUR) == (off_t)-1)
 
2049
            exit_on_error("lseek");
2086
2050
        }
2087
2051
      }
2088
2052
    }
2247
2211
#endif
2248
2212
  
2249
2213
  buff = (ub1 *) malloc(RDSZ * sizeof(ub1));
2250
 
  if (NULL == buff) {
2251
 
    perror("malloc");
2252
 
    exit(EXIT_FAILURE);
2253
 
  }
 
2214
  if (NULL == buff)
 
2215
    exit_on_error("malloc");
2254
2216
 
2255
2217
  if (seekable){
2256
2218
    if (amt <= pbf->buff_amt)
2258
2220
    else {
2259
2221
      off_t location;
2260
2222
      location = lseek(pbf->fd, amt - pbf->buff_amt, SEEK_CUR);
2261
 
      if (location == -1) {
2262
 
        perror("lseek");
2263
 
        exit(EXIT_FAILURE);
2264
 
      }
 
2223
      if (location == -1)
 
2224
        exit_on_error("lseek");
 
2225
 
2265
2226
      tc += pb_read(pbf, buff, pbf->buff_amt); /* clear pbf */
2266
2227
    }
2267
2228
  } else
2464
2425
    }
2465
2426
 
2466
2427
  current_time = time(NULL);
2467
 
  if(current_time == (time_t)-1){
2468
 
    perror("time");
2469
 
    exit(EXIT_FAILURE);
2470
 
  }
 
2428
  if(current_time == (time_t)-1)
 
2429
    exit_on_error("time");
2471
2430
 
2472
2431
  mod_time = unix2dostime(&current_time);
2473
2432
  file_name_length = strlen(fname);
2495
2454
      memset((file_header + LOC_CRC), '\0', 12);  /* clear crc/usize/csize */
2496
2455
  
2497
2456
  ze = (zipentry*)malloc(sizeof(zipentry));
2498
 
  if(ze == NULL){
2499
 
    perror("malloc");
2500
 
    exit(EXIT_FAILURE);
2501
 
  }
 
2457
  if(ze == NULL)
 
2458
    exit_on_error("malloc");
2502
2459
  
2503
2460
  memset(ze, 0, sizeof(zipentry)); /* clear all the fields*/
2504
2461
  ze->filename = strdup(fname);
2534
2491
  
2535
2492
  /* Write the local header */
2536
2493
  written = write(jfd, file_header, 30);
2537
 
  if ((written == -1) || ((size_t) written != 30)){
2538
 
    perror("write");
2539
 
    exit(EXIT_FAILURE);
2540
 
  }
 
2494
  if ((written == -1) || ((size_t) written != 30))
 
2495
    exit_on_error("write");
2541
2496
 
2542
2497
  /* write the file name to the zip file */
2543
2498
  written = write(jfd, fname, file_name_length);
2544
 
  if ((written == -1) || ((size_t) written != file_name_length)){
2545
 
    perror("write");
2546
 
    exit(EXIT_FAILURE);
2547
 
  }
2548
 
 
 
2499
  if ((written == -1) || ((size_t) written != file_name_length))
 
2500
    exit_on_error("write");
2549
2501
 
2550
2502
  if(verbose){
2551
2503
    if (existing)
2576
2528
      
2577
2529
  ze->crc = crc32(ze->crc, (unsigned char*)content, content_length);
2578
2530
  written = write(jfd, content, content_length);
2579
 
  if ((written == -1) || ((size_t) written != content_length)){
2580
 
    perror("write");
2581
 
    exit(EXIT_FAILURE);
2582
 
  }
 
2531
  if ((written == -1) || ((size_t) written != content_length))
 
2532
    exit_on_error("write");
2583
2533
      
2584
2534
  /* write out data descriptor */
2585
2535
  PACK_UB4(data_descriptor, 4, ze->crc);
2590
2540
  if(seekable){
2591
2541
    offset = (ze->csize + strlen(ze->filename) + 16);
2592
2542
    
2593
 
    if(lseek(jfd, -offset, SEEK_CUR) == (off_t)-1){
2594
 
      perror("lseek");
2595
 
      exit(EXIT_FAILURE);
2596
 
    }
 
2543
    if(lseek(jfd, -offset, SEEK_CUR) == (off_t)-1)
 
2544
      exit_on_error("lseek");
2597
2545
 
2598
2546
    if(write(jfd, (data_descriptor + 4), 12) != 12){
2599
2547
      perror("write");
2602
2550
    
2603
2551
    offset -= 12;
2604
2552
 
2605
 
    if(lseek(jfd, offset, SEEK_CUR) == (off_t)-1){
2606
 
      perror("lseek");
2607
 
      exit(EXIT_FAILURE);
2608
 
    }
 
2553
    if(lseek(jfd, offset, SEEK_CUR) == (off_t)-1)
 
2554
      exit_on_error("lseek");
2609
2555
  } 
2610
2556
  
2611
2557
  if (existing)
2651
2597
    {
2652
2598
      char * result = strdup(fname);
2653
2599
      if (!result)
2654
 
        {
2655
 
          perror("strdup");
2656
 
          exit(EXIT_FAILURE);
2657
 
        }
 
2600
          exit_on_error("strdup");
 
2601
 
2658
2602
      *strrchr(result, '/') = '\0';
2659
2603
      return result;
2660
2604
    }