decompress_inflate.c 3.6 KB

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