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.

TarUtils.java 22 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. /*
  19. * This package is based on the work done by Timothy Gerard Endres
  20. * (time@ice.com) to whom the Ant project is very grateful for his great code.
  21. */
  22. package org.apache.tools.tar;
  23. import java.io.IOException;
  24. import java.math.BigInteger;
  25. import java.nio.ByteBuffer;
  26. import org.apache.tools.zip.ZipEncoding;
  27. import org.apache.tools.zip.ZipEncodingHelper;
  28. /**
  29. * This class provides static utility methods to work with byte streams.
  30. *
  31. */
  32. // CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
  33. public class TarUtils {
  34. private static final int BYTE_MASK = 255;
  35. static final ZipEncoding DEFAULT_ENCODING =
  36. ZipEncodingHelper.getZipEncoding(null);
  37. /**
  38. * Encapsulates the algorithms used up to Ant 1.8 as ZipEncoding.
  39. */
  40. static final ZipEncoding FALLBACK_ENCODING = new ZipEncoding() {
  41. public boolean canEncode(final String name) { return true; }
  42. public ByteBuffer encode(final String name) {
  43. final int length = name.length();
  44. final byte[] buf = new byte[length];
  45. // copy until end of input or output is reached.
  46. for (int i = 0; i < length; ++i) {
  47. buf[i] = (byte) name.charAt(i);
  48. }
  49. return ByteBuffer.wrap(buf);
  50. }
  51. public String decode(final byte[] buffer) {
  52. final int length = buffer.length;
  53. final StringBuilder result = new StringBuilder(length);
  54. for (int i = 0; i < length; ++i) {
  55. final byte b = buffer[i];
  56. if (b == 0) { // Trailing null
  57. break;
  58. }
  59. result.append((char) (b & 0xFF)); // Allow for sign-extension
  60. }
  61. return result.toString();
  62. }
  63. };
  64. /** Private constructor to prevent instantiation of this utility class. */
  65. private TarUtils(){
  66. }
  67. /**
  68. * Parse an octal string from a buffer.
  69. *
  70. * <p>Leading spaces are ignored.
  71. * The buffer must contain a trailing space or NUL,
  72. * and may contain an additional trailing space or NUL.</p>
  73. *
  74. * <p>The input buffer is allowed to contain all NULs,
  75. * in which case the method returns 0L
  76. * (this allows for missing fields).</p>
  77. *
  78. * <p>To work-around some tar implementations that insert a
  79. * leading NUL this method returns 0 if it detects a leading NUL
  80. * since Ant 1.9.</p>
  81. *
  82. * @param buffer The buffer from which to parse.
  83. * @param offset The offset into the buffer from which to parse.
  84. * @param length The maximum number of bytes to parse - must be at least 2 bytes.
  85. * @return The long value of the octal string.
  86. * @throws IllegalArgumentException if the trailing space/NUL is missing or if a invalid byte is detected.
  87. */
  88. public static long parseOctal(final byte[] buffer, final int offset, final int length) {
  89. long result = 0;
  90. int end = offset + length;
  91. int start = offset;
  92. if (length < 2){
  93. throw new IllegalArgumentException("Length "+length+" must be at least 2");
  94. }
  95. if (buffer[start] == 0) {
  96. return 0L;
  97. }
  98. // Skip leading spaces
  99. while (start < end){
  100. if (buffer[start] == ' '){
  101. start++;
  102. } else {
  103. break;
  104. }
  105. }
  106. // Trim all trailing NULs and spaces.
  107. // The ustar and POSIX tar specs require a trailing NUL or
  108. // space but some implementations use the extra digit for big
  109. // sizes/uids/gids ...
  110. byte trailer = buffer[end - 1];
  111. while (start < end && (trailer == 0 || trailer == ' ')) {
  112. end--;
  113. trailer = buffer[end - 1];
  114. }
  115. for ( ;start < end; start++) {
  116. final byte currentByte = buffer[start];
  117. // CheckStyle:MagicNumber OFF
  118. if (currentByte < '0' || currentByte > '7'){
  119. throw new IllegalArgumentException(
  120. exceptionMessage(buffer, offset, length, start, currentByte));
  121. }
  122. result = (result << 3) + (currentByte - '0'); // convert from ASCII
  123. // CheckStyle:MagicNumber ON
  124. }
  125. return result;
  126. }
  127. /**
  128. * Compute the value contained in a byte buffer. If the most
  129. * significant bit of the first byte in the buffer is set, this
  130. * bit is ignored and the rest of the buffer is interpreted as a
  131. * binary number. Otherwise, the buffer is interpreted as an
  132. * octal number as per the parseOctal function above.
  133. *
  134. * @param buffer The buffer from which to parse.
  135. * @param offset The offset into the buffer from which to parse.
  136. * @param length The maximum number of bytes to parse.
  137. * @return The long value of the octal or binary string.
  138. * @throws IllegalArgumentException if the trailing space/NUL is
  139. * missing or an invalid byte is detected in an octal number, or
  140. * if a binary number would exceed the size of a signed long
  141. * 64-bit integer.
  142. */
  143. public static long parseOctalOrBinary(final byte[] buffer, final int offset,
  144. final int length) {
  145. if ((buffer[offset] & 0x80) == 0) {
  146. return parseOctal(buffer, offset, length);
  147. }
  148. final boolean negative = buffer[offset] == (byte) 0xff;
  149. if (length < 9) {
  150. return parseBinaryLong(buffer, offset, length, negative);
  151. }
  152. return parseBinaryBigInteger(buffer, offset, length, negative);
  153. }
  154. private static long parseBinaryLong(final byte[] buffer, final int offset,
  155. final int length,
  156. final boolean negative) {
  157. if (length >= 9) {
  158. throw new IllegalArgumentException("At offset " + offset + ", "
  159. + length + " byte binary number"
  160. + " exceeds maximum signed long"
  161. + " value");
  162. }
  163. long val = 0;
  164. for (int i = 1; i < length; i++) {
  165. val = (val << 8) + (buffer[offset + i] & 0xff);
  166. }
  167. if (negative) {
  168. // 2's complement
  169. val--;
  170. val ^= (long) Math.pow(2, (length - 1) * 8.0) - 1;
  171. }
  172. return negative ? -val : val;
  173. }
  174. private static long parseBinaryBigInteger(final byte[] buffer,
  175. final int offset,
  176. final int length,
  177. final boolean negative) {
  178. final byte[] remainder = new byte[length - 1];
  179. System.arraycopy(buffer, offset + 1, remainder, 0, length - 1);
  180. BigInteger val = new BigInteger(remainder);
  181. if (negative) {
  182. // 2's complement
  183. val = val.add(BigInteger.valueOf(-1)).not();
  184. }
  185. if (val.bitLength() > 63) {
  186. throw new IllegalArgumentException("At offset " + offset + ", "
  187. + length + " byte binary number"
  188. + " exceeds maximum signed long"
  189. + " value");
  190. }
  191. return negative ? -val.longValue() : val.longValue();
  192. }
  193. /**
  194. * Parse a boolean byte from a buffer.
  195. * Leading spaces and NUL are ignored.
  196. * The buffer may contain trailing spaces or NULs.
  197. *
  198. * @param buffer The buffer from which to parse.
  199. * @param offset The offset into the buffer from which to parse.
  200. * @return The boolean value of the bytes.
  201. * @throws IllegalArgumentException if an invalid byte is detected.
  202. */
  203. public static boolean parseBoolean(final byte[] buffer, final int offset) {
  204. return buffer[offset] == 1;
  205. }
  206. // Helper method to generate the exception message
  207. private static String exceptionMessage(final byte[] buffer, final int offset,
  208. final int length, final int current, final byte currentByte) {
  209. // default charset is good enough for an exception message,
  210. //
  211. // the alternative was to modify parseOctal and
  212. // parseOctalOrBinary to receive the ZipEncoding of the
  213. // archive (deprecating the existing public methods, of
  214. // course) and dealing with the fact that ZipEncoding#decode
  215. // can throw an IOException which parseOctal* doesn't declare
  216. String string = new String(buffer, offset, length);
  217. string=string.replaceAll("\0", "{NUL}"); // Replace NULs to allow string to be printed
  218. final String s = "Invalid byte "+currentByte+" at offset "+(current-offset)+" in '"+string+"' len="+length;
  219. return s;
  220. }
  221. /**
  222. * Parse an entry name from a buffer.
  223. * Parsing stops when a NUL is found
  224. * or the buffer length is reached.
  225. *
  226. * @param buffer The buffer from which to parse.
  227. * @param offset The offset into the buffer from which to parse.
  228. * @param length The maximum number of bytes to parse.
  229. * @return The entry name.
  230. */
  231. public static String parseName(final byte[] buffer, final int offset, final int length) {
  232. try {
  233. return parseName(buffer, offset, length, DEFAULT_ENCODING);
  234. } catch (final IOException ex) {
  235. try {
  236. return parseName(buffer, offset, length, FALLBACK_ENCODING);
  237. } catch (final IOException ex2) {
  238. // impossible
  239. throw new RuntimeException(ex2);
  240. }
  241. }
  242. }
  243. /**
  244. * Parse an entry name from a buffer.
  245. * Parsing stops when a NUL is found
  246. * or the buffer length is reached.
  247. *
  248. * @param buffer The buffer from which to parse.
  249. * @param offset The offset into the buffer from which to parse.
  250. * @param length The maximum number of bytes to parse.
  251. * @param encoding name of the encoding to use for file names
  252. * @return The entry name.
  253. */
  254. public static String parseName(final byte[] buffer, final int offset,
  255. final int length,
  256. final ZipEncoding encoding)
  257. throws IOException {
  258. int len = length;
  259. for (; len > 0; len--) {
  260. if (buffer[offset + len - 1] != 0) {
  261. break;
  262. }
  263. }
  264. if (len > 0) {
  265. final byte[] b = new byte[len];
  266. System.arraycopy(buffer, offset, b, 0, len);
  267. return encoding.decode(b);
  268. }
  269. return "";
  270. }
  271. /**
  272. * Copy a name into a buffer.
  273. * Copies characters from the name into the buffer
  274. * starting at the specified offset.
  275. * If the buffer is longer than the name, the buffer
  276. * is filled with trailing NULs.
  277. * If the name is longer than the buffer,
  278. * the output is truncated.
  279. *
  280. * @param name The header name from which to copy the characters.
  281. * @param buf The buffer where the name is to be stored.
  282. * @param offset The starting offset into the buffer
  283. * @param length The maximum number of header bytes to copy.
  284. * @return The updated offset, i.e. offset + length
  285. */
  286. public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length) {
  287. try {
  288. return formatNameBytes(name, buf, offset, length, DEFAULT_ENCODING);
  289. } catch (final IOException ex) {
  290. try {
  291. return formatNameBytes(name, buf, offset, length,
  292. FALLBACK_ENCODING);
  293. } catch (final IOException ex2) {
  294. // impossible
  295. throw new RuntimeException(ex2);
  296. }
  297. }
  298. }
  299. /**
  300. * Copy a name into a buffer.
  301. * Copies characters from the name into the buffer
  302. * starting at the specified offset.
  303. * If the buffer is longer than the name, the buffer
  304. * is filled with trailing NULs.
  305. * If the name is longer than the buffer,
  306. * the output is truncated.
  307. *
  308. * @param name The header name from which to copy the characters.
  309. * @param buf The buffer where the name is to be stored.
  310. * @param offset The starting offset into the buffer
  311. * @param length The maximum number of header bytes to copy.
  312. * @param encoding name of the encoding to use for file names
  313. * @return The updated offset, i.e. offset + length
  314. */
  315. public static int formatNameBytes(final String name, final byte[] buf, final int offset,
  316. final int length,
  317. final ZipEncoding encoding)
  318. throws IOException {
  319. int len = name.length();
  320. ByteBuffer b = encoding.encode(name);
  321. while (b.limit() > length && len > 0) {
  322. b = encoding.encode(name.substring(0, --len));
  323. }
  324. final int limit = b.limit() - b.position();
  325. System.arraycopy(b.array(), b.arrayOffset(), buf, offset, limit);
  326. // Pad any remaining output bytes with NUL
  327. for (int i = limit; i < length; ++i) {
  328. buf[offset + i] = 0;
  329. }
  330. return offset + length;
  331. }
  332. /**
  333. * Fill buffer with unsigned octal number, padded with leading zeroes.
  334. *
  335. * @param value number to convert to octal - treated as unsigned
  336. * @param buffer destination buffer
  337. * @param offset starting offset in buffer
  338. * @param length length of buffer to fill
  339. * @throws IllegalArgumentException if the value will not fit in the buffer
  340. */
  341. public static void formatUnsignedOctalString(final long value, final byte[] buffer,
  342. final int offset, final int length) {
  343. int remaining = length;
  344. remaining--;
  345. if (value == 0) {
  346. buffer[offset + remaining--] = (byte) '0';
  347. } else {
  348. long val = value;
  349. for (; remaining >= 0 && val != 0; --remaining) {
  350. // CheckStyle:MagicNumber OFF
  351. buffer[offset + remaining] = (byte) ((byte) '0' + (byte) (val & 7));
  352. val = val >>> 3;
  353. // CheckStyle:MagicNumber ON
  354. }
  355. if (val != 0){
  356. throw new IllegalArgumentException
  357. (value+"="+Long.toOctalString(value)+ " will not fit in octal number buffer of length "+length);
  358. }
  359. }
  360. for (; remaining >= 0; --remaining) { // leading zeros
  361. buffer[offset + remaining] = (byte) '0';
  362. }
  363. }
  364. /**
  365. * Write an octal integer into a buffer.
  366. *
  367. * Uses {@link #formatUnsignedOctalString} to format
  368. * the value as an octal string with leading zeros.
  369. * The converted number is followed by space and NUL
  370. *
  371. * @param value The value to write
  372. * @param buf The buffer to receive the output
  373. * @param offset The starting offset into the buffer
  374. * @param length The size of the output buffer
  375. * @return The updated offset, i.e offset+length
  376. * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer
  377. */
  378. public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
  379. int idx=length-2; // For space and trailing null
  380. formatUnsignedOctalString(value, buf, offset, idx);
  381. buf[offset + idx++] = (byte) ' '; // Trailing space
  382. buf[offset + idx] = 0; // Trailing null
  383. return offset + length;
  384. }
  385. /**
  386. * Write an octal long integer into a buffer.
  387. *
  388. * Uses {@link #formatUnsignedOctalString} to format
  389. * the value as an octal string with leading zeros.
  390. * The converted number is followed by a space.
  391. *
  392. * @param value The value to write as octal
  393. * @param buf The destinationbuffer.
  394. * @param offset The starting offset into the buffer.
  395. * @param length The length of the buffer
  396. * @return The updated offset
  397. * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer
  398. */
  399. public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
  400. final int idx=length-1; // For space
  401. formatUnsignedOctalString(value, buf, offset, idx);
  402. buf[offset + idx] = (byte) ' '; // Trailing space
  403. return offset + length;
  404. }
  405. /**
  406. * Write an long integer into a buffer as an octal string if this
  407. * will fit, or as a binary number otherwise.
  408. *
  409. * Uses {@link #formatUnsignedOctalString} to format
  410. * the value as an octal string with leading zeros.
  411. * The converted number is followed by a space.
  412. *
  413. * @param value The value to write into the buffer.
  414. * @param buf The destination buffer.
  415. * @param offset The starting offset into the buffer.
  416. * @param length The length of the buffer.
  417. * @return The updated offset.
  418. * @throws IllegalArgumentException if the value (and trailer)
  419. * will not fit in the buffer.
  420. */
  421. public static int formatLongOctalOrBinaryBytes(
  422. final long value, final byte[] buf, final int offset, final int length) {
  423. // Check whether we are dealing with UID/GID or SIZE field
  424. final long maxAsOctalChar = length == TarConstants.UIDLEN ? TarConstants.MAXID : TarConstants.MAXSIZE;
  425. final boolean negative = value < 0;
  426. if (!negative && value <= maxAsOctalChar) { // OK to store as octal chars
  427. return formatLongOctalBytes(value, buf, offset, length);
  428. }
  429. if (length < 9) {
  430. formatLongBinary(value, buf, offset, length, negative);
  431. }
  432. formatBigIntegerBinary(value, buf, offset, length, negative);
  433. buf[offset] = (byte) (negative ? 0xff : 0x80);
  434. return offset + length;
  435. }
  436. private static void formatLongBinary(final long value, final byte[] buf,
  437. final int offset, final int length,
  438. final boolean negative) {
  439. final int bits = (length - 1) * 8;
  440. final long max = 1L << bits;
  441. long val = Math.abs(value);
  442. if (val >= max) {
  443. throw new IllegalArgumentException("Value " + value +
  444. " is too large for " + length + " byte field.");
  445. }
  446. if (negative) {
  447. val ^= max - 1;
  448. val |= 0xff << bits;
  449. val++;
  450. }
  451. for (int i = offset + length - 1; i >= offset; i--) {
  452. buf[i] = (byte) val;
  453. val >>= 8;
  454. }
  455. }
  456. private static void formatBigIntegerBinary(final long value, final byte[] buf,
  457. final int offset,
  458. final int length,
  459. final boolean negative) {
  460. final BigInteger val = BigInteger.valueOf(value);
  461. final byte[] b = val.toByteArray();
  462. final int len = b.length;
  463. final int off = offset + length - len;
  464. System.arraycopy(b, 0, buf, off, len);
  465. final byte fill = (byte) (negative ? 0xff : 0);
  466. for (int i = offset + 1; i < off; i++) {
  467. buf[i] = fill;
  468. }
  469. }
  470. /**
  471. * Writes an octal value into a buffer.
  472. *
  473. * Uses {@link #formatUnsignedOctalString} to format
  474. * the value as an octal string with leading zeros.
  475. * The converted number is followed by NUL and then space.
  476. *
  477. * @param value The value to convert
  478. * @param buf The destination buffer
  479. * @param offset The starting offset into the buffer.
  480. * @param length The size of the buffer.
  481. * @return The updated value of offset, i.e. offset+length
  482. * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer
  483. */
  484. public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
  485. int idx=length-2; // for NUL and space
  486. formatUnsignedOctalString(value, buf, offset, idx);
  487. buf[offset + idx++] = 0; // Trailing null
  488. buf[offset + idx] = (byte) ' '; // Trailing space
  489. return offset + length;
  490. }
  491. /**
  492. * Compute the checksum of a tar entry header.
  493. *
  494. * @param buf The tar entry's header buffer.
  495. * @return The computed checksum.
  496. */
  497. public static long computeCheckSum(final byte[] buf) {
  498. long sum = 0;
  499. for (final byte element : buf) {
  500. sum += BYTE_MASK & element;
  501. }
  502. return sum;
  503. }
  504. }