/
FileMap.java
462 lines (412 loc) · 14 KB
/
FileMap.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package org.robolectric.res.android;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.robolectric.res.android.Asset.toIntExact;
import static org.robolectric.res.android.Util.ALOGV;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Shorts;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class FileMap {
/** ZIP archive central directory end header signature. */
private static final int ENDSIG = 0x6054b50;
private static final int ENDHDR = 22;
/** ZIP64 archive central directory end header signature. */
private static final int ENDSIG64 = 0x6064b50;
/** the maximum size of the end of central directory section in bytes */
private static final int MAXIMUM_ZIP_EOCD_SIZE = 64 * 1024 + ENDHDR;
private ZipFile zipFile;
private ZipEntry zipEntry;
private boolean readOnly;
private int fd;
private boolean isFromZip;
// Create a new mapping on an open file.
//
// Closing the file descriptor does not unmap the pages, so we don't
// claim ownership of the fd.
//
// Returns "false" on failure.
boolean create(String origFileName, int fd, long offset, int length,
boolean readOnly)
{
this.mFileName = origFileName;
this.fd = fd;
this.mDataOffset = offset;
this.readOnly = readOnly;
return true;
}
// #if defined(__MINGW32__)
// int adjust;
// off64_t adjOffset;
// size_t adjLength;
//
// if (mPageSize == -1) {
// SYSTEM_INFO si;
//
// GetSystemInfo( &si );
// mPageSize = si.dwAllocationGranularity;
// }
//
// DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
//
// mFileHandle = (HANDLE) _get_osfhandle(fd);
// mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
// if (mFileMapping == NULL) {
// ALOGE("CreateFileMapping(%s, %" PRIx32 ") failed with error %" PRId32 "\n",
// mFileHandle, protect, GetLastError() );
// return false;
// }
//
// adjust = offset % mPageSize;
// adjOffset = offset - adjust;
// adjLength = length + adjust;
//
// mBasePtr = MapViewOfFile( mFileMapping,
// readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
// 0,
// (DWORD)(adjOffset),
// adjLength );
// if (mBasePtr == NULL) {
// ALOGE("MapViewOfFile(%" PRId64 ", 0x%x) failed with error %" PRId32 "\n",
// adjOffset, adjLength, GetLastError() );
// CloseHandle(mFileMapping);
// mFileMapping = INVALID_HANDLE_VALUE;
// return false;
// }
// #else // !defined(__MINGW32__)
// int prot, flags, adjust;
// off64_t adjOffset;
// size_t adjLength;
//
// void* ptr;
//
// assert(fd >= 0);
// assert(offset >= 0);
// assert(length > 0);
//
// // init on first use
// if (mPageSize == -1) {
// mPageSize = sysconf(_SC_PAGESIZE);
// if (mPageSize == -1) {
// ALOGE("could not get _SC_PAGESIZE\n");
// return false;
// }
// }
//
// adjust = offset % mPageSize;
// adjOffset = offset - adjust;
// adjLength = length + adjust;
//
// flags = MAP_SHARED;
// prot = PROT_READ;
// if (!readOnly)
// prot |= PROT_WRITE;
//
// ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
// if (ptr == MAP_FAILED) {
// ALOGE("mmap(%lld,0x%x) failed: %s\n",
// (long long)adjOffset, adjLength, strerror(errno));
// return false;
// }
// mBasePtr = ptr;
// #endif // !defined(__MINGW32__)
//
// mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
// mBaseLength = adjLength;
// mDataOffset = offset;
// mDataPtr = (char*) mBasePtr + adjust;
// mDataLength = length;
//
// assert(mBasePtr != NULL);
//
// ALOGV("MAP: base %s/0x%x data %s/0x%x\n",
// mBasePtr, mBaseLength, mDataPtr, mDataLength);
//
// return true;
// }
boolean createFromZip(
String origFileName,
ZipFile zipFile,
ZipEntry entry,
long offset,
int length,
boolean readOnly) {
isFromZip = true;
this.zipFile = zipFile;
this.zipEntry = entry;
int prot, flags, adjust;
long adjOffset;
int adjLength;
int ptr;
assert(fd >= 0);
assert(offset >= 0);
// assert(length > 0);
// init on first use
// if (mPageSize == -1) {
// mPageSize = sysconf(_SC_PAGESIZE);
// if (mPageSize == -1) {
// ALOGE("could not get _SC_PAGESIZE\n");
// return false;
// }
// }
// adjust = Math.toIntExact(offset % mPageSize);
// adjOffset = offset - adjust;
// adjLength = length + adjust;
//flags = MAP_SHARED;
//prot = PROT_READ;
//if (!readOnly)
// prot |= PROT_WRITE;
// ptr = mmap(null, adjLength, prot, flags, fd, adjOffset);
// if (ptr == MAP_FAILED) {
// ALOGE("mmap(%lld,0x%x) failed: %s\n",
// (long long)adjOffset, adjLength, strerror(errno));
// return false;
// }
// mBasePtr = ptr;
mFileName = origFileName != null ? origFileName : null;
//mBaseLength = adjLength;
mDataOffset = offset;
//mDataPtr = mBasePtr + adjust;
mDataLength = toIntExact(entry.getSize());
//assert(mBasePtr != 0);
ALOGV("MAP: base %s/0x%x data %s/0x%x\n",
mBasePtr, mBaseLength, mDataPtr, mDataLength);
return true;
}
static ImmutableMap<String, Long> guessDataOffsets(File zipFile, int length) {
ImmutableMap.Builder<String, Long> result = ImmutableMap.builder();
// Parse the zip file entry offsets from the central directory section.
// See https://en.wikipedia.org/wiki/Zip_(file_format)
try (RandomAccessFile randomAccessFile = new RandomAccessFile(zipFile, "r")) {
// First read the 'end of central directory record' in order to find the start of the central
// directory
// The end of central directory record (EOCD) is max comment length (64K) + 22 bytes
int endOfCdSize = Math.min(MAXIMUM_ZIP_EOCD_SIZE, length);
int endofCdOffset = length - endOfCdSize;
randomAccessFile.seek(endofCdOffset);
byte[] buffer = new byte[endOfCdSize];
randomAccessFile.readFully(buffer);
int centralDirOffset = findCentralDir(buffer);
int offset = centralDirOffset - endofCdOffset;
if (offset < 0) {
// read the entire central directory record into memory
// for the framework jars this max of 5MB for Q
// TODO: consider using a smaller buffer size and re-reading as necessary
offset = 0;
randomAccessFile.seek(centralDirOffset);
final int cdSize = length - centralDirOffset;
buffer = new byte[cdSize];
randomAccessFile.readFully(buffer);
} else {
// the central directory is already in the buffer, no need to reread
}
// now read the entries
while (true) {
// Instead of trusting numRecords, read until we find the
// end-of-central-directory signature. numRecords may wrap
// around with >64K entries.
int sig = readInt(buffer, offset);
if (sig == ENDSIG || sig == ENDSIG64) {
break;
}
int bitFlag = readShort(buffer, offset + 8);
int fileNameLength = readShort(buffer, offset + 28);
int extraLength = readShort(buffer, offset + 30);
int fieldCommentLength = readShort(buffer, offset + 32);
int relativeOffsetOfLocalFileHeader = readInt(buffer, offset + 42);
byte[] nameBytes = copyBytes(buffer, offset + 46, fileNameLength);
Charset encoding = getEncoding(bitFlag);
String fileName = new String(nameBytes, encoding);
byte[] localHeaderBuffer = new byte[30];
randomAccessFile.seek(relativeOffsetOfLocalFileHeader);
randomAccessFile.readFully(localHeaderBuffer);
// There are two extra field lengths stored in the zip - one in the central directory,
// one in the local header. And we should use one in local header to calculate the
// correct file content offset, because they are different some times.
int localHeaderExtraLength = readShort(localHeaderBuffer, 28);
int fileOffset =
relativeOffsetOfLocalFileHeader + 30 + fileNameLength + localHeaderExtraLength;
result.put(fileName, (long) fileOffset);
offset += 46 + fileNameLength + extraLength + fieldCommentLength;
}
return result.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static byte[] copyBytes(byte[] buffer, int offset, int length) {
byte[] result = new byte[length];
System.arraycopy(buffer, offset, result, 0, length);
return result;
}
private static Charset getEncoding(int bitFlags) {
// UTF-8 now supported in name and comments: check general bit flag, bit
// 11, to determine if UTF-8 is being used or ISO-8859-1 is being used.
return (0 != ((bitFlags >>> 11) & 1)) ? UTF_8 : ISO_8859_1;
}
private static int findCentralDir(byte[] buffer) throws IOException {
// find start of central directory by scanning backwards
int scanOffset = buffer.length - ENDHDR;
while (true) {
int val = readInt(buffer, scanOffset);
if (val == ENDSIG) {
break;
}
// Ok, keep backing up looking for the ZIP end central directory
// signature.
--scanOffset;
if (scanOffset < 0) {
throw new ZipException("ZIP directory not found, not a ZIP archive.");
}
}
// scanOffset is now start of end of central directory record
// the 'offset to central dir' data is at position 16 in the record
int offsetToCentralDir = readInt(buffer, scanOffset + 16);
return offsetToCentralDir;
}
/** Read a 32-bit integer from a bytebuffer in little-endian order. */
private static int readInt(byte[] buffer, int offset) {
return Ints.fromBytes(
buffer[offset + 3], buffer[offset + 2], buffer[offset + 1], buffer[offset]);
}
/** Read a 16-bit short from a bytebuffer in little-endian order. */
private static short readShort(byte[] buffer, int offset) {
return Shorts.fromBytes(buffer[offset + 1], buffer[offset]);
}
/*
* This represents a memory-mapped file. It might be the entire file or
* only part of it. This requires a little bookkeeping because the mapping
* needs to be aligned on page boundaries, and in some cases we'd like to
* have multiple references to the mapped area without creating additional
* maps.
*
* This always uses MAP_SHARED.
*
* TODO: we should be able to create a new FileMap that is a subset of
* an existing FileMap and shares the underlying mapped pages. Requires
* completing the refcounting stuff and possibly introducing the notion
* of a FileMap hierarchy.
*/
// class FileMap {
// public:
// FileMap(void);
//
// FileMap(FileMap&& f);
// FileMap& operator=(FileMap&& f);
/*
* Create a new mapping on an open file.
*
* Closing the file descriptor does not unmap the pages, so we don't
* claim ownership of the fd.
*
* Returns "false" on failure.
*/
// boolean create(String origFileName, int fd,
// long offset, int length, boolean readOnly) {
// }
// ~FileMap(void);
/*
* Return the name of the file this map came from, if known.
*/
String getFileName() { return mFileName; }
/*
* Get a pointer to the piece of the file we requested.
*/
synchronized byte[] getDataPtr() {
if (mDataPtr == null) {
mDataPtr = new byte[mDataLength];
InputStream is;
try {
if (isFromZip) {
is = zipFile.getInputStream(zipEntry);
} else {
is = new FileInputStream(getFileName());
}
try {
readFully(is, mDataPtr);
} finally {
is.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return mDataPtr;
}
public static void readFully(InputStream is, byte[] bytes) throws IOException {
int size = bytes.length;
int remaining = size;
while (remaining > 0) {
int location = size - remaining;
int bytesRead = is.read(bytes, location, remaining);
if (bytesRead == -1) {
break;
}
remaining -= bytesRead;
}
if (remaining > 0) {
throw new RuntimeException("failed to read " + size + " (" + remaining + " bytes unread)");
}
}
/*
* Get the length we requested.
*/
int getDataLength() { return mDataLength; }
/*
* Get the data offset used to create this map.
*/
long getDataOffset() { return mDataOffset; }
public ZipEntry getZipEntry() {
return zipEntry;
}
// /*
// * This maps directly to madvise() values, but allows us to avoid
// * including <sys/mman.h> everywhere.
// */
// enum MapAdvice {
// NORMAL, RANDOM, SEQUENTIAL, WILLNEED, DONTNEED
// };
//
// /*
// * Apply an madvise() call to the entire file.
// *
// * Returns 0 on success, -1 on failure.
// */
// int advise(MapAdvice advice);
//
// protected:
//
// private:
// // these are not implemented
// FileMap(const FileMap& src);
// const FileMap& operator=(const FileMap& src);
//
String mFileName; // original file name, if known
int mBasePtr; // base of mmap area; page aligned
int mBaseLength; // length, measured from "mBasePtr"
long mDataOffset; // offset used when map was created
byte[] mDataPtr; // start of requested data, offset from base
int mDataLength; // length, measured from "mDataPtr"
static long mPageSize;
@Override
public String toString() {
if (isFromZip) {
return "FileMap{" +
"zipFile=" + zipFile.getName() +
", zipEntry=" + zipEntry +
'}';
} else {
return "FileMap{" +
"mFileName='" + mFileName + '\'' +
'}';
}
}
}