gunzip_util.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2007 David Gibson, IBM Corporation.
  3. * Based on earlier work, Copyright (C) Paul Mackerras 1997.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <stddef.h>
  11. #include "string.h"
  12. #include "stdio.h"
  13. #include "ops.h"
  14. #include "gunzip_util.h"
  15. #define HEAD_CRC 2
  16. #define EXTRA_FIELD 4
  17. #define ORIG_NAME 8
  18. #define COMMENT 0x10
  19. #define RESERVED 0xe0
  20. /**
  21. * gunzip_start - prepare to decompress gzip data
  22. * @state: decompressor state structure to be initialized
  23. * @src: buffer containing gzip compressed or uncompressed data
  24. * @srclen: size in bytes of the buffer at src
  25. *
  26. * If the buffer at @src contains a gzip header, this function
  27. * initializes zlib to decompress the data, storing the decompression
  28. * state in @state. The other functions in this file can then be used
  29. * to decompress data from the gzipped stream.
  30. *
  31. * If the buffer at @src does not contain a gzip header, it is assumed
  32. * to contain uncompressed data. The buffer information is recorded
  33. * in @state and the other functions in this file will simply copy
  34. * data from the uncompressed data stream at @src.
  35. *
  36. * Any errors, such as bad compressed data, cause an error to be
  37. * printed an the platform's exit() function to be called.
  38. */
  39. void gunzip_start(struct gunzip_state *state, void *src, int srclen)
  40. {
  41. char *hdr = src;
  42. int hdrlen = 0;
  43. memset(state, 0, sizeof(*state));
  44. /* Check for gzip magic number */
  45. if ((hdr[0] == 0x1f) && (hdr[1] == 0x8b)) {
  46. /* gzip data, initialize zlib parameters */
  47. int r, flags;
  48. state->s.workspace = state->scratch;
  49. if (zlib_inflate_workspacesize() > sizeof(state->scratch))
  50. fatal("insufficient scratch space for gunzip\n\r");
  51. /* skip header */
  52. hdrlen = 10;
  53. flags = hdr[3];
  54. if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0)
  55. fatal("bad gzipped data\n\r");
  56. if ((flags & EXTRA_FIELD) != 0)
  57. hdrlen = 12 + hdr[10] + (hdr[11] << 8);
  58. if ((flags & ORIG_NAME) != 0)
  59. while (hdr[hdrlen++] != 0)
  60. ;
  61. if ((flags & COMMENT) != 0)
  62. while (hdr[hdrlen++] != 0)
  63. ;
  64. if ((flags & HEAD_CRC) != 0)
  65. hdrlen += 2;
  66. if (hdrlen >= srclen)
  67. fatal("gunzip_start: ran out of data in header\n\r");
  68. r = zlib_inflateInit2(&state->s, -MAX_WBITS);
  69. if (r != Z_OK)
  70. fatal("inflateInit2 returned %d\n\r", r);
  71. }
  72. state->s.total_in = hdrlen;
  73. state->s.next_in = src + hdrlen;
  74. state->s.avail_in = srclen - hdrlen;
  75. }
  76. /**
  77. * gunzip_partial - extract bytes from a gzip data stream
  78. * @state: gzip state structure previously initialized by gunzip_start()
  79. * @dst: buffer to store extracted data
  80. * @dstlen: maximum number of bytes to extract
  81. *
  82. * This function extracts at most @dstlen bytes from the data stream
  83. * previously associated with @state by gunzip_start(), decompressing
  84. * if necessary. Exactly @dstlen bytes are extracted unless the data
  85. * stream doesn't contain enough bytes, in which case the entire
  86. * remainder of the stream is decompressed.
  87. *
  88. * Returns the actual number of bytes extracted. If any errors occur,
  89. * such as a corrupted compressed stream, an error is printed an the
  90. * platform's exit() function is called.
  91. */
  92. int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
  93. {
  94. int len;
  95. if (state->s.workspace) {
  96. /* gunzipping */
  97. int r;
  98. state->s.next_out = dst;
  99. state->s.avail_out = dstlen;
  100. r = zlib_inflate(&state->s, Z_FULL_FLUSH);
  101. if (r != Z_OK && r != Z_STREAM_END)
  102. fatal("inflate returned %d msg: %s\n\r", r, state->s.msg);
  103. len = state->s.next_out - (Byte *)dst;
  104. } else {
  105. /* uncompressed image */
  106. len = min(state->s.avail_in, (uLong)dstlen);
  107. memcpy(dst, state->s.next_in, len);
  108. state->s.next_in += len;
  109. state->s.avail_in -= len;
  110. }
  111. return len;
  112. }
  113. /**
  114. * gunzip_exactly - extract a fixed number of bytes from a gzip data stream
  115. * @state: gzip state structure previously initialized by gunzip_start()
  116. * @dst: buffer to store extracted data
  117. * @dstlen: number of bytes to extract
  118. *
  119. * This function extracts exactly @dstlen bytes from the data stream
  120. * previously associated with @state by gunzip_start(), decompressing
  121. * if necessary.
  122. *
  123. * If there are less @dstlen bytes available in the data stream, or if
  124. * any other errors occur, such as a corrupted compressed stream, an
  125. * error is printed an the platform's exit() function is called.
  126. */
  127. void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen)
  128. {
  129. int len;
  130. len = gunzip_partial(state, dst, dstlen);
  131. if (len < dstlen)
  132. fatal("\n\rgunzip_exactly: ran out of data!"
  133. " Wanted %d, got %d.\n\r", dstlen, len);
  134. }
  135. /**
  136. * gunzip_discard - discard bytes from a gzip data stream
  137. * @state: gzip state structure previously initialized by gunzip_start()
  138. * @len: number of bytes to discard
  139. *
  140. * This function extracts, then discards exactly @len bytes from the
  141. * data stream previously associated with @state by gunzip_start().
  142. * Subsequent gunzip_partial(), gunzip_exactly() or gunzip_finish()
  143. * calls will extract the data following the discarded bytes in the
  144. * data stream.
  145. *
  146. * If there are less @len bytes available in the data stream, or if
  147. * any other errors occur, such as a corrupted compressed stream, an
  148. * error is printed an the platform's exit() function is called.
  149. */
  150. void gunzip_discard(struct gunzip_state *state, int len)
  151. {
  152. static char discard_buf[128];
  153. while (len > sizeof(discard_buf)) {
  154. gunzip_exactly(state, discard_buf, sizeof(discard_buf));
  155. len -= sizeof(discard_buf);
  156. }
  157. if (len > 0)
  158. gunzip_exactly(state, discard_buf, len);
  159. }
  160. /**
  161. * gunzip_finish - extract all remaining bytes from a gzip data stream
  162. * @state: gzip state structure previously initialized by gunzip_start()
  163. * @dst: buffer to store extracted data
  164. * @dstlen: maximum number of bytes to extract
  165. *
  166. * This function extracts all remaining data, or at most @dstlen
  167. * bytes, from the stream previously associated with @state by
  168. * gunzip_start(). zlib is then shut down, so it is an error to use
  169. * any of the functions in this file on @state until it is
  170. * re-initialized with another call to gunzip_start().
  171. *
  172. * If any errors occur, such as a corrupted compressed stream, an
  173. * error is printed an the platform's exit() function is called.
  174. */
  175. int gunzip_finish(struct gunzip_state *state, void *dst, int dstlen)
  176. {
  177. int len;
  178. len = gunzip_partial(state, dst, dstlen);
  179. if (state->s.workspace) {
  180. zlib_inflateEnd(&state->s);
  181. }
  182. return len;
  183. }