compress_none.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "common/util.h"
  17. #include "common/torlog.h"
  18. #include "common/compress.h"
  19. #include "common/compress_none.h"
  20. /** Transfer some bytes using the identity transformation. Read up to
  21. * *<b>in_len</b> bytes from *<b>in</b>, and write up to *<b>out_len</b> bytes
  22. * to *<b>out</b>, adjusting the values as we go. If <b>finish</b> is true,
  23. * we've reached the end of the input.
  24. *
  25. * Return TOR_COMPRESS_DONE if we've finished the entire
  26. * compression/decompression.
  27. * Return TOR_COMPRESS_OK if we're processed everything from the input.
  28. * Return TOR_COMPRESS_BUFFER_FULL if we're out of space on <b>out</b>.
  29. * Return TOR_COMPRESS_ERROR if the stream is corrupt.
  30. */
  31. tor_compress_output_t
  32. tor_cnone_compress_process(char **out, size_t *out_len,
  33. const char **in, size_t *in_len,
  34. int finish)
  35. {
  36. size_t n_to_copy = MIN(*in_len, *out_len);
  37. memcpy(*out, *in, n_to_copy);
  38. *out += n_to_copy;
  39. *in += n_to_copy;
  40. *out_len -= n_to_copy;
  41. *in_len -= n_to_copy;
  42. if (*in_len == 0) {
  43. return finish ? TOR_COMPRESS_DONE : TOR_COMPRESS_OK;
  44. } else {
  45. return TOR_COMPRESS_BUFFER_FULL;
  46. }
  47. }