2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-18 10:25:12 +00:00
qpdf/libtests/predictors.cc
Jay Berkenbilt d71f05ca07 Fix sign and conversion warnings (major)
This makes all integer type conversions that have potential data loss
explicit with calls that do range checks and raise an exception. After
this commit, qpdf builds with no warnings when -Wsign-conversion
-Wconversion is used with gcc or clang or when -W3 -Wd4800 is used
with MSVC. This significantly reduces the likelihood of potential
crashes from bogus integer values.

There are some parts of the code that take int when they should take
size_t or an offset. Such places would make qpdf not support files
with more than 2^31 of something that usually wouldn't be so large. In
the event that such a file shows up and is valid, at least qpdf would
raise an error in the right spot so the issue could be legitimately
addressed rather than failing in some weird way because of a silent
overflow condition.
2019-06-21 13:17:21 -04:00

103 lines
2.4 KiB
C++

#include <qpdf/Pl_PNGFilter.hh>
#include <qpdf/Pl_TIFFPredictor.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>
#include <iostream>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
void run(char const* filename, char const* filter,
bool encode, unsigned int columns,
unsigned int bits_per_sample, unsigned int samples_per_pixel)
{
FILE* in = QUtil::safe_fopen(filename, "rb");
FILE* o1 = QUtil::safe_fopen("out", "wb");
Pipeline* out = new Pl_StdioFile("out", o1);
Pipeline* pl = 0;
if (strcmp(filter, "png") == 0)
{
pl = new Pl_PNGFilter(
"png", out,
encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode,
columns, samples_per_pixel, bits_per_sample);
}
else if (strcmp(filter, "tiff") == 0)
{
pl = new Pl_TIFFPredictor(
"png", out,
encode ? Pl_TIFFPredictor::a_encode : Pl_TIFFPredictor::a_decode,
columns, samples_per_pixel, bits_per_sample);
}
else
{
std::cerr << "unknown filter " << filter << std::endl;
exit(2);
}
assert((2 * (columns + 1)) < 1024);
unsigned char buf[1024];
size_t len;
while (true)
{
len = fread(buf, 1, (2 * columns) + 1, in);
if (len == 0)
{
break;
}
pl->write(buf, len);
len = fread(buf, 1, 1, in);
if (len == 0)
{
break;
}
pl->write(buf, len);
len = fread(buf, 1, 1, in);
if (len == 0)
{
break;
}
pl->write(buf, len);
}
pl->finish();
delete pl;
delete out;
fclose(o1);
fclose(in);
std::cout << "done" << std::endl;
}
int main(int argc, char* argv[])
{
if (argc != 7)
{
std::cerr << "Usage: predictor {png|tiff} {en,de}code filename"
<< " columns samples-per-pixel bits-per-sample"
<< std::endl;
exit(2);
}
char* filter = argv[1];
bool encode = (strcmp(argv[2], "encode") == 0);
char* filename = argv[3];
int columns = QUtil::string_to_int(argv[4]);
int samples_per_pixel = QUtil::string_to_int(argv[5]);
int bits_per_sample = QUtil::string_to_int(argv[6]);
try
{
run(filename, filter, encode,
QIntC::to_uint(columns),
QIntC::to_uint(bits_per_sample),
QIntC::to_uint(samples_per_pixel));
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
return 0;
}