gunzip.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * (C) Copyright 2000-2006
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <watchdog.h>
  25. #include <command.h>
  26. #include <image.h>
  27. #include <malloc.h>
  28. #include <u-boot/zlib.h>
  29. #define ZALLOC_ALIGNMENT 16
  30. #define HEAD_CRC 2
  31. #define EXTRA_FIELD 4
  32. #define ORIG_NAME 8
  33. #define COMMENT 0x10
  34. #define RESERVED 0xe0
  35. #define DEFLATED 8
  36. int gunzip(void *, int, unsigned char *, unsigned long *);
  37. void *zalloc(void *, unsigned, unsigned);
  38. void zfree(void *, void *, unsigned);
  39. int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
  40. int stoponerr, int offset);
  41. void *zalloc(void *x, unsigned items, unsigned size)
  42. {
  43. void *p;
  44. size *= items;
  45. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  46. p = malloc (size);
  47. return (p);
  48. }
  49. void zfree(void *x, void *addr, unsigned nb)
  50. {
  51. free (addr);
  52. }
  53. int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
  54. {
  55. int i, flags;
  56. /* skip header */
  57. i = 10;
  58. flags = src[3];
  59. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  60. puts ("Error: Bad gzipped data\n");
  61. return (-1);
  62. }
  63. if ((flags & EXTRA_FIELD) != 0)
  64. i = 12 + src[10] + (src[11] << 8);
  65. if ((flags & ORIG_NAME) != 0)
  66. while (src[i++] != 0)
  67. ;
  68. if ((flags & COMMENT) != 0)
  69. while (src[i++] != 0)
  70. ;
  71. if ((flags & HEAD_CRC) != 0)
  72. i += 2;
  73. if (i >= *lenp) {
  74. puts ("Error: gunzip out of data in header\n");
  75. return (-1);
  76. }
  77. return zunzip(dst, dstlen, src, lenp, 1, i);
  78. }
  79. /*
  80. * Uncompress blocks compressed with zlib without headers
  81. */
  82. int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
  83. int stoponerr, int offset)
  84. {
  85. z_stream s;
  86. int r;
  87. s.zalloc = zalloc;
  88. s.zfree = zfree;
  89. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  90. s.outcb = (cb_func)WATCHDOG_RESET;
  91. #else
  92. s.outcb = Z_NULL;
  93. #endif /* CONFIG_HW_WATCHDOG */
  94. r = inflateInit2(&s, -MAX_WBITS);
  95. if (r != Z_OK) {
  96. printf ("Error: inflateInit2() returned %d\n", r);
  97. return -1;
  98. }
  99. s.next_in = src + offset;
  100. s.avail_in = *lenp - offset;
  101. s.next_out = dst;
  102. s.avail_out = dstlen;
  103. r = inflate(&s, Z_FINISH);
  104. if ((r != Z_STREAM_END) && (stoponerr==1)) {
  105. printf ("Error: inflate() returned %d\n", r);
  106. inflateEnd(&s);
  107. return (-1);
  108. }
  109. *lenp = s.next_out - (unsigned char *) dst;
  110. inflateEnd(&s);
  111. return 0;
  112. }