gzio.c 39 KB

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