-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostprogs_lib_lzo.c
104 lines (85 loc) · 2.72 KB
/
hostprogs_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
/* microfs - Minimally Improved Compressed Read Only File System
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, ..., +%Y
* Erik Edlund <[email protected]>
*
* 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 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; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "hostprogs_lib.h"
#include "microfs_fs.h"
#include "libinfo_lzo.h"
#ifdef HOSTPROGS_LIB_LZO
#include <errno.h>
#include <stdlib.h>
#include <lzo/lzoconf.h>
#include <lzo/lzo1x.h>
static int hostprog_lib_lzo_init(void** data, __u32 blksz)
{
(void)blksz;
if (!(*data = malloc(LZO1X_999_MEM_COMPRESS))) {
errno = ENOMEM;
return -1;
}
return 0;
}
static int hostprog_lib_lzo_compress(void* data, void* destbuf, __u32* destbufsz,
void* srcbuf, __u32 srcbufsz, int* implerr)
{
lzo_uint destsz = *destbufsz;
lzo_voidp workmem = data;
*implerr = lzo1x_999_compress(srcbuf, srcbufsz,
destbuf, &destsz, workmem);
*destbufsz = *implerr == LZO_E_OK ? destsz : 0;
return *implerr == LZO_E_OK ? 0 : -1;
}
static int hostprog_lib_lzo_decompress(void* data, void* destbuf, __u32* destbufsz,
void* srcbuf, __u32 srcbufsz, int* implerr)
{
(void)data;
lzo_uint destsz = *destbufsz;
*implerr = lzo1x_decompress_safe(srcbuf, srcbufsz,
destbuf, &destsz, NULL);
*destbufsz = *implerr == LZO_E_OK? destsz: 0;
return *implerr == LZO_E_OK ? 0 : -1;
}
static __u32 hostprog_lib_lzo_upperbound(void* data, __u32 size)
{
(void)data;
return size + (size / 16) + 64 + 3;
}
static const char* hostprog_lib_lzo_strerror(void* data, int implerr)
{
(void)data;
(void)implerr;
return "unknown error";
}
const struct hostprog_lib hostprog_lib_lzo = {
.hl_info = &libinfo_lzo,
.hl_compiled = 1,
.hl_init = hostprog_lib_lzo_init,
.hl_mk_usage = hostprog_lib_mk_usage,
.hl_mk_option = hostprog_lib_mk_option,
.hl_mk_dd = hostprog_lib_mk_dd,
.hl_ck_dd = hostprog_lib_ck_dd,
.hl_compress = hostprog_lib_lzo_compress,
.hl_decompress = hostprog_lib_lzo_decompress,
.hl_upperbound = hostprog_lib_lzo_upperbound,
.hl_strerror = hostprog_lib_lzo_strerror
};
#else
const struct hostprog_lib hostprog_lib_lzo = {
.hl_info = &libinfo_lzo,
.hl_compiled = 0
};
#endif