summaryrefslogtreecommitdiff
path: root/package/cfgfs/src/c_lzo1x1.c
blob: 570f8673ca09db058fcb3b802da08b05494b4498 (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
/* $MirOS: contrib/hosted/fwcf/c_lzo1x1.c,v 1.5 2007/03/13 18:31:07 tg Exp $ */

/*-
 * MiniLZO (LZO1X-1) compression plug-in for FWCF
 * Copyright (c) 2006
 *	Thorsten Glaser <tg@mirbsd.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 1, 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; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
 * the utmost extent permitted by applicable law, neither express nor
 * implied; without malicious intent or gross negligence. In no event
 * may a licensor, author or contributor be held liable for indirect,
 * direct, other damage, loss, or other issues arising in any way out
 * of dealing in the work, even if advised of the possibility of such
 * damage or existence of a defect, except proven that it results out
 * of said person's immediate fault when using the work as intended.
 */

#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "defs.h"
#include "compress.h"

__RCSID("$MirOS: contrib/hosted/fwcf/c_lzo1x1.c,v 1.5 2007/03/13 18:31:07 tg Exp $");

#define C_LZO1X1_T1(a,b)			c_lzo1x1_ ## a ## _ ## b
#define C_LZO1X1_T2(a,b)			C_LZO1X1_T1(a,b)
#define LZO_COMPILE_TIME_ASSERT_HEADER(e)	\
		extern int C_LZO1X1_T2(__LINE__,__lzo_cta)[1-2*!(e)];
#define MINILZO_CFG_SKIP_LZO1X_DECOMPRESS	1
#define MINILZO_CFG_SKIP_LZO_PTR		1
#define MINILZO_CFG_SKIP_LZO_STRING		1
#include "minilzo.c"

static void c_lzo1x1_load(void) __attribute__((constructor));
static int c_init(void);
static int c_compress(char **, char *, size_t)
    __attribute__((bounded (string, 2, 3)));
static int c_decompress(char *, size_t, char *, size_t)
    __attribute__((bounded (string, 1, 2)))
    __attribute__((bounded (string, 3, 4)));

static fwcf_compressor c_lzo1x1 = {
	c_init,			/* init */
	c_compress,		/* compress */
	c_decompress,		/* decompress */
	"lzo1x1",		/* name */
	0x10			/* code */
};

/* Work-memory needed for compression. Allocate memory in units
 * of `lzo_align_t' (instead of `char') to make sure it is properly aligned.
 */

#define HEAP_ALLOC(var,size) \
    lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]

static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS);

static void
c_lzo1x1_load(void)
{
	if (compress_register(&c_lzo1x1))
		fputs("warning: cannot register compressor 'lzo1x1'!\n", stderr);
}

static int
c_init(void)
{
	return ((lzo_init() == LZO_E_OK) ? 0 : 1);
}

static int
c_compress(char **dst, char *src, size_t len)
{
	lzo_bytep ldst;
	lzo_uint ldlen = len + (len / 16) + 64 + 3;

	if ((ldst = malloc(ldlen)) == NULL)
		return (-1);
#ifdef DEBUG
	fprintf(stderr, "LZO1X-1 compress %lu bytes -> (%lu)",
	    (u_long)len, (u_long)ldlen);
#endif
	lzo1x_1_compress((lzo_bytep)src, len, ldst, &ldlen, wrkmem);
#ifdef DEBUG
	fprintf(stderr, " %lu bytes\n", (u_long)ldlen);
#endif
	*dst = (char *)ldst;
	return (ldlen);
}

static int
c_decompress(char *dst, size_t dstlen, char *src, size_t srclen)
{
	lzo_uint ldlen = dstlen;
	int i;

#ifdef DEBUG
	fprintf(stderr, "LZO1X decompress %lu -> %lu bytes\n",
	    (u_long)srclen, (u_long)dstlen);
#endif
	if (((i = lzo1x_decompress_safe((lzo_bytep)src, srclen, (lzo_bytep)dst,
	    &ldlen, wrkmem)) == LZO_E_OK) || (i == LZO_E_INPUT_NOT_CONSUMED))
		return (ldlen);
#ifdef DEBUG
	fprintf(stderr, "LZO1X decompress error code %d\n", i);
#endif
	return (-1);
}