Upx_Doxygen
https://github.com/upx/upx
filter.h
1 /* filter.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 
29 #ifndef __UPX_FILTER_H
30 #define __UPX_FILTER_H 1
31 
32 class Filter;
33 class FilterImp;
34 
35 
36 /*************************************************************************
37 // A filter is a reversible operation that modifies a given
38 // block of memory.
39 //
40 // A filter can fail and return false. In this case the buffer
41 // must be unmodified (or otherwise restored).
42 //
43 // If a filter fails and somehow cannot restore the block it must
44 // call throwFilterException() - this will cause the compression
45 // to fail.
46 //
47 // Unfilters throw exceptions in case of errors.
48 //
49 // The main idea behind filters is to convert relative jumps and calls
50 // to absolute addresses so that the buffer compresses better.
51 **************************************************************************/
52 
53 class Filter
54 {
55 public:
56  Filter(int level) { clevel = level; init(); }
57  void init(int id=0, unsigned addvalue=0);
58 
59  bool filter(upx_byte *buf, unsigned buf_len);
60  void unfilter(upx_byte *buf, unsigned buf_len, bool verify_checksum=false);
61  void verifyUnfilter();
62  bool scan(const upx_byte *buf, unsigned buf_len);
63 
64  static bool isValidFilter(int filter_id);
65  static bool isValidFilter(int filter_id, const int *allowed_filters);
66 
67 public:
68  // Will be set by each call to filter()/unfilter().
69  // Read-only afterwards.
70  upx_byte *buf;
71  unsigned buf_len;
72 
73  // Checksum of the buffer before applying the filter
74  // or after un-applying the filter.
75  unsigned adler;
76 
77  // Input parameters used by various filters.
78  unsigned addvalue;
79  const int *preferred_ctos;
80 
81  // Input/output parameters used by various filters
82  unsigned char cto; // call trick offset
83 
84  // Output used by various filters. Read only.
85  unsigned calls;
86  unsigned noncalls;
87  unsigned wrongcalls;
88  unsigned firstcall;
89  unsigned lastcall;
90  unsigned n_mru; // ctojr only
91 
92  // Read only.
93  int id;
94 
95 private:
96  int clevel; // compression level
97 };
98 
99 
100 /*************************************************************************
101 // We don't want a full OO interface here because of
102 // certain implementation speed reasons.
103 //
104 // This class is private to Filter - don't look.
105 **************************************************************************/
106 
108 {
109  friend class Filter;
110 
111 private:
112  struct FilterEntry
113  {
114  int id; // 0 .. 255
115  unsigned min_buf_len;
116  unsigned max_buf_len;
117  int (*do_filter)(Filter *); // filter a buffer
118  int (*do_unfilter)(Filter *); // unfilter a buffer
119  int (*do_scan)(Filter *); // scan a buffer
120  };
121 
122  // get a specific filter entry
123  static const FilterEntry *getFilter(int id);
124 
125 private:
126  // strictly private filter database
127  static const FilterEntry filters[];
128  static const int n_filters; // number of filters[]
129 };
130 
131 
132 #endif /* already included */
133 
134 /* vim:set ts=4 sw=4 et: */
Definition: filter.h:107
Definition: filter.h:53