decompress_inflate.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifdef STATIC
  2. /* Pre-boot environment: included */
  3. /* prevent inclusion of _LINUX_KERNEL_H in pre-boot environment: lots
  4. * errors about console_printk etc... on ARM */
  5. #define _LINUX_KERNEL_H
  6. #include "zlib_inflate/inftrees.c"
  7. #include "zlib_inflate/inffast.c"
  8. #include "zlib_inflate/inflate.c"
  9. #else /* STATIC */
  10. /* initramfs et al: linked */
  11. #include <linux/zutil.h>
  12. #include "zlib_inflate/inftrees.h"
  13. #include "zlib_inflate/inffast.h"
  14. #include "zlib_inflate/inflate.h"
  15. #include "zlib_inflate/infutil.h"
  16. #endif /* STATIC */
  17. #include <linux/decompress/mm.h>
  18. #define GZIP_IOBUF_SIZE (16*1024)
  19. static int INIT nofill(void *buffer, unsigned int len)
  20. {
  21. return -1;
  22. }
  23. /* Included from initramfs et al code */
  24. STATIC int INIT gunzip(unsigned char *buf, int len,
  25. int(*fill)(void*, unsigned int),
  26. int(*flush)(void*, unsigned int),
  27. unsigned char *out_buf,
  28. int *pos,
  29. void(*error)(char *x)) {
  30. u8 *zbuf;
  31. struct z_stream_s *strm;
  32. int rc;
  33. size_t out_len;
  34. rc = -1;
  35. if (flush) {
  36. out_len = 0x8000; /* 32 K */
  37. out_buf = malloc(out_len);
  38. } else {
  39. out_len = 0x7fffffff; /* no limit */
  40. }
  41. if (!out_buf) {
  42. error("Out of memory while allocating output buffer");
  43. goto gunzip_nomem1;
  44. }
  45. if (buf)
  46. zbuf = buf;
  47. else {
  48. zbuf = malloc(GZIP_IOBUF_SIZE);
  49. len = 0;
  50. }
  51. if (!zbuf) {
  52. error("Out of memory while allocating input buffer");
  53. goto gunzip_nomem2;
  54. }
  55. strm = malloc(sizeof(*strm));
  56. if (strm == NULL) {
  57. error("Out of memory while allocating z_stream");
  58. goto gunzip_nomem3;
  59. }
  60. strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
  61. sizeof(struct inflate_state));
  62. if (strm->workspace == NULL) {
  63. error("Out of memory while allocating workspace");
  64. goto gunzip_nomem4;
  65. }
  66. if (!fill)
  67. fill = nofill;
  68. if (len == 0)
  69. len = fill(zbuf, GZIP_IOBUF_SIZE);
  70. /* verify the gzip header */
  71. if (len < 10 ||
  72. zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
  73. if (pos)
  74. *pos = 0;
  75. error("Not a gzip file");
  76. goto gunzip_5;
  77. }
  78. /* skip over gzip header (1f,8b,08... 10 bytes total +
  79. * possible asciz filename)
  80. */
  81. strm->next_in = zbuf + 10;
  82. strm->avail_in = len - 10;
  83. /* skip over asciz filename */
  84. if (zbuf[3] & 0x8) {
  85. do {
  86. /*
  87. * If the filename doesn't fit into the buffer,
  88. * the file is very probably corrupt. Don't try
  89. * to read more data.
  90. */
  91. if (strm->avail_in == 0) {
  92. error("header error");
  93. goto gunzip_5;
  94. }
  95. --strm->avail_in;
  96. } while (*strm->next_in++);
  97. }
  98. strm->next_out = out_buf;
  99. strm->avail_out = out_len;
  100. rc = zlib_inflateInit2(strm, -MAX_WBITS);
  101. if (!flush) {
  102. WS(strm)->inflate_state.wsize = 0;
  103. WS(strm)->inflate_state.window = NULL;
  104. }
  105. while (rc == Z_OK) {
  106. if (strm->avail_in == 0) {
  107. /* TODO: handle case where both pos and fill are set */
  108. len = fill(zbuf, GZIP_IOBUF_SIZE);
  109. if (len < 0) {
  110. rc = -1;
  111. error("read error");
  112. break;
  113. }
  114. strm->next_in = zbuf;
  115. strm->avail_in = len;
  116. }
  117. rc = zlib_inflate(strm, 0);
  118. /* Write any data generated */
  119. if (flush && strm->next_out > out_buf) {
  120. int l = strm->next_out - out_buf;
  121. if (l != flush(out_buf, l)) {
  122. rc = -1;
  123. error("write error");
  124. break;
  125. }
  126. strm->next_out = out_buf;
  127. strm->avail_out = out_len;
  128. }
  129. /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
  130. if (rc == Z_STREAM_END) {
  131. rc = 0;
  132. break;
  133. } else if (rc != Z_OK) {
  134. error("uncompression error");
  135. rc = -1;
  136. }
  137. }
  138. zlib_inflateEnd(strm);
  139. if (pos)
  140. /* add + 8 to skip over trailer */
  141. *pos = strm->next_in - zbuf+8;
  142. gunzip_5:
  143. free(strm->workspace);
  144. gunzip_nomem4:
  145. free(strm);
  146. gunzip_nomem3:
  147. if (!buf)
  148. free(zbuf);
  149. gunzip_nomem2:
  150. if (flush)
  151. free(out_buf);
  152. gunzip_nomem1:
  153. return rc; /* returns Z_OK (0) if successful */
  154. }
  155. #define decompress gunzip