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
020/**
021 * Parser/encoder for the "general purpose bit" field in ZIP's local
022 * file and central directory headers.
023 *
024 * @since 1.1
025 * @NotThreadSafe
026 */
027public final class GeneralPurposeBit implements Cloneable {
028
029    /**
030     * Indicates that the file is encrypted.
031     */
032    private static final int ENCRYPTION_FLAG = 1 << 0;
033
034    /**
035     * Indicates the size of the sliding dictionary used by the compression method 6 (imploding).
036     * <ul>
037     *   <li>0: 4096 bytes</li>
038     *   <li>1: 8192 bytes</li>
039     * </ul>
040     */
041    private static final int SLIDING_DICTIONARY_SIZE_FLAG = 1 << 1;
042
043    /**
044     * Indicates the number of Shannon-Fano trees used by the compression method 6 (imploding).
045     * <ul>
046     *   <li>0: 2 trees (lengths, distances)</li>
047     *   <li>1: 3 trees (literals, lengths, distances)</li>
048     * </ul>
049     */
050    private static final int NUMBER_OF_SHANNON_FANO_TREES_FLAG = 1 << 2;
051
052    /**
053     * Indicates that a data descriptor stored after the file contents
054     * will hold CRC and size information.
055     */
056    private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
057
058    /**
059     * Indicates strong encryption.
060     */
061    private static final int STRONG_ENCRYPTION_FLAG = 1 << 6;
062
063    /**
064     * Indicates that filenames are written in UTF-8.
065     *
066     * <p>The only reason this is public is that {@link
067     * ZipArchiveOutputStream#EFS_FLAG} was public in Apache Commons
068     * Compress 1.0 and we needed a substitute for it.</p>
069     */
070    public static final int UFT8_NAMES_FLAG = 1 << 11;
071
072    private boolean languageEncodingFlag = false;
073    private boolean dataDescriptorFlag = false;
074    private boolean encryptionFlag = false;
075    private boolean strongEncryptionFlag = false;
076    private int slidingDictionarySize;
077    private int numberOfShannonFanoTrees;
078
079    public GeneralPurposeBit() {
080    }
081
082    /**
083     * whether the current entry uses UTF8 for file name and comment.
084     * @return whether the current entry uses UTF8 for file name and comment.
085     */
086    public boolean usesUTF8ForNames() {
087        return languageEncodingFlag;
088    }
089
090    /**
091     * whether the current entry will use UTF8 for file name and comment.
092     * @param b whether the current entry will use UTF8 for file name and comment.
093     */
094    public void useUTF8ForNames(final boolean b) {
095        languageEncodingFlag = b;
096    }
097
098    /**
099     * whether the current entry uses the data descriptor to store CRC
100     * and size information.
101     * @return whether the current entry uses the data descriptor to store CRC
102     * and size information
103     */
104    public boolean usesDataDescriptor() {
105        return dataDescriptorFlag;
106    }
107
108    /**
109     * whether the current entry will use the data descriptor to store
110     * CRC and size information.
111     * @param b whether the current entry will use the data descriptor to store
112     * CRC and size information
113     */
114    public void useDataDescriptor(final boolean b) {
115        dataDescriptorFlag = b;
116    }
117
118    /**
119     * whether the current entry is encrypted.
120     * @return whether the current entry is encrypted
121     */
122    public boolean usesEncryption() {
123        return encryptionFlag;
124    }
125
126    /**
127     * whether the current entry will be encrypted.
128     * @param b whether the current entry will be encrypted
129     */
130    public void useEncryption(final boolean b) {
131        encryptionFlag = b;
132    }
133
134    /**
135     * whether the current entry is encrypted using strong encryption.
136     * @return whether the current entry is encrypted using strong encryption
137     */
138    public boolean usesStrongEncryption() {
139        return encryptionFlag && strongEncryptionFlag;
140    }
141
142    /**
143     * whether the current entry will be encrypted  using strong encryption.
144     * @param b whether the current entry will be encrypted  using strong encryption
145     */
146    public void useStrongEncryption(final boolean b) {
147        strongEncryptionFlag = b;
148        if (b) {
149            useEncryption(true);
150        }
151    }
152
153    /**
154     * Returns the sliding dictionary size used by the compression method 6 (imploding).
155     */
156    int getSlidingDictionarySize() {
157        return slidingDictionarySize;
158    }
159
160    /**
161     * Returns the number of trees used by the compression method 6 (imploding).
162     */
163    int getNumberOfShannonFanoTrees() {
164        return numberOfShannonFanoTrees;
165    }
166
167    /**
168     * Encodes the set bits in a form suitable for ZIP archives.
169     * @return the encoded general purpose bits
170     */
171    public byte[] encode() {
172        final byte[] result = new byte[2];
173        encode(result, 0);
174        return result;
175    }
176
177
178    /**
179     * Encodes the set bits in a form suitable for ZIP archives.
180     *
181     * @param buf the output buffer
182     * @param  offset
183     *         The offset within the output buffer of the first byte to be written.
184     *         must be non-negative and no larger than <tt>buf.length-2</tt>
185     */
186    public void encode(final byte[] buf, final int offset) {
187                ZipShort.putShort((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
188                        |
189                        (languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
190                        |
191                        (encryptionFlag ? ENCRYPTION_FLAG : 0)
192                        |
193                        (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0)
194                        , buf, offset);
195    }
196
197    /**
198     * Parses the supported flags from the given archive data.
199     *
200     * @param data local file header or a central directory entry.
201     * @param offset offset at which the general purpose bit starts
202     * @return parsed flags
203     */
204    public static GeneralPurposeBit parse(final byte[] data, final int offset) {
205        final int generalPurposeFlag = ZipShort.getValue(data, offset);
206        final GeneralPurposeBit b = new GeneralPurposeBit();
207        b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
208        b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
209        b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG) != 0);
210        b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
211        b.slidingDictionarySize = (generalPurposeFlag & SLIDING_DICTIONARY_SIZE_FLAG) != 0 ? 8192 : 4096;
212        b.numberOfShannonFanoTrees = (generalPurposeFlag & NUMBER_OF_SHANNON_FANO_TREES_FLAG) != 0 ? 3 : 2;
213        return b;
214    }
215
216    @Override
217    public int hashCode() {
218        return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0)
219                               + (strongEncryptionFlag ? 1 : 0))
220                         + (languageEncodingFlag ? 1 : 0))
221                    + (dataDescriptorFlag ? 1 : 0));
222    }
223
224    @Override
225    public boolean equals(final Object o) {
226        if (!(o instanceof GeneralPurposeBit)) {
227            return false;
228        }
229        final GeneralPurposeBit g = (GeneralPurposeBit) o;
230        return g.encryptionFlag == encryptionFlag
231            && g.strongEncryptionFlag == strongEncryptionFlag
232            && g.languageEncodingFlag == languageEncodingFlag
233            && g.dataDescriptorFlag == dataDescriptorFlag;
234    }
235
236    @Override
237    public Object clone() {
238        try {
239            return super.clone();
240        } catch (final CloneNotSupportedException ex) {
241            // impossible
242            throw new RuntimeException("GeneralPurposeBit is not Cloneable?", ex); //NOSONAR
243        }
244    }
245}