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 file names 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 /** 073 * Parses the supported flags from the given archive data. 074 * 075 * @param data local file header or a central directory entry. 076 * @param offset offset at which the general purpose bit starts 077 * @return parsed flags 078 */ 079 public static GeneralPurposeBit parse(final byte[] data, final int offset) { 080 final int generalPurposeFlag = ZipShort.getValue(data, offset); 081 final GeneralPurposeBit b = new GeneralPurposeBit(); 082 b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0); 083 b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0); 084 b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG) != 0); 085 b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0); 086 b.slidingDictionarySize = (generalPurposeFlag & SLIDING_DICTIONARY_SIZE_FLAG) != 0 ? 8192 : 4096; 087 b.numberOfShannonFanoTrees = (generalPurposeFlag & NUMBER_OF_SHANNON_FANO_TREES_FLAG) != 0 ? 3 : 2; 088 return b; 089 } 090 private boolean languageEncodingFlag; 091 private boolean dataDescriptorFlag; 092 private boolean encryptionFlag; 093 private boolean strongEncryptionFlag; 094 private int slidingDictionarySize; 095 096 private int numberOfShannonFanoTrees; 097 098 public GeneralPurposeBit() { 099 } 100 101 @Override 102 public Object clone() { 103 try { 104 return super.clone(); 105 } catch (final CloneNotSupportedException ex) { 106 // impossible 107 throw new IllegalStateException("GeneralPurposeBit is not Cloneable?", ex); //NOSONAR 108 } 109 } 110 111 /** 112 * Encodes the set bits in a form suitable for ZIP archives. 113 * @return the encoded general purpose bits 114 */ 115 public byte[] encode() { 116 final byte[] result = new byte[2]; 117 encode(result, 0); 118 return result; 119 } 120 121 /** 122 * Encodes the set bits in a form suitable for ZIP archives. 123 * 124 * @param buf the output buffer 125 * @param offset 126 * The offset within the output buffer of the first byte to be written. 127 * must be non-negative and no larger than {@code buf.length-2} 128 */ 129 public void encode(final byte[] buf, final int offset) { 130 ZipShort.putShort((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0) 131 | 132 (languageEncodingFlag ? UFT8_NAMES_FLAG : 0) 133 | 134 (encryptionFlag ? ENCRYPTION_FLAG : 0) 135 | 136 (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0) 137 , buf, offset); 138 } 139 140 @Override 141 public boolean equals(final Object o) { 142 if (!(o instanceof GeneralPurposeBit)) { 143 return false; 144 } 145 final GeneralPurposeBit g = (GeneralPurposeBit) o; 146 return g.encryptionFlag == encryptionFlag 147 && g.strongEncryptionFlag == strongEncryptionFlag 148 && g.languageEncodingFlag == languageEncodingFlag 149 && g.dataDescriptorFlag == dataDescriptorFlag; 150 } 151 152 /** 153 * Returns the number of trees used by the compression method 6 (imploding). 154 */ 155 int getNumberOfShannonFanoTrees() { 156 return numberOfShannonFanoTrees; 157 } 158 159 /** 160 * Returns the sliding dictionary size used by the compression method 6 (imploding). 161 */ 162 int getSlidingDictionarySize() { 163 return slidingDictionarySize; 164 } 165 166 @Override 167 public int hashCode() { 168 return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0) 169 + (strongEncryptionFlag ? 1 : 0)) 170 + (languageEncodingFlag ? 1 : 0)) 171 + (dataDescriptorFlag ? 1 : 0)); 172 } 173 174 /** 175 * whether the current entry will use the data descriptor to store 176 * CRC and size information. 177 * @param b whether the current entry will use the data descriptor to store 178 * CRC and size information 179 */ 180 public void useDataDescriptor(final boolean b) { 181 dataDescriptorFlag = b; 182 } 183 184 /** 185 * whether the current entry will be encrypted. 186 * @param b whether the current entry will be encrypted 187 */ 188 public void useEncryption(final boolean b) { 189 encryptionFlag = b; 190 } 191 192 /** 193 * whether the current entry uses the data descriptor to store CRC 194 * and size information. 195 * @return whether the current entry uses the data descriptor to store CRC 196 * and size information 197 */ 198 public boolean usesDataDescriptor() { 199 return dataDescriptorFlag; 200 } 201 202 203 /** 204 * whether the current entry is encrypted. 205 * @return whether the current entry is encrypted 206 */ 207 public boolean usesEncryption() { 208 return encryptionFlag; 209 } 210 211 /** 212 * whether the current entry is encrypted using strong encryption. 213 * @return whether the current entry is encrypted using strong encryption 214 */ 215 public boolean usesStrongEncryption() { 216 return encryptionFlag && strongEncryptionFlag; 217 } 218 219 /** 220 * whether the current entry will be encrypted using strong encryption. 221 * @param b whether the current entry will be encrypted using strong encryption 222 */ 223 public void useStrongEncryption(final boolean b) { 224 strongEncryptionFlag = b; 225 if (b) { 226 useEncryption(true); 227 } 228 } 229 230 /** 231 * whether the current entry uses UTF8 for file name and comment. 232 * @return whether the current entry uses UTF8 for file name and comment. 233 */ 234 public boolean usesUTF8ForNames() { 235 return languageEncodingFlag; 236 } 237 238 /** 239 * whether the current entry will use UTF8 for file name and comment. 240 * @param b whether the current entry will use UTF8 for file name and comment. 241 */ 242 public void useUTF8ForNames(final boolean b) { 243 languageEncodingFlag = b; 244 } 245}