2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-07 21:00:54 +00:00

Remove all old-style casts from C++ code

This commit is contained in:
Jay Berkenbilt 2013-02-23 21:46:21 -05:00
parent babb47948a
commit 30027481f7
43 changed files with 346 additions and 276 deletions

View File

@ -39,7 +39,7 @@ ImageProvider::provideStreamData(int objid, int generation,
{ {
for (int i = 0; i < width * height; ++i) for (int i = 0; i < width * height; ++i)
{ {
pipeline->write((unsigned char*)"\xff\x7f\x00", 3); pipeline->write(QUtil::unsigned_char_pointer("\xff\x7f\x00"), 3);
} }
pipeline->finish(); pipeline->finish();
} }

View File

@ -53,7 +53,7 @@ ImageInverter::provideStreamData(int objid, int generation,
unsigned char ch; unsigned char ch;
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
ch = (unsigned char)0xff - buf[i]; ch = static_cast<unsigned char>(0xff) - buf[i];
pipeline->write(&ch, 1); pipeline->write(&ch, 1);
} }
pipeline->finish(); pipeline->finish();

View File

@ -69,7 +69,7 @@ int main(int argc, char* argv[])
QPDF pdf; QPDF pdf;
pdf.processFile(filename); pdf.processFile(filename);
std::vector<QPDFObjectHandle> pages = pdf.getAllPages(); std::vector<QPDFObjectHandle> pages = pdf.getAllPages();
if ((pageno < 1) || (pageno > (int)pages.size())) if ((pageno < 1) || (static_cast<size_t>(pageno) > pages.size()))
{ {
usage(); usage();
} }

View File

@ -931,7 +931,7 @@ class QPDF
void readHPageOffset(BitStream); void readHPageOffset(BitStream);
void readHSharedObject(BitStream); void readHSharedObject(BitStream);
void readHGeneric(BitStream, HGeneric&); void readHGeneric(BitStream, HGeneric&);
int maxEnd(ObjUser const& ou); qpdf_offset_t maxEnd(ObjUser const& ou);
qpdf_offset_t getLinearizationOffset(ObjGen const&); qpdf_offset_t getLinearizationOffset(ObjGen const&);
QPDFObjectHandle getUncompressedObject( QPDFObjectHandle getUncompressedObject(
QPDFObjectHandle&, std::map<int, int> const& object_stream_data); QPDFObjectHandle&, std::map<int, int> const& object_stream_data);

View File

@ -27,6 +27,17 @@ namespace QUtil
QPDF_DLL QPDF_DLL
long long string_to_ll(char const* str); long long string_to_ll(char const* str);
// Pipeline's write method wants unsigned char*, but we often have
// some other type of string. These methods do combinations of
// const_cast and reinterpret_cast to give us an unsigned char*.
// They should only be used when it is known that it is safe.
// None of the pipelines in qpdf modify the data passed to them,
// so within qpdf, it should always be safe.
QPDF_DLL
unsigned char* unsigned_char_pointer(std::string const& str);
QPDF_DLL
unsigned char* unsigned_char_pointer(char const* str);
// Throw std::runtime_error with a string formed by appending to // Throw std::runtime_error with a string formed by appending to
// "description: " the standard string corresponding to the // "description: " the standard string corresponding to the
// current value of errno. // current value of errno.

View File

@ -20,7 +20,7 @@ BufferInputSource::BufferInputSource(std::string const& description,
{ {
this->buf = new Buffer(contents.length()); this->buf = new Buffer(contents.length());
unsigned char* bp = buf->getBuffer(); unsigned char* bp = buf->getBuffer();
memcpy(bp, (char*)contents.c_str(), contents.length()); memcpy(bp, contents.c_str(), contents.length());
} }
BufferInputSource::~BufferInputSource() BufferInputSource::~BufferInputSource()
@ -38,7 +38,7 @@ BufferInputSource::findAndSkipNextEOL()
{ {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
} }
qpdf_offset_t end_pos = (qpdf_offset_t) this->buf->getSize(); qpdf_offset_t end_pos = this->buf->getSize();
if (this->cur_offset >= end_pos) if (this->cur_offset >= end_pos)
{ {
this->last_offset = end_pos; this->last_offset = end_pos;
@ -47,12 +47,12 @@ BufferInputSource::findAndSkipNextEOL()
} }
qpdf_offset_t result = 0; qpdf_offset_t result = 0;
size_t len = (size_t)(end_pos - this->cur_offset); size_t len = end_pos - this->cur_offset;
unsigned char const* buffer = this->buf->getBuffer(); unsigned char const* buffer = this->buf->getBuffer();
void* start = (void*)(buffer + this->cur_offset); void* start = const_cast<unsigned char*>(buffer) + this->cur_offset;
unsigned char* p1 = (unsigned char*)memchr(start, '\r', len); unsigned char* p1 = static_cast<unsigned char*>(memchr(start, '\r', len));
unsigned char* p2 = (unsigned char*)memchr(start, '\n', len); unsigned char* p2 = static_cast<unsigned char*>(memchr(start, '\n', len));
unsigned char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2; unsigned char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2;
if (p) if (p)
{ {
@ -96,7 +96,7 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
break; break;
case SEEK_END: case SEEK_END:
this->cur_offset = (qpdf_offset_t)this->buf->getSize() + offset; this->cur_offset = this->buf->getSize() + offset;
break; break;
case SEEK_CUR: case SEEK_CUR:
@ -129,7 +129,7 @@ BufferInputSource::read(char* buffer, size_t length)
{ {
throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0"); throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
} }
qpdf_offset_t end_pos = (qpdf_offset_t) this->buf->getSize(); qpdf_offset_t end_pos = this->buf->getSize();
if (this->cur_offset >= end_pos) if (this->cur_offset >= end_pos)
{ {
this->last_offset = end_pos; this->last_offset = end_pos;
@ -137,7 +137,8 @@ BufferInputSource::read(char* buffer, size_t length)
} }
this->last_offset = this->cur_offset; this->last_offset = this->cur_offset;
size_t len = std::min((size_t)(end_pos - this->cur_offset), length); size_t len = std::min(
static_cast<size_t>(end_pos - this->cur_offset), length);
memcpy(buffer, buf->getBuffer() + this->cur_offset, len); memcpy(buffer, buf->getBuffer() + this->cur_offset, len);
this->cur_offset += len; this->cur_offset += len;
return len; return len;

View File

@ -62,8 +62,8 @@ FileInputSource::findAndSkipNextEOL()
} }
else else
{ {
char* p1 = (char*)memchr((void*)buf, '\r', len); char* p1 = static_cast<char*>(memchr(buf, '\r', len));
char* p2 = (char*)memchr((void*)buf, '\n', len); char* p2 = static_cast<char*>(memchr(buf, '\n', len));
char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2; char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2;
if (p) if (p)
{ {
@ -137,5 +137,5 @@ void
FileInputSource::unreadCh(char ch) FileInputSource::unreadCh(char ch)
{ {
QUtil::os_wrapper(this->filename + ": unread character", QUtil::os_wrapper(this->filename + ": unread character",
ungetc((unsigned char)ch, this->file)); ungetc(static_cast<unsigned char>(ch), this->file));
} }

View File

@ -71,22 +71,22 @@ static unsigned char PADDING[64] = {
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
// Rotation is separate from addition to prevent recomputation. // Rotation is separate from addition to prevent recomputation.
#define FF(a, b, c, d, x, s, ac) { \ #define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) += F ((b), (c), (d)) + (x) + static_cast<UINT4>(ac); \
(a) = ROTATE_LEFT ((a), (s)); \ (a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define GG(a, b, c, d, x, s, ac) { \ #define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) += G ((b), (c), (d)) + (x) + static_cast<UINT4>(ac); \
(a) = ROTATE_LEFT ((a), (s)); \ (a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define HH(a, b, c, d, x, s, ac) { \ #define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) += H ((b), (c), (d)) + (x) + static_cast<UINT4>(ac); \
(a) = ROTATE_LEFT ((a), (s)); \ (a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
#define II(a, b, c, d, x, s, ac) { \ #define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \ (a) += I ((b), (c), (d)) + (x) + static_cast<UINT4>(ac); \
(a) = ROTATE_LEFT ((a), (s)); \ (a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \ (a) += (b); \
} }
@ -115,21 +115,20 @@ void MD5::update(unsigned char *input,
unsigned int i, index, partLen; unsigned int i, index, partLen;
// Compute number of bytes mod 64 // Compute number of bytes mod 64
index = (unsigned int)((count[0] >> 3) & 0x3F); index = static_cast<unsigned int>((count[0] >> 3) & 0x3f);
// Update number of bits // Update number of bits
if ((count[0] += ((UINT4)inputLen << 3)) if ((count[0] += (static_cast<UINT4>(inputLen) << 3)) <
< ((UINT4)inputLen << 3)) (static_cast<UINT4>(inputLen) << 3))
count[1]++; count[1]++;
count[1] += ((UINT4)inputLen >> 29); count[1] += (static_cast<UINT4>(inputLen) >> 29);
partLen = 64 - index; partLen = 64 - index;
// Transform as many times as possible. // Transform as many times as possible.
if (inputLen >= partLen) { if (inputLen >= partLen) {
memcpy memcpy(&buffer[index], input, partLen);
((POINTER)&buffer[index], (POINTER)input, partLen);
transform(state, buffer); transform(state, buffer);
for (i = partLen; i + 63 < inputLen; i += 64) for (i = partLen; i + 63 < inputLen; i += 64)
@ -141,9 +140,7 @@ void MD5::update(unsigned char *input,
i = 0; i = 0;
// Buffer remaining input // Buffer remaining input
memcpy memcpy(&buffer[index], &input[i], inputLen-i);
((POINTER)&buffer[index], (POINTER)&input[i],
inputLen-i);
} }
// MD5 finalization. Ends an MD5 message-digest operation, writing the // MD5 finalization. Ends an MD5 message-digest operation, writing the
@ -163,7 +160,7 @@ void MD5::final()
// Pad out to 56 mod 64. // Pad out to 56 mod 64.
index = (unsigned int)((count[0] >> 3) & 0x3f); index = static_cast<unsigned int>((count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index); padLen = (index < 56) ? (56 - index) : (120 - index);
update(PADDING, padLen); update(PADDING, padLen);
@ -266,7 +263,7 @@ void MD5::transform(UINT4 state[4], unsigned char block[64])
// Zeroize sensitive information. // Zeroize sensitive information.
memset ((POINTER)x, 0, sizeof (x)); memset (x, 0, sizeof (x));
} }
// Encodes input (UINT4) into output (unsigned char). Assumes len is a // Encodes input (UINT4) into output (unsigned char). Assumes len is a
@ -276,10 +273,10 @@ void MD5::encode(unsigned char *output, UINT4 *input, unsigned int len)
unsigned int i, j; unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) { for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff); output[j] = static_cast<unsigned char>(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); output[j+1] = static_cast<unsigned char>((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); output[j+2] = static_cast<unsigned char>((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); output[j+3] = static_cast<unsigned char>((input[i] >> 24) & 0xff);
} }
} }
@ -290,8 +287,11 @@ void MD5::decode(UINT4 *output, unsigned char *input, unsigned int len)
unsigned int i, j; unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | output[i] =
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); static_cast<UINT4>(input[j]) |
(static_cast<UINT4>(input[j+1]) << 8) |
(static_cast<UINT4>(input[j+2]) << 16) |
(static_cast<UINT4>(input[j+3]) << 24);
} }
// Public functions // Public functions
@ -308,20 +308,20 @@ void MD5::reset()
void MD5::encodeString(char const* str) void MD5::encodeString(char const* str)
{ {
unsigned int len = (unsigned int)strlen(str); unsigned int len = strlen(str);
update((unsigned char *)str, len); update(QUtil::unsigned_char_pointer(str), len);
final(); final();
} }
void MD5::appendString(char const* input_string) void MD5::appendString(char const* input_string)
{ {
update((unsigned char *)input_string, (unsigned int) strlen(input_string)); update(QUtil::unsigned_char_pointer(input_string), strlen(input_string));
} }
void MD5::encodeDataIncrementally(char const* data, int len) void MD5::encodeDataIncrementally(char const* data, int len)
{ {
update((unsigned char *)data, len); update(QUtil::unsigned_char_pointer(data), len);
} }
void MD5::encodeFile(char const *filename, int up_to_size) void MD5::encodeFile(char const *filename, int up_to_size)
@ -344,7 +344,7 @@ void MD5::encodeFile(char const *filename, int up_to_size)
len = fread(buffer, 1, to_try, file); len = fread(buffer, 1, to_try, file);
if (len > 0) if (len > 0)
{ {
update(buffer, (unsigned int) len); update(buffer, len);
so_far += len; so_far += len;
if ((up_to_size >= 0) && (so_far >= up_to_size)) if ((up_to_size >= 0) && (so_far >= up_to_size))
{ {
@ -386,7 +386,8 @@ void MD5::print()
std::string MD5::unparse() std::string MD5::unparse()
{ {
final(); final();
return QUtil::hex_encode(std::string((char*)digest_val, 16)); return QUtil::hex_encode(
std::string(reinterpret_cast<char*>(digest_val), 16));
} }
std::string std::string

View File

@ -166,7 +166,7 @@ PCRE::match(char const* subject, int options, int startoffset, int size)
{ {
if (size == -1) if (size == -1)
{ {
size = (int) strlen(subject); size = strlen(subject);
} }
Match result(this->nbackrefs, subject); Match result(this->nbackrefs, subject);

View File

@ -122,7 +122,8 @@ Pl_AES_PDF::finish()
// Pad as described in section 3.5.1 of version 1.7 of the PDF // Pad as described in section 3.5.1 of version 1.7 of the PDF
// specification, including providing an entire block of padding // specification, including providing an entire block of padding
// if the input was a multiple of 16 bytes. // if the input was a multiple of 16 bytes.
unsigned char pad = (unsigned char) (this->buf_size - this->offset); unsigned char pad =
static_cast<unsigned char>(this->buf_size - this->offset);
memset(this->inbuf + this->offset, pad, pad); memset(this->inbuf + this->offset, pad, pad);
this->offset = this->buf_size; this->offset = this->buf_size;
flush(false); flush(false);

View File

@ -68,7 +68,9 @@ Pl_ASCII85Decoder::write(unsigned char* buf, size_t len)
else else
{ {
QTC::TC("libtests", "Pl_ASCII85Decoder read z"); QTC::TC("libtests", "Pl_ASCII85Decoder read z");
getNext()->write((unsigned char*)"\000\000\000\000", 4); unsigned char zeroes[4];
memset(zeroes, '\0', 4);
getNext()->write(zeroes, 4);
} }
break; break;

View File

@ -91,7 +91,7 @@ Pl_ASCIIHexDecoder::flush()
b[i] = this->inbuf[i] - '0'; b[i] = this->inbuf[i] - '0';
} }
} }
unsigned char ch = (unsigned char)((b[0] << 4) + b[1]); unsigned char ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush", QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush",
(this->pos == 2) ? 0 : 1); (this->pos == 2) ? 0 : 1);

View File

@ -18,10 +18,10 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
// Windows environment. // Windows environment.
this->zdata = new z_stream; this->zdata = new z_stream;
z_stream& zstream = *((z_stream*) this->zdata); z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
zstream.zalloc = (alloc_func)0; zstream.zalloc = 0;
zstream.zfree = (free_func)0; zstream.zfree = 0;
zstream.opaque = (voidpf)0; zstream.opaque = 0;
zstream.next_in = 0; zstream.next_in = 0;
zstream.avail_in = 0; zstream.avail_in = 0;
zstream.next_out = this->outbuf; zstream.next_out = this->outbuf;
@ -35,7 +35,7 @@ Pl_Flate::~Pl_Flate()
delete [] this->outbuf; delete [] this->outbuf;
this->outbuf = 0; this->outbuf = 0;
} }
delete (z_stream*)this->zdata; delete static_cast<z_stream*>(this->zdata);
this->zdata = 0; this->zdata = 0;
} }
@ -57,7 +57,7 @@ Pl_Flate::write(unsigned char* data, size_t len)
while (bytes_left > 0) while (bytes_left > 0)
{ {
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left); size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
handleData(buf, (int)bytes, Z_NO_FLUSH); handleData(buf, bytes, Z_NO_FLUSH);
bytes_left -= bytes; bytes_left -= bytes;
buf += bytes; buf += bytes;
} }
@ -66,13 +66,20 @@ Pl_Flate::write(unsigned char* data, size_t len)
void void
Pl_Flate::handleData(unsigned char* data, int len, int flush) Pl_Flate::handleData(unsigned char* data, int len, int flush)
{ {
z_stream& zstream = *((z_stream*) this->zdata); z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
zstream.next_in = data; zstream.next_in = data;
zstream.avail_in = len; zstream.avail_in = len;
if (! this->initialized) if (! this->initialized)
{ {
int err = Z_OK; int err = Z_OK;
// deflateInit and inflateInit are macros that use old-style
// casts.
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
if (this->action == a_deflate) if (this->action == a_deflate)
{ {
err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION); err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
@ -81,6 +88,10 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
{ {
err = inflateInit(&zstream); err = inflateInit(&zstream);
} }
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif
checkError("Init", err); checkError("Init", err);
this->initialized = true; this->initialized = true;
} }
@ -146,7 +157,7 @@ Pl_Flate::finish()
{ {
if (this->initialized) if (this->initialized)
{ {
z_stream& zstream = *((z_stream*) this->zdata); z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
unsigned char buf[1]; unsigned char buf[1];
buf[0] = '\0'; buf[0] = '\0';
handleData(buf, 0, Z_FINISH); handleData(buf, 0, Z_FINISH);
@ -171,7 +182,7 @@ Pl_Flate::finish()
void void
Pl_Flate::checkError(char const* prefix, int error_code) Pl_Flate::checkError(char const* prefix, int error_code)
{ {
z_stream& zstream = *((z_stream*) this->zdata); z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
if (error_code != Z_OK) if (error_code != Z_OK)
{ {
char const* action_str = (action == a_deflate ? "deflate" : "inflate"); char const* action_str = (action == a_deflate ? "deflate" : "inflate");

View File

@ -98,7 +98,7 @@ Pl_LZWDecoder::getFirstChar(int code)
unsigned char result = '\0'; unsigned char result = '\0';
if (code < 256) if (code < 256)
{ {
result = (unsigned char) code; result = static_cast<unsigned char>(code);
} }
else else
{ {
@ -131,7 +131,7 @@ Pl_LZWDecoder::addToTable(unsigned char next)
assert(idx < table.size()); assert(idx < table.size());
Buffer& b = table[idx]; Buffer& b = table[idx];
last_data = b.getBuffer(); last_data = b.getBuffer();
last_size = (unsigned int) b.getSize(); last_size = b.getSize();
} }
Buffer entry(1 + last_size); Buffer entry(1 + last_size);
@ -170,7 +170,7 @@ Pl_LZWDecoder::handleCode(int code)
// be what we read last plus the first character of what // be what we read last plus the first character of what
// we're reading now. // we're reading now.
unsigned char next = '\0'; unsigned char next = '\0';
unsigned int table_size = (unsigned int) table.size(); unsigned int table_size = table.size();
if (code < 256) if (code < 256)
{ {
// just read < 256; last time's next was code // just read < 256; last time's next was code
@ -214,7 +214,7 @@ Pl_LZWDecoder::handleCode(int code)
if (code < 256) if (code < 256)
{ {
unsigned char ch = (unsigned char) code; unsigned char ch = static_cast<unsigned char>(code);
getNext()->write(&ch, 1); getNext()->write(&ch, 1);
} }
else else

View File

@ -28,7 +28,8 @@ Pl_MD5::write(unsigned char* buf, size_t len)
while (bytes_left > 0) while (bytes_left > 0)
{ {
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left); size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
this->md5.encodeDataIncrementally((char*) data, (int)bytes); this->md5.encodeDataIncrementally(
reinterpret_cast<char*>(data), bytes);
bytes_left -= bytes; bytes_left -= bytes;
data += bytes; data += bytes;
} }

View File

@ -73,7 +73,7 @@ Pl_PNGFilter::processRow()
void void
Pl_PNGFilter::decodeRow() Pl_PNGFilter::decodeRow()
{ {
int filter = (int) this->cur_row[0]; int filter = this->cur_row[0];
if (this->prev_row) if (this->prev_row)
{ {
switch (filter) switch (filter)

View File

@ -38,7 +38,7 @@ Pl_RC4::write(unsigned char* data, size_t len)
size_t bytes = size_t bytes =
(bytes_left < this->out_bufsize ? bytes_left : out_bufsize); (bytes_left < this->out_bufsize ? bytes_left : out_bufsize);
bytes_left -= bytes; bytes_left -= bytes;
rc4.process(p, (int)bytes, outbuf); rc4.process(p, bytes, outbuf);
p += bytes; p += bytes;
getNext()->write(outbuf, bytes); getNext()->write(outbuf, bytes);
} }

View File

@ -128,13 +128,16 @@ Pl_SHA2::getRawDigest()
switch (bits) switch (bits)
{ {
case 256: case 256:
result = std::string((char*)this->sha256sum, sizeof(this->sha256sum)); result = std::string(reinterpret_cast<char*>(this->sha256sum),
sizeof(this->sha256sum));
break; break;
case 384: case 384:
result = std::string((char*)this->sha384sum, sizeof(this->sha384sum)); result = std::string(reinterpret_cast<char*>(this->sha384sum),
sizeof(this->sha384sum));
break; break;
case 512: case 512:
result = std::string((char*)this->sha512sum, sizeof(this->sha512sum)); result = std::string(reinterpret_cast<char*>(this->sha512sum),
sizeof(this->sha512sum));
break; break;
default: default:
badBits(); badBits();

View File

@ -159,8 +159,9 @@ QPDF::processMemoryFile(char const* description,
char const* password) char const* password)
{ {
processInputSource( processInputSource(
new BufferInputSource(description, new BufferInputSource(
new Buffer((unsigned char*)buf, length), description,
new Buffer(QUtil::unsigned_char_pointer(buf), length),
true), true),
password); password);
} }
@ -280,7 +281,7 @@ QPDF::parse(char const* password)
// where the regexp matches. // where the regexp matches.
char* p = buf; char* p = buf;
char const* candidate = ""; char const* candidate = "";
while ((p = (char*)memchr(p, 's', tbuf_size - (p - buf))) != 0) while ((p = static_cast<char*>(memchr(p, 's', tbuf_size - (p - buf)))) != 0)
{ {
if (eof_re.match(p)) if (eof_re.match(p))
{ {
@ -796,7 +797,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj)
for (int k = 0; k < W[j]; ++k) for (int k = 0; k < W[j]; ++k)
{ {
fields[j] <<= 8; fields[j] <<= 8;
fields[j] += (int)(*p++); fields[j] += static_cast<int>(*p++);
} }
} }
@ -828,7 +829,8 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj)
// This is needed by checkLinearization() // This is needed by checkLinearization()
this->first_xref_item_offset = xref_offset; this->first_xref_item_offset = xref_offset;
} }
insertXrefEntry(obj, (int)fields[0], fields[1], (int)fields[2]); insertXrefEntry(obj, static_cast<int>(fields[0]),
fields[1], static_cast<int>(fields[2]));
} }
if (! this->trailer.isInitialized()) if (! this->trailer.isInitialized())
@ -1096,8 +1098,7 @@ QPDF::readObject(PointerHolder<InputSource> input,
} }
length = length_obj.getIntValue(); length = length_obj.getIntValue();
input->seek( input->seek(stream_offset + length, SEEK_SET);
stream_offset + (qpdf_offset_t)length, SEEK_SET);
if (! (readToken(input) == if (! (readToken(input) ==
QPDFTokenizer::Token( QPDFTokenizer::Token(
QPDFTokenizer::tt_word, "endstream"))) QPDFTokenizer::tt_word, "endstream")))
@ -1395,7 +1396,7 @@ QPDF::readObjectAtOffset(bool try_recovery,
char ch; char ch;
if (this->file->read(&ch, 1)) if (this->file->read(&ch, 1))
{ {
if (! isspace((unsigned char)ch)) if (! isspace(static_cast<unsigned char>(ch)))
{ {
this->file->seek(-1, SEEK_CUR); this->file->seek(-1, SEEK_CUR);
break; break;
@ -2064,7 +2065,7 @@ QPDF::pipeStreamData(int objid, int generation,
"unexpected EOF reading stream data"); "unexpected EOF reading stream data");
} }
length -= len; length -= len;
pipeline->write((unsigned char*)buf, len); pipeline->write(QUtil::unsigned_char_pointer(buf), len);
} }
} }
catch (QPDFExc& e) catch (QPDFExc& e)

View File

@ -508,7 +508,7 @@ QPDFObjectHandle::replaceStreamData(std::string const& data,
assertStream(); assertStream();
PointerHolder<Buffer> b = new Buffer(data.length()); PointerHolder<Buffer> b = new Buffer(data.length());
unsigned char* bp = b->getBuffer(); unsigned char* bp = b->getBuffer();
memcpy(bp, (char*)data.c_str(), data.length()); memcpy(bp, data.c_str(), data.length());
dynamic_cast<QPDF_Stream*>(obj.getPointer())->replaceStreamData( dynamic_cast<QPDF_Stream*>(obj.getPointer())->replaceStreamData(
b, filter, decode_parms); b, filter, decode_parms);
} }
@ -690,7 +690,7 @@ QPDFObjectHandle::parse(std::string const& object_str,
bool empty = false; bool empty = false;
QPDFObjectHandle result = QPDFObjectHandle result =
parse(input, object_description, tokenizer, empty, 0, 0); parse(input, object_description, tokenizer, empty, 0, 0);
size_t offset = (size_t) input->tell(); size_t offset = input->tell();
while (offset < object_str.length()) while (offset < object_str.length())
{ {
if (! isspace(object_str[offset])) if (! isspace(object_str[offset]))
@ -748,7 +748,7 @@ QPDFObjectHandle::parseContentStream_internal(QPDFObjectHandle stream,
QPDFTokenizer tokenizer; QPDFTokenizer tokenizer;
tokenizer.allowEOF(); tokenizer.allowEOF();
bool empty = false; bool empty = false;
while ((size_t) input->tell() < length) while (static_cast<size_t>(input->tell()) < length)
{ {
QPDFObjectHandle obj = QPDFObjectHandle obj =
parseInternal(input, "content", tokenizer, empty, parseInternal(input, "content", tokenizer, empty,

View File

@ -80,7 +80,7 @@ QPDFTokenizer::resolveLiteral()
num[0] = p[1]; num[0] = p[1];
num[1] = p[2]; num[1] = p[2];
num[2] = '\0'; num[2] = '\0';
char ch = (char)(strtol(num, 0, 16)); char ch = static_cast<char>(strtol(num, 0, 16));
if (ch == '\0') if (ch == '\0')
{ {
type = tt_bad; type = tt_bad;
@ -273,7 +273,7 @@ QPDFTokenizer::presentCharacter(char ch)
{ {
// We've accumulated \ddd. PDF Spec says to ignore // We've accumulated \ddd. PDF Spec says to ignore
// high-order overflow. // high-order overflow.
val += (char) strtol(bs_num_register, 0, 8); val += static_cast<char>(strtol(bs_num_register, 0, 8));
memset(bs_num_register, '\0', sizeof(bs_num_register)); memset(bs_num_register, '\0', sizeof(bs_num_register));
bs_num_count = 0; bs_num_count = 0;
} }
@ -399,7 +399,7 @@ QPDFTokenizer::presentCharacter(char ch)
{ {
num[0] = val[i]; num[0] = val[i];
num[1] = val[i+1]; num[1] = val[i+1];
char nch = (char)(strtol(num, 0, 16)); char nch = static_cast<char>(strtol(num, 0, 16));
nval += nch; nval += nch;
} }
val = nval; val = nval;
@ -511,7 +511,7 @@ QPDFTokenizer::readToken(PointerHolder<InputSource> input,
} }
else else
{ {
if (is_space((unsigned char)ch) && if (is_space(static_cast<unsigned char>(ch)) &&
(input->getLastOffset() == offset)) (input->getLastOffset() == offset))
{ {
++offset; ++offset;

View File

@ -772,7 +772,7 @@ QPDFWriter::writeBinary(unsigned long long val, unsigned int bytes)
unsigned char data[sizeof(unsigned long long)]; unsigned char data[sizeof(unsigned long long)];
for (unsigned int i = 0; i < bytes; ++i) for (unsigned int i = 0; i < bytes; ++i)
{ {
data[bytes - i - 1] = (unsigned char)(val & 0xff); data[bytes - i - 1] = static_cast<unsigned char>(val & 0xff);
val >>= 8; val >>= 8;
} }
this->pipeline->write(data, bytes); this->pipeline->write(data, bytes);
@ -781,7 +781,7 @@ QPDFWriter::writeBinary(unsigned long long val, unsigned int bytes)
void void
QPDFWriter::writeString(std::string const& str) QPDFWriter::writeString(std::string const& str)
{ {
this->pipeline->write((unsigned char*)str.c_str(), str.length()); this->pipeline->write(QUtil::unsigned_char_pointer(str), str.length());
} }
void void
@ -887,14 +887,14 @@ QPDFWriter::pushEncryptionFilter()
{ {
p = new Pl_AES_PDF( p = new Pl_AES_PDF(
"aes stream encryption", this->pipeline, true, "aes stream encryption", this->pipeline, true,
(unsigned char*) this->cur_data_key.c_str(), QUtil::unsigned_char_pointer(this->cur_data_key),
(unsigned int)this->cur_data_key.length()); this->cur_data_key.length());
} }
else else
{ {
p = new Pl_RC4("rc4 stream encryption", this->pipeline, p = new Pl_RC4("rc4 stream encryption", this->pipeline,
(unsigned char*) this->cur_data_key.c_str(), QUtil::unsigned_char_pointer(this->cur_data_key),
(unsigned int)this->cur_data_key.length()); this->cur_data_key.length());
} }
pushPipeline(p); pushPipeline(p);
} }
@ -1087,7 +1087,7 @@ QPDFWriter::writeTrailer(trailer_e which, int size, bool xref_stream,
writeString(" /Prev "); writeString(" /Prev ");
qpdf_offset_t pos = this->pipeline->getCount(); qpdf_offset_t pos = this->pipeline->getCount();
writeString(QUtil::int_to_string(prev)); writeString(QUtil::int_to_string(prev));
int nspaces = (int)(pos - this->pipeline->getCount() + 21); int nspaces = pos - this->pipeline->getCount() + 21;
assert(nspaces >= 0); assert(nspaces >= 0);
writePad(nspaces); writePad(nspaces);
} }
@ -1504,14 +1504,15 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
if (this->encrypt_use_aes) if (this->encrypt_use_aes)
{ {
Pl_Buffer bufpl("encrypted string"); Pl_Buffer bufpl("encrypted string");
Pl_AES_PDF pl("aes encrypt string", &bufpl, true, Pl_AES_PDF pl(
(unsigned char const*)this->cur_data_key.c_str(), "aes encrypt string", &bufpl, true,
(unsigned int)this->cur_data_key.length()); QUtil::unsigned_char_pointer(this->cur_data_key),
pl.write((unsigned char*) val.c_str(), val.length()); this->cur_data_key.length());
pl.write(QUtil::unsigned_char_pointer(val), val.length());
pl.finish(); pl.finish();
Buffer* buf = bufpl.getBuffer(); Buffer* buf = bufpl.getBuffer();
val = QPDF_String( val = QPDF_String(
std::string((char*)buf->getBuffer(), std::string(reinterpret_cast<char*>(buf->getBuffer()),
buf->getSize())).unparse(true); buf->getSize())).unparse(true);
delete buf; delete buf;
} }
@ -1519,9 +1520,9 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
{ {
char* tmp = QUtil::copy_string(val); char* tmp = QUtil::copy_string(val);
size_t vlen = val.length(); size_t vlen = val.length();
RC4 rc4((unsigned char const*)this->cur_data_key.c_str(), RC4 rc4(QUtil::unsigned_char_pointer(this->cur_data_key),
(int)this->cur_data_key.length()); this->cur_data_key.length());
rc4.process((unsigned char*)tmp, (int)vlen); rc4.process(QUtil::unsigned_char_pointer(tmp), vlen);
val = QPDF_String(std::string(tmp, vlen)).unparse(); val = QPDF_String(std::string(tmp, vlen)).unparse();
delete [] tmp; delete [] tmp;
} }
@ -1788,7 +1789,7 @@ QPDFWriter::generateID()
0x23, 0x84, 0x62, 0x64, 0x23, 0x84, 0x62, 0x64,
0x33, 0x83, 0x27, 0x95, 0x33, 0x83, 0x27, 0x95,
0x00}; 0x00};
result = (char*)tmp; result = reinterpret_cast<char*>(tmp);
} }
else else
{ {
@ -1799,7 +1800,7 @@ QPDFWriter::generateID()
// the file yet. This scheme should be fine though. // the file yet. This scheme should be fine though.
std::string seed; std::string seed;
seed += QUtil::int_to_string((int)QUtil::get_current_time()); seed += QUtil::int_to_string(QUtil::get_current_time());
seed += " QPDF "; seed += " QPDF ";
seed += this->filename; seed += this->filename;
seed += " "; seed += " ";
@ -1823,7 +1824,8 @@ QPDFWriter::generateID()
m.encodeString(seed.c_str()); m.encodeString(seed.c_str());
MD5::Digest digest; MD5::Digest digest;
m.digest(digest); m.digest(digest);
result = std::string((char*)digest, sizeof(MD5::Digest)); result = std::string(reinterpret_cast<char*>(digest),
sizeof(MD5::Digest));
} }
// If /ID already exists, follow the spec: use the original first // If /ID already exists, follow the spec: use the original first
@ -1899,9 +1901,8 @@ QPDFWriter::generateObjectStreams()
// This code doesn't do anything with /Extends. // This code doesn't do anything with /Extends.
std::vector<int> const& eligible = this->pdf.getCompressibleObjects(); std::vector<int> const& eligible = this->pdf.getCompressibleObjects();
unsigned int n_object_streams = unsigned int n_object_streams = (eligible.size() + 99) / 100;
(unsigned int)((eligible.size() + 99) / 100); unsigned int n_per = eligible.size() / n_object_streams;
unsigned int n_per = (unsigned int)(eligible.size() / n_object_streams);
if (n_per * n_object_streams < eligible.size()) if (n_per * n_object_streams < eligible.size())
{ {
++n_per; ++n_per;
@ -2206,7 +2207,8 @@ QPDFWriter::write()
this->object_stream_to_objects[stream].insert(obj); this->object_stream_to_objects[stream].insert(obj);
this->max_ostream_index = this->max_ostream_index =
std::max(this->max_ostream_index, std::max(this->max_ostream_index,
(int)this->object_stream_to_objects[stream].size() - 1); static_cast<int>(
this->object_stream_to_objects[stream].size()) - 1);
} }
if (! this->object_stream_to_objects.empty()) if (! this->object_stream_to_objects.empty())
@ -2553,8 +2555,7 @@ QPDFWriter::writeLinearized()
// //
// Second half objects // Second half objects
int second_half_uncompressed = int second_half_uncompressed = part7.size() + part8.size() + part9.size();
(int)(part7.size() + part8.size() + part9.size());
int second_half_first_obj = 1; int second_half_first_obj = 1;
int after_second_half = 1 + second_half_uncompressed; int after_second_half = 1 + second_half_uncompressed;
this->next_objid = after_second_half; this->next_objid = after_second_half;
@ -2661,7 +2662,7 @@ QPDFWriter::writeLinearized()
{ {
std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
int first_page_object = obj_renumber[pages[0].getObjectID()]; int first_page_object = obj_renumber[pages[0].getObjectID()];
int npages = (int)pages.size(); int npages = pages.size();
writeString(" /Linearized 1 /L "); writeString(" /Linearized 1 /L ");
writeString(QUtil::int_to_string(file_size + hint_length)); writeString(QUtil::int_to_string(file_size + hint_length));
@ -2821,7 +2822,7 @@ QPDFWriter::writeLinearized()
// If this assertion fails, maybe we didn't have // If this assertion fails, maybe we didn't have
// enough padding above. // enough padding above.
assert(this->pipeline->getCount() == assert(this->pipeline->getCount() ==
(qpdf_offset_t)(second_xref_end + hint_length)); second_xref_end + hint_length);
} }
} }
else else
@ -2849,7 +2850,7 @@ QPDFWriter::writeLinearized()
activatePipelineStack(); activatePipelineStack();
writeHintStream(hint_id); writeHintStream(hint_id);
popPipelineStack(&hint_buffer); popPipelineStack(&hint_buffer);
hint_length = (qpdf_offset_t)hint_buffer->getSize(); hint_length = hint_buffer->getSize();
// Restore hint offset // Restore hint offset
this->xref[hint_id] = QPDFXRefEntry(1, hint_offset, 0); this->xref[hint_id] = QPDFXRefEntry(1, hint_offset, 0);

View File

@ -46,7 +46,7 @@ QPDFXRefEntry::getObjStreamNumber() const
throw std::logic_error( throw std::logic_error(
"getObjStreamNumber called for xref entry of type != 2"); "getObjStreamNumber called for xref entry of type != 2");
} }
return (int) this->field1; return this->field1;
} }
int int

View File

@ -49,13 +49,13 @@ QPDF_Array::getTypeName() const
int int
QPDF_Array::getNItems() const QPDF_Array::getNItems() const
{ {
return (int)this->items.size(); return this->items.size();
} }
QPDFObjectHandle QPDFObjectHandle
QPDF_Array::getItem(int n) const QPDF_Array::getItem(int n) const
{ {
if ((n < 0) || (n >= (int)this->items.size())) if ((n < 0) || (n >= static_cast<int>(this->items.size())))
{ {
throw std::logic_error( throw std::logic_error(
"INTERNAL ERROR: bounds error accessing QPDF_Array element"); "INTERNAL ERROR: bounds error accessing QPDF_Array element");
@ -87,7 +87,7 @@ void
QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
{ {
// As special case, also allow insert beyond the end // As special case, also allow insert beyond the end
if ((at < 0) || (at > (int)this->items.size())) if ((at < 0) || (at > static_cast<int>(this->items.size())))
{ {
throw std::logic_error( throw std::logic_error(
"INTERNAL ERROR: bounds error accessing QPDF_Array element"); "INTERNAL ERROR: bounds error accessing QPDF_Array element");

View File

@ -519,7 +519,7 @@ QPDF_Stream::replaceFilterData(QPDFObjectHandle const& filter,
else else
{ {
this->stream_dict.replaceKey( this->stream_dict.replaceKey(
"/Length", QPDFObjectHandle::newInteger((int)length)); "/Length", QPDFObjectHandle::newInteger(length));
} }
} }

View File

@ -140,7 +140,7 @@ QPDF_String::unparse(bool force_binary)
} }
else else
{ {
sprintf(num, "\\%03o", (unsigned char)ch); sprintf(num, "\\%03o", static_cast<unsigned char>(ch));
result += num; result += num;
} }
break; break;
@ -181,8 +181,8 @@ QPDF_String::getUTF8Val() const
// discarded, and a low codepoint not preceded by a high // discarded, and a low codepoint not preceded by a high
// codepoint will just get its low 10 bits output. // codepoint will just get its low 10 bits output.
unsigned short bits = unsigned short bits =
(((unsigned char) this->val[i]) << 8) + (static_cast<unsigned char>(this->val[i]) << 8) +
((unsigned char) this->val[i+1]); static_cast<unsigned char>(this->val[i+1]);
if ((bits & 0xFC00) == 0xD800) if ((bits & 0xFC00) == 0xD800)
{ {
codepoint = 0x10000 + ((bits & 0x3FF) << 10); codepoint = 0x10000 + ((bits & 0x3FF) << 10);
@ -209,7 +209,7 @@ QPDF_String::getUTF8Val() const
{ {
for (unsigned int i = 0; i < len; ++i) for (unsigned int i = 0; i < len; ++i)
{ {
result += QUtil::toUTF8((unsigned char) this->val[i]); result += QUtil::toUTF8(static_cast<unsigned char>(this->val[i]));
} }
} }
return result; return result;

View File

@ -129,7 +129,8 @@ QPDF::EncryptionData::setV5EncryptionParameters(
static void static void
pad_or_truncate_password_V4(std::string const& password, char k1[key_bytes]) pad_or_truncate_password_V4(std::string const& password, char k1[key_bytes])
{ {
int password_bytes = std::min(key_bytes, (unsigned int)password.length()); int password_bytes = std::min(static_cast<size_t>(key_bytes),
password.length());
int pad_bytes = key_bytes - password_bytes; int pad_bytes = key_bytes - password_bytes;
memcpy(k1, password.c_str(), password_bytes); memcpy(k1, password.c_str(), password_bytes);
memcpy(k1 + password_bytes, padding_string, pad_bytes); memcpy(k1 + password_bytes, padding_string, pad_bytes);
@ -176,7 +177,8 @@ pad_or_truncate_password_V4(std::string const& password)
static std::string static std::string
truncate_password_V5(std::string const& password) truncate_password_V5(std::string const& password)
{ {
return password.substr(0, std::min((size_t)127, password.length())); return password.substr(
0, std::min(static_cast<size_t>(127), password.length()));
} }
static void static void
@ -187,7 +189,8 @@ iterate_md5_digest(MD5& md5, MD5::Digest& digest, int iterations)
for (int i = 0; i < iterations; ++i) for (int i = 0; i < iterations; ++i)
{ {
MD5 m; MD5 m;
m.encodeDataIncrementally((char*)digest, sizeof(digest)); m.encodeDataIncrementally(reinterpret_cast<char*>(digest),
sizeof(digest));
m.digest(digest); m.digest(digest);
} }
} }
@ -223,8 +226,8 @@ process_with_aes(std::string const& key,
{ {
Pl_Buffer buffer("buffer"); Pl_Buffer buffer("buffer");
Pl_AES_PDF aes("aes", &buffer, encrypt, Pl_AES_PDF aes("aes", &buffer, encrypt,
(unsigned char const*)key.c_str(), QUtil::unsigned_char_pointer(key),
(unsigned int)key.length()); key.length());
if (iv) if (iv)
{ {
aes.setIV(iv, iv_length); aes.setIV(iv, iv_length);
@ -236,7 +239,7 @@ process_with_aes(std::string const& key,
aes.disablePadding(); aes.disablePadding();
for (unsigned int i = 0; i < repetitions; ++i) for (unsigned int i = 0; i < repetitions; ++i)
{ {
aes.write((unsigned char*)data.c_str(), data.length()); aes.write(QUtil::unsigned_char_pointer(data), data.length());
} }
aes.finish(); aes.finish();
PointerHolder<Buffer> bufp = buffer.getBuffer(); PointerHolder<Buffer> bufp = buffer.getBuffer();
@ -248,7 +251,7 @@ process_with_aes(std::string const& key,
{ {
outlength = std::min(outlength, bufp->getSize()); outlength = std::min(outlength, bufp->getSize());
} }
return std::string((char const*)bufp->getBuffer(), outlength); return std::string(reinterpret_cast<char*>(bufp->getBuffer()), outlength);
} }
static std::string static std::string
@ -258,9 +261,9 @@ hash_V5(std::string const& password,
QPDF::EncryptionData const& data) QPDF::EncryptionData const& data)
{ {
Pl_SHA2 hash(256); Pl_SHA2 hash(256);
hash.write((unsigned char*)password.c_str(), password.length()); hash.write(QUtil::unsigned_char_pointer(password), password.length());
hash.write((unsigned char*)salt.c_str(), salt.length()); hash.write(QUtil::unsigned_char_pointer(salt), salt.length());
hash.write((unsigned char*)udata.c_str(), udata.length()); hash.write(QUtil::unsigned_char_pointer(udata), udata.length());
hash.finish(); hash.finish();
std::string K = hash.getRawDigest(); std::string K = hash.getRawDigest();
@ -299,7 +302,7 @@ hash_V5(std::string const& password,
assert(K.length() >= 32); assert(K.length() >= 32);
std::string E = process_with_aes( std::string E = process_with_aes(
K.substr(0, 16), true, K1, 0, 64, K.substr(0, 16), true, K1, 0, 64,
(unsigned char*)K.substr(16, 16).c_str(), 16); QUtil::unsigned_char_pointer(K.substr(16, 16)), 16);
// E_mod_3 is supposed to be mod 3 of the first 16 bytes // E_mod_3 is supposed to be mod 3 of the first 16 bytes
// of E taken as as a (128-bit) big-endian number. Since // of E taken as as a (128-bit) big-endian number. Since
@ -309,22 +312,22 @@ hash_V5(std::string const& password,
int E_mod_3 = 0; int E_mod_3 = 0;
for (unsigned int i = 0; i < 16; ++i) for (unsigned int i = 0; i < 16; ++i)
{ {
E_mod_3 += (unsigned char)E[i]; E_mod_3 += static_cast<unsigned char>(E[i]);
} }
E_mod_3 %= 3; E_mod_3 %= 3;
int next_hash = ((E_mod_3 == 0) ? 256 : int next_hash = ((E_mod_3 == 0) ? 256 :
(E_mod_3 == 1) ? 384 : (E_mod_3 == 1) ? 384 :
512); 512);
Pl_SHA2 hash(next_hash); Pl_SHA2 hash(next_hash);
hash.write((unsigned char*)E.c_str(), E.length()); hash.write(QUtil::unsigned_char_pointer(E), E.length());
hash.finish(); hash.finish();
K = hash.getRawDigest(); K = hash.getRawDigest();
if (round_number >= 64) if (round_number >= 64)
{ {
unsigned int ch = (unsigned int)((unsigned char) *(E.rbegin())); unsigned int ch = static_cast<unsigned char>(*(E.rbegin()));
if (ch <= (unsigned int)(round_number - 32)) if (ch <= static_cast<unsigned int>(round_number - 32))
{ {
done = true; done = true;
} }
@ -353,22 +356,22 @@ QPDF::compute_data_key(std::string const& encryption_key,
} }
// Append low three bytes of object ID and low two bytes of generation // Append low three bytes of object ID and low two bytes of generation
result += (char) (objid & 0xff); result += static_cast<char>(objid & 0xff);
result += (char) ((objid >> 8) & 0xff); result += static_cast<char>((objid >> 8) & 0xff);
result += (char) ((objid >> 16) & 0xff); result += static_cast<char>((objid >> 16) & 0xff);
result += (char) (generation & 0xff); result += static_cast<char>(generation & 0xff);
result += (char) ((generation >> 8) & 0xff); result += static_cast<char>((generation >> 8) & 0xff);
if (use_aes) if (use_aes)
{ {
result += "sAlT"; result += "sAlT";
} }
MD5 md5; MD5 md5;
md5.encodeDataIncrementally(result.c_str(), (int)result.length()); md5.encodeDataIncrementally(result.c_str(), result.length());
MD5::Digest digest; MD5::Digest digest;
md5.digest(digest); md5.digest(digest);
return std::string((char*) digest, return std::string(reinterpret_cast<char*>(digest),
std::min(result.length(), (size_t) 16)); std::min(result.length(), static_cast<size_t>(16)));
} }
std::string std::string
@ -409,13 +412,13 @@ QPDF::compute_encryption_key_from_password(
md5.encodeDataIncrementally(data.getO().c_str(), key_bytes); md5.encodeDataIncrementally(data.getO().c_str(), key_bytes);
char pbytes[4]; char pbytes[4];
int P = data.getP(); int P = data.getP();
pbytes[0] = (char) (P & 0xff); pbytes[0] = static_cast<char>(P & 0xff);
pbytes[1] = (char) ((P >> 8) & 0xff); pbytes[1] = static_cast<char>((P >> 8) & 0xff);
pbytes[2] = (char) ((P >> 16) & 0xff); pbytes[2] = static_cast<char>((P >> 16) & 0xff);
pbytes[3] = (char) ((P >> 24) & 0xff); pbytes[3] = static_cast<char>((P >> 24) & 0xff);
md5.encodeDataIncrementally(pbytes, 4); md5.encodeDataIncrementally(pbytes, 4);
md5.encodeDataIncrementally(data.getId1().c_str(), md5.encodeDataIncrementally(data.getId1().c_str(),
(int)data.getId1().length()); data.getId1().length());
if ((data.getR() >= 4) && (! data.getEncryptMetadata())) if ((data.getR() >= 4) && (! data.getEncryptMetadata()))
{ {
char bytes[4]; char bytes[4];
@ -424,7 +427,7 @@ QPDF::compute_encryption_key_from_password(
} }
MD5::Digest digest; MD5::Digest digest;
iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0)); iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0));
return std::string((char*)digest, data.getLengthBytes()); return std::string(reinterpret_cast<char*>(digest), data.getLengthBytes());
} }
static void static void
@ -463,7 +466,7 @@ compute_O_value(std::string const& user_password,
char upass[key_bytes]; char upass[key_bytes];
pad_or_truncate_password_V4(user_password, upass); pad_or_truncate_password_V4(user_password, upass);
iterate_rc4((unsigned char*) upass, key_bytes, iterate_rc4(QUtil::unsigned_char_pointer(upass), key_bytes,
O_key, data.getLengthBytes(), O_key, data.getLengthBytes(),
(data.getR() >= 3) ? 20 : 1, false); (data.getR() >= 3) ? 20 : 1, false);
return std::string(upass, key_bytes); return std::string(upass, key_bytes);
@ -479,8 +482,9 @@ compute_U_value_R2(std::string const& user_password,
std::string k1 = QPDF::compute_encryption_key(user_password, data); std::string k1 = QPDF::compute_encryption_key(user_password, data);
char udata[key_bytes]; char udata[key_bytes];
pad_or_truncate_password_V4("", udata); pad_or_truncate_password_V4("", udata);
iterate_rc4((unsigned char*) udata, key_bytes, iterate_rc4(QUtil::unsigned_char_pointer(udata), key_bytes,
(unsigned char*)k1.c_str(), data.getLengthBytes(), 1, false); QUtil::unsigned_char_pointer(k1),
data.getLengthBytes(), 1, false);
return std::string(udata, key_bytes); return std::string(udata, key_bytes);
} }
@ -496,18 +500,19 @@ compute_U_value_R3(std::string const& user_password,
md5.encodeDataIncrementally( md5.encodeDataIncrementally(
pad_or_truncate_password_V4("").c_str(), key_bytes); pad_or_truncate_password_V4("").c_str(), key_bytes);
md5.encodeDataIncrementally(data.getId1().c_str(), md5.encodeDataIncrementally(data.getId1().c_str(),
(int)data.getId1().length()); data.getId1().length());
MD5::Digest digest; MD5::Digest digest;
md5.digest(digest); md5.digest(digest);
iterate_rc4(digest, sizeof(MD5::Digest), iterate_rc4(digest, sizeof(MD5::Digest),
(unsigned char*) k1.c_str(), data.getLengthBytes(), 20, false); QUtil::unsigned_char_pointer(k1),
data.getLengthBytes(), 20, false);
char result[key_bytes]; char result[key_bytes];
memcpy(result, digest, sizeof(MD5::Digest)); memcpy(result, digest, sizeof(MD5::Digest));
// pad with arbitrary data -- make it consistent for the sake of // pad with arbitrary data -- make it consistent for the sake of
// testing // testing
for (unsigned int i = sizeof(MD5::Digest); i < key_bytes; ++i) for (unsigned int i = sizeof(MD5::Digest); i < key_bytes; ++i)
{ {
result[i] = (char)((i * i) % 0xff); result[i] = static_cast<char>((i * i) % 0xff);
} }
return std::string(result, key_bytes); return std::string(result, key_bytes);
} }
@ -572,11 +577,11 @@ check_owner_password_V4(std::string& user_password,
unsigned char key[OU_key_bytes_V4]; unsigned char key[OU_key_bytes_V4];
compute_O_rc4_key(user_password, owner_password, data, key); compute_O_rc4_key(user_password, owner_password, data, key);
unsigned char O_data[key_bytes]; unsigned char O_data[key_bytes];
memcpy(O_data, (unsigned char*) data.getO().c_str(), key_bytes); memcpy(O_data, QUtil::unsigned_char_pointer(data.getO()), key_bytes);
iterate_rc4(O_data, key_bytes, key, data.getLengthBytes(), iterate_rc4(O_data, key_bytes, key, data.getLengthBytes(),
(data.getR() >= 3) ? 20 : 1, true); (data.getR() >= 3) ? 20 : 1, true);
std::string new_user_password = std::string new_user_password =
std::string((char*)O_data, key_bytes); std::string(reinterpret_cast<char*>(O_data), key_bytes);
bool result = false; bool result = false;
if (check_user_password(new_user_password, data)) if (check_user_password(new_user_password, data))
{ {
@ -632,7 +637,8 @@ compute_U_UE_value_V5(std::string const& user_password,
{ {
// Algorithm 3.8 from the PDF 1.7 extension level 3 // Algorithm 3.8 from the PDF 1.7 extension level 3
char k[16]; char k[16];
QUtil::initializeWithRandomBytes((unsigned char*) k, sizeof(k)); QUtil::initializeWithRandomBytes(
QUtil::unsigned_char_pointer(k), sizeof(k));
std::string validation_salt(k, 8); std::string validation_salt(k, 8);
std::string key_salt(k + 8, 8); std::string key_salt(k + 8, 8);
U = hash_V5(user_password, validation_salt, "", data) + U = hash_V5(user_password, validation_salt, "", data) +
@ -650,7 +656,8 @@ compute_O_OE_value_V5(std::string const& owner_password,
{ {
// Algorithm 3.9 from the PDF 1.7 extension level 3 // Algorithm 3.9 from the PDF 1.7 extension level 3
char k[16]; char k[16];
QUtil::initializeWithRandomBytes((unsigned char*) k, sizeof(k)); QUtil::initializeWithRandomBytes(
QUtil::unsigned_char_pointer(k), sizeof(k));
std::string validation_salt(k, 8); std::string validation_salt(k, 8);
std::string key_salt(k + 8, 8); std::string key_salt(k + 8, 8);
O = hash_V5(owner_password, validation_salt, U, data) + O = hash_V5(owner_password, validation_salt, U, data) +
@ -668,7 +675,7 @@ compute_Perms_value_V5_clear(std::string const& encryption_key,
unsigned long long extended_perms = 0xffffffff00000000LL | data.getP(); unsigned long long extended_perms = 0xffffffff00000000LL | data.getP();
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
{ {
k[i] = (unsigned char) (extended_perms & 0xff); k[i] = static_cast<unsigned char>(extended_perms & 0xff);
extended_perms >>= 8; extended_perms >>= 8;
} }
k[8] = data.getEncryptMetadata() ? 'T' : 'F'; k[8] = data.getEncryptMetadata() ? 'T' : 'F';
@ -685,8 +692,9 @@ compute_Perms_value_V5(std::string const& encryption_key,
// Algorithm 3.10 from the PDF 1.7 extension level 3 // Algorithm 3.10 from the PDF 1.7 extension level 3
unsigned char k[16]; unsigned char k[16];
compute_Perms_value_V5_clear(encryption_key, data, k); compute_Perms_value_V5_clear(encryption_key, data, k);
return process_with_aes(encryption_key, true, return process_with_aes(
std::string((char const*) k, sizeof(k))); encryption_key, true,
std::string(reinterpret_cast<char*>(k), sizeof(k)));
} }
std::string std::string
@ -834,7 +842,7 @@ QPDF::initializeEncryption()
int R = encryption_dict.getKey("/R").getIntValue(); int R = encryption_dict.getKey("/R").getIntValue();
std::string O = encryption_dict.getKey("/O").getStringValue(); std::string O = encryption_dict.getKey("/O").getStringValue();
std::string U = encryption_dict.getKey("/U").getStringValue(); std::string U = encryption_dict.getKey("/U").getStringValue();
unsigned int P = (unsigned int) encryption_dict.getKey("/P").getIntValue(); unsigned int P = encryption_dict.getKey("/P").getIntValue();
// If supporting new encryption R/V values, remember to update // If supporting new encryption R/V values, remember to update
// error message inside this if statement. // error message inside this if statement.
@ -1084,22 +1092,23 @@ QPDF::decryptString(std::string& str, int objid, int generation)
QTC::TC("qpdf", "QPDF_encryption aes decode string"); QTC::TC("qpdf", "QPDF_encryption aes decode string");
Pl_Buffer bufpl("decrypted string"); Pl_Buffer bufpl("decrypted string");
Pl_AES_PDF pl("aes decrypt string", &bufpl, false, Pl_AES_PDF pl("aes decrypt string", &bufpl, false,
(unsigned char const*)key.c_str(), QUtil::unsigned_char_pointer(key),
(unsigned int)key.length()); key.length());
pl.write((unsigned char*)str.c_str(), str.length()); pl.write(QUtil::unsigned_char_pointer(str), str.length());
pl.finish(); pl.finish();
PointerHolder<Buffer> buf = bufpl.getBuffer(); PointerHolder<Buffer> buf = bufpl.getBuffer();
str = std::string((char*)buf->getBuffer(), buf->getSize()); str = std::string(reinterpret_cast<char*>(buf->getBuffer()),
buf->getSize());
} }
else else
{ {
QTC::TC("qpdf", "QPDF_encryption rc4 decode string"); QTC::TC("qpdf", "QPDF_encryption rc4 decode string");
unsigned int vlen = (int)str.length(); unsigned int vlen = str.length();
// Using PointerHolder guarantees that tmp will // Using PointerHolder guarantees that tmp will
// be freed even if rc4.process throws an exception. // be freed even if rc4.process throws an exception.
PointerHolder<char> tmp(true, QUtil::copy_string(str)); PointerHolder<char> tmp(true, QUtil::copy_string(str));
RC4 rc4((unsigned char const*)key.c_str(), (int)key.length()); RC4 rc4(QUtil::unsigned_char_pointer(key), key.length());
rc4.process((unsigned char*)tmp.getPointer(), vlen); rc4.process(QUtil::unsigned_char_pointer(tmp.getPointer()), vlen);
str = std::string(tmp.getPointer(), vlen); str = std::string(tmp.getPointer(), vlen);
} }
} }
@ -1240,15 +1249,15 @@ QPDF::decryptStream(Pipeline*& pipeline, int objid, int generation,
{ {
QTC::TC("qpdf", "QPDF_encryption aes decode stream"); QTC::TC("qpdf", "QPDF_encryption aes decode stream");
pipeline = new Pl_AES_PDF("AES stream decryption", pipeline, pipeline = new Pl_AES_PDF("AES stream decryption", pipeline,
false, (unsigned char*) key.c_str(), false, QUtil::unsigned_char_pointer(key),
(unsigned int) key.length()); key.length());
} }
else else
{ {
QTC::TC("qpdf", "QPDF_encryption rc4 decode stream"); QTC::TC("qpdf", "QPDF_encryption rc4 decode stream");
pipeline = new Pl_RC4("RC4 stream decryption", pipeline, pipeline = new Pl_RC4("RC4 stream decryption", pipeline,
(unsigned char*) key.c_str(), QUtil::unsigned_char_pointer(key),
(unsigned int) key.length()); key.length());
} }
heap.push_back(pipeline); heap.push_back(pipeline);
} }
@ -1285,7 +1294,7 @@ QPDF::compute_encryption_parameters_V5(
id1, encrypt_metadata); id1, encrypt_metadata);
unsigned char k[key_bytes]; unsigned char k[key_bytes];
QUtil::initializeWithRandomBytes(k, key_bytes); QUtil::initializeWithRandomBytes(k, key_bytes);
encryption_key = std::string((char const*)k, key_bytes); encryption_key = std::string(reinterpret_cast<char*>(k), key_bytes);
compute_U_UE_value_V5(user_password, encryption_key, data, U, UE); compute_U_UE_value_V5(user_password, encryption_key, data, U, UE);
compute_O_OE_value_V5(owner_password, encryption_key, data, U, O, OE); compute_O_OE_value_V5(owner_password, encryption_key, data, U, O, OE);
Perms = compute_Perms_value_V5(encryption_key, data); Perms = compute_Perms_value_V5(encryption_key, data);

View File

@ -109,7 +109,7 @@ QPDF::isLinearized()
} }
else else
{ {
p = (char*)memchr(p, '\0', tbuf_size - (p - buf)); p = reinterpret_cast<char*>(memchr(p, '\0', tbuf_size - (p - buf)));
assert(p != 0); assert(p != 0);
while ((p - buf < tbuf_size) && (*p == 0)) while ((p - buf < tbuf_size) && (*p == 0))
{ {
@ -136,7 +136,8 @@ QPDF::isLinearized()
} }
QPDFObjectHandle linkey = candidate.getKey("/Linearized"); QPDFObjectHandle linkey = candidate.getKey("/Linearized");
if (! (linkey.isNumber() && ((int)floor(linkey.getNumericValue()) == 1))) if (! (linkey.isNumber() &&
(static_cast<int>(floor(linkey.getNumericValue())) == 1)))
{ {
return false; return false;
} }
@ -289,7 +290,7 @@ QPDF::readLinearizationData()
PointerHolder<Buffer> hbp = pb.getBuffer(); PointerHolder<Buffer> hbp = pb.getBuffer();
Buffer* hb = hbp.getPointer(); Buffer* hb = hbp.getPointer();
unsigned char const* h_buf = hb->getBuffer(); unsigned char const* h_buf = hb->getBuffer();
int h_size = (int)hb->getSize(); int h_size = hb->getSize();
readHPageOffset(BitStream(h_buf, h_size)); readHPageOffset(BitStream(h_buf, h_size));
@ -345,7 +346,7 @@ QPDF::readHintStream(Pipeline& pl, qpdf_offset_t offset, size_t length)
{ {
QTC::TC("qpdf", "QPDF hint table length direct"); QTC::TC("qpdf", "QPDF hint table length direct");
} }
qpdf_offset_t computed_end = offset + (qpdf_offset_t)length; qpdf_offset_t computed_end = offset + length;
if ((computed_end < min_end_offset) || if ((computed_end < min_end_offset) ||
(computed_end > max_end_offset)) (computed_end > max_end_offset))
{ {
@ -488,7 +489,7 @@ QPDF::checkLinearizationInternal()
} }
// N: number of pages // N: number of pages
int npages = (int)pages.size(); int npages = pages.size();
if (p.npages != npages) if (p.npages != npages)
{ {
// Not tested in the test suite // Not tested in the test suite
@ -576,8 +577,8 @@ QPDF::checkLinearizationInternal()
// contain any files with threads. // contain any files with threads.
assert(! this->part6.empty()); assert(! this->part6.empty());
int min_E = -1; qpdf_offset_t min_E = -1;
int max_E = -1; qpdf_offset_t max_E = -1;
for (std::vector<QPDFObjectHandle>::iterator iter = this->part6.begin(); for (std::vector<QPDFObjectHandle>::iterator iter = this->part6.begin();
iter != this->part6.end(); ++iter) iter != this->part6.end(); ++iter)
{ {
@ -585,8 +586,8 @@ QPDF::checkLinearizationInternal()
// All objects have to have been dereferenced to be classified. // All objects have to have been dereferenced to be classified.
assert(this->obj_cache.count(og) > 0); assert(this->obj_cache.count(og) > 0);
ObjCache const& oc = this->obj_cache[og]; ObjCache const& oc = this->obj_cache[og];
min_E = std::max(min_E, (int)oc.end_before_space); min_E = std::max(min_E, oc.end_before_space);
max_E = std::max(max_E, (int)oc.end_after_space); max_E = std::max(max_E, oc.end_after_space);
} }
if ((p.first_page_end < min_E) || (p.first_page_end > max_E)) if ((p.first_page_end < min_E) || (p.first_page_end > max_E))
{ {
@ -632,19 +633,18 @@ QPDF::checkLinearizationInternal()
return result; return result;
} }
int qpdf_offset_t
QPDF::maxEnd(ObjUser const& ou) QPDF::maxEnd(ObjUser const& ou)
{ {
assert(this->obj_user_to_objects.count(ou) > 0); assert(this->obj_user_to_objects.count(ou) > 0);
std::set<ObjGen> const& ogs = this->obj_user_to_objects[ou]; std::set<ObjGen> const& ogs = this->obj_user_to_objects[ou];
int end = 0; qpdf_offset_t end = 0;
for (std::set<ObjGen>::const_iterator iter = ogs.begin(); for (std::set<ObjGen>::const_iterator iter = ogs.begin();
iter != ogs.end(); ++iter) iter != ogs.end(); ++iter)
{ {
ObjGen const& og = *iter; ObjGen const& og = *iter;
assert(this->obj_cache.count(og) > 0); assert(this->obj_cache.count(og) > 0);
end = std::max( end = std::max(end, this->obj_cache[og].end_after_space);
end, (int)(this->obj_cache[og].end_after_space));
} }
return end; return end;
} }
@ -736,7 +736,7 @@ QPDF::checkHPageOffset(std::list<std::string>& errors,
// under a page's /Resources dictionary in with shared objects // under a page's /Resources dictionary in with shared objects
// even when they are private. // even when they are private.
unsigned int npages = (unsigned int)pages.size(); unsigned int npages = pages.size();
int table_offset = adjusted_offset( int table_offset = adjusted_offset(
this->page_offset_hints.first_page_offset); this->page_offset_hints.first_page_offset);
ObjGen first_page_og(pages[0].getObjectID(), pages[0].getGeneration()); ObjGen first_page_og(pages[0].getObjectID(), pages[0].getGeneration());
@ -1435,7 +1435,7 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
pages.push_back(getUncompressedObject(*iter, object_stream_data)); pages.push_back(getUncompressedObject(*iter, object_stream_data));
} }
} }
unsigned int npages = (unsigned int)pages.size(); unsigned int npages = pages.size();
// We will be initializing some values of the computed hint // We will be initializing some values of the computed hint
// tables. Specifically, we can initialize any items that deal // tables. Specifically, we can initialize any items that deal
@ -1505,7 +1505,7 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
// in garbage values for all the shared object identifiers on the // in garbage values for all the shared object identifiers on the
// first page. // first page.
this->c_page_offset_data.entries[0].nobjects = (int)this->part6.size(); this->c_page_offset_data.entries[0].nobjects = this->part6.size();
// Part 7: other pages' private objects // Part 7: other pages' private objects
@ -1657,10 +1657,9 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
// Make sure we got everything exactly once. // Make sure we got everything exactly once.
unsigned int num_placed = unsigned int num_placed =
(unsigned int)(this->part4.size() + this->part6.size() + this->part4.size() + this->part6.size() + this->part7.size() +
this->part7.size() + this->part8.size() + this->part8.size() + this->part9.size();
this->part9.size()); unsigned int num_wanted = this->object_to_obj_users.size();
unsigned int num_wanted = (unsigned int)this->object_to_obj_users.size();
if (num_placed != num_wanted) if (num_placed != num_wanted)
{ {
throw std::logic_error( throw std::logic_error(
@ -1684,11 +1683,9 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
// can map from object number only without regards to generation. // can map from object number only without regards to generation.
std::map<int, int> obj_to_index; std::map<int, int> obj_to_index;
this->c_shared_object_data.nshared_first_page = this->c_shared_object_data.nshared_first_page = this->part6.size();
(unsigned int)this->part6.size();
this->c_shared_object_data.nshared_total = this->c_shared_object_data.nshared_total =
this->c_shared_object_data.nshared_first_page + this->c_shared_object_data.nshared_first_page + this->part8.size();
(unsigned int) this->part8.size();
std::vector<CHSharedObjectEntry>& shared = std::vector<CHSharedObjectEntry>& shared =
this->c_shared_object_data.entries; this->c_shared_object_data.entries;
@ -1697,7 +1694,7 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
{ {
QPDFObjectHandle& oh = *iter; QPDFObjectHandle& oh = *iter;
int obj = oh.getObjectID(); int obj = oh.getObjectID();
obj_to_index[obj] = (int)shared.size(); obj_to_index[obj] = shared.size();
shared.push_back(CHSharedObjectEntry(obj)); shared.push_back(CHSharedObjectEntry(obj));
} }
QTC::TC("qpdf", "QPDF lin part 8 empty", this->part8.empty() ? 1 : 0); QTC::TC("qpdf", "QPDF lin part 8 empty", this->part8.empty() ? 1 : 0);
@ -1711,12 +1708,12 @@ QPDF::calculateLinearizationData(std::map<int, int> const& object_stream_data)
{ {
QPDFObjectHandle& oh = *iter; QPDFObjectHandle& oh = *iter;
int obj = oh.getObjectID(); int obj = oh.getObjectID();
obj_to_index[obj] = (int)shared.size(); obj_to_index[obj] = shared.size();
shared.push_back(CHSharedObjectEntry(obj)); shared.push_back(CHSharedObjectEntry(obj));
} }
} }
assert(this->c_shared_object_data.nshared_total == assert(static_cast<size_t>(this->c_shared_object_data.nshared_total) ==
(int) this->c_shared_object_data.entries.size()); this->c_shared_object_data.entries.size());
// Now compute the list of shared objects for each page after the // Now compute the list of shared objects for each page after the
// first page. // first page.
@ -1827,7 +1824,7 @@ QPDF::calculateHPageOffset(
// values. // values.
std::vector<QPDFObjectHandle> const& pages = getAllPages(); std::vector<QPDFObjectHandle> const& pages = getAllPages();
unsigned int npages = (unsigned int)pages.size(); unsigned int npages = pages.size();
CHPageOffset& cph = this->c_page_offset_data; CHPageOffset& cph = this->c_page_offset_data;
std::vector<CHPageOffsetEntry>& cphe = cph.entries; std::vector<CHPageOffsetEntry>& cphe = cph.entries;
@ -2032,7 +2029,7 @@ QPDF::writeHPageOffset(BitWriter& w)
w.writeBits(t.nbits_shared_numerator, 16); // 12 w.writeBits(t.nbits_shared_numerator, 16); // 12
w.writeBits(t.shared_denominator, 16); // 13 w.writeBits(t.shared_denominator, 16); // 13
unsigned int nitems = (unsigned int)getAllPages().size(); unsigned int nitems = getAllPages().size();
std::vector<HPageOffsetEntry>& entries = t.entries; std::vector<HPageOffsetEntry>& entries = t.entries;
write_vector_int(w, nitems, entries, write_vector_int(w, nitems, entries,
@ -2123,12 +2120,12 @@ QPDF::generateHintStream(std::map<int, QPDFXRefEntry> const& xref,
BitWriter w(&c); BitWriter w(&c);
writeHPageOffset(w); writeHPageOffset(w);
S = (int)c.getCount(); S = c.getCount();
writeHSharedObject(w); writeHSharedObject(w);
O = 0; O = 0;
if (this->outline_hints.nobjects > 0) if (this->outline_hints.nobjects > 0)
{ {
O = (int)c.getCount(); O = c.getCount();
writeHGeneric(w, this->outline_hints); writeHGeneric(w, this->outline_hints);
} }
c.finish(); c.finish();

View File

@ -73,7 +73,7 @@ QPDF::optimize(std::map<int, int> const& object_stream_data,
pushInheritedAttributesToPage(allow_changes, false); pushInheritedAttributesToPage(allow_changes, false);
// Traverse pages // Traverse pages
int n = (int)this->all_pages.size(); int n = this->all_pages.size();
for (int pageno = 0; pageno < n; ++pageno) for (int pageno = 0; pageno < n; ++pageno)
{ {
updateObjectMaps(ObjUser(ObjUser::ou_page, pageno), updateObjectMaps(ObjUser(ObjUser::ou_page, pageno),

View File

@ -110,7 +110,7 @@ QPDF::flattenPagesTree()
QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle pages = getRoot().getKey("/Pages");
int const len = (int)this->all_pages.size(); int const len = this->all_pages.size();
for (int pos = 0; pos < len; ++pos) for (int pos = 0; pos < len; ++pos)
{ {
// populate pageobj_to_pages_pos and fix parent pointer // populate pageobj_to_pages_pos and fix parent pointer
@ -175,25 +175,26 @@ QPDF::insertPage(QPDFObjectHandle newpage, int pos)
QTC::TC("qpdf", "QPDF insert page", QTC::TC("qpdf", "QPDF insert page",
(pos == 0) ? 0 : // insert at beginning (pos == 0) ? 0 : // insert at beginning
(pos == ((int)this->all_pages.size())) ? 1 : // insert at end (pos == static_cast<int>(this->all_pages.size())) ? 1 : // at end
2); // insert in middle 2); // insert in middle
QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle pages = getRoot().getKey("/Pages");
QPDFObjectHandle kids = pages.getKey("/Kids"); QPDFObjectHandle kids = pages.getKey("/Kids");
assert ((pos >= 0) && (pos <= (int)this->all_pages.size())); assert ((pos >= 0) &&
(static_cast<size_t>(pos) <= this->all_pages.size()));
newpage.replaceKey("/Parent", pages); newpage.replaceKey("/Parent", pages);
kids.insertItem(pos, newpage); kids.insertItem(pos, newpage);
int npages = kids.getArrayNItems(); int npages = kids.getArrayNItems();
pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
this->all_pages.insert(this->all_pages.begin() + pos, newpage); this->all_pages.insert(this->all_pages.begin() + pos, newpage);
assert((int)this->all_pages.size() == npages); assert(this->all_pages.size() == static_cast<size_t>(npages));
for (int i = pos + 1; i < npages; ++i) for (int i = pos + 1; i < npages; ++i)
{ {
insertPageobjToPage(this->all_pages[i], i, false); insertPageobjToPage(this->all_pages[i], i, false);
} }
insertPageobjToPage(newpage, pos, true); insertPageobjToPage(newpage, pos, true);
assert((int)this->pageobj_to_pages_pos.size() == npages); assert(this->pageobj_to_pages_pos.size() == static_cast<size_t>(npages));
} }
void void
@ -202,7 +203,7 @@ QPDF::removePage(QPDFObjectHandle page)
int pos = findPage(page); // also ensures flat /Pages int pos = findPage(page); // also ensures flat /Pages
QTC::TC("qpdf", "QPDF remove page", QTC::TC("qpdf", "QPDF remove page",
(pos == 0) ? 0 : // remove at beginning (pos == 0) ? 0 : // remove at beginning
(pos == ((int)this->all_pages.size() - 1)) ? 1 : // remove at end (pos == static_cast<int>(this->all_pages.size() - 1)) ? 1 : // end
2); // remove in middle 2); // remove in middle
QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle pages = getRoot().getKey("/Pages");
@ -212,10 +213,10 @@ QPDF::removePage(QPDFObjectHandle page)
int npages = kids.getArrayNItems(); int npages = kids.getArrayNItems();
pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
this->all_pages.erase(this->all_pages.begin() + pos); this->all_pages.erase(this->all_pages.begin() + pos);
assert((int)this->all_pages.size() == npages); assert(this->all_pages.size() == static_cast<size_t>(npages));
this->pageobj_to_pages_pos.erase( this->pageobj_to_pages_pos.erase(
ObjGen(page.getObjectID(), page.getGeneration())); ObjGen(page.getObjectID(), page.getGeneration()));
assert((int)this->pageobj_to_pages_pos.size() == npages); assert(this->pageobj_to_pages_pos.size() == static_cast<size_t>(npages));
for (int i = pos; i < npages; ++i) for (int i = pos; i < npages; ++i)
{ {
insertPageobjToPage(this->all_pages[i], i, false); insertPageobjToPage(this->all_pages[i], i, false);

View File

@ -26,7 +26,7 @@ QUtil::int_to_string(long long num, int fullpad)
char t[50]; char t[50];
// -2 or -1 to leave space for the possible negative sign and for NUL... // -2 or -1 to leave space for the possible negative sign and for NUL...
if (abs(fullpad) > (int)sizeof(t) - ((num < 0)?2:1)) if (abs(fullpad) > sizeof(t) - ((num < 0)?2:1))
{ {
throw std::logic_error("Util::int_to_string has been called with " throw std::logic_error("Util::int_to_string has been called with "
"a padding value greater than its internal " "a padding value greater than its internal "
@ -58,7 +58,7 @@ QUtil::double_to_string(double num, int decimal_places)
// 99 digits. // 99 digits.
char t[100]; char t[100];
std::string lhs = int_to_string((int)num); std::string lhs = int_to_string(static_cast<int>(num));
// lhs.length() gives us the length of the part on the right hand // lhs.length() gives us the length of the part on the right hand
// side of the dot + 1 for the dot + decimal_places: total size of // side of the dot + 1 for the dot + decimal_places: total size of
@ -68,7 +68,8 @@ QUtil::double_to_string(double num, int decimal_places)
// If decimal_places <= 0, it is as if no precision was provided // If decimal_places <= 0, it is as if no precision was provided
// so trust the buffer is big enough. The following test will // so trust the buffer is big enough. The following test will
// always pass in those cases. // always pass in those cases.
if (decimal_places + 1 + (int)lhs.length() > (int)sizeof(t) - 1) if (decimal_places + 1 + static_cast<int>(lhs.length()) >
static_cast<int>(sizeof(t)) - 1)
{ {
throw std::logic_error("Util::double_to_string has been called with " throw std::logic_error("Util::double_to_string has been called with "
"a number and a decimal places specification " "a number and a decimal places specification "
@ -96,6 +97,18 @@ QUtil::string_to_ll(char const* str)
#endif #endif
} }
unsigned char*
QUtil::unsigned_char_pointer(std::string const& str)
{
return reinterpret_cast<unsigned char*>(const_cast<char*>(str.c_str()));
}
unsigned char*
QUtil::unsigned_char_pointer(char const* str)
{
return reinterpret_cast<unsigned char*>(const_cast<char*>(str));
}
void void
QUtil::throw_system_error(std::string const& description) QUtil::throw_system_error(std::string const& description)
{ {
@ -126,14 +139,14 @@ int
QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence) QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence)
{ {
#if HAVE_FSEEKO #if HAVE_FSEEKO
return fseeko(stream, (off_t)offset, whence); return fseeko(stream, static_cast<off_t>(offset), whence);
#elif HAVE_FSEEKO64 #elif HAVE_FSEEKO64
return fseeko64(stream, offset, whence); return fseeko64(stream, offset, whence);
#else #else
# ifdef _MSC_VER # ifdef _MSC_VER
return _fseeki64(stream, offset, whence); return _fseeki64(stream, offset, whence);
# else # else
return fseek(stream, (long)offset, whence); return fseek(stream, static_cast<long>(offset), whence);
# endif # endif
#endif #endif
} }
@ -142,14 +155,14 @@ qpdf_offset_t
QUtil::tell(FILE* stream) QUtil::tell(FILE* stream)
{ {
#if HAVE_FSEEKO #if HAVE_FSEEKO
return (qpdf_offset_t)ftello(stream); return static_cast<qpdf_offset_t>(ftello(stream));
#elif HAVE_FSEEKO64 #elif HAVE_FSEEKO64
return (qpdf_offset_t)ftello64(stream); return static_cast<qpdf_offset_t>(ftello64(stream));
#else #else
# ifdef _MSC_VER # ifdef _MSC_VER
return _ftelli64(stream); return _ftelli64(stream);
# else # else
return (qpdf_offset_t)ftell(stream); return static_cast<qpdf_offset_t>(ftell(stream));
# endif # endif
#endif #endif
} }
@ -174,7 +187,7 @@ QUtil::hex_encode(std::string const& input)
buf[hex_size - 1] = '\0'; buf[hex_size - 1] = '\0';
for (unsigned int i = 0; i < input_size; ++i) for (unsigned int i = 0; i < input_size; ++i)
{ {
sprintf(buf + i * 2, "%02x", (unsigned char)input[i]); sprintf(buf + i * 2, "%02x", static_cast<unsigned char>(input[i]));
} }
return buf; return buf;
} }
@ -199,7 +212,7 @@ void
QUtil::setLineBuf(FILE* f) QUtil::setLineBuf(FILE* f)
{ {
#ifndef _WIN32 #ifndef _WIN32
setvbuf(f, (char *) NULL, _IOLBF, 0); setvbuf(f, reinterpret_cast<char *>(NULL), _IOLBF, 0);
#endif #endif
} }
@ -314,7 +327,7 @@ QUtil::toUTF8(unsigned long uval)
} }
else if (uval < 128) else if (uval < 128)
{ {
result += (char)(uval); result += static_cast<char>(uval);
} }
else else
{ {
@ -329,7 +342,7 @@ QUtil::toUTF8(unsigned long uval)
{ {
// Assign low six bits plus 10000000 to lowest unused // Assign low six bits plus 10000000 to lowest unused
// byte position, then shift // byte position, then shift
*cur_byte = (unsigned char) (0x80 + (uval & 0x3f)); *cur_byte = static_cast<unsigned char>(0x80 + (uval & 0x3f));
uval >>= 6; uval >>= 6;
// Maximum that will fit in high byte now shrinks by one bit // Maximum that will fit in high byte now shrinks by one bit
maxval >>= 1; maxval >>= 1;
@ -342,9 +355,10 @@ QUtil::toUTF8(unsigned long uval)
} }
// If maxval is k bits long, the high (7 - k) bits of the // If maxval is k bits long, the high (7 - k) bits of the
// resulting byte must be high. // resulting byte must be high.
*cur_byte = (unsigned char)((0xff - (1 + (maxval << 1))) + uval); *cur_byte = static_cast<unsigned char>(
(0xff - (1 + (maxval << 1))) + uval);
result += (char*)cur_byte; result += reinterpret_cast<char*>(cur_byte);
} }
return result; return result;
@ -358,8 +372,8 @@ QUtil::random()
{ {
// Seed the random number generator with something simple, but // Seed the random number generator with something simple, but
// just to be interesting, don't use the unmodified current // just to be interesting, don't use the unmodified current
// time.... // time. It would be better if this were a more secure seed.
QUtil::srandom((int)QUtil::get_current_time() ^ 0xcccc); QUtil::srandom(QUtil::get_current_time() ^ 0xcccc);
seeded_random = true; seeded_random = true;
} }
@ -385,6 +399,6 @@ QUtil::initializeWithRandomBytes(unsigned char* data, size_t len)
{ {
for (size_t i = 0; i < len; ++i) for (size_t i = 0; i < len; ++i)
{ {
data[i] = (unsigned char)((QUtil::random() & 0xff0) >> 4); data[i] = static_cast<unsigned char>((QUtil::random() & 0xff0) >> 4);
} }
} }

View File

@ -15,7 +15,7 @@ RC4::RC4(unsigned char const* key_data, int key_len)
{ {
if (key_len == -1) if (key_len == -1)
{ {
key_len = (int)strlen((char*)key_data); key_len = strlen(reinterpret_cast<char const*>(key_data));
} }
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)

View File

@ -693,15 +693,17 @@ static const u32 rcon[] =
/* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
}; };
#define GETU32(plaintext) (((u32)(plaintext)[0] << 24) ^ \ #define GETU32(plaintext) \
((u32)(plaintext)[1] << 16) ^ \ ((static_cast<u32>((plaintext)[0]) << 24) ^ \
((u32)(plaintext)[2] << 8) ^ \ (static_cast<u32>((plaintext)[1]) << 16) ^ \
((u32)(plaintext)[3])) (static_cast<u32>((plaintext)[2]) << 8) ^ \
(static_cast<u32>((plaintext)[3])))
#define PUTU32(ciphertext, st) { (ciphertext)[0] = (u8)((st) >> 24); \ #define PUTU32(ciphertext, st) { \
(ciphertext)[1] = (u8)((st) >> 16); \ (ciphertext)[0] = static_cast<u8>((st) >> 24); \
(ciphertext)[2] = (u8)((st) >> 8); \ (ciphertext)[1] = static_cast<u8>((st) >> 16); \
(ciphertext)[3] = (u8)(st); } (ciphertext)[2] = static_cast<u8>((st) >> 8); \
(ciphertext)[3] = static_cast<u8>(st); }
/** /**
* Expand the cipher key into the encryption key schedule. * Expand the cipher key into the encryption key schedule.

View File

@ -86,7 +86,7 @@ int main(int argc, char* argv[])
usage(); usage();
} }
unsigned int hexkeylen = (unsigned int)strlen(hexkey); unsigned int hexkeylen = strlen(hexkey);
unsigned int keylen = hexkeylen / 2; unsigned int keylen = hexkeylen / 2;
FILE* infile = fopen(infilename, "rb"); FILE* infile = fopen(infilename, "rb");
@ -112,7 +112,7 @@ int main(int argc, char* argv[])
t[2] = '\0'; t[2] = '\0';
long val = strtol(t, 0, 16); long val = strtol(t, 0, 16);
key[i/2] = (unsigned char) val; key[i/2] = static_cast<unsigned char>(val);
} }
Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);

View File

@ -37,7 +37,8 @@ test_write_bits(unsigned char& ch, unsigned int& bit_offset, unsigned long val,
int bits, Pl_Buffer* bp) int bits, Pl_Buffer* bp)
{ {
write_bits(ch, bit_offset, val, bits, bp); write_bits(ch, bit_offset, val, bits, bp);
printf("ch = %02x, bit_offset = %d\n", (unsigned int) ch, bit_offset); printf("ch = %02x, bit_offset = %d\n",
static_cast<unsigned int>(ch), bit_offset);
} }
static void static void
@ -49,7 +50,7 @@ print_buffer(Pl_Buffer* bp)
size_t l = b->getSize(); size_t l = b->getSize();
for (unsigned long i = 0; i < l; ++i) for (unsigned long i = 0; i < l; ++i)
{ {
printf("%02x%s", (unsigned int)(p[i]), printf("%02x%s", static_cast<unsigned int>(p[i]),
(i == l - 1) ? "\n" : " "); (i == l - 1) ? "\n" : " ");
} }
printf("\n"); printf("\n");

View File

@ -1,11 +1,15 @@
#include <qpdf/Pl_Buffer.hh> #include <qpdf/Pl_Buffer.hh>
#include <qpdf/Pl_Count.hh> #include <qpdf/Pl_Count.hh>
#include <qpdf/Pl_Discard.hh> #include <qpdf/Pl_Discard.hh>
#include <qpdf/QUtil.hh>
#include <stdlib.h> #include <stdlib.h>
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
typedef unsigned char* uc; static unsigned char* uc(char const* s)
{
return QUtil::unsigned_char_pointer(s);
}
int main() int main()
{ {
@ -14,20 +18,20 @@ int main()
Pl_Discard discard; Pl_Discard discard;
Pl_Count count("count", &discard); Pl_Count count("count", &discard);
Pl_Buffer bp1("bp1", &count); Pl_Buffer bp1("bp1", &count);
bp1.write((uc)"12345", 5); bp1.write(uc("12345"), 5);
bp1.write((uc)"67890", 5); bp1.write(uc("67890"), 5);
bp1.finish(); bp1.finish();
std::cout << "count: " << count.getCount() << std::endl; std::cout << "count: " << count.getCount() << std::endl;
bp1.write((uc)"abcde", 5); bp1.write(uc("abcde"), 5);
bp1.write((uc)"fghij", 6); bp1.write(uc("fghij"), 6);
bp1.finish(); bp1.finish();
std::cout << "count: " << count.getCount() << std::endl; std::cout << "count: " << count.getCount() << std::endl;
Buffer* b = bp1.getBuffer(); Buffer* b = bp1.getBuffer();
std::cout << "size: " << b->getSize() << std::endl; std::cout << "size: " << b->getSize() << std::endl;
std::cout << "data: " << b->getBuffer() << std::endl; std::cout << "data: " << b->getBuffer() << std::endl;
delete b; delete b;
bp1.write((uc)"qwert", 5); bp1.write(uc("qwert"), 5);
bp1.write((uc)"yuiop", 6); bp1.write(uc("yuiop"), 6);
bp1.finish(); bp1.finish();
std::cout << "count: " << count.getCount() << std::endl; std::cout << "count: " << count.getCount() << std::endl;
b = bp1.getBuffer(); b = bp1.getBuffer();
@ -36,8 +40,8 @@ int main()
delete b; delete b;
Pl_Buffer bp2("bp2"); Pl_Buffer bp2("bp2");
bp2.write((uc)"moo", 3); bp2.write(uc("moo"), 3);
bp2.write((uc)"quack", 6); bp2.write(uc("quack"), 6);
try try
{ {
delete bp2.getBuffer(); delete bp2.getBuffer();

View File

@ -1,12 +1,13 @@
#include <qpdf/Pl_Concatenate.hh> #include <qpdf/Pl_Concatenate.hh>
#include <qpdf/Pl_Flate.hh> #include <qpdf/Pl_Flate.hh>
#include <qpdf/Pl_Buffer.hh> #include <qpdf/Pl_Buffer.hh>
#include <qpdf/QUtil.hh>
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
static void pipeStringAndFinish(Pipeline* p, std::string const& str) static void pipeStringAndFinish(Pipeline* p, std::string const& str)
{ {
p->write((unsigned char*)str.c_str(), str.length()); p->write(QUtil::unsigned_char_pointer(str), str.length());
p->finish(); p->finish();
} }
@ -25,7 +26,8 @@ int main(int argc, char* argv[])
inflate.write(b1_buf->getBuffer(), b1_buf->getSize()); inflate.write(b1_buf->getBuffer(), b1_buf->getSize());
inflate.finish(); inflate.finish();
PointerHolder<Buffer> b2_buf = b2.getBuffer(); PointerHolder<Buffer> b2_buf = b2.getBuffer();
std::string result((char const*)b2_buf->getBuffer(), b2_buf->getSize()); std::string result(reinterpret_cast<char*>(b2_buf->getBuffer()),
b2_buf->getSize());
if (result == "-one--two-") if (result == "-one--two-")
{ {
std::cout << "concatenate test passed" << std::endl; std::cout << "concatenate test passed" << std::endl;

View File

@ -152,7 +152,7 @@ static void print_utf8(unsigned long val)
iter != result.end(); ++iter) iter != result.end(); ++iter)
{ {
char t[3]; char t[3];
sprintf(t, "%02x", (unsigned char) (*iter)); sprintf(t, "%02x", static_cast<unsigned char>(*iter));
std::cout << " " << t; std::cout << " " << t;
} }
} }

View File

@ -17,7 +17,7 @@ int main(int argc, char* argv[])
char* hexkey = argv[1]; char* hexkey = argv[1];
char* infilename = argv[2]; char* infilename = argv[2];
char* outfilename = argv[3]; char* outfilename = argv[3];
unsigned int hexkeylen = (unsigned int)strlen(hexkey); unsigned int hexkeylen = strlen(hexkey);
unsigned int keylen = hexkeylen / 2; unsigned int keylen = hexkeylen / 2;
unsigned char* key = new unsigned char[keylen + 1]; unsigned char* key = new unsigned char[keylen + 1];
key[keylen] = '\0'; key[keylen] = '\0';
@ -37,7 +37,7 @@ int main(int argc, char* argv[])
t[2] = '\0'; t[2] = '\0';
long val = strtol(t, 0, 16); long val = strtol(t, 0, 16);
key[i/2] = (unsigned char) val; key[i/2] = static_cast<unsigned char>(val);
} }
FILE* outfile = fopen(outfilename, "wb"); FILE* outfile = fopen(outfilename, "wb");

View File

@ -8,7 +8,7 @@ static void test(Pl_SHA2& sha2, char const* description, int bits,
char const* input, std::string const& output) char const* input, std::string const& output)
{ {
sha2.resetBits(bits); sha2.resetBits(bits);
sha2.write((unsigned char*) input, strlen(input)); sha2.write(QUtil::unsigned_char_pointer(input), strlen(input));
sha2.finish(); sha2.finish();
std::cout << description << ": "; std::cout << description << ": ";
if (output == sha2.getHexDigest()) if (output == sha2.getHexDigest())

View File

@ -881,7 +881,7 @@ QPDFPageData::QPDFPageData(QPDF* qpdf, char const* range) :
qpdf(qpdf), qpdf(qpdf),
orig_pages(qpdf->getAllPages()) orig_pages(qpdf->getAllPages())
{ {
this->selected_pages = parse_numrange(range, (int)this->orig_pages.size()); this->selected_pages = parse_numrange(range, this->orig_pages.size());
} }
static void parse_version(std::string const& full_version_string, static void parse_version(std::string const& full_version_string,
@ -1012,7 +1012,7 @@ int main(int argc, char* argv[])
// Be lax about -arg vs --arg // Be lax about -arg vs --arg
++arg; ++arg;
} }
char* parameter = (char*)strchr(arg, '='); char* parameter = const_cast<char*>(strchr(arg, '='));
if (parameter) if (parameter)
{ {
*parameter++ = 0; *parameter++ = 0;

View File

@ -95,7 +95,8 @@ static std::string getPageContents(QPDFObjectHandle page)
{ {
PointerHolder<Buffer> b1 = PointerHolder<Buffer> b1 =
page.getKey("/Contents").getStreamData(); page.getKey("/Contents").getStreamData();
return std::string((char *)(b1->getBuffer()), b1->getSize()) + "\0"; return std::string(
reinterpret_cast<char *>(b1->getBuffer()), b1->getSize()) + "\0";
} }
static void checkPageContents(QPDFObjectHandle page, static void checkPageContents(QPDFObjectHandle page,
@ -175,7 +176,7 @@ void runtest(int n, char const* filename1, char const* arg2)
FILE* f = QUtil::fopen_wrapper(std::string("open ") + filename1, FILE* f = QUtil::fopen_wrapper(std::string("open ") + filename1,
fopen(filename1, "rb")); fopen(filename1, "rb"));
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size_t size = (size_t) QUtil::tell(f); size_t size = QUtil::tell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
file_buf = PointerHolder<char>(true, new char[size]); file_buf = PointerHolder<char>(true, new char[size]);
char* buf_p = file_buf.getPointer(); char* buf_p = file_buf.getPointer();
@ -495,7 +496,8 @@ void runtest(int n, char const* filename1, char const* arg2)
unsigned char const* data = buf->getBuffer(); unsigned char const* data = buf->getBuffer();
bool cleartext = false; bool cleartext = false;
if ((buf->getSize() > 9) && if ((buf->getSize() > 9) &&
(strncmp((char const*)data, "<?xpacket", 9) == 0)) (strncmp(reinterpret_cast<char const*>(data),
"<?xpacket", 9) == 0))
{ {
cleartext = true; cleartext = true;
} }
@ -532,7 +534,8 @@ void runtest(int n, char const* filename1, char const* arg2)
} }
Pl_Buffer p1("buffer"); Pl_Buffer p1("buffer");
Pl_Flate p2("compress", &p1, Pl_Flate::a_deflate); Pl_Flate p2("compress", &p1, Pl_Flate::a_deflate);
p2.write((unsigned char*)"new data for stream\n", 20); // no null! p2.write(QUtil::unsigned_char_pointer("new data for stream\n"),
20); // no null!
p2.finish(); p2.finish();
PointerHolder<Buffer> b = p1.getBuffer(); PointerHolder<Buffer> b = p1.getBuffer();
// This is a bogus way to use StreamDataProvider, but it does // This is a bogus way to use StreamDataProvider, but it does
@ -569,7 +572,7 @@ void runtest(int n, char const* filename1, char const* arg2)
// Explicitly exercise the Buffer version of newStream // Explicitly exercise the Buffer version of newStream
PointerHolder<Buffer> buf = new Buffer(20); PointerHolder<Buffer> buf = new Buffer(20);
unsigned char* bp = buf->getBuffer(); unsigned char* bp = buf->getBuffer();
memcpy(bp, (char*)"data for new stream\n", 20); // no null! memcpy(bp, "data for new stream\n", 20); // no null!
QPDFObjectHandle qstream = QPDFObjectHandle::newStream( QPDFObjectHandle qstream = QPDFObjectHandle::newStream(
&pdf, buf); &pdf, buf);
QPDFObjectHandle rstream = QPDFObjectHandle::newStream(&pdf); QPDFObjectHandle rstream = QPDFObjectHandle::newStream(&pdf);
@ -824,7 +827,7 @@ void runtest(int n, char const* filename1, char const* arg2)
page.replaceKey("/Parent", pages); page.replaceKey("/Parent", pages);
pages.replaceKey( pages.replaceKey(
"/Count", "/Count",
QPDFObjectHandle::newInteger(1 + (int)all_pages.size())); QPDFObjectHandle::newInteger(1 + all_pages.size()));
kids.appendItem(page); kids.appendItem(page);
assert(all_pages.size() == 10); assert(all_pages.size() == 10);
pdf.updateAllPagesCache(); pdf.updateAllPagesCache();
@ -1220,7 +1223,7 @@ void runtest(int n, char const* filename1, char const* arg2)
{ {
std::string const& filename = (*iter).first; std::string const& filename = (*iter).first;
std::string data = std::string( std::string data = std::string(
(char const*)(*iter).second->getBuffer(), reinterpret_cast<char const*>((*iter).second->getBuffer()),
(*iter).second->getSize()); (*iter).second->getSize());
bool is_binary = false; bool is_binary = false;
for (size_t i = 0; i < data.size(); ++i) for (size_t i = 0; i < data.size(); ++i)
@ -1234,7 +1237,9 @@ void runtest(int n, char const* filename1, char const* arg2)
if (is_binary) if (is_binary)
{ {
std::string t; std::string t;
for (size_t i = 0; i < std::min(data.size(), (size_t)20); ++i) for (size_t i = 0;
i < std::min(data.size(), static_cast<size_t>(20));
++i)
{ {
if ((data[i] >= 32) && (data[i] <= 126)) if ((data[i] >= 32) && (data[i] <= 126))
{ {
@ -1276,7 +1281,8 @@ void runtest(int n, char const* filename1, char const* arg2)
stream.pipeStreamData(&p2, false, false, false); stream.pipeStreamData(&p2, false, false, false);
PointerHolder<Buffer> buf = p1.getBuffer(); PointerHolder<Buffer> buf = p1.getBuffer();
std::string data = std::string( std::string data = std::string(
(char const*)buf->getBuffer(), buf->getSize()); reinterpret_cast<char const*>(buf->getBuffer()),
buf->getSize());
std::cout << stream.getDict().unparse() std::cout << stream.getDict().unparse()
<< filename << ":\n" << data << "--END--\n"; << filename << ":\n" << data << "--END--\n";
} }

View File

@ -135,7 +135,7 @@ ImageProvider::provideStreamData(int objid, int generation,
for (int y = 0; y < nstripes; ++y) for (int y = 0; y < nstripes; ++y)
{ {
unsigned char color = get_pixel_color(n, y); unsigned char color = get_pixel_color(n, y);
memset(buf, (int) color, width * stripesize); memset(buf, color, width * stripesize);
pipeline->write(buf, width * stripesize); pipeline->write(buf, width * stripesize);
} }
pipeline->finish(); pipeline->finish();
@ -256,7 +256,8 @@ static void check_page_contents(int pageno, QPDFObjectHandle page)
PointerHolder<Buffer> buf = PointerHolder<Buffer> buf =
page.getKey("/Contents").getStreamData(); page.getKey("/Contents").getStreamData();
std::string actual_contents = std::string actual_contents =
std::string((char *)(buf->getBuffer()), buf->getSize()); std::string(reinterpret_cast<char *>(buf->getBuffer()),
buf->getSize());
std::string expected_contents = generate_page_contents(pageno); std::string expected_contents = generate_page_contents(pageno);
if (expected_contents != actual_contents) if (expected_contents != actual_contents)
{ {
@ -280,7 +281,7 @@ static void check_pdf(char const* filename)
QPDF pdf; QPDF pdf;
pdf.processFile(filename); pdf.processFile(filename);
std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages(); std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
assert(pages.size() == (size_t)npages); assert(pages.size() == static_cast<size_t>(npages));
for (int i = 0; i < npages; ++i) for (int i = 0; i < npages; ++i)
{ {
int pageno = i + 1; int pageno = i + 1;