2012-07-21 09:37:14 +00:00
|
|
|
#include <qpdf/FileInputSource.hh>
|
|
|
|
#include <string.h>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <qpdf/QPDFExc.hh>
|
2013-11-29 15:20:50 +00:00
|
|
|
#include <algorithm>
|
2012-07-21 09:37:14 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
FileInputSource::Members::Members(bool close_file) :
|
|
|
|
close_file(close_file),
|
2012-07-21 09:37:14 +00:00
|
|
|
file(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
FileInputSource::Members::~Members()
|
|
|
|
{
|
|
|
|
if (this->file && this->close_file)
|
|
|
|
{
|
|
|
|
fclose(this->file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInputSource::FileInputSource() :
|
|
|
|
m(new Members(false))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-21 09:37:14 +00:00
|
|
|
void
|
|
|
|
FileInputSource::setFilename(char const* filename)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m = new Members(true);
|
|
|
|
this->m->filename = filename;
|
|
|
|
this->m->file = QUtil::safe_fopen(filename, "rb");
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileInputSource::setFile(
|
|
|
|
char const* description, FILE* filep, bool close_file)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m = new Members(close_file);
|
|
|
|
this->m->filename = description;
|
|
|
|
this->m->file = filep;
|
2012-07-21 09:37:14 +00:00
|
|
|
this->seek(0, SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInputSource::~FileInputSource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
FileInputSource::findAndSkipNextEOL()
|
|
|
|
{
|
|
|
|
qpdf_offset_t result = 0;
|
|
|
|
bool done = false;
|
|
|
|
char buf[10240];
|
|
|
|
while (! done)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
qpdf_offset_t cur_offset = QUtil::tell(this->m->file);
|
2012-07-21 09:37:14 +00:00
|
|
|
size_t len = this->read(buf, sizeof(buf));
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
result = this->tell();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
char* p1 = static_cast<char*>(memchr(buf, '\r', len));
|
|
|
|
char* p2 = static_cast<char*>(memchr(buf, '\n', len));
|
2012-07-21 09:37:14 +00:00
|
|
|
char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2;
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
result = cur_offset + (p - buf);
|
|
|
|
// We found \r or \n. Keep reading until we get past
|
|
|
|
// \r and \n characters.
|
|
|
|
this->seek(result + 1, SEEK_SET);
|
|
|
|
char ch;
|
|
|
|
while (! done)
|
|
|
|
{
|
|
|
|
if (this->read(&ch, 1) == 0)
|
|
|
|
{
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
else if (! ((ch == '\r') || (ch == '\n')))
|
|
|
|
{
|
2020-10-27 14:51:41 +00:00
|
|
|
this->unreadCh(ch);
|
2012-07-21 09:37:14 +00:00
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string const&
|
|
|
|
FileInputSource::getName() const
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return this->m->filename;
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qpdf_offset_t
|
|
|
|
FileInputSource::tell()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
return QUtil::tell(this->m->file);
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileInputSource::seek(qpdf_offset_t offset, int whence)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
QUtil::os_wrapper(std::string("seek to ") +
|
|
|
|
this->m->filename + ", offset " +
|
2012-07-21 09:37:14 +00:00
|
|
|
QUtil::int_to_string(offset) + " (" +
|
|
|
|
QUtil::int_to_string(whence) + ")",
|
2019-06-22 01:32:47 +00:00
|
|
|
QUtil::seek(this->m->file, offset, whence));
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileInputSource::rewind()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
::rewind(this->m->file);
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
FileInputSource::read(char* buffer, size_t length)
|
|
|
|
{
|
2018-01-28 23:28:45 +00:00
|
|
|
this->last_offset = this->tell();
|
2019-06-22 01:32:47 +00:00
|
|
|
size_t len = fread(buffer, 1, length, this->m->file);
|
2018-01-28 23:28:45 +00:00
|
|
|
if (len == 0)
|
2012-07-21 09:37:14 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (ferror(this->m->file))
|
2018-01-28 23:28:45 +00:00
|
|
|
{
|
|
|
|
throw QPDFExc(qpdf_e_system,
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->filename, "",
|
2018-01-28 23:28:45 +00:00
|
|
|
this->last_offset,
|
|
|
|
std::string("read ") +
|
2019-06-21 03:35:23 +00:00
|
|
|
QUtil::uint_to_string(length) + " bytes");
|
2018-01-28 23:28:45 +00:00
|
|
|
}
|
|
|
|
else if (length > 0)
|
|
|
|
{
|
|
|
|
this->seek(0, SEEK_END);
|
|
|
|
this->last_offset = this->tell();
|
|
|
|
}
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileInputSource::unreadCh(char ch)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
QUtil::os_wrapper(this->m->filename + ": unread character",
|
|
|
|
ungetc(static_cast<unsigned char>(ch), this->m->file));
|
2012-07-21 09:37:14 +00:00
|
|
|
}
|