Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gunzip_util.c
Go to the documentation of this file.
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 
11 #include <stddef.h>
12 #include "string.h"
13 #include "stdio.h"
14 #include "ops.h"
15 #include "gunzip_util.h"
16 
17 #define HEAD_CRC 2
18 #define EXTRA_FIELD 4
19 #define ORIG_NAME 8
20 #define COMMENT 0x10
21 #define RESERVED 0xe0
22 
42 void gunzip_start(struct gunzip_state *state, void *src, int srclen)
43 {
44  char *hdr = src;
45  int hdrlen = 0;
46 
47  memset(state, 0, sizeof(*state));
48 
49  /* Check for gzip magic number */
50  if ((hdr[0] == 0x1f) && (hdr[1] == 0x8b)) {
51  /* gzip data, initialize zlib parameters */
52  int r, flags;
53 
54  state->s.workspace = state->scratch;
55  if (zlib_inflate_workspacesize() > sizeof(state->scratch))
56  fatal("insufficient scratch space for gunzip\n\r");
57 
58  /* skip header */
59  hdrlen = 10;
60  flags = hdr[3];
61  if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0)
62  fatal("bad gzipped data\n\r");
63  if ((flags & EXTRA_FIELD) != 0)
64  hdrlen = 12 + hdr[10] + (hdr[11] << 8);
65  if ((flags & ORIG_NAME) != 0)
66  while (hdr[hdrlen++] != 0)
67  ;
68  if ((flags & COMMENT) != 0)
69  while (hdr[hdrlen++] != 0)
70  ;
71  if ((flags & HEAD_CRC) != 0)
72  hdrlen += 2;
73  if (hdrlen >= srclen)
74  fatal("gunzip_start: ran out of data in header\n\r");
75 
76  r = zlib_inflateInit2(&state->s, -MAX_WBITS);
77  if (r != Z_OK)
78  fatal("inflateInit2 returned %d\n\r", r);
79  }
80 
81  state->s.total_in = hdrlen;
82  state->s.next_in = src + hdrlen;
83  state->s.avail_in = srclen - hdrlen;
84 }
85 
102 int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
103 {
104  int len;
105 
106  if (state->s.workspace) {
107  /* gunzipping */
108  int r;
109 
110  state->s.next_out = dst;
111  state->s.avail_out = dstlen;
112  r = zlib_inflate(&state->s, Z_FULL_FLUSH);
113  if (r != Z_OK && r != Z_STREAM_END)
114  fatal("inflate returned %d msg: %s\n\r", r, state->s.msg);
115  len = state->s.next_out - (unsigned char *)dst;
116  } else {
117  /* uncompressed image */
118  len = min(state->s.avail_in, (unsigned)dstlen);
119  memcpy(dst, state->s.next_in, len);
120  state->s.next_in += len;
121  state->s.avail_in -= len;
122  }
123  return len;
124 }
125 
140 void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen)
141 {
142  int len;
143 
144  len = gunzip_partial(state, dst, dstlen);
145  if (len < dstlen)
146  fatal("\n\rgunzip_exactly: ran out of data!"
147  " Wanted %d, got %d.\n\r", dstlen, len);
148 }
149 
165 void gunzip_discard(struct gunzip_state *state, int len)
166 {
167  static char discard_buf[128];
168 
169  while (len > sizeof(discard_buf)) {
170  gunzip_exactly(state, discard_buf, sizeof(discard_buf));
171  len -= sizeof(discard_buf);
172  }
173 
174  if (len > 0)
175  gunzip_exactly(state, discard_buf, len);
176 }
177 
193 int gunzip_finish(struct gunzip_state *state, void *dst, int dstlen)
194 {
195  int len;
196 
197  len = gunzip_partial(state, dst, dstlen);
198 
199  if (state->s.workspace) {
200  zlib_inflateEnd(&state->s);
201  }
202 
203  return len;
204 }