]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/binflate.c
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / src / lib / binflate.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Bacula zlib compression wrappers
21  *
22  */
23
24 #include "bacula.h"
25 #ifdef HAVE_LIBZ
26 #include <zlib.h>
27 #endif
28
29 /*
30  * Deflate or compress and input buffer.  You must supply an
31  *  output buffer sufficiently long and the length of the
32  *  output buffer. Generally, if the output buffer is the
33  *  same size as the input buffer, it should work (at least
34  *  for text).
35  */
36 int Zdeflate(char *in, int in_len, char *out, int &out_len)
37 {
38 #ifdef HAVE_LIBZ
39    z_stream strm;
40    int ret;
41
42    /* allocate deflate state */
43    strm.zalloc = Z_NULL;
44    strm.zfree = Z_NULL;
45    strm.opaque = Z_NULL;
46    ret = deflateInit(&strm, 9);
47    if (ret != Z_OK) {
48       Dmsg0(200, "deflateInit error\n");
49       (void)deflateEnd(&strm);
50       return ret;
51    }
52
53    strm.next_in = (Bytef *)in;
54    strm.avail_in = in_len;
55    Dmsg1(200, "In: %d bytes\n", strm.avail_in);
56    strm.avail_out = out_len;
57    strm.next_out = (Bytef *)out;
58    ret = deflate(&strm, Z_FINISH);
59    out_len = out_len - strm.avail_out;
60    Dmsg1(200, "compressed=%d\n", out_len);
61    (void)deflateEnd(&strm);
62    return ret;
63 #else
64    return 1;
65 #endif
66 }
67
68 /*
69  * Inflate or uncompress an input buffer.  You must supply
70  *  and output buffer and an output length sufficiently long
71  *  or there will be an error.  This uncompresses in one call.
72  */
73 int Zinflate(char *in, int in_len, char *out, int &out_len)
74 {
75 #ifdef HAVE_LIBZ
76    z_stream strm;
77    int ret;
78
79    /* allocate deflate state */
80    strm.zalloc = Z_NULL;
81    strm.zfree = Z_NULL;
82    strm.opaque = Z_NULL;
83    strm.next_in = (Bytef *)in;
84    strm.avail_in = in_len;
85    ret = inflateInit(&strm);
86    if (ret != Z_OK) {
87       Dmsg0(200, "inflateInit error\n");
88       (void)inflateEnd(&strm);
89       return ret;
90    }
91
92    Dmsg1(200, "In len: %d bytes\n", strm.avail_in);
93    strm.avail_out = out_len;
94    strm.next_out = (Bytef *)out;
95    ret = inflate(&strm, Z_FINISH);
96    out_len -= strm.avail_out;
97    Dmsg1(200, "Uncompressed=%d\n", out_len);
98    (void)inflateEnd(&strm);
99    return ret;
100 #else
101    return 1;
102 #endif
103 }