-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibAPH.cc
635 lines (534 loc) · 15.2 KB
/
libAPH.cc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
* Copyright (C) 2008 Piotr Rak <[email protected]>
*
* Partly Copyright (C) 2001 Geert Bevin, Uwyn, http://www.uwyn.com
* Partly Copyright (C) 2002 Martin Schlemmer <[email protected]>
* Partly Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <[email protected]>,
* as some of Sandbox (http://www.gentoo.org/) code was used
*
*
* This file is part of the libAPH. libAPH is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
* Public License version 2, as published by the Free Software Foundation.
*
* libAPH 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, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define open xxx_open
#include <dlfcn.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#undef open
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stdexcept>
#include <pcre.h>
#define LIBC_VERSION "libc-2.6.1.so" ///< libc version
#define symname_open "open" ///< libc open() symbol name
#define symver_open NULL ///< libc open() symbol version
#define LIBAPH_NAME "libAPH: " ///< prefix string for lib output
/**
* Library environment variable.
* If set location where to store files, may be used to override $HOME/.auteria.
*/
#define LIBAPH_CONFIG_LOCATION_ENVVAR "LIBAPH_CONFIG_LOCATION"
/**
* Library environment variable.
* If set to "1", library will give additional runtime info
*/
#define LIBAPH_VERBOSE_ENVVAR "LIBAPH_VERBOSE"
/**
* Handful macro for lazy loading symbols from libc
*/
#define check_sym(_name) \
do { \
int old_errno = errno; \
if(NULL == true_ ## _name) \
true_ ## _name = reinterpret_cast<typeof(true_ ## _name)>(get_dlsym(symname_ ##_name, symver_ ## _name)); \
errno = old_errno; \
} while(false);
/**
* Returns ostream for verbose messages.
*/
static std::ostream&
verbose_out() __attribute__((warn_unused_result));
/**
* Simple posix positional syntax string formatting.
*/
static std::ostream&
format(std::ostream&, const std::string&, const std::vector<std::string>&);
/**
* Returns environment variable or default value if unset.
*/
static std::string
getenv_with_default(const std::string&, const std::string&);
/**
* Adds slash at the end of string.
*/
static void
add_trailing_slash(std::string&);
/**
* Returns configuration files store location.
*/
static std::string
get_config_files_location();
/**
* Load function symbol from libc.
*/
static
void* get_dlsym(const char* name, const char *version);
/**
* Tha Queen, open() wrapper.
*/
extern "C" __attribute__ ((visibility("default"))) int open(char * pathname, int flags, ...);
/**
* NonCopyable object idiom.
*/
template <typename Ty_>
class NonCopyable
{
protected:
NonCopyable() {}
private:
NonCopyable(const Ty_&); //= delete
const NonCopyable& operator= (const Ty_&); //= delete
};
/**
* Represents path substition.
*/
struct Substition :
NonCopyable<Substition>
{
/**
* Constructor.
*/
Substition(const char*, const char*);
/**
* Destructor.
*/
~Substition();
/**
* Returns weather match for given path occured.
*/
bool matches(const std::string&) const;
/**
* Deos substiution of recently matched path.
* If none matched yet, returns NULL.
*/
char* substitute() __attribute__((warn_unused_result));
/**
* Clears last match.
*/
void clear_match() const;
private:
/**
*Returns weather regular exression was compiled already.
*/
bool is_compiled() const;
/**
* Compiles regular expression.
*/
void compile() const;
/* Note: All mutable fields of this object should be protected from races,
* if open() might be called from different threads,
* altough research doesn't show it is issue ATM...
*/
const char* _re_str; ///< Regular expression string
const std::string _format; ///< Format string use for substiution (uses posix positional syntax)
mutable pcre* _re; ///< Compiled regular exression
mutable pcre_extra* _pe; ///< Addition information for compiled regexp
mutable int _substr_count; ///< Substring count in match
mutable std::string _last_match; ///< Last matched path
mutable int* _outvec; ///< Submatches indecies vector
#if 0
mutex_t mutex; //protecting all mutable fields probably might be sane...
#endif
};
/**
* NoOpOutputStream buffer implementation.
*/
class NoOpOutputStreamBuf :
public std::streambuf
{
protected:
virtual int_type
overflow(int_type c)
{
// always success write single not EOF char
return c;
}
virtual std::streamsize
xsputn(char *, std::streamsize s)
{
//do nothing, always success write s chars
return s;
}
};
/**
* Member from base initialization for NoOpOutputStream.
*/
class NoOpOutputStreamBase
{
protected:
NoOpOutputStreamBuf buf;
};
/**
* Output stream that does nothing (kind of /dev/null output).
*/
class NoOpOutputStream :
protected NoOpOutputStreamBase,
public std::ostream
{
public:
/**
* Constructor.
*/
NoOpOutputStream() :
NoOpOutputStreamBase(),
std::ostream(&buf)
{
}
};
/*
* To get explanation of NoOpOutputStream please refer to chapter 13.13.3 of "The C++ Standard Library"
* Nicolai M. Josuttis / Addison-Wesley / ISBN 0-201-37926-0
*/
// GLOBALS
static void* libc_handle = NULL; ///< handle to dynloaded libc.so
static int (*true_open) (char *, int flags, ...) = NULL; ///< pointer to true open() from libc
/**
* All path substiutions.
*/
static Substition SUBSTITUTE_PATHS[] = {
Substition("game/client/(config\\.cs.*)$", "%1%"), //config.cs
Substition("game/client/(prefs\\.cs.*)$", "%1%"), //prefs.cs
Substition("console.log", "console.log"), //console.log
Substition("log/([^/]+)/(.+\\.qlog)", "%1%@%2%"), // player/Map.qlog -> [email protected]
Substition("log/([^/]+)/(questlog\\.cs.*)", "%1%@%2%"), // player/qestlog.cs -> [email protected]
Substition("log/([^/]+)/chat.log", "%1%@chat.log"), // player/chat.log -> [email protected]
Substition("(screenshot_.+)", "%1%")
};
// IMPLEMENATION
static std::ostream&
format(std::ostream& os, const std::string& frmt, const std::vector<std::string>& args_array)
{
std::vector<std::string>::size_type args_count = args_array.size();
std::string::size_type i(0), eaten(0);
for(; std::string::npos != (i = frmt.find("%", i)); ++i)
{
if (frmt.size() == i+1) //we have found not matching '%' at end
throw std::logic_error(
std::string("Invalid format string `") + frmt + "'");
if ('%'==frmt[++i]) //using escape "%%"
{
os << frmt.substr(eaten, i-1-eaten);
eaten = i;
continue;
}
if (eaten != i)
{
os << frmt.substr(eaten, i-1-eaten);
eaten = i-1;
}
std::size_t arg_num(0);
for (; frmt.size()>i ; ++i) //parse arg number
if (! std::isdigit(frmt[i]))
break;
else
{
arg_num *= 10;
arg_num += frmt[i] - '0';
}
if (0 >= arg_num || args_count < arg_num) // arg out of range
throw std::logic_error(
std::string("Format argument number out of range"));
if ('%' != frmt[i]) // not %[0..9]*%
throw std::logic_error(
std::string("Invalid format string `") + frmt + "'");
os << args_array[arg_num-1];
eaten = i+1;
}
if (eaten < frmt.size())
os << frmt.substr(eaten);
return os;
}
static std::string
getenv_with_default(const std::string& env_var, const std::string& value)
{
char * s(getenv(env_var.c_str()));
if (! s)
return value;
std::string result(s);
if (result.empty())
return value;
return result;
}
static void
add_trailing_slash(std::string& s)
{
if (s.size() > 0 &&
s[s.size()-1] != '/')
s += "/";
}
static std::string
get_config_files_location()
{
std::string location = getenv_with_default(LIBAPH_CONFIG_LOCATION_ENVVAR, "");
if ( location.empty())
{
location = getenv_with_default("HOME", "");
if (location.empty())
{
std::cerr << LIBAPH_NAME "Unable to get config files location.\n"
LIBAPH_NAME "REASON: $HOME environment variable is not set!" << std::endl;
exit(EXIT_FAILURE);
}
location += "/.auteria";
}
//XXX: canonicalize_file_name() glibc extension, realpath is br00ken tho...
std::string canon_loc = canonicalize_file_name(location.c_str());
add_trailing_slash(canon_loc);
verbose_out() << LIBAPH_NAME "Will use location: `" << canon_loc << "'" << std::endl;
return canon_loc;
}
static void*
get_dlsym(const char* name, const char *version)
{
void * sym_addr = NULL;
if (NULL == libc_handle)
{
libc_handle = dlopen(LIBC_VERSION, RTLD_LAZY);
if (! libc_handle)
{
std::cerr << LIBAPH_NAME "Unable load libc!\n"
<< LIBAPH_NAME "\t" << dlerror() << std::endl;
exit(EXIT_FAILURE);
}
}
if (NULL == version)
sym_addr = dlsym(libc_handle, name);
else
sym_addr = reinterpret_cast<void* >(dlvsym(libc_handle, name, version));
if (! sym_addr)
{
std::cerr << LIBAPH_NAME "Can't resolve symbol: `" << name << "'\n"
<< LIBAPH_NAME "\t" << dlerror() << std::endl;
exit(EXIT_FAILURE);
}
return sym_addr;
}
Substition
::Substition(const char* re, const char* f) :
_re_str(re),
_format(f),
_re(),
_pe(),
_substr_count(),
_last_match(),
_outvec()
{
}
Substition
::~Substition()
{
if (NULL != _re)
(*pcre_free)(_re);
if (NULL != _pe)
(*pcre_free)(_pe);
delete[] _outvec;
_outvec = 0;
}
//XXX: not thread safe...
bool
Substition
::matches(const std::string& pathname) const
{
if (! is_compiled())
compile();
clear_match();
//XXX: delete & new of _outvec seq
_outvec = new int[3*_substr_count];
int match = pcre_exec(
/*regexp*/ _re,
/*study*/ _pe,
/*subject*/ pathname.c_str(),
/*subject len*/ pathname.size(),
/*offset*/ 0,
/*options*/ 0,
/*matches*/ _outvec,
/*matches len*/ 3*_substr_count
);
if (PCRE_ERROR_NOMATCH == match)
return false;
if (0 > match)
{
std::cerr << LIBAPH_NAME "Error matching `" << pathname << "'"
<< " with regexp `" << _re_str << "'" <<std::endl;
exit(EXIT_FAILURE);
}
_last_match = pathname;
return true;
}
//XXX: again not thread safe...
char*
Substition
::substitute()
{
static std::string location = get_config_files_location();
char * new_path = 0;
std::vector<std::string> strings;
std::ostringstream os;
if (_last_match.empty())
{
std::cerr << LIBAPH_NAME "No match, huh!?" << std::endl;
return NULL;
}
if (_substr_count > 1)
{
const char** substr_list;
strings.reserve(_substr_count);
pcre_get_substring_list(_last_match.c_str(), _outvec, _substr_count, &substr_list);
for (int i=1; i < _substr_count; ++i) // substr_list[0] is whole match
{
strings.push_back(std::string(substr_list[i]));
}
pcre_free_substring_list(substr_list);
}
try
{
::format(os, this->_format, strings);
verbose_out() << LIBAPH_NAME "s/`" << _last_match << "'/`" << os.str() << "'/"
<< std::endl;
std::string p = location + os.str();
new_path = new char[p.size()+1];
strcpy(new_path, p.c_str());
}
catch (std::exception& e)
{
std::cerr << LIBAPH_NAME "Error formatting `" << _format << "'\n"
<< LIBAPH_NAME "\t" << e.what() << std::endl;
exit(EXIT_FAILURE);
}
return new_path;
}
//XXX: and again not thread safe...
void
Substition
::clear_match() const
{
_last_match = "";
delete[] _outvec;
_outvec = 0;
}
//XXX: not thread safe too...
bool
Substition
::is_compiled() const
{
return NULL != _re;
}
//XXX: not thread safe too...
void
Substition
::compile() const
{
const char* error;
int error_offset;
int substr_count;
pcre* re = pcre_compile(this->_re_str, 0, &error, &error_offset, NULL);
if (NULL == re)
{
std::cerr << LIBAPH_NAME "Failed pcre_compile() with regexp `" << _re_str << "'\n"
<< LIBAPH_NAME "\t" << error << std::endl;
exit(EXIT_FAILURE);
}
error = NULL;
pcre_extra* pe = pcre_study(re, 0, &error);
if (NULL != error)
{
std::cerr << LIBAPH_NAME "Failed pcre_study() with regexp `" << _re_str << "'\n"
<< LIBAPH_NAME "\t" << error << std::endl;
exit(EXIT_FAILURE);
}
pcre_fullinfo(re, pe, PCRE_INFO_CAPTURECOUNT, &substr_count);
++substr_count;
this->_re = re;
this->_pe = pe;
this->_substr_count = substr_count;
}
inline static char*
substitute_path_if_needed(std::string pathname)
{
static const unsigned SUBST_PATHS_LEN = sizeof(SUBSTITUTE_PATHS) / sizeof(Substition);
for (unsigned i=0; i < SUBST_PATHS_LEN; ++i)
{
Substition* s = SUBSTITUTE_PATHS + i;
if (s->matches(pathname))
{
return s->substitute();
}
}
return NULL;
}
static std::ostream&
verbose_out()
{
static NoOpOutputStream noop_stream;
static const bool verbose_output("1" == getenv_with_default(LIBAPH_VERBOSE_ENVVAR, "0"));
if (verbose_output)
return std::cout;
else
return noop_stream;
}
extern "C" int
open(char * pathname, int flags, ...)
{
va_list ap;
int mode = 0;
int result = -1;
char* new_pathname;
if (flags & O_CREAT)
{
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
verbose_out() << LIBAPH_NAME "open(`" << pathname <<"', ...)" << std::endl;
try
{
new_pathname = substitute_path_if_needed(pathname);
}
catch (std::exception& e) // memory allocation failure might be
{
std::cerr << LIBAPH_NAME "Unhandled exception: "
<< LIBAPH_NAME "\t" << e.what() << std::endl;
exit(EXIT_FAILURE);
}
catch (...) // just in case
{
std::cerr << LIBAPH_NAME "Unknown unhandled exception" << std::endl;
exit(EXIT_FAILURE);
}
if (NULL != new_pathname)
pathname = new_pathname;
check_sym(open);
if (flags & O_CREAT)
result = true_open(pathname, flags, mode);
else
result = true_open(pathname, flags);
delete[] new_pathname;
return result;
}