    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.archivers.zip;
    020
    021import java.util.Arrays;
    022
    023/**
    024 * Wrapper for extra field data that doesn't conform to the recommended format of header-tag + size + data.
    025 *
    026 * <p>
    027 * The header-id is artificial (and not listed as a known ID in <a href="https://www.pkware.com/documents/casestudies/APPNOTE.TXT">APPNOTE.TXT</a>). Since it
    028 * isn't used anywhere except to satisfy the ZipExtraField contract it shouldn't matter anyway.
    029 * </p>
    030 *
    031 * @since 1.1
    032 * @NotThreadSafe
    033 */
    034public final class UnparseableExtraFieldData implements ZipExtraField {
    035    private static final ZipShort HEADER_ID = new ZipShort(0xACC1);
    036
    037    private byte[] localFileData;
    038    private byte[] centralDirectoryData;
    039
    040    /**
    041     * The actual data to put into central directory.
    042     *
    043     * @return The CentralDirectoryData value
    044     */
    045    @Override
    046    public byte[] getCentralDirectoryData() {
    047        return centralDirectoryData == null ? getLocalFileDataData() : ZipUtil.copy(centralDirectoryData);
    048    }
    049
    050    /**
    051     * Length of the complete extra field in the central directory.
    052     *
    053     * @return The CentralDirectoryLength value
    054     */
    055    @Override
    056    public ZipShort getCentralDirectoryLength() {
    057        return centralDirectoryData == null ? getLocalFileDataLength() : new ZipShort(centralDirectoryData.length);
    058    }
    059
    060    /**
    061     * The Header-ID.
    062     *
    063     * @return a completely arbitrary value that should be ignored.
    064     */
    065    @Override
    066    public ZipShort getHeaderId() {
    067        return HEADER_ID;
    068    }
    069
    070    /**
    071     * The actual data to put into local file data.
    072     *
    073     * @return The LocalFileDataData value
    074     */
    075    @Override
    076    public byte[] getLocalFileDataData() {
    077        return ZipUtil.copy(localFileData);
    078    }
    079
    080    /**
    081     * Length of the complete extra field in the local file data.
    082     *
    083     * @return The LocalFileDataLength value
    084     */
    085    @Override
    086    public ZipShort getLocalFileDataLength() {
    087        return new ZipShort(localFileData == null ? 0 : localFileData.length);
    088    }
    089
    090    /**
    091     * Populate data from this array as if it was in central directory data.
    092     *
    093     * @param buffer the buffer to read data from
    094     * @param offset offset into buffer to read data
    095     * @param length the length of data
    096     */
    097    @Override
    098    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) {
    099        centralDirectoryData = Arrays.copyOfRange(buffer, offset, offset + length);
    100        if (localFileData == null) {
    101            parseFromLocalFileData(buffer, offset, length);
    102        }
    103    }
    104
    105    /**
    106     * Populate data from this array as if it was in local file data.
    107     *
    108     * @param buffer the buffer to read data from
    109     * @param offset offset into buffer to read data
    110     * @param length the length of data
    111     */
    112    @Override
    113    public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length) {
    114        localFileData = Arrays.copyOfRange(buffer, offset, offset + length);
    115    }
    116
    117}