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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <lz4.h>
#include <lz4hc.h>
#include "internal.h"
typedef struct {
compressor_t base;
} lz4_compressor_t;
typedef struct {
uint32_t version;
uint32_t flags;
} lz4_options;
#define LZ4LEGACY 1
static int lz4_write_options(compressor_t *base, int fd)
{
lz4_options opt = {
.version = htole32(LZ4LEGACY),
.flags = htole32(0),
};
(void)base;
return generic_write_options(fd, &opt, sizeof(opt));
}
static int lz4_read_options(compressor_t *base, int fd)
{
lz4_options opt;
(void)base;
if (generic_read_options(fd, &opt, sizeof(opt)))
return -1;
opt.version = le32toh(opt.version);
opt.flags = le32toh(opt.flags);
if (opt.version != LZ4LEGACY) {
fprintf(stderr, "unsupported lz4 version '%d'\n", opt.version);
return -1;
}
return 0;
}
static ssize_t lz4_comp_block(compressor_t *base, const uint8_t *in,
size_t size, uint8_t *out, size_t outsize)
{
int ret;
(void)base;
ret = LZ4_compress_default((void *)in, (void *)out, size, outsize);
if (ret < 0) {
fputs("internal error in lz4 compressor\n", stderr);
return -1;
}
return ret;
}
static ssize_t lz4_uncomp_block(compressor_t *base, const uint8_t *in,
size_t size, uint8_t *out, size_t outsize)
{
int ret;
(void)base;
ret = LZ4_decompress_safe((void *)in, (void *)out, size, outsize);
if (ret < 0) {
fputs("internal error in lz4 decompressor\n", stderr);
return -1;
}
return ret;
}
static void lz4_destroy(compressor_t *base)
{
free(base);
}
compressor_t *create_lz4_compressor(bool compress, size_t block_size,
char *options)
{
lz4_compressor_t *lz4 = calloc(1, sizeof(*lz4));
compressor_t *base = (compressor_t *)lz4;
(void)block_size;
if (lz4 == NULL) {
perror("creating lz4 compressor");
return NULL;
}
base->destroy = lz4_destroy;
base->do_block = compress ? lz4_comp_block : lz4_uncomp_block;
base->write_options = lz4_write_options;
base->read_options = lz4_read_options;
return base;
}
void compressor_lz4_print_help(void)
{
}
|