decompress_inflate.c 3.6 KB

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