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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // CheckStyle:MagicNumber OFF
  131. System.arraycopy(ZipLong.getBytes(linkArray.length),
  132. 0, data, 2, WORD);
  133. System.arraycopy(ZipShort.getBytes(getUserId()),
  134. 0, data, 6, 2);
  135. System.arraycopy(ZipShort.getBytes(getGroupId()),
  136. 0, data, 8, 2);
  137. System.arraycopy(linkArray, 0, data, 10, linkArray.length);
  138. // CheckStyle:MagicNumber ON
  139. crc.reset();
  140. crc.update(data);
  141. long checksum = crc.getValue();
  142. byte[] result = new byte[data.length + WORD];
  143. System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
  144. System.arraycopy(data, 0, result, WORD, data.length);
  145. return result;
  146. }
  147. /**
  148. * Delegate to local file data.
  149. * @return the local file data
  150. * @since 1.1
  151. */
  152. public byte[] getCentralDirectoryData() {
  153. return getLocalFileDataData();
  154. }
  155. /**
  156. * Set the user id.
  157. * @param uid the user id
  158. * @since 1.1
  159. */
  160. public void setUserId(int uid) {
  161. this.uid = uid;
  162. }
  163. /**
  164. * Get the user id.
  165. * @return the user id
  166. * @since 1.1
  167. */
  168. public int getUserId() {
  169. return uid;
  170. }
  171. /**
  172. * Set the group id.
  173. * @param gid the group id
  174. * @since 1.1
  175. */
  176. public void setGroupId(int gid) {
  177. this.gid = gid;
  178. }
  179. /**
  180. * Get the group id.
  181. * @return the group id
  182. * @since 1.1
  183. */
  184. public int getGroupId() {
  185. return gid;
  186. }
  187. /**
  188. * Indicate that this entry is a symbolic link to the given filename.
  189. *
  190. * @param name Name of the file this entry links to, empty String
  191. * if it is not a symbolic link.
  192. *
  193. * @since 1.1
  194. */
  195. public void setLinkedFile(String name) {
  196. link = name;
  197. mode = getMode(mode);
  198. }
  199. /**
  200. * Name of linked file
  201. *
  202. * @return name of the file this entry links to if it is a
  203. * symbolic link, the empty string otherwise.
  204. *
  205. * @since 1.1
  206. */
  207. public String getLinkedFile() {
  208. return link;
  209. }
  210. /**
  211. * Is this entry a symbolic link?
  212. * @return true if this is a symbolic link
  213. * @since 1.1
  214. */
  215. public boolean isLink() {
  216. return getLinkedFile().length() != 0;
  217. }
  218. /**
  219. * File mode of this file.
  220. * @param mode the file mode
  221. * @since 1.1
  222. */
  223. public void setMode(int mode) {
  224. this.mode = getMode(mode);
  225. }
  226. /**
  227. * File mode of this file.
  228. * @return the file mode
  229. * @since 1.1
  230. */
  231. public int getMode() {
  232. return mode;
  233. }
  234. /**
  235. * Indicate whether this entry is a directory.
  236. * @param dirFlag if true, this entry is a directory
  237. * @since 1.1
  238. */
  239. public void setDirectory(boolean dirFlag) {
  240. this.dirFlag = dirFlag;
  241. mode = getMode(mode);
  242. }
  243. /**
  244. * Is this entry a directory?
  245. * @return true if this entry is a directory
  246. * @since 1.1
  247. */
  248. public boolean isDirectory() {
  249. return dirFlag && !isLink();
  250. }
  251. /**
  252. * Populate data from this array as if it was in local file data.
  253. * @param data an array of bytes
  254. * @param offset the start offset
  255. * @param length the number of bytes in the array from offset
  256. * @since 1.1
  257. * @throws ZipException on error
  258. */
  259. public void parseFromLocalFileData(byte[] data, int offset, int length)
  260. throws ZipException {
  261. long givenChecksum = ZipLong.getValue(data, offset);
  262. byte[] tmp = new byte[length - WORD];
  263. System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
  264. crc.reset();
  265. crc.update(tmp);
  266. long realChecksum = crc.getValue();
  267. if (givenChecksum != realChecksum) {
  268. throw new ZipException("bad CRC checksum "
  269. + Long.toHexString(givenChecksum)
  270. + " instead of "
  271. + Long.toHexString(realChecksum));
  272. }
  273. int newMode = ZipShort.getValue(tmp, 0);
  274. // CheckStyle:MagicNumber OFF
  275. byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
  276. uid = ZipShort.getValue(tmp, 6);
  277. gid = ZipShort.getValue(tmp, 8);
  278. if (linkArray.length == 0) {
  279. link = "";
  280. } else {
  281. System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
  282. link = new String(linkArray);
  283. }
  284. // CheckStyle:MagicNumber ON
  285. setDirectory((newMode & DIR_FLAG) != 0);
  286. setMode(newMode);
  287. }
  288. /**
  289. * Get the file mode for given permissions with the correct file type.
  290. * @param mode the mode
  291. * @return the type with the mode
  292. * @since 1.1
  293. */
  294. protected int getMode(int mode) {
  295. int type = FILE_FLAG;
  296. if (isLink()) {
  297. type = LINK_FLAG;
  298. } else if (isDirectory()) {
  299. type = DIR_FLAG;
  300. }
  301. return type | (mode & PERM_MASK);
  302. }
  303. }