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