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 *   https://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.zip.ZipException;
022
023/**
024 * X.509 Certificate ID and Signature for central directory (0x0016).
025 *
026 * <p>
027 * This field contains the information about which certificate in the PKCS#7 store was used to sign the central directory structure. When the Central Directory
028 * Encryption feature is enabled for a ZIP file, this record will appear in the Archive Extra Data Record, otherwise it will appear in the first central
029 * directory record.
030 * </p>
031 *
032 * <p>
033 * Note: all fields stored in Intel low-byte/high-byte order.
034 * </p>
035 *
036 * <pre>
037 *         Value     Size     Description
038 *         -----     ----     -----------
039 * (CDID)  0x0016    2 bytes  Tag for this "extra" block type
040 *         TSize     2 bytes  Size of data that follows
041 *         RCount    4 bytes  Number of recipients. (inferred)
042 *         HashAlg   2 bytes  Hash algorithm identifier. (inferred)
043 *         TData     TSize    Data
044 * </pre>
045 *
046 * @NotThreadSafe
047 * @since 1.11
048 */
049public class X0016_CertificateIdForCentralDirectory extends PKWareExtraHeader {
050
051    static final ZipShort HEADER_ID = new ZipShort(0x0016);
052
053    private int rcount;
054
055    private HashAlgorithm hashAlg;
056
057    /**
058     * Constructs a new instance.
059     */
060    public X0016_CertificateIdForCentralDirectory() {
061        super(HEADER_ID);
062    }
063
064    /**
065     * Gets hash algorithm.
066     *
067     * @return the hash algorithm
068     */
069    public HashAlgorithm getHashAlgorithm() {
070        return hashAlg;
071    }
072
073    /**
074     * Gets record count.
075     *
076     * @return the record count
077     */
078    public int getRecordCount() {
079        return rcount;
080    }
081
082    @Override
083    public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) throws ZipException {
084        assertMinimalLength(4, length);
085        // TODO: double check we really do not want to call super here
086        this.rcount = ZipShort.getValue(data, offset);
087        this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2));
088    }
089}