~serge-hallyn/ubuntu/raring/spice/spice-fixed2

« back to all changes in this revision

Viewing changes to client/zlib_decoder.cpp

  • Committer: Package Import Robot
  • Author(s): Liang Guo
  • Date: 2011-07-23 12:21:04 UTC
  • Revision ID: package-import@ubuntu.com-20110723122104-gvv70gd6ui6213qd
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2010 Red Hat, Inc.
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Lesser General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Lesser General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Lesser General Public
 
15
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
#include "common.h"
 
18
#include "zlib_decoder.h"
 
19
#include "debug.h"
 
20
#include "utils.h"
 
21
 
 
22
static void op_decode(SpiceZlibDecoder *decoder,
 
23
                      uint8_t *data,
 
24
                      int data_size,
 
25
                      uint8_t *dest,
 
26
                      int dest_size)
 
27
{
 
28
    ZlibDecoder* _decoder = static_cast<ZlibDecoder*>(decoder);
 
29
    _decoder->decode(data, data_size, dest, dest_size);
 
30
}
 
31
 
 
32
ZlibDecoder::ZlibDecoder()
 
33
{
 
34
    int z_ret;
 
35
    
 
36
    _z_strm.zalloc = Z_NULL;
 
37
    _z_strm.zfree = Z_NULL;
 
38
    _z_strm.opaque = Z_NULL;
 
39
    _z_strm.next_in = Z_NULL;
 
40
    _z_strm.avail_in = 0;
 
41
    z_ret = inflateInit(&_z_strm);
 
42
    if (z_ret != Z_OK) {
 
43
        THROW("zlib decoder init failed, error %d", z_ret);
 
44
    }
 
45
 
 
46
    static SpiceZlibDecoderOps decoder_ops = {
 
47
        op_decode,
 
48
    };
 
49
 
 
50
    ops = &decoder_ops;
 
51
}
 
52
 
 
53
ZlibDecoder::~ZlibDecoder()
 
54
{
 
55
    inflateEnd(&_z_strm);
 
56
}
 
57
 
 
58
 
 
59
void ZlibDecoder::decode(uint8_t *data, int data_size, uint8_t *dest, int dest_size)
 
60
{
 
61
    int z_ret;
 
62
 
 
63
    inflateReset(&_z_strm);
 
64
    _z_strm.next_in = data;
 
65
    _z_strm.avail_in = data_size;
 
66
    _z_strm.next_out = dest;
 
67
    _z_strm.avail_out = dest_size;
 
68
 
 
69
    z_ret = inflate(&_z_strm, Z_FINISH);
 
70
   
 
71
    if (z_ret != Z_STREAM_END) {
 
72
        THROW("zlib inflate failed, error %d", z_ret);
 
73
    }
 
74
}