summaryrefslogtreecommitdiff
path: root/src/common/grfio.h
blob: 13e7201fe3405ba4f642658f5268dfbc21f17ad4 (plain) (blame)
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
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2018  Hercules Dev Team
 * Copyright (C)  Athena Dev Teams
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */
#ifndef COMMON_GRFIO_H
#define COMMON_GRFIO_H

#include "common/hercules.h"

/** @file
 * GRF I/O library.
 */

/// The GRF I/O interface.
struct grfio_interface {
	/**
	 * Interface initialization.
	 *
	 * @param fname Name of the configuration file.
	 */
	void (*init) (const char *fname);

	/// Interface finalization.
	void (*final) (void);

	/**
	 * Reads a file into a newly allocated buffer (from grf or data directory).
	 *
	 * @param[in]  fname Name of the file to read.
	 * @param[out] size  Buffer to return the size of the read file (optional).
	 * @return The file data.
	 * @retval NULL in case of error.
	 */
	void *(*reads) (const char *fname, int *size);

	/**
	 * Finds a file in the grf or data directory
	 *
	 * @param fname The file to find.
	 * @return The original file name.
	 * @retval NULL if the file wasn't found.
	 */
	const char *(*find_file) (const char *fname);

	/**
	 * Calculates a CRC32 hash.
	 *
	 * @param buf The data to hash.
	 * @param len Data length.
	 *
	 * @return The CRC32 hash.
	 */
	unsigned long (*crc32) (const unsigned char *buf, unsigned int len);

	/**
	 * Decompresses ZIP data.
	 *
	 * Decompresses the source buffer into the destination buffer.
	 * source_len is the byte length of the source buffer.  Upon entry,
	 * dest_len is the total size of the destination buffer, which must be
	 * large enough to hold the entire uncompressed data.  (The size of the
	 * uncompressed data must have been saved previously by the compressor
	 * and transmitted to the decompressor by some mechanism outside the
	 * scope of this compression library.) Upon exit, dest_len is the
	 * actual size of the uncompressed buffer.
	 *
	 * @param[in,out] dest       The destination (uncompressed) buffer.
	 * @param[in,out] dest_len   Max length of the destination buffer, returns length of the decompressed data.
	 * @param[in]     source     The source (compressed) buffer.
	 * @param[in]     source_len Source data length.
	 * @return error code.
	 * @retval Z_OK in case of success.
	 */
	int (*decode_zip) (void *dest, unsigned long *dest_len, const void *source, unsigned long source_len);

	/**
	 * Compresses data to ZIP format.
	 *
	 * Compresses the source buffer into the destination buffer.
	 * source_len is the byte length of the source buffer.  Upon entry,
	 * dest_len is the total size of the destination buffer, which must be
	 * at least the value returned by compressBound(source_len).  Upon
	 * exit, dest_len is the actual size of the compressed buffer.
	 *
	 * @param[in,out] dest       The destination (compressed) buffer (if NULL, a new buffer will be created).
	 * @param[in,out] dest_len   Max length of the destination buffer (if 0, it will be calculated).
	 * @param[in]     source     The source (uncompressed) buffer.
	 * @param[in]     source_len Source data length.
	 */
	int (*encode_zip) (void *dest, unsigned long *dest_len, const void *source, unsigned long source_len);
};

/**
 * Reads a file into a newly allocated buffer (from grf or data directory)
 *
 * @param fn The filename to read.
 *
 * @see grfio_interface::reads()
 * @related grfio_interface
 */
#define grfio_read(fn) grfio->reads((fn), NULL)

#ifdef HERCULES_CORE
void grfio_defaults(void);
#endif // HERCULES_CORE

HPShared struct grfio_interface *grfio; ///< Pointer to the grfio interface.
#endif /* COMMON_GRFIO_H */