Upx_Doxygen
https://github.com/upx/upx
p_unix.h
1 /* p_unix.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_P_UNIX_H
30 #define __UPX_P_UNIX_H 1
31 
32 
33 /*************************************************************************
34 // Abstract class for all Unix-type packers.
35 // Already provides most of the functionality.
36 **************************************************************************/
37 
38 class PackUnix : public Packer
39 {
40  typedef Packer super;
41 protected:
42  PackUnix(InputFile *f);
43 public:
44  virtual int getVersion() const { return 13; }
45  virtual const int *getFilters() const { return NULL; }
46  virtual int getStrategy(Filter &);
47 
48  virtual void pack(OutputFile *fo);
49  virtual void unpack(OutputFile *fo);
50 
51  virtual bool canPack();
52  virtual int canUnpack();
53 
54 protected:
55  // called by the generic pack()
56  virtual void pack1(OutputFile *, Filter &); // generate executable header
57  virtual int pack2(OutputFile *, Filter &); // append compressed data
58  virtual void pack3(OutputFile *, Filter &); // append loader
59  virtual void pack4(OutputFile *, Filter &); // append PackHeader
60 
61  virtual void patchLoader() = 0;
62  virtual void patchLoaderChecksum();
63  virtual void updateLoader(OutputFile *) = 0;
64 
65  virtual void writePackHeader(OutputFile *fo);
66 
67  virtual bool checkCompressionRatio(unsigned, unsigned) const;
68 
69 protected:
70  struct Extent {
71  off_t offset;
72  off_t size;
73  };
74  virtual void packExtent(const Extent &x,
75  unsigned &total_in, unsigned &total_out, Filter *, OutputFile *,
76  unsigned hdr_len = 0);
77  virtual void unpackExtent(unsigned wanted, OutputFile *fo,
78  unsigned &total_in, unsigned &total_out,
79  unsigned &c_adler, unsigned &u_adler,
80  bool first_PF_X, unsigned szb_info );
81 
82  int exetype;
83  unsigned blocksize;
84  unsigned progid; // program id
85  unsigned overlay_offset; // used when decompressing
86 
87  MemBuffer loader;
88  int lsize;
89  MemBuffer pt_dynamic;
90  int sz_dynamic;
91 
92  unsigned b_len; // total length of b_info blocks
93 
94  // must agree with stub/linux.hh
95  __packed_struct(b_info) // 12-byte header before each compressed block
96  unsigned sz_unc; // uncompressed_size
97  unsigned sz_cpr; // compressed_size
98  unsigned char b_method; // compression algorithm
99  unsigned char b_ftid; // filter id
100  unsigned char b_cto8; // filter parameter
101  unsigned char b_unused;
102  __packed_struct_end()
103 
104  __packed_struct(l_info) // 12-byte trailer in header for loader
105  LE32 l_checksum;
106  LE32 l_magic;
107  LE16 l_lsize;
108  unsigned char l_version;
109  unsigned char l_format;
110  __packed_struct_end()
111 
112  __packed_struct(p_info) // 12-byte packed program header
113  unsigned p_progid;
114  unsigned p_filesize;
115  unsigned p_blocksize;
116  __packed_struct_end()
117 
118  struct l_info linfo;
119 
120  // do not change !!!
121  enum { OVERHEAD = 2048 };
122 };
123 
124 
125 /*************************************************************************
126 // abstract classes encapsulating endian issues
127 // note: UPX_MAGIC is always stored in le32 format
128 **************************************************************************/
129 class PackUnixBe32 : public PackUnix
130 {
131  typedef PackUnix super;
132 protected:
133  PackUnixBe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; }
134 
135  // must agree with stub/linux.hh
136  __packed_struct(b_info) // 12-byte header before each compressed block
137  BE32 sz_unc; // uncompressed_size
138  BE32 sz_cpr; // compressed_size
139  unsigned char b_method; // compression algorithm
140  unsigned char b_ftid; // filter id
141  unsigned char b_cto8; // filter parameter
142  unsigned char b_unused;
143  __packed_struct_end()
144 
145  __packed_struct(l_info) // 12-byte trailer in header for loader
146  BE32 l_checksum;
147  BE32 l_magic;
148  BE16 l_lsize;
149  unsigned char l_version;
150  unsigned char l_format;
151  __packed_struct_end()
152 
153  __packed_struct(p_info) // 12-byte packed program header
154  BE32 p_progid;
155  BE32 p_filesize;
156  BE32 p_blocksize;
157  __packed_struct_end()
158 };
159 
160 
161 class PackUnixLe32 : public PackUnix
162 {
163  typedef PackUnix super;
164 protected:
165  PackUnixLe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; }
166 
167  // must agree with stub/linux.hh
168  __packed_struct(b_info) // 12-byte header before each compressed block
169  LE32 sz_unc; // uncompressed_size
170  LE32 sz_cpr; // compressed_size
171  unsigned char b_method; // compression algorithm
172  unsigned char b_ftid; // filter id
173  unsigned char b_cto8; // filter parameter
174  unsigned char b_unused;
175  __packed_struct_end()
176 
177  __packed_struct(l_info) // 12-byte trailer in header for loader
178  LE32 l_checksum;
179  LE32 l_magic;
180  LE16 l_lsize;
181  unsigned char l_version;
182  unsigned char l_format;
183  __packed_struct_end()
184 
185  __packed_struct(p_info) // 12-byte packed program header
186  LE32 p_progid;
187  LE32 p_filesize;
188  LE32 p_blocksize;
189  __packed_struct_end()
190 };
191 
192 
193 /*************************************************************************
194 // solaris/sparc
195 **************************************************************************/
196 
197 #if 0
198 class PackSolarisSparc : public PackUnixBe32
199 {
200  typedef PackUnixBe32 super;
201 public:
202  PackSolarisSparc(InputFile *f) : super(f) { }
203  virtual int getFormat() const { return UPX_F_SOLARIS_SPARC; }
204  virtual const char *getName() const { return "solaris/sparc"; }
205 
206  virtual bool canPack();
207 
208 protected:
209  virtual upx_byte *getLoader() const;
210  virtual int getLoaderSize() const;
211 
212  virtual void patchLoader();
213 };
214 #endif /* #if 0 */
215 
216 
217 #endif /* already included */
218 
219 /* vim:set ts=4 sw=4 et: */
Definition: mem.h:37
Definition: p_unix.h:70
Definition: p_unix.h:38
Definition: file.h:89
Definition: packer.h:115
Definition: p_unix.h:129
Definition: file.h:121
Definition: p_unix.h:161
Definition: filter.h:53