]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/binflate.c
Removed old sd plugins which doesn't work anymore.
[bacula/bacula] / bacula / src / lib / binflate.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf <at> fsfeurope.org.
27 */
28 /*
29  * Bacula zlib compression wrappers
30  *
31  */
32
33 #include "bacula.h"
34 #ifdef HAVE_LIBZ
35 #include <zlib.h>
36 #endif
37
38 /*
39  * Deflate or compress and input buffer.  You must supply an
40  *  output buffer sufficiently long and the length of the
41  *  output buffer. Generally, if the output buffer is the
42  *  same size as the input buffer, it should work (at least
43  *  for text).
44  */
45 int Zdeflate(char *in, int in_len, char *out, int &out_len)
46 {
47 #ifdef HAVE_LIBZ
48    z_stream strm;
49    int ret;
50
51    /* allocate deflate state */
52    strm.zalloc = Z_NULL;
53    strm.zfree = Z_NULL;
54    strm.opaque = Z_NULL;
55    ret = deflateInit(&strm, 9);
56    if (ret != Z_OK) {
57       Dmsg0(200, "deflateInit error\n");
58       (void)deflateEnd(&strm);
59       return ret;
60    }
61
62    strm.next_in = (Bytef *)in;
63    strm.avail_in = in_len;
64    Dmsg1(200, "In: %d bytes\n", strm.avail_in);
65    strm.avail_out = out_len;
66    strm.next_out = (Bytef *)out;
67    ret = deflate(&strm, Z_FINISH);
68    out_len = out_len - strm.avail_out;
69    Dmsg1(200, "compressed=%d\n", out_len);
70    (void)deflateEnd(&strm);
71    return ret;
72 #else
73    return 1;
74 #endif
75 }
76
77 /*
78  * Inflate or uncompress an input buffer.  You must supply
79  *  and output buffer and an output length sufficiently long
80  *  or there will be an error.  This uncompresses in one call.
81  */
82 int Zinflate(char *in, int in_len, char *out, int &out_len)
83 {
84 #ifdef HAVE_LIBZ
85    z_stream strm;
86    int ret;
87
88    /* allocate deflate state */
89    strm.zalloc = Z_NULL;
90    strm.zfree = Z_NULL;
91    strm.opaque = Z_NULL;
92    strm.next_in = (Bytef *)in;
93    strm.avail_in = in_len;
94    ret = inflateInit(&strm);
95    if (ret != Z_OK) {
96       Dmsg0(200, "inflateInit error\n");
97       (void)inflateEnd(&strm);
98       return ret;
99    }
100
101    Dmsg1(200, "In len: %d bytes\n", strm.avail_in);
102    strm.avail_out = out_len;
103    strm.next_out = (Bytef *)out;
104    ret = inflate(&strm, Z_FINISH);
105    out_len -= strm.avail_out;
106    Dmsg1(200, "Uncompressed=%d\n", out_len);
107    (void)inflateEnd(&strm);
108    return ret;
109 #else
110    return 1;
111 #endif
112 }
113