deflateblock.c 787 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <flate.h>
  4. typedef struct Block Block;
  5. struct Block
  6. {
  7. uchar *pos;
  8. uchar *limit;
  9. };
  10. static int
  11. blread(void *vb, void *buf, int n)
  12. {
  13. Block *b;
  14. b = vb;
  15. if(n > b->limit - b->pos)
  16. n = b->limit - b->pos;
  17. memmove(buf, b->pos, n);
  18. b->pos += n;
  19. return n;
  20. }
  21. static int
  22. blwrite(void *vb, void *buf, int n)
  23. {
  24. Block *b;
  25. b = vb;
  26. if(n > b->limit - b->pos)
  27. n = b->limit - b->pos;
  28. memmove(b->pos, buf, n);
  29. b->pos += n;
  30. return n;
  31. }
  32. int
  33. deflateblock(uchar *dst, int dsize, uchar *src, int ssize, int level, int debug)
  34. {
  35. Block bd, bs;
  36. int ok;
  37. bs.pos = src;
  38. bs.limit = src + ssize;
  39. bd.pos = dst;
  40. bd.limit = dst + dsize;
  41. ok = deflate(&bd, blwrite, &bs, blread, level, debug);
  42. if(ok != FlateOk)
  43. return ok;
  44. return bd.pos - dst;
  45. }