001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.zip;
019
020import java.util.ArrayList;
021import java.util.List;
022import java.util.Map;
023import java.util.concurrent.ConcurrentHashMap;
024import java.util.zip.ZipException;
025
026/**
027 * ZipExtraField related methods
028 * @NotThreadSafe because the HashMap is not synch.
029 */
030// CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
031public class ExtraFieldUtils {
032
033    private static final int WORD = 4;
034
035    /**
036     * Static registry of known extra fields.
037     */
038    private static final Map<ZipShort, Class<?>> implementations;
039
040    static {
041        implementations = new ConcurrentHashMap<>();
042        register(AsiExtraField.class);
043        register(X5455_ExtendedTimestamp.class);
044        register(X7875_NewUnix.class);
045        register(JarMarker.class);
046        register(UnicodePathExtraField.class);
047        register(UnicodeCommentExtraField.class);
048        register(Zip64ExtendedInformationExtraField.class);
049        register(X000A_NTFS.class);
050        register(X0014_X509Certificates.class);
051        register(X0015_CertificateIdForFile.class);
052        register(X0016_CertificateIdForCentralDirectory.class);
053        register(X0017_StrongEncryptionHeader.class);
054        register(X0019_EncryptionRecipientCertificateList.class);
055        register(ResourceAlignmentExtraField.class);
056    }
057
058    /**
059     * Register a ZipExtraField implementation.
060     *
061     * <p>The given class must have a no-arg constructor and implement
062     * the {@link ZipExtraField ZipExtraField interface}.</p>
063     * @param c the class to register
064     */
065    public static void register(final Class<?> c) {
066        try {
067            final ZipExtraField ze = (ZipExtraField) c.newInstance();
068            implementations.put(ze.getHeaderId(), c);
069        } catch (final ClassCastException cc) {
070            throw new RuntimeException(c + " doesn\'t implement ZipExtraField"); //NOSONAR
071        } catch (final InstantiationException ie) {
072            throw new RuntimeException(c + " is not a concrete class"); //NOSONAR
073        } catch (final IllegalAccessException ie) {
074            throw new RuntimeException(c + "\'s no-arg constructor is not public"); //NOSONAR
075        }
076    }
077
078    /**
079     * Create an instance of the appropriate ExtraField, falls back to
080     * {@link UnrecognizedExtraField UnrecognizedExtraField}.
081     * @param headerId the header identifier
082     * @return an instance of the appropriate ExtraField
083     * @throws InstantiationException if unable to instantiate the class
084     * @throws IllegalAccessException if not allowed to instantiate the class
085     */
086    public static ZipExtraField createExtraField(final ZipShort headerId)
087        throws InstantiationException, IllegalAccessException {
088        final Class<?> c = implementations.get(headerId);
089        if (c != null) {
090            return (ZipExtraField) c.newInstance();
091        }
092        final UnrecognizedExtraField u = new UnrecognizedExtraField();
093        u.setHeaderId(headerId);
094        return u;
095    }
096
097    /**
098     * Split the array into ExtraFields and populate them with the
099     * given data as local file data, throwing an exception if the
100     * data cannot be parsed.
101     * @param data an array of bytes as it appears in local file data
102     * @return an array of ExtraFields
103     * @throws ZipException on error
104     */
105    public static ZipExtraField[] parse(final byte[] data) throws ZipException {
106        return parse(data, true, UnparseableExtraField.THROW);
107    }
108
109    /**
110     * Split the array into ExtraFields and populate them with the
111     * given data, throwing an exception if the data cannot be parsed.
112     * @param data an array of bytes
113     * @param local whether data originates from the local file data
114     * or the central directory
115     * @return an array of ExtraFields
116     * @throws ZipException on error
117     */
118    public static ZipExtraField[] parse(final byte[] data, final boolean local)
119        throws ZipException {
120        return parse(data, local, UnparseableExtraField.THROW);
121    }
122
123    /**
124     * Split the array into ExtraFields and populate them with the
125     * given data.
126     * @param data an array of bytes
127     * @param local whether data originates from the local file data
128     * or the central directory
129     * @param onUnparseableData what to do if the extra field data
130     * cannot be parsed.
131     * @return an array of ExtraFields
132     * @throws ZipException on error
133     *
134     * @since 1.1
135     */
136    public static ZipExtraField[] parse(final byte[] data, final boolean local,
137                                        final UnparseableExtraField onUnparseableData)
138        throws ZipException {
139        final List<ZipExtraField> v = new ArrayList<>();
140        int start = 0;
141        LOOP:
142        while (start <= data.length - WORD) {
143            final ZipShort headerId = new ZipShort(data, start);
144            final int length = new ZipShort(data, start + 2).getValue();
145            if (start + WORD + length > data.length) {
146                switch(onUnparseableData.getKey()) {
147                case UnparseableExtraField.THROW_KEY:
148                    throw new ZipException("bad extra field starting at "
149                                           + start + ".  Block length of "
150                                           + length + " bytes exceeds remaining"
151                                           + " data of "
152                                           + (data.length - start - WORD)
153                                           + " bytes.");
154                case UnparseableExtraField.READ_KEY:
155                    final UnparseableExtraFieldData field =
156                        new UnparseableExtraFieldData();
157                    if (local) {
158                        field.parseFromLocalFileData(data, start,
159                                                     data.length - start);
160                    } else {
161                        field.parseFromCentralDirectoryData(data, start,
162                                                            data.length - start);
163                    }
164                    v.add(field);
165                    //$FALL-THROUGH$
166                case UnparseableExtraField.SKIP_KEY:
167                    // since we cannot parse the data we must assume
168                    // the extra field consumes the whole rest of the
169                    // available data
170                    break LOOP;
171                default:
172                    throw new ZipException("unknown UnparseableExtraField key: "
173                                           + onUnparseableData.getKey());
174                }
175            }
176            try {
177                final ZipExtraField ze = createExtraField(headerId);
178                try {
179                    if (local) {
180                        ze.parseFromLocalFileData(data, start + WORD, length);
181                    } else {
182                        ze.parseFromCentralDirectoryData(data, start + WORD, length);
183                    }
184                } catch (ArrayIndexOutOfBoundsException aiobe) {
185                    throw (ZipException) new ZipException("Failed to parse corrupt ZIP extra field of type "
186                        + Integer.toHexString(headerId.getValue())).initCause(aiobe);
187                }
188                v.add(ze);
189            } catch (final InstantiationException | IllegalAccessException ie) {
190                throw (ZipException) new ZipException(ie.getMessage()).initCause(ie);
191            }
192            start += length + WORD;
193        }
194
195        final ZipExtraField[] result = new ZipExtraField[v.size()];
196        return v.toArray(result);
197    }
198
199    /**
200     * Merges the local file data fields of the given ZipExtraFields.
201     * @param data an array of ExtraFiles
202     * @return an array of bytes
203     */
204    public static byte[] mergeLocalFileDataData(final ZipExtraField[] data) {
205        final boolean lastIsUnparseableHolder = data.length > 0
206            && data[data.length - 1] instanceof UnparseableExtraFieldData;
207        final int regularExtraFieldCount =
208            lastIsUnparseableHolder ? data.length - 1 : data.length;
209
210        int sum = WORD * regularExtraFieldCount;
211        for (final ZipExtraField element : data) {
212            sum += element.getLocalFileDataLength().getValue();
213        }
214
215        final byte[] result = new byte[sum];
216        int start = 0;
217        for (int i = 0; i < regularExtraFieldCount; i++) {
218            System.arraycopy(data[i].getHeaderId().getBytes(),
219                             0, result, start, 2);
220            System.arraycopy(data[i].getLocalFileDataLength().getBytes(),
221                             0, result, start + 2, 2);
222            start += WORD;
223            final byte[] local = data[i].getLocalFileDataData();
224            if (local != null) {
225                System.arraycopy(local, 0, result, start, local.length);
226                start += local.length;
227            }
228        }
229        if (lastIsUnparseableHolder) {
230            final byte[] local = data[data.length - 1].getLocalFileDataData();
231            if (local != null) {
232                System.arraycopy(local, 0, result, start, local.length);
233            }
234        }
235        return result;
236    }
237
238    /**
239     * Merges the central directory fields of the given ZipExtraFields.
240     * @param data an array of ExtraFields
241     * @return an array of bytes
242     */
243    public static byte[] mergeCentralDirectoryData(final ZipExtraField[] data) {
244        final boolean lastIsUnparseableHolder = data.length > 0
245            && data[data.length - 1] instanceof UnparseableExtraFieldData;
246        final int regularExtraFieldCount =
247            lastIsUnparseableHolder ? data.length - 1 : data.length;
248
249        int sum = WORD * regularExtraFieldCount;
250        for (final ZipExtraField element : data) {
251            sum += element.getCentralDirectoryLength().getValue();
252        }
253        final byte[] result = new byte[sum];
254        int start = 0;
255        for (int i = 0; i < regularExtraFieldCount; i++) {
256            System.arraycopy(data[i].getHeaderId().getBytes(),
257                             0, result, start, 2);
258            System.arraycopy(data[i].getCentralDirectoryLength().getBytes(),
259                             0, result, start + 2, 2);
260            start += WORD;
261            final byte[] local = data[i].getCentralDirectoryData();
262            if (local != null) {
263                System.arraycopy(local, 0, result, start, local.length);
264                start += local.length;
265            }
266        }
267        if (lastIsUnparseableHolder) {
268            final byte[] local = data[data.length - 1].getCentralDirectoryData();
269            if (local != null) {
270                System.arraycopy(local, 0, result, start, local.length);
271            }
272        }
273        return result;
274    }
275
276    /**
277     * "enum" for the possible actions to take if the extra field
278     * cannot be parsed.
279     *
280     * @since 1.1
281     */
282    public static final class UnparseableExtraField {
283        /**
284         * Key for "throw an exception" action.
285         */
286        public static final int THROW_KEY = 0;
287        /**
288         * Key for "skip" action.
289         */
290        public static final int SKIP_KEY = 1;
291        /**
292         * Key for "read" action.
293         */
294        public static final int READ_KEY = 2;
295
296        /**
297         * Throw an exception if field cannot be parsed.
298         */
299        public static final UnparseableExtraField THROW
300            = new UnparseableExtraField(THROW_KEY);
301
302        /**
303         * Skip the extra field entirely and don't make its data
304         * available - effectively removing the extra field data.
305         */
306        public static final UnparseableExtraField SKIP
307            = new UnparseableExtraField(SKIP_KEY);
308
309        /**
310         * Read the extra field data into an instance of {@link
311         * UnparseableExtraFieldData UnparseableExtraFieldData}.
312         */
313        public static final UnparseableExtraField READ
314            = new UnparseableExtraField(READ_KEY);
315
316        private final int key;
317
318        private UnparseableExtraField(final int k) {
319            key = k;
320        }
321
322        /**
323         * Key of the action to take.
324         * @return the key
325         */
326        public int getKey() { return key; }
327    }
328}