1
0
mirror of https://github.com/upx/upx synced 2025-09-28 19:06:07 +08:00

FileBase::set_extent(offset, length) constrains seek() and tell()

This commit is contained in:
John Reiser 2007-02-03 13:31:44 -08:00
parent d1e352c9ee
commit a545a3938f
3 changed files with 40 additions and 7 deletions

View File

@ -69,7 +69,7 @@ void File::unlink(const char *name)
**************************************************************************/ **************************************************************************/
FileBase::FileBase() : FileBase::FileBase() :
_fd(-1), _flags(0), _shflags(0), _mode(0), _name(NULL) _fd(-1), _flags(0), _shflags(0), _mode(0), _name(NULL), _offset(0), _length(0)
{ {
memset(&st,0,sizeof(st)); memset(&st,0,sizeof(st));
} }
@ -104,6 +104,8 @@ void FileBase::sopen()
assert(0); assert(0);
#endif #endif
} }
::fstat(_fd, &st);
_length = st.st_size;
} }
@ -117,6 +119,8 @@ bool FileBase::close()
_flags = 0; _flags = 0;
_mode = 0; _mode = 0;
_name = NULL; _name = NULL;
_offset = 0;
_length = 0;
return ok; return ok;
} }
@ -164,10 +168,17 @@ void FileBase::seek(off_t off, int whence)
{ {
if (!isOpen()) if (!isOpen())
throwIOException("bad seek 1"); throwIOException("bad seek 1");
if (whence == SEEK_SET && off < 0) if (whence == SEEK_SET) {
throwIOException("bad seek 2"); if (off < 0)
if (whence == SEEK_END && off > 0) throwIOException("bad seek 2");
throwIOException("bad seek 3"); off += _offset;
}
if (whence == SEEK_END) {
if (off > 0)
throwIOException("bad seek 3");
off += _length;
whence = SEEK_SET;
}
if (::lseek(_fd,off,whence) < 0) if (::lseek(_fd,off,whence) < 0)
throwIOException("seek error",errno); throwIOException("seek error",errno);
} }
@ -180,7 +191,19 @@ off_t FileBase::tell() const
off_t l = ::lseek(_fd, 0, SEEK_CUR); off_t l = ::lseek(_fd, 0, SEEK_CUR);
if (l < 0) if (l < 0)
throwIOException("tell error",errno); throwIOException("tell error",errno);
return l; return l - _offset;
}
void FileBase::set_extent(off_t offset, off_t length)
{
_offset = offset;
_length = length;
}
off_t FileBase::st_size() const
{
return _length;
} }
@ -205,6 +228,8 @@ void InputFile::sopen(const char *name, int flags, int shflags)
_flags = flags; _flags = flags;
_shflags = shflags; _shflags = shflags;
_mode = 0; _mode = 0;
_offset = 0;
_length = 0;
FileBase::sopen(); FileBase::sopen();
if (!isOpen()) if (!isOpen())
{ {
@ -289,6 +314,8 @@ void OutputFile::sopen(const char *name, int flags, int shflags, int mode)
_flags = flags; _flags = flags;
_shflags = shflags; _shflags = shflags;
_mode = mode; _mode = mode;
_offset = 0;
_length = 0;
FileBase::sopen(); FileBase::sopen();
if (!isOpen()) if (!isOpen())
{ {
@ -316,6 +343,8 @@ bool OutputFile::openStdout(int flags, bool force)
_flags = flags; _flags = flags;
_shflags = -1; _shflags = -1;
_mode = 0; _mode = 0;
_offset = 0;
_length = 0;
if (flags && acc_set_binmode(fd, 1) == -1) if (flags && acc_set_binmode(fd, 1) == -1)
throwIOException(_name, errno); throwIOException(_name, errno);
_fd = fd; _fd = fd;

View File

@ -59,6 +59,7 @@ public:
virtual bool isOpen() const { return _fd >= 0; } virtual bool isOpen() const { return _fd >= 0; }
int getFd() const { return _fd; } int getFd() const { return _fd; }
const char *getName() const { return _name; } const char *getName() const { return _name; }
virtual off_t st_size() const; // { return _length; }
protected: protected:
void sopen(); void sopen();
@ -67,12 +68,15 @@ protected:
virtual void write(const void *buf, int len); virtual void write(const void *buf, int len);
virtual void seek(off_t off, int whence); virtual void seek(off_t off, int whence);
virtual off_t tell() const; virtual off_t tell() const;
virtual void set_extent(off_t offset, off_t length);
int _fd; int _fd;
int _flags; int _flags;
int _shflags; int _shflags;
int _mode; int _mode;
const char *_name; const char *_name;
off_t _offset;
off_t _length;
public: public:
struct stat st; struct stat st;
}; };

View File

@ -46,7 +46,7 @@ Packer::Packer(InputFile *f) :
last_patch(NULL), last_patch_len(0), last_patch_off(0) last_patch(NULL), last_patch_len(0), last_patch_off(0)
{ {
if (fi != NULL) if (fi != NULL)
file_size = fi->st.st_size; file_size = fi->st_size();
uip = new UiPacker(this); uip = new UiPacker(this);
memset(&ph, 0, sizeof(ph)); memset(&ph, 0, sizeof(ph));
} }