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