This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtre_reader.cpp
More file actions
155 lines (128 loc) · 4.24 KB
/
tre_reader.cpp
File metadata and controls
155 lines (128 loc) · 4.24 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "stdafx.h"
#include "tre_reader.h"
namespace Tre_navigator
{
Tre_reader::Tre_reader(const std::string & filename)
: m_filename(filename)
{
m_stream.open(filename, std::ios_base::binary);
_read_header();
_read_resource_block();
_read_names_block();
_build_lookup_table();
}
bool Tre_reader::is_resource_present(const std::string & res_name)
{
auto result = m_lookup.find(res_name);
return (result != m_lookup.end());
}
std::string Tre_reader::get_resource_name(uint32_t index)
{
if (index >= m_resources.size())
return std::move(std::string(""));
std::string name = (char *)(m_names.data() + m_resources[index].name_offset);
return std::move(name);
}
bool Tre_reader::get_resource(uint32_t index, std::vector<uint8_t>& buffer)
{
if (index >= m_resources.size())
return false;
auto& res_info = m_resources[index];
if (res_info.data_size > 0)
{
if (buffer.size() < res_info.data_size)
buffer.resize(res_info.data_size);
_read_block(res_info.data_offset,
res_info.data_compression,
res_info.data_compressed_size,
res_info.data_size,
buffer.data());
}
return true;
}
bool Tre_reader::get_resource(const std::string & res_name, std::vector<uint8_t>& buffer)
{
auto index = _get_resourece_index(res_name);
if (index == size_t(-1))
return false;
return get_resource(static_cast<uint32_t>(index), buffer);
}
void Tre_reader::_read_header()
{
m_stream.read(reinterpret_cast<char *>(&m_header), sizeof(m_header));
if (strncmp(m_header.file_type, "EERT", 4) != 0)
throw new std::runtime_error("Invalid TRE file format");
if (strncmp(m_header.file_version, "5000", 4) != 0)
throw new std::runtime_error("Invalid TRE file format");
}
void Tre_reader::_read_resource_block()
{
m_resources.resize(m_header.resource_count);
uint32_t uncomp_size = m_header.resource_count * sizeof(Resource_info);
_read_block(m_header.info_offset,
m_header.info_compression,
m_header.info_compressed_size,
uncomp_size,
reinterpret_cast<uint8_t *>(m_resources.data()));
}
void Tre_reader::_read_names_block()
{
m_names.resize(m_header.name_uncompressed_size);
auto name_offset = m_header.info_offset + m_header.info_compressed_size;
_read_block(
name_offset,
m_header.name_compression,
m_header.name_compressed_size,
m_header.name_uncompressed_size,
reinterpret_cast<uint8_t *>(m_names.data()));
}
void Tre_reader::_build_lookup_table()
{
for (size_t index = 0; index < m_resources.size(); ++index)
{
std::string resource_name(get_resource_name(static_cast<uint32_t>(index)));
m_lookup.insert(std::make_pair(resource_name, index));
}
}
size_t Tre_reader::_get_resourece_index(const std::string & name)
{
auto result = m_lookup.find(name);
if (result == m_lookup.end())
return size_t(-1);
return result->second;
}
void Tre_reader::_read_block(uint32_t offset, uint32_t compression, uint32_t comp_size, uint32_t uncomp_size, uint8_t * buffer)
{
if (buffer == nullptr)
return;
if (compression == 0)
{
m_stream.seekg(offset, std::ios_base::beg);
m_stream.read(reinterpret_cast<char *>(buffer), uncomp_size);
}
else if (compression == 2)
{
std::vector<char> compressed_data(comp_size);
m_stream.seekg(offset, std::ios_base::beg);
m_stream.read(compressed_data.data(), comp_size);
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = Z_NULL;
stream.next_in = Z_NULL;
auto result = inflateInit(&stream);
if (result != Z_OK)
throw new std::runtime_error("Zlib error");
stream.next_in = reinterpret_cast<Bytef *>(compressed_data.data());
stream.avail_in = comp_size;
stream.next_out = reinterpret_cast<Bytef *>(buffer);
stream.avail_out = uncomp_size;
inflate(&stream, Z_FINISH);
if (stream.total_out > 0)
inflateEnd(&stream);
}
else
throw new std::runtime_error("Unknown format");
}
}