-
Notifications
You must be signed in to change notification settings - Fork 2
/
libxpack.h
148 lines (126 loc) · 5.21 KB
/
libxpack.h
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
/*
* libxpack.h - public header for libxpack
*/
#ifndef LIBXPACK_H
#define LIBXPACK_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
/* Microsoft C / Visual Studio garbage. If you want to link to the DLL version
* of libxpack, then #define LIBXPACK_DLL. */
#ifdef _MSC_VER
# ifdef BUILDING_LIBXPACK
# define LIBXPACKAPI __declspec(dllexport)
# elif defined(LIBXPACK_DLL)
# define LIBXPACKAPI __declspec(dllimport)
# endif
#endif
#ifndef LIBXPACKAPI
# define LIBXPACKAPI
#endif
/* ========================================================================== */
/* Compression */
/* ========================================================================== */
struct xpack_compressor;
/*
* xpack_alloc_compressor() allocates a new compressor.
*
* 'max_buffer_size' is the maximum size of any buffer which will be compressed
* by the compressor. This specifies the maximum allowed value for the
* 'uncompressed_size' parameter of xpack_compress() when called using this
* compressor.
*
* 'compression_level' is the compression level on a zlib-like scale (1 =
* fastest, 6 = medium/default, 9 = slowest).
*
* Returns a pointer to the new compressor, or NULL if out of memory or the
* maximum buffer size or compression level is not supported.
*/
LIBXPACKAPI struct xpack_compressor *
xpack_alloc_compressor(size_t max_buffer_size, int compression_level);
/*
* xpack_compress() compresses a buffer of data. The function attempts to
* compress 'in_nbytes' bytes of data located at 'in' and write the results to
* 'out', which has space for 'out_nbytes_avail' bytes. The return value is the
* compressed size in bytes, or 0 if the data could not be compressed to
* 'out_nbytes_avail' bytes or fewer.
*/
LIBXPACKAPI size_t
xpack_compress(struct xpack_compressor *compressor,
const void *in, size_t in_nbytes,
void *out, size_t out_nbytes_avail);
/*
* xpack_free_compressor() frees a compressor allocated with
* xpack_alloc_compressor(). If NULL is passed, then no action is taken.
*/
LIBXPACKAPI void
xpack_free_compressor(struct xpack_compressor *compressor);
/* ========================================================================== */
/* Decompression */
/* ========================================================================== */
struct xpack_decompressor;
/*
* xpack_alloc_decompressor() allocates a new decompressor.
*
* Returns a pointer to the new decompressor, or NULL if out of memory.
*/
LIBXPACKAPI struct xpack_decompressor *
xpack_alloc_decompressor(void);
/* Result of a call to xpack_decompress() */
enum decompress_result {
/* Decompression was successful */
DECOMPRESS_SUCCESS = 0,
/* Decompressed failed because the compressed data was invalid, corrupt,
* or otherwise unsupported */
DECOMPRESS_BAD_DATA = 1,
/* A NULL 'actual_out_nbytes_ret' was provided, but the data would have
* decompressed to fewer than 'out_nbytes_avail' bytes */
DECOMPRESS_SHORT_OUTPUT = 2,
/* The data would have decompressed to more than 'out_nbytes_avail'
* bytes */
DECOMPRESS_INSUFFICIENT_SPACE = 3,
};
/*
* xpack_decompress() decompresses 'in_nbytes' bytes of compressed data at 'in'
* and writes the uncompressed data to 'out', which is a buffer of at least
* 'out_nbytes_avail' bytes. If decompression was successful, then 0
* (DECOMPRESS_SUCCESS) is returned; otherwise, a nonzero result code such as
* DECOMPRESS_BAD_DATA is returned. If a nonzero result code is returned, then
* the contents of the output buffer are undefined.
*
* xpack_decompress() can be used in cases where the actual uncompressed size is
* known (recommended) or unknown (not recommended):
*
* - If the actual uncompressed size is known, then pass the actual
* uncompressed size as 'out_nbytes_avail' and pass NULL for
* 'actual_out_nbytes_ret'. This makes xpack_decompress() fail with
* DECOMPRESS_SHORT_OUTPUT if the data decompressed to fewer than the
* specified number of bytes.
*
* - If the actual uncompressed size is unknown, then provide a non-NULL
* 'actual_out_nbytes_ret' and provide a buffer with some size
* 'out_nbytes_avail' that you think is large enough to hold all the
* uncompressed data. In this case, if the data decompresses to less than
* or equal to 'out_nbytes_avail' bytes, then xpack_decompress() will write
* the actual uncompressed size to *actual_out_nbytes_ret and return 0
* (DECOMPRESS_SUCCESS). Otherwise, it will return
* DECOMPRESS_INSUFFICIENT_SPACE if the provided buffer was not large enough
* but no other problems were encountered, or another nonzero result code if
* decompression failed for another reason.
*/
LIBXPACKAPI enum decompress_result
xpack_decompress(struct xpack_decompressor *decompressor,
const void *in, size_t in_nbytes,
void *out, size_t out_nbytes_avail,
size_t *actual_out_nbytes_ret);
/*
* xpack_free_decompressor() frees a decompressor allocated with
* xpack_alloc_decompressor(). If NULL is passed, no action is taken.
*/
LIBXPACKAPI void
xpack_free_decompressor(struct xpack_decompressor *decompressor);
#ifdef __cplusplus
}
#endif
#endif /* LIBXPACK_H */