deflatezlibblock.c 686 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <flate.h>
  4. #include "zlib.h"
  5. int
  6. deflatezlibblock(uchar *dst, int dsize, uchar *src, int ssize, int level, int debug)
  7. {
  8. ulong adler;
  9. int n;
  10. if(dsize < 6)
  11. return FlateOutputFail;
  12. n = deflateblock(dst + 2, dsize - 6, src, ssize, level, debug);
  13. if(n < 0)
  14. return n;
  15. dst[0] = ZlibDeflate | ZlibWin32k;
  16. /* bogus zlib encoding of compression level */
  17. dst[1] = ((level > 2) + (level > 5) + (level > 8)) << 6;
  18. /* header check field */
  19. dst[1] |= 31 - ((dst[0] << 8) | dst[1]) % 31;
  20. adler = adler32(1, src, ssize);
  21. dst[n + 2] = adler >> 24;
  22. dst[n + 3] = adler >> 16;
  23. dst[n + 4] = adler >> 8;
  24. dst[n + 5] = adler;
  25. return n + 6;
  26. }