Line | Count | Source (jump to first uncovered line) |
1 | | /* file.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 | | 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 | | #pragma once |
29 | | |
30 | | /************************************************************************* |
31 | | // |
32 | | **************************************************************************/ |
33 | | |
34 | | class FileBase { |
35 | | protected: |
36 | 33.9k | explicit FileBase() noexcept = default; |
37 | | virtual ~FileBase() may_throw; |
38 | | |
39 | | public: |
40 | | bool close_noexcept() noexcept; |
41 | | void closex() may_throw; |
42 | 915k | bool isOpen() const noexcept { return _fd >= 0; } |
43 | 17.0k | int getFd() const noexcept { return _fd; } |
44 | 261 | const char *getName() const noexcept { return _name; } |
45 | | |
46 | | virtual upx_off_t seek(upx_off_t off, int whence); |
47 | | upx_off_t tell() const; |
48 | | virtual upx_off_t st_size() const; // { return _length; } |
49 | | virtual void set_extent(upx_off_t offset, upx_off_t length); |
50 | | |
51 | | public: |
52 | | // static file-related util functions; will throw on error |
53 | | static void chmod(const char *name, int mode) may_throw; |
54 | | static void rename(const char *old_, const char *new_) may_throw; |
55 | | static void unlink(const char *name) may_throw; |
56 | | static bool unlink_noexcept(const char *name) noexcept; |
57 | | |
58 | | protected: |
59 | | bool do_sopen(); |
60 | | int _fd = -1; |
61 | | int _flags = 0; |
62 | | int _shflags = 0; |
63 | | int _mode = 0; |
64 | | const char *_name = nullptr; |
65 | | upx_off_t _offset = 0; |
66 | | upx_off_t _length = 0; |
67 | | |
68 | | public: |
69 | | struct stat st = {}; |
70 | | }; |
71 | | |
72 | | /************************************************************************* |
73 | | // |
74 | | **************************************************************************/ |
75 | | |
76 | | class InputFile final : public FileBase { |
77 | | typedef FileBase super; |
78 | | |
79 | | public: |
80 | 16.9k | explicit InputFile() noexcept = default; |
81 | | |
82 | | void sopen(const char *name, int flags, int shflags); |
83 | 0 | void open(const char *name, int flags) { sopen(name, flags, -1); } |
84 | | |
85 | | int read(SPAN_P(void) buf, upx_int64_t blen); |
86 | | int readx(SPAN_P(void) buf, upx_int64_t blen); |
87 | | |
88 | | virtual upx_off_t seek(upx_off_t off, int whence) override; |
89 | | upx_off_t st_size_orig() const; |
90 | | |
91 | | noinline int dupFd() may_throw; |
92 | | |
93 | | protected: |
94 | | upx_off_t _length_orig = 0; |
95 | | }; |
96 | | |
97 | | /************************************************************************* |
98 | | // |
99 | | **************************************************************************/ |
100 | | |
101 | | class OutputFile final : public FileBase { |
102 | | typedef FileBase super; |
103 | | |
104 | | public: |
105 | 16.9k | explicit OutputFile() noexcept = default; |
106 | | |
107 | | void sopen(const char *name, int flags, int shflags, int mode); |
108 | 0 | void open(const char *name, int flags, int mode) { sopen(name, flags, -1, mode); } |
109 | | bool openStdout(int flags = 0, bool force = false); |
110 | | |
111 | | // info: allow nullptr if blen == 0 |
112 | | void write(SPAN_0(const void) buf, upx_int64_t blen); |
113 | | |
114 | | virtual upx_off_t seek(upx_off_t off, int whence) override; |
115 | | virtual upx_off_t st_size() const override; // { return _length; } |
116 | | virtual void set_extent(upx_off_t offset, upx_off_t length) override; |
117 | | upx_off_t unset_extent(); // returns actual length |
118 | | |
119 | 8.63k | upx_off_t getBytesWritten() const { return bytes_written; } |
120 | | |
121 | | // FIXME - this won't work when using the '--stdout' option |
122 | | void rewrite(SPAN_P(const void) buf, int len); |
123 | | |
124 | | // util |
125 | | static void dump(const char *name, SPAN_P(const void) buf, int len, int flags = -1); |
126 | | |
127 | | protected: |
128 | | upx_off_t bytes_written = 0; |
129 | | }; |
130 | | |
131 | | /* vim:set ts=4 sw=4 et: */ |