inflatezlib.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <flate.h>
  4. #include "zlib.h"
  5. typedef struct ZWrite ZWrite;
  6. struct ZWrite
  7. {
  8. ulong adler;
  9. void *wr;
  10. int (*w)(void*, void*, int);
  11. };
  12. static int
  13. zlwrite(void *vzw, void *buf, int n)
  14. {
  15. ZWrite *zw;
  16. zw = vzw;
  17. zw->adler = adler32(zw->adler, buf, n);
  18. n = (*zw->w)(zw->wr, buf, n);
  19. if(n <= 0)
  20. return n;
  21. return n;
  22. }
  23. int
  24. inflatezlib(void *wr, int (*w)(void*, void*, int), void *getr, int (*get)(void*))
  25. {
  26. ZWrite zw;
  27. ulong v;
  28. int c, i;
  29. c = (*get)(getr);
  30. if(c < 0)
  31. return FlateInputFail;
  32. i = (*get)(getr);
  33. if(i < 0)
  34. return FlateInputFail;
  35. if(((c << 8) | i) % 31)
  36. return FlateCorrupted;
  37. if((c & ZlibMeth) != ZlibDeflate
  38. || (c & ZlibCInfo) > ZlibWin32k)
  39. return FlateCorrupted;
  40. zw.wr = wr;
  41. zw.w = w;
  42. zw.adler = 1;
  43. i = inflate(&zw, zlwrite, getr, get);
  44. if(i != FlateOk)
  45. return i;
  46. v = 0;
  47. for(i = 0; i < 4; i++){
  48. c = (*get)(getr);
  49. if(c < 0)
  50. return FlateInputFail;
  51. v = (v << 8) | c;
  52. }
  53. if(zw.adler != v)
  54. return FlateCorrupted;
  55. return FlateOk;
  56. }