Horizon Official Technical Documentation
GRF.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
11 * Copyright (c) 2019 Horizon Dev Team.
12 *
13 * Base Author - Sagun K. (sagunxp@gmail.com)
14 *
15 * This library is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 **************************************************/
28
29#ifndef HORIZON_LIBRARIES_GRF_GRF_HPP
30#define HORIZON_LIBRARIES_GRF_GRF_HPP
31
32#include "DES.hpp"
33
34#include <cstdlib>
35#include <string>
36#include <unordered_map>
37#include <boost/filesystem/path.hpp>
38#include <memory>
40{
41 DATAFILE_TYPE_FILE = 0x01, // entry is a file
42 DATAFILE_TYPE_DES_MIXED = 0x02, // encryption mode 0 (header DES + periodic DES/shuffle)
43 DATAFILE_TYPE_DES_HEADER = 0x04, // encryption mode 1 (header DES only)
44};
45
47{
57};
58
60{
63};
64
66{
67 GRE_OK = 0,
71};
72
74{
75 int compressed_size; // compressed size
77 int original_size; // original size
78 int entry_position; // position of entry in grf
79 int hash_table_position; // position in hash table.
80 int next_hash_collision; // index of next DataFile entry with same hash (-1: end of entry chain)
81 char type;
82 char file_name[128 - 4 * 5]; // file name
83};
84
85class GRF
86{
87 typedef std::unordered_map<std::string, std::shared_ptr<DataFile>> FileMapType;
88 typedef std::unordered_map<std::string, grf_file_error_type> FileErrorMapType;
89public:
90 GRF();
92 void extractAllFiles();
93 void extractFile(std::string file_name, std::string output_path, clock_t begin_time);
94 std::pair<grf_read_error_type, uint8_t *> read(const char *in_name, int *size);
95 void decode(unsigned char *buf, size_t len, char entry_type, int entry_len);
96 void decodeFull(unsigned char *buf, size_t len, int cycle);
97 void decodeHeader(unsigned char *buf, size_t len);
98 void decodeShuffledBytes(BIT64 *src);
99 uint8_t substituteObfuscatedByte(uint8_t in);
100
101 uint8_t get_id() { return _id; }
102 void set_id(uint8_t id) { _id = id; }
103
104 /* Path */
105 const boost::filesystem::path &getGRFPath() const { return _path; }
106 void setGRFPath(std::string const &path) { _path = path; }
107 /* Size */
108 std::size_t getGRFSize() const { return _grf_size; }
109 void setGRFSize(std::size_t const &size) { _grf_size = size; }
110 /* Version */
111 int getGRFVersion() const { return _grf_version; }
112 void setGRFVersion(int version) { _grf_version = version; }
113 /* Total Files */
114 int getTotalFiles() const { return _total_files; }
115 void setTotalFiles(int total) { _total_files = total; }
116
119
120private:
121 int _id;
122 boost::filesystem::path _path;
123 std::size_t _grf_size;
128};
129
130
131#endif // HORIZON_LIBRARIES_GRF_GRF_HPP
grf_file_error_type
Definition: GRF.hpp:60
@ GRF_FILE_ERROR_NAME_TOO_LONG
Definition: GRF.hpp:62
@ GRF_FILE_ERROR_NONE
Definition: GRF.hpp:61
datafile_type
Definition: GRF.hpp:40
@ DATAFILE_TYPE_FILE
Definition: GRF.hpp:41
@ DATAFILE_TYPE_DES_MIXED
Definition: GRF.hpp:42
@ DATAFILE_TYPE_DES_HEADER
Definition: GRF.hpp:43
grf_read_error_type
Definition: GRF.hpp:66
@ GRE_READ_ERROR
Definition: GRF.hpp:69
@ GRE_NOT_FOUND
Definition: GRF.hpp:68
@ GRE_DECOMPRESS_SIZE_MISMATCH
Definition: GRF.hpp:70
@ GRE_OK
Definition: GRF.hpp:67
grf_load_result_type
Definition: GRF.hpp:47
@ GRF_LOAD_INCOMPLETE_HEADER
Definition: GRF.hpp:50
@ GRF_LOAD_ILLEGAL_DATA_FORMAT
Definition: GRF.hpp:55
@ GRF_LOAD_OK
Definition: GRF.hpp:48
@ GRF_LOAD_MAGIC_ERROR
Definition: GRF.hpp:51
@ GRF_LOAD_FORMAT_ERROR
Definition: GRF.hpp:52
@ GRF_LOAD_READ_ERROR
Definition: GRF.hpp:56
@ GRF_LOAD_PATH_ERROR
Definition: GRF.hpp:49
@ GRF_LOAD_INVALID_VERSION
Definition: GRF.hpp:53
@ GRF_LOAD_HEADER_READ_ERROR
Definition: GRF.hpp:54
Definition: GRF.hpp:86
FileMapType & getFileMap()
Definition: GRF.hpp:117
void decodeFull(unsigned char *buf, size_t len, int cycle)
Definition: GRF.cpp:120
const boost::filesystem::path & getGRFPath() const
Definition: GRF.hpp:105
FileErrorMapType & getFileErrorMap()
Definition: GRF.hpp:118
void setGRFSize(std::size_t const &size)
Definition: GRF.hpp:109
std::unordered_map< std::string, std::shared_ptr< DataFile > > FileMapType
Definition: GRF.hpp:87
void decodeShuffledBytes(BIT64 *src)
Substitutes some specific values for others, leaves rest intact.
Definition: GRF.cpp:89
boost::filesystem::path _path
Definition: GRF.hpp:122
int getTotalFiles() const
Definition: GRF.hpp:114
uint8_t get_id()
Definition: GRF.hpp:101
grf_load_result_type load()
Definition: GRF.cpp:188
void set_id(uint8_t id)
Definition: GRF.hpp:102
void setGRFPath(std::string const &path)
Definition: GRF.hpp:106
FileMapType _file_map
Definition: GRF.hpp:126
void setTotalFiles(int total)
Definition: GRF.hpp:115
std::size_t getGRFSize() const
Definition: GRF.hpp:108
GRF()
Definition: GRF.cpp:48
void decodeHeader(unsigned char *buf, size_t len)
Definition: GRF.cpp:105
FileErrorMapType _file_error_map
Definition: GRF.hpp:127
std::pair< grf_read_error_type, uint8_t * > read(const char *in_name, int *size)
Definition: GRF.cpp:345
int getGRFVersion() const
Definition: GRF.hpp:111
int _grf_version
Definition: GRF.hpp:124
void extractAllFiles()
Definition: GRF.cpp:274
int _total_files
Definition: GRF.hpp:125
void setGRFVersion(int version)
Definition: GRF.hpp:112
int _id
Definition: GRF.hpp:121
uint8_t substituteObfuscatedByte(uint8_t in)
Substitutes some specific values for others, leaves rest intact.
Definition: GRF.cpp:59
std::size_t _grf_size
Definition: GRF.hpp:123
void decode(unsigned char *buf, size_t len, char entry_type, int entry_len)
Decodes grf data.
Definition: GRF.cpp:162
void extractFile(std::string file_name, std::string output_path, clock_t begin_time)
Definition: GRF.cpp:286
std::unordered_map< std::string, grf_file_error_type > FileErrorMapType
Definition: GRF.hpp:88
One 64-bit block.
Definition: DES.hpp:42
Definition: GRF.hpp:74
int next_hash_collision
Definition: GRF.hpp:80
int original_size
Definition: GRF.hpp:77
char file_name[128 - 4 *5]
Definition: GRF.hpp:82
int compressed_size
Definition: GRF.hpp:75
int hash_table_position
Definition: GRF.hpp:79
char type
Definition: GRF.hpp:81
int entry_position
Definition: GRF.hpp:78
int compressed_aligned_size
Definition: GRF.hpp:76