1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3    Copyright (C) 2010 Red Hat, Inc.
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "config.h"
19 
20 #include "decode.h"
21 
22 #ifndef __GNUC__
23 #define ZLIB_WINAPI
24 #endif
25 
26 #include <zlib.h>
27 
28 typedef struct GlibZlibDecoder
29 {
30     SpiceZlibDecoder         base;
31     z_stream                 _z_strm;
32 } GlibZlibDecoder;
33 
decode(SpiceZlibDecoder * decoder,uint8_t * data,int data_size,uint8_t * dest,int dest_size)34 static void decode(SpiceZlibDecoder *decoder,
35                    uint8_t *data, int data_size,
36                    uint8_t *dest, int dest_size)
37 {
38     GlibZlibDecoder *d = SPICE_CONTAINEROF(decoder, GlibZlibDecoder, base);
39     int z_ret;
40 
41     inflateReset(&d->_z_strm);
42     d->_z_strm.next_in = data;
43     d->_z_strm.avail_in = data_size;
44     d->_z_strm.next_out = dest;
45     d->_z_strm.avail_out = dest_size;
46 
47     z_ret = inflate(&d->_z_strm, Z_FINISH);
48 
49     if (z_ret != Z_STREAM_END) {
50         g_warning("zlib inflate failed, error %d", z_ret);
51     }
52 }
53 
54 static SpiceZlibDecoderOps zlib_decoder_ops = {
55     .decode = decode,
56 };
57 
zlib_decoder_new(void)58 SpiceZlibDecoder *zlib_decoder_new(void)
59 {
60     GlibZlibDecoder *d = g_new0(GlibZlibDecoder, 1);
61     int z_ret;
62 
63     d->_z_strm.zalloc = Z_NULL;
64     d->_z_strm.zfree = Z_NULL;
65     d->_z_strm.opaque = Z_NULL;
66     d->_z_strm.next_in = Z_NULL;
67     d->_z_strm.avail_in = 0;
68     z_ret = inflateInit(&d->_z_strm);
69     if (z_ret != Z_OK) {
70         g_warning("zlib decoder init failed, error %d", z_ret);
71         goto fail;
72     }
73 
74     d->base.ops = &zlib_decoder_ops;
75 
76     return &d->base;
77 
78 fail:
79     g_free(d);
80     return NULL;
81 }
82 
zlib_decoder_destroy(SpiceZlibDecoder * decoder)83 void zlib_decoder_destroy(SpiceZlibDecoder *decoder)
84 {
85     GlibZlibDecoder *d = SPICE_CONTAINEROF(decoder, GlibZlibDecoder, base);
86 
87     inflateEnd(&d->_z_strm);
88     g_free(d);
89 }
90