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