NeoMutt  2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
zstrm.c File Reference

Zlib compression of network traffic. More...

#include "config.h"
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <zconf.h>
#include <zlib.h>
#include "mutt/lib.h"
#include "zstrm.h"
#include "connection.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().
static void zstrm_free (void *opaque, void *address)
 Redirector function for zlib's free().
static int zstrm_open (struct Connection *conn)
 Open a socket - Implements Connection::open() -.
static int zstrm_close (struct Connection *conn)
 Close a socket - Implements Connection::close() -.
static int zstrm_read (struct Connection *conn, char *buf, size_t len)
 Read compressed data from a socket - Implements Connection::read() -.
static int zstrm_poll (struct Connection *conn, time_t wait_secs)
 Check if any data is waiting on a socket - Implements Connection::poll() -.
static int zstrm_write (struct Connection *conn, const char *buf, size_t count)
 Write compressed data to a socket - Implements Connection::write() -.
void mutt_zstrm_wrap_conn (struct Connection *conn)
 Wrap a compression layer around a Connection.

Detailed Description

Zlib compression of network traffic.

Authors
  • Fabian Groffen
  • Richard Russon
  • Pietro Cerutti

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()

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 72 of file zstrm.c.

73{
74 return mutt_mem_calloc(items, size);
75}
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition memory.c:79
Here is the call graph for this function:
Here is the caller graph for this function:

◆ zstrm_free()

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

Redirector function for zlib's free().

Parameters
opaqueOpaque zlib handle
addressMemory to free

Definition at line 82 of file zstrm.c.

83{
84 FREE(&address);
85}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
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 303 of file zstrm.c.

304{
305 struct ZstrmContext *zctx = MUTT_MEM_CALLOC(1, struct ZstrmContext);
306
307 /* store wrapped stream as next stream */
308 zctx->next_conn.fd = conn->fd;
309 zctx->next_conn.sockdata = conn->sockdata;
310 zctx->next_conn.open = conn->open;
311 zctx->next_conn.close = conn->close;
312 zctx->next_conn.read = conn->read;
313 zctx->next_conn.write = conn->write;
314 zctx->next_conn.poll = conn->poll;
315
316 /* replace connection with our wrappers, where appropriate */
317 conn->sockdata = zctx;
318 conn->open = zstrm_open;
319 conn->read = zstrm_read;
320 conn->write = zstrm_write;
321 conn->close = zstrm_close;
322 conn->poll = zstrm_poll;
323
324 /* allocate/setup (de)compression buffers */
325 zctx->read.len = 8192;
326 zctx->read.buf = MUTT_MEM_MALLOC(zctx->read.len, char);
327 zctx->read.pos = 0;
328 zctx->write.len = 8192;
329 zctx->write.buf = MUTT_MEM_MALLOC(zctx->write.len, char);
330 zctx->write.pos = 0;
331
332 /* initialise zlib for inflate and deflate for RFC4978 */
333 zctx->read.z.zalloc = zstrm_malloc;
334 zctx->read.z.zfree = zstrm_free;
335 zctx->read.z.opaque = NULL;
336 zctx->read.z.avail_out = zctx->read.len;
337 (void) inflateInit2(&zctx->read.z, -15);
338 zctx->write.z.zalloc = zstrm_malloc;
339 zctx->write.z.zfree = zstrm_free;
340 zctx->write.z.opaque = NULL;
341 zctx->write.z.avail_out = zctx->write.len;
342 (void) deflateInit2(&zctx->write.z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8,
343 Z_DEFAULT_STRATEGY);
344}
static int zstrm_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition zstrm.c:101
static int zstrm_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
Definition zstrm.c:93
static int zstrm_poll(struct Connection *conn, time_t wait_secs)
Check if any data is waiting on a socket - Implements Connection::poll() -.
Definition zstrm.c:221
static int zstrm_read(struct Connection *conn, char *buf, size_t len)
Read compressed data from a socket - Implements Connection::read() -.
Definition zstrm.c:139
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:238
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
void * sockdata
Backend-specific socket data.
Definition connection.h:55
int(* poll)(struct Connection *conn, time_t wait_secs)
Definition connection.h:105
int(* write)(struct Connection *conn, const char *buf, size_t count)
Definition connection.h:92
int(* close)(struct Connection *conn)
Definition connection.h:116
int(* open)(struct Connection *conn)
Definition connection.h:66
int fd
Socket file descriptor.
Definition connection.h:53
int(* read)(struct Connection *conn, char *buf, size_t count)
Definition connection.h:79
Data compression layer.
Definition zstrm.c:59
struct ZstrmDirection read
Data being read and de-compressed.
Definition zstrm.c:60
struct ZstrmDirection write
Data being compressed and written.
Definition zstrm.c:61
struct Connection next_conn
Underlying stream.
Definition zstrm.c:62
unsigned int pos
Current position.
Definition zstrm.c:50
unsigned int len
Length of data.
Definition zstrm.c:49
z_stream z
zlib compression handle
Definition zstrm.c:47
char * buf
Buffer for data being (de-)compressed.
Definition zstrm.c:48
static void * zstrm_malloc(void *opaque, unsigned int items, unsigned int size)
Redirector function for zlib's malloc().
Definition zstrm.c:72
static void zstrm_free(void *opaque, void *address)
Redirector function for zlib's free().
Definition zstrm.c:82
Here is the call graph for this function:
Here is the caller graph for this function: