| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.apache.tools.zip; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.zip.ZipException; |
| 26 |
|
| 27 |
/** |
| 28 |
* ZipExtraField related methods |
| 29 |
* |
| 30 |
*/ |
| 31 |
// CheckStyle:HideUtilityClassConstructorCheck OFF (bc) |
| 32 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 33 |
public class ExtraFieldUtils { |
| 34 |
|
| 35 |
private static final int WORD = 4; |
| 36 |
|
| 37 |
/** |
| 38 |
* Static registry of known extra fields. |
| 39 |
* |
| 40 |
* @since 1.1 |
| 41 |
*/ |
| 42 |
private static final Map implementations; |
| 43 |
|
| 44 |
static { |
| 45 |
implementations = new HashMap(); |
| 46 |
register(AsiExtraField.class); |
| 47 |
register(JarMarker.class); |
| 48 |
register(UnicodePathExtraField.class); |
| 49 |
register(UnicodeCommentExtraField.class); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Register a ZipExtraField implementation. |
| 54 |
* |
| 55 |
* <p>The given class must have a no-arg constructor and implement |
| 56 |
* the {@link ZipExtraField ZipExtraField interface}.</p> |
| 57 |
* @param c the class to register |
| 58 |
* |
| 59 |
* @since 1.1 |
| 60 |
*/ |
| 61 |
public static void register(Class c) { |
| 62 |
try { |
| 63 |
ZipExtraField ze = (ZipExtraField) c.newInstance(); |
| 64 |
implementations.put(ze.getHeaderId(), c); |
| 65 |
} catch (ClassCastException cc) { |
| 66 |
throw new RuntimeException(c + " doesn\'t implement ZipExtraField"); |
| 67 |
} catch (InstantiationException ie) { |
| 68 |
throw new RuntimeException(c + " is not a concrete class"); |
| 69 |
} catch (IllegalAccessException ie) { |
| 70 |
throw new RuntimeException(c + "\'s no-arg constructor is not public"); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Create an instance of the approriate ExtraField, falls back to |
| 76 |
* {@link UnrecognizedExtraField UnrecognizedExtraField}. |
| 77 |
* @param headerId the header identifier |
| 78 |
* @return an instance of the appropiate ExtraField |
| 79 |
* @exception InstantiationException if unable to instantiate the class |
| 80 |
* @exception IllegalAccessException if not allowed to instatiate the class |
| 81 |
* @since 1.1 |
| 82 |
*/ |
| 83 |
public static ZipExtraField createExtraField(ZipShort headerId) |
| 84 |
throws InstantiationException, IllegalAccessException { |
| 85 |
Class c = (Class) implementations.get(headerId); |
| 86 |
if (c != null) { |
| 87 |
return (ZipExtraField) c.newInstance(); |
| 88 |
} |
| 89 |
UnrecognizedExtraField u = new UnrecognizedExtraField(); |
| 90 |
u.setHeaderId(headerId); |
| 91 |
return u; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Split the array into ExtraFields and populate them with the |
| 96 |
* given data as local file data, throwing an exception if the |
| 97 |
* data cannot be parsed. |
| 98 |
* @param data an array of bytes as it appears in local file data |
| 99 |
* @return an array of ExtraFields |
| 100 |
* @throws ZipException on error |
| 101 |
*/ |
| 102 |
public static ZipExtraField[] parse(byte[] data) throws ZipException { |
| 103 |
return parse(data, true, UnparseableExtraField.THROW); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Split the array into ExtraFields and populate them with the |
| 108 |
* given data, throwing an exception if the data cannot be parsed. |
| 109 |
* @param data an array of bytes |
| 110 |
* @param local whether data originates from the local file data |
| 111 |
* or the central directory |
| 112 |
* @return an array of ExtraFields |
| 113 |
* @since 1.1 |
| 114 |
* @throws ZipException on error |
| 115 |
*/ |
| 116 |
public static ZipExtraField[] parse(byte[] data, boolean local) |
| 117 |
throws ZipException { |
| 118 |
return parse(data, local, UnparseableExtraField.THROW); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Split the array into ExtraFields and populate them with the |
| 123 |
* given data. |
| 124 |
* @param data an array of bytes |
| 125 |
* @param local whether data originates from the local file data |
| 126 |
* or the central directory |
| 127 |
* @param onUnparseableData what to do if the extra field data |
| 128 |
* cannot be parsed. |
| 129 |
* @return an array of ExtraFields |
| 130 |
* @throws ZipException on error |
| 131 |
* @since Ant 1.8.1 |
| 132 |
*/ |
| 133 |
public static ZipExtraField[] parse(byte[] data, boolean local, |
| 134 |
UnparseableExtraField onUnparseableData) |
| 135 |
throws ZipException { |
| 136 |
List v = new ArrayList(); |
| 137 |
int start = 0; |
| 138 |
LOOP: |
| 139 |
while (start <= data.length - WORD) { |
| 140 |
ZipShort headerId = new ZipShort(data, start); |
| 141 |
int length = (new ZipShort(data, start + 2)).getValue(); |
| 142 |
if (start + WORD + length > data.length) { |
| 143 |
switch(onUnparseableData.getKey()) { |
| 144 |
case UnparseableExtraField.THROW_KEY: |
| 145 |
throw new ZipException("bad extra field starting at " |
| 146 |
+ start + ". Block length of " |
| 147 |
+ length + " bytes exceeds remaining" |
| 148 |
+ " data of " |
| 149 |
+ (data.length - start - WORD) |
| 150 |
+ " bytes."); |
| 151 |
case UnparseableExtraField.READ_KEY: |
| 152 |
UnparseableExtraFieldData field = |
| 153 |
new UnparseableExtraFieldData(); |
| 154 |
if (local) { |
| 155 |
field.parseFromLocalFileData(data, start, |
| 156 |
data.length - start); |
| 157 |
} else { |
| 158 |
field.parseFromCentralDirectoryData(data, start, |
| 159 |
data.length - start); |
| 160 |
} |
| 161 |
v.add(field); |
| 162 |
/*FALLTHROUGH*/ |
| 163 |
case UnparseableExtraField.SKIP_KEY: |
| 164 |
// since we cannot parse the data we must assume |
| 165 |
// the extra field consumes the whole rest of the |
| 166 |
// available data |
| 167 |
break LOOP; |
| 168 |
default: |
| 169 |
throw new ZipException("unknown UnparseableExtraField key: " |
| 170 |
+ onUnparseableData.getKey()); |
| 171 |
} |
| 172 |
} |
| 173 |
try { |
| 174 |
ZipExtraField ze = createExtraField(headerId); |
| 175 |
if (local |
| 176 |
|| !(ze instanceof CentralDirectoryParsingZipExtraField)) { |
| 177 |
ze.parseFromLocalFileData(data, start + WORD, length); |
| 178 |
} else { |
| 179 |
((CentralDirectoryParsingZipExtraField) ze) |
| 180 |
.parseFromCentralDirectoryData(data, start + WORD, |
| 181 |
length); |
| 182 |
} |
| 183 |
v.add(ze); |
| 184 |
} catch (InstantiationException ie) { |
| 185 |
throw new ZipException(ie.getMessage()); |
| 186 |
} catch (IllegalAccessException iae) { |
| 187 |
throw new ZipException(iae.getMessage()); |
| 188 |
} |
| 189 |
start += (length + WORD); |
| 190 |
} |
| 191 |
|
| 192 |
ZipExtraField[] result = new ZipExtraField[v.size()]; |
| 193 |
return (ZipExtraField[]) v.toArray(result); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Merges the local file data fields of the given ZipExtraFields. |
| 198 |
* @param data an array of ExtraFiles |
| 199 |
* @return an array of bytes |
| 200 |
* @since 1.1 |
| 201 |
*/ |
| 202 |
public static byte[] mergeLocalFileDataData(ZipExtraField[] data) { |
| 203 |
final boolean lastIsUnparseableHolder = data.length > 0 |
| 204 |
&& data[data.length - 1] instanceof UnparseableExtraFieldData; |
| 205 |
int regularExtraFieldCount = |
| 206 |
lastIsUnparseableHolder ? data.length - 1 : data.length; |
| 207 |
|
| 208 |
int sum = WORD * regularExtraFieldCount; |
| 209 |
for (int i = 0; i < data.length; i++) { |
| 210 |
sum += data[i].getLocalFileDataLength().getValue(); |
| 211 |
} |
| 212 |
|
| 213 |
byte[] result = new byte[sum]; |
| 214 |
int start = 0; |
| 215 |
for (int i = 0; i < regularExtraFieldCount; i++) { |
| 216 |
System.arraycopy(data[i].getHeaderId().getBytes(), |
| 217 |
0, result, start, 2); |
| 218 |
System.arraycopy(data[i].getLocalFileDataLength().getBytes(), |
| 219 |
0, result, start + 2, 2); |
| 220 |
byte[] local = data[i].getLocalFileDataData(); |
| 221 |
System.arraycopy(local, 0, result, start + WORD, local.length); |
| 222 |
start += (local.length + WORD); |
| 223 |
} |
| 224 |
if (lastIsUnparseableHolder) { |
| 225 |
byte[] local = data[data.length - 1].getLocalFileDataData(); |
| 226 |
System.arraycopy(local, 0, result, start, local.length); |
| 227 |
} |
| 228 |
return result; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Merges the central directory fields of the given ZipExtraFields. |
| 233 |
* @param data an array of ExtraFields |
| 234 |
* @return an array of bytes |
| 235 |
* @since 1.1 |
| 236 |
*/ |
| 237 |
public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) { |
| 238 |
final boolean lastIsUnparseableHolder = data.length > 0 |
| 239 |
&& data[data.length - 1] instanceof UnparseableExtraFieldData; |
| 240 |
int regularExtraFieldCount = |
| 241 |
lastIsUnparseableHolder ? data.length - 1 : data.length; |
| 242 |
|
| 243 |
int sum = WORD * regularExtraFieldCount; |
| 244 |
for (int i = 0; i < data.length; i++) { |
| 245 |
sum += data[i].getCentralDirectoryLength().getValue(); |
| 246 |
} |
| 247 |
byte[] result = new byte[sum]; |
| 248 |
int start = 0; |
| 249 |
for (int i = 0; i < regularExtraFieldCount; i++) { |
| 250 |
System.arraycopy(data[i].getHeaderId().getBytes(), |
| 251 |
0, result, start, 2); |
| 252 |
System.arraycopy(data[i].getCentralDirectoryLength().getBytes(), |
| 253 |
0, result, start + 2, 2); |
| 254 |
byte[] local = data[i].getCentralDirectoryData(); |
| 255 |
System.arraycopy(local, 0, result, start + WORD, local.length); |
| 256 |
start += (local.length + WORD); |
| 257 |
} |
| 258 |
if (lastIsUnparseableHolder) { |
| 259 |
byte[] local = data[data.length - 1].getCentralDirectoryData(); |
| 260 |
System.arraycopy(local, 0, result, start, local.length); |
| 261 |
} |
| 262 |
return result; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* "enum" for the possible actions to take if the extra field |
| 267 |
* cannot be parsed. |
| 268 |
*/ |
| 269 |
public static final class UnparseableExtraField { |
| 270 |
/** |
| 271 |
* Key for "throw an exception" action. |
| 272 |
*/ |
| 273 |
public static final int THROW_KEY = 0; |
| 274 |
/** |
| 275 |
* Key for "skip" action. |
| 276 |
*/ |
| 277 |
public static final int SKIP_KEY = 1; |
| 278 |
/** |
| 279 |
* Key for "read" action. |
| 280 |
*/ |
| 281 |
public static final int READ_KEY = 2; |
| 282 |
|
| 283 |
/** |
| 284 |
* Throw an exception if field cannot be parsed. |
| 285 |
*/ |
| 286 |
public static final UnparseableExtraField THROW |
| 287 |
= new UnparseableExtraField(THROW_KEY); |
| 288 |
|
| 289 |
/** |
| 290 |
* Skip the extra field entirely and don't make its data |
| 291 |
* available - effectively removing the extra field data. |
| 292 |
*/ |
| 293 |
public static final UnparseableExtraField SKIP |
| 294 |
= new UnparseableExtraField(SKIP_KEY); |
| 295 |
|
| 296 |
/** |
| 297 |
* Read the extra field data into an instance of {@link |
| 298 |
* UnparseableExtraFieldData UnparseableExtraFieldData}. |
| 299 |
*/ |
| 300 |
public static final UnparseableExtraField READ |
| 301 |
= new UnparseableExtraField(READ_KEY); |
| 302 |
|
| 303 |
private final int key; |
| 304 |
|
| 305 |
private UnparseableExtraField(int k) { |
| 306 |
key = k; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Key of the action to take. |
| 311 |
*/ |
| 312 |
public int getKey() { return key; } |
| 313 |
} |
| 314 |
} |