libctru  v2.4.1
decompress.h
Go to the documentation of this file.
1 /**
2  * @file decompress.h
3  * @brief Decompression functions.
4  */
5 #pragma once
6 
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 /** @brief Compression types */
12 typedef enum
13 {
14  DECOMPRESS_DUMMY = 0x00, ///< Dummy compression
15  DECOMPRESS_LZSS = 0x10, ///< LZSS/LZ10 compression
16  DECOMPRESS_LZ10 = 0x10, ///< LZSS/LZ10 compression
17  DECOMPRESS_LZ11 = 0x11, ///< LZ11 compression
18  DECOMPRESS_HUFF1 = 0x21, ///< Huffman compression with 1-bit data
19  DECOMPRESS_HUFF2 = 0x22, ///< Huffman compression with 2-bit data
20  DECOMPRESS_HUFF3 = 0x23, ///< Huffman compression with 3-bit data
21  DECOMPRESS_HUFF4 = 0x24, ///< Huffman compression with 4-bit data
22  DECOMPRESS_HUFF5 = 0x25, ///< Huffman compression with 5-bit data
23  DECOMPRESS_HUFF6 = 0x26, ///< Huffman compression with 6-bit data
24  DECOMPRESS_HUFF7 = 0x27, ///< Huffman compression with 7-bit data
25  DECOMPRESS_HUFF8 = 0x28, ///< Huffman compression with 8-bit data
26  DECOMPRESS_HUFF = 0x28, ///< Huffman compression with 8-bit data
27  DECOMPRESS_RLE = 0x30, ///< Run-length encoding compression
29 
30 /** @brief I/O vector */
31 typedef struct
32 {
33  void *data; ///< I/O buffer
34  size_t size; ///< Buffer size
36 
37 /** @brief Data callback */
38 typedef ssize_t (*decompressCallback)(void *userdata, void *buffer,
39  size_t size);
40 
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45 
46 /** @brief Decompression callback for file descriptors
47  * @param[in] userdata Address of file descriptor
48  * @param[in] buffer Buffer to write into
49  * @param[in] size Size to read from file descriptor
50  * @returns Number of bytes read
51  */
52 ssize_t decompressCallback_FD(void *userdata, void *buffer, size_t size);
53 
54 /** @brief Decompression callback for stdio FILE*
55  * @param[in] userdata FILE*
56  * @param[in] buffer Buffer to write into
57  * @param[in] size Size to read from file descriptor
58  * @returns Number of bytes read
59  */
60 ssize_t decompressCallback_Stdio(void *userdata, void *buffer, size_t size);
61 
62 /** @brief Decode decompression header
63  * @param[out] type Decompression type
64  * @param[out] size Decompressed size
65  * @param[in] callback Data callback (see decompressV())
66  * @param[in] userdata User data passed to callback (see decompressV())
67  * @param[in] insize Size of userdata (see decompressV())
68  * @returns Bytes consumed
69  * @retval -1 error
70  */
71 ssize_t decompressHeader(decompressType *type, size_t *size,
72  decompressCallback callback, void *userdata,
73  size_t insize);
74 
75 /** @brief Decompress data
76  * @param[in] iov Output vector
77  * @param[in] iovcnt Number of buffers
78  * @param[in] callback Data callback (see note)
79  * @param[in] userdata User data passed to callback (see note)
80  * @param[in] insize Size of userdata (see note)
81  * @returns Whether succeeded
82  *
83  * @note If callback is null, userdata is a pointer to memory to read from,
84  * and insize is the size of that data. If callback is not null,
85  * userdata is passed to callback to fetch more data, and insize is
86  * unused.
87  */
88 bool decompressV(const decompressIOVec *iov, size_t iovcnt,
89  decompressCallback callback, void *userdata, size_t insize);
90 
91 /** @brief Decompress data
92  * @param[in] output Output buffer
93  * @param[in] size Output size limit
94  * @param[in] callback Data callback (see decompressV())
95  * @param[in] userdata User data passed to callback (see decompressV())
96  * @param[in] insize Size of userdata (see decompressV())
97  * @returns Whether succeeded
98  */
99 static inline bool
100 decompress(void *output, size_t size, decompressCallback callback,
101  void *userdata, size_t insize)
102 {
103  decompressIOVec iov;
104  iov.data = output;
105  iov.size = size;
106 
107  return decompressV(&iov, 1, callback, userdata, insize);
108 }
109 
110 /** @brief Decompress LZSS/LZ10
111  * @param[in] iov Output vector
112  * @param[in] iovcnt Number of buffers
113  * @param[in] callback Data callback (see decompressV())
114  * @param[in] userdata User data passed to callback (see decompressV())
115  * @param[in] insize Size of userdata (see decompressV())
116  * @returns Whether succeeded
117  */
118 bool decompressV_LZSS(const decompressIOVec *iov, size_t iovcnt,
119  decompressCallback callback, void *userdata,
120  size_t insize);
121 
122 /** @brief Decompress LZSS/LZ10
123  * @param[in] output Output buffer
124  * @param[in] size Output size limit
125  * @param[in] callback Data callback (see decompressV())
126  * @param[in] userdata User data passed to callback (see decompressV())
127  * @param[in] insize Size of userdata (see decompressV())
128  * @returns Whether succeeded
129  */
130 static inline bool
131 decompress_LZSS(void *output, size_t size, decompressCallback callback,
132  void *userdata, size_t insize)
133 {
134  decompressIOVec iov;
135  iov.data = output;
136  iov.size = size;
137 
138  return decompressV_LZSS(&iov, 1, callback, userdata, insize);
139 }
140 
141 /** @brief Decompress LZ11
142  * @param[in] iov Output vector
143  * @param[in] iovcnt Number of buffers
144  * @param[in] callback Data callback (see decompressV())
145  * @param[in] userdata User data passed to callback (see decompressV())
146  * @param[in] insize Size of userdata (see decompressV())
147  * @returns Whether succeeded
148  */
149 bool decompressV_LZ11(const decompressIOVec *iov, size_t iovcnt,
150  decompressCallback callback, void *userdata,
151  size_t insize);
152 
153 /** @brief Decompress LZ11
154  * @param[in] output Output buffer
155  * @param[in] size Output size limit
156  * @param[in] callback Data callback (see decompressV())
157  * @param[in] userdata User data passed to callback (see decompressV())
158  * @param[in] insize Size of userdata (see decompressV())
159  * @returns Whether succeeded
160  */
161 static inline bool
162 decompress_LZ11(void *output, size_t size, decompressCallback callback,
163  void *userdata, size_t insize)
164 {
165  decompressIOVec iov;
166  iov.data = output;
167  iov.size = size;
168 
169  return decompressV_LZ11(&iov, 1, callback, userdata, insize);
170 }
171 
172 /** @brief Decompress Huffman
173  * @param[in] bits Data size in bits (usually 4 or 8)
174  * @param[in] iov Output vector
175  * @param[in] iovcnt Number of buffers
176  * @param[in] callback Data callback (see decompressV())
177  * @param[in] userdata User data passed to callback (see decompressV())
178  * @param[in] insize Size of userdata (see decompressV())
179  * @returns Whether succeeded
180  */
181 bool decompressV_Huff(size_t bits, const decompressIOVec *iov, size_t iovcnt,
182  decompressCallback callback, void *userdata,
183  size_t insize);
184 
185 /** @brief Decompress Huffman
186  * @param[in] bits Data size in bits (usually 4 or 8)
187  * @param[in] output Output buffer
188  * @param[in] size Output size limit
189  * @param[in] callback Data callback (see decompressV())
190  * @param[in] userdata User data passed to callback (see decompressV())
191  * @param[in] insize Size of userdata (see decompressV())
192  * @returns Whether succeeded
193  */
194 static inline bool
195 decompress_Huff(size_t bits, void *output, size_t size,
196  decompressCallback callback, void *userdata, size_t insize)
197 {
198  decompressIOVec iov;
199  iov.data = output;
200  iov.size = size;
201 
202  return decompressV_Huff(bits, &iov, 1, callback, userdata, insize);
203 }
204 
205 /** @brief Decompress run-length encoding
206  * @param[in] iov Output vector
207  * @param[in] iovcnt Number of buffers
208  * @param[in] callback Data callback (see decompressV())
209  * @param[in] userdata User data passed to callback (see decompressV())
210  * @param[in] insize Size of userdata (see decompressV())
211  * @returns Whether succeeded
212  */
213 bool decompressV_RLE(const decompressIOVec *iov, size_t iovcnt,
214  decompressCallback callback, void *userdata,
215  size_t insize);
216 
217 /** @brief Decompress run-length encoding
218  * @param[in] output Output buffer
219  * @param[in] size Output size limit
220  * @param[in] callback Data callback (see decompressV())
221  * @param[in] userdata User data passed to callback (see decompressV())
222  * @param[in] insize Size of userdata (see decompressV())
223  * @returns Whether succeeded
224  */
225 static inline bool
226 decompress_RLE(void *output, size_t size, decompressCallback callback,
227  void *userdata, size_t insize)
228 {
229  decompressIOVec iov;
230  iov.data = output;
231  iov.size = size;
232 
233  return decompressV_RLE(&iov, 1, callback, userdata, insize);
234 }
235 
236 #ifdef __cplusplus
237 }
238 #endif
bool decompressV_RLE(const decompressIOVec *iov, size_t iovcnt, decompressCallback callback, void *userdata, size_t insize)
Decompress run-length encoding.
static bool decompress_RLE(void *output, size_t size, decompressCallback callback, void *userdata, size_t insize)
Decompress run-length encoding.
Definition: decompress.h:226
static bool decompress_LZSS(void *output, size_t size, decompressCallback callback, void *userdata, size_t insize)
Decompress LZSS/LZ10.
Definition: decompress.h:131
ssize_t decompressCallback_FD(void *userdata, void *buffer, size_t size)
Decompression callback for file descriptors.
ssize_t decompressHeader(decompressType *type, size_t *size, decompressCallback callback, void *userdata, size_t insize)
Decode decompression header.
ssize_t decompressCallback_Stdio(void *userdata, void *buffer, size_t size)
Decompression callback for stdio FILE*.
bool decompressV_LZ11(const decompressIOVec *iov, size_t iovcnt, decompressCallback callback, void *userdata, size_t insize)
Decompress LZ11.
ssize_t(* decompressCallback)(void *userdata, void *buffer, size_t size)
Data callback.
Definition: decompress.h:38
static bool decompress_Huff(size_t bits, void *output, size_t size, decompressCallback callback, void *userdata, size_t insize)
Decompress Huffman.
Definition: decompress.h:195
bool decompressV_Huff(size_t bits, const decompressIOVec *iov, size_t iovcnt, decompressCallback callback, void *userdata, size_t insize)
Decompress Huffman.
bool decompressV(const decompressIOVec *iov, size_t iovcnt, decompressCallback callback, void *userdata, size_t insize)
Decompress data.
bool decompressV_LZSS(const decompressIOVec *iov, size_t iovcnt, decompressCallback callback, void *userdata, size_t insize)
Decompress LZSS/LZ10.
static bool decompress_LZ11(void *output, size_t size, decompressCallback callback, void *userdata, size_t insize)
Decompress LZ11.
Definition: decompress.h:162
static bool decompress(void *output, size_t size, decompressCallback callback, void *userdata, size_t insize)
Decompress data.
Definition: decompress.h:100
decompressType
Compression types.
Definition: decompress.h:13
@ DECOMPRESS_LZSS
LZSS/LZ10 compression.
Definition: decompress.h:15
@ DECOMPRESS_HUFF4
Huffman compression with 4-bit data.
Definition: decompress.h:21
@ DECOMPRESS_LZ10
LZSS/LZ10 compression.
Definition: decompress.h:16
@ DECOMPRESS_HUFF3
Huffman compression with 3-bit data.
Definition: decompress.h:20
@ DECOMPRESS_HUFF2
Huffman compression with 2-bit data.
Definition: decompress.h:19
@ DECOMPRESS_HUFF5
Huffman compression with 5-bit data.
Definition: decompress.h:22
@ DECOMPRESS_HUFF
Huffman compression with 8-bit data.
Definition: decompress.h:26
@ DECOMPRESS_HUFF8
Huffman compression with 8-bit data.
Definition: decompress.h:25
@ DECOMPRESS_RLE
Run-length encoding compression.
Definition: decompress.h:27
@ DECOMPRESS_DUMMY
Dummy compression.
Definition: decompress.h:14
@ DECOMPRESS_LZ11
LZ11 compression.
Definition: decompress.h:17
@ DECOMPRESS_HUFF7
Huffman compression with 7-bit data.
Definition: decompress.h:24
@ DECOMPRESS_HUFF6
Huffman compression with 6-bit data.
Definition: decompress.h:23
@ DECOMPRESS_HUFF1
Huffman compression with 1-bit data.
Definition: decompress.h:18
I/O vector.
Definition: decompress.h:32
size_t size
Buffer size.
Definition: decompress.h:34
void * data
I/O buffer.
Definition: decompress.h:33