File: zlib.c

package info (click to toggle)
inn2 2.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,228 kB
  • sloc: ansic: 96,526; sh: 15,562; perl: 13,281; makefile: 3,700; yacc: 842; python: 309; lex: 262
file content (100 lines) | stat: -rw-r--r-- 2,741 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*  $Id: zlib.c 10135 2017-01-12 21:09:58Z iulius $
**
**  COMPRESS functionality.  RFC 8054.
*/

#include "config.h"
#include "clibrary.h"

#include "inn/messages.h"
#include "nnrpd.h"

#if defined(HAVE_ZLIB)
# define ZBUFSIZE 65536
# define MEM_LEVEL 9
# define WINDOW_BITS (-15)      /* Raw deflate. */
bool compression_layer_on = false;
bool tls_compression_on = false;
z_stream *zstream_in = NULL;
z_stream *zstream_out = NULL;
bool zstream_inflate_needed = false;
bool zstream_flush_needed = false;
unsigned char *zbuf_in = NULL;
unsigned char *zbuf_out = NULL;
size_t zbuf_in_size = NNTP_MAXLEN_COMMAND;  /* Initial size of the input
                                             * buffer.  Can be reallocated. */
size_t zbuf_in_allocated = 0;
size_t zbuf_out_size = ZBUFSIZE;  /* Initial size of the output buffer.
                                   * Can be reallocated, when needed. */

/*
**  Wrappers for our memory management functions.
*/
static voidpf zalloc(voidpf opaque UNUSED, uInt items, uInt size)
{
    return (voidpf) xcalloc(items, size);
}

static void zfree(voidpf opaque UNUSED, voidpf address)
{
    free(address);
}


/*
**  The function called by nnrpd to initialize compression support.  Calls
**  both deflateInit2 and inflateInit2, and then checks the result.
**
**  Returns false on error.
*/
bool
zlib_init(void)
{
    int result;
    zstream_in = (z_stream *) xmalloc(sizeof(z_stream));
    zstream_out = (z_stream *) xmalloc(sizeof(z_stream));

    /* Allocate the buffer for compressed input data given to inflate(). */
    zbuf_in = (unsigned char *) xmalloc(zbuf_in_size);

    /* Allocate the buffer for compressed output data produced by deflate(). */
    zbuf_out = (unsigned char *) xmalloc(zbuf_out_size);

    zstream_in->zalloc = zalloc;
    zstream_in->zfree = zfree;
    zstream_in->opaque = Z_NULL;
    zstream_in->next_in = Z_NULL;
    zstream_in->avail_in = 0;

    zstream_out->zalloc = zalloc;
    zstream_out->zfree = zfree;
    zstream_out->opaque = Z_NULL;

    result = inflateInit2(zstream_in, WINDOW_BITS);

    if (result != Z_OK) {
        syslog(L_NOTICE, "inflateInit2() failed with error %d", result);
        free(zstream_in);
        free(zstream_out);
        free(zbuf_in);
        free(zbuf_out);
        return false;
    }

    result = deflateInit2(zstream_out, Z_BEST_COMPRESSION, Z_DEFLATED,
                          WINDOW_BITS, MEM_LEVEL, Z_DEFAULT_STRATEGY);

    if (result != Z_OK) {
        syslog(L_NOTICE, "deflateInit2() failed with error %d", result);
        inflateEnd(zstream_in);
        free(zstream_in);
        free(zstream_out);
        free(zbuf_in);
        free(zbuf_out);
        return false;
    }

    return true;
}

#endif /* HAVE_ZLIB */