GPAK  1.0.0
A general-purpose archive library
gpak_data.h
Go to the documentation of this file.
1 /**
2  * @file gpak_data.h
3  * @author AdamFull
4  * @date 23.04.2023
5  * @brief The gpak_data.h header file contains all the data structures used by
6  * the Gpak library API for managing game archives.
7  *
8  * This header file defines the data structures used to represent the internal
9  * organization of a game archive file, including its metadata, file and
10  * directory entries, and the internal file system tree. It also includes
11  * structures for managing iterators and other utility functions.
12  *
13  * These data structures are used throughout the Gpak API to provide users
14  * with the ability to create, manipulate, and extract information from game
15  * archive files.
16  *
17  * To use the Gpak library, simply include this header file along with the main
18  * gpak.h header file and link against the Gpak library in your project.
19  */
20 
21 #ifndef GPAK_DATA_H
22 #define GPAK_DATA_H
23 
24 #include <stdio.h>
25 #include <stdint.h>
26 
27 /**
28  * @brief Enumeration representing the compression algorithms for G-PAK headers.
29  *
30  * This enumeration contains values representing the different compression algorithms that can be used for G-PAK archive headers. The available compression algorithms are none, deflate, LZ4, and Zstandard (ZST).
31  */
33 {
34  GPAK_HEADER_COMPRESSION_NONE = 0, /**< No compression. */
35  GPAK_HEADER_COMPRESSION_DEFLATE = 1 << 0, /**< Deflate compression algorithm. */
36  GPAK_HEADER_COMPRESSION_ZST = 1 << 1 /**< Zstandard (ZST) compression algorithm. */
37 };
38 
39 /**
40  * @brief Typedef for the gpak_header_compression_algorithm enumeration.
41  *
42  * This typedef is used to create an alias for the gpak_header_compression_algorithm enumeration, providing a more convenient way to use the enumeration in the code.
43  */
45 
46 /**
47  * @brief Enumeration representing the deflate compression levels for G-PAK.
48  *
49  * This enumeration contains values representing the different compression levels that can be used with the deflate algorithm in G-PAK operations. The available compression levels are none, fast, and best.
50  */
52 {
53  GPAK_COMPRESSION_DEFLATE_NONE = 0, /**< No compression. */
54  GPAK_COMPRESSION_DEFLATE_FAST = 1, /**< Fast compression. */
55  GPAK_COMPRESSION_DEFLATE_BEST = 9 /**< Best compression. */
56 };
57 
58 /**
59  * @brief Typedef for the gpak_compression_deflate enumeration.
60  *
61  * This typedef is used to create an alias for the gpak_compression_deflate enumeration, providing a more convenient way to use the enumeration in the code.
62  */
64 
65 /**
66  * @brief Enumeration representing the compression levels for LZ4 algorithm in G-PAK.
67  *
68  * This enumeration contains values representing the different compression levels that can be used with the LZ4 compression algorithm in G-PAK. The higher the compression level, the better the compression ratio but the slower the compression speed.
69  */
71 {
72  GPAK_COMPRESSION_LZ4_NONE = 0, /**< No compression is applied. */
73  GPAK_COMPRESSION_LZ4_FAST = 3, /**< Fast compression with lower compression ratio. */
74  GPAK_COMPRESSION_LZ4_MEDIUM = 6, /**< Medium compression level, balancing compression speed and ratio. */
75  GPAK_COMPRESSION_LZ4_BEST = 12, /**< Best compression level with higher compression ratio but slower compression speed. */
76 };
77 
78 /**
79  * @brief Typedef for the gpak_compression_lz4 enumeration.
80  *
81  * This typedef is used to create an alias for the gpak_compression_lz4 enumeration, providing a more convenient way to use the enumeration in the code.
82  */
84 
85 /**
86  * @brief Enumeration representing the Zstandard compression levels for G-PAK.
87  *
88  * This enumeration contains values representing the different Zstandard compression levels that can be used in G-PAK. The compression levels range from no compression (GPAK_COMPRESSION_ZST_NONE) to best compression (GPAK_COMPRESSION_ZST_BEST).
89  */
91 {
92  GPAK_COMPRESSION_ZST_NONE = 0, /**< No compression. */
93  GPAK_COMPRESSION_ZST_FAST = 5, /**< Fast compression, lower compression ratio. */
94  GPAK_COMPRESSION_ZST_MEDIUM = 10, /**< Medium compression, balance between speed and compression ratio. */
95  GPAK_COMPRESSION_ZST_BEST = 22, /**< Best compression, higher compression ratio but slower speed. */
96 };
97 
98 /**
99  * @brief Typedef for the gpak_compression_zstd enumeration.
100  *
101  * This typedef is used to create an alias for the gpak_compression_zstd enumeration, providing a more convenient way to use the enumeration in the code.
102  */
104 
105 /**
106  * @brief Structure representing the header of a G-PAK archive.
107  *
108  * This structure contains information about the G-PAK archive itself, including the format identifier, compression algorithm, compression level, the number of entries in the archive, and the dictionary size.
109  */
111 {
112  char format_[5]; /**< A null-terminated string representing the G-PAK archive format identifier. */
113  gpak_header_compression_algorithm_t compression_; /**< The compression algorithm used in the G-PAK archive. */
114  char compression_level_; /**< The compression level applied to the entries in the G-PAK archive. */
115  uint32_t entry_count_; /**< The number of entries in the G-PAK archive. */
116  uint32_t dictionary_size_; /**< The size of the dictionary used for compression in the G-PAK archive. */
117 };
118 
119 /**
120  * @brief Typedef for the gpak_header structure.
121  *
122  * This typedef is used to create an alias for the gpak_header structure, providing a more convenient way to use the structure in the code.
123  */
124 typedef struct gpak_header pak_header_t;
125 
126 
127 /**
128  * @brief Structure representing an entry header in a G-PAK archive.
129  *
130  * This structure contains information about a single entry within a G-PAK archive, including its compressed and uncompressed sizes, the offset at which it is stored, and its CRC-32 checksum.
131  */
133 {
134  uint32_t compressed_size_; /**< The compressed size of the entry in bytes. */
135  uint32_t uncompressed_size_; /**< The uncompressed size of the entry in bytes. */
136  size_t offset_; /**< The offset at which the entry is stored in the G-PAK archive. */
137  uint32_t crc32_; /**< The CRC-32 checksum of the entry. */
138 };
139 
140 /**
141  * @brief Typedef for the gpak_entry_header structure.
142  *
143  * This typedef is used to create an alias for the gpak_entry_header structure, providing a more convenient way to use the structure in the code.
144  */
145 typedef struct gpak_entry_header pak_entry_t;
146 
147 
148 /**
149  * @brief Enumeration representing the error codes for G-PAK operations.
150  *
151  * This enumeration contains values representing the different error codes that can be encountered during G-PAK operations, such as issues with archive or stream handling, mode errors, and compression/decompression errors.
152  */
154 {
155  GPAK_ERROR_OK = 0, /**< No error occurred. */
156  GPAK_ERROR_ARCHIVE_IS_NULL = -1, /**< The archive pointer is NULL. */
157  GPAK_ERROR_STREAM_IS_NULL = -2, /**< The stream pointer is NULL. */
158  GPAK_ERROR_INCORRECT_MODE = -3, /**< The mode flag is incorrect. */
159  GPAK_ERROR_INCORRECT_HEADER = -4, /**< The archive header is incorrect. */
160  GPAK_ERROR_OPEN_FILE = -5, /**< An error occurred while opening a file. */
161  GPAK_ERROR_WRITE = -6, /**< An error occurred while writing to a file. */
162  GPAK_ERROR_READ = -7, /**< An error occurred while reading from a file. */
163  GPAK_ERROR_FILE_NOT_FOUND = -8, /**< The specified file was not found. */
164 
165  // Deflate
166  GPAK_ERROR_DEFLATE_INIT = -9, /**< An error occurred while initializing deflate. */
167  GPAK_ERROR_DEFLATE_FAILED = -10, /**< Deflate operation failed. */
168  GPAK_ERROR_INFLATE_INIT = -11, /**< An error occurred while initializing inflate. */
169  GPAK_ERROR_INFLATE_FAILED = -12, /**< Inflate operation failed. */
170 
171  // LZ4 errors
172  GPAK_ERROR_LZ4_WRITE_OPEN = -13, /**< An error occurred while opening an LZ4 file for writing. */
173  GPAK_ERROR_LZ4_WRITE = -14, /**< An error occurred while writing to an LZ4 file. */
174  GPAK_ERROR_LZ4_WRITE_CLOSE = -15, /**< An error occurred while closing an LZ4 file after writing. */
175  GPAK_ERROR_LZ4_READ_OPEN = -16, /**< An error occurred while opening an LZ4 file for reading. */
176  GPAK_ERROR_LZ4_READ = -17, /**< An error occurred while reading from an LZ4 file. */
177  GPAK_ERROR_LZ4_READ_CLOSE = -18, /**< An error occurred while closing an LZ4 file after reading. */
178  GPAK_ERROR_LZ4_DECOMPRESS = -19, /**< An error occurred during LZ4 decompression. */
179 
180  // ZSTD
181  GPAK_ERROR_EMPTY_INPUT = -20, /**< The input is empty. */
182  GPAK_ERROR_EOF_BEFORE_EOS = -21, /**< End of file encountered before end of stream. */
183 
184  //crc
185  GPAK_ERROR_FILE_CRC_NOT_MATCH = -22, /**< The file CRC does not match. */
186 
187  // dictionary
188  GPAK_ERROR_INVALID_DICTIONARY = -23, /**< The dictionary is invalid. */
189  GPAK_ERROR_FAILED_TO_CREATE_DICTIONARY = -24 /**< Failed to create a dictionary. */
190 };
191 
192 /**
193  * @brief Typedef for the gpak_error enumeration.
194  *
195  * This typedef is used to create an alias for the gpak_error enumeration, providing a more convenient way to use the enumeration in the code.
196  */
197 typedef enum gpak_error gpak_error_t;
198 
199 /**
200  * @brief Enumeration representing the mode flags for G-PAK archive operations.
201  *
202  * This enumeration contains values representing the different mode flags that can be used to control the behavior of G-PAK archive operations, such as creating, reading, and updating archives.
203  */
205 {
206  GPAK_MODE_NONE = 0, /**< No specific mode flag is set. */
207  GPAK_MODE_CREATE = 1 << 0, /**< Create mode: allows writing to the archive. Analogous to write byte operation. */
208  GPAK_MODE_READ_ONLY = 1 << 1, /**< Read-only mode: allows only reading from the archive. Analogous to read byte operation. */
209  GPAK_MODE_UPDATE = 1 << 2 /**< Update mode: allows both reading and writing to the archive. */
210 };
211 
212 /**
213  * @brief Typedef for the gpak_mode_flag enumeration.
214  *
215  * This typedef is used to create an alias for the gpak_mode_flag enumeration, providing a more convenient way to use the enumeration in the code.
216  */
217 typedef enum gpak_mode_flag gpak_mode_flag_t;
218 
219 /**
220  * @brief Enumeration representing the stages of the G-PAK archive operations.
221  *
222  * This enumeration contains values representing the different stages during which the G-PAK archive operations occur, such as compression and decompression.
223  */
225 {
226  GPAK_STAGE_COMPRESSION, /**< The compression stage of a G-PAK operation. */
227  GPAK_STAGE_DECOMPRESSION /**< The decompression stage of a G-PAK operation. */
228 };
229 
230 /**
231  * @brief Typedef for the gpak_stage_flag enumeration.
232  *
233  * This typedef is used to create an alias for the gpak_stage_flag enumeration, providing a more convenient way to use the enumeration in the code.
234  */
236 
237 /**
238  * @typedef gpak_error_handler_t
239  * @brief A callback function for handling errors in G-PAK operations.
240  */
241 typedef void (*gpak_error_handler_t)(const char*, int32_t, void*);
242 
243 /**
244  * @typedef gpak_progress_handler_t
245  * @brief A callback function for handling progress in G-PAK operations.
246  */
247 typedef void (*gpak_progress_handler_t)(const char*, size_t, size_t, int32_t, void*);
248 
249 
250 /**
251  * @brief Structure representing a G-PAK archive.
252  *
253  * This structure contains information about a G-PAK archive, including its mode, header, file stream, filesystem tree root, error handling, progress handling, and user data. It also includes a dictionary and a pointer to the current file being processed.
254  */
255 struct gpak
256 {
257  int mode_; /**< The access mode of the G-PAK archive (read, write, update, etc.). */
258  pak_header_t header_; /**< The header information of the G-PAK archive. */
259  FILE* stream_; /**< The file stream associated with the G-PAK archive. */
260  struct filesystem_tree_node* root_; /**< The root node of the filesystem tree representing the archive's directory structure. */
261  int last_error_; /**< The last error code encountered during G-PAK operations. */
262  char* dictionary_; /**< The compression dictionary for the G-PAK archive. */
263  char* current_file_; /**< The current file being processed during G-PAK operations. */
264  gpak_error_handler_t error_handler_; /**< The error handler function for G-PAK operations. */
265  gpak_progress_handler_t progress_handler_; /**< The progress handler function for G-PAK operations. */
266  void* user_data_; /**< User-defined data associated with the G-PAK archive. */
267 };
268 
269 /**
270  * @brief Typedef for the gpak structure.
271  *
272  * This typedef is used to create an alias for the gpak structure, providing a more convenient way to use the structure in the code.
273  */
274 typedef struct gpak gpak_t;
275 
276 
277 /**
278  * @brief Structure representing a file within a G-PAK archive.
279  *
280  * This structure contains information about a file in a G-PAK archive, including its data, file stream, and CRC32 checksum.
281  */
282 struct gpak_file
283 {
284  char* data_; /**< The data of the file in the G-PAK archive. */
285  FILE* stream_; /**< The file stream associated with the file in the G-PAK archive. */
286  uint32_t crc32_; /**< The CRC32 checksum of the file in the G-PAK archive. */
287 };
288 
289 /**
290  * @brief Typedef for the gpak_file structure.
291  *
292  * This typedef is used to create an alias for the gpak_file structure, providing a more convenient way to use the structure in the code.
293  */
294 typedef struct gpak_file gpak_file_t;
295 
296 
297 /**
298  * @brief Structure representing a file within a filesystem tree.
299  *
300  * This structure contains information about a file in a filesystem tree, including its name, path, and associated pak_entry_t data.
301  */
303 {
304  char* name_; /**< The name of the file in the filesystem tree. */
305  char* path_; /**< The path of the file in the filesystem tree. */
306  pak_entry_t entry_; /**< The pak_entry_t data associated with the file in the filesystem tree. */
307 };
308 
309 /**
310  * @brief Typedef for the filesystem_tree_file structure.
311  *
312  * This typedef is used to create an alias for the filesystem_tree_file structure, providing a more convenient way to use the structure in the code.
313  */
315 
316 
317 /**
318  * @brief Structure representing a node within a filesystem tree.
319  *
320  * This structure contains information about a directory node in a filesystem tree, including its name, parent, children nodes, files, and counts of children and files.
321  */
323 {
324  char* name_; /**< The name of the directory node in the filesystem tree. */
325  struct filesystem_tree_node* parent_; /**< The parent directory node of the current node in the filesystem tree. */
326  struct filesystem_tree_node** children_; /**< An array of pointers to the children directory nodes of the current node in the filesystem tree. */
327  size_t num_children_; /**< The number of children directory nodes in the current node of the filesystem tree. */
328  filesystem_tree_file_t** files_; /**< An array of pointers to the files contained in the current directory node of the filesystem tree. */
329  size_t num_files_; /**< The number of files contained in the current directory node of the filesystem tree. */
330 };
331 
332 /**
333  * @brief Typedef for the filesystem_tree_node structure.
334  *
335  * This typedef is used to create an alias for the filesystem_tree_node structure, providing a more convenient way to use the structure in the code.
336  */
338 
339 
340 /**
341  * @brief Structure representing the state of a filesystem iterator.
342  *
343  * This structure contains information about the current state of a filesystem iterator, including the current node, child index, and file index.
344  */
346 {
347  filesystem_tree_node_t* node_; /**< The current node in the filesystem tree being iterated. */
348  size_t child_index_; /**< The index of the current child directory node in the current node of the filesystem tree. */
349  size_t file_index_; /**< The index of the current file in the current directory node of the filesystem tree. */
350 };
351 
352 /**
353  * @brief Typedef for the filesystem_iterator_state structure.
354  *
355  * This typedef is used to create an alias for the filesystem_iterator_state structure, providing a more convenient way to use the structure in the code.
356  */
358 
359 
360 /**
361  * @brief Structure representing a filesystem tree iterator.
362  *
363  * This structure contains information about a filesystem tree iterator, including a stack of iterator states, the current stack size, and stack capacity.
364  */
366 {
367  filesystem_iterator_state_t* stack_; /**< A dynamic array of filesystem iterator states representing the stack for the iterator. */
368  size_t stack_size_; /**< The current size of the stack, indicating the number of elements in the stack. */
369  size_t stack_capacity_; /**< The maximum capacity of the stack, indicating the maximum number of elements that can be stored in the stack without resizing. */
370 };
371 
372 /**
373  * @brief Typedef for the filesystem_tree_iterator structure.
374  *
375  * This typedef is used to create an alias for the filesystem_tree_iterator structure, providing a more convenient way to use the structure in the code.
376  */
378 
379 #endif // GPAK_DATA_H
gpak_header_compression_algorithm
Definition: gpak_data.h:33
@ GPAK_HEADER_COMPRESSION_NONE
Definition: gpak_data.h:34
@ GPAK_HEADER_COMPRESSION_ZST
Definition: gpak_data.h:36
@ GPAK_HEADER_COMPRESSION_DEFLATE
Definition: gpak_data.h:35
gpak_mode_flag
Definition: gpak_data.h:205
@ GPAK_MODE_READ_ONLY
Definition: gpak_data.h:208
@ GPAK_MODE_NONE
Definition: gpak_data.h:206
@ GPAK_MODE_CREATE
Definition: gpak_data.h:207
@ GPAK_MODE_UPDATE
Definition: gpak_data.h:209
void(* gpak_error_handler_t)(const char *, int32_t, void *)
Definition: gpak_data.h:241
enum gpak_stage_flag gpak_stage_flag_t
Definition: gpak_data.h:235
void(* gpak_progress_handler_t)(const char *, size_t, size_t, int32_t, void *)
Definition: gpak_data.h:247
gpak_error
Definition: gpak_data.h:154
@ GPAK_ERROR_INFLATE_FAILED
Definition: gpak_data.h:169
@ GPAK_ERROR_INVALID_DICTIONARY
Definition: gpak_data.h:188
@ GPAK_ERROR_LZ4_DECOMPRESS
Definition: gpak_data.h:178
@ GPAK_ERROR_INFLATE_INIT
Definition: gpak_data.h:168
@ GPAK_ERROR_FAILED_TO_CREATE_DICTIONARY
Definition: gpak_data.h:189
@ GPAK_ERROR_WRITE
Definition: gpak_data.h:161
@ GPAK_ERROR_INCORRECT_MODE
Definition: gpak_data.h:158
@ GPAK_ERROR_STREAM_IS_NULL
Definition: gpak_data.h:157
@ GPAK_ERROR_LZ4_READ_CLOSE
Definition: gpak_data.h:177
@ GPAK_ERROR_EMPTY_INPUT
Definition: gpak_data.h:181
@ GPAK_ERROR_LZ4_WRITE_CLOSE
Definition: gpak_data.h:174
@ GPAK_ERROR_ARCHIVE_IS_NULL
Definition: gpak_data.h:156
@ GPAK_ERROR_READ
Definition: gpak_data.h:162
@ GPAK_ERROR_OPEN_FILE
Definition: gpak_data.h:160
@ GPAK_ERROR_DEFLATE_FAILED
Definition: gpak_data.h:167
@ GPAK_ERROR_LZ4_WRITE_OPEN
Definition: gpak_data.h:172
@ GPAK_ERROR_DEFLATE_INIT
Definition: gpak_data.h:166
@ GPAK_ERROR_EOF_BEFORE_EOS
Definition: gpak_data.h:182
@ GPAK_ERROR_LZ4_READ_OPEN
Definition: gpak_data.h:175
@ GPAK_ERROR_LZ4_WRITE
Definition: gpak_data.h:173
@ GPAK_ERROR_FILE_NOT_FOUND
Definition: gpak_data.h:163
@ GPAK_ERROR_OK
Definition: gpak_data.h:155
@ GPAK_ERROR_INCORRECT_HEADER
Definition: gpak_data.h:159
@ GPAK_ERROR_FILE_CRC_NOT_MATCH
Definition: gpak_data.h:185
@ GPAK_ERROR_LZ4_READ
Definition: gpak_data.h:176
gpak_compression_lz4
Definition: gpak_data.h:71
@ GPAK_COMPRESSION_LZ4_NONE
Definition: gpak_data.h:72
@ GPAK_COMPRESSION_LZ4_FAST
Definition: gpak_data.h:73
@ GPAK_COMPRESSION_LZ4_BEST
Definition: gpak_data.h:75
@ GPAK_COMPRESSION_LZ4_MEDIUM
Definition: gpak_data.h:74
enum gpak_header_compression_algorithm gpak_header_compression_algorithm_t
Definition: gpak_data.h:44
enum gpak_error gpak_error_t
Definition: gpak_data.h:197
gpak_compression_zstd
Definition: gpak_data.h:91
@ GPAK_COMPRESSION_ZST_MEDIUM
Definition: gpak_data.h:94
@ GPAK_COMPRESSION_ZST_BEST
Definition: gpak_data.h:95
@ GPAK_COMPRESSION_ZST_NONE
Definition: gpak_data.h:92
@ GPAK_COMPRESSION_ZST_FAST
Definition: gpak_data.h:93
gpak_stage_flag
Definition: gpak_data.h:225
@ GPAK_STAGE_COMPRESSION
Definition: gpak_data.h:226
@ GPAK_STAGE_DECOMPRESSION
Definition: gpak_data.h:227
gpak_compression_deflate
Definition: gpak_data.h:52
@ GPAK_COMPRESSION_DEFLATE_NONE
Definition: gpak_data.h:53
@ GPAK_COMPRESSION_DEFLATE_BEST
Definition: gpak_data.h:55
@ GPAK_COMPRESSION_DEFLATE_FAST
Definition: gpak_data.h:54
enum gpak_compression_lz4 gpak_compression_lz4_t
Definition: gpak_data.h:83
enum gpak_compression_deflate gpak_compression_deflate_t
Definition: gpak_data.h:63
enum gpak_mode_flag gpak_mode_flag_t
Definition: gpak_data.h:217
enum gpak_compression_zstd gpak_compression_zstd_t
Definition: gpak_data.h:103
filesystem_tree_node_t * node_
Definition: gpak_data.h:347
pak_entry_t entry_
Definition: gpak_data.h:306
filesystem_iterator_state_t * stack_
Definition: gpak_data.h:367
struct filesystem_tree_node * parent_
Definition: gpak_data.h:325
struct filesystem_tree_node ** children_
Definition: gpak_data.h:326
filesystem_tree_file_t ** files_
Definition: gpak_data.h:328
uint32_t crc32_
Definition: gpak_data.h:137
uint32_t uncompressed_size_
Definition: gpak_data.h:135
uint32_t compressed_size_
Definition: gpak_data.h:134
size_t offset_
Definition: gpak_data.h:136
Definition: gpak_data.h:133
uint32_t crc32_
Definition: gpak_data.h:286
FILE * stream_
Definition: gpak_data.h:285
char * data_
Definition: gpak_data.h:284
char format_[5]
Definition: gpak_data.h:112
uint32_t entry_count_
Definition: gpak_data.h:115
char compression_level_
Definition: gpak_data.h:114
uint32_t dictionary_size_
Definition: gpak_data.h:116
gpak_header_compression_algorithm_t compression_
Definition: gpak_data.h:113
gpak_progress_handler_t progress_handler_
Definition: gpak_data.h:265
char * current_file_
Definition: gpak_data.h:263
char * dictionary_
Definition: gpak_data.h:262
struct filesystem_tree_node * root_
Definition: gpak_data.h:260
void * user_data_
Definition: gpak_data.h:266
gpak_error_handler_t error_handler_
Definition: gpak_data.h:264
FILE * stream_
Definition: gpak_data.h:259
int mode_
Definition: gpak_data.h:257
pak_header_t header_
Definition: gpak_data.h:258
int last_error_
Definition: gpak_data.h:261