gunzip_util.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.next_in = src + hdrlen;
  73. state->s.avail_in = srclen - hdrlen;
  74. }
  75. /**
  76. * gunzip_partial - extract bytes from a gzip data stream
  77. * @state: gzip state structure previously initialized by gunzip_start()
  78. * @dst: buffer to store extracted data
  79. * @dstlen: maximum number of bytes to extract
  80. *
  81. * This function extracts at most @dstlen bytes from the data stream
  82. * previously associated with @state by gunzip_start(), decompressing
  83. * if necessary. Exactly @dstlen bytes are extracted unless the data
  84. * stream doesn't contain enough bytes, in which case the entire
  85. * remainder of the stream is decompressed.
  86. *
  87. * Returns the actual number of bytes extracted. If any errors occur,
  88. * such as a corrupted compressed stream, an error is printed an the
  89. * platform's exit() function is called.
  90. */
  91. int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
  92. {
  93. int len;
  94. if (state->s.workspace) {
  95. /* gunzipping */
  96. int r;
  97. state->s.next_out = dst;
  98. state->s.avail_out = dstlen;
  99. r = zlib_inflate(&state->s, Z_FULL_FLUSH);
  100. if (r != Z_OK && r != Z_STREAM_END)
  101. fatal("inflate returned %d msg: %s\n\r", r, state->s.msg);
  102. len = state->s.next_out - (unsigned char *)dst;
  103. } else {
  104. /* uncompressed image */
  105. len = min(state->s.avail_in, (unsigned)dstlen);
  106. memcpy(dst, state->s.next_in, len);
  107. state->s.next_in += len;
  108. state->s.avail_in -= len;
  109. }
  110. return len;
  111. }
  112. /**
  113. * gunzip_exactly - extract a fixed number of bytes from a gzip data stream
  114. * @state: gzip state structure previously initialized by gunzip_start()
  115. * @dst: buffer to store extracted data
  116. * @dstlen: number of bytes to extract
  117. *
  118. * This function extracts exactly @dstlen bytes from the data stream
  119. * previously associated with @state by gunzip_start(), decompressing
  120. * if necessary.
  121. *
  122. * If there are less @dstlen bytes available in the data stream, or if
  123. * any other errors occur, such as a corrupted compressed stream, an
  124. * error is printed an the platform's exit() function is called.
  125. */
  126. void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen)
  127. {
  128. int len;
  129. len = gunzip_partial(state, dst, dstlen);
  130. if (len < dstlen)
  131. fatal("\n\rgunzip_exactly: ran out of data!"
  132. " Wanted %d, got %d.\n\r", dstlen, len);
  133. }
  134. /**
  135. * gunzip_discard - discard bytes from a gzip data stream
  136. * @state: gzip state structure previously initialized by gunzip_start()
  137. * @len: number of bytes to discard
  138. *
  139. * This function extracts, then discards exactly @len bytes from the
  140. * data stream previously associated with @state by gunzip_start().
  141. * Subsequent gunzip_partial(), gunzip_exactly() or gunzip_finish()
  142. * calls will extract the data following the discarded bytes in the
  143. * data stream.
  144. *
  145. * If there are less @len bytes available in the data stream, or if
  146. * any other errors occur, such as a corrupted compressed stream, an
  147. * error is printed an the platform's exit() function is called.
  148. */
  149. void gunzip_discard(struct gunzip_state *state, int len)
  150. {
  151. static char discard_buf[128];
  152. while (len > sizeof(discard_buf)) {
  153. gunzip_exactly(state, discard_buf, sizeof(discard_buf));
  154. len -= sizeof(discard_buf);
  155. }
  156. if (len > 0)
  157. gunzip_exactly(state, discard_buf, len);
  158. }
  159. /**
  160. * gunzip_finish - extract all remaining bytes from a gzip data stream
  161. * @state: gzip state structure previously initialized by gunzip_start()
  162. * @dst: buffer to store extracted data
  163. * @dstlen: maximum number of bytes to extract
  164. *
  165. * This function extracts all remaining data, or at most @dstlen
  166. * bytes, from the stream previously associated with @state by
  167. * gunzip_start(). zlib is then shut down, so it is an error to use
  168. * any of the functions in this file on @state until it is
  169. * re-initialized with another call to gunzip_start().
  170. *
  171. * If any errors occur, such as a corrupted compressed stream, an
  172. * error is printed an the platform's exit() function is called.
  173. */
  174. int gunzip_finish(struct gunzip_state *state, void *dst, int dstlen)
  175. {
  176. int len;
  177. if (state->s.workspace) {
  178. len = gunzip_partial(state, dst, dstlen);
  179. zlib_inflateEnd(&state->s);
  180. } else {
  181. /* uncompressed image */
  182. len = min(state->s.avail_in, (unsigned)dstlen);
  183. memcpy(dst, state->s.next_in, len);
  184. }
  185. return len;
  186. }