1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 #ifndef _CFILEARCHIVE_H
11 #define _CFILEARCHIVE_H
12 
13 #ifndef _CFILE_INTERNAL
14 #pragma message("This file should only be included internally in CFILE!!")
15 #endif
16 
17 #include "globalincs/pstypes.h"
18 
19 // The following Cfile_block data is private to cfile.cpp
20 // DO NOT MOVE the Cfile_block* information to cfile.h / do not extern this data
21 //
22 #define CFILE_BLOCK_UNUSED		0
23 #define CFILE_BLOCK_USED		1
24 
25 struct COMPRESSION_INFO {
26 	int header = 0;
27 	size_t compressed_size = 0;
28 	int block_size = 0;
29 	int num_offsets = 0;
30 	int* offsets = nullptr;
31 	char* decoder_buffer = nullptr;
32 	int last_decoded_block_pos = 0;
33 	int last_decoded_block_bytes = 0;
34 };
35 
36 struct CFILE {
37 	int type = CFILE_BLOCK_UNUSED;                // CFILE_BLOCK_UNUSED, CFILE_BLOCK_USED
38 	int dir_type;        // directory location
39 	FILE* fp;                // File pointer if opening an individual file
40 	const void* data;            // Pointer for memory-mapped file access.  NULL if not mem-mapped.
41 	bool mem_mapped; // Flag for memory mapped files (if data is not null and this is false it means that it's an embedded file)
42 #ifdef _WIN32
43 	HANDLE	hInFile;			// Handle from CreateFile()
44 	HANDLE	hMapFile;		// Handle from CreateFileMapping()
45 #else
46 	size_t data_length;    // length of data for mmap
47 #endif
48 	size_t lib_offset;
49 	size_t raw_position;
50 	size_t size;                // for packed files
51 
52 	size_t max_read_len;    // max read offset, for special error handling
53 
54 	SCP_string original_filename;
55 	const char* source_file;
56 	int line_num;
57 	COMPRESSION_INFO compression_info;
58 };
59 
60 #define MAX_CFILE_BLOCKS	64
61 extern std::array<CFILE, MAX_CFILE_BLOCKS> Cfile_block_list;
62 
63 // Called once to setup the low-level reading code.
64 void cf_init_lowlevel_read_code(CFILE* cfile, size_t lib_offset, size_t size, size_t pos);
65 
66 // This checks if the file is compressed or not, and creates the proper compression info if so.
67 void cf_check_compression(CFILE* cfile);
68 
69 // Used to clear compression info data and free dynamic memory used by compression
70 void cf_clear_compression_info(CFILE* cfile);
71 
72 #endif
73