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.

AsiExtraField.java 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. import java.util.zip.CRC32;
  20. import java.util.zip.ZipException;
  21. /**
  22. * Adds Unix file permission and UID/GID fields as well as symbolic
  23. * link handling.
  24. *
  25. * <p>This class uses the ASi extra field in the format:
  26. * <pre>
  27. * Value Size Description
  28. * ----- ---- -----------
  29. * (Unix3) 0x756e Short tag for this extra block type
  30. * TSize Short total data size for this block
  31. * CRC Long CRC-32 of the remaining data
  32. * Mode Short file permissions
  33. * SizDev Long symlink'd size OR major/minor dev num
  34. * UID Short user ID
  35. * GID Short group ID
  36. * (var.) variable symbolic link filename
  37. * </pre>
  38. * taken from appnote.iz (Info-ZIP note, 981119) found at <a
  39. * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
  40. *
  41. * <p>Short is two bytes and Long is four bytes in big endian byte and
  42. * word order, device numbers are currently not supported.</p>
  43. *
  44. */
  45. public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
  46. private static final ZipShort HEADER_ID = new ZipShort(0x756E);
  47. private static final int WORD = 4;
  48. /**
  49. * Standard Unix stat(2) file mode.
  50. *
  51. * @since 1.1
  52. */
  53. private int mode = 0;
  54. /**
  55. * User ID.
  56. *
  57. * @since 1.1
  58. */
  59. private int uid = 0;
  60. /**
  61. * Group ID.
  62. *
  63. * @since 1.1
  64. */
  65. private int gid = 0;
  66. /**
  67. * File this entry points to, if it is a symbolic link.
  68. *
  69. * <p>empty string - if entry is not a symbolic link.</p>
  70. *
  71. * @since 1.1
  72. */
  73. private String link = "";
  74. /**
  75. * Is this an entry for a directory?
  76. *
  77. * @since 1.1
  78. */
  79. private boolean dirFlag = false;
  80. /**
  81. * Instance used to calculate checksums.
  82. *
  83. * @since 1.1
  84. */
  85. private CRC32 crc = new CRC32();
  86. /** Constructor for AsiExtraField. */
  87. public AsiExtraField() {
  88. }
  89. /**
  90. * The Header-ID.
  91. * @return the value for the header id for this extrafield
  92. * @since 1.1
  93. */
  94. public ZipShort getHeaderId() {
  95. return HEADER_ID;
  96. }
  97. /**
  98. * Length of the extra field in the local file data - without
  99. * Header-ID or length specifier.
  100. * @return a <code>ZipShort</code> for the length of the data of this extra field
  101. * @since 1.1
  102. */
  103. public ZipShort getLocalFileDataLength() {
  104. return new ZipShort(WORD // CRC
  105. + 2 // Mode
  106. + WORD // SizDev
  107. + 2 // UID
  108. + 2 // GID
  109. + getLinkedFile().getBytes().length);
  110. }
  111. /**
  112. * Delegate to local file data.
  113. * @return the centralDirectory length
  114. * @since 1.1
  115. */
  116. public ZipShort getCentralDirectoryLength() {
  117. return getLocalFileDataLength();
  118. }
  119. /**
  120. * The actual data to put into local file data - without Header-ID
  121. * or length specifier.
  122. * @return get the data
  123. * @since 1.1
  124. */
  125. public byte[] getLocalFileDataData() {
  126. // CRC will be added later
  127. byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
  128. System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
  129. byte[] linkArray = getLinkedFile().getBytes();
  130. System.arraycopy(ZipLong.getBytes(linkArray.length),
  131. 0, data, 2, WORD);
  132. System.arraycopy(ZipShort.getBytes(getUserId()),
  133. 0, data, 6, 2);
  134. System.arraycopy(ZipShort.getBytes(getGroupId()),
  135. 0, data, 8, 2);
  136. System.arraycopy(linkArray, 0, data, 10, linkArray.length);
  137. crc.reset();
  138. crc.update(data);
  139. long checksum = crc.getValue();
  140. byte[] result = new byte[data.length + WORD];
  141. System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
  142. System.arraycopy(data, 0, result, WORD, data.length);
  143. return result;
  144. }
  145. /**
  146. * Delegate to local file data.
  147. * @return the local file data
  148. * @since 1.1
  149. */
  150. public byte[] getCentralDirectoryData() {
  151. return getLocalFileDataData();
  152. }
  153. /**
  154. * Set the user id.
  155. * @param uid the user id
  156. * @since 1.1
  157. */
  158. public void setUserId(int uid) {
  159. this.uid = uid;
  160. }
  161. /**
  162. * Get the user id.
  163. * @return the user id
  164. * @since 1.1
  165. */
  166. public int getUserId() {
  167. return uid;
  168. }
  169. /**
  170. * Set the group id.
  171. * @param gid the group id
  172. * @since 1.1
  173. */
  174. public void setGroupId(int gid) {
  175. this.gid = gid;
  176. }
  177. /**
  178. * Get the group id.
  179. * @return the group id
  180. * @since 1.1
  181. */
  182. public int getGroupId() {
  183. return gid;
  184. }
  185. /**
  186. * Indicate that this entry is a symbolic link to the given filename.
  187. *
  188. * @param name Name of the file this entry links to, empty String
  189. * if it is not a symbolic link.
  190. *
  191. * @since 1.1
  192. */
  193. public void setLinkedFile(String name) {
  194. link = name;
  195. mode = getMode(mode);
  196. }
  197. /**
  198. * Name of linked file
  199. *
  200. * @return name of the file this entry links to if it is a
  201. * symbolic link, the empty string otherwise.
  202. *
  203. * @since 1.1
  204. */
  205. public String getLinkedFile() {
  206. return link;
  207. }
  208. /**
  209. * Is this entry a symbolic link?
  210. * @return true if this is a symbolic link
  211. * @since 1.1
  212. */
  213. public boolean isLink() {
  214. return getLinkedFile().length() != 0;
  215. }
  216. /**
  217. * File mode of this file.
  218. * @param mode the file mode
  219. * @since 1.1
  220. */
  221. public void setMode(int mode) {
  222. this.mode = getMode(mode);
  223. }
  224. /**
  225. * File mode of this file.
  226. * @return the file mode
  227. * @since 1.1
  228. */
  229. public int getMode() {
  230. return mode;
  231. }
  232. /**
  233. * Indicate whether this entry is a directory.
  234. * @param dirFlag if true, this entry is a directory
  235. * @since 1.1
  236. */
  237. public void setDirectory(boolean dirFlag) {
  238. this.dirFlag = dirFlag;
  239. mode = getMode(mode);
  240. }
  241. /**
  242. * Is this entry a directory?
  243. * @return true if this entry is a directory
  244. * @since 1.1
  245. */
  246. public boolean isDirectory() {
  247. return dirFlag && !isLink();
  248. }
  249. /**
  250. * Populate data from this array as if it was in local file data.
  251. * @param data an array of bytes
  252. * @param offset the start offset
  253. * @param length the number of bytes in the array from offset
  254. * @since 1.1
  255. * @throws ZipException on error
  256. */
  257. public void parseFromLocalFileData(byte[] data, int offset, int length)
  258. throws ZipException {
  259. long givenChecksum = ZipLong.getValue(data, offset);
  260. byte[] tmp = new byte[length - WORD];
  261. System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
  262. crc.reset();
  263. crc.update(tmp);
  264. long realChecksum = crc.getValue();
  265. if (givenChecksum != realChecksum) {
  266. throw new ZipException("bad CRC checksum "
  267. + Long.toHexString(givenChecksum)
  268. + " instead of "
  269. + Long.toHexString(realChecksum));
  270. }
  271. int newMode = ZipShort.getValue(tmp, 0);
  272. byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
  273. uid = ZipShort.getValue(tmp, 6);
  274. gid = ZipShort.getValue(tmp, 8);
  275. if (linkArray.length == 0) {
  276. link = "";
  277. } else {
  278. System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
  279. link = new String(linkArray);
  280. }
  281. setDirectory((newMode & DIR_FLAG) != 0);
  282. setMode(newMode);
  283. }
  284. /**
  285. * Get the file mode for given permissions with the correct file type.
  286. * @param mode the mode
  287. * @return the type with the mode
  288. * @since 1.1
  289. */
  290. protected int getMode(int mode) {
  291. int type = FILE_FLAG;
  292. if (isLink()) {
  293. type = LINK_FLAG;
  294. } else if (isDirectory()) {
  295. type = DIR_FLAG;
  296. }
  297. return type | (mode & PERM_MASK);
  298. }
  299. }