zmem.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <linux/zlib.h>
  2. /* bits taken from ppc */
  3. extern void *avail_ram, *end_avail;
  4. void exit (void)
  5. {
  6. for (;;);
  7. }
  8. void *zalloc(unsigned size)
  9. {
  10. void *p = avail_ram;
  11. size = (size + 7) & -8;
  12. avail_ram += size;
  13. if (avail_ram > end_avail) {
  14. //puts("oops... out of memory\n");
  15. //pause();
  16. exit ();
  17. }
  18. return p;
  19. }
  20. #define HEAD_CRC 2
  21. #define EXTRA_FIELD 4
  22. #define ORIG_NAME 8
  23. #define COMMENT 0x10
  24. #define RESERVED 0xe0
  25. #define DEFLATED 8
  26. void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp)
  27. {
  28. z_stream s;
  29. int r, i, flags;
  30. /* skip header */
  31. i = 10;
  32. flags = src[3];
  33. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  34. //puts("bad gzipped data\n");
  35. exit();
  36. }
  37. if ((flags & EXTRA_FIELD) != 0)
  38. i = 12 + src[10] + (src[11] << 8);
  39. if ((flags & ORIG_NAME) != 0)
  40. while (src[i++] != 0)
  41. ;
  42. if ((flags & COMMENT) != 0)
  43. while (src[i++] != 0)
  44. ;
  45. if ((flags & HEAD_CRC) != 0)
  46. i += 2;
  47. if (i >= *lenp) {
  48. //puts("gunzip: ran out of data in header\n");
  49. exit();
  50. }
  51. s.workspace = zalloc(zlib_inflate_workspacesize());
  52. r = zlib_inflateInit2(&s, -MAX_WBITS);
  53. if (r != Z_OK) {
  54. //puts("inflateInit2 returned "); puthex(r); puts("\n");
  55. exit();
  56. }
  57. s.next_in = src + i;
  58. s.avail_in = *lenp - i;
  59. s.next_out = dst;
  60. s.avail_out = dstlen;
  61. r = zlib_inflate(&s, Z_FINISH);
  62. if (r != Z_OK && r != Z_STREAM_END) {
  63. //puts("inflate returned "); puthex(r); puts("\n");
  64. exit();
  65. }
  66. *lenp = s.next_out - (unsigned char *) dst;
  67. zlib_inflateEnd(&s);
  68. }