1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2011-2012 Planets Communications B.V.
6    Copyright (C) 2013-2016 Bareos GmbH & Co. KG
7 
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    Affero 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 /*
24  * Kern Sibbald, March MM
25  * Extracted from other source files by Marco van Wieringen, June 2011
26  */
27 /**
28  * @file
29  * Functions to handle compression/decompression of data.
30  */
31 
32 #include "include/bareos.h"
33 #include "filed/filed.h"
34 #include "filed/filed_globals.h"
35 
36 #if defined(HAVE_LZO) || defined(HAVE_LIBZ) || defined(HAVE_FASTLZ)
37 #if defined(HAVE_LIBZ)
38 #include <zlib.h>
39 #endif
40 
41 #if defined(HAVE_FASTLZ)
42 #include <fastlzlib.h>
43 #endif
44 #endif /* defined(HAVE_LZO) || defined(HAVE_LIBZ) || defined(HAVE_FASTLZ) */
45 
46 
47 namespace filedaemon {
48 #if defined(HAVE_LZO) || defined(HAVE_LIBZ) || defined(HAVE_FASTLZ)
49 
50 /**
51  * For compression we enable all used compressors in the fileset.
52  */
AdjustCompressionBuffers(JobControlRecord * jcr)53 bool AdjustCompressionBuffers(JobControlRecord *jcr)
54 {
55    findFILESET *fileset = jcr->ff->fileset;
56    uint32_t compress_buf_size = 0;
57 
58    if (fileset) {
59       int i, j;
60 
61       for (i = 0; i < fileset->include_list.size(); i++) {
62          findIncludeExcludeItem *incexe = (findIncludeExcludeItem *)fileset->include_list.get(i);
63          for (j = 0; j < incexe->opts_list.size(); j++) {
64             findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
65 
66             if (!SetupCompressionBuffers(jcr, me->compatible, fo->Compress_algo,
67                                            &compress_buf_size)) {
68                return false;
69             }
70          }
71       }
72 
73       if (compress_buf_size > 0) {
74          jcr->compress.deflate_buffer = GetMemory(compress_buf_size);
75          jcr->compress.deflate_buffer_size = compress_buf_size;
76       }
77    }
78 
79    return true;
80 }
81 
82 /**
83  * For decompression we use the same decompression buffer for each algorithm.
84  */
AdjustDecompressionBuffers(JobControlRecord * jcr)85 bool AdjustDecompressionBuffers(JobControlRecord *jcr)
86 {
87    uint32_t decompress_buf_size;
88 
89    SetupDecompressionBuffers(jcr, &decompress_buf_size);
90 
91    if (decompress_buf_size > 0) {
92       jcr->compress.inflate_buffer = GetMemory(decompress_buf_size);
93       jcr->compress.inflate_buffer_size = decompress_buf_size;
94    }
95 
96    return true;
97 }
98 
SetupCompressionContext(b_ctx & bctx)99 bool SetupCompressionContext(b_ctx &bctx)
100 {
101    bool retval = false;
102 
103    if (BitIsSet(FO_COMPRESS, bctx.ff_pkt->flags)) {
104       /*
105        * See if we need to be compatible with the old GZIP stream encoding.
106        */
107       if (!me->compatible || bctx.ff_pkt->Compress_algo != COMPRESS_GZIP) {
108          memset(&bctx.ch, 0, sizeof(comp_stream_header));
109 
110          /*
111           * Calculate buffer offsets.
112           */
113          if (BitIsSet(FO_SPARSE, bctx.ff_pkt->flags) ||
114              BitIsSet(FO_OFFSETS, bctx.ff_pkt->flags)) {
115             bctx.chead = (uint8_t *)bctx.jcr->compress.deflate_buffer + OFFSET_FADDR_SIZE;
116             bctx.cbuf = (uint8_t *)bctx.jcr->compress.deflate_buffer + OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
117             bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size - (sizeof(comp_stream_header) + OFFSET_FADDR_SIZE);
118          } else {
119             bctx.chead = (uint8_t *)bctx.jcr->compress.deflate_buffer;
120             bctx.cbuf = (uint8_t *)bctx.jcr->compress.deflate_buffer + sizeof(comp_stream_header);
121             bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size - sizeof(comp_stream_header);
122          }
123 
124          bctx.wbuf = bctx.jcr->compress.deflate_buffer; /* compressed output here */
125          bctx.cipher_input = (uint8_t *)bctx.jcr->compress.deflate_buffer; /* encrypt compressed data */
126          bctx.ch.magic = bctx.ff_pkt->Compress_algo;
127          bctx.ch.version = COMP_HEAD_VERSION;
128       } else {
129          /*
130           * Calculate buffer offsets.
131           */
132          bctx.chead = NULL;
133          if (BitIsSet(FO_SPARSE, bctx.ff_pkt->flags) ||
134              BitIsSet(FO_OFFSETS, bctx.ff_pkt->flags)) {
135             bctx.cbuf = (uint8_t *)bctx.jcr->compress.deflate_buffer + OFFSET_FADDR_SIZE;
136             bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size - OFFSET_FADDR_SIZE;
137          } else {
138             bctx.cbuf = (uint8_t *)bctx.jcr->compress.deflate_buffer;
139             bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size;
140          }
141          bctx.wbuf = bctx.jcr->compress.deflate_buffer; /* compressed output here */
142          bctx.cipher_input = (uint8_t *)bctx.jcr->compress.deflate_buffer; /* encrypt compressed data */
143       }
144 
145       /*
146        * Do compression specific actions and set the magic, header version and compression level.
147        */
148       switch (bctx.ff_pkt->Compress_algo) {
149 #if defined(HAVE_LIBZ)
150       case COMPRESS_GZIP: {
151          z_stream *pZlibStream;
152 
153          /**
154           * Only change zlib parameters if there is no pending operation.
155           * This should never happen as deflateReset is called after each
156           * deflate.
157           */
158          pZlibStream = (z_stream *)bctx.jcr->compress.workset.pZLIB;
159          if (pZlibStream->total_in == 0) {
160             int zstat;
161 
162             /*
163              * Set gzip compression level - must be done per file
164              */
165             if ((zstat = deflateParams(pZlibStream, bctx.ff_pkt->Compress_level, Z_DEFAULT_STRATEGY)) != Z_OK) {
166                Jmsg(bctx.jcr, M_FATAL, 0, _("Compression deflateParams error: %d\n"), zstat);
167                bctx.jcr->setJobStatus(JS_ErrorTerminated);
168                goto bail_out;
169             }
170          }
171          bctx.ch.level = bctx.ff_pkt->Compress_level;
172          break;
173      }
174 #endif
175 #if defined(HAVE_LZO)
176       case COMPRESS_LZO1X:
177          break;
178 #endif
179 #if defined(HAVE_FASTLZ)
180       case COMPRESS_FZFZ:
181       case COMPRESS_FZ4L:
182       case COMPRESS_FZ4H: {
183          int zstat;
184          zfast_stream *pZfastStream;
185          zfast_stream_compressor compressor = COMPRESSOR_FASTLZ;
186 
187          /**
188           * Only change fastlz parameters if there is no pending operation.
189           * This should never happen as fastlzlibCompressReset is called after each
190           * fastlzlibCompress.
191           */
192          pZfastStream = (zfast_stream *)bctx.jcr->compress.workset.pZFAST;
193          if (pZfastStream->total_in == 0) {
194 
195             switch (bctx.ff_pkt->Compress_algo) {
196             case COMPRESS_FZ4L:
197             case COMPRESS_FZ4H:
198                compressor = COMPRESSOR_LZ4;
199                break;
200             }
201 
202             if ((zstat = fastlzlibSetCompressor(pZfastStream, compressor)) != Z_OK) {
203                Jmsg(bctx.jcr, M_FATAL, 0, _("Compression fastlzlibSetCompressor error: %d\n"), zstat);
204                bctx.jcr->setJobStatus(JS_ErrorTerminated);
205                goto bail_out;
206             }
207          }
208          bctx.ch.level = bctx.ff_pkt->Compress_level;
209          break;
210       }
211 #endif
212       default:
213          break;
214       }
215    }
216 
217    retval = true;
218 
219 bail_out:
220    return retval;
221 }
222 
223 #else
224 
225 bool AdjustCompressionBuffers(JobControlRecord *jcr)
226 {
227    return true;
228 }
229 
230 bool SetupCompressionContext(b_ctx &bctx)
231 {
232    return true;
233 }
234 
235 #endif /* defined(HAVE_LZO) || defined(HAVE_LIBZ) || defined(HAVE_FASTLZ) */
236 } /* namespace filedaemon */
237