torgzip.c File Reference

A simple in-memory gzip implementation. More...

#include "orconfig.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <zlib.h>
#include <string.h>
#include <netinet/in.h>
#include "util.h"
#include "log.h"
#include "torgzip.h"

Data Structures

struct  tor_zlib_state_t

Functions

int is_gzip_supported (void)
static INLINE int method_bits (compress_method_t method)
int tor_gzip_compress (char **out, size_t *out_len, const char *in, size_t in_len, compress_method_t method)
int tor_gzip_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)
tor_zlib_state_ttor_zlib_new (int compress, compress_method_t method)
tor_zlib_output_t tor_zlib_process (tor_zlib_state_t *state, char **out, size_t *out_len, const char **in, size_t *in_len, int finish)
void tor_zlib_free (tor_zlib_state_t *state)

Variables

static int gzip_is_supported = -1


Detailed Description

A simple in-memory gzip implementation.


Function Documentation

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.

References get_uint16().

Referenced by connection_dir_client_reached_eof().

int is_gzip_supported ( void   ) 

Return true iff we support gzip-based compression. Otherwise, we need to use zlib.

References gzip_is_supported, and strcmpstart().

Referenced by tor_gzip_compress(), tor_gzip_uncompress(), and tor_zlib_new().

static INLINE int method_bits ( compress_method_t  method  )  [static]

Return the 'bits' value to tell zlib to use method.

Referenced by tor_gzip_compress(), tor_gzip_uncompress(), and tor_zlib_new().

int tor_gzip_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.

References is_gzip_supported(), LD_BUG, LD_GENERAL, method_bits(), tor_assert, and tor_free.

Referenced by new_cached_dir(), and set_cached_dir().

int tor_gzip_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 zlib-compressed or gzip-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 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.

References is_gzip_supported(), LD_BUG, LD_GENERAL, LD_PROTOCOL, method_bits(), tor_assert, and tor_free.

Referenced by connection_dir_client_reached_eof().

void tor_zlib_free ( tor_zlib_state_t state  ) 

tor_zlib_state_t* tor_zlib_new ( int  compress,
compress_method_t  method 
)

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

References tor_zlib_state_t::compress, is_gzip_supported(), LD_BUG, method_bits(), tor_zlib_state_t::stream, and tor_free.

Referenced by connection_dirserv_add_networkstatus_bytes_to_outbuf(), and directory_handle_command_get().

tor_zlib_output_t tor_zlib_process ( tor_zlib_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_ZLIB_DONE if we've finished the entire compression/decompression. Return TOR_ZLIB_OK if we're processed everything from the input. Return TOR_ZLIB_BUF_FULL if we're out of space on out. Return TOR_ZLIB_ERR if the stream is corrupt.

References tor_zlib_state_t::compress, LD_GENERAL, tor_zlib_state_t::stream, and tor_assert.

Referenced by write_to_buf_zlib().


Variable Documentation

int gzip_is_supported = -1 [static]

Set to 1 if zlib is a version that supports gzip; set to 0 if it doesn't; set to -1 if we haven't checked yet.

Referenced by is_gzip_supported().


Generated on Tue May 25 00:30:59 2010 for tor by  doxygen 1.5.6