You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GeneralPurposeBit.java 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.zip;
  19. /**
  20. * Parser/encoder for the "general purpose bit" field in ZIP's local
  21. * file and central directory headers.
  22. *
  23. * @since Ant 1.9.0
  24. */
  25. public final class GeneralPurposeBit implements Cloneable {
  26. /**
  27. * Indicates that the file is encrypted.
  28. */
  29. private static final int ENCRYPTION_FLAG = 1 << 0;
  30. /**
  31. * Indicates that a data descriptor stored after the file contents
  32. * will hold CRC and size information.
  33. */
  34. private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
  35. /**
  36. * Indicates strong encryption.
  37. */
  38. private static final int STRONG_ENCRYPTION_FLAG = 1 << 6;
  39. /**
  40. * Indicates that filenames are written in utf-8.
  41. *
  42. * <p>The only reason this is public is that {@link
  43. * ZipOutputStream#EFS_FLAG} was public in several versions of
  44. * Apache Ant and we needed a substitute for it.</p>
  45. */
  46. public static final int UFT8_NAMES_FLAG = 1 << 11;
  47. private boolean languageEncodingFlag = false;
  48. private boolean dataDescriptorFlag = false;
  49. private boolean encryptionFlag = false;
  50. private boolean strongEncryptionFlag = false;
  51. public GeneralPurposeBit() {
  52. }
  53. /**
  54. * whether the current entry uses UTF8 for file name and comment.
  55. */
  56. public boolean usesUTF8ForNames() {
  57. return languageEncodingFlag;
  58. }
  59. /**
  60. * whether the current entry will use UTF8 for file name and comment.
  61. */
  62. public void useUTF8ForNames(boolean b) {
  63. languageEncodingFlag = b;
  64. }
  65. /**
  66. * whether the current entry uses the data descriptor to store CRC
  67. * and size information
  68. */
  69. public boolean usesDataDescriptor() {
  70. return dataDescriptorFlag;
  71. }
  72. /**
  73. * whether the current entry will use the data descriptor to store
  74. * CRC and size information
  75. */
  76. public void useDataDescriptor(boolean b) {
  77. dataDescriptorFlag = b;
  78. }
  79. /**
  80. * whether the current entry is encrypted
  81. */
  82. public boolean usesEncryption() {
  83. return encryptionFlag;
  84. }
  85. /**
  86. * whether the current entry will be encrypted
  87. */
  88. public void useEncryption(boolean b) {
  89. encryptionFlag = b;
  90. }
  91. /**
  92. * whether the current entry is encrypted using strong encryption
  93. */
  94. public boolean usesStrongEncryption() {
  95. return encryptionFlag && strongEncryptionFlag;
  96. }
  97. /**
  98. * whether the current entry will be encrypted using strong encryption
  99. */
  100. public void useStrongEncryption(boolean b) {
  101. strongEncryptionFlag = b;
  102. if (b) {
  103. useEncryption(true);
  104. }
  105. }
  106. /**
  107. * Encodes the set bits in a form suitable for ZIP archives.
  108. */
  109. public byte[] encode() {
  110. byte[] result = new byte[2];
  111. encode(result, 0);
  112. return result;
  113. }
  114. /**
  115. * Encodes the set bits in a form suitable for ZIP archives.
  116. *
  117. * @param buf the output buffer
  118. * @param offset
  119. * The offset within the output buffer of the first byte to be written.
  120. * must be non-negative and no larger than <tt>buf.length-2</tt>
  121. */
  122. public void encode(byte[] buf, int offset) {
  123. ZipShort.putShort((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0)
  124. |
  125. (languageEncodingFlag ? UFT8_NAMES_FLAG : 0)
  126. |
  127. (encryptionFlag ? ENCRYPTION_FLAG : 0)
  128. |
  129. (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0)
  130. , buf, offset);
  131. }
  132. /**
  133. * Parses the supported flags from the given archive data.
  134. * @param data local file header or a central directory entry.
  135. * @param offset offset at which the general purpose bit starts
  136. */
  137. public static GeneralPurposeBit parse(final byte[] data, final int offset) {
  138. final int generalPurposeFlag = ZipShort.getValue(data, offset);
  139. GeneralPurposeBit b = new GeneralPurposeBit();
  140. b.useDataDescriptor((generalPurposeFlag & DATA_DESCRIPTOR_FLAG) != 0);
  141. b.useUTF8ForNames((generalPurposeFlag & UFT8_NAMES_FLAG) != 0);
  142. b.useStrongEncryption((generalPurposeFlag & STRONG_ENCRYPTION_FLAG)
  143. != 0);
  144. b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
  145. return b;
  146. }
  147. @Override
  148. public int hashCode() {
  149. return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0)
  150. + (strongEncryptionFlag ? 1 : 0))
  151. + (languageEncodingFlag ? 1 : 0))
  152. + (dataDescriptorFlag ? 1 : 0));
  153. }
  154. @Override
  155. public boolean equals(Object o) {
  156. if (!(o instanceof GeneralPurposeBit)) {
  157. return false;
  158. }
  159. GeneralPurposeBit g = (GeneralPurposeBit) o;
  160. return g.encryptionFlag == encryptionFlag
  161. && g.strongEncryptionFlag == strongEncryptionFlag
  162. && g.languageEncodingFlag == languageEncodingFlag
  163. && g.dataDescriptorFlag == dataDescriptorFlag;
  164. }
  165. @Override
  166. public Object clone() {
  167. try {
  168. return super.clone();
  169. } catch (CloneNotSupportedException ex) {
  170. // impossible
  171. throw new RuntimeException("GeneralPurposeBit is not Cloneable?", ex); //NOSONAR
  172. }
  173. }
  174. }