forked from vandry/mairix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xzfile.c
103 lines (84 loc) · 2.35 KB
/
xzfile.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
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* xz support:
* Copyright (C) Conrad Hughes 2020
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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.
*
**********************************************************************
*/
#ifdef USE_XZ_MBOX
#include <lzma.h>
#include <stdlib.h>
#include <string.h>
#include "mairix.h"
#include "memmac.h"
#include "xzfile.h"
static const lzma_stream s_lzma_init = LZMA_STREAM_INIT;
struct xzFile_s {
lzma_stream m_lzma;
};
xzFile *
xzopen(const char *filename, const char *mode)
{
xzFile *xzf;
lzma_stream *lzs;
int len;
if(filename == NULL || strcmp(mode, "rb") != 0)
goto fail;
if((xzf = new(xzFile)) == NULL)
goto fail;
lzs = &xzf->m_lzma;
memcpy(lzs, &s_lzma_init, sizeof(*lzs));
if(lzma_stream_decoder(lzs, UINT64_MAX, LZMA_CONCATENATED) != LZMA_OK)
goto fail_free;
create_ro_mapping(filename, (unsigned char **) &lzs->next_in, &len,
MAP_NO_DECOMPRESSION);
if(lzs->next_in != NULL) {
lzs->avail_in = len;
return xzf;
}
lzma_end(&xzf->m_lzma);
fail_free:
free(xzf);
fail:
return (xzFile *) NULL;
}
int
xzread(xzFile *xzf, void *buf, int len)
{
lzma_stream *lzs;
if(xzf == NULL || buf == NULL || len <= 0)
return -1;
lzs = &xzf->m_lzma;
lzs->next_out = buf;
lzs->avail_out = len;
if(lzma_code(lzs, LZMA_RUN) != LZMA_OK) /* Argh! */
return -1;
return lzs->next_out - (uint8_t *) buf;
}
void
xzclose(xzFile *xzf)
{
lzma_stream *lzs;
if(xzf == NULL)
return;
lzs = &xzf->m_lzma;
free_ro_mapping((unsigned char *) lzs->next_in - lzs->total_in,
lzs->total_in + lzs->avail_in);
lzma_end(&xzf->m_lzma);
free(xzf);
}
#endif /* USE_XZ_MBOX */