c_rle.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <openssl/objects.h>
  5. #include <openssl/comp.h>
  6. static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
  7. unsigned int olen, unsigned char *in,
  8. unsigned int ilen);
  9. static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
  10. unsigned int olen, unsigned char *in,
  11. unsigned int ilen);
  12. static COMP_METHOD rle_method = {
  13. NID_rle_compression,
  14. LN_rle_compression,
  15. NULL,
  16. NULL,
  17. rle_compress_block,
  18. rle_expand_block,
  19. NULL,
  20. NULL,
  21. };
  22. COMP_METHOD *COMP_rle(void)
  23. {
  24. return (&rle_method);
  25. }
  26. static int rle_compress_block(COMP_CTX *ctx, unsigned char *out,
  27. unsigned int olen, unsigned char *in,
  28. unsigned int ilen)
  29. {
  30. /* int i; */
  31. if (olen < (ilen + 1)) {
  32. /* ZZZZZZZZZZZZZZZZZZZZZZ */
  33. return (-1);
  34. }
  35. *(out++) = 0;
  36. memcpy(out, in, ilen);
  37. return (ilen + 1);
  38. }
  39. static int rle_expand_block(COMP_CTX *ctx, unsigned char *out,
  40. unsigned int olen, unsigned char *in,
  41. unsigned int ilen)
  42. {
  43. int i;
  44. if (ilen == 0 || olen < (ilen - 1)) {
  45. /* ZZZZZZZZZZZZZZZZZZZZZZ */
  46. return (-1);
  47. }
  48. i = *(in++);
  49. if (i == 0) {
  50. memcpy(out, in, ilen - 1);
  51. }
  52. return (ilen - 1);
  53. }