-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUtils.java
More file actions
243 lines (216 loc) · 7.92 KB
/
Utils.java
File metadata and controls
243 lines (216 loc) · 7.92 KB
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
package software.coley.lljzip;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import software.coley.lljzip.util.MemorySegmentUtil;
import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.foreign.MemorySegment;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Utilities for zip tests.
*
* @author Matt Coley
*/
public class Utils {
private static final int VERSION_ZIP64 = 45;
private static final int GP_FLAG_DATA_DESCRIPTOR = 0x0008;
private static final long ZIP64_MAGIC_DWORD = 0xFFFFFFFFL;
/**
* Asserts the string has been found and is the <b>ONLY</b> matching string in the class.
*
* @param code
* Class bytecode.
* @param target
* String instance to look for.
*/
public static void assertDefinesString(byte[] code, String target) {
boolean[] visited = new boolean[1];
ClassReader cr = new ClassReader(code);
cr.accept(new ClassVisitor(Opcodes.ASM9) {
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM9) {
@Override
public void visitLdcInsn(Object value) {
visited[0] = true;
assertEquals(target, value);
}
};
}
}, ClassReader.SKIP_FRAMES);
assertTrue(visited[0], "The entry did not visit any LDC constants");
}
/**
* Asserts the string has been found and is the <b>ONLY</b> matching string in the class.
*
* @param code
* Class bytecode.
* @param target
* String instance to look for.
*/
public static void assertDefinesString(MemorySegment code, String target) {
assertDefinesString(MemorySegmentUtil.toByteArray(code), target);
}
@Nonnull
static byte[] zip64OffsetArchive() throws IOException {
List<EntrySpec> entries = List.of(
new EntrySpec("alpha.txt", "alpha".getBytes(StandardCharsets.UTF_8), false, false),
new EntrySpec("beta.txt", "second-entry".getBytes(StandardCharsets.UTF_8), false, false)
);
return createArchive(entries, false);
}
@Nonnull
static byte[] zip64DataDescriptorArchive(boolean includeDescriptorSignature) throws IOException {
List<EntrySpec> entries = List.of(
new EntrySpec("descriptor.txt", "descriptor-data".getBytes(StandardCharsets.UTF_8),
true, includeDescriptorSignature)
);
return createArchive(entries, false);
}
@Nonnull
static byte[] splitZip64Archive() throws IOException {
List<EntrySpec> entries = List.of(
new EntrySpec("split.txt", "split-archive".getBytes(StandardCharsets.UTF_8), false, false)
);
return createArchive(entries, true);
}
@Nonnull
private static byte[] createArchive(@Nonnull List<EntrySpec> entries, boolean splitArchive) throws IOException {
ByteArrayOutputStream localOut = new ByteArrayOutputStream();
List<EntryMeta> metas = new ArrayList<>();
for (EntrySpec entry : entries) {
byte[] name = entry.name().getBytes(StandardCharsets.UTF_8);
long localOffset = localOut.size();
long crc32 = crc32(entry.data());
byte[] localExtra = localZip64Extra(entry.data().length);
int generalPurposeBitFlag = entry.useDataDescriptor() ? GP_FLAG_DATA_DESCRIPTOR : 0;
writeIntLE(localOut, 0x04034B50);
writeShortLE(localOut, VERSION_ZIP64);
writeShortLE(localOut, generalPurposeBitFlag);
writeShortLE(localOut, 0);
writeShortLE(localOut, 0);
writeShortLE(localOut, 0);
writeIntLE(localOut, entry.useDataDescriptor() ? 0L : crc32);
writeIntLE(localOut, ZIP64_MAGIC_DWORD);
writeIntLE(localOut, ZIP64_MAGIC_DWORD);
writeShortLE(localOut, name.length);
writeShortLE(localOut, localExtra.length);
localOut.write(name);
localOut.write(localExtra);
localOut.write(entry.data());
if (entry.useDataDescriptor()) {
if (entry.includeDescriptorSignature())
writeIntLE(localOut, 0x08074B50);
writeIntLE(localOut, crc32);
writeLongLE(localOut, entry.data().length);
writeLongLE(localOut, entry.data().length);
}
metas.add(new EntryMeta(entry, name, localOffset, crc32));
}
long centralDirectoryOffset = localOut.size();
ByteArrayOutputStream centralOut = new ByteArrayOutputStream();
for (EntryMeta meta : metas) {
byte[] centralExtra = centralZip64Extra(meta.entry().data().length, meta.localOffset());
writeIntLE(centralOut, 0x02014B50);
writeShortLE(centralOut, VERSION_ZIP64);
writeShortLE(centralOut, VERSION_ZIP64);
writeShortLE(centralOut, meta.entry().useDataDescriptor() ? GP_FLAG_DATA_DESCRIPTOR : 0);
writeShortLE(centralOut, 0);
writeShortLE(centralOut, 0);
writeShortLE(centralOut, 0);
writeIntLE(centralOut, meta.crc32());
writeIntLE(centralOut, ZIP64_MAGIC_DWORD);
writeIntLE(centralOut, ZIP64_MAGIC_DWORD);
writeShortLE(centralOut, meta.name().length);
writeShortLE(centralOut, centralExtra.length);
writeShortLE(centralOut, 0);
writeShortLE(centralOut, 0);
writeShortLE(centralOut, 0);
writeIntLE(centralOut, 0);
writeIntLE(centralOut, ZIP64_MAGIC_DWORD);
centralOut.write(meta.name());
centralOut.write(centralExtra);
}
long centralDirectorySize = centralOut.size();
localOut.write(centralOut.toByteArray());
long zip64EndOffset = localOut.size();
writeIntLE(localOut, 0x06064B50);
writeLongLE(localOut, 44L);
writeShortLE(localOut, VERSION_ZIP64);
writeShortLE(localOut, VERSION_ZIP64);
writeIntLE(localOut, splitArchive ? 1L : 0L);
writeIntLE(localOut, splitArchive ? 1L : 0L);
writeLongLE(localOut, metas.size());
writeLongLE(localOut, metas.size());
writeLongLE(localOut, centralDirectorySize);
writeLongLE(localOut, centralDirectoryOffset);
writeIntLE(localOut, 0x07064B50);
writeIntLE(localOut, splitArchive ? 1L : 0L);
writeLongLE(localOut, zip64EndOffset);
writeIntLE(localOut, splitArchive ? 2L : 1L);
writeIntLE(localOut, 0x06054B50);
writeShortLE(localOut, splitArchive ? 1 : 0);
writeShortLE(localOut, splitArchive ? 1 : 0);
writeShortLE(localOut, 0xFFFF);
writeShortLE(localOut, 0xFFFF);
writeIntLE(localOut, ZIP64_MAGIC_DWORD);
writeIntLE(localOut, ZIP64_MAGIC_DWORD);
writeShortLE(localOut, 0);
return localOut.toByteArray();
}
@Nonnull
private static byte[] localZip64Extra(long size) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeShortLE(out, 0x0001);
writeShortLE(out, 16);
writeLongLE(out, size);
writeLongLE(out, size);
return out.toByteArray();
}
@Nonnull
private static byte[] centralZip64Extra(long size, long localOffset) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeShortLE(out, 0x0001);
writeShortLE(out, 24);
writeLongLE(out, size);
writeLongLE(out, size);
writeLongLE(out, localOffset);
return out.toByteArray();
}
private static long crc32(byte[] data) {
CRC32 crc32 = new CRC32();
crc32.update(data);
return crc32.getValue();
}
private static void writeShortLE(ByteArrayOutputStream out, int value) {
out.write(value & 0xFF);
out.write((value >>> 8) & 0xFF);
}
private static void writeIntLE(ByteArrayOutputStream out, long value) {
out.write((int) (value & 0xFF));
out.write((int) ((value >>> 8) & 0xFF));
out.write((int) ((value >>> 16) & 0xFF));
out.write((int) ((value >>> 24) & 0xFF));
}
private static void writeLongLE(ByteArrayOutputStream out, long value) {
writeIntLE(out, value);
writeIntLE(out, value >>> 32);
}
@FunctionalInterface
public interface ThrowingConsumer<T> {
void accept(T value) throws IOException;
}
private record EntrySpec(@Nonnull String name, @Nonnull byte[] data,
boolean useDataDescriptor, boolean includeDescriptorSignature) {
}
private record EntryMeta(@Nonnull EntrySpec entry, @Nonnull byte[] name, long localOffset, long crc32) {
}
}