Tor 0.4.9.3-alpha-dev
Loading...
Searching...
No Matches
Data Structures | Functions | Variables
compress.c File Reference

Common compression API implementation. More...

#include "orconfig.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lib/cc/torint.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/arch/bytes.h"
#include "lib/ctime/di_ops.h"
#include "lib/compress/compress.h"
#include "lib/compress/compress_lzma.h"
#include "lib/compress/compress_none.h"
#include "lib/compress/compress_sys.h"
#include "lib/compress/compress_zlib.h"
#include "lib/compress/compress_zstd.h"
#include "lib/intmath/cmp.h"
#include "lib/malloc/malloc.h"
#include "lib/subsys/subsys.h"
#include "lib/thread/threads.h"

Go to the source code of this file.

Data Structures

struct  tor_compress_state_t
 

Macros

#define MAX_UNCOMPRESSION_FACTOR   25
 
#define CHECK_FOR_COMPRESSION_BOMB_AFTER   (1024*64)
 

Functions

int tor_compress_is_compression_bomb (size_t size_in, size_t size_out)
 
static size_t guess_compress_size (int compress, compress_method_t method, compression_level_t compression_level, size_t in_len)
 
static int tor_compress_impl (int compress, char **out, size_t *out_len, const char *in, size_t in_len, compress_method_t method, compression_level_t compression_level, int complete_only, int protocol_warn_level)
 
int tor_compress (char **out, size_t *out_len, const char *in, size_t in_len, compress_method_t method)
 
int tor_uncompress (char **out, size_t *out_len, const char *in, size_t in_len, compress_method_t method, int complete_only, int protocol_warn_level)
 
compress_method_t detect_compression_method (const char *in, size_t in_len)
 
int tor_compress_supports_method (compress_method_t method)
 
unsigned tor_compress_get_supported_method_bitmask (void)
 
const char * compression_method_get_name (compress_method_t method)
 
const char * compression_method_get_human_name (compress_method_t method)
 
compress_method_t compression_method_get_by_name (const char *name)
 
const char * tor_compress_version_str (compress_method_t method)
 
const char * tor_compress_header_version_str (compress_method_t method)
 
size_t tor_compress_get_total_allocation (void)
 
tor_compress_state_ttor_compress_new (int compress, compress_method_t method, compression_level_t compression_level)
 
tor_compress_output_t tor_compress_process (tor_compress_state_t *state, char **out, size_t *out_len, const char **in, size_t *in_len, int finish)
 
void tor_compress_free_ (tor_compress_state_t *state)
 
size_t tor_compress_state_size (const tor_compress_state_t *state)
 
int tor_compress_init (void)
 
void tor_compress_log_init_warnings (void)
 
static int subsys_compress_initialize (void)
 

Variables

static atomic_counter_t total_compress_allocation
 
struct { 
 
   const char *   name 
 
   compress_method_t   method 
 
compression_method_names [] 
 
struct { 
 
   compress_method_t   method 
 
   const char *   name 
 
compression_method_human_names [] 
 
const subsys_fns_t sys_compress
 

Detailed Description

Common compression API implementation.

This file provides a unified interface to all the compression libraries Tor knows how to use.

Definition in file compress.c.

Macro Definition Documentation

◆ CHECK_FOR_COMPRESSION_BOMB_AFTER

#define CHECK_FOR_COMPRESSION_BOMB_AFTER   (1024*64)

Definition at line 58 of file compress.c.

◆ MAX_UNCOMPRESSION_FACTOR

#define MAX_UNCOMPRESSION_FACTOR   25

Definition at line 57 of file compress.c.

Function Documentation

◆ compression_method_get_by_name()

compress_method_t compression_method_get_by_name ( const char *  name)

Return the compression method represented by the string name, or UNKNOWN_METHOD if the string isn't recognized.

Definition at line 413 of file compress.c.

Referenced by consdiffmgr_consensus_load(), consdiffmgr_diffs_load(), parse_accept_encoding_header(), parse_http_response(), and uncompress_or_set_ptr().

◆ compression_method_get_human_name()

const char * compression_method_get_human_name ( compress_method_t  method)

Return a human readable string representation of the compression method method, or NULL if the method isn't recognized.

Definition at line 400 of file compress.c.

Referenced by tor_compress_process(), and warn_disallowed_anonymous_compression_method().

◆ compression_method_get_name()

const char * compression_method_get_name ( compress_method_t  method)

Return the canonical string representation of the compression method method, or NULL if the method isn't recognized.

Definition at line 374 of file compress.c.

Referenced by accept_encoding_header(), compress_multiple(), consdiffmgr_cleanup(), consdiffmgr_rescan_flavor_(), store_multiple(), and write_http_response_headers().

◆ detect_compression_method()

compress_method_t detect_compression_method ( const char *  in,
size_t  in_len 
)

Try to tell whether the in_len-byte string in in is likely to be compressed or not. If it is, return the likeliest compression method. Otherwise, return UNKNOWN_METHOD.

Definition at line 294 of file compress.c.

◆ guess_compress_size()

static size_t guess_compress_size ( int  compress,
compress_method_t  method,
compression_level_t  compression_level,
size_t  in_len 
)
static

Guess the size that in_len will be after compression or decompression.

Definition at line 85 of file compress.c.

Referenced by tor_compress_impl().

◆ subsys_compress_initialize()

static int subsys_compress_initialize ( void  )
static

Definition at line 700 of file compress.c.

◆ tor_compress()

int tor_compress ( char **  out,
size_t *  out_len,
const char *  in,
size_t  in_len,
compress_method_t  method 
)

Given in_len bytes at in, compress them into a newly allocated buffer, using the method described in method. Store the compressed string in *out, and its length in *out_len. Return 0 on success, -1 on failure.

Definition at line 252 of file compress.c.

Referenced by compress_multiple(), and new_cached_dir().

◆ tor_compress_free_()

void tor_compress_free_ ( tor_compress_state_t state)

Deallocate state.

Definition at line 620 of file compress.c.

◆ tor_compress_get_supported_method_bitmask()

unsigned tor_compress_get_supported_method_bitmask ( void  )

Return a bitmask of the supported compression types, where 1<<m is set in the bitmask if and only if compression with method m is supported.

Definition at line 338 of file compress.c.

Referenced by directory_handle_command_get().

◆ tor_compress_get_total_allocation()

size_t tor_compress_get_total_allocation ( void  )

Return the approximate number of bytes allocated for all supported compression schemas.

Definition at line 468 of file compress.c.

Referenced by cell_queues_check_size(), and circuits_handle_oom().

◆ tor_compress_header_version_str()

const char * tor_compress_header_version_str ( compress_method_t  method)

Return a string representation of the version of the library, found at compile time, providing the compression method given in method. Returns NULL if method is unknown or unsupported.

Definition at line 448 of file compress.c.

Referenced by print_library_versions().

◆ tor_compress_impl()

static int tor_compress_impl ( int  compress,
char **  out,
size_t *  out_len,
const char *  in,
size_t  in_len,
compress_method_t  method,
compression_level_t  compression_level,
int  complete_only,
int  protocol_warn_level 
)
static

Internal function to implement tor_compress/tor_uncompress, depending on whether compress is set. All arguments are as for tor_compress or tor_uncompress.

Definition at line 111 of file compress.c.

Referenced by tor_compress(), and tor_uncompress().

◆ tor_compress_init()

int tor_compress_init ( void  )

Initialize all compression modules.

Definition at line 676 of file compress.c.

◆ tor_compress_is_compression_bomb()

int tor_compress_is_compression_bomb ( size_t  size_in,
size_t  size_out 
)

Return true if uncompressing an input of size in_size to an input of size at least size_out looks like a compression bomb.

Definition at line 64 of file compress.c.

Referenced by tor_compress_impl(), tor_lzma_compress_process(), tor_zlib_compress_process(), and tor_zstd_compress_process().

◆ tor_compress_log_init_warnings()

void tor_compress_log_init_warnings ( void  )

Warn if we had any problems while setting up our compression libraries.

(This isn't part of tor_compress_init, since the logs aren't set up yet.)

Definition at line 692 of file compress.c.

Referenced by tor_init().

◆ tor_compress_new()

tor_compress_state_t * tor_compress_new ( int  compress,
compress_method_t  method,
compression_level_t  compression_level 
)

Construct and return a tor_compress_state_t object using method. If compress, it's for compression; otherwise it's for decompression.

Definition at line 491 of file compress.c.

Referenced by handle_get_current_consensus(), handle_get_descriptor(), handle_get_keys(), handle_get_microdesc(), handle_get_next_bandwidth(), handle_get_status_vote(), and tor_compress_impl().

◆ tor_compress_process()

tor_compress_output_t tor_compress_process ( tor_compress_state_t state,
char **  out,
size_t *  out_len,
const char **  in,
size_t *  in_len,
int  finish 
)

Compress/decompress some bytes using state. Read up to *in_len bytes from *in, and write up to *out_len bytes to *out, adjusting the values as we go. If finish is true, we've reached the end of the input.

Return TOR_COMPRESS_DONE if we've finished the entire compression/decompression. Return TOR_COMPRESS_OK if we're processed everything from the input. Return TOR_COMPRESS_BUFFER_FULL if we're out of space on out. Return TOR_COMPRESS_ERROR if the stream is corrupt.

Definition at line 559 of file compress.c.

Referenced by buf_add_compress(), and tor_compress_impl().

◆ tor_compress_state_size()

size_t tor_compress_state_size ( const tor_compress_state_t state)

Return the approximate number of bytes allocated for state.

Definition at line 649 of file compress.c.

◆ tor_compress_supports_method()

int tor_compress_supports_method ( compress_method_t  method)

◆ tor_compress_version_str()

const char * tor_compress_version_str ( compress_method_t  method)

Return a string representation of the version of the library providing the compression method given in method. Returns NULL if method is unknown or unsupported.

Definition at line 427 of file compress.c.

Referenced by options_init_from_torrc(), print_library_versions(), and tor_init().

◆ tor_uncompress()

int tor_uncompress ( char **  out,
size_t *  out_len,
const char *  in,
size_t  in_len,
compress_method_t  method,
int  complete_only,
int  protocol_warn_level 
)

Given zero or more compressed strings of total length in_len bytes at in, uncompress them into a newly allocated buffer, using the method described in method. Store the uncompressed string in *out, and its length in *out_len. Return 0 on success, -1 on failure.

If any bytes are written to out, an extra byte NUL is always written at the end, but not counted in out_len. This is a safety feature to ensure that the output can be treated as a NUL-terminated string – though of course, callers should check out_len anyway.

If complete_only is true, we consider a truncated input as a failure; otherwise we decompress as much as we can. Warn about truncated or corrupt inputs at protocol_warn_level.

Definition at line 278 of file compress.c.

Referenced by uncompress_or_set_ptr().

Variable Documentation

◆ [struct]

const struct { ... } compression_method_human_names[]
Initial value:
= {
{ NO_METHOD, "uncompressed" },
{ GZIP_METHOD, "gzipped" },
{ ZLIB_METHOD, "deflated" },
{ LZMA_METHOD, "LZMA compressed" },
{ ZSTD_METHOD, "Zstandard compressed" },
{ UNKNOWN_METHOD, "unknown encoding" },
}

Table of compression human readable method names.

Referenced by compression_method_get_human_name().

◆ [struct]

const struct { ... } compression_method_names[]
Initial value:
= {
{ "gzip", GZIP_METHOD },
{ "deflate", ZLIB_METHOD },
{ "x-tor-lzma", LZMA_METHOD },
{ "x-zstd" , ZSTD_METHOD },
{ "identity", NO_METHOD },
{ "x-gzip", GZIP_METHOD },
}

Table of compression method names. These should have an "x-" prefix, if they are not listed in the IANA content coding registry.

Referenced by compression_method_get_by_name(), and compression_method_get_name().

◆ method

Definition at line 356 of file compress.c.

◆ name

const char* name

Definition at line 355 of file compress.c.

◆ sys_compress

const subsys_fns_t sys_compress
Initial value:
= {
.name = "compress",
.supported = true,
.level = -55,
.initialize = subsys_compress_initialize,
}
#define SUBSYS_DECLARE_LOCATION()
Definition subsys.h:211

Definition at line 705 of file compress.c.

◆ total_compress_allocation

atomic_counter_t total_compress_allocation
static

Total number of bytes allocated for compression state overhead.

Definition at line 41 of file compress.c.

Referenced by tor_compress_free_(), tor_compress_get_total_allocation(), tor_compress_init(), and tor_compress_new().