2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 08:20:53 +00:00
qpdf/libtests/dct_compress.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

98 lines
2.0 KiB
C++

#include <qpdf/Pl_DCT.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
static void usage()
{
std::cerr << "Usage: dct_compress infile outfile width height"
<< " {rgb|cmyk|gray}" << std::endl;
exit(2);
}
class Callback: public Pl_DCT::CompressConfig
{
public:
Callback() :
called(false)
{
}
virtual ~Callback()
{
}
virtual void apply(jpeg_compress_struct*);
bool called;
};
void Callback::apply(jpeg_compress_struct*)
{
this->called = true;
}
int main(int argc, char* argv[])
{
if (argc != 6)
{
usage();
}
char* infilename = argv[1];
char* outfilename = argv[2];
JDIMENSION width = QUtil::string_to_uint(argv[3]);
JDIMENSION height = QUtil::string_to_uint(argv[4]);
char* colorspace = argv[5];
J_COLOR_SPACE cs =
((strcmp(colorspace, "rgb") == 0) ? JCS_RGB :
(strcmp(colorspace, "cmyk") == 0) ? JCS_CMYK :
(strcmp(colorspace, "gray") == 0) ? JCS_GRAYSCALE :
JCS_UNKNOWN);
int components = 0;
switch (cs)
{
case JCS_RGB:
components = 3;
break;
case JCS_CMYK:
components = 4;
break;
case JCS_GRAYSCALE:
components = 1;
break;
default:
usage();
break;
}
FILE* infile = QUtil::safe_fopen(infilename, "rb");
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
Pl_StdioFile out("stdout", outfile);
unsigned char buf[100];
bool done = false;
Callback callback;
Pl_DCT dct("dct", &out, width, height, components, cs, &callback);
while (! done)
{
size_t len = fread(buf, 1, sizeof(buf), infile);
if (len <= 0)
{
done = true;
}
else
{
dct.write(buf, len);
}
}
dct.finish();
if (! callback.called)
{
std::cout << "Callback was not called" << std::endl;
}
fclose(infile);
fclose(outfile);
return 0;
}