summaryrefslogtreecommitdiff
path: root/src/compress.c
blob: eaa9407c8e43494af85def87370abaed2acca9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* compress.c --

   This file is part of the lzop file compressor.

   Copyright (C) 1996-2010 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   lzop and the LZO library are free software; you can redistribute them
   and/or modify them under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/lzop/
 */


#include "conf.h"


/*************************************************************************
//
**************************************************************************/

int x_set_method(int m, int l)
{
    int r = -1;

#if defined(WITH_LZO)
    r = lzo_set_method(m,l);
    if (r >= 0)
        return r;
#endif
#if defined(WITH_NRV)
    r = nrv_set_method(m,l);
    if (r >= 0)
        return r;
#endif
#if defined(WITH_ZLIB)
    r = zlib_set_method(m,l);
    if (r >= 0)
        return r;
#endif

    UNUSED(r);
    return -1;
}


int x_get_method(header_t *h)
{
    int r = -1;

#if defined(WITH_LZO)
    r = lzo_get_method(h);
    if (r >= 0)
        return r;
#endif
#if defined(WITH_NRV)
    r = nrv_get_method(h);
    if (r >= 0)
        return r;
#endif
#if defined(WITH_ZLIB)
    r = zlib_get_method(h);
    if (r >= 0)
        return 0;
#endif

    UNUSED(r);
    return 14;
}


/*************************************************************************
// memory setup
**************************************************************************/

lzo_bool x_enter(const header_t *h)
{
    if (h == NULL)
    {
        lzo_bool ok = 1;
#if defined(WITH_LZO)
        ok &= lzo_enter(NULL);
#endif
#if defined(WITH_NRV)
        ok &= nrv_enter(NULL);
#endif
#if defined(WITH_ZLIB)
        ok &= zlib_enter(NULL);
#endif
        return ok;
    }

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        return lzo_enter(h);
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        return nrv_enter(h);
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        return zlib_enter(h);
#endif
    }
    return 0;
}


void x_leave(const header_t *h)
{
    if (h == NULL)
    {
#if defined(WITH_ZLIB)
        zlib_leave(NULL);
#endif
#if defined(WITH_NRV)
        nrv_leave(NULL);
#endif
#if defined(WITH_LZO)
        lzo_leave(NULL);
#endif
        return;
    }

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        lzo_leave(h);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        nrv_leave(h);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        zlib_leave(h);
        break;
#endif
    }
}


/*************************************************************************
// compress a file
**************************************************************************/

lzo_bool x_compress(file_t *fip, file_t *fop, header_t *h)
{
    lzo_bool ok = 0;

    init_compress_header(h,fip,fop);
    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        lzo_init_compress_header(h);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        nrv_init_compress_header(h);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        zlib_init_compress_header(h);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        break;
    }

    if (!x_enter(h))
        e_memory();

    if (opt_verbose > 1)
    {
        if (opt_unlink)
            fprintf(con_term,"replacing %s with %s", fip->name, fop->name);
        else
            fprintf(con_term,"compressing %s into %s", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }

    write_header(fop,h);

    fip->bytes_processed = fop->bytes_processed = 0;

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        ok = lzo_compress(fip,fop,h);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        ok = nrv_compress(fip,fop,h);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        ok = zlib_compress(fip,fop,h);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        ok = 0;
        break;
    }

    if (opt_cmd == CMD_COMPRESS && opt_verbose > 1)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    set_err_nl(0);

    x_leave(h);
    return ok;
}


/*************************************************************************
// decompress a file
**************************************************************************/

lzo_bool x_decompress(file_t *fip, file_t *fop,
                      const header_t *h, lzo_bool skip)
{
    lzo_bool ok = 0;

    if (!x_enter(h))
        e_memory();

    if (skip)
    {
        assert(opt_cmd != CMD_TEST);
        assert(fop->fd < 0);
        if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
            fprintf(con_term,"skipping %s [%s]", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }
    else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
    {
        if (opt_unlink)
            fprintf(con_term,"restoring %s into %s", fip->name, fop->name);
        else
            fprintf(con_term,"decompressing %s into %s", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }
    else if (opt_cmd == CMD_TEST && opt_verbose > 0)
    {
        /* note: gzip is quiet by default when testing, lzop is not */
        if (opt_verbose > 2)
            fprintf(con_term,"testing %s [%s]", fip->name, fop->name);
        else
            fprintf(con_term,"testing %s", fip->name);
        fflush(con_term);
        set_err_nl(1);
    }

    fip->bytes_processed = fop->bytes_processed = 0;

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        ok = lzo_decompress(fip,fop,h,skip);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        ok = nrv_decompress(fip,fop,h,skip);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        ok = zlib_decompress(fip,fop,h,skip);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        ok = 0;
        break;
    }

    if (skip && opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    else if (opt_cmd == CMD_TEST && opt_verbose > 0)
    {
        fprintf(con_term, ok ? " OK\n" : " FAILED\n");
        fflush(con_term);
    }
    set_err_nl(0);

    if (ok && opt_cmd == CMD_TEST)
        do_test(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_LIST)
        do_list(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_LS)
        do_ls(h,fop->bytes_processed,fip->bytes_processed);
    if (ok && opt_cmd == CMD_INFO)
        do_info(h,fop->bytes_processed,fip->bytes_processed);

    x_leave(h);
    return ok;
}


/*************************************************************************
//
**************************************************************************/

void x_filter(lzo_bytep p, lzo_uint l, const header_t *h)
{
    unsigned f = (unsigned) h->filter;
    lzo_bool c = (opt_cmd == CMD_COMPRESS);

    if (f == 0 || l <= 0)
        return;
    if (f == 1)
    {
        if (c) t_sub1(p,l); else t_add1(p,l);
    }
    else if (f <= 16)
    {
        if (c) t_sub(p,l,f); else t_add(p,l,f);
    }
    else
    {
        fatal(NULL,"Invalid filter");
    }
}



/*
vi:ts=4:et
*/