summaryrefslogtreecommitdiffstats
path: root/server/zlib_encoder.c
blob: e0d8d830a286e76b042cce33136b324669d9eb3f (plain)
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
101
102
103
104
#include "red_common.h"
#include "zlib_encoder.h"
#include <zlib.h>

struct ZlibEncoder {
    ZlibEncoderUsrContext *usr;

    z_stream strm;
    int last_level;
};

ZlibEncoder* zlib_encoder_create(ZlibEncoderUsrContext *usr, int level)
{
    ZlibEncoder *enc;
    int z_ret;

    if (!usr->more_space || !usr->more_input) {
        return NULL;
    }

    enc = spice_new0(ZlibEncoder, 1);

    enc->usr = usr;

    enc->strm.zalloc = Z_NULL;
    enc->strm.zfree = Z_NULL;
    enc->strm.opaque = Z_NULL;

    z_ret = deflateInit(&enc->strm, level);
    enc->last_level = level;
    if (z_ret != Z_OK) {
        red_printf("zlib error");
        free(enc);
        return NULL;
    }

    return enc;
}

void zlib_encoder_destroy(ZlibEncoder *encoder)
{
    deflateEnd(&encoder->strm);
    free(encoder);
}

/* returns the total size of the encoded data */
int zlib_encode(ZlibEncoder *zlib, int level, int input_size,
                uint8_t *io_ptr, unsigned int num_io_bytes)
{
    int flush;
    int enc_size = 0;
    int out_size = 0;
    int z_ret;

    z_ret = deflateReset(&zlib->strm);

    if (z_ret != Z_OK) {
        red_error("deflateReset failed");
    }

    zlib->strm.next_out = io_ptr;
    zlib->strm.avail_out = num_io_bytes;

    if (level != zlib->last_level) {
        if (zlib->strm.avail_out == 0) {
            zlib->strm.avail_out = zlib->usr->more_space(zlib->usr, &zlib->strm.next_out);
            if (zlib->strm.avail_out == 0) {
                red_error("not enough space");
            }
        }
        z_ret = deflateParams(&zlib->strm, level, Z_DEFAULT_STRATEGY);
        if (z_ret != Z_OK) {
            red_error("deflateParams failed");
        }
        zlib->last_level = level;
    }


    do {
        zlib->strm.avail_in = zlib->usr->more_input(zlib->usr, &zlib->strm.next_in);
        if (zlib->strm.avail_in <= 0) {
            red_error("more input failed\n");
        }
        enc_size += zlib->strm.avail_in;
        flush = (enc_size == input_size) ?  Z_FINISH : Z_NO_FLUSH;
        while (1) {
            int deflate_size = zlib->strm.avail_out;
            z_ret = deflate(&zlib->strm, flush);
            ASSERT(z_ret != Z_STREAM_ERROR);
            out_size += deflate_size - zlib->strm.avail_out;
            if (zlib->strm.avail_out) {
                break;
            }

            zlib->strm.avail_out = zlib->usr->more_space(zlib->usr, &zlib->strm.next_out);
            if (zlib->strm.avail_out == 0) {
                red_error("not enough space");
            }
        }
    } while (flush != Z_FINISH);

    ASSERT(z_ret == Z_STREAM_END);
    return out_size;
}