gunzip_util.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. struct gunzip_state state;
  16. #define HEAD_CRC 2
  17. #define EXTRA_FIELD 4
  18. #define ORIG_NAME 8
  19. #define COMMENT 0x10
  20. #define RESERVED 0xe0
  21. void gunzip_start(struct gunzip_state *state, void *src, int srclen)
  22. {
  23. char *hdr = src;
  24. int hdrlen = 0;
  25. memset(state, 0, sizeof(*state));
  26. /* Check for gzip magic number */
  27. if ((hdr[0] == 0x1f) && (hdr[1] == 0x8b)) {
  28. /* gzip data, initialize zlib parameters */
  29. int r, flags;
  30. state->s.workspace = state->scratch;
  31. if (zlib_inflate_workspacesize() > sizeof(state->scratch)) {
  32. printf("insufficient scratch space for gunzip\n\r");
  33. exit();
  34. }
  35. /* skip header */
  36. hdrlen = 10;
  37. flags = hdr[3];
  38. if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
  39. printf("bad gzipped data\n\r");
  40. exit();
  41. }
  42. if ((flags & EXTRA_FIELD) != 0)
  43. hdrlen = 12 + hdr[10] + (hdr[11] << 8);
  44. if ((flags & ORIG_NAME) != 0)
  45. while (hdr[hdrlen++] != 0)
  46. ;
  47. if ((flags & COMMENT) != 0)
  48. while (hdr[hdrlen++] != 0)
  49. ;
  50. if ((flags & HEAD_CRC) != 0)
  51. hdrlen += 2;
  52. if (hdrlen >= srclen) {
  53. printf("gunzip_start: ran out of data in header\n\r");
  54. exit();
  55. }
  56. r = zlib_inflateInit2(&state->s, -MAX_WBITS);
  57. if (r != Z_OK) {
  58. printf("inflateInit2 returned %d\n\r", r);
  59. exit();
  60. }
  61. }
  62. state->s.next_in = src + hdrlen;
  63. state->s.avail_in = srclen - hdrlen;
  64. }
  65. int gunzip_partial(struct gunzip_state *state, void *dst, int dstlen)
  66. {
  67. int len;
  68. if (state->s.workspace) {
  69. /* gunzipping */
  70. int r;
  71. state->s.next_out = dst;
  72. state->s.avail_out = dstlen;
  73. r = zlib_inflate(&state->s, Z_FULL_FLUSH);
  74. if (r != Z_OK && r != Z_STREAM_END) {
  75. printf("inflate returned %d msg: %s\n\r", r, state->s.msg);
  76. exit();
  77. }
  78. len = state->s.next_out - (unsigned char *)dst;
  79. } else {
  80. /* uncompressed image */
  81. len = min(state->s.avail_in, (unsigned)dstlen);
  82. memcpy(dst, state->s.next_in, len);
  83. state->s.next_in += len;
  84. state->s.avail_in -= len;
  85. }
  86. return len;
  87. }
  88. void gunzip_exactly(struct gunzip_state *state, void *dst, int dstlen)
  89. {
  90. int len;
  91. len = gunzip_partial(state, dst, dstlen);
  92. if (len < dstlen) {
  93. printf("gunzip_block: ran out of data\n\r");
  94. exit();
  95. }
  96. }
  97. void gunzip_discard(struct gunzip_state *state, int len)
  98. {
  99. static char discard_buf[128];
  100. while (len > sizeof(discard_buf)) {
  101. gunzip_exactly(state, discard_buf, sizeof(discard_buf));
  102. len -= sizeof(discard_buf);
  103. }
  104. if (len > 0)
  105. gunzip_exactly(state, discard_buf, len);
  106. }
  107. int gunzip_finish(struct gunzip_state *state, void *dst, int dstlen)
  108. {
  109. int len;
  110. if (state->s.workspace) {
  111. len = gunzip_partial(state, dst, dstlen);
  112. zlib_inflateEnd(&state->s);
  113. } else {
  114. /* uncompressed image */
  115. len = min(state->s.avail_in, (unsigned)dstlen);
  116. memcpy(dst, state->s.next_in, len);
  117. }
  118. return len;
  119. }