Tor  0.4.7.0-alpha-dev
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.

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 403 of file compress.c.

◆ 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 390 of file compress.c.

◆ 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 364 of file compress.c.

Referenced by consdiffmgr_rescan_flavor_().

◆ 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 284 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 75 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 242 of file compress.c.

◆ tor_compress_free_()

void tor_compress_free_ ( tor_compress_state_t state)

Deallocate state.

Definition at line 610 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 328 of file compress.c.

◆ 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 458 of file compress.c.

◆ 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 438 of file compress.c.

◆ 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 101 of file compress.c.

◆ tor_compress_init()

int tor_compress_init ( void  )

Initialize all compression modules.

Definition at line 666 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_lzma_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 682 of file compress.c.

◆ 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 481 of file compress.c.

◆ 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 549 of file compress.c.

Referenced by buf_add_compress().

◆ 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 639 of file compress.c.

◆ tor_compress_supports_method()

int tor_compress_supports_method ( compress_method_t  method)

Return 1 if a given method is supported; otherwise 0.

Definition at line 304 of file compress.c.

Referenced by options_init_from_torrc().

◆ 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 417 of file compress.c.

◆ 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 268 of file compress.c.

Variable Documentation

◆ 

const { ... } 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.

◆ 

const { ... } 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().

◆ 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 695 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_get_total_allocation(), and tor_compress_init().