Coverage Report

Created: 2025-10-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/upx/src/p_unix.h
Line
Count
Source
1
/* p_unix.h --
2
3
   This file is part of the UPX executable compressor.
4
5
   Copyright (C) 1996-2025 Markus Franz Xaver Johannes Oberhumer
6
   Copyright (C) 1996-2025 Laszlo Molnar
7
   Copyright (C) 2000-2025 John F. Reiser
8
   All Rights Reserved.
9
10
   UPX and the UCL library are free software; you can redistribute them
11
   and/or modify them under the terms of the GNU General Public License as
12
   published by the Free Software Foundation; either version 2 of
13
   the License, or (at your option) any later version.
14
15
   This program is distributed in the hope that it will be useful,
16
   but WITHOUT ANY WARRANTY; without even the implied warranty of
17
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
   GNU General Public License for more details.
19
20
   You should have received a copy of the GNU General Public License
21
   along with this program; see the file COPYING.
22
   If not, write to the Free Software Foundation, Inc.,
23
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25
   Markus F.X.J. Oberhumer              Laszlo Molnar
26
   <markus@oberhumer.com>               <ezerotven+github@gmail.com>
27
28
   John F. Reiser
29
   <jreiser@users.sourceforge.net>
30
 */
31
32
33
#pragma once
34
#ifndef __UPX_P_UNIX_H
35
#define __UPX_P_UNIX_H 1
36
37
38
/*************************************************************************
39
// Abstract class for all Unix-type packers.
40
// Already provides most of the functionality.
41
**************************************************************************/
42
43
class PackUnix : public Packer
44
{
45
public:
46
    ~PackUnix();
47
    typedef Packer super;
48
protected:
49
    PackUnix(InputFile *f);
50
public:
51
34.0k
    virtual int getVersion() const override { return 13; }
52
0
    virtual const int *getFilters() const override { return nullptr; }
53
    virtual int getStrategy(Filter &);
54
55
    virtual void pack(OutputFile *fo) override;
56
    virtual void unpack(OutputFile *fo) override;
57
58
    virtual tribool canPack() override;
59
    virtual tribool canUnpack() override; // bool, except -1: format known, but not packed
60
    int find_overlay_offset(MemBuffer const &buf);
61
62
protected:
63
    // called by the generic pack()
64
    virtual void pack1(OutputFile *, Filter &);  // generate executable header
65
    virtual int  pack2(OutputFile *, Filter &);  // append compressed data
66
    virtual off_t pack3(OutputFile *, Filter &);  // append loader
67
    virtual void pack4(OutputFile *, Filter &);  // append PackHeader
68
69
    virtual void patchLoader() = 0;
70
    virtual void patchLoaderChecksum();
71
    virtual void updateLoader(OutputFile *) = 0;
72
73
    virtual void writePackHeader(OutputFile *fo);
74
75
    virtual bool checkCompressionRatio(unsigned, unsigned) const override;
76
77
protected:
78
    struct Extent {
79
        upx_off_t offset;
80
        upx_off_t size;
81
    };
82
    virtual void packExtent(const Extent &x,
83
        Filter *, OutputFile *,
84
        unsigned hdr_len = 0, unsigned b_extra = 0 ,
85
        bool inhibit_compression_check = false);
86
    virtual unsigned unpackExtent(unsigned wanted, OutputFile *fo,
87
        unsigned &c_adler, unsigned &u_adler,
88
        bool first_PF_X,
89
        int is_rewrite = false  // 0(false): write; 1(true): rewrite; -1: no write
90
        );
91
    unsigned total_in, total_out;  // unpack
92
93
    int exetype;  // 0: unknown; 1: ELF; 2: pre-ELF; -1: /bin/sh; -2: Java
94
    unsigned blocksize;
95
    unsigned progid;              // program id
96
    unsigned overlay_offset;      // used when decompressing
97
98
    MemBuffer loader;
99
    int lsize;
100
    MemBuffer pt_dynamic;
101
    int sz_dynamic;
102
103
    unsigned b_len;  // total length of b_info blocks  FIXME: unused
104
    unsigned methods_used;  // bitmask of compression methods
105
    unsigned szb_info;  // 3*4 (sizeof b_info); or 2*4 if ancient
106
    unsigned saved_opt_android_shlib;
107
108
    // must agree with stub/linux.hh
109
    packed_struct(b_info) { // 12-byte header before each compressed block
110
        NE32 sz_unc;  // uncompressed_size
111
        NE32 sz_cpr;  //   compressed_size
112
        unsigned char b_method;  // compression algorithm
113
        unsigned char b_ftid;  // filter id
114
        unsigned char b_cto8;  // filter parameter
115
        unsigned char b_extra;
116
    };
117
118
    packed_struct(l_info) { // 12-byte trailer in header for loader
119
        LE32 l_checksum;
120
        LE32 l_magic;
121
        LE16 l_lsize;
122
        unsigned char l_version;
123
        unsigned char l_format;
124
    };
125
126
    packed_struct(p_info) { // 12-byte packed program header
127
        NE32 p_progid;
128
        NE32 p_filesize;
129
        NE32 p_blocksize;
130
    };
131
132
    struct l_info linfo;
133
134
    // do not change !!!
135
    enum { OVERHEAD = 2048 };
136
};
137
138
139
/*************************************************************************
140
// abstract classes encapsulating endian issues
141
// note: UPX_MAGIC is always stored in le32 format
142
**************************************************************************/
143
144
class PackUnixBe32 : public PackUnix
145
{
146
    typedef PackUnix super;
147
protected:
148
0
    PackUnixBe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::be_policy; }
149
150
    // must agree with stub/linux.hh
151
    packed_struct(b_info) { // 12-byte header before each compressed block
152
        BE32 sz_unc;  // uncompressed_size
153
        BE32 sz_cpr;  //   compressed_size
154
        unsigned char b_method;  // compression algorithm
155
        unsigned char b_ftid;  // filter id
156
        unsigned char b_cto8;  // filter parameter
157
        unsigned char b_extra;
158
    };
159
160
    packed_struct(l_info) { // 12-byte trailer in header for loader
161
        BE32 l_checksum;
162
        BE32 l_magic;
163
        BE16 l_lsize;
164
        unsigned char l_version;
165
        unsigned char l_format;
166
    };
167
168
    packed_struct(p_info)  {// 12-byte packed program header
169
        BE32 p_progid;
170
        BE32 p_filesize;
171
        BE32 p_blocksize;
172
    };
173
};
174
175
176
class PackUnixLe32 : public PackUnix
177
{
178
    typedef PackUnix super;
179
protected:
180
42.9k
    PackUnixLe32(InputFile *f) : super(f) { bele = &N_BELE_RTP::le_policy; }
181
182
    // must agree with stub/linux.hh
183
    packed_struct(b_info) { // 12-byte header before each compressed block
184
        LE32 sz_unc;  // uncompressed_size
185
        LE32 sz_cpr;  //   compressed_size
186
        unsigned char b_method;  // compression algorithm
187
        unsigned char b_ftid;  // filter id
188
        unsigned char b_cto8;  // filter parameter
189
        unsigned char b_extra;
190
    };
191
192
    packed_struct(l_info) { // 12-byte trailer in header for loader
193
        LE32 l_checksum;
194
        LE32 l_magic;
195
        LE16 l_lsize;
196
        unsigned char l_version;
197
        unsigned char l_format;
198
    };
199
200
    packed_struct(p_info) { // 12-byte packed program header
201
        LE32 p_progid;
202
        LE32 p_filesize;
203
        LE32 p_blocksize;
204
    };
205
};
206
207
208
#endif /* already included */
209
210
/* vim:set ts=4 sw=4 et: */