-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib_lzo.c
158 lines (139 loc) · 4.26 KB
/
lib_lzo.c
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
/*
* Lessfs: A data deduplicating filesystem.
* Copyright (C) 2008 Mark Ruijter <[email protected]>
*
* This program is free software.
* You can redistribute lessfs and/or modify it under the terms of either
* (1) the GNU General Public License; either version 3 of the License,
* or (at your option) any later version as published by
* the Free Software Foundation; or (2) obtain a commercial license
* by contacting the Author.
*
* 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; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#ifdef LZO
#include <lzo/lzoconf.h>
#include <lzo/lzo1a.h>
#include <lzo/lzo_asm.h>
#include <lzo/lzoutil.h>
/* portability layer */
#define WANT_LZO_MALLOC 1
//#include "portab.h"
#include "lib_log.h"
#include "lib_io.h"
#include "lib_safe.h"
#include "lib_lzo.h"
static pthread_mutex_t lzo_mutex = PTHREAD_MUTEX_INITIALIZER;
extern char *logname;
extern char *function;
extern int debug;
extern int BLKSIZE;
/* We want to compress the data block at `in' with length `IN_LEN' to
* the block at `out'. Because the input block may be incompressible,
* we must provide a little more output space in case that compression
* is not possible.
*/
#ifndef IN_LEN
#define IN_LEN (128*1024L)
#endif
#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
/*************************************************************************
//
**************************************************************************/
void initlzo()
{
if (lzo_init() != LZO_E_OK) {
LFATAL("internal error - lzo_init() failed !!!");
exit(4);
}
}
compr *lzo_compress(unsigned char *buf, int buflen)
{
int r;
lzo_bytep in;
lzo_bytep out;
lzo_bytep wrkmem;
lzo_uint out_len;
compr *retdata;
pthread_mutex_lock(&lzo_mutex);
retdata = s_malloc(sizeof(compr));
in = (lzo_bytep) buf;
out = (lzo_bytep) lzo_malloc(OUT_LEN);
wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS);
if (in == NULL || out == NULL || wrkmem == NULL) {
LFATAL("out of memory\n");
exit(3);
}
r = lzo1a_compress(in, buflen, out, &out_len, wrkmem);
if (r != LZO_E_OK) {
/* this should NEVER happen */
LFATAL("internal error - compression failed: %d\n", r);
exit(2);
}
// LDEBUG("Compressed %i bytes to %lu bytes",buflen,(unsigned long)out_len);
if ( out_len > buflen ) {
retdata->data = s_malloc(buflen+1);
memcpy(&retdata->data[1], buf, buflen);
retdata->size = buflen+1;
retdata->data[0]=0;
} else {
retdata->data = s_malloc(out_len+1);
retdata->size = out_len+1;
memcpy(&retdata->data[1], out, out_len);
retdata->data[0]='L';
}
lzo_free(wrkmem);
lzo_free(out);
pthread_mutex_unlock(&lzo_mutex);
return retdata;
}
compr *lzo_decompress(unsigned char *buf, int buflen)
{
int r;
lzo_bytep in;
lzo_bytep out;
lzo_bytep wrkmem;
lzo_uint out_len = 0;
lzo_uint in_len = 0;
compr *retdata;
FUNC;
pthread_mutex_lock(&lzo_mutex);
retdata = s_malloc(sizeof(compr));
in_len = buflen-1;
in = (lzo_bytep) &buf[1];
out = (lzo_bytep) lzo_malloc(BLKSIZE);
wrkmem = (lzo_bytep) lzo_malloc(LZO1A_MEM_COMPRESS); //FASTER
if (in == NULL || out == NULL || wrkmem == NULL) {
LFATAL("out of memory\n");
exit(3);
}
r = lzo1a_decompress(in, in_len, out, &out_len, NULL);
if (r != LZO_E_OK) {
/* this should NEVER happen */
LFATAL("internal error - decompression failed: %d\n", r);
exit(22);
}
retdata->data = s_malloc(out_len);
retdata->size = out_len;
memcpy(retdata->data, out, out_len);
lzo_free(wrkmem);
lzo_free(out);
pthread_mutex_unlock(&lzo_mutex);
return retdata;
}
#endif