Upx_Doxygen
https://github.com/upx/upx
file.h
1 /* file.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_FILE_H
30 #define __UPX_FILE_H 1
31 
32 class MemBuffer;
33 
34 
35 /*************************************************************************
36 //
37 **************************************************************************/
38 
39 class File
40 {
41 protected:
42  File() { }
43  virtual ~File() { }
44 public:
45  static void chmod(const char *name, int mode);
46  static void rename(const char *old_, const char *new_);
47  static void unlink(const char *name);
48 };
49 
50 
51 class FileBase : public File
52 {
53 protected:
54  FileBase();
55  virtual ~FileBase();
56 public:
57  virtual bool close();
58  virtual void closex();
59  virtual bool isOpen() const { return _fd >= 0; }
60  int getFd() const { return _fd; }
61  const char *getName() const { return _name; }
62  virtual off_t st_size() const; // { return _length; }
63  virtual void set_extent(off_t offset, off_t length);
64 
65 protected:
66  bool do_sopen();
67  virtual int read(void *buf, int len);
68  virtual int readx(void *buf, int len);
69  virtual void write(const void *buf, int len);
70  virtual off_t seek(upx_int64_t off, int whence);
71  virtual off_t tell() const;
72 
73  int _fd;
74  int _flags;
75  int _shflags;
76  int _mode;
77  const char *_name;
78  off_t _offset;
79  off_t _length;
80 public:
81  struct stat st;
82 };
83 
84 
85 /*************************************************************************
86 //
87 **************************************************************************/
88 
89 class InputFile : public FileBase
90 {
91  typedef FileBase super;
92 public:
93  InputFile();
94  virtual ~InputFile();
95 
96  virtual void sopen(const char *name, int flags, int shflags);
97  virtual void open(const char *name, int flags)
98  {
99  sopen(name, flags, -1);
100  }
101 
102  virtual int read(void *buf, int len);
103  virtual int readx(void *buf, int len);
104  virtual int read(MemBuffer *buf, int len);
105  virtual int readx(MemBuffer *buf, int len);
106  virtual int read(MemBuffer &buf, int len);
107  virtual int readx(MemBuffer &buf, int len);
108 
109  virtual off_t seek(upx_int64_t off, int whence);
110  virtual off_t tell() const;
111  virtual off_t st_size_orig() const;
112 protected:
113  off_t _length_orig;
114 };
115 
116 
117 /*************************************************************************
118 //
119 **************************************************************************/
120 
121 class OutputFile : public FileBase
122 {
123  typedef FileBase super;
124 public:
125  OutputFile();
126  virtual ~OutputFile();
127 
128  virtual void sopen(const char *name, int flags, int shflags, int mode);
129  virtual void open(const char *name, int flags, int mode)
130  {
131  sopen(name, flags, -1, mode);
132  }
133  virtual bool openStdout(int flags=0, bool force=false);
134 
135  virtual void write(const void *buf, int len);
136  virtual void write(const MemBuffer *buf, int len);
137  virtual void write(const MemBuffer &buf, int len);
138  virtual void set_extent(off_t offset, off_t length);
139  virtual off_t unset_extent(); // returns actual length
140 
141  off_t getBytesWritten() const { return bytes_written; }
142  virtual off_t st_size() const; // { return _length; }
143 
144  // FIXME - these won't work when using the '--stdout' option
145  virtual off_t seek(upx_int64_t off, int whence);
146  virtual void rewrite(const void *buf, int len);
147 
148  // util
149  static void dump(const char *name, const void *buf, int len, int flags=-1);
150 
151 protected:
152  off_t bytes_written;
153 };
154 
155 
156 /*************************************************************************
157 //
158 **************************************************************************/
159 
160 #if 0 /* NOT USED */
161 class MemoryOutputFile : public FileBase
162 {
163  typedef FileBase super;
164 public:
165  MemoryOutputFile();
166  virtual ~MemoryOutputFile() { b = NULL; }
167 
168  virtual bool close() { b = NULL; return true; }
169  virtual bool isOpen() const { return b != NULL; }
170  virtual void open(void *buf, unsigned size)
171  { b = (upx_bytep) buf; b_size = size; }
172 
173  virtual void write(const void *buf, int len);
174 
175  off_t getBytesWritten() const { return bytes_written; }
176 
177 protected:
178  upx_bytep b;
179  unsigned b_size;
180  unsigned b_pos;
181  off_t bytes_written;
182 };
183 #endif /* if 0 */
184 
185 
186 #endif /* already included */
187 
188 /* vim:set ts=4 sw=4 et: */
Definition: mem.h:37
Definition: file.h:51
Definition: file.h:39
Definition: file.h:89
Definition: file.h:121