compress_none.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright (c) 2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file compress_none.c
  7. * \brief Compression backend for identity compression.
  8. *
  9. * We actually define this backend so that we can treat the identity transform
  10. * as another case of compression.
  11. *
  12. * This module should never be invoked directly. Use the compress module
  13. * instead.
  14. **/
  15. #include "orconfig.h"
  16. #include "lib/log/log.h"
  17. #include "lib/compress/compress.h"
  18. #include "lib/compress/compress_none.h"
  19. #include "lib/intmath/cmp.h"
  20. #include <string.h>
  21. /** Transfer some bytes using the identity transformation. Read up to
  22. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  23. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  24. * we've reached the end of the input.
  25. *
  26. * Return TOR_COMPRESS_DONE if we've finished the entire
  27. * compression/decompression.
  28. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  29. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  30. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  31. */
  32. tor_compress_output_t
  33. tor_cnone_compress_process(char **out, size_t *out_len,
  34. const char **in, size_t *in_len,
  35. int finish)
  36. {
  37. size_t n_to_copy = MIN(*in_len, *out_len);
  38. memcpy(*out, *in, n_to_copy);
  39. *out += n_to_copy;
  40. *in += n_to_copy;
  41. *out_len -= n_to_copy;
  42. *in_len -= n_to_copy;
  43. if (*in_len == 0) {
  44. return finish ? TOR_COMPRESS_DONE : TOR_COMPRESS_OK;
  45. } else {
  46. return TOR_COMPRESS_BUFFER_FULL;
  47. }
  48. }