NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
zstrm.c File Reference

Zlib compression of network traffic. More...

#include "config.h"
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <zconf.h>
#include <zlib.h>
#include "mutt/lib.h"
#include "zstrm.h"
#include "lib.h"
+ Include dependency graph for zstrm.c:

Go to the source code of this file.

Data Structures

struct  ZstrmDirection
 A stream of data being (de-)compressed. More...
 
struct  ZstrmContext
 Data compression layer. More...
 

Functions

static void * zstrm_malloc (void *opaque, unsigned int items, unsigned int size)
 Redirector function for zlib's malloc() More...
 
static void zstrm_free (void *opaque, void *address)
 Redirector function for zlib's free() More...
 
static int zstrm_open (struct Connection *conn)
 Open a socket - Implements Connection::open() -. More...
 
static int zstrm_close (struct Connection *conn)
 Close a socket - Implements Connection::close() -. More...
 
static int zstrm_read (struct Connection *conn, char *buf, size_t len)
 Read compressed data from a socket - Implements Connection::read() -. More...
 
static int zstrm_poll (struct Connection *conn, time_t wait_secs)
 Checks whether reads would block - Implements Connection::poll() -. More...
 
static int zstrm_write (struct Connection *conn, const char *buf, size_t count)
 Write compressed data to a socket - Implements Connection::write() -. More...
 
void mutt_zstrm_wrap_conn (struct Connection *conn)
 Wrap a compression layer around a Connection. More...
 

Detailed Description

Zlib compression of network traffic.

Copyright(C) 2019 Fabian Groffen grobi.nosp@m.an@g.nosp@m.entoo.nosp@m..org

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file zstrm.c.

Function Documentation

◆ zstrm_malloc()

static void* zstrm_malloc ( void *  opaque,
unsigned int  items,
unsigned int  size 
)
static

Redirector function for zlib's malloc()

Parameters
opaqueOpaque zlib handle
itemsNumber of blocks
sizeSize of blocks
Return values
ptrMemory on the heap

Definition at line 68 of file zstrm.c.

69 {
70  return mutt_mem_calloc(items, size);
71 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ zstrm_free()

static void zstrm_free ( void *  opaque,
void *  address 
)
static

Redirector function for zlib's free()

Parameters
opaqueOpaque zlib handle
addressMemory to free

Definition at line 78 of file zstrm.c.

79 {
80  FREE(&address);
81 }
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

◆ mutt_zstrm_wrap_conn()

void mutt_zstrm_wrap_conn ( struct Connection conn)

Wrap a compression layer around a Connection.

Parameters
connConnection to wrap

Replace the read/write functions with our compression functions. After reading from the socket, we decompress and pass on the data. Before writing to a socket, we compress the data.

Definition at line 288 of file zstrm.c.

289 {
290  struct ZstrmContext *zctx = mutt_mem_calloc(1, sizeof(struct ZstrmContext));
291 
292  /* store wrapped stream as next stream */
293  zctx->next_conn.fd = conn->fd;
294  zctx->next_conn.sockdata = conn->sockdata;
295  zctx->next_conn.open = conn->open;
296  zctx->next_conn.close = conn->close;
297  zctx->next_conn.read = conn->read;
298  zctx->next_conn.write = conn->write;
299  zctx->next_conn.poll = conn->poll;
300 
301  /* replace connection with our wrappers, where appropriate */
302  conn->sockdata = zctx;
303  conn->open = zstrm_open;
304  conn->read = zstrm_read;
305  conn->write = zstrm_write;
306  conn->close = zstrm_close;
307  conn->poll = zstrm_poll;
308 
309  /* allocate/setup (de)compression buffers */
310  zctx->read.len = 8192;
311  zctx->read.buf = mutt_mem_malloc(zctx->read.len);
312  zctx->read.pos = 0;
313  zctx->write.len = 8192;
314  zctx->write.buf = mutt_mem_malloc(zctx->write.len);
315  zctx->write.pos = 0;
316 
317  /* initialise zlib for inflate and deflate for RFC4978 */
318  zctx->read.z.zalloc = zstrm_malloc;
319  zctx->read.z.zfree = zstrm_free;
320  zctx->read.z.opaque = NULL;
321  zctx->read.z.avail_out = zctx->read.len;
322  inflateInit2(&zctx->read.z, -15);
323  zctx->write.z.zalloc = zstrm_malloc;
324  zctx->write.z.zfree = zstrm_free;
325  zctx->write.z.opaque = NULL;
326  zctx->write.z.avail_out = zctx->write.len;
327  deflateInit2(&zctx->write.z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
328 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
Data compression layer.
Definition: zstrm.c:54
struct ZstrmDirection write
Data being compressed and written.
Definition: zstrm.c:57
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition: connection.h:82
static void zstrm_free(void *opaque, void *address)
Redirector function for zlib&#39;s free()
Definition: zstrm.c:78
void * sockdata
Backend-specific socket data.
Definition: connection.h:46
static int zstrm_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: zstrm.c:97
static int zstrm_poll(struct Connection *conn, time_t wait_secs)
Checks whether reads would block - Implements Connection::poll() -.
Definition: zstrm.c:211
struct Connection next_conn
Underlying stream.
Definition: zstrm.c:58
int(* open)(struct Connection *conn)
Definition: connection.h:69
struct ZstrmDirection read
Data being read and de-compressed.
Definition: zstrm.c:56
static void * zstrm_malloc(void *opaque, unsigned int items, unsigned int size)
Redirector function for zlib&#39;s malloc()
Definition: zstrm.c:68
int(* close)(struct Connection *conn)
Definition: connection.h:119
int fd
Socket file descriptor.
Definition: connection.h:44
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
unsigned int len
Length of data.
Definition: zstrm.c:45
unsigned int pos
Current position.
Definition: zstrm.c:46
char * buf
Buffer for data being (de-)compressed.
Definition: zstrm.c:44
static int zstrm_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
Definition: zstrm.c:89
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition: connection.h:108
static int zstrm_write(struct Connection *conn, const char *buf, size_t count)
Write compressed data to a socket - Implements Connection::write() -.
Definition: zstrm.c:228
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition: connection.h:95
z_stream z
zlib compression handle
Definition: zstrm.c:43
static int zstrm_read(struct Connection *conn, char *buf, size_t len)
Read compressed data from a socket - Implements Connection::read() -.
Definition: zstrm.c:129
+ Here is the call graph for this function:
+ Here is the caller graph for this function: