2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 02:49:00 +00:00

Call QUtil::safe_fopen in place of fopen

fopen was previuosly called wrapped by QUtil::fopen_wrapper, but
QUtil::safe_fopen does this itself, which is less cumbersome.
This commit is contained in:
Jay Berkenbilt 2013-02-28 16:45:11 -05:00
parent 7ccc9bd9d5
commit ac4deac187
17 changed files with 77 additions and 123 deletions

View File

@ -1,5 +1,8 @@
2013-02-28 Jay Berkenbilt <ejb@ql.org>
* New method QUtil::safe_fopen to wrap calls to fopen. This is
less cumbersome than calling QUtil::fopen_wrapper.
* Remove all calls to sprintf
* New method QUtil::int_to_string_base to convert to octal or

View File

@ -54,6 +54,11 @@ namespace QUtil
QPDF_DLL
int os_wrapper(std::string const& description, int status);
// If the open fails, throws std::runtime_error. Otherwise, the
// FILE* is returned.
QPDF_DLL
FILE* safe_fopen(char const* filename, char const* mode);
// The FILE* argument is assumed to be the return of fopen. If
// null, throw std::runtime_error. Otherwise, return the FILE*
// argument.

View File

@ -15,8 +15,7 @@ FileInputSource::setFilename(char const* filename)
destroy();
this->filename = filename;
this->close_file = true;
this->file = QUtil::fopen_wrapper(std::string("open ") + this->filename,
fopen(this->filename.c_str(), "rb")); // XXXX
this->file = QUtil::safe_fopen(this->filename.c_str(), "rb");
}
void

View File

@ -328,10 +328,7 @@ void MD5::encodeFile(char const *filename, int up_to_size)
{
unsigned char buffer[1024];
FILE *file = QUtil::fopen_wrapper(
std::string("MD5: open ") + filename,
fopen(filename, "rb")); // XXXX
FILE *file = QUtil::safe_fopen(filename, "rb");
size_t len;
int so_far = 0;
int to_try = 1024;

View File

@ -104,8 +104,7 @@ QPDFWriter::setOutputFilename(char const* filename)
else
{
QTC::TC("qpdf", "QPDFWriter write to file");
f = QUtil::fopen_wrapper(std::string("open ") + filename,
fopen(filename, "wb+")); // XXXX
f = QUtil::safe_fopen(filename, "wb+");
close_file = true;
}
setOutputFile(description, f, close_file);

View File

@ -37,9 +37,7 @@ void QTC::TC(char const* const scope, char const* const ccase, int n)
}
cache.insert(std::make_pair(ccase, n));
FILE* tc =
QUtil::fopen_wrapper("open test coverage file (" + filename + ")",
fopen(filename.c_str(), "ab")); // XXXX
FILE* tc = QUtil::safe_fopen(filename.c_str(), "ab");
fprintf(tc, "%s %d\n", ccase, n);
fclose(tc);
}

View File

@ -109,6 +109,13 @@ QUtil::os_wrapper(std::string const& description, int status)
return status;
}
FILE*
QUtil::safe_fopen(char const* filename, char const* mode)
{
return fopen_wrapper(std::string("open ") + filename,
fopen(filename, mode)); // XXXX
}
FILE*
QUtil::fopen_wrapper(std::string const& description, FILE* f)
{

View File

@ -1,5 +1,6 @@
#include <qpdf/Pl_AES_PDF.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <stdio.h>
#include <string.h>
@ -89,20 +90,8 @@ int main(int argc, char* argv[])
unsigned int hexkeylen = strlen(hexkey);
unsigned int keylen = hexkeylen / 2;
FILE* infile = fopen(infilename, "rb"); // XXXX
if (infile == 0)
{
std::cerr << "can't open " << infilename << std::endl;
exit(2);
}
FILE* outfile = fopen(outfilename, "wb"); // XXXX
if (outfile == 0)
{
std::cerr << "can't open " << outfilename << std::endl;
exit(2);
}
FILE* infile = QUtil::safe_fopen(infilename, "rb");
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
unsigned char* key = new unsigned char[keylen];
for (unsigned int i = 0; i < strlen(hexkey); i += 2)
{

View File

@ -2,33 +2,22 @@
#include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/Pl_Count.hh>
#include <qpdf/QUtil.hh>
#include <iostream>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
FILE* safe_fopen(char const* filename, char const* mode)
{
FILE* result = fopen(filename, mode); // XXXX
if (result == 0)
{
std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX
<< std::endl;
exit(2);
}
return result;
}
void run(char const* filename)
{
std::string n1 = std::string(filename) + ".1";
std::string n2 = std::string(filename) + ".2";
std::string n3 = std::string(filename) + ".3";
FILE* o1 = safe_fopen(n1.c_str(), "wb");
FILE* o2 = safe_fopen(n2.c_str(), "wb");
FILE* o3 = safe_fopen(n3.c_str(), "wb");
FILE* o1 = QUtil::safe_fopen(n1.c_str(), "wb");
FILE* o2 = QUtil::safe_fopen(n2.c_str(), "wb");
FILE* o3 = QUtil::safe_fopen(n3.c_str(), "wb");
Pipeline* out1 = new Pl_StdioFile("o1", o1);
Pipeline* out2 = new Pl_StdioFile("o2", o2);
Pipeline* out3 = new Pl_StdioFile("o3", o3);
@ -46,7 +35,7 @@ void run(char const* filename)
Pipeline* inf3 = new Pl_Flate("inf3", count3, Pl_Flate::a_inflate);
Pipeline* def3 = new Pl_Flate("def3", inf3, Pl_Flate::a_deflate);
FILE* in1 = safe_fopen(filename, "rb");
FILE* in1 = QUtil::safe_fopen(filename, "rb");
unsigned char buf[1024];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), in1)) > 0)
@ -75,7 +64,7 @@ void run(char const* filename)
fclose(o3);
// Now read the compressed data and write to the output uncompress pipeline
FILE* in2 = safe_fopen(n1.c_str(), "rb");
FILE* in2 = QUtil::safe_fopen(n1.c_str(), "rb");
while ((len = fread(buf, 1, sizeof(buf), in2)) > 0)
{
inf2->write(buf, len);

View File

@ -26,10 +26,8 @@ int main(int argc, char* argv[])
char* infilename = argv[1];
char* outfilename = argv[2];
FILE* infile = QUtil::fopen_wrapper("open input file",
fopen(infilename, "rb")); // XXXX
FILE* outfile = QUtil::fopen_wrapper("open output file",
fopen(outfilename, "wb")); // XXXX
FILE* infile = QUtil::safe_fopen(infilename, "rb");
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
Pl_StdioFile out("output", outfile);
Pl_LZWDecoder decode("decode", &out, early_code_change);

View File

@ -1,6 +1,7 @@
#include <qpdf/MD5.hh>
#include <qpdf/Pl_MD5.hh>
#include <qpdf/Pl_Discard.hh>
#include <qpdf/QUtil.hh>
#include <iostream>
#include <stdio.h>
@ -46,28 +47,25 @@ int main(int, char*[])
Pl_MD5 p("MD5", &d);
for (int i = 0; i < 2; ++i)
{
FILE* f = fopen("md5.in", "rb"); // XXXX
if (f)
{
// buffer size < size of md5.in
unsigned char buf[50];
bool done = false;
while (! done)
{
size_t len = fread(buf, 1, sizeof(buf), f);
if (len <= 0)
{
done = true;
}
else
{
p.write(buf, len);
}
}
fclose(f);
p.finish();
std::cout << p.getHexDigest() << std::endl;
}
FILE* f = QUtil::safe_fopen("md5.in", "rb");
// buffer size < size of md5.in
unsigned char buf[50];
bool done = false;
while (! done)
{
size_t len = fread(buf, 1, sizeof(buf), f);
if (len <= 0)
{
done = true;
}
else
{
p.write(buf, len);
}
}
fclose(f);
p.finish();
std::cout << p.getHexDigest() << std::endl;
}
return 0;

View File

@ -1,5 +1,6 @@
#include <qpdf/Pl_PNGFilter.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <iostream>
#include <errno.h>
@ -7,23 +8,11 @@
#include <string.h>
#include <stdlib.h>
FILE* safe_fopen(char const* filename, char const* mode)
{
FILE* result = fopen(filename, mode); // XXXX
if (result == 0)
{
std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX
<< std::endl;
exit(2);
}
return result;
}
void run(char const* filename, bool encode, unsigned int columns)
{
// Decode the file
FILE* in = safe_fopen(filename, "rb");
FILE* o1 = safe_fopen("out", "wb");
FILE* in = QUtil::safe_fopen(filename, "rb");
FILE* o1 = QUtil::safe_fopen("out", "wb");
Pipeline* out = new Pl_StdioFile("out", o1);
Pipeline* pl = new Pl_PNGFilter(
"png", out,

View File

@ -19,7 +19,7 @@ before remove
exception: remove file: No such file or directory
----
before fopen
exception: fopen file: No such file or directory
exception: open /this/file/does/not/exist: No such file or directory
----
IN_TESTSUITE: 1: 1
HAGOOGAMAGOOGLE: 0

View File

@ -62,12 +62,10 @@ void os_wrapper_test()
void fopen_wrapper_test()
{
FILE* f = 0;
try
{
std::cout << "before fopen" << std::endl;
f = QUtil::fopen_wrapper("fopen file",
fopen("/this/file/does/not/exist", "r")); // XXXX
FILE* f = QUtil::safe_fopen("/this/file/does/not/exist", "r");
std::cout << "after fopen" << std::endl;
(void) fclose(f);
}

View File

@ -1,5 +1,6 @@
#include <qpdf/Pl_RC4.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <stdio.h>
#include <string.h>
@ -22,13 +23,7 @@ int main(int argc, char* argv[])
unsigned char* key = new unsigned char[keylen + 1];
key[keylen] = '\0';
FILE* infile = fopen(infilename, "rb"); // XXXX
if (infile == 0)
{
std::cerr << "can't open " << infilename << std::endl;
exit(2);
}
FILE* infile = QUtil::safe_fopen(infilename, "rb");
for (unsigned int i = 0; i < strlen(hexkey); i += 2)
{
char t[3];
@ -40,12 +35,7 @@ int main(int argc, char* argv[])
key[i/2] = static_cast<unsigned char>(val);
}
FILE* outfile = fopen(outfilename, "wb"); // XXXX
if (outfile == 0)
{
std::cerr << "can't open " << outfilename << std::endl;
exit(2);
}
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
// Use a small buffer size (64) for testing
Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64);

View File

@ -8,6 +8,18 @@
static char* whoami = 0;
static qpdf_data qpdf = 0;
static FILE* safe_fopen(char const* filename, char const* mode)
{
FILE* f = fopen(filename, mode); /* XXXX */
if (f == NULL)
{
fprintf(stderr, "%s: unable to open %s: %s\n",
whoami, filename, strerror(errno)); /* XXXX */
exit(2);
}
return f;
}
static void report_errors()
{
qpdf_error e = 0;
@ -56,13 +68,7 @@ static void read_file_into_memory(char const* filename,
size_t bytes_read = 0;
size_t len = 0;
f = fopen(filename, "rb"); /* XXXX */
if (f == NULL)
{
fprintf(stderr, "%s: unable to open %s: %s\n",
whoami, filename, strerror(errno)); /* XXXX */
exit(2);
}
f = safe_fopen(filename, "rb");
fseek(f, 0, SEEK_END);
*size = (unsigned long) ftell(f);
fseek(f, 0, SEEK_SET);
@ -364,13 +370,7 @@ static void test16(char const* infile,
qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress);
qpdf_write(qpdf);
f = fopen(outfile, "wb"); /* XXXX */
if (f == NULL)
{
fprintf(stderr, "%s: unable to open %s: %s\n",
whoami, outfile, strerror(errno)); /* XXXX */
exit(2);
}
f = safe_fopen(outfile, "wb");
buflen = qpdf_get_buffer_length(qpdf);
buf = qpdf_get_buffer(qpdf);
fwrite(buf, 1, buflen, f);

View File

@ -165,16 +165,14 @@ void runtest(int n, char const* filename1, char const* arg2)
else
{
QTC::TC("qpdf", "exercise processFile(FILE*)");
filep = QUtil::fopen_wrapper(std::string("open ") + filename1,
fopen(filename1, "rb")); // XXXX
filep = QUtil::safe_fopen(filename1, "rb");
pdf.processFile(filename1, filep, false);
}
}
else
{
QTC::TC("qpdf", "exercise processMemoryFile");
FILE* f = QUtil::fopen_wrapper(std::string("open ") + filename1,
fopen(filename1, "rb")); // XXXX
FILE* f = QUtil::safe_fopen(filename1, "rb");
fseek(f, 0, SEEK_END);
size_t size = QUtil::tell(f);
fseek(f, 0, SEEK_SET);
@ -718,8 +716,7 @@ void runtest(int n, char const* filename1, char const* arg2)
w.write();
Buffer* b = w.getBuffer();
std::string const filename = (i == 0 ? "a.pdf" : "b.pdf");
FILE* f = QUtil::fopen_wrapper("open " + filename,
fopen(filename.c_str(), "wb")); // XXXX
FILE* f = QUtil::safe_fopen(filename.c_str(), "wb");
fwrite(b->getBuffer(), b->getSize(), 1, f);
fclose(f);
delete b;
@ -802,8 +799,7 @@ void runtest(int n, char const* filename1, char const* arg2)
checkPageContents(pages[12], "New page 12");
// Exercise writing to FILE*
FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"),
fopen("a.pdf", "wb")); // XXXX
FILE* out = QUtil::safe_fopen("a.pdf", "wb");
QPDFWriter w(pdf, "FILE* a.pdf", out, true);
w.setStaticID(true);
w.setStreamDataMode(qpdf_s_preserve);
@ -1183,8 +1179,7 @@ void runtest(int n, char const* filename1, char const* arg2)
w.setOutputPipeline(&p);
w.write();
PointerHolder<Buffer> b = p.getBuffer();
FILE* f = QUtil::fopen_wrapper("open a.pdf",
fopen("a.pdf", "wb")); // XXXX
FILE* f = QUtil::safe_fopen("a.pdf", "wb");
fwrite(b->getBuffer(), b->getSize(), 1, f);
fclose(f);
}