jdk/src/share/native/com/sun/java/util/jar/pack/zip.cpp
author ksrini
Tue, 07 May 2013 13:37:03 -0700
changeset 20797 bb3ea44a7875
parent 5506 202f599c92aa
child 21299 b4e7e588552d
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8013506: Better Pack200 data handling Reviewed-by: jrose, kizune, mschoene
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
20797
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1082
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Note: Lifted from uncrunch.c from jdk sources
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#ifndef _MSC_VER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <strings.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "defines.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "bytes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "utils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "constants.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "unpack.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "zip.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#ifdef NO_ZLIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
inline bool jar::deflate_bytes(bytes& head, bytes& tail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
inline uint jar::get_crc32(uint c, uchar *ptr, uint len) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#define Z_NULL NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#else // Have ZLIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#include <zlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
inline uint jar::get_crc32(uint c, uchar *ptr, uint len) { return crc32(c, ptr, len); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#endif // End of ZLIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#ifdef sparc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define SWAP_BYTES(a) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    ((((a) << 8) & 0xff00) | 0x00ff) & (((a) >> 8) | 0xff00)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#define SWAP_BYTES(a)  (a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
#define GET_INT_LO(a) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    SWAP_BYTES(a & 0xFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#define GET_INT_HI(a) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    SWAP_BYTES((a >> 16) & 0xFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
void jar::init(unpacker* u_) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  BYTES_OF(*this).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  u = u_;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  u->jarout = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
// Write data to the ZIP output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
void jar::write_data(void* buff, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  while (len > 0) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    88
    int rc = (int)fwrite(buff, 1, len, jarfp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (rc <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      fprintf(u->errstrm, "Error: write on output file failed err=%d\n",errno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      exit(1); // Called only from the native standalone unpacker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    output_file_offset += rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    buff = ((char *)buff) + rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    len -= rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
void jar::add_to_jar_directory(const char* fname, bool store, int modtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                               int len, int clen, uLong crc) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   101
  uint fname_length = (uint)strlen(fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  ushort header[23];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  if (modtime == 0)  modtime = default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  uLong dostime = get_dostime(modtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   106
  header[0] = (ushort)SWAP_BYTES(0x4B50);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   107
  header[1] = (ushort)SWAP_BYTES(0x0201);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   108
  header[2] = (ushort)SWAP_BYTES(0xA);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  // required version
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   111
  header[3] = (ushort)SWAP_BYTES(0xA);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  // flags 02 = maximum  sub-compression flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  header[4] = ( store ) ? 0x0 : SWAP_BYTES(0x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  // Compression method 8=deflate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  header[5] = ( store ) ? 0x0 : SWAP_BYTES(0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  // Last modified date and time.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   120
  header[6] = (ushort)GET_INT_LO(dostime);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   121
  header[7] = (ushort)GET_INT_HI(dostime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  // CRC
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   124
  header[8] = (ushort)GET_INT_LO(crc);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   125
  header[9] = (ushort)GET_INT_HI(crc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  // Compressed length:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   128
  header[10] = (ushort)GET_INT_LO(clen);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   129
  header[11] = (ushort)GET_INT_HI(clen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  // Uncompressed length.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   132
  header[12] = (ushort)GET_INT_LO(len);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   133
  header[13] = (ushort)GET_INT_HI(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  // Filename length
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   136
  header[14] = (ushort)SWAP_BYTES(fname_length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
  // So called "extra field" length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
  header[15] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
  // So called "comment" length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  header[16] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
  // Disk number start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
  header[17] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
  // File flags => binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
  header[18] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
  // More file flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  header[19] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
  header[20] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
  // Offset within ZIP file.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   149
  header[21] = (ushort)GET_INT_LO(output_file_offset);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   150
  header[22] = (ushort)GET_INT_HI(output_file_offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
  // Copy the whole thing into the central directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  central_directory.append(header, sizeof(header));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  // Copy the fname to the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  central_directory.append(fname, fname_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  central_directory_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
void jar::write_jar_header(const char* fname, bool store, int modtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                           int len, int clen, uint crc) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   163
  uint fname_length = (uint)strlen(fname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
  ushort header[15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  if (modtime == 0)  modtime = default_modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
  uLong dostime = get_dostime(modtime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  // ZIP LOC magic.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   169
  header[0] = (ushort)SWAP_BYTES(0x4B50);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   170
  header[1] = (ushort)SWAP_BYTES(0x0403);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  // Version
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   173
  header[2] = (ushort)SWAP_BYTES(0xA);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  // flags 02 = maximum  sub-compression flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
  header[3] = ( store ) ? 0x0 : SWAP_BYTES(0x2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
  // Compression method = deflate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
  header[4] = ( store ) ? 0x0 : SWAP_BYTES(0x08);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
  // Last modified date and time.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   182
  header[5] = (ushort)GET_INT_LO(dostime);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   183
  header[6] = (ushort)GET_INT_HI(dostime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
  // CRC
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   186
  header[7] = (ushort)GET_INT_LO(crc);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   187
  header[8] = (ushort)GET_INT_HI(crc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  // Compressed length:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   190
  header[9] = (ushort)GET_INT_LO(clen);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   191
  header[10] = (ushort)GET_INT_HI(clen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
  // Uncompressed length.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   194
  header[11] = (ushort)GET_INT_LO(len);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   195
  header[12] = (ushort)GET_INT_HI(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
  // Filename length
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   198
  header[13] = (ushort)SWAP_BYTES(fname_length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
  // So called "extra field" length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
  header[14] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
  // Write the LOC header to the output file.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   203
  write_data(header, (int)sizeof(header));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  // Copy the fname to the header.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   206
  write_data((char*)fname, (int)fname_length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
static const char marker_comment[] = ZIP_ARCHIVE_MARKER_COMMENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
void jar::write_central_directory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  bytes mc; mc.set(marker_comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
  ushort header[11];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
  // Create the End of Central Directory structure.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   217
  header[0] = (ushort)SWAP_BYTES(0x4B50);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   218
  header[1] = (ushort)SWAP_BYTES(0x0605);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
  // disk numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
  header[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
  header[3] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
  // Number of entries in central directory.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   223
  header[4] = (ushort)SWAP_BYTES(central_directory_count);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   224
  header[5] = (ushort)SWAP_BYTES(central_directory_count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
  // Size of the central directory}
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   226
  header[6] = (ushort)GET_INT_LO((int)central_directory.size());
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   227
  header[7] = (ushort)GET_INT_HI((int)central_directory.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
  // Offset of central directory within disk.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   229
  header[8] = (ushort)GET_INT_LO(output_file_offset);
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   230
  header[9] = (ushort)GET_INT_HI(output_file_offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
  // zipfile comment length;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   232
  header [10] = (ushort)SWAP_BYTES((int)mc.len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
  // Write the central directory.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   235
  PRINTCR((2, "Central directory at %d\n", output_file_offset));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
  write_data(central_directory.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
  // Write the End of Central Directory structure.
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   239
  PRINTCR((2, "end-of-directory at %d\n", output_file_offset));
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   240
  write_data(header, (int)sizeof(header));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   242
  PRINTCR((2, "writing zip comment\n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
  // Write the comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  write_data(mc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
// Public API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
// Open a Jar file and initialize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
void jar::openJarFile(const char* fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
  if (!jarfp) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   252
    PRINTCR((1, "jar::openJarFile: opening %s\n",fname));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    jarfp = fopen(fname, "wb");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if (!jarfp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
      fprintf(u->errstrm, "Error: Could not open jar file: %s\n",fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
      exit(3); // Called only from the native standalone unpacker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
// Add a ZIP entry and copy the file data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
void jar::addJarEntry(const char* fname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                      bool deflate_hint, int modtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                      bytes& head, bytes& tail) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   265
  int len = (int)(head.len + tail.len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
  int clen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   268
  uint crc = get_crc32(0,Z_NULL,0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
  if (head.len != 0)
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   270
    crc = get_crc32(crc, (uchar *)head.ptr, (uint)head.len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
  if (tail.len != 0)
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   272
    crc = get_crc32(crc, (uchar *)tail.ptr, (uint)tail.len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  bool deflate = (deflate_hint && len > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
  if (deflate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    if (deflate_bytes(head, tail) == false) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   278
      PRINTCR((2, "Reverting to store fn=%s\t%d -> %d\n",
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   279
              fname, len, deflated.size()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
      deflate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
  }
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   283
  clen = (int)((deflate) ? deflated.size() : len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
  add_to_jar_directory(fname, !deflate, modtime, len, clen, crc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
  write_jar_header(    fname, !deflate, modtime, len, clen, crc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
  if (deflate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    write_data(deflated.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    write_data(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    write_data(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
// Add a ZIP entry for a directory name no data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
void jar::addDirectoryToJarFile(const char* dir_name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
  bool store = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
  add_to_jar_directory((const char*)dir_name, store, default_modtime, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
  write_jar_header(    (const char*)dir_name, store, default_modtime, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
// Write out the central directory and close the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
void jar::closeJarFile(bool central) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
  if (jarfp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    fflush(jarfp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    if (central) write_central_directory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    fflush(jarfp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    fclose(jarfp);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   309
    PRINTCR((2, "jar::closeJarFile:closed jar-file\n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
  reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
/* Convert the date y/n/d and time h:m:s to a four byte DOS date and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *  time (date in high two bytes, time in low two bytes allowing magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *  comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
inline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
uLong jar::dostime(int y, int n, int d, int h, int m, int s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
  return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    (((uLong)y - 1980) << 25) | ((uLong)n << 21) | ((uLong)d << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    ((uLong)h << 11) | ((uLong)m << 5) | ((uLong)s >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
#ifdef _REENTRANT // solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
extern "C" struct tm *gmtime_r(const time_t *, struct tm *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
#define gmtime_r(t, s) gmtime(t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * Return the Unix time in DOS format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
uLong jar::get_dostime(int modtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
  // see defines.h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  if (modtime != 0 && modtime == modtime_cache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    return dostime_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  if (modtime != 0 && default_modtime == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    default_modtime = modtime;  // catch a reasonable default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
  time_t t = modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
  struct tm sbuf;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   341
  (void)memset((void*)&sbuf,0, sizeof(sbuf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
  struct tm* s = gmtime_r(&t, &sbuf);
20797
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
   343
  if (s == NULL) {
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
   344
    fprintf(u->errstrm, "Error: gmtime failure, invalid input archive\n");
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
   345
    exit(2);
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
   346
  }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
  modtime_cache = modtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  dostime_cache = dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                          s->tm_hour, s->tm_min, s->tm_sec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
  //printf("modtime %d => %d\n", modtime_cache, dostime_cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
  return dostime_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
#ifndef NO_ZLIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
/* Returns true on success, and will set the clen to the compressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
   length, the caller should verify if true and clen less than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
   input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
bool jar::deflate_bytes(bytes& head, bytes& tail) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   363
  int len = (int)(head.len + tail.len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
  z_stream zs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
  BYTES_OF(zs).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
  // NOTE: the window size should always be -MAX_WBITS normally -15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
  // unzip/zipup.c and java/Deflater.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
  int error = deflateInit2(&zs, Z_BEST_COMPRESSION, Z_DEFLATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                           -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
  if (error != Z_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    switch (error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    case Z_MEM_ERROR:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   376
      PRINTCR((2, "Error: deflate error : Out of memory \n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    case Z_STREAM_ERROR:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   379
      PRINTCR((2,"Error: deflate error : Invalid compression level \n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    case Z_VERSION_ERROR:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   382
      PRINTCR((2,"Error: deflate error : Invalid version\n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    default:
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   385
      PRINTCR((2,"Error: Internal deflate error error = %d\n", error));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
  deflated.empty();
20797
bb3ea44a7875 8013506: Better Pack200 data handling
ksrini
parents: 5506
diff changeset
   391
  zs.next_out  = (uchar*) deflated.grow(add_size(len, (len/2)));
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   392
  zs.avail_out = (int)deflated.size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
  zs.next_in = (uchar*)head.ptr;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   395
  zs.avail_in = (int)head.len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
  bytes* first = &head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
  bytes* last  = &tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
  if (last->len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    last = &head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
  } else if (first->len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
  if (first != null && error == Z_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    zs.next_in = (uchar*) first->ptr;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   408
    zs.avail_in = (int)first->len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    error = deflate(&zs, Z_NO_FLUSH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
  if (error == Z_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    zs.next_in = (uchar*) last->ptr;
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   413
    zs.avail_in = (int)last->len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    error = deflate(&zs, Z_FINISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
  if (error == Z_STREAM_END) {
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   417
    if (len > (int)zs.total_out ) {
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   418
      PRINTCR((2, "deflate compressed data %d -> %d\n", len, zs.total_out));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      deflated.b.len = zs.total_out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
      deflateEnd(&zs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   423
    PRINTCR((2, "deflate expanded data %d -> %d\n", len, zs.total_out));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    deflateEnd(&zs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
  deflateEnd(&zs);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   429
  PRINTCR((2, "Error: deflate error deflate did not finish error=%d\n",error));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
// Callback for fetching data from a GZIP input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
static jlong read_input_via_gzip(unpacker* u,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                  void* buf, jlong minlen, jlong maxlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
  assert(minlen <= maxlen);  // don't talk nonsense
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
  jlong numread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
  char* bufptr = (char*) buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
  char* inbuf = u->gzin->inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
  size_t inbuflen = sizeof(u->gzin->inbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
  unpacker::read_input_fn_t read_gzin_fn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    (unpacker::read_input_fn_t) u->gzin->read_input_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
  z_stream& zs = *(z_stream*) u->gzin->zstream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
  while (numread < minlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    int readlen = (1 << 16);  // pretty arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    if (readlen > (maxlen - numread))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      readlen = (int)(maxlen - numread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    zs.next_out = (uchar*) bufptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    zs.avail_out = readlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    if (zs.avail_in == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      zs.avail_in = (int) read_gzin_fn(u, inbuf, 1, inbuflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
      zs.next_in = (uchar*) inbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    int error = inflate(&zs, Z_NO_FLUSH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    if (error != Z_OK && error != Z_STREAM_END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
      u->abort("error inflating input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    int nr = readlen - zs.avail_out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    numread += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    bufptr += nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    assert(numread <= maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    if (error == Z_STREAM_END) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
      enum { TRAILER_LEN = 8 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
      // skip 8-byte trailer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
      if (zs.avail_in >= TRAILER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        zs.avail_in -= TRAILER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // Bug: 5023768,we read past the TRAILER_LEN to see if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // any extraneous data, as we dont support concatenated .gz
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // files just yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int extra = (int) read_gzin_fn(u, inbuf, 1, inbuflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        zs.avail_in += extra - TRAILER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
      // %%% should check final CRC and length here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
      // %%% should check for concatenated *.gz files here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
      if (zs.avail_in > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        u->abort("garbage after end of deflated input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
      // pop this filter off:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
      u->gzin->free();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
  //fprintf(u->errstrm, "readInputFn(%d,%d) => %d (gunzip)\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
  //        (int)minlen, (int)maxlen, (int)numread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
  return numread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
void gunzip::init(unpacker* u_) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
  BYTES_OF(*this).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
  u = u_;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
  assert(u->gzin == null);  // once only, please
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   494
  read_input_fn = (void*)u->read_input_fn;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
  zstream = NEW(z_stream, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
  u->gzin = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
  u->read_input_fn = read_input_via_gzip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
void gunzip::start(int magic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
  assert((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
  int gz_flg = (magic & 0xFF);  // keep "flg", discard other 3 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
  enum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    FHCRC    = (1<<1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    FEXTRA   = (1<<2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    FNAME    = (1<<3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    FCOMMENT = (1<<4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
  };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
  char gz_mtime[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
  char gz_xfl[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
  char gz_os[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
  char gz_extra_len[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
  char gz_hcrc[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
  char gz_ignore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
  // do not save extra, name, comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
  read_fixed_field(gz_mtime, sizeof(gz_mtime));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
  read_fixed_field(gz_xfl, sizeof(gz_xfl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
  read_fixed_field(gz_os, sizeof(gz_os));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
  if (gz_flg & FEXTRA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    read_fixed_field(gz_extra_len, sizeof(gz_extra_len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    int extra_len = gz_extra_len[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    extra_len += (gz_extra_len[1] & 0xFF) << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    for (; extra_len > 0; extra_len--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
      read_fixed_field(&gz_ignore, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
  int null_terms = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
  if (gz_flg & FNAME)     null_terms++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
  if (gz_flg & FCOMMENT)  null_terms++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
  for (; null_terms; null_terms--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
      gz_ignore = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
      read_fixed_field(&gz_ignore, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
      if (gz_ignore == 0)  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
  if (gz_flg & FHCRC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    read_fixed_field(gz_hcrc, sizeof(gz_hcrc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
  if (aborting())  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
  // now the input stream is ready to read into the inflater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
  int error = inflateInit2((z_stream*) zstream, -MAX_WBITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
  if (error != Z_OK) { abort("cannot create input"); return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
void gunzip::free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
  assert(u->gzin == this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
  u->gzin = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
  u->read_input_fn = (unpacker::read_input_fn_t) this->read_input_fn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
  inflateEnd((z_stream*) zstream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
  mtrace('f', zstream, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
  ::free(zstream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
  zstream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
  mtrace('f', this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
  ::free(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
void gunzip::read_fixed_field(char* buf, size_t buflen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
  if (aborting())  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
  jlong nr = ((unpacker::read_input_fn_t)read_input_fn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    (u, buf, buflen, buflen);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
   563
  if ((size_t)nr != buflen)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    u->abort("short stream header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
#else // NO_ZLIB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
void gunzip::free() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
#endif // NO_ZLIB