1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.zip.ZipException;
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 public abstract class PKWareExtraHeader implements ZipExtraField {
68
69
70
71
72
73
74 public enum EncryptionAlgorithm {
75 DES(0x6601), RC2pre52(0x6602), TripleDES168(0x6603), TripleDES192(0x6609), AES128(0x660E), AES192(0x660F), AES256(0x6610), RC2(0x6702), RC4(0x6801),
76 UNKNOWN(0xFFFF);
77
78 private static final Map<Integer, EncryptionAlgorithm> codeToEnum;
79
80 static {
81 final Map<Integer, EncryptionAlgorithm> cte = new HashMap<>();
82 for (final EncryptionAlgorithm method : values()) {
83 cte.put(method.getCode(), method);
84 }
85 codeToEnum = Collections.unmodifiableMap(cte);
86 }
87
88
89
90
91
92
93
94 public static EncryptionAlgorithm getAlgorithmByCode(final int code) {
95 return codeToEnum.get(code);
96 }
97
98 private final int code;
99
100
101
102
103 EncryptionAlgorithm(final int code) {
104 this.code = code;
105 }
106
107
108
109
110
111
112 public int getCode() {
113 return code;
114 }
115 }
116
117
118
119
120
121
122 public enum HashAlgorithm {
123 NONE(0), CRC32(1), MD5(0x8003), SHA1(0x8004), RIPEND160(0x8007), SHA256(0x800C), SHA384(0x800D), SHA512(0x800E);
124
125 private static final Map<Integer, HashAlgorithm> codeToEnum;
126
127 static {
128 final Map<Integer, HashAlgorithm> cte = new HashMap<>();
129 for (final HashAlgorithm method : values()) {
130 cte.put(method.getCode(), method);
131 }
132 codeToEnum = Collections.unmodifiableMap(cte);
133 }
134
135
136
137
138
139
140
141 public static HashAlgorithm getAlgorithmByCode(final int code) {
142 return codeToEnum.get(code);
143 }
144
145 private final int code;
146
147
148
149
150 HashAlgorithm(final int code) {
151 this.code = code;
152 }
153
154
155
156
157
158
159 public int getCode() {
160 return code;
161 }
162 }
163
164 private final ZipShort headerId;
165
166
167
168
169 private byte[] localData;
170
171
172
173
174 private byte[] centralData;
175
176 protected PKWareExtraHeader(final ZipShort headerId) {
177 this.headerId = headerId;
178 }
179
180 protected final void assertMinimalLength(final int minimum, final int length) throws ZipException {
181 if (length < minimum) {
182 throw new ZipException(getClass().getName() + " is too short, only " + length + " bytes, expected at least " + minimum);
183 }
184 }
185
186
187
188
189
190
191 @Override
192 public byte[] getCentralDirectoryData() {
193 if (centralData != null) {
194 return ZipUtil.copy(centralData);
195 }
196 return getLocalFileDataData();
197 }
198
199
200
201
202
203
204 @Override
205 public ZipShort getCentralDirectoryLength() {
206 if (centralData != null) {
207 return new ZipShort(centralData.length);
208 }
209 return getLocalFileDataLength();
210 }
211
212
213
214
215
216
217 @Override
218 public ZipShort getHeaderId() {
219 return headerId;
220 }
221
222
223
224
225
226
227 @Override
228 public byte[] getLocalFileDataData() {
229 return ZipUtil.copy(localData);
230 }
231
232
233
234
235
236
237 @Override
238 public ZipShort getLocalFileDataLength() {
239 return new ZipShort(localData != null ? localData.length : 0);
240 }
241
242
243
244
245
246
247
248 @Override
249 public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) throws ZipException {
250 final byte[] tmp = Arrays.copyOfRange(data, offset, offset + length);
251 setCentralDirectoryData(tmp);
252 if (localData == null) {
253 setLocalFileDataData(tmp);
254 }
255 }
256
257
258
259
260
261
262
263 @Override
264 public void parseFromLocalFileData(final byte[] data, final int offset, final int length) throws ZipException {
265 setLocalFileDataData(Arrays.copyOfRange(data, offset, offset + length));
266 }
267
268
269
270
271
272
273 public void setCentralDirectoryData(final byte[] data) {
274 centralData = ZipUtil.copy(data);
275 }
276
277
278
279
280
281
282 public void setLocalFileDataData(final byte[] data) {
283 localData = ZipUtil.copy(data);
284 }
285 }