NeoMutt  2024-04-16-36-g75b6fb
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 <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()

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

72{
73 return mutt_mem_calloc(items, size);
74}
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 81 of file zstrm.c.

82{
83 FREE(&address);
84}
#define FREE(x)
Definition: memory.h:45
+ 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 291 of file zstrm.c.

292{
293 struct ZstrmContext *zctx = mutt_mem_calloc(1, sizeof(struct ZstrmContext));
294
295 /* store wrapped stream as next stream */
296 zctx->next_conn.fd = conn->fd;
297 zctx->next_conn.sockdata = conn->sockdata;
298 zctx->next_conn.open = conn->open;
299 zctx->next_conn.close = conn->close;
300 zctx->next_conn.read = conn->read;
301 zctx->next_conn.write = conn->write;
302 zctx->next_conn.poll = conn->poll;
303
304 /* replace connection with our wrappers, where appropriate */
305 conn->sockdata = zctx;
306 conn->open = zstrm_open;
307 conn->read = zstrm_read;
308 conn->write = zstrm_write;
309 conn->close = zstrm_close;
310 conn->poll = zstrm_poll;
311
312 /* allocate/setup (de)compression buffers */
313 zctx->read.len = 8192;
314 zctx->read.buf = mutt_mem_malloc(zctx->read.len);
315 zctx->read.pos = 0;
316 zctx->write.len = 8192;
317 zctx->write.buf = mutt_mem_malloc(zctx->write.len);
318 zctx->write.pos = 0;
319
320 /* initialise zlib for inflate and deflate for RFC4978 */
321 zctx->read.z.zalloc = zstrm_malloc;
322 zctx->read.z.zfree = zstrm_free;
323 zctx->read.z.opaque = NULL;
324 zctx->read.z.avail_out = zctx->read.len;
325 (void) inflateInit2(&zctx->read.z, -15);
326 zctx->write.z.zalloc = zstrm_malloc;
327 zctx->write.z.zfree = zstrm_free;
328 zctx->write.z.opaque = NULL;
329 zctx->write.z.avail_out = zctx->write.len;
330 (void) deflateInit2(&zctx->write.z, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, 8,
331 Z_DEFAULT_STRATEGY);
332}
static int zstrm_close(struct Connection *conn)
Close a socket - Implements Connection::close() -.
Definition: zstrm.c:100
static int zstrm_open(struct Connection *conn)
Open a socket - Implements Connection::open() -.
Definition: zstrm.c:92
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:214
static int zstrm_read(struct Connection *conn, char *buf, size_t len)
Read compressed data from a socket - Implements Connection::read() -.
Definition: zstrm.c:132
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:231
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
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:58
struct ZstrmDirection read
Data being read and de-compressed.
Definition: zstrm.c:59
struct ZstrmDirection write
Data being compressed and written.
Definition: zstrm.c:60
struct Connection next_conn
Underlying stream.
Definition: zstrm.c:61
unsigned int pos
Current position.
Definition: zstrm.c:49
unsigned int len
Length of data.
Definition: zstrm.c:48
z_stream z
zlib compression handle
Definition: zstrm.c:46
char * buf
Buffer for data being (de-)compressed.
Definition: zstrm.c:47
static void * zstrm_malloc(void *opaque, unsigned int items, unsigned int size)
Redirector function for zlib's malloc()
Definition: zstrm.c:71
static void zstrm_free(void *opaque, void *address)
Redirector function for zlib's free()
Definition: zstrm.c:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function: