gzio.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. /* gzio.c - decompression support for gzip */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2005,2006,2007,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Most of this file was originally the source file "inflate.c", written
  21. * by Mark Adler. It has been very heavily modified. In particular, the
  22. * original would run through the whole file at once, and this version can
  23. * be stopped and restarted on any boundary during the decompression process.
  24. *
  25. * The license and header comments that file are included here.
  26. */
  27. /* inflate.c -- Not copyrighted 1992 by Mark Adler
  28. version c10p1, 10 January 1993 */
  29. /* You can do whatever you like with this source file, though I would
  30. prefer that if you modify it and redistribute it that you include
  31. comments to that effect with your name and the date. Thank you.
  32. */
  33. #include <grub/err.h>
  34. #include <grub/types.h>
  35. #include <grub/mm.h>
  36. #include <grub/misc.h>
  37. #include <grub/fs.h>
  38. #include <grub/file.h>
  39. #include <grub/gzio.h>
  40. GRUB_EXPORT(grub_gzfile_open);
  41. GRUB_EXPORT(grub_gzio_open);
  42. /*
  43. * Window Size
  44. *
  45. * This must be a power of two, and at least 32K for zip's deflate method
  46. */
  47. #define WSIZE 0x8000
  48. #define INBUFSIZ 0x2000
  49. /* The state stored in filesystem-specific data. */
  50. struct grub_gzio
  51. {
  52. /* The underlying file object. */
  53. grub_file_t file;
  54. /* The offset at which the data starts in the underlying file. */
  55. grub_off_t data_offset;
  56. /* The type of current block. */
  57. int block_type;
  58. /* The length of current block. */
  59. int block_len;
  60. /* The flag of the last block. */
  61. int last_block;
  62. /* The flag of codes. */
  63. int code_state;
  64. /* The length of a copy. */
  65. unsigned inflate_n;
  66. /* The index of a copy. */
  67. unsigned inflate_d;
  68. /* The input buffer. */
  69. grub_uint8_t inbuf[INBUFSIZ];
  70. int inbuf_d;
  71. /* The bit buffer. */
  72. unsigned long bb;
  73. /* The bits in the bit buffer. */
  74. unsigned bk;
  75. /* The sliding window in uncompressed data. */
  76. grub_uint8_t slide[WSIZE];
  77. /* Current position in the slide. */
  78. unsigned wp;
  79. /* The literal/length code table. */
  80. struct huft *tl;
  81. /* The distance code table. */
  82. struct huft *td;
  83. /* The lookup bits for the literal/length code table. */
  84. int bl;
  85. /* The lookup bits for the distance code table. */
  86. int bd;
  87. /* The original offset value. */
  88. grub_off_t saved_offset;
  89. };
  90. typedef struct grub_gzio *grub_gzio_t;
  91. /* Declare the filesystem structure for grub_gzio_open. */
  92. static struct grub_fs grub_gzio_fs;
  93. /* Function prototypes */
  94. static void initialize_tables (grub_file_t file);
  95. /* Eat variable-length header fields. */
  96. static int
  97. eat_field (grub_file_t file, int len)
  98. {
  99. char ch = 1;
  100. int not_retval = 1;
  101. do
  102. {
  103. if (len >= 0)
  104. {
  105. if (! (len--))
  106. break;
  107. }
  108. else
  109. {
  110. if (! ch)
  111. break;
  112. }
  113. }
  114. while ((not_retval = grub_file_read (file, &ch, 1)) == 1);
  115. return ! not_retval;
  116. }
  117. /* Little-Endian defines for the 2-byte magic numbers for gzip files. */
  118. #define GZIP_MAGIC grub_le_to_cpu16 (0x8B1F)
  119. #define OLD_GZIP_MAGIC grub_le_to_cpu16 (0x9E1F)
  120. /* Compression methods (see algorithm.doc) */
  121. #define STORED 0
  122. #define COMPRESSED 1
  123. #define PACKED 2
  124. #define LZHED 3
  125. /* methods 4 to 7 reserved */
  126. #define DEFLATED 8
  127. #define MAX_METHODS 9
  128. /* gzip flag byte */
  129. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
  130. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  131. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  132. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  133. #define COMMENT 0x10 /* bit 4 set: file comment present */
  134. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  135. #define RESERVED 0xC0 /* bit 6,7: reserved */
  136. #define UNSUPPORTED_FLAGS (CONTINUATION | ENCRYPTED | RESERVED)
  137. /* inflate block codes */
  138. #define INFLATE_STORED 0
  139. #define INFLATE_FIXED 1
  140. #define INFLATE_DYNAMIC 2
  141. typedef unsigned char uch;
  142. typedef unsigned short ush;
  143. typedef unsigned long ulg;
  144. static int
  145. test_header (grub_file_t file)
  146. {
  147. struct {
  148. grub_uint16_t magic;
  149. grub_uint8_t method;
  150. grub_uint8_t flags;
  151. grub_uint32_t timestamp;
  152. grub_uint8_t extra_flags;
  153. grub_uint8_t os_type;
  154. } hdr;
  155. grub_uint16_t extra_len;
  156. grub_uint32_t orig_len;
  157. grub_gzio_t gzio = file->data;
  158. if (grub_file_tell (gzio->file) != 0)
  159. grub_file_seek (gzio->file, 0);
  160. /*
  161. * This checks if the file is gzipped. If a problem occurs here
  162. * (other than a real error with the disk) then we don't think it
  163. * is a compressed file, and simply mark it as such.
  164. */
  165. if (grub_file_read (gzio->file, &hdr, 10) != 10
  166. || ((hdr.magic != GZIP_MAGIC)
  167. && (hdr.magic != OLD_GZIP_MAGIC)))
  168. {
  169. grub_error (GRUB_ERR_BAD_FILE_TYPE, "no gzip magic found");
  170. return 0;
  171. }
  172. /*
  173. * This does consistency checking on the header data. If a
  174. * problem occurs from here on, then we have corrupt or otherwise
  175. * bad data, and the error should be reported to the user.
  176. */
  177. if (hdr.method != DEFLATED
  178. || (hdr.flags & UNSUPPORTED_FLAGS)
  179. || ((hdr.flags & EXTRA_FIELD)
  180. && (grub_file_read (gzio->file, &extra_len, 2) != 2
  181. || eat_field (gzio->file,
  182. grub_le_to_cpu16 (extra_len))))
  183. || ((hdr.flags & ORIG_NAME) && eat_field (gzio->file, -1))
  184. || ((hdr.flags & COMMENT) && eat_field (gzio->file, -1)))
  185. {
  186. grub_error (GRUB_ERR_BAD_GZIP_DATA, "unsupported gzip format");
  187. return 0;
  188. }
  189. gzio->data_offset = grub_file_tell (gzio->file);
  190. grub_file_seek (gzio->file, grub_file_size (gzio->file) - 4);
  191. if (grub_file_read (gzio->file, &orig_len, 4) != 4)
  192. {
  193. grub_error (GRUB_ERR_BAD_FILE_TYPE, "unsupported gzip format");
  194. return 0;
  195. }
  196. /* FIXME: this does not handle files whose original size is over 4GB.
  197. But how can we know the real original size? */
  198. file->size = grub_le_to_cpu32 (orig_len);
  199. initialize_tables (file);
  200. return 1;
  201. }
  202. /* Huffman code lookup table entry--this entry is four bytes for machines
  203. that have 16-bit pointers (e.g. PC's in the small or medium model).
  204. Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
  205. means that v is a literal, 16 < e < 32 means that v is a pointer to
  206. the next table, which codes e - 16 bits, and lastly e == 99 indicates
  207. an unused code. If a code with e == 99 is looked up, this implies an
  208. error in the data. */
  209. struct huft
  210. {
  211. uch e; /* number of extra bits or operation */
  212. uch b; /* number of bits in this code or subcode */
  213. union
  214. {
  215. ush n; /* literal, length base, or distance base */
  216. struct huft *t; /* pointer to next level of table */
  217. }
  218. v;
  219. };
  220. /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
  221. stream to find repeated byte strings. This is implemented here as a
  222. circular buffer. The index is updated simply by incrementing and then
  223. and'ing with 0x7fff (32K-1). */
  224. /* It is left to other modules to supply the 32K area. It is assumed
  225. to be usable as if it were declared "uch slide[32768];" or as just
  226. "uch *slide;" and then malloc'ed in the latter case. The definition
  227. must be in unzip.h, included above. */
  228. /* Tables for deflate from PKZIP's appnote.txt. */
  229. static unsigned bitorder[] =
  230. { /* Order of the bit length code lengths */
  231. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  232. static ush cplens[] =
  233. { /* Copy lengths for literal codes 257..285 */
  234. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  235. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  236. /* note: see note #13 above about the 258 in this list. */
  237. static ush cplext[] =
  238. { /* Extra bits for literal codes 257..285 */
  239. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  240. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
  241. static ush cpdist[] =
  242. { /* Copy offsets for distance codes 0..29 */
  243. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  244. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  245. 8193, 12289, 16385, 24577};
  246. static ush cpdext[] =
  247. { /* Extra bits for distance codes */
  248. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  249. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  250. 12, 12, 13, 13};
  251. /*
  252. Huffman code decoding is performed using a multi-level table lookup.
  253. The fastest way to decode is to simply build a lookup table whose
  254. size is determined by the longest code. However, the time it takes
  255. to build this table can also be a factor if the data being decoded
  256. is not very long. The most common codes are necessarily the
  257. shortest codes, so those codes dominate the decoding time, and hence
  258. the speed. The idea is you can have a shorter table that decodes the
  259. shorter, more probable codes, and then point to subsidiary tables for
  260. the longer codes. The time it costs to decode the longer codes is
  261. then traded against the time it takes to make longer tables.
  262. This results of this trade are in the variables lbits and dbits
  263. below. lbits is the number of bits the first level table for literal/
  264. length codes can decode in one step, and dbits is the same thing for
  265. the distance codes. Subsequent tables are also less than or equal to
  266. those sizes. These values may be adjusted either when all of the
  267. codes are shorter than that, in which case the longest code length in
  268. bits is used, or when the shortest code is *longer* than the requested
  269. table size, in which case the length of the shortest code in bits is
  270. used.
  271. There are two different values for the two tables, since they code a
  272. different number of possibilities each. The literal/length table
  273. codes 286 possible values, or in a flat code, a little over eight
  274. bits. The distance table codes 30 possible values, or a little less
  275. than five bits, flat. The optimum values for speed end up being
  276. about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  277. The optimum values may differ though from machine to machine, and
  278. possibly even between compilers. Your mileage may vary.
  279. */
  280. static int lbits = 9; /* bits in base literal/length lookup table */
  281. static int dbits = 6; /* bits in base distance lookup table */
  282. /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
  283. #define BMAX 16 /* maximum bit length of any code (16 for explode) */
  284. #define N_MAX 288 /* maximum number of codes in any set */
  285. /* Macros for inflate() bit peeking and grabbing.
  286. The usage is:
  287. NEEDBITS(j)
  288. x = b & mask_bits[j];
  289. DUMPBITS(j)
  290. where NEEDBITS makes sure that b has at least j bits in it, and
  291. DUMPBITS removes the bits from b. The macros use the variable k
  292. for the number of bits in b. Normally, b and k are register
  293. variables for speed, and are initialized at the beginning of a
  294. routine that uses these macros from a global bit buffer and count.
  295. If we assume that EOB will be the longest code, then we will never
  296. ask for bits with NEEDBITS that are beyond the end of the stream.
  297. So, NEEDBITS should not read any more bytes than are needed to
  298. meet the request. Then no bytes need to be "returned" to the buffer
  299. at the end of the last block.
  300. However, this assumption is not true for fixed blocks--the EOB code
  301. is 7 bits, but the other literal/length codes can be 8 or 9 bits.
  302. (The EOB code is shorter than other codes because fixed blocks are
  303. generally short. So, while a block always has an EOB, many other
  304. literal/length codes have a significantly lower probability of
  305. showing up at all.) However, by making the first table have a
  306. lookup of seven bits, the EOB code will be found in that first
  307. lookup, and so will not require that too many bits be pulled from
  308. the stream.
  309. */
  310. static ush mask_bits[] =
  311. {
  312. 0x0000,
  313. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  314. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  315. };
  316. #define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte(file))<<k;k+=8;}} while (0)
  317. #define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
  318. static int
  319. get_byte (grub_file_t file)
  320. {
  321. grub_gzio_t gzio = file->data;
  322. if (grub_file_tell (gzio->file) == (grub_off_t) gzio->data_offset
  323. || gzio->inbuf_d == INBUFSIZ)
  324. {
  325. gzio->inbuf_d = 0;
  326. grub_file_read (gzio->file, gzio->inbuf, INBUFSIZ);
  327. }
  328. return gzio->inbuf[gzio->inbuf_d++];
  329. }
  330. /* more function prototypes */
  331. static int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
  332. struct huft **, int *);
  333. static int huft_free (struct huft *);
  334. static int inflate_codes_in_window (grub_file_t);
  335. /* Given a list of code lengths and a maximum table size, make a set of
  336. tables to decode that set of codes. Return zero on success, one if
  337. the given code set is incomplete (the tables are still built in this
  338. case), two if the input is invalid (all zero length codes or an
  339. oversubscribed set of lengths), and three if not enough memory. */
  340. static int
  341. huft_build (unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
  342. unsigned n, /* number of codes (assumed <= N_MAX) */
  343. unsigned s, /* number of simple-valued codes (0..s-1) */
  344. ush * d, /* list of base values for non-simple codes */
  345. ush * e, /* list of extra bits for non-simple codes */
  346. struct huft **t, /* result: starting table */
  347. int *m) /* maximum lookup bits, returns actual */
  348. {
  349. unsigned a; /* counter for codes of length k */
  350. unsigned c[BMAX + 1]; /* bit length count table */
  351. unsigned f; /* i repeats in table every f entries */
  352. int g; /* maximum code length */
  353. int h; /* table level */
  354. register unsigned i; /* counter, current code */
  355. register unsigned j; /* counter */
  356. register int k; /* number of bits in current code */
  357. int l; /* bits per table (returned in m) */
  358. register unsigned *p; /* pointer into c[], b[], or v[] */
  359. register struct huft *q; /* points to current table */
  360. struct huft r; /* table entry for structure assignment */
  361. struct huft *u[BMAX]; /* table stack */
  362. unsigned v[N_MAX]; /* values in order of bit length */
  363. register int w; /* bits before this table == (l * h) */
  364. unsigned x[BMAX + 1]; /* bit offsets, then code stack */
  365. unsigned *xp; /* pointer into x */
  366. int y; /* number of dummy codes added */
  367. unsigned z; /* number of entries in current table */
  368. /* Generate counts for each bit length */
  369. grub_memset ((char *) c, 0, sizeof (c));
  370. p = b;
  371. i = n;
  372. do
  373. {
  374. c[*p]++; /* assume all entries <= BMAX */
  375. p++; /* Can't combine with above line (Solaris bug) */
  376. }
  377. while (--i);
  378. if (c[0] == n) /* null input--all zero length codes */
  379. {
  380. *t = (struct huft *) NULL;
  381. *m = 0;
  382. return 0;
  383. }
  384. /* Find minimum and maximum length, bound *m by those */
  385. l = *m;
  386. for (j = 1; j <= BMAX; j++)
  387. if (c[j])
  388. break;
  389. k = j; /* minimum code length */
  390. if ((unsigned) l < j)
  391. l = j;
  392. for (i = BMAX; i; i--)
  393. if (c[i])
  394. break;
  395. g = i; /* maximum code length */
  396. if ((unsigned) l > i)
  397. l = i;
  398. *m = l;
  399. /* Adjust last length count to fill out codes, if needed */
  400. for (y = 1 << j; j < i; j++, y <<= 1)
  401. if ((y -= c[j]) < 0)
  402. return 2; /* bad input: more codes than bits */
  403. if ((y -= c[i]) < 0)
  404. return 2;
  405. c[i] += y;
  406. /* Generate starting offsets into the value table for each length */
  407. x[1] = j = 0;
  408. p = c + 1;
  409. xp = x + 2;
  410. while (--i)
  411. { /* note that i == g from above */
  412. *xp++ = (j += *p++);
  413. }
  414. /* Make a table of values in order of bit lengths */
  415. p = b;
  416. i = 0;
  417. do
  418. {
  419. if ((j = *p++) != 0)
  420. v[x[j]++] = i;
  421. }
  422. while (++i < n);
  423. /* Generate the Huffman codes and for each, make the table entries */
  424. x[0] = i = 0; /* first Huffman code is zero */
  425. p = v; /* grab values in bit order */
  426. h = -1; /* no tables yet--level -1 */
  427. w = -l; /* bits decoded == (l * h) */
  428. u[0] = (struct huft *) NULL; /* just to keep compilers happy */
  429. q = (struct huft *) NULL; /* ditto */
  430. z = 0; /* ditto */
  431. /* go through the bit lengths (k already is bits in shortest code) */
  432. for (; k <= g; k++)
  433. {
  434. a = c[k];
  435. while (a--)
  436. {
  437. /* here i is the Huffman code of length k bits for value *p */
  438. /* make tables up to required level */
  439. while (k > w + l)
  440. {
  441. h++;
  442. w += l; /* previous table always l bits */
  443. /* compute minimum size table less than or equal to l bits */
  444. z = (z = (unsigned) (g - w)) > (unsigned) l ? (unsigned) l : z; /* upper limit on table size */
  445. if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  446. { /* too few codes for k-w bit table */
  447. f -= a + 1; /* deduct codes from patterns left */
  448. xp = c + k;
  449. while (++j < z) /* try smaller tables up to z bits */
  450. {
  451. if ((f <<= 1) <= *++xp)
  452. break; /* enough codes to use up j bits */
  453. f -= *xp; /* else deduct codes from patterns */
  454. }
  455. }
  456. z = 1 << j; /* table entries for j-bit table */
  457. /* allocate and link in new table */
  458. q = (struct huft *) grub_malloc ((z + 1) * sizeof (struct huft));
  459. if (! q)
  460. {
  461. if (h)
  462. huft_free (u[0]);
  463. return 3;
  464. }
  465. *t = q + 1; /* link to list for huft_free() */
  466. *(t = &(q->v.t)) = (struct huft *) NULL;
  467. u[h] = ++q; /* table starts after link */
  468. /* connect to last table, if there is one */
  469. if (h)
  470. {
  471. x[h] = i; /* save pattern for backing up */
  472. r.b = (uch) l; /* bits to dump before this table */
  473. r.e = (uch) (16 + j); /* bits in this table */
  474. r.v.t = q; /* pointer to this table */
  475. j = i >> (w - l); /* (get around Turbo C bug) */
  476. u[h - 1][j] = r; /* connect to last table */
  477. }
  478. }
  479. /* set up table entry in r */
  480. r.b = (uch) (k - w);
  481. if (p >= v + n)
  482. r.e = 99; /* out of values--invalid code */
  483. else if (*p < s)
  484. {
  485. r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
  486. r.v.n = (ush) (*p); /* simple code is just the value */
  487. p++; /* one compiler does not like *p++ */
  488. }
  489. else
  490. {
  491. r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
  492. r.v.n = d[*p++ - s];
  493. }
  494. /* fill code-like entries with r */
  495. f = 1 << (k - w);
  496. for (j = i >> w; j < z; j += f)
  497. q[j] = r;
  498. /* backwards increment the k-bit code i */
  499. for (j = 1 << (k - 1); i & j; j >>= 1)
  500. i ^= j;
  501. i ^= j;
  502. /* backup over finished tables */
  503. while ((i & ((1 << w) - 1)) != x[h])
  504. {
  505. h--; /* don't need to update q */
  506. w -= l;
  507. }
  508. }
  509. }
  510. /* Return true (1) if we were given an incomplete table */
  511. return y != 0 && g != 1;
  512. }
  513. /* Free the malloc'ed tables built by huft_build(), which makes a linked
  514. list of the tables it made, with the links in a dummy first entry of
  515. each table. */
  516. static int
  517. huft_free (struct huft *t)
  518. {
  519. register struct huft *p, *q;
  520. /* Go through linked list, freeing from the malloced (t[-1]) address. */
  521. p = t;
  522. while (p != (struct huft *) NULL)
  523. {
  524. q = (--p)->v.t;
  525. grub_free ((char *) p);
  526. p = q;
  527. }
  528. return 0;
  529. }
  530. /*
  531. * inflate (decompress) the codes in a deflated (compressed) block.
  532. * Return an error code or zero if it all goes ok.
  533. */
  534. static int
  535. inflate_codes_in_window (grub_file_t file)
  536. {
  537. register unsigned e; /* table entry flag/number of extra bits */
  538. unsigned n, d; /* length and index for copy */
  539. unsigned w; /* current window position */
  540. struct huft *t; /* pointer to table entry */
  541. unsigned ml, md; /* masks for bl and bd bits */
  542. register ulg b; /* bit buffer */
  543. register unsigned k; /* number of bits in bit buffer */
  544. grub_gzio_t gzio = file->data;
  545. /* make local copies of globals */
  546. d = gzio->inflate_d;
  547. n = gzio->inflate_n;
  548. b = gzio->bb; /* initialize bit buffer */
  549. k = gzio->bk;
  550. w = gzio->wp; /* initialize window position */
  551. /* inflate the coded data */
  552. ml = mask_bits[gzio->bl]; /* precompute masks for speed */
  553. md = mask_bits[gzio->bd];
  554. for (;;) /* do until end of block */
  555. {
  556. if (! gzio->code_state)
  557. {
  558. NEEDBITS ((unsigned) gzio->bl);
  559. if ((e = (t = gzio->tl + ((unsigned) b & ml))->e) > 16)
  560. do
  561. {
  562. if (e == 99)
  563. {
  564. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  565. "an unused code found");
  566. return 1;
  567. }
  568. DUMPBITS (t->b);
  569. e -= 16;
  570. NEEDBITS (e);
  571. }
  572. while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
  573. DUMPBITS (t->b);
  574. if (e == 16) /* then it's a literal */
  575. {
  576. gzio->slide[w++] = (uch) t->v.n;
  577. if (w == WSIZE)
  578. break;
  579. }
  580. else
  581. /* it's an EOB or a length */
  582. {
  583. /* exit if end of block */
  584. if (e == 15)
  585. {
  586. gzio->block_len = 0;
  587. break;
  588. }
  589. /* get length of block to copy */
  590. NEEDBITS (e);
  591. n = t->v.n + ((unsigned) b & mask_bits[e]);
  592. DUMPBITS (e);
  593. /* decode distance of block to copy */
  594. NEEDBITS ((unsigned) gzio->bd);
  595. if ((e = (t = gzio->td + ((unsigned) b & md))->e) > 16)
  596. do
  597. {
  598. if (e == 99)
  599. {
  600. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  601. "an unused code found");
  602. return 1;
  603. }
  604. DUMPBITS (t->b);
  605. e -= 16;
  606. NEEDBITS (e);
  607. }
  608. while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
  609. > 16);
  610. DUMPBITS (t->b);
  611. NEEDBITS (e);
  612. d = w - t->v.n - ((unsigned) b & mask_bits[e]);
  613. DUMPBITS (e);
  614. gzio->code_state++;
  615. }
  616. }
  617. if (gzio->code_state)
  618. {
  619. /* do the copy */
  620. do
  621. {
  622. n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
  623. : e);
  624. if (w - d >= e)
  625. {
  626. grub_memmove (gzio->slide + w, gzio->slide + d, e);
  627. w += e;
  628. d += e;
  629. }
  630. else
  631. /* purposefully use the overlap for extra copies here!! */
  632. {
  633. while (e--)
  634. gzio->slide[w++] = gzio->slide[d++];
  635. }
  636. if (w == WSIZE)
  637. break;
  638. }
  639. while (n);
  640. if (! n)
  641. gzio->code_state--;
  642. /* did we break from the loop too soon? */
  643. if (w == WSIZE)
  644. break;
  645. }
  646. }
  647. /* restore the globals from the locals */
  648. gzio->inflate_d = d;
  649. gzio->inflate_n = n;
  650. gzio->wp = w; /* restore global window pointer */
  651. gzio->bb = b; /* restore global bit buffer */
  652. gzio->bk = k;
  653. return ! gzio->block_len;
  654. }
  655. /* get header for an inflated type 0 (stored) block. */
  656. static void
  657. init_stored_block (grub_file_t file)
  658. {
  659. register ulg b; /* bit buffer */
  660. register unsigned k; /* number of bits in bit buffer */
  661. grub_gzio_t gzio = file->data;
  662. /* make local copies of globals */
  663. b = gzio->bb; /* initialize bit buffer */
  664. k = gzio->bk;
  665. /* go to byte boundary */
  666. DUMPBITS (k & 7);
  667. /* get the length and its complement */
  668. NEEDBITS (16);
  669. gzio->block_len = ((unsigned) b & 0xffff);
  670. DUMPBITS (16);
  671. NEEDBITS (16);
  672. if (gzio->block_len != (int) ((~b) & 0xffff))
  673. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  674. "the length of a stored block does not match");
  675. DUMPBITS (16);
  676. /* restore global variables */
  677. gzio->bb = b;
  678. gzio->bk = k;
  679. }
  680. /* get header for an inflated type 1 (fixed Huffman codes) block. We should
  681. either replace this with a custom decoder, or at least precompute the
  682. Huffman tables. */
  683. static void
  684. init_fixed_block (grub_file_t file)
  685. {
  686. int i; /* temporary variable */
  687. unsigned l[288]; /* length list for huft_build */
  688. grub_gzio_t gzio = file->data;
  689. /* set up literal table */
  690. for (i = 0; i < 144; i++)
  691. l[i] = 8;
  692. for (; i < 256; i++)
  693. l[i] = 9;
  694. for (; i < 280; i++)
  695. l[i] = 7;
  696. for (; i < 288; i++) /* make a complete, but wrong code set */
  697. l[i] = 8;
  698. gzio->bl = 7;
  699. if (huft_build (l, 288, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
  700. {
  701. if (grub_errno == GRUB_ERR_NONE)
  702. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  703. "failed in building a Huffman code table");
  704. return;
  705. }
  706. /* set up distance table */
  707. for (i = 0; i < 30; i++) /* make an incomplete code set */
  708. l[i] = 5;
  709. gzio->bd = 5;
  710. if (huft_build (l, 30, 0, cpdist, cpdext, &gzio->td, &gzio->bd) > 1)
  711. {
  712. if (grub_errno == GRUB_ERR_NONE)
  713. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  714. "failed in building a Huffman code table");
  715. huft_free (gzio->tl);
  716. gzio->tl = 0;
  717. return;
  718. }
  719. /* indicate we're now working on a block */
  720. gzio->code_state = 0;
  721. gzio->block_len++;
  722. }
  723. /* get header for an inflated type 2 (dynamic Huffman codes) block. */
  724. static void
  725. init_dynamic_block (grub_file_t file)
  726. {
  727. int i; /* temporary variables */
  728. unsigned j;
  729. unsigned l; /* last length */
  730. unsigned m; /* mask for bit lengths table */
  731. unsigned n; /* number of lengths to get */
  732. unsigned nb; /* number of bit length codes */
  733. unsigned nl; /* number of literal/length codes */
  734. unsigned nd; /* number of distance codes */
  735. unsigned ll[286 + 30]; /* literal/length and distance code lengths */
  736. register ulg b; /* bit buffer */
  737. register unsigned k; /* number of bits in bit buffer */
  738. grub_gzio_t gzio = file->data;
  739. /* make local bit buffer */
  740. b = gzio->bb;
  741. k = gzio->bk;
  742. /* read in table lengths */
  743. NEEDBITS (5);
  744. nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
  745. DUMPBITS (5);
  746. NEEDBITS (5);
  747. nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
  748. DUMPBITS (5);
  749. NEEDBITS (4);
  750. nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
  751. DUMPBITS (4);
  752. if (nl > 286 || nd > 30)
  753. {
  754. grub_error (GRUB_ERR_BAD_GZIP_DATA, "too much data");
  755. return;
  756. }
  757. /* read in bit-length-code lengths */
  758. for (j = 0; j < nb; j++)
  759. {
  760. NEEDBITS (3);
  761. ll[bitorder[j]] = (unsigned) b & 7;
  762. DUMPBITS (3);
  763. }
  764. for (; j < 19; j++)
  765. ll[bitorder[j]] = 0;
  766. /* build decoding table for trees--single level, 7 bit lookup */
  767. gzio->bl = 7;
  768. if (huft_build (ll, 19, 19, NULL, NULL, &gzio->tl, &gzio->bl) != 0)
  769. {
  770. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  771. "failed in building a Huffman code table");
  772. return;
  773. }
  774. /* read in literal and distance code lengths */
  775. n = nl + nd;
  776. m = mask_bits[gzio->bl];
  777. i = l = 0;
  778. while ((unsigned) i < n)
  779. {
  780. NEEDBITS ((unsigned) gzio->bl);
  781. j = (gzio->td = gzio->tl + ((unsigned) b & m))->b;
  782. DUMPBITS (j);
  783. j = gzio->td->v.n;
  784. if (j < 16) /* length of code in bits (0..15) */
  785. ll[i++] = l = j; /* save last length in l */
  786. else if (j == 16) /* repeat last length 3 to 6 times */
  787. {
  788. NEEDBITS (2);
  789. j = 3 + ((unsigned) b & 3);
  790. DUMPBITS (2);
  791. if ((unsigned) i + j > n)
  792. {
  793. grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
  794. return;
  795. }
  796. while (j--)
  797. ll[i++] = l;
  798. }
  799. else if (j == 17) /* 3 to 10 zero length codes */
  800. {
  801. NEEDBITS (3);
  802. j = 3 + ((unsigned) b & 7);
  803. DUMPBITS (3);
  804. if ((unsigned) i + j > n)
  805. {
  806. grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
  807. return;
  808. }
  809. while (j--)
  810. ll[i++] = 0;
  811. l = 0;
  812. }
  813. else
  814. /* j == 18: 11 to 138 zero length codes */
  815. {
  816. NEEDBITS (7);
  817. j = 11 + ((unsigned) b & 0x7f);
  818. DUMPBITS (7);
  819. if ((unsigned) i + j > n)
  820. {
  821. grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
  822. return;
  823. }
  824. while (j--)
  825. ll[i++] = 0;
  826. l = 0;
  827. }
  828. }
  829. /* free decoding table for trees */
  830. huft_free (gzio->tl);
  831. gzio->td = 0;
  832. gzio->tl = 0;
  833. /* restore the global bit buffer */
  834. gzio->bb = b;
  835. gzio->bk = k;
  836. /* build the decoding tables for literal/length and distance codes */
  837. gzio->bl = lbits;
  838. if (huft_build (ll, nl, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
  839. {
  840. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  841. "failed in building a Huffman code table");
  842. return;
  843. }
  844. gzio->bd = dbits;
  845. if (huft_build (ll + nl, nd, 0, cpdist, cpdext, &gzio->td, &gzio->bd) != 0)
  846. {
  847. huft_free (gzio->tl);
  848. gzio->tl = 0;
  849. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  850. "failed in building a Huffman code table");
  851. return;
  852. }
  853. /* indicate we're now working on a block */
  854. gzio->code_state = 0;
  855. gzio->block_len++;
  856. }
  857. static void
  858. get_new_block (grub_file_t file)
  859. {
  860. register ulg b; /* bit buffer */
  861. register unsigned k; /* number of bits in bit buffer */
  862. grub_gzio_t gzio = file->data;
  863. /* make local bit buffer */
  864. b = gzio->bb;
  865. k = gzio->bk;
  866. /* read in last block bit */
  867. NEEDBITS (1);
  868. gzio->last_block = (int) b & 1;
  869. DUMPBITS (1);
  870. /* read in block type */
  871. NEEDBITS (2);
  872. gzio->block_type = (unsigned) b & 3;
  873. DUMPBITS (2);
  874. /* restore the global bit buffer */
  875. gzio->bb = b;
  876. gzio->bk = k;
  877. switch (gzio->block_type)
  878. {
  879. case INFLATE_STORED:
  880. init_stored_block (file);
  881. break;
  882. case INFLATE_FIXED:
  883. init_fixed_block (file);
  884. break;
  885. case INFLATE_DYNAMIC:
  886. init_dynamic_block (file);
  887. break;
  888. default:
  889. break;
  890. }
  891. }
  892. static void
  893. inflate_window (grub_file_t file)
  894. {
  895. grub_gzio_t gzio = file->data;
  896. /* initialize window */
  897. gzio->wp = 0;
  898. /*
  899. * Main decompression loop.
  900. */
  901. while (gzio->wp < WSIZE && grub_errno == GRUB_ERR_NONE)
  902. {
  903. if (! gzio->block_len)
  904. {
  905. if (gzio->last_block)
  906. break;
  907. get_new_block (file);
  908. }
  909. if (gzio->block_type > INFLATE_DYNAMIC)
  910. grub_error (GRUB_ERR_BAD_GZIP_DATA,
  911. "unknown block type %d", gzio->block_type);
  912. if (grub_errno != GRUB_ERR_NONE)
  913. return;
  914. /*
  915. * Expand stored block here.
  916. */
  917. if (gzio->block_type == INFLATE_STORED)
  918. {
  919. int w = gzio->wp;
  920. /*
  921. * This is basically a glorified pass-through
  922. */
  923. while (gzio->block_len && w < WSIZE && grub_errno == GRUB_ERR_NONE)
  924. {
  925. gzio->slide[w++] = get_byte (file);
  926. gzio->block_len--;
  927. }
  928. gzio->wp = w;
  929. continue;
  930. }
  931. /*
  932. * Expand other kind of block.
  933. */
  934. if (inflate_codes_in_window (file))
  935. {
  936. huft_free (gzio->tl);
  937. huft_free (gzio->td);
  938. gzio->tl = 0;
  939. gzio->td = 0;
  940. }
  941. }
  942. gzio->saved_offset += WSIZE;
  943. /* XXX do CRC calculation here! */
  944. }
  945. static void
  946. initialize_tables (grub_file_t file)
  947. {
  948. grub_gzio_t gzio = file->data;
  949. gzio->saved_offset = 0;
  950. grub_file_seek (gzio->file, gzio->data_offset);
  951. /* Initialize the bit buffer. */
  952. gzio->bk = 0;
  953. gzio->bb = 0;
  954. /* Reset partial decompression code. */
  955. gzio->last_block = 0;
  956. gzio->block_len = 0;
  957. /* Reset memory allocation stuff. */
  958. huft_free (gzio->tl);
  959. huft_free (gzio->td);
  960. }
  961. /* Open a new decompressing object on the top of IO. If TRANSPARENT is true,
  962. even if IO does not contain data compressed by gzip, return a valid file
  963. object. Note that this function won't close IO, even if an error occurs. */
  964. grub_file_t
  965. grub_gzio_open (grub_file_t io, int transparent)
  966. {
  967. grub_file_t file;
  968. grub_gzio_t gzio = 0;
  969. file = (grub_file_t) grub_malloc (sizeof (*file));
  970. if (! file)
  971. return 0;
  972. gzio = grub_zalloc (sizeof (*gzio));
  973. if (! gzio)
  974. {
  975. grub_free (file);
  976. return 0;
  977. }
  978. gzio->file = io;
  979. file->device = io->device;
  980. file->offset = 0;
  981. file->data = gzio;
  982. file->read_hook = 0;
  983. file->fs = &grub_gzio_fs;
  984. if (! test_header (file))
  985. {
  986. grub_free (gzio);
  987. grub_free (file);
  988. grub_file_seek (io, 0);
  989. if (grub_errno == GRUB_ERR_BAD_FILE_TYPE && transparent)
  990. {
  991. grub_errno = GRUB_ERR_NONE;
  992. return io;
  993. }
  994. else
  995. return 0;
  996. }
  997. return file;
  998. }
  999. /* This is similar to grub_gzio_open, but takes a file name as an argument. */
  1000. grub_file_t
  1001. grub_gzfile_open (const char *name, int transparent)
  1002. {
  1003. grub_file_t io, file;
  1004. io = grub_file_open (name);
  1005. if (! io)
  1006. return 0;
  1007. file = grub_gzio_open (io, transparent);
  1008. if (! file)
  1009. {
  1010. grub_file_close (io);
  1011. return 0;
  1012. }
  1013. return file;
  1014. }
  1015. static grub_ssize_t
  1016. grub_gzio_read (grub_file_t file, char *buf, grub_size_t len)
  1017. {
  1018. grub_ssize_t ret = 0;
  1019. grub_gzio_t gzio = file->data;
  1020. grub_off_t offset;
  1021. /* Do we reset decompression to the beginning of the file? */
  1022. if (gzio->saved_offset > file->offset + WSIZE)
  1023. initialize_tables (file);
  1024. /*
  1025. * This loop operates upon uncompressed data only. The only
  1026. * special thing it does is to make sure the decompression
  1027. * window is within the range of data it needs.
  1028. */
  1029. offset = file->offset;
  1030. while (len > 0 && grub_errno == GRUB_ERR_NONE)
  1031. {
  1032. register grub_size_t size;
  1033. register char *srcaddr;
  1034. while (offset >= gzio->saved_offset)
  1035. inflate_window (file);
  1036. srcaddr = (char *) ((offset & (WSIZE - 1)) + gzio->slide);
  1037. size = gzio->saved_offset - offset;
  1038. if (size > len)
  1039. size = len;
  1040. grub_memmove (buf, srcaddr, size);
  1041. buf += size;
  1042. len -= size;
  1043. ret += size;
  1044. offset += size;
  1045. }
  1046. if (grub_errno != GRUB_ERR_NONE)
  1047. ret = -1;
  1048. return ret;
  1049. }
  1050. /* Release everything, including the underlying file object. */
  1051. static grub_err_t
  1052. grub_gzio_close (grub_file_t file)
  1053. {
  1054. grub_gzio_t gzio = file->data;
  1055. grub_file_close (gzio->file);
  1056. huft_free (gzio->tl);
  1057. huft_free (gzio->td);
  1058. grub_free (gzio);
  1059. /* No need to close the same device twice. */
  1060. file->device = 0;
  1061. return grub_errno;
  1062. }
  1063. static struct grub_fs grub_gzio_fs =
  1064. {
  1065. .name = "gzio",
  1066. .dir = 0,
  1067. .open = 0,
  1068. .read = grub_gzio_read,
  1069. .close = grub_gzio_close,
  1070. .label = 0,
  1071. .next = 0
  1072. };