forked from xomniversex/foo_gep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpackers.cpp
211 lines (186 loc) · 6.21 KB
/
unpackers.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <foobar2000.h>
#include <fex/Zip7_Extractor.h>
#include <fex/Rar_Extractor.h>
#include <memory>
#include "reader.h"
static void handle_error( const char * str )
{
if ( str ) throw exception_io_data( str );
}
static t_filestats get_stats_in_archive( const char * p_archive, const char * p_file, abort_callback & p_abort )
{
service_ptr_t< file > m_file;
filesystem::g_open( m_file, p_archive, filesystem::open_mode_read, p_abort );
foobar_File_Reader in( m_file, p_abort );
std::unique_ptr<fex_t> ex( new Zip7_Extractor );
if ( ex->open( &in ) )
{
ex.reset( new Rar_Extractor );
handle_error( in.seek( 0 ) );
}
handle_error( ex->open( &in ) );
while ( ! ex->done() )
{
handle_error( ex->stat() );
if ( ! strcmp( ex->name(), p_file ) ) break;
handle_error( ex->next() );
}
if ( ex->done() ) throw exception_io_not_found();
t_filestats ret;
ret.m_size = ex->size();
ret.m_timestamp = m_file->get_timestamp( p_abort );
return ret;
}
static void open_archive( service_ptr_t< file > & p_out, const char * p_archive, const char * p_file, abort_callback & p_abort )
{
service_ptr_t< file > m_file;
filesystem::g_open( m_file, p_archive, filesystem::open_mode_read, p_abort );
t_filetimestamp timestamp = m_file->get_timestamp( p_abort );
foobar_File_Reader in( m_file, p_abort );
std::unique_ptr<fex_t> ex( new Zip7_Extractor );
if ( ex->open( &in ) )
{
ex.reset( new Rar_Extractor );
in.seek( 0 );
}
handle_error( ex->open( &in ) );
while ( ! ex->done() )
{
handle_error( ex->stat() );
if ( ! strcmp( ex->name(), p_file ) ) break;
handle_error( ex->next() );
}
if ( ex->done() ) throw exception_io_not_found();
const void * data;
handle_error( ex->data( &data ) );
p_out = new service_impl_t<reader_membuffer_simple>( data, (t_size)ex->size(), timestamp, m_file->is_remote() );
}
static void archive_list( archive * owner, const char * type, const char * path, const service_ptr_t< file > & p_reader, archive_callback & p_out, bool p_want_readers )
{
if ( stricmp_utf8( pfc::string_extension( path ), type ) )
throw exception_io_data();
service_ptr_t< file > m_file = p_reader;
if ( m_file.is_empty() )
filesystem::g_open( m_file, path, filesystem::open_mode_read, p_out );
else
m_file->reopen( p_out );
t_filetimestamp timestamp = m_file->get_timestamp( p_out );
foobar_File_Reader in( m_file, p_out );
std::unique_ptr<fex_t> ex( new Zip7_Extractor );
if ( ex->open( &in ) )
{
ex.reset( new Rar_Extractor );
in.seek( 0 );
}
handle_error( ex->open( &in ) );
pfc::string8_fastalloc m_path;
service_ptr_t<file> m_out_file;
t_filestats m_stats;
m_stats.m_timestamp = timestamp;
while ( ! ex->done() )
{
handle_error( ex->stat() );
archive_impl::g_make_unpack_path( m_path, path, ex->name(), type );
m_stats.m_size = ex->size();
if ( p_want_readers )
{
const void * data;
handle_error( ex->data( &data ) );
m_out_file = new service_impl_t<reader_membuffer_simple>( data, (t_size)m_stats.m_size, timestamp, m_file->is_remote() );
}
if ( ! p_out.on_entry( owner, m_path, m_stats, m_out_file ) ) break;
handle_error( ex->next() );
}
}
class archive_rsn : public archive_impl
{
public:
virtual bool supports_content_types()
{
return false;
}
virtual const char * get_archive_type()
{
return "rsn";
}
virtual t_filestats get_stats_in_archive( const char * p_archive, const char * p_file, abort_callback & p_abort )
{
return ::get_stats_in_archive( p_archive, p_file, p_abort );
}
virtual void open_archive( service_ptr_t< file > & p_out, const char * p_archive, const char * p_file, abort_callback & p_abort )
{
::open_archive( p_out, p_archive, p_file, p_abort );
}
virtual void archive_list( const char * path, const service_ptr_t< file > & p_reader, archive_callback & p_out, bool p_want_readers )
{
::archive_list( this, get_archive_type(), path, p_reader, p_out, p_want_readers );
}
};
class archive_vgm7z : public archive_impl
{
public:
virtual bool supports_content_types()
{
return false;
}
virtual const char * get_archive_type()
{
return "vgm7z";
}
virtual t_filestats get_stats_in_archive( const char * p_archive, const char * p_file, abort_callback & p_abort )
{
return ::get_stats_in_archive( p_archive, p_file, p_abort );
}
virtual void open_archive( service_ptr_t< file > & p_out, const char * p_archive, const char * p_file, abort_callback & p_abort )
{
::open_archive( p_out, p_archive, p_file, p_abort );
}
virtual void archive_list( const char * path, const service_ptr_t< file > & p_reader, archive_callback & p_out, bool p_want_readers )
{
::archive_list( this, get_archive_type(), path, p_reader, p_out, p_want_readers );
}
};
class unpacker_stuff : public unpacker
{
inline bool skip_ext( const char * p )
{
static const char * exts[] = { "txt", "nfo", "info", "diz" };
pfc::string_extension ext( p );
for ( unsigned n = 0; n < tabsize( exts ); ++n )
{
if ( ! stricmp_utf8( ext, exts[ n ] ) ) return true;
}
return false;
}
public:
virtual void open( service_ptr_t< file > & p_out, const service_ptr_t< file > & p_source, abort_callback & p_abort )
{
if ( p_source.is_empty() ) throw exception_io_data();
foobar_File_Reader in( p_source, p_abort );
std::unique_ptr<fex_t> ex( new Zip7_Extractor );
if ( ex->open( &in ) )
{
ex.reset( new Rar_Extractor );
in.seek( 0 );
}
handle_error( ex->open( &in ) );
while ( ! ex->done() )
{
handle_error( ex->stat() );
if ( ! skip_ext( ex->name() ) )
{
const void * data;
handle_error( ex->data( &data ) );
p_out = new service_impl_t<reader_membuffer_simple>( data, (t_size)ex->size(), p_source->get_timestamp( p_abort ), p_source->is_remote() );
return;
}
handle_error( ex->next() );
}
throw exception_io_data();
}
};
static archive_factory_t < archive_rsn > g_archive_rsn_factory;
static archive_factory_t < archive_vgm7z > g_archive_vgm7z_factory;
static unpacker_factory_t< unpacker_stuff > g_unpacker_7z_factory;
namespace b { DECLARE_FILE_TYPE("RSN files", "*.RSN"); }
namespace d { DECLARE_FILE_TYPE("VGM7Z files", "*.VGM7Z"); }