-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
CentralDirectoryEndRecord.java
258 lines (211 loc) · 8.37 KB
/
CentralDirectoryEndRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.jar;
import java.io.IOException;
import org.springframework.boot.loader.data.RandomAccessData;
/**
* A ZIP File "End of central directory record" (EOCD).
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Camille Vienot
* @see <a href="https://en.wikipedia.org/wiki/Zip_%28file_format%29">Zip File Format</a>
*/
class CentralDirectoryEndRecord {
private static final int MINIMUM_SIZE = 22;
private static final int MAXIMUM_COMMENT_LENGTH = 0xFFFF;
private static final int MAXIMUM_SIZE = MINIMUM_SIZE + MAXIMUM_COMMENT_LENGTH;
private static final int SIGNATURE = 0x06054b50;
private static final int COMMENT_LENGTH_OFFSET = 20;
private static final int READ_BLOCK_SIZE = 256;
private final Zip64End zip64End;
private byte[] block;
private int offset;
private int size;
/**
* Create a new {@link CentralDirectoryEndRecord} instance from the specified
* {@link RandomAccessData}, searching backwards from the end until a valid block is
* located.
* @param data the source data
* @throws IOException in case of I/O errors
*/
CentralDirectoryEndRecord(RandomAccessData data) throws IOException {
this.block = createBlockFromEndOfData(data, READ_BLOCK_SIZE);
this.size = MINIMUM_SIZE;
this.offset = this.block.length - this.size;
while (!isValid()) {
this.size++;
if (this.size > this.block.length) {
if (this.size >= MAXIMUM_SIZE || this.size > data.getSize()) {
throw new IOException(
"Unable to find ZIP central directory records after reading " + this.size + " bytes");
}
this.block = createBlockFromEndOfData(data, this.size + READ_BLOCK_SIZE);
}
this.offset = this.block.length - this.size;
}
long startOfCentralDirectoryEndRecord = data.getSize() - this.size;
Zip64Locator zip64Locator = Zip64Locator.find(data, startOfCentralDirectoryEndRecord);
this.zip64End = (zip64Locator != null) ? new Zip64End(data, zip64Locator) : null;
}
private byte[] createBlockFromEndOfData(RandomAccessData data, int size) throws IOException {
int length = (int) Math.min(data.getSize(), size);
return data.read(data.getSize() - length, length);
}
private boolean isValid() {
if (this.block.length < MINIMUM_SIZE || Bytes.littleEndianValue(this.block, this.offset + 0, 4) != SIGNATURE) {
return false;
}
// Total size must be the structure size + comment
long commentLength = Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
return this.size == MINIMUM_SIZE + commentLength;
}
/**
* Returns the location in the data that the archive actually starts. For most files
* the archive data will start at 0, however, it is possible to have prefixed bytes
* (often used for startup scripts) at the beginning of the data.
* @param data the source data
* @return the offset within the data where the archive begins
*/
long getStartOfArchive(RandomAccessData data) {
long length = Bytes.littleEndianValue(this.block, this.offset + 12, 4);
long specifiedOffset = (this.zip64End != null) ? this.zip64End.centralDirectoryOffset
: Bytes.littleEndianValue(this.block, this.offset + 16, 4);
long zip64EndSize = (this.zip64End != null) ? this.zip64End.getSize() : 0L;
int zip64LocSize = (this.zip64End != null) ? Zip64Locator.ZIP64_LOCSIZE : 0;
long actualOffset = data.getSize() - this.size - length - zip64EndSize - zip64LocSize;
return actualOffset - specifiedOffset;
}
/**
* Return the bytes of the "Central directory" based on the offset indicated in this
* record.
* @param data the source data
* @return the central directory data
*/
RandomAccessData getCentralDirectory(RandomAccessData data) {
if (this.zip64End != null) {
return this.zip64End.getCentralDirectory(data);
}
long offset = Bytes.littleEndianValue(this.block, this.offset + 16, 4);
long length = Bytes.littleEndianValue(this.block, this.offset + 12, 4);
return data.getSubsection(offset, length);
}
/**
* Return the number of ZIP entries in the file.
* @return the number of records in the zip
*/
int getNumberOfRecords() {
if (this.zip64End != null) {
return this.zip64End.getNumberOfRecords();
}
long numberOfRecords = Bytes.littleEndianValue(this.block, this.offset + 10, 2);
return (int) numberOfRecords;
}
String getComment() {
int commentLength = (int) Bytes.littleEndianValue(this.block, this.offset + COMMENT_LENGTH_OFFSET, 2);
AsciiBytes comment = new AsciiBytes(this.block, this.offset + COMMENT_LENGTH_OFFSET + 2, commentLength);
return comment.toString();
}
boolean isZip64() {
return this.zip64End != null;
}
/**
* A Zip64 end of central directory record.
*
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
* 4.3.14 of Zip64 specification</a>
*/
private static final class Zip64End {
private static final int ZIP64_ENDTOT = 32; // total number of entries
private static final int ZIP64_ENDSIZ = 40; // central directory size in bytes
private static final int ZIP64_ENDOFF = 48; // offset of first CEN header
private final Zip64Locator locator;
private final long centralDirectoryOffset;
private final long centralDirectoryLength;
private final int numberOfRecords;
private Zip64End(RandomAccessData data, Zip64Locator locator) throws IOException {
this.locator = locator;
byte[] block = data.read(locator.getZip64EndOffset(), 56);
this.centralDirectoryOffset = Bytes.littleEndianValue(block, ZIP64_ENDOFF, 8);
this.centralDirectoryLength = Bytes.littleEndianValue(block, ZIP64_ENDSIZ, 8);
this.numberOfRecords = (int) Bytes.littleEndianValue(block, ZIP64_ENDTOT, 8);
}
/**
* Return the size of this zip 64 end of central directory record.
* @return size of this zip 64 end of central directory record
*/
private long getSize() {
return this.locator.getZip64EndSize();
}
/**
* Return the bytes of the "Central directory" based on the offset indicated in
* this record.
* @param data the source data
* @return the central directory data
*/
private RandomAccessData getCentralDirectory(RandomAccessData data) {
return data.getSubsection(this.centralDirectoryOffset, this.centralDirectoryLength);
}
/**
* Return the number of entries in the zip64 archive.
* @return the number of records in the zip
*/
private int getNumberOfRecords() {
return this.numberOfRecords;
}
}
/**
* A Zip64 end of central directory locator.
*
* @see <a href="https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT">Chapter
* 4.3.15 of Zip64 specification</a>
*/
private static final class Zip64Locator {
static final int SIGNATURE = 0x07064b50;
static final int ZIP64_LOCSIZE = 20; // locator size
static final int ZIP64_LOCOFF = 8; // offset of zip64 end
private final long zip64EndOffset;
private final long offset;
private Zip64Locator(long offset, byte[] block) throws IOException {
this.offset = offset;
this.zip64EndOffset = Bytes.littleEndianValue(block, ZIP64_LOCOFF, 8);
}
/**
* Return the size of the zip 64 end record located by this zip64 end locator.
* @return size of the zip 64 end record located by this zip64 end locator
*/
private long getZip64EndSize() {
return this.offset - this.zip64EndOffset;
}
/**
* Return the offset to locate {@link Zip64End}.
* @return offset of the Zip64 end of central directory record
*/
private long getZip64EndOffset() {
return this.zip64EndOffset;
}
private static Zip64Locator find(RandomAccessData data, long centralDirectoryEndOffset) throws IOException {
long offset = centralDirectoryEndOffset - ZIP64_LOCSIZE;
if (offset >= 0) {
byte[] block = data.read(offset, ZIP64_LOCSIZE);
if (Bytes.littleEndianValue(block, 0, 4) == SIGNATURE) {
return new Zip64Locator(offset, block);
}
}
return null;
}
}
}