GZIPInputStream.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* GZIPInputStream.java - Input filter for reading gzip file
  2. Copyright (C) 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. import java.io.EOFException;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. /**
  36. * This filter stream is used to decompress a "GZIP" format stream.
  37. * The "GZIP" format is described in RFC 1952.
  38. *
  39. * @author John Leuner
  40. * @author Tom Tromey
  41. * @since JDK 1.1
  42. */
  43. public class GZIPInputStream
  44. extends InflaterInputStream
  45. {
  46. /**
  47. * The magic number found at the start of a GZIP stream.
  48. */
  49. public static final int GZIP_MAGIC = 0x8b1f;
  50. /**
  51. * The mask for bit 0 of the flag byte.
  52. */
  53. static final int FTEXT = 0x1;
  54. /**
  55. * The mask for bit 1 of the flag byte.
  56. */
  57. static final int FHCRC = 0x2;
  58. /**
  59. * The mask for bit 2 of the flag byte.
  60. */
  61. static final int FEXTRA = 0x4;
  62. /**
  63. * The mask for bit 3 of the flag byte.
  64. */
  65. static final int FNAME = 0x8;
  66. /**
  67. * The mask for bit 4 of the flag byte.
  68. */
  69. static final int FCOMMENT = 0x10;
  70. /**
  71. * The CRC-32 checksum value for uncompressed data.
  72. */
  73. protected CRC32 crc;
  74. /**
  75. * Indicates whether or not the end of the stream has been reached.
  76. */
  77. protected boolean eos;
  78. /**
  79. * Indicates whether or not the GZIP header has been read in.
  80. */
  81. private boolean readGZIPHeader;
  82. /**
  83. * Creates a GZIPInputStream with the default buffer size.
  84. *
  85. * @param in The stream to read compressed data from
  86. * (in GZIP format).
  87. *
  88. * @throws IOException if an error occurs during an I/O operation.
  89. */
  90. public GZIPInputStream(InputStream in)
  91. throws IOException
  92. {
  93. this(in, 4096);
  94. }
  95. /**
  96. * Creates a GZIPInputStream with the specified buffer size.
  97. *
  98. * @param in The stream to read compressed data from
  99. * (in GZIP format).
  100. * @param size The size of the buffer to use.
  101. *
  102. * @throws IOException if an error occurs during an I/O operation.
  103. * @throws IllegalArgumentException if <code>size</code>
  104. * is less than or equal to 0.
  105. */
  106. public GZIPInputStream(InputStream in, int size)
  107. throws IOException
  108. {
  109. super(in, new Inflater(true), size);
  110. crc = new CRC32();
  111. readHeader();
  112. }
  113. /**
  114. * Closes the input stream.
  115. *
  116. * @throws IOException if an error occurs during an I/O operation.
  117. */
  118. public void close()
  119. throws IOException
  120. {
  121. // Nothing to do here.
  122. super.close();
  123. }
  124. /**
  125. * Reads in GZIP-compressed data and stores it in uncompressed form
  126. * into an array of bytes. The method will block until either
  127. * enough input data becomes available or the compressed stream
  128. * reaches its end.
  129. *
  130. * @param buf the buffer into which the uncompressed data will
  131. * be stored.
  132. * @param offset the offset indicating where in <code>buf</code>
  133. * the uncompressed data should be placed.
  134. * @param len the number of uncompressed bytes to be read.
  135. */
  136. public int read(byte[] buf, int offset, int len) throws IOException
  137. {
  138. // We first have to slurp in the GZIP header, then we feed all the
  139. // rest of the data to the superclass.
  140. //
  141. // As we do that we continually update the CRC32. Once the data is
  142. // finished, we check the CRC32.
  143. //
  144. // This means we don't need our own buffer, as everything is done
  145. // in the superclass.
  146. if (!readGZIPHeader)
  147. readHeader();
  148. if (eos)
  149. return -1;
  150. // System.err.println("GZIPIS.read(byte[], off, len ... " + offset + " and len " + len);
  151. /* We don't have to read the header,
  152. * so we just grab data from the superclass.
  153. */
  154. int numRead = super.read(buf, offset, len);
  155. if (numRead > 0)
  156. crc.update(buf, offset, numRead);
  157. if (inf.finished())
  158. readFooter();
  159. return numRead;
  160. }
  161. /**
  162. * Reads in the GZIP header.
  163. */
  164. private void readHeader() throws IOException
  165. {
  166. /* 1. Check the two magic bytes */
  167. CRC32 headCRC = new CRC32();
  168. int magic = in.read();
  169. if (magic < 0)
  170. {
  171. eos = true;
  172. return;
  173. }
  174. int magic2 = in.read();
  175. if ((magic + (magic2 << 8)) != GZIP_MAGIC)
  176. throw new IOException("Error in GZIP header, bad magic code");
  177. headCRC.update(magic);
  178. headCRC.update(magic2);
  179. /* 2. Check the compression type (must be 8) */
  180. int CM = in.read();
  181. if (CM != Deflater.DEFLATED)
  182. throw new IOException("Error in GZIP header, data not in deflate format");
  183. headCRC.update(CM);
  184. /* 3. Check the flags */
  185. int flags = in.read();
  186. if (flags < 0)
  187. throw new EOFException("Early EOF in GZIP header");
  188. headCRC.update(flags);
  189. /* This flag byte is divided into individual bits as follows:
  190. bit 0 FTEXT
  191. bit 1 FHCRC
  192. bit 2 FEXTRA
  193. bit 3 FNAME
  194. bit 4 FCOMMENT
  195. bit 5 reserved
  196. bit 6 reserved
  197. bit 7 reserved
  198. */
  199. /* 3.1 Check the reserved bits are zero */
  200. if ((flags & 0xd0) != 0)
  201. throw new IOException("Reserved flag bits in GZIP header != 0");
  202. /* 4.-6. Skip the modification time, extra flags, and OS type */
  203. for (int i=0; i< 6; i++)
  204. {
  205. int readByte = in.read();
  206. if (readByte < 0)
  207. throw new EOFException("Early EOF in GZIP header");
  208. headCRC.update(readByte);
  209. }
  210. /* 7. Read extra field */
  211. if ((flags & FEXTRA) != 0)
  212. {
  213. /* Skip subfield id */
  214. for (int i=0; i< 2; i++)
  215. {
  216. int readByte = in.read();
  217. if (readByte < 0)
  218. throw new EOFException("Early EOF in GZIP header");
  219. headCRC.update(readByte);
  220. }
  221. if (in.read() < 0 || in.read() < 0)
  222. throw new EOFException("Early EOF in GZIP header");
  223. int len1, len2, extraLen;
  224. len1 = in.read();
  225. len2 = in.read();
  226. if ((len1 < 0) || (len2 < 0))
  227. throw new EOFException("Early EOF in GZIP header");
  228. headCRC.update(len1);
  229. headCRC.update(len2);
  230. extraLen = (len1 << 8) | len2;
  231. for (int i = 0; i < extraLen;i++)
  232. {
  233. int readByte = in.read();
  234. if (readByte < 0)
  235. throw new EOFException("Early EOF in GZIP header");
  236. headCRC.update(readByte);
  237. }
  238. }
  239. /* 8. Read file name */
  240. if ((flags & FNAME) != 0)
  241. {
  242. int readByte;
  243. while ( (readByte = in.read()) > 0)
  244. headCRC.update(readByte);
  245. if (readByte < 0)
  246. throw new EOFException("Early EOF in GZIP file name");
  247. headCRC.update(readByte);
  248. }
  249. /* 9. Read comment */
  250. if ((flags & FCOMMENT) != 0)
  251. {
  252. int readByte;
  253. while ( (readByte = in.read()) > 0)
  254. headCRC.update(readByte);
  255. if (readByte < 0)
  256. throw new EOFException("Early EOF in GZIP comment");
  257. headCRC.update(readByte);
  258. }
  259. /* 10. Read header CRC */
  260. if ((flags & FHCRC) != 0)
  261. {
  262. int tempByte;
  263. int crcval = in.read();
  264. if (crcval < 0)
  265. throw new EOFException("Early EOF in GZIP header");
  266. tempByte = in.read();
  267. if (tempByte < 0)
  268. throw new EOFException("Early EOF in GZIP header");
  269. crcval = (crcval << 8) | tempByte;
  270. if (crcval != ((int) headCRC.getValue() & 0xffff))
  271. throw new IOException("Header CRC value mismatch");
  272. }
  273. readGZIPHeader = true;
  274. //System.err.println("Read GZIP header");
  275. }
  276. private void readFooter() throws IOException
  277. {
  278. byte[] footer = new byte[8];
  279. int avail = inf.getRemaining();
  280. if (avail > 8)
  281. avail = 8;
  282. System.arraycopy(buf, len - inf.getRemaining(), footer, 0, avail);
  283. int needed = 8 - avail;
  284. while (needed > 0)
  285. {
  286. int count = in.read(footer, 8-needed, needed);
  287. if (count <= 0)
  288. throw new EOFException("Early EOF in GZIP footer");
  289. needed -= count; //Jewel Jan 16
  290. }
  291. int crcval = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8)
  292. | ((footer[2] & 0xff) << 16) | (footer[3] << 24);
  293. if (crcval != (int) crc.getValue())
  294. throw new IOException("GZIP crc sum mismatch, theirs \""
  295. + Integer.toHexString(crcval)
  296. + "\" and ours \""
  297. + Integer.toHexString( (int) crc.getValue()));
  298. int total = (footer[4] & 0xff) | ((footer[5] & 0xff) << 8)
  299. | ((footer[6] & 0xff) << 16) | (footer[7] << 24);
  300. if (total != inf.getTotalOut())
  301. throw new IOException("Number of bytes mismatch");
  302. /* FIXME" XXX Should we support multiple members.
  303. * Difficult, since there may be some bytes still in buf
  304. */
  305. eos = true;
  306. }
  307. }