deflatezlibblock.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <flate.h>
  12. #include "zlib.h"
  13. int
  14. deflatezlibblock(uint8_t *dst, int dsize, uint8_t *src, int ssize,
  15. int level, int debug)
  16. {
  17. uint32_t adler;
  18. int n;
  19. if(dsize < 6)
  20. return FlateOutputFail;
  21. n = deflateblock(dst + 2, dsize - 6, src, ssize, level, debug);
  22. if(n < 0)
  23. return n;
  24. dst[0] = ZlibDeflate | ZlibWin32k;
  25. /* bogus zlib encoding of compression level */
  26. dst[1] = ((level > 2) + (level > 5) + (level > 8)) << 6;
  27. /* header check field */
  28. dst[1] |= 31 - ((dst[0] << 8) | dst[1]) % 31;
  29. adler = adler32(1, src, ssize);
  30. dst[n + 2] = adler >> 24;
  31. dst[n + 3] = adler >> 16;
  32. dst[n + 4] = adler >> 8;
  33. dst[n + 5] = adler;
  34. return n + 6;
  35. }