decompress_inflate.c 3.4 KB

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