Upx_Doxygen
https://github.com/upx/upx
util.h
1 /* util.h --
2 
3  This file is part of the UPX executable compressor.
4 
5  Copyright (C) 1996-2016 Markus Franz Xaver Johannes Oberhumer
6  Copyright (C) 1996-2016 Laszlo Molnar
7  All Rights Reserved.
8 
9  UPX and the UCL library are free software; you can redistribute them
10  and/or modify them under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of
12  the License, or (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; see the file COPYING.
21  If not, write to the Free Software Foundation, Inc.,
22  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24  Markus F.X.J. Oberhumer Laszlo Molnar
25  <markus@oberhumer.com> <ezerotven+github@gmail.com>
26  */
27 
28 #ifndef __UPX_UTIL_H
29 #define __UPX_UTIL_H 1
30 
31 /*************************************************************************
32 // protect against integer overflows and malicious header fields
33 **************************************************************************/
34 
35 #define New(type, n) new type[mem_size_get_n(sizeof(type), n)]
36 
37 upx_rsize_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1 = 0,
38  upx_uint64_t extra2 = 0);
39 upx_rsize_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n);
40 
41 inline void mem_size_assert(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1 = 0,
42  upx_uint64_t extra2 = 0) {
43  (void) mem_size(element_size, n, extra1, extra2); // sanity check
44 }
45 
46 bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra1 = 0,
47  upx_uint64_t extra2 = 0);
48 bool mem_size_valid_bytes(upx_uint64_t bytes);
49 
50 int ptr_diff(const void *p1, const void *p2);
51 unsigned ptr_udiff(const void *p1, const void *p2); // asserts p1 >= p2
52 
53 /*************************************************************************
54 // misc. support functions
55 **************************************************************************/
56 
57 char *fn_basename(const char *name);
58 int fn_strcmp(const char *n1, const char *n2);
59 char *fn_strlwr(char *n);
60 bool fn_has_ext(const char *name, const char *ext, bool ignore_case = true);
61 
62 bool file_exists(const char *name);
63 bool maketempname(char *ofilename, size_t size, const char *ifilename, const char *ext,
64  bool force = true);
65 bool makebakname(char *ofilename, size_t size, const char *ifilename, bool force = true);
66 
67 unsigned get_ratio(upx_uint64_t u_len, upx_uint64_t c_len);
68 bool set_method_name(char *buf, size_t size, int method, int level);
69 void center_string(char *buf, size_t size, const char *s);
70 
71 int find(const void *b, int blen, const void *what, int wlen);
72 int find_be16(const void *b, int blen, unsigned what);
73 int find_be32(const void *b, int blen, unsigned what);
74 int find_be64(const void *b, int blen, upx_uint64_t what);
75 int find_le16(const void *b, int blen, unsigned what);
76 int find_le32(const void *b, int blen, unsigned what);
77 int find_le64(const void *b, int blen, upx_uint64_t what);
78 
79 int mem_replace(void *b, int blen, const void *what, int wlen, const void *r);
80 
81 #endif /* already included */
82 
83 /* vim:set ts=4 sw=4 et: */