author | jwilhelm |
Thu, 27 Jun 2019 03:13:54 +0200 | |
changeset 55515 | 4c52949a3487 |
parent 53421 | 06862c019f3f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
2 |
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/** |
|
27 |
* Note: Lifted from uncrunch.c from jdk sources |
|
28 |
*/ |
|
29 |
#include <stdio.h> |
|
30 |
#include <string.h> |
|
31 |
#include <errno.h> |
|
32 |
#include <time.h> |
|
33 |
||
34 |
#include <stdlib.h> |
|
35 |
||
36 |
#ifndef _MSC_VER |
|
37 |
#include <strings.h> |
|
38 |
#endif |
|
39 |
||
40 |
#include "defines.h" |
|
41 |
#include "bytes.h" |
|
42 |
#include "utils.h" |
|
43 |
||
44 |
#include "constants.h" |
|
45 |
#include "unpack.h" |
|
46 |
||
47 |
#include "zip.h" |
|
48 |
||
49 |
#ifdef NO_ZLIB |
|
50 |
||
51 |
inline bool jar::deflate_bytes(bytes& head, bytes& tail) { |
|
52 |
return false; |
|
53 |
} |
|
54 |
inline uint jar::get_crc32(uint c, uchar *ptr, uint len) { return 0; } |
|
55 |
#define Z_NULL NULL |
|
56 |
||
57 |
#else // Have ZLIB |
|
58 |
||
59 |
#include <zlib.h> |
|
60 |
||
61 |
inline uint jar::get_crc32(uint c, uchar *ptr, uint len) { return crc32(c, ptr, len); } |
|
62 |
||
63 |
#endif // End of ZLIB |
|
64 |
||
21299
b4e7e588552d
8026794: Test tools/pack200/TimeStamp.java fails while opening golden.jar.native.IST on linux-ppc(v2)
ksrini
parents:
20797
diff
changeset
|
65 |
#ifdef _BIG_ENDIAN |
2 | 66 |
#define SWAP_BYTES(a) \ |
67 |
((((a) << 8) & 0xff00) | 0x00ff) & (((a) >> 8) | 0xff00) |
|
68 |
#else |
|
69 |
#define SWAP_BYTES(a) (a) |
|
70 |
#endif |
|
71 |
||
72 |
#define GET_INT_LO(a) \ |
|
73 |
SWAP_BYTES(a & 0xFFFF) |
|
74 |
||
75 |
#define GET_INT_HI(a) \ |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
76 |
SWAP_BYTES((a >> 16) & 0xFFFF) |
2 | 77 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
78 |
static const ushort jarmagic[2] = { SWAP_BYTES(0xCAFE), 0 }; |
2 | 79 |
|
80 |
void jar::init(unpacker* u_) { |
|
81 |
BYTES_OF(*this).clear(); |
|
82 |
u = u_; |
|
83 |
u->jarout = this; |
|
84 |
} |
|
85 |
||
86 |
// Write data to the ZIP output stream. |
|
29591
51244d1ddffc
8074839: Resolve disabled warnings for libunpack and the unpack200 binary
mikael
parents:
29368
diff
changeset
|
87 |
void jar::write_data(void* buff, size_t len) { |
2 | 88 |
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
|
89 |
int rc = (int)fwrite(buff, 1, len, jarfp); |
2 | 90 |
if (rc <= 0) { |
91 |
fprintf(u->errstrm, "Error: write on output file failed err=%d\n",errno); |
|
92 |
exit(1); // Called only from the native standalone unpacker |
|
93 |
} |
|
94 |
output_file_offset += rc; |
|
95 |
buff = ((char *)buff) + rc; |
|
96 |
len -= rc; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
void jar::add_to_jar_directory(const char* fname, bool store, int modtime, |
|
101 |
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
|
102 |
uint fname_length = (uint)strlen(fname); |
2 | 103 |
ushort header[23]; |
104 |
if (modtime == 0) modtime = default_modtime; |
|
105 |
uLong dostime = get_dostime(modtime); |
|
106 |
||
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
107 |
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
|
108 |
header[1] = (ushort)SWAP_BYTES(0x0201); |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
109 |
header[2] = (ushort)SWAP_BYTES(( store ) ? 0x0A : 0x14); |
2 | 110 |
|
111 |
// required version |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
112 |
header[3] = (ushort)SWAP_BYTES(( store ) ? 0x0A : 0x14); |
2 | 113 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
114 |
// Flags - UTF-8 compression and separating crc and sizes |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
115 |
// into separate headers for deflated file |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
116 |
header[4] = ( store ) ? SWAP_BYTES(0x0800) : 0x0808; |
2 | 117 |
|
118 |
// Compression method 8=deflate. |
|
119 |
header[5] = ( store ) ? 0x0 : SWAP_BYTES(0x08); |
|
120 |
||
121 |
// 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
|
122 |
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
|
123 |
header[7] = (ushort)GET_INT_HI(dostime); |
2 | 124 |
|
125 |
// CRC |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
126 |
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
|
127 |
header[9] = (ushort)GET_INT_HI(crc); |
2 | 128 |
|
129 |
// 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
|
130 |
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
|
131 |
header[11] = (ushort)GET_INT_HI(clen); |
2 | 132 |
|
133 |
// 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
|
134 |
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
|
135 |
header[13] = (ushort)GET_INT_HI(len); |
2 | 136 |
|
137 |
// 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
|
138 |
header[14] = (ushort)SWAP_BYTES(fname_length); |
2 | 139 |
// So called "extra field" length. |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
140 |
// If it's the first record we must add JAR magic sequence |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
141 |
header[15] = ( central_directory_count ) ? 0 : (ushort)SWAP_BYTES(4); |
2 | 142 |
// So called "comment" length. |
143 |
header[16] = 0; |
|
144 |
// Disk number start |
|
145 |
header[17] = 0; |
|
146 |
// File flags => binary |
|
147 |
header[18] = 0; |
|
148 |
// More file flags |
|
149 |
header[19] = 0; |
|
150 |
header[20] = 0; |
|
151 |
// 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
|
152 |
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
|
153 |
header[22] = (ushort)GET_INT_HI(output_file_offset); |
2 | 154 |
|
155 |
// Copy the whole thing into the central directory. |
|
156 |
central_directory.append(header, sizeof(header)); |
|
157 |
||
158 |
// Copy the fname to the header. |
|
159 |
central_directory.append(fname, fname_length); |
|
160 |
||
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
161 |
// Add jar magic for the first record |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
162 |
if (central_directory_count == 0) { |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
163 |
central_directory.append((void *)jarmagic, sizeof(jarmagic)); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
164 |
} |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
165 |
|
2 | 166 |
central_directory_count++; |
167 |
} |
|
168 |
||
169 |
void jar::write_jar_header(const char* fname, bool store, int modtime, |
|
170 |
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
|
171 |
uint fname_length = (uint)strlen(fname); |
2 | 172 |
ushort header[15]; |
173 |
if (modtime == 0) modtime = default_modtime; |
|
174 |
uLong dostime = get_dostime(modtime); |
|
175 |
||
176 |
// 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
|
177 |
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
|
178 |
header[1] = (ushort)SWAP_BYTES(0x0403); |
2 | 179 |
|
180 |
// Version |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
181 |
header[2] = (ushort)SWAP_BYTES(( store ) ? 0x0A : 0x14); |
2 | 182 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
183 |
// General purpose flags - same as in the Central Directory |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
184 |
header[3] = ( store ) ? SWAP_BYTES(0x0800) : 0x0808; |
2 | 185 |
|
186 |
// Compression method = deflate |
|
187 |
header[4] = ( store ) ? 0x0 : SWAP_BYTES(0x08); |
|
188 |
||
189 |
// 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
|
190 |
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
|
191 |
header[6] = (ushort)GET_INT_HI(dostime); |
2 | 192 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
193 |
// CRC, 0 if deflated, will come separately in extra header |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
194 |
header[7] = ( store ) ? (ushort)GET_INT_LO(crc) : 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
195 |
header[8] = ( store ) ? (ushort)GET_INT_HI(crc) : 0; |
2 | 196 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
197 |
// Compressed length, 0 if deflated |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
198 |
header[9] = ( store ) ? (ushort)GET_INT_LO(clen) : 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
199 |
header[10] = ( store ) ? (ushort)GET_INT_HI(clen) : 0; |
2 | 200 |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
201 |
// Uncompressed length, 0 if deflated |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
202 |
header[11] = ( store ) ? (ushort)GET_INT_LO(len) : 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
203 |
header[12] = ( store ) ? (ushort)GET_INT_HI(len) : 0; |
2 | 204 |
|
205 |
// 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
|
206 |
header[13] = (ushort)SWAP_BYTES(fname_length); |
2 | 207 |
// So called "extra field" length. |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
208 |
header[14] = ( central_directory_count - 1 ) ? 0 : (ushort)SWAP_BYTES(4); |
2 | 209 |
|
210 |
// 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
|
211 |
write_data(header, (int)sizeof(header)); |
2 | 212 |
|
213 |
// 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
|
214 |
write_data((char*)fname, (int)fname_length); |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
215 |
|
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
216 |
if (central_directory_count == 1) { |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
217 |
// Write JAR magic sequence |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
218 |
write_data((void *)jarmagic, (int)sizeof(jarmagic)); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
219 |
} |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
220 |
} |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
221 |
|
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
222 |
void jar::write_jar_extra(int len, int clen, uint crc) { |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
223 |
ushort header[8]; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
224 |
// Extra field signature |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
225 |
header[0] = (ushort)SWAP_BYTES(0x4B50); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
226 |
header[1] = (ushort)SWAP_BYTES(0x0807); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
227 |
// CRC |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
228 |
header[2] = (ushort)GET_INT_LO(crc); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
229 |
header[3] = (ushort)GET_INT_HI(crc); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
230 |
// Compressed length |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
231 |
header[4] = (ushort)GET_INT_LO(clen); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
232 |
header[5] = (ushort)GET_INT_HI(clen); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
233 |
// Uncompressed length |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
234 |
header[6] = (ushort)GET_INT_LO(len); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
235 |
header[7] = (ushort)GET_INT_HI(len); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
236 |
|
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
237 |
write_data(header, sizeof(header)); |
2 | 238 |
} |
239 |
||
240 |
static const char marker_comment[] = ZIP_ARCHIVE_MARKER_COMMENT; |
|
241 |
||
242 |
void jar::write_central_directory() { |
|
243 |
bytes mc; mc.set(marker_comment); |
|
244 |
||
245 |
ushort header[11]; |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
246 |
ushort header64[38]; |
2 | 247 |
|
248 |
// 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
|
249 |
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
|
250 |
header[1] = (ushort)SWAP_BYTES(0x0605); |
2 | 251 |
// disk numbers |
252 |
header[2] = 0; |
|
253 |
header[3] = 0; |
|
254 |
// Number of entries in central directory. |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
255 |
header[4] = ( central_directory_count >= 0xffff ) ? 0xffff : (ushort)SWAP_BYTES(central_directory_count); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
256 |
header[5] = ( central_directory_count >= 0xffff ) ? 0xffff : (ushort)SWAP_BYTES(central_directory_count); |
2 | 257 |
// 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
|
258 |
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
|
259 |
header[7] = (ushort)GET_INT_HI((int)central_directory.size()); |
2 | 260 |
// 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
|
261 |
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
|
262 |
header[9] = (ushort)GET_INT_HI(output_file_offset); |
2 | 263 |
// zipfile comment length; |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
264 |
header[10] = (ushort)SWAP_BYTES((int)mc.len); |
2 | 265 |
|
266 |
// 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
|
267 |
PRINTCR((2, "Central directory at %d\n", output_file_offset)); |
2 | 268 |
write_data(central_directory.b); |
269 |
||
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
270 |
// If number of records exceeds the 0xFFFF we need to prepend extended |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
271 |
// Zip64 End of Central Directory record and its locator to the old |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
272 |
// style ECD record |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
273 |
if (central_directory_count > 0xFFFF) { |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
274 |
// Zip64 END signature |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
275 |
header64[0] = (ushort)SWAP_BYTES(0x4B50); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
276 |
header64[1] = (ushort)0x0606; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
277 |
// Size of header (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
278 |
header64[2] = (ushort)SWAP_BYTES(44);; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
279 |
header64[3] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
280 |
header64[4] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
281 |
header64[5] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
282 |
// Version produced and required (short) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
283 |
header64[6] = (ushort)SWAP_BYTES(45); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
284 |
header64[7] = (ushort)SWAP_BYTES(45); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
285 |
// Current disk number (int) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
286 |
header64[8] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
287 |
header64[9] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
288 |
// Central directory start disk (int) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
289 |
header64[10] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
290 |
header64[11] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
291 |
// Count of records on disk (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
292 |
header64[12] = (ushort)GET_INT_LO(central_directory_count); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
293 |
header64[13] = (ushort)GET_INT_HI(central_directory_count); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
294 |
header64[14] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
295 |
header64[15] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
296 |
// Count of records totally (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
297 |
header64[16] = (ushort)GET_INT_LO(central_directory_count); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
298 |
header64[17] = (ushort)GET_INT_HI(central_directory_count); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
299 |
header64[18] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
300 |
header64[19] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
301 |
// Length of the central directory (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
302 |
header64[20] = header[6]; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
303 |
header64[21] = header[7]; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
304 |
header64[22] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
305 |
header64[23] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
306 |
// Offset of central directory (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
307 |
header64[24] = header[8]; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
308 |
header64[25] = header[9]; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
309 |
header64[26] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
310 |
header64[27] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
311 |
// Zip64 end of central directory locator |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
312 |
// Locator signature |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
313 |
header64[28] = (ushort)SWAP_BYTES(0x4B50); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
314 |
header64[29] = (ushort)SWAP_BYTES(0x0706); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
315 |
// Start disk number (int) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
316 |
header64[30] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
317 |
header64[31] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
318 |
// Offset of zip64 END record (long) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
319 |
header64[32] = (ushort)GET_INT_LO(output_file_offset); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
320 |
header64[33] = (ushort)GET_INT_HI(output_file_offset); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
321 |
header64[34] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
322 |
header64[35] = 0; |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
323 |
// Total number of disks (int) |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
324 |
header64[36] = (ushort)SWAP_BYTES(1); |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
325 |
header64[37] = 0; |
29591
51244d1ddffc
8074839: Resolve disabled warnings for libunpack and the unpack200 binary
mikael
parents:
29368
diff
changeset
|
326 |
write_data(header64, sizeof(header64)); |
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
327 |
} |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
328 |
|
2 | 329 |
// 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
|
330 |
PRINTCR((2, "end-of-directory at %d\n", output_file_offset)); |
29591
51244d1ddffc
8074839: Resolve disabled warnings for libunpack and the unpack200 binary
mikael
parents:
29368
diff
changeset
|
331 |
write_data(header, sizeof(header)); |
2 | 332 |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
333 |
PRINTCR((2, "writing zip comment\n")); |
2 | 334 |
// Write the comment. |
335 |
write_data(mc); |
|
336 |
} |
|
337 |
||
338 |
// Public API |
|
339 |
||
340 |
// Open a Jar file and initialize. |
|
341 |
void jar::openJarFile(const char* fname) { |
|
342 |
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
|
343 |
PRINTCR((1, "jar::openJarFile: opening %s\n",fname)); |
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
344 |
jarname = fname; |
2 | 345 |
jarfp = fopen(fname, "wb"); |
346 |
if (!jarfp) { |
|
347 |
fprintf(u->errstrm, "Error: Could not open jar file: %s\n",fname); |
|
348 |
exit(3); // Called only from the native standalone unpacker |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
// Add a ZIP entry and copy the file data |
|
354 |
void jar::addJarEntry(const char* fname, |
|
355 |
bool deflate_hint, int modtime, |
|
356 |
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
|
357 |
int len = (int)(head.len + tail.len); |
2 | 358 |
int clen = 0; |
359 |
||
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
360 |
uint crc = get_crc32(0,Z_NULL,0); |
2 | 361 |
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
|
362 |
crc = get_crc32(crc, (uchar *)head.ptr, (uint)head.len); |
2 | 363 |
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
|
364 |
crc = get_crc32(crc, (uchar *)tail.ptr, (uint)tail.len); |
2 | 365 |
|
366 |
bool deflate = (deflate_hint && len > 0); |
|
367 |
||
368 |
if (deflate) { |
|
369 |
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
|
370 |
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
|
371 |
fname, len, deflated.size())); |
2 | 372 |
deflate = false; |
373 |
} |
|
374 |
} |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
375 |
clen = (int)((deflate) ? deflated.size() : len); |
2 | 376 |
add_to_jar_directory(fname, !deflate, modtime, len, clen, crc); |
377 |
write_jar_header( fname, !deflate, modtime, len, clen, crc); |
|
378 |
||
379 |
if (deflate) { |
|
380 |
write_data(deflated.b); |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
381 |
// Write deflated information in extra header |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
382 |
write_jar_extra(len, clen, crc); |
2 | 383 |
} else { |
384 |
write_data(head); |
|
385 |
write_data(tail); |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
// Add a ZIP entry for a directory name no data |
|
390 |
void jar::addDirectoryToJarFile(const char* dir_name) { |
|
391 |
bool store = true; |
|
392 |
add_to_jar_directory((const char*)dir_name, store, default_modtime, 0, 0, 0); |
|
393 |
write_jar_header( (const char*)dir_name, store, default_modtime, 0, 0, 0); |
|
394 |
} |
|
395 |
||
396 |
// Write out the central directory and close the jar file. |
|
397 |
void jar::closeJarFile(bool central) { |
|
398 |
if (jarfp) { |
|
399 |
fflush(jarfp); |
|
400 |
if (central) write_central_directory(); |
|
401 |
fflush(jarfp); |
|
402 |
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
|
403 |
PRINTCR((2, "jar::closeJarFile:closed jar-file\n")); |
2 | 404 |
} |
405 |
reset(); |
|
406 |
} |
|
407 |
||
408 |
/* Convert the date y/n/d and time h:m:s to a four byte DOS date and |
|
409 |
* time (date in high two bytes, time in low two bytes allowing magnitude |
|
410 |
* comparison). |
|
411 |
*/ |
|
412 |
inline |
|
413 |
uLong jar::dostime(int y, int n, int d, int h, int m, int s) { |
|
414 |
return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) : |
|
415 |
(((uLong)y - 1980) << 25) | ((uLong)n << 21) | ((uLong)d << 16) | |
|
416 |
((uLong)h << 11) | ((uLong)m << 5) | ((uLong)s >> 1); |
|
417 |
} |
|
418 |
||
53421
06862c019f3f
8215976: Fix gmtime_r declaration conflicts in zip.cpp with linux header files
rriggs
parents:
50884
diff
changeset
|
419 |
/* |
06862c019f3f
8215976: Fix gmtime_r declaration conflicts in zip.cpp with linux header files
rriggs
parents:
50884
diff
changeset
|
420 |
* For thread-safe reasons, non-Windows platforms need gmtime_r |
06862c019f3f
8215976: Fix gmtime_r declaration conflicts in zip.cpp with linux header files
rriggs
parents:
50884
diff
changeset
|
421 |
* while Windows can directly use gmtime that is already thread-safe. |
06862c019f3f
8215976: Fix gmtime_r declaration conflicts in zip.cpp with linux header files
rriggs
parents:
50884
diff
changeset
|
422 |
*/ |
06862c019f3f
8215976: Fix gmtime_r declaration conflicts in zip.cpp with linux header files
rriggs
parents:
50884
diff
changeset
|
423 |
#ifdef _MSC_VER |
2 | 424 |
#define gmtime_r(t, s) gmtime(t) |
425 |
#endif |
|
426 |
/* |
|
427 |
* Return the Unix time in DOS format |
|
428 |
*/ |
|
429 |
uLong jar::get_dostime(int modtime) { |
|
430 |
// see defines.h |
|
431 |
if (modtime != 0 && modtime == modtime_cache) |
|
432 |
return dostime_cache; |
|
433 |
if (modtime != 0 && default_modtime == 0) |
|
434 |
default_modtime = modtime; // catch a reasonable default |
|
435 |
time_t t = modtime; |
|
436 |
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
|
437 |
(void)memset((void*)&sbuf,0, sizeof(sbuf)); |
2 | 438 |
struct tm* s = gmtime_r(&t, &sbuf); |
20797 | 439 |
if (s == NULL) { |
440 |
fprintf(u->errstrm, "Error: gmtime failure, invalid input archive\n"); |
|
25156
354c86ad0bc8
8046337: Test closed/tools/pack200/MemoryAllocatorTest.java fails on windows-i586
kizune
parents:
25149
diff
changeset
|
441 |
exit(-1); |
20797 | 442 |
} |
2 | 443 |
modtime_cache = modtime; |
444 |
dostime_cache = dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday, |
|
445 |
s->tm_hour, s->tm_min, s->tm_sec); |
|
446 |
//printf("modtime %d => %d\n", modtime_cache, dostime_cache); |
|
447 |
return dostime_cache; |
|
448 |
} |
|
449 |
||
450 |
||
451 |
||
452 |
#ifndef NO_ZLIB |
|
453 |
||
454 |
/* Returns true on success, and will set the clen to the compressed |
|
455 |
length, the caller should verify if true and clen less than the |
|
456 |
input data |
|
457 |
*/ |
|
458 |
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
|
459 |
int len = (int)(head.len + tail.len); |
2 | 460 |
|
461 |
z_stream zs; |
|
462 |
BYTES_OF(zs).clear(); |
|
463 |
||
464 |
// NOTE: the window size should always be -MAX_WBITS normally -15. |
|
465 |
// unzip/zipup.c and java/Deflater.c |
|
466 |
||
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
467 |
int error = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, |
2 | 468 |
-MAX_WBITS, 8, Z_DEFAULT_STRATEGY); |
469 |
if (error != Z_OK) { |
|
470 |
switch (error) { |
|
471 |
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
|
472 |
PRINTCR((2, "Error: deflate error : Out of memory \n")); |
2 | 473 |
break; |
474 |
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
|
475 |
PRINTCR((2,"Error: deflate error : Invalid compression level \n")); |
2 | 476 |
break; |
477 |
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
|
478 |
PRINTCR((2,"Error: deflate error : Invalid version\n")); |
2 | 479 |
break; |
480 |
default: |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
481 |
PRINTCR((2,"Error: Internal deflate error error = %d\n", error)); |
2 | 482 |
} |
483 |
return false; |
|
484 |
} |
|
485 |
||
486 |
deflated.empty(); |
|
20797 | 487 |
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
|
488 |
zs.avail_out = (int)deflated.size(); |
2 | 489 |
|
490 |
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
|
491 |
zs.avail_in = (int)head.len; |
2 | 492 |
|
493 |
bytes* first = &head; |
|
494 |
bytes* last = &tail; |
|
495 |
if (last->len == 0) { |
|
496 |
first = null; |
|
497 |
last = &head; |
|
498 |
} else if (first->len == 0) { |
|
499 |
first = null; |
|
500 |
} |
|
501 |
||
502 |
if (first != null && error == Z_OK) { |
|
503 |
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
|
504 |
zs.avail_in = (int)first->len; |
2 | 505 |
error = deflate(&zs, Z_NO_FLUSH); |
506 |
} |
|
507 |
if (error == Z_OK) { |
|
508 |
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
|
509 |
zs.avail_in = (int)last->len; |
2 | 510 |
error = deflate(&zs, Z_FINISH); |
511 |
} |
|
512 |
if (error == Z_STREAM_END) { |
|
22575
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
513 |
if ((int)zs.total_out > 0) { |
0c2768190cf4
8029646: [pack200] should support the new zip64 format.
kizune
parents:
21428
diff
changeset
|
514 |
// Even if compressed size is bigger than uncompressed, write it |
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
515 |
PRINTCR((2, "deflate compressed data %d -> %d\n", len, zs.total_out)); |
2 | 516 |
deflated.b.len = zs.total_out; |
517 |
deflateEnd(&zs); |
|
518 |
return true; |
|
519 |
} |
|
1082
53833ff90c45
6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents:
2
diff
changeset
|
520 |
PRINTCR((2, "deflate expanded data %d -> %d\n", len, zs.total_out)); |
2 | 521 |
deflateEnd(&zs); |
522 |
return false; |
|
523 |
} |
|
524 |
||
525 |
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
|
526 |
PRINTCR((2, "Error: deflate error deflate did not finish error=%d\n",error)); |
2 | 527 |
return false; |
528 |
} |
|
529 |
||
530 |
// Callback for fetching data from a GZIP input stream |
|
531 |
static jlong read_input_via_gzip(unpacker* u, |
|
532 |
void* buf, jlong minlen, jlong maxlen) { |
|
533 |
assert(minlen <= maxlen); // don't talk nonsense |
|
534 |
jlong numread = 0; |
|
535 |
char* bufptr = (char*) buf; |
|
536 |
char* inbuf = u->gzin->inbuf; |
|
537 |
size_t inbuflen = sizeof(u->gzin->inbuf); |
|
538 |
unpacker::read_input_fn_t read_gzin_fn = |
|
539 |
(unpacker::read_input_fn_t) u->gzin->read_input_fn; |
|
540 |
z_stream& zs = *(z_stream*) u->gzin->zstream; |
|
541 |
while (numread < minlen) { |
|
542 |
int readlen = (1 << 16); // pretty arbitrary |
|
543 |
if (readlen > (maxlen - numread)) |
|
544 |
readlen = (int)(maxlen - numread); |
|
545 |
zs.next_out = (uchar*) bufptr; |
|
546 |
zs.avail_out = readlen; |
|
547 |
if (zs.avail_in == 0) { |
|
548 |
zs.avail_in = (int) read_gzin_fn(u, inbuf, 1, inbuflen); |
|
549 |
zs.next_in = (uchar*) inbuf; |
|
550 |
} |
|
551 |
int error = inflate(&zs, Z_NO_FLUSH); |
|
552 |
if (error != Z_OK && error != Z_STREAM_END) { |
|
553 |
u->abort("error inflating input"); |
|
554 |
break; |
|
555 |
} |
|
556 |
int nr = readlen - zs.avail_out; |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
557 |
u->gzin->gzlen += nr; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
558 |
u->gzin->gzcrc = crc32(u->gzin->gzcrc, (const unsigned char *)bufptr, nr); |
2 | 559 |
numread += nr; |
560 |
bufptr += nr; |
|
561 |
assert(numread <= maxlen); |
|
562 |
if (error == Z_STREAM_END) { |
|
563 |
enum { TRAILER_LEN = 8 }; |
|
564 |
// skip 8-byte trailer |
|
565 |
if (zs.avail_in >= TRAILER_LEN) { |
|
566 |
zs.avail_in -= TRAILER_LEN; |
|
567 |
} else { |
|
568 |
// Bug: 5023768,we read past the TRAILER_LEN to see if there is |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
569 |
// any extraneous data, as we don't support concatenated .gz files. |
2 | 570 |
int extra = (int) read_gzin_fn(u, inbuf, 1, inbuflen); |
571 |
zs.avail_in += extra - TRAILER_LEN; |
|
572 |
} |
|
573 |
// %%% should check for concatenated *.gz files here |
|
574 |
if (zs.avail_in > 0) |
|
575 |
u->abort("garbage after end of deflated input stream"); |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
576 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
577 |
// at this point we know there are no trailing bytes, |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
578 |
// we are safe to get the crc and len. |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
579 |
if (u->gzin->gzcrc != 0) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
580 |
// Read the CRC information from the gzip container |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
581 |
fseek(u->infileptr, -TRAILER_LEN, SEEK_END); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
582 |
uint filecrc; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
583 |
uint filelen; |
50884 | 584 |
if (fread(&filecrc, sizeof(filecrc), 1, u->infileptr) != 1) { |
585 |
fprintf(u->errstrm, "Error:reading CRC information on input file failed err=%d\n",errno); |
|
586 |
exit(1); |
|
587 |
} |
|
588 |
if (fread(&filelen, sizeof(filelen), 1, u->infileptr) != 1) { |
|
589 |
fprintf(u->errstrm, "Error:reading file length on input file failed err=%d\n",errno); |
|
590 |
exit(1); |
|
591 |
} |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
592 |
filecrc = SWAP_INT(filecrc); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
593 |
filelen = SWAP_INT(filelen); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
594 |
if (u->gzin->gzcrc != filecrc || |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
595 |
// rfc1952; ISIZE is the input size modulo 2^32 |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
596 |
u->gzin->gzlen != (filelen & 0xffffffff)) { // CRC error |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
597 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
598 |
PRINTCR((1, "crc: 0x%x 0x%x\n", u->gzin->gzcrc, filecrc)); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
599 |
PRINTCR((1, "len: 0x%x 0x%x\n", u->gzin->gzlen, filelen)); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
600 |
|
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
601 |
if (u->jarout != null) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
602 |
// save the file name first, if any |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
603 |
const char* outfile = u->jarout->jarname; |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
604 |
u->jarout->closeJarFile(false); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
605 |
if (outfile != null) { |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
606 |
remove(outfile); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
607 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
608 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
609 |
// Print out the error and exit with return code != 0 |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
610 |
u->abort("CRC error, invalid compressed data."); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
611 |
} |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
612 |
} |
2 | 613 |
// pop this filter off: |
614 |
u->gzin->free(); |
|
615 |
break; |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
//fprintf(u->errstrm, "readInputFn(%d,%d) => %d (gunzip)\n", |
|
620 |
// (int)minlen, (int)maxlen, (int)numread); |
|
621 |
return numread; |
|
622 |
} |
|
623 |
||
624 |
void gunzip::init(unpacker* u_) { |
|
625 |
BYTES_OF(*this).clear(); |
|
626 |
u = u_; |
|
627 |
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
|
628 |
read_input_fn = (void*)u->read_input_fn; |
2 | 629 |
zstream = NEW(z_stream, 1); |
630 |
u->gzin = this; |
|
631 |
u->read_input_fn = read_input_via_gzip; |
|
36960
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
632 |
u->gzin->gzcrc = crc32(0, Z_NULL, 0); |
d7731fdfe7c3
8150469: unpack200 fails to compare crc correctly.
ksrini
parents:
29591
diff
changeset
|
633 |
u->gzin->gzlen = 0; |
2 | 634 |
} |
635 |
||
636 |
void gunzip::start(int magic) { |
|
637 |
assert((magic & GZIP_MAGIC_MASK) == GZIP_MAGIC); |
|
638 |
int gz_flg = (magic & 0xFF); // keep "flg", discard other 3 bytes |
|
639 |
enum { |
|
640 |
FHCRC = (1<<1), |
|
641 |
FEXTRA = (1<<2), |
|
642 |
FNAME = (1<<3), |
|
643 |
FCOMMENT = (1<<4) |
|
644 |
}; |
|
645 |
char gz_mtime[4]; |
|
646 |
char gz_xfl[1]; |
|
647 |
char gz_os[1]; |
|
648 |
char gz_extra_len[2]; |
|
649 |
char gz_hcrc[2]; |
|
650 |
char gz_ignore; |
|
651 |
// do not save extra, name, comment |
|
652 |
read_fixed_field(gz_mtime, sizeof(gz_mtime)); |
|
653 |
read_fixed_field(gz_xfl, sizeof(gz_xfl)); |
|
654 |
read_fixed_field(gz_os, sizeof(gz_os)); |
|
655 |
if (gz_flg & FEXTRA) { |
|
656 |
read_fixed_field(gz_extra_len, sizeof(gz_extra_len)); |
|
657 |
int extra_len = gz_extra_len[0] & 0xFF; |
|
658 |
extra_len += (gz_extra_len[1] & 0xFF) << 8; |
|
659 |
for (; extra_len > 0; extra_len--) { |
|
660 |
read_fixed_field(&gz_ignore, 1); |
|
661 |
} |
|
662 |
} |
|
663 |
int null_terms = 0; |
|
664 |
if (gz_flg & FNAME) null_terms++; |
|
665 |
if (gz_flg & FCOMMENT) null_terms++; |
|
666 |
for (; null_terms; null_terms--) { |
|
667 |
for (;;) { |
|
668 |
gz_ignore = 0; |
|
669 |
read_fixed_field(&gz_ignore, 1); |
|
670 |
if (gz_ignore == 0) break; |
|
671 |
} |
|
672 |
} |
|
673 |
if (gz_flg & FHCRC) |
|
674 |
read_fixed_field(gz_hcrc, sizeof(gz_hcrc)); |
|
675 |
||
676 |
if (aborting()) return; |
|
677 |
||
678 |
// now the input stream is ready to read into the inflater |
|
679 |
int error = inflateInit2((z_stream*) zstream, -MAX_WBITS); |
|
680 |
if (error != Z_OK) { abort("cannot create input"); return; } |
|
681 |
} |
|
682 |
||
683 |
void gunzip::free() { |
|
684 |
assert(u->gzin == this); |
|
685 |
u->gzin = null; |
|
686 |
u->read_input_fn = (unpacker::read_input_fn_t) this->read_input_fn; |
|
687 |
inflateEnd((z_stream*) zstream); |
|
688 |
mtrace('f', zstream, 0); |
|
689 |
::free(zstream); |
|
690 |
zstream = null; |
|
691 |
mtrace('f', this, 0); |
|
692 |
::free(this); |
|
693 |
} |
|
694 |
||
695 |
void gunzip::read_fixed_field(char* buf, size_t buflen) { |
|
696 |
if (aborting()) return; |
|
697 |
jlong nr = ((unpacker::read_input_fn_t)read_input_fn) |
|
698 |
(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
|
699 |
if ((size_t)nr != buflen) |
2 | 700 |
u->abort("short stream header"); |
701 |
} |
|
702 |
||
703 |
#else // NO_ZLIB |
|
704 |
||
705 |
void gunzip::free() { |
|
706 |
} |
|
707 |
||
708 |
#endif // NO_ZLIB |