~ubuntu-branches/ubuntu/breezy/atlas-cpp/breezy

« back to all changes in this revision

Viewing changes to Atlas/Filters/Gzip.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2005-10-02 11:41:44 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051002114144-8qmn4d1cdn9g27ta
Tags: 0.5.98-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file may be redistributed and modified only under the terms of
 
2
// the GNU Lesser General Public License (See COPYING for details).
 
3
// Copyright (C) 2000 Dmitry Derevyanko
 
4
 
 
5
#ifdef HAVE_CONFIG_H
 
6
#include "config.h"
 
7
#endif
 
8
 
 
9
#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
 
10
 
 
11
#include <Atlas/Filters/Gzip.h>
 
12
 
 
13
#ifndef ASSERT
 
14
#include <cassert>
 
15
#define ASSERT(exp) assert(exp)
 
16
#endif
 
17
 
 
18
using Atlas::Filters::Gzip;
 
19
 
 
20
const int DEFAULT_LEVEL = 6;
 
21
 
 
22
void Gzip::begin()
 
23
{
 
24
    incoming.next_in = Z_NULL;
 
25
    incoming.avail_in = 0;
 
26
    incoming.zalloc = Z_NULL;
 
27
    incoming.zfree = Z_NULL;
 
28
  
 
29
    outgoing.zalloc = Z_NULL;
 
30
    outgoing.zfree = Z_NULL;
 
31
  
 
32
    inflateInit(&incoming);
 
33
    deflateInit(&outgoing, DEFAULT_LEVEL);
 
34
}
 
35
  
 
36
void Gzip::end()
 
37
{
 
38
    inflateEnd(&incoming);
 
39
    deflateEnd(&outgoing);
 
40
}
 
41
    
 
42
std::string Gzip::encode(const std::string& data)
 
43
{
 
44
    std::string out_string;
 
45
    int status;
 
46
    
 
47
    buf[0] = 0;
 
48
 
 
49
    outgoing.next_in = (unsigned char *)data.data();
 
50
    outgoing.avail_in = data.size();
 
51
 
 
52
    do 
 
53
    {       
 
54
        outgoing.next_out = buf;
 
55
        outgoing.avail_out = sizeof(buf);
 
56
          
 
57
        status = deflate(&outgoing, Z_SYNC_FLUSH);
 
58
          
 
59
        ASSERT(status == Z_OK);
 
60
          
 
61
        out_string.append((char*)buf, sizeof(buf) - outgoing.avail_out);        
 
62
          
 
63
    } while (outgoing.avail_out == 0);
 
64
    
 
65
    return out_string;
 
66
}
 
67
    
 
68
std::string Gzip::decode(const std::string& data)
 
69
{
 
70
    std::string out_string;
 
71
    int status;
 
72
 
 
73
    buf[0] = 0;
 
74
 
 
75
    incoming.next_in = (unsigned char*)data.data();
 
76
    incoming.avail_in = data.size();
 
77
 
 
78
    do 
 
79
    {
 
80
        incoming.next_out = buf;
 
81
        incoming.avail_out = sizeof(buf);
 
82
          
 
83
        status = inflate(&incoming, Z_SYNC_FLUSH);
 
84
          
 
85
        ASSERT(status == Z_OK);
 
86
          
 
87
        out_string.append((char*)buf, sizeof(buf) - incoming.avail_out);
 
88
          
 
89
    } while(incoming.avail_out == 0);
 
90
 
 
91
    return out_string;
 
92
}
 
93
 
 
94
#endif // HAVE_ZLIB_H && HAVE_LIBZ