diff --git a/examples/pdf-create.cc b/examples/pdf-create.cc index 902c6805..a7b11a50 100644 --- a/examples/pdf-create.cc +++ b/examples/pdf-create.cc @@ -39,7 +39,7 @@ ImageProvider::provideStreamData(int objid, int generation, { 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(); } diff --git a/examples/pdf-invert-images.cc b/examples/pdf-invert-images.cc index 997fc37c..60f4e8d1 100644 --- a/examples/pdf-invert-images.cc +++ b/examples/pdf-invert-images.cc @@ -53,7 +53,7 @@ ImageInverter::provideStreamData(int objid, int generation, unsigned char ch; for (size_t i = 0; i < size; ++i) { - ch = (unsigned char)0xff - buf[i]; + ch = static_cast(0xff) - buf[i]; pipeline->write(&ch, 1); } pipeline->finish(); diff --git a/examples/pdf-parse-content.cc b/examples/pdf-parse-content.cc index 29a989e7..0394e938 100644 --- a/examples/pdf-parse-content.cc +++ b/examples/pdf-parse-content.cc @@ -69,7 +69,7 @@ int main(int argc, char* argv[]) QPDF pdf; pdf.processFile(filename); std::vector pages = pdf.getAllPages(); - if ((pageno < 1) || (pageno > (int)pages.size())) + if ((pageno < 1) || (static_cast(pageno) > pages.size())) { usage(); } diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh index eab64072..a1644f84 100644 --- a/include/qpdf/QPDF.hh +++ b/include/qpdf/QPDF.hh @@ -931,7 +931,7 @@ class QPDF void readHPageOffset(BitStream); void readHSharedObject(BitStream); void readHGeneric(BitStream, HGeneric&); - int maxEnd(ObjUser const& ou); + qpdf_offset_t maxEnd(ObjUser const& ou); qpdf_offset_t getLinearizationOffset(ObjGen const&); QPDFObjectHandle getUncompressedObject( QPDFObjectHandle&, std::map const& object_stream_data); diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index 9639917d..5b71a362 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -27,6 +27,17 @@ namespace QUtil QPDF_DLL 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 // "description: " the standard string corresponding to the // current value of errno. diff --git a/libqpdf/BufferInputSource.cc b/libqpdf/BufferInputSource.cc index 03439955..73baefae 100644 --- a/libqpdf/BufferInputSource.cc +++ b/libqpdf/BufferInputSource.cc @@ -20,7 +20,7 @@ BufferInputSource::BufferInputSource(std::string const& description, { this->buf = new Buffer(contents.length()); unsigned char* bp = buf->getBuffer(); - memcpy(bp, (char*)contents.c_str(), contents.length()); + memcpy(bp, contents.c_str(), contents.length()); } BufferInputSource::~BufferInputSource() @@ -38,7 +38,7 @@ BufferInputSource::findAndSkipNextEOL() { 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) { this->last_offset = end_pos; @@ -47,12 +47,12 @@ BufferInputSource::findAndSkipNextEOL() } 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(); - void* start = (void*)(buffer + this->cur_offset); - unsigned char* p1 = (unsigned char*)memchr(start, '\r', len); - unsigned char* p2 = (unsigned char*)memchr(start, '\n', len); + void* start = const_cast(buffer) + this->cur_offset; + unsigned char* p1 = static_cast(memchr(start, '\r', len)); + unsigned char* p2 = static_cast(memchr(start, '\n', len)); unsigned char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2; if (p) { @@ -96,7 +96,7 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence) break; case SEEK_END: - this->cur_offset = (qpdf_offset_t)this->buf->getSize() + offset; + this->cur_offset = this->buf->getSize() + offset; break; case SEEK_CUR: @@ -129,7 +129,7 @@ BufferInputSource::read(char* buffer, size_t length) { 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) { this->last_offset = end_pos; @@ -137,7 +137,8 @@ BufferInputSource::read(char* buffer, size_t length) } 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(end_pos - this->cur_offset), length); memcpy(buffer, buf->getBuffer() + this->cur_offset, len); this->cur_offset += len; return len; diff --git a/libqpdf/FileInputSource.cc b/libqpdf/FileInputSource.cc index b1f7b5d2..b49fad99 100644 --- a/libqpdf/FileInputSource.cc +++ b/libqpdf/FileInputSource.cc @@ -62,8 +62,8 @@ FileInputSource::findAndSkipNextEOL() } else { - char* p1 = (char*)memchr((void*)buf, '\r', len); - char* p2 = (char*)memchr((void*)buf, '\n', len); + char* p1 = static_cast(memchr(buf, '\r', len)); + char* p2 = static_cast(memchr(buf, '\n', len)); char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2; if (p) { @@ -137,5 +137,5 @@ void FileInputSource::unreadCh(char ch) { QUtil::os_wrapper(this->filename + ": unread character", - ungetc((unsigned char)ch, this->file)); + ungetc(static_cast(ch), this->file)); } diff --git a/libqpdf/MD5.cc b/libqpdf/MD5.cc index 57bcc45f..73ba7f9f 100644 --- a/libqpdf/MD5.cc +++ b/libqpdf/MD5.cc @@ -71,22 +71,22 @@ static unsigned char PADDING[64] = { // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. // Rotation is separate from addition to prevent recomputation. #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(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #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(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #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(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } #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(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ } @@ -115,21 +115,20 @@ void MD5::update(unsigned char *input, unsigned int i, index, partLen; // Compute number of bytes mod 64 - index = (unsigned int)((count[0] >> 3) & 0x3F); + index = static_cast((count[0] >> 3) & 0x3f); // Update number of bits - if ((count[0] += ((UINT4)inputLen << 3)) - < ((UINT4)inputLen << 3)) + if ((count[0] += (static_cast(inputLen) << 3)) < + (static_cast(inputLen) << 3)) count[1]++; - count[1] += ((UINT4)inputLen >> 29); + count[1] += (static_cast(inputLen) >> 29); partLen = 64 - index; // Transform as many times as possible. if (inputLen >= partLen) { - memcpy - ((POINTER)&buffer[index], (POINTER)input, partLen); + memcpy(&buffer[index], input, partLen); transform(state, buffer); for (i = partLen; i + 63 < inputLen; i += 64) @@ -141,9 +140,7 @@ void MD5::update(unsigned char *input, i = 0; // Buffer remaining input - memcpy - ((POINTER)&buffer[index], (POINTER)&input[i], - inputLen-i); + memcpy(&buffer[index], &input[i], inputLen-i); } // MD5 finalization. Ends an MD5 message-digest operation, writing the @@ -163,7 +160,7 @@ void MD5::final() // Pad out to 56 mod 64. - index = (unsigned int)((count[0] >> 3) & 0x3f); + index = static_cast((count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); update(PADDING, padLen); @@ -266,7 +263,7 @@ void MD5::transform(UINT4 state[4], unsigned char block[64]) // 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 @@ -276,10 +273,10 @@ void MD5::encode(unsigned char *output, UINT4 *input, unsigned int len) unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) { - output[j] = (unsigned char)(input[i] & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + output[j] = static_cast(input[i] & 0xff); + output[j+1] = static_cast((input[i] >> 8) & 0xff); + output[j+2] = static_cast((input[i] >> 16) & 0xff); + output[j+3] = static_cast((input[i] >> 24) & 0xff); } } @@ -290,8 +287,11 @@ void MD5::decode(UINT4 *output, unsigned char *input, unsigned int len) unsigned int i, j; for (i = 0, j = 0; j < len; i++, j += 4) - output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | - (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); + output[i] = + static_cast(input[j]) | + (static_cast(input[j+1]) << 8) | + (static_cast(input[j+2]) << 16) | + (static_cast(input[j+3]) << 24); } // Public functions @@ -308,20 +308,20 @@ void MD5::reset() 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(); } 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) { - update((unsigned char *)data, len); + update(QUtil::unsigned_char_pointer(data), len); } 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); if (len > 0) { - update(buffer, (unsigned int) len); + update(buffer, len); so_far += len; if ((up_to_size >= 0) && (so_far >= up_to_size)) { @@ -386,7 +386,8 @@ void MD5::print() std::string MD5::unparse() { final(); - return QUtil::hex_encode(std::string((char*)digest_val, 16)); + return QUtil::hex_encode( + std::string(reinterpret_cast(digest_val), 16)); } std::string diff --git a/libqpdf/PCRE.cc b/libqpdf/PCRE.cc index 1e5585b0..2e808aa7 100644 --- a/libqpdf/PCRE.cc +++ b/libqpdf/PCRE.cc @@ -166,7 +166,7 @@ PCRE::match(char const* subject, int options, int startoffset, int size) { if (size == -1) { - size = (int) strlen(subject); + size = strlen(subject); } Match result(this->nbackrefs, subject); diff --git a/libqpdf/Pl_AES_PDF.cc b/libqpdf/Pl_AES_PDF.cc index 52876101..5c493cb4 100644 --- a/libqpdf/Pl_AES_PDF.cc +++ b/libqpdf/Pl_AES_PDF.cc @@ -122,7 +122,8 @@ Pl_AES_PDF::finish() // Pad as described in section 3.5.1 of version 1.7 of the PDF // specification, including providing an entire block of padding // if the input was a multiple of 16 bytes. - unsigned char pad = (unsigned char) (this->buf_size - this->offset); + unsigned char pad = + static_cast(this->buf_size - this->offset); memset(this->inbuf + this->offset, pad, pad); this->offset = this->buf_size; flush(false); diff --git a/libqpdf/Pl_ASCII85Decoder.cc b/libqpdf/Pl_ASCII85Decoder.cc index 5c9c9f56..3a3b57b1 100644 --- a/libqpdf/Pl_ASCII85Decoder.cc +++ b/libqpdf/Pl_ASCII85Decoder.cc @@ -68,7 +68,9 @@ Pl_ASCII85Decoder::write(unsigned char* buf, size_t len) else { 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; diff --git a/libqpdf/Pl_ASCIIHexDecoder.cc b/libqpdf/Pl_ASCIIHexDecoder.cc index ca153e89..0ba85dc7 100644 --- a/libqpdf/Pl_ASCIIHexDecoder.cc +++ b/libqpdf/Pl_ASCIIHexDecoder.cc @@ -91,7 +91,7 @@ Pl_ASCIIHexDecoder::flush() b[i] = this->inbuf[i] - '0'; } } - unsigned char ch = (unsigned char)((b[0] << 4) + b[1]); + unsigned char ch = static_cast((b[0] << 4) + b[1]); QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush", (this->pos == 2) ? 0 : 1); diff --git a/libqpdf/Pl_Flate.cc b/libqpdf/Pl_Flate.cc index 30616707..be8ca8ba 100644 --- a/libqpdf/Pl_Flate.cc +++ b/libqpdf/Pl_Flate.cc @@ -18,10 +18,10 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, // Windows environment. this->zdata = new z_stream; - z_stream& zstream = *((z_stream*) this->zdata); - zstream.zalloc = (alloc_func)0; - zstream.zfree = (free_func)0; - zstream.opaque = (voidpf)0; + z_stream& zstream = *(static_cast(this->zdata)); + zstream.zalloc = 0; + zstream.zfree = 0; + zstream.opaque = 0; zstream.next_in = 0; zstream.avail_in = 0; zstream.next_out = this->outbuf; @@ -35,7 +35,7 @@ Pl_Flate::~Pl_Flate() delete [] this->outbuf; this->outbuf = 0; } - delete (z_stream*)this->zdata; + delete static_cast(this->zdata); this->zdata = 0; } @@ -57,7 +57,7 @@ Pl_Flate::write(unsigned char* data, size_t len) while (bytes_left > 0) { 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; buf += bytes; } @@ -66,13 +66,20 @@ Pl_Flate::write(unsigned char* data, size_t len) void Pl_Flate::handleData(unsigned char* data, int len, int flush) { - z_stream& zstream = *((z_stream*) this->zdata); + z_stream& zstream = *(static_cast(this->zdata)); zstream.next_in = data; zstream.avail_in = len; if (! this->initialized) { 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) { err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION); @@ -81,6 +88,10 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) { err = inflateInit(&zstream); } +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif + checkError("Init", err); this->initialized = true; } @@ -146,7 +157,7 @@ Pl_Flate::finish() { if (this->initialized) { - z_stream& zstream = *((z_stream*) this->zdata); + z_stream& zstream = *(static_cast(this->zdata)); unsigned char buf[1]; buf[0] = '\0'; handleData(buf, 0, Z_FINISH); @@ -171,7 +182,7 @@ Pl_Flate::finish() void Pl_Flate::checkError(char const* prefix, int error_code) { - z_stream& zstream = *((z_stream*) this->zdata); + z_stream& zstream = *(static_cast(this->zdata)); if (error_code != Z_OK) { char const* action_str = (action == a_deflate ? "deflate" : "inflate"); diff --git a/libqpdf/Pl_LZWDecoder.cc b/libqpdf/Pl_LZWDecoder.cc index 646e45de..f7c6fb30 100644 --- a/libqpdf/Pl_LZWDecoder.cc +++ b/libqpdf/Pl_LZWDecoder.cc @@ -98,7 +98,7 @@ Pl_LZWDecoder::getFirstChar(int code) unsigned char result = '\0'; if (code < 256) { - result = (unsigned char) code; + result = static_cast(code); } else { @@ -131,7 +131,7 @@ Pl_LZWDecoder::addToTable(unsigned char next) assert(idx < table.size()); Buffer& b = table[idx]; last_data = b.getBuffer(); - last_size = (unsigned int) b.getSize(); + last_size = b.getSize(); } 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 // we're reading now. unsigned char next = '\0'; - unsigned int table_size = (unsigned int) table.size(); + unsigned int table_size = table.size(); if (code < 256) { // just read < 256; last time's next was code @@ -214,7 +214,7 @@ Pl_LZWDecoder::handleCode(int code) if (code < 256) { - unsigned char ch = (unsigned char) code; + unsigned char ch = static_cast(code); getNext()->write(&ch, 1); } else diff --git a/libqpdf/Pl_MD5.cc b/libqpdf/Pl_MD5.cc index 44911b42..3a78cb33 100644 --- a/libqpdf/Pl_MD5.cc +++ b/libqpdf/Pl_MD5.cc @@ -28,7 +28,8 @@ Pl_MD5::write(unsigned char* buf, size_t len) while (bytes_left > 0) { size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left); - this->md5.encodeDataIncrementally((char*) data, (int)bytes); + this->md5.encodeDataIncrementally( + reinterpret_cast(data), bytes); bytes_left -= bytes; data += bytes; } diff --git a/libqpdf/Pl_PNGFilter.cc b/libqpdf/Pl_PNGFilter.cc index 19b90c03..4b79e6b1 100644 --- a/libqpdf/Pl_PNGFilter.cc +++ b/libqpdf/Pl_PNGFilter.cc @@ -73,7 +73,7 @@ Pl_PNGFilter::processRow() void Pl_PNGFilter::decodeRow() { - int filter = (int) this->cur_row[0]; + int filter = this->cur_row[0]; if (this->prev_row) { switch (filter) diff --git a/libqpdf/Pl_RC4.cc b/libqpdf/Pl_RC4.cc index 74bb16b9..87c17f83 100644 --- a/libqpdf/Pl_RC4.cc +++ b/libqpdf/Pl_RC4.cc @@ -38,7 +38,7 @@ Pl_RC4::write(unsigned char* data, size_t len) size_t bytes = (bytes_left < this->out_bufsize ? bytes_left : out_bufsize); bytes_left -= bytes; - rc4.process(p, (int)bytes, outbuf); + rc4.process(p, bytes, outbuf); p += bytes; getNext()->write(outbuf, bytes); } diff --git a/libqpdf/Pl_SHA2.cc b/libqpdf/Pl_SHA2.cc index 84f24d6e..17eff7e3 100644 --- a/libqpdf/Pl_SHA2.cc +++ b/libqpdf/Pl_SHA2.cc @@ -128,13 +128,16 @@ Pl_SHA2::getRawDigest() switch (bits) { case 256: - result = std::string((char*)this->sha256sum, sizeof(this->sha256sum)); + result = std::string(reinterpret_cast(this->sha256sum), + sizeof(this->sha256sum)); break; case 384: - result = std::string((char*)this->sha384sum, sizeof(this->sha384sum)); + result = std::string(reinterpret_cast(this->sha384sum), + sizeof(this->sha384sum)); break; case 512: - result = std::string((char*)this->sha512sum, sizeof(this->sha512sum)); + result = std::string(reinterpret_cast(this->sha512sum), + sizeof(this->sha512sum)); break; default: badBits(); diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 5860fb11..7cd3509b 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -159,9 +159,10 @@ QPDF::processMemoryFile(char const* description, char const* password) { processInputSource( - new BufferInputSource(description, - new Buffer((unsigned char*)buf, length), - true), + new BufferInputSource( + description, + new Buffer(QUtil::unsigned_char_pointer(buf), length), + true), password); } @@ -280,7 +281,7 @@ QPDF::parse(char const* password) // where the regexp matches. char* p = buf; char const* candidate = ""; - while ((p = (char*)memchr(p, 's', tbuf_size - (p - buf))) != 0) + while ((p = static_cast(memchr(p, 's', tbuf_size - (p - buf)))) != 0) { 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) { fields[j] <<= 8; - fields[j] += (int)(*p++); + fields[j] += static_cast(*p++); } } @@ -828,7 +829,8 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj) // This is needed by checkLinearization() this->first_xref_item_offset = xref_offset; } - insertXrefEntry(obj, (int)fields[0], fields[1], (int)fields[2]); + insertXrefEntry(obj, static_cast(fields[0]), + fields[1], static_cast(fields[2])); } if (! this->trailer.isInitialized()) @@ -1096,8 +1098,7 @@ QPDF::readObject(PointerHolder input, } length = length_obj.getIntValue(); - input->seek( - stream_offset + (qpdf_offset_t)length, SEEK_SET); + input->seek(stream_offset + length, SEEK_SET); if (! (readToken(input) == QPDFTokenizer::Token( QPDFTokenizer::tt_word, "endstream"))) @@ -1395,7 +1396,7 @@ QPDF::readObjectAtOffset(bool try_recovery, char ch; if (this->file->read(&ch, 1)) { - if (! isspace((unsigned char)ch)) + if (! isspace(static_cast(ch))) { this->file->seek(-1, SEEK_CUR); break; @@ -2064,7 +2065,7 @@ QPDF::pipeStreamData(int objid, int generation, "unexpected EOF reading stream data"); } length -= len; - pipeline->write((unsigned char*)buf, len); + pipeline->write(QUtil::unsigned_char_pointer(buf), len); } } catch (QPDFExc& e) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 1a7dfc73..e6f53d08 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -508,7 +508,7 @@ QPDFObjectHandle::replaceStreamData(std::string const& data, assertStream(); PointerHolder b = new Buffer(data.length()); unsigned char* bp = b->getBuffer(); - memcpy(bp, (char*)data.c_str(), data.length()); + memcpy(bp, data.c_str(), data.length()); dynamic_cast(obj.getPointer())->replaceStreamData( b, filter, decode_parms); } @@ -690,7 +690,7 @@ QPDFObjectHandle::parse(std::string const& object_str, bool empty = false; QPDFObjectHandle result = 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()) { if (! isspace(object_str[offset])) @@ -748,7 +748,7 @@ QPDFObjectHandle::parseContentStream_internal(QPDFObjectHandle stream, QPDFTokenizer tokenizer; tokenizer.allowEOF(); bool empty = false; - while ((size_t) input->tell() < length) + while (static_cast(input->tell()) < length) { QPDFObjectHandle obj = parseInternal(input, "content", tokenizer, empty, diff --git a/libqpdf/QPDFTokenizer.cc b/libqpdf/QPDFTokenizer.cc index a6333b73..19442bab 100644 --- a/libqpdf/QPDFTokenizer.cc +++ b/libqpdf/QPDFTokenizer.cc @@ -80,7 +80,7 @@ QPDFTokenizer::resolveLiteral() num[0] = p[1]; num[1] = p[2]; num[2] = '\0'; - char ch = (char)(strtol(num, 0, 16)); + char ch = static_cast(strtol(num, 0, 16)); if (ch == '\0') { type = tt_bad; @@ -273,7 +273,7 @@ QPDFTokenizer::presentCharacter(char ch) { // We've accumulated \ddd. PDF Spec says to ignore // high-order overflow. - val += (char) strtol(bs_num_register, 0, 8); + val += static_cast(strtol(bs_num_register, 0, 8)); memset(bs_num_register, '\0', sizeof(bs_num_register)); bs_num_count = 0; } @@ -399,7 +399,7 @@ QPDFTokenizer::presentCharacter(char ch) { num[0] = val[i]; num[1] = val[i+1]; - char nch = (char)(strtol(num, 0, 16)); + char nch = static_cast(strtol(num, 0, 16)); nval += nch; } val = nval; @@ -511,7 +511,7 @@ QPDFTokenizer::readToken(PointerHolder input, } else { - if (is_space((unsigned char)ch) && + if (is_space(static_cast(ch)) && (input->getLastOffset() == offset)) { ++offset; diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index a1949a9b..6384df54 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -772,7 +772,7 @@ QPDFWriter::writeBinary(unsigned long long val, unsigned int bytes) unsigned char data[sizeof(unsigned long long)]; for (unsigned int i = 0; i < bytes; ++i) { - data[bytes - i - 1] = (unsigned char)(val & 0xff); + data[bytes - i - 1] = static_cast(val & 0xff); val >>= 8; } this->pipeline->write(data, bytes); @@ -781,7 +781,7 @@ QPDFWriter::writeBinary(unsigned long long val, unsigned int bytes) void 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 @@ -887,14 +887,14 @@ QPDFWriter::pushEncryptionFilter() { p = new Pl_AES_PDF( "aes stream encryption", this->pipeline, true, - (unsigned char*) this->cur_data_key.c_str(), - (unsigned int)this->cur_data_key.length()); + QUtil::unsigned_char_pointer(this->cur_data_key), + this->cur_data_key.length()); } else { p = new Pl_RC4("rc4 stream encryption", this->pipeline, - (unsigned char*) this->cur_data_key.c_str(), - (unsigned int)this->cur_data_key.length()); + QUtil::unsigned_char_pointer(this->cur_data_key), + this->cur_data_key.length()); } pushPipeline(p); } @@ -1087,7 +1087,7 @@ QPDFWriter::writeTrailer(trailer_e which, int size, bool xref_stream, writeString(" /Prev "); qpdf_offset_t pos = this->pipeline->getCount(); writeString(QUtil::int_to_string(prev)); - int nspaces = (int)(pos - this->pipeline->getCount() + 21); + int nspaces = pos - this->pipeline->getCount() + 21; assert(nspaces >= 0); writePad(nspaces); } @@ -1504,14 +1504,15 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, if (this->encrypt_use_aes) { Pl_Buffer bufpl("encrypted string"); - Pl_AES_PDF pl("aes encrypt string", &bufpl, true, - (unsigned char const*)this->cur_data_key.c_str(), - (unsigned int)this->cur_data_key.length()); - pl.write((unsigned char*) val.c_str(), val.length()); + Pl_AES_PDF pl( + "aes encrypt string", &bufpl, true, + QUtil::unsigned_char_pointer(this->cur_data_key), + this->cur_data_key.length()); + pl.write(QUtil::unsigned_char_pointer(val), val.length()); pl.finish(); Buffer* buf = bufpl.getBuffer(); val = QPDF_String( - std::string((char*)buf->getBuffer(), + std::string(reinterpret_cast(buf->getBuffer()), buf->getSize())).unparse(true); delete buf; } @@ -1519,9 +1520,9 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level, { char* tmp = QUtil::copy_string(val); size_t vlen = val.length(); - RC4 rc4((unsigned char const*)this->cur_data_key.c_str(), - (int)this->cur_data_key.length()); - rc4.process((unsigned char*)tmp, (int)vlen); + RC4 rc4(QUtil::unsigned_char_pointer(this->cur_data_key), + this->cur_data_key.length()); + rc4.process(QUtil::unsigned_char_pointer(tmp), vlen); val = QPDF_String(std::string(tmp, vlen)).unparse(); delete [] tmp; } @@ -1788,7 +1789,7 @@ QPDFWriter::generateID() 0x23, 0x84, 0x62, 0x64, 0x33, 0x83, 0x27, 0x95, 0x00}; - result = (char*)tmp; + result = reinterpret_cast(tmp); } else { @@ -1799,7 +1800,7 @@ QPDFWriter::generateID() // the file yet. This scheme should be fine though. 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 += this->filename; seed += " "; @@ -1823,7 +1824,8 @@ QPDFWriter::generateID() m.encodeString(seed.c_str()); MD5::Digest digest; m.digest(digest); - result = std::string((char*)digest, sizeof(MD5::Digest)); + result = std::string(reinterpret_cast(digest), + sizeof(MD5::Digest)); } // 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. std::vector const& eligible = this->pdf.getCompressibleObjects(); - unsigned int n_object_streams = - (unsigned int)((eligible.size() + 99) / 100); - unsigned int n_per = (unsigned int)(eligible.size() / n_object_streams); + unsigned int n_object_streams = (eligible.size() + 99) / 100; + unsigned int n_per = eligible.size() / n_object_streams; if (n_per * n_object_streams < eligible.size()) { ++n_per; @@ -2206,7 +2207,8 @@ QPDFWriter::write() this->object_stream_to_objects[stream].insert(obj); this->max_ostream_index = std::max(this->max_ostream_index, - (int)this->object_stream_to_objects[stream].size() - 1); + static_cast( + this->object_stream_to_objects[stream].size()) - 1); } if (! this->object_stream_to_objects.empty()) @@ -2553,8 +2555,7 @@ QPDFWriter::writeLinearized() // // Second half objects - int second_half_uncompressed = - (int)(part7.size() + part8.size() + part9.size()); + int second_half_uncompressed = part7.size() + part8.size() + part9.size(); int second_half_first_obj = 1; int after_second_half = 1 + second_half_uncompressed; this->next_objid = after_second_half; @@ -2661,7 +2662,7 @@ QPDFWriter::writeLinearized() { std::vector const& pages = pdf.getAllPages(); int first_page_object = obj_renumber[pages[0].getObjectID()]; - int npages = (int)pages.size(); + int npages = pages.size(); writeString(" /Linearized 1 /L "); writeString(QUtil::int_to_string(file_size + hint_length)); @@ -2821,7 +2822,7 @@ QPDFWriter::writeLinearized() // If this assertion fails, maybe we didn't have // enough padding above. assert(this->pipeline->getCount() == - (qpdf_offset_t)(second_xref_end + hint_length)); + second_xref_end + hint_length); } } else @@ -2849,7 +2850,7 @@ QPDFWriter::writeLinearized() activatePipelineStack(); writeHintStream(hint_id); popPipelineStack(&hint_buffer); - hint_length = (qpdf_offset_t)hint_buffer->getSize(); + hint_length = hint_buffer->getSize(); // Restore hint offset this->xref[hint_id] = QPDFXRefEntry(1, hint_offset, 0); diff --git a/libqpdf/QPDFXRefEntry.cc b/libqpdf/QPDFXRefEntry.cc index 847fc8e6..2e458ada 100644 --- a/libqpdf/QPDFXRefEntry.cc +++ b/libqpdf/QPDFXRefEntry.cc @@ -46,7 +46,7 @@ QPDFXRefEntry::getObjStreamNumber() const throw std::logic_error( "getObjStreamNumber called for xref entry of type != 2"); } - return (int) this->field1; + return this->field1; } int diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc index 0c2be92f..00903fa6 100644 --- a/libqpdf/QPDF_Array.cc +++ b/libqpdf/QPDF_Array.cc @@ -49,13 +49,13 @@ QPDF_Array::getTypeName() const int QPDF_Array::getNItems() const { - return (int)this->items.size(); + return this->items.size(); } QPDFObjectHandle QPDF_Array::getItem(int n) const { - if ((n < 0) || (n >= (int)this->items.size())) + if ((n < 0) || (n >= static_cast(this->items.size()))) { throw std::logic_error( "INTERNAL ERROR: bounds error accessing QPDF_Array element"); @@ -87,7 +87,7 @@ void QPDF_Array::insertItem(int at, QPDFObjectHandle const& item) { // As special case, also allow insert beyond the end - if ((at < 0) || (at > (int)this->items.size())) + if ((at < 0) || (at > static_cast(this->items.size()))) { throw std::logic_error( "INTERNAL ERROR: bounds error accessing QPDF_Array element"); diff --git a/libqpdf/QPDF_Stream.cc b/libqpdf/QPDF_Stream.cc index 15024849..6ca88caf 100644 --- a/libqpdf/QPDF_Stream.cc +++ b/libqpdf/QPDF_Stream.cc @@ -519,7 +519,7 @@ QPDF_Stream::replaceFilterData(QPDFObjectHandle const& filter, else { this->stream_dict.replaceKey( - "/Length", QPDFObjectHandle::newInteger((int)length)); + "/Length", QPDFObjectHandle::newInteger(length)); } } diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc index 619adef6..db6fb2f5 100644 --- a/libqpdf/QPDF_String.cc +++ b/libqpdf/QPDF_String.cc @@ -140,7 +140,7 @@ QPDF_String::unparse(bool force_binary) } else { - sprintf(num, "\\%03o", (unsigned char)ch); + sprintf(num, "\\%03o", static_cast(ch)); result += num; } break; @@ -181,8 +181,8 @@ QPDF_String::getUTF8Val() const // discarded, and a low codepoint not preceded by a high // codepoint will just get its low 10 bits output. unsigned short bits = - (((unsigned char) this->val[i]) << 8) + - ((unsigned char) this->val[i+1]); + (static_cast(this->val[i]) << 8) + + static_cast(this->val[i+1]); if ((bits & 0xFC00) == 0xD800) { codepoint = 0x10000 + ((bits & 0x3FF) << 10); @@ -209,7 +209,7 @@ QPDF_String::getUTF8Val() const { for (unsigned int i = 0; i < len; ++i) { - result += QUtil::toUTF8((unsigned char) this->val[i]); + result += QUtil::toUTF8(static_cast(this->val[i])); } } return result; diff --git a/libqpdf/QPDF_encryption.cc b/libqpdf/QPDF_encryption.cc index 60d54b77..c49f4b26 100644 --- a/libqpdf/QPDF_encryption.cc +++ b/libqpdf/QPDF_encryption.cc @@ -129,7 +129,8 @@ QPDF::EncryptionData::setV5EncryptionParameters( static void 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(key_bytes), + password.length()); int pad_bytes = key_bytes - password_bytes; memcpy(k1, password.c_str(), password_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 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(127), password.length())); } static void @@ -187,7 +189,8 @@ iterate_md5_digest(MD5& md5, MD5::Digest& digest, int iterations) for (int i = 0; i < iterations; ++i) { MD5 m; - m.encodeDataIncrementally((char*)digest, sizeof(digest)); + m.encodeDataIncrementally(reinterpret_cast(digest), + sizeof(digest)); m.digest(digest); } } @@ -223,8 +226,8 @@ process_with_aes(std::string const& key, { Pl_Buffer buffer("buffer"); Pl_AES_PDF aes("aes", &buffer, encrypt, - (unsigned char const*)key.c_str(), - (unsigned int)key.length()); + QUtil::unsigned_char_pointer(key), + key.length()); if (iv) { aes.setIV(iv, iv_length); @@ -236,7 +239,7 @@ process_with_aes(std::string const& key, aes.disablePadding(); 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(); PointerHolder bufp = buffer.getBuffer(); @@ -248,7 +251,7 @@ process_with_aes(std::string const& key, { outlength = std::min(outlength, bufp->getSize()); } - return std::string((char const*)bufp->getBuffer(), outlength); + return std::string(reinterpret_cast(bufp->getBuffer()), outlength); } static std::string @@ -258,9 +261,9 @@ hash_V5(std::string const& password, QPDF::EncryptionData const& data) { Pl_SHA2 hash(256); - hash.write((unsigned char*)password.c_str(), password.length()); - hash.write((unsigned char*)salt.c_str(), salt.length()); - hash.write((unsigned char*)udata.c_str(), udata.length()); + hash.write(QUtil::unsigned_char_pointer(password), password.length()); + hash.write(QUtil::unsigned_char_pointer(salt), salt.length()); + hash.write(QUtil::unsigned_char_pointer(udata), udata.length()); hash.finish(); std::string K = hash.getRawDigest(); @@ -299,7 +302,7 @@ hash_V5(std::string const& password, assert(K.length() >= 32); std::string E = process_with_aes( 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 // 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; for (unsigned int i = 0; i < 16; ++i) { - E_mod_3 += (unsigned char)E[i]; + E_mod_3 += static_cast(E[i]); } E_mod_3 %= 3; int next_hash = ((E_mod_3 == 0) ? 256 : (E_mod_3 == 1) ? 384 : 512); 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(); K = hash.getRawDigest(); if (round_number >= 64) { - unsigned int ch = (unsigned int)((unsigned char) *(E.rbegin())); + unsigned int ch = static_cast(*(E.rbegin())); - if (ch <= (unsigned int)(round_number - 32)) + if (ch <= static_cast(round_number - 32)) { 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 - result += (char) (objid & 0xff); - result += (char) ((objid >> 8) & 0xff); - result += (char) ((objid >> 16) & 0xff); - result += (char) (generation & 0xff); - result += (char) ((generation >> 8) & 0xff); + result += static_cast(objid & 0xff); + result += static_cast((objid >> 8) & 0xff); + result += static_cast((objid >> 16) & 0xff); + result += static_cast(generation & 0xff); + result += static_cast((generation >> 8) & 0xff); if (use_aes) { result += "sAlT"; } MD5 md5; - md5.encodeDataIncrementally(result.c_str(), (int)result.length()); + md5.encodeDataIncrementally(result.c_str(), result.length()); MD5::Digest digest; md5.digest(digest); - return std::string((char*) digest, - std::min(result.length(), (size_t) 16)); + return std::string(reinterpret_cast(digest), + std::min(result.length(), static_cast(16))); } std::string @@ -409,13 +412,13 @@ QPDF::compute_encryption_key_from_password( md5.encodeDataIncrementally(data.getO().c_str(), key_bytes); char pbytes[4]; int P = data.getP(); - pbytes[0] = (char) (P & 0xff); - pbytes[1] = (char) ((P >> 8) & 0xff); - pbytes[2] = (char) ((P >> 16) & 0xff); - pbytes[3] = (char) ((P >> 24) & 0xff); + pbytes[0] = static_cast(P & 0xff); + pbytes[1] = static_cast((P >> 8) & 0xff); + pbytes[2] = static_cast((P >> 16) & 0xff); + pbytes[3] = static_cast((P >> 24) & 0xff); md5.encodeDataIncrementally(pbytes, 4); md5.encodeDataIncrementally(data.getId1().c_str(), - (int)data.getId1().length()); + data.getId1().length()); if ((data.getR() >= 4) && (! data.getEncryptMetadata())) { char bytes[4]; @@ -424,7 +427,7 @@ QPDF::compute_encryption_key_from_password( } MD5::Digest digest; iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0)); - return std::string((char*)digest, data.getLengthBytes()); + return std::string(reinterpret_cast(digest), data.getLengthBytes()); } static void @@ -463,7 +466,7 @@ compute_O_value(std::string const& user_password, char upass[key_bytes]; 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(), (data.getR() >= 3) ? 20 : 1, false); 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); char udata[key_bytes]; pad_or_truncate_password_V4("", udata); - iterate_rc4((unsigned char*) udata, key_bytes, - (unsigned char*)k1.c_str(), data.getLengthBytes(), 1, false); + iterate_rc4(QUtil::unsigned_char_pointer(udata), key_bytes, + QUtil::unsigned_char_pointer(k1), + data.getLengthBytes(), 1, false); return std::string(udata, key_bytes); } @@ -496,18 +500,19 @@ compute_U_value_R3(std::string const& user_password, md5.encodeDataIncrementally( pad_or_truncate_password_V4("").c_str(), key_bytes); md5.encodeDataIncrementally(data.getId1().c_str(), - (int)data.getId1().length()); + data.getId1().length()); MD5::Digest digest; md5.digest(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]; memcpy(result, digest, sizeof(MD5::Digest)); // pad with arbitrary data -- make it consistent for the sake of // testing for (unsigned int i = sizeof(MD5::Digest); i < key_bytes; ++i) { - result[i] = (char)((i * i) % 0xff); + result[i] = static_cast((i * i) % 0xff); } 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]; compute_O_rc4_key(user_password, owner_password, data, key); 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(), (data.getR() >= 3) ? 20 : 1, true); std::string new_user_password = - std::string((char*)O_data, key_bytes); + std::string(reinterpret_cast(O_data), key_bytes); bool result = false; 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 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 key_salt(k + 8, 8); 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 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 key_salt(k + 8, 8); 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(); for (int i = 0; i < 8; ++i) { - k[i] = (unsigned char) (extended_perms & 0xff); + k[i] = static_cast(extended_perms & 0xff); extended_perms >>= 8; } 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 unsigned char k[16]; compute_Perms_value_V5_clear(encryption_key, data, k); - return process_with_aes(encryption_key, true, - std::string((char const*) k, sizeof(k))); + return process_with_aes( + encryption_key, true, + std::string(reinterpret_cast(k), sizeof(k))); } std::string @@ -834,7 +842,7 @@ QPDF::initializeEncryption() int R = encryption_dict.getKey("/R").getIntValue(); std::string O = encryption_dict.getKey("/O").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 // 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"); Pl_Buffer bufpl("decrypted string"); Pl_AES_PDF pl("aes decrypt string", &bufpl, false, - (unsigned char const*)key.c_str(), - (unsigned int)key.length()); - pl.write((unsigned char*)str.c_str(), str.length()); + QUtil::unsigned_char_pointer(key), + key.length()); + pl.write(QUtil::unsigned_char_pointer(str), str.length()); pl.finish(); PointerHolder buf = bufpl.getBuffer(); - str = std::string((char*)buf->getBuffer(), buf->getSize()); + str = std::string(reinterpret_cast(buf->getBuffer()), + buf->getSize()); } else { 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 // be freed even if rc4.process throws an exception. PointerHolder tmp(true, QUtil::copy_string(str)); - RC4 rc4((unsigned char const*)key.c_str(), (int)key.length()); - rc4.process((unsigned char*)tmp.getPointer(), vlen); + RC4 rc4(QUtil::unsigned_char_pointer(key), key.length()); + rc4.process(QUtil::unsigned_char_pointer(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"); pipeline = new Pl_AES_PDF("AES stream decryption", pipeline, - false, (unsigned char*) key.c_str(), - (unsigned int) key.length()); + false, QUtil::unsigned_char_pointer(key), + key.length()); } else { QTC::TC("qpdf", "QPDF_encryption rc4 decode stream"); pipeline = new Pl_RC4("RC4 stream decryption", pipeline, - (unsigned char*) key.c_str(), - (unsigned int) key.length()); + QUtil::unsigned_char_pointer(key), + key.length()); } heap.push_back(pipeline); } @@ -1285,7 +1294,7 @@ QPDF::compute_encryption_parameters_V5( id1, encrypt_metadata); unsigned char k[key_bytes]; QUtil::initializeWithRandomBytes(k, key_bytes); - encryption_key = std::string((char const*)k, key_bytes); + encryption_key = std::string(reinterpret_cast(k), key_bytes); 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); Perms = compute_Perms_value_V5(encryption_key, data); diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index 65f2b99e..d047ad11 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -109,7 +109,7 @@ QPDF::isLinearized() } else { - p = (char*)memchr(p, '\0', tbuf_size - (p - buf)); + p = reinterpret_cast(memchr(p, '\0', tbuf_size - (p - buf))); assert(p != 0); while ((p - buf < tbuf_size) && (*p == 0)) { @@ -136,7 +136,8 @@ QPDF::isLinearized() } QPDFObjectHandle linkey = candidate.getKey("/Linearized"); - if (! (linkey.isNumber() && ((int)floor(linkey.getNumericValue()) == 1))) + if (! (linkey.isNumber() && + (static_cast(floor(linkey.getNumericValue())) == 1))) { return false; } @@ -289,7 +290,7 @@ QPDF::readLinearizationData() PointerHolder hbp = pb.getBuffer(); Buffer* hb = hbp.getPointer(); unsigned char const* h_buf = hb->getBuffer(); - int h_size = (int)hb->getSize(); + int h_size = hb->getSize(); 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"); } - qpdf_offset_t computed_end = offset + (qpdf_offset_t)length; + qpdf_offset_t computed_end = offset + length; if ((computed_end < min_end_offset) || (computed_end > max_end_offset)) { @@ -488,7 +489,7 @@ QPDF::checkLinearizationInternal() } // N: number of pages - int npages = (int)pages.size(); + int npages = pages.size(); if (p.npages != npages) { // Not tested in the test suite @@ -576,8 +577,8 @@ QPDF::checkLinearizationInternal() // contain any files with threads. assert(! this->part6.empty()); - int min_E = -1; - int max_E = -1; + qpdf_offset_t min_E = -1; + qpdf_offset_t max_E = -1; for (std::vector::iterator iter = this->part6.begin(); iter != this->part6.end(); ++iter) { @@ -585,8 +586,8 @@ QPDF::checkLinearizationInternal() // All objects have to have been dereferenced to be classified. assert(this->obj_cache.count(og) > 0); ObjCache const& oc = this->obj_cache[og]; - min_E = std::max(min_E, (int)oc.end_before_space); - max_E = std::max(max_E, (int)oc.end_after_space); + min_E = std::max(min_E, oc.end_before_space); + max_E = std::max(max_E, oc.end_after_space); } if ((p.first_page_end < min_E) || (p.first_page_end > max_E)) { @@ -632,19 +633,18 @@ QPDF::checkLinearizationInternal() return result; } -int +qpdf_offset_t QPDF::maxEnd(ObjUser const& ou) { assert(this->obj_user_to_objects.count(ou) > 0); std::set const& ogs = this->obj_user_to_objects[ou]; - int end = 0; + qpdf_offset_t end = 0; for (std::set::const_iterator iter = ogs.begin(); iter != ogs.end(); ++iter) { ObjGen const& og = *iter; assert(this->obj_cache.count(og) > 0); - end = std::max( - end, (int)(this->obj_cache[og].end_after_space)); + end = std::max(end, this->obj_cache[og].end_after_space); } return end; } @@ -736,7 +736,7 @@ QPDF::checkHPageOffset(std::list& errors, // under a page's /Resources dictionary in with shared objects // even when they are private. - unsigned int npages = (unsigned int)pages.size(); + unsigned int npages = pages.size(); int table_offset = adjusted_offset( this->page_offset_hints.first_page_offset); ObjGen first_page_og(pages[0].getObjectID(), pages[0].getGeneration()); @@ -1435,7 +1435,7 @@ QPDF::calculateLinearizationData(std::map const& 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 // tables. Specifically, we can initialize any items that deal @@ -1505,7 +1505,7 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) // in garbage values for all the shared object identifiers on the // 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 @@ -1657,10 +1657,9 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) // Make sure we got everything exactly once. unsigned int num_placed = - (unsigned int)(this->part4.size() + this->part6.size() + - this->part7.size() + this->part8.size() + - this->part9.size()); - unsigned int num_wanted = (unsigned int)this->object_to_obj_users.size(); + this->part4.size() + this->part6.size() + this->part7.size() + + this->part8.size() + this->part9.size(); + unsigned int num_wanted = this->object_to_obj_users.size(); if (num_placed != num_wanted) { throw std::logic_error( @@ -1684,11 +1683,9 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) // can map from object number only without regards to generation. std::map obj_to_index; - this->c_shared_object_data.nshared_first_page = - (unsigned int)this->part6.size(); + this->c_shared_object_data.nshared_first_page = this->part6.size(); this->c_shared_object_data.nshared_total = - this->c_shared_object_data.nshared_first_page + - (unsigned int) this->part8.size(); + this->c_shared_object_data.nshared_first_page + this->part8.size(); std::vector& shared = this->c_shared_object_data.entries; @@ -1697,7 +1694,7 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) { QPDFObjectHandle& oh = *iter; int obj = oh.getObjectID(); - obj_to_index[obj] = (int)shared.size(); + obj_to_index[obj] = shared.size(); shared.push_back(CHSharedObjectEntry(obj)); } QTC::TC("qpdf", "QPDF lin part 8 empty", this->part8.empty() ? 1 : 0); @@ -1711,12 +1708,12 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) { QPDFObjectHandle& oh = *iter; int obj = oh.getObjectID(); - obj_to_index[obj] = (int)shared.size(); + obj_to_index[obj] = shared.size(); shared.push_back(CHSharedObjectEntry(obj)); } } - assert(this->c_shared_object_data.nshared_total == - (int) this->c_shared_object_data.entries.size()); + assert(static_cast(this->c_shared_object_data.nshared_total) == + this->c_shared_object_data.entries.size()); // Now compute the list of shared objects for each page after the // first page. @@ -1827,7 +1824,7 @@ QPDF::calculateHPageOffset( // values. std::vector const& pages = getAllPages(); - unsigned int npages = (unsigned int)pages.size(); + unsigned int npages = pages.size(); CHPageOffset& cph = this->c_page_offset_data; std::vector& cphe = cph.entries; @@ -2032,7 +2029,7 @@ QPDF::writeHPageOffset(BitWriter& w) w.writeBits(t.nbits_shared_numerator, 16); // 12 w.writeBits(t.shared_denominator, 16); // 13 - unsigned int nitems = (unsigned int)getAllPages().size(); + unsigned int nitems = getAllPages().size(); std::vector& entries = t.entries; write_vector_int(w, nitems, entries, @@ -2123,12 +2120,12 @@ QPDF::generateHintStream(std::map const& xref, BitWriter w(&c); writeHPageOffset(w); - S = (int)c.getCount(); + S = c.getCount(); writeHSharedObject(w); O = 0; if (this->outline_hints.nobjects > 0) { - O = (int)c.getCount(); + O = c.getCount(); writeHGeneric(w, this->outline_hints); } c.finish(); diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index f832a883..befa871e 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -73,7 +73,7 @@ QPDF::optimize(std::map const& object_stream_data, pushInheritedAttributesToPage(allow_changes, false); // Traverse pages - int n = (int)this->all_pages.size(); + int n = this->all_pages.size(); for (int pageno = 0; pageno < n; ++pageno) { updateObjectMaps(ObjUser(ObjUser::ou_page, pageno), diff --git a/libqpdf/QPDF_pages.cc b/libqpdf/QPDF_pages.cc index 36ca951d..11f57dd9 100644 --- a/libqpdf/QPDF_pages.cc +++ b/libqpdf/QPDF_pages.cc @@ -110,7 +110,7 @@ QPDF::flattenPagesTree() 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) { // 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", (pos == 0) ? 0 : // insert at beginning - (pos == ((int)this->all_pages.size())) ? 1 : // insert at end + (pos == static_cast(this->all_pages.size())) ? 1 : // at end 2); // insert in middle QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle kids = pages.getKey("/Kids"); - assert ((pos >= 0) && (pos <= (int)this->all_pages.size())); + assert ((pos >= 0) && + (static_cast(pos) <= this->all_pages.size())); newpage.replaceKey("/Parent", pages); kids.insertItem(pos, newpage); int npages = kids.getArrayNItems(); pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); this->all_pages.insert(this->all_pages.begin() + pos, newpage); - assert((int)this->all_pages.size() == npages); + assert(this->all_pages.size() == static_cast(npages)); for (int i = pos + 1; i < npages; ++i) { insertPageobjToPage(this->all_pages[i], i, false); } insertPageobjToPage(newpage, pos, true); - assert((int)this->pageobj_to_pages_pos.size() == npages); + assert(this->pageobj_to_pages_pos.size() == static_cast(npages)); } void @@ -201,9 +202,9 @@ QPDF::removePage(QPDFObjectHandle page) { int pos = findPage(page); // also ensures flat /Pages QTC::TC("qpdf", "QPDF remove page", - (pos == 0) ? 0 : // remove at beginning - (pos == ((int)this->all_pages.size() - 1)) ? 1 : // remove at end - 2); // remove in middle + (pos == 0) ? 0 : // remove at beginning + (pos == static_cast(this->all_pages.size() - 1)) ? 1 : // end + 2); // remove in middle QPDFObjectHandle pages = getRoot().getKey("/Pages"); QPDFObjectHandle kids = pages.getKey("/Kids"); @@ -212,10 +213,10 @@ QPDF::removePage(QPDFObjectHandle page) int npages = kids.getArrayNItems(); pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages)); this->all_pages.erase(this->all_pages.begin() + pos); - assert((int)this->all_pages.size() == npages); + assert(this->all_pages.size() == static_cast(npages)); this->pageobj_to_pages_pos.erase( ObjGen(page.getObjectID(), page.getGeneration())); - assert((int)this->pageobj_to_pages_pos.size() == npages); + assert(this->pageobj_to_pages_pos.size() == static_cast(npages)); for (int i = pos; i < npages; ++i) { insertPageobjToPage(this->all_pages[i], i, false); diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index faccaee7..9549bfa1 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -26,7 +26,7 @@ QUtil::int_to_string(long long num, int fullpad) char t[50]; // -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 " "a padding value greater than its internal " @@ -58,7 +58,7 @@ QUtil::double_to_string(double num, int decimal_places) // 99 digits. char t[100]; - std::string lhs = int_to_string((int)num); + std::string lhs = int_to_string(static_cast(num)); // 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 @@ -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 // so trust the buffer is big enough. The following test will // always pass in those cases. - if (decimal_places + 1 + (int)lhs.length() > (int)sizeof(t) - 1) + if (decimal_places + 1 + static_cast(lhs.length()) > + static_cast(sizeof(t)) - 1) { throw std::logic_error("Util::double_to_string has been called with " "a number and a decimal places specification " @@ -96,6 +97,18 @@ QUtil::string_to_ll(char const* str) #endif } +unsigned char* +QUtil::unsigned_char_pointer(std::string const& str) +{ + return reinterpret_cast(const_cast(str.c_str())); +} + +unsigned char* +QUtil::unsigned_char_pointer(char const* str) +{ + return reinterpret_cast(const_cast(str)); +} + void QUtil::throw_system_error(std::string const& description) { @@ -126,14 +139,14 @@ int QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence) { #if HAVE_FSEEKO - return fseeko(stream, (off_t)offset, whence); + return fseeko(stream, static_cast(offset), whence); #elif HAVE_FSEEKO64 return fseeko64(stream, offset, whence); #else # ifdef _MSC_VER return _fseeki64(stream, offset, whence); # else - return fseek(stream, (long)offset, whence); + return fseek(stream, static_cast(offset), whence); # endif #endif } @@ -142,14 +155,14 @@ qpdf_offset_t QUtil::tell(FILE* stream) { #if HAVE_FSEEKO - return (qpdf_offset_t)ftello(stream); + return static_cast(ftello(stream)); #elif HAVE_FSEEKO64 - return (qpdf_offset_t)ftello64(stream); + return static_cast(ftello64(stream)); #else # ifdef _MSC_VER return _ftelli64(stream); # else - return (qpdf_offset_t)ftell(stream); + return static_cast(ftell(stream)); # endif #endif } @@ -174,7 +187,7 @@ QUtil::hex_encode(std::string const& input) buf[hex_size - 1] = '\0'; 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(input[i])); } return buf; } @@ -199,7 +212,7 @@ void QUtil::setLineBuf(FILE* f) { #ifndef _WIN32 - setvbuf(f, (char *) NULL, _IOLBF, 0); + setvbuf(f, reinterpret_cast(NULL), _IOLBF, 0); #endif } @@ -314,7 +327,7 @@ QUtil::toUTF8(unsigned long uval) } else if (uval < 128) { - result += (char)(uval); + result += static_cast(uval); } else { @@ -329,7 +342,7 @@ QUtil::toUTF8(unsigned long uval) { // Assign low six bits plus 10000000 to lowest unused // byte position, then shift - *cur_byte = (unsigned char) (0x80 + (uval & 0x3f)); + *cur_byte = static_cast(0x80 + (uval & 0x3f)); uval >>= 6; // Maximum that will fit in high byte now shrinks by one bit maxval >>= 1; @@ -342,9 +355,10 @@ QUtil::toUTF8(unsigned long uval) } // If maxval is k bits long, the high (7 - k) bits of the // resulting byte must be high. - *cur_byte = (unsigned char)((0xff - (1 + (maxval << 1))) + uval); + *cur_byte = static_cast( + (0xff - (1 + (maxval << 1))) + uval); - result += (char*)cur_byte; + result += reinterpret_cast(cur_byte); } return result; @@ -358,8 +372,8 @@ QUtil::random() { // Seed the random number generator with something simple, but // just to be interesting, don't use the unmodified current - // time.... - QUtil::srandom((int)QUtil::get_current_time() ^ 0xcccc); + // time. It would be better if this were a more secure seed. + QUtil::srandom(QUtil::get_current_time() ^ 0xcccc); seeded_random = true; } @@ -385,6 +399,6 @@ QUtil::initializeWithRandomBytes(unsigned char* data, size_t len) { for (size_t i = 0; i < len; ++i) { - data[i] = (unsigned char)((QUtil::random() & 0xff0) >> 4); + data[i] = static_cast((QUtil::random() & 0xff0) >> 4); } } diff --git a/libqpdf/RC4.cc b/libqpdf/RC4.cc index b992937f..7639a364 100644 --- a/libqpdf/RC4.cc +++ b/libqpdf/RC4.cc @@ -15,7 +15,7 @@ RC4::RC4(unsigned char const* key_data, int key_len) { if (key_len == -1) { - key_len = (int)strlen((char*)key_data); + key_len = strlen(reinterpret_cast(key_data)); } for (int i = 0; i < 256; ++i) diff --git a/libqpdf/rijndael.cc b/libqpdf/rijndael.cc index 64748d82..7f711df7 100644 --- a/libqpdf/rijndael.cc +++ b/libqpdf/rijndael.cc @@ -693,15 +693,17 @@ static const u32 rcon[] = /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ }; -#define GETU32(plaintext) (((u32)(plaintext)[0] << 24) ^ \ - ((u32)(plaintext)[1] << 16) ^ \ - ((u32)(plaintext)[2] << 8) ^ \ - ((u32)(plaintext)[3])) +#define GETU32(plaintext) \ + ((static_cast((plaintext)[0]) << 24) ^ \ + (static_cast((plaintext)[1]) << 16) ^ \ + (static_cast((plaintext)[2]) << 8) ^ \ + (static_cast((plaintext)[3]))) -#define PUTU32(ciphertext, st) { (ciphertext)[0] = (u8)((st) >> 24); \ - (ciphertext)[1] = (u8)((st) >> 16); \ - (ciphertext)[2] = (u8)((st) >> 8); \ - (ciphertext)[3] = (u8)(st); } +#define PUTU32(ciphertext, st) { \ + (ciphertext)[0] = static_cast((st) >> 24); \ + (ciphertext)[1] = static_cast((st) >> 16); \ + (ciphertext)[2] = static_cast((st) >> 8); \ + (ciphertext)[3] = static_cast(st); } /** * Expand the cipher key into the encryption key schedule. diff --git a/libtests/aes.cc b/libtests/aes.cc index 381148c1..5535cbb8 100644 --- a/libtests/aes.cc +++ b/libtests/aes.cc @@ -86,7 +86,7 @@ int main(int argc, char* argv[]) usage(); } - unsigned int hexkeylen = (unsigned int)strlen(hexkey); + unsigned int hexkeylen = strlen(hexkey); unsigned int keylen = hexkeylen / 2; FILE* infile = fopen(infilename, "rb"); @@ -112,7 +112,7 @@ int main(int argc, char* argv[]) t[2] = '\0'; long val = strtol(t, 0, 16); - key[i/2] = (unsigned char) val; + key[i/2] = static_cast(val); } Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); diff --git a/libtests/bits.cc b/libtests/bits.cc index 7837ac68..c14bceb6 100644 --- a/libtests/bits.cc +++ b/libtests/bits.cc @@ -37,7 +37,8 @@ test_write_bits(unsigned char& ch, unsigned int& bit_offset, unsigned long val, int bits, Pl_Buffer* 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(ch), bit_offset); } static void @@ -49,7 +50,7 @@ print_buffer(Pl_Buffer* bp) size_t l = b->getSize(); for (unsigned long i = 0; i < l; ++i) { - printf("%02x%s", (unsigned int)(p[i]), + printf("%02x%s", static_cast(p[i]), (i == l - 1) ? "\n" : " "); } printf("\n"); diff --git a/libtests/buffer.cc b/libtests/buffer.cc index 923be197..094a3284 100644 --- a/libtests/buffer.cc +++ b/libtests/buffer.cc @@ -1,11 +1,15 @@ #include #include #include +#include #include #include #include -typedef unsigned char* uc; +static unsigned char* uc(char const* s) +{ + return QUtil::unsigned_char_pointer(s); +} int main() { @@ -14,20 +18,20 @@ int main() Pl_Discard discard; Pl_Count count("count", &discard); Pl_Buffer bp1("bp1", &count); - bp1.write((uc)"12345", 5); - bp1.write((uc)"67890", 5); + bp1.write(uc("12345"), 5); + bp1.write(uc("67890"), 5); bp1.finish(); std::cout << "count: " << count.getCount() << std::endl; - bp1.write((uc)"abcde", 5); - bp1.write((uc)"fghij", 6); + bp1.write(uc("abcde"), 5); + bp1.write(uc("fghij"), 6); bp1.finish(); std::cout << "count: " << count.getCount() << std::endl; Buffer* b = bp1.getBuffer(); std::cout << "size: " << b->getSize() << std::endl; std::cout << "data: " << b->getBuffer() << std::endl; delete b; - bp1.write((uc)"qwert", 5); - bp1.write((uc)"yuiop", 6); + bp1.write(uc("qwert"), 5); + bp1.write(uc("yuiop"), 6); bp1.finish(); std::cout << "count: " << count.getCount() << std::endl; b = bp1.getBuffer(); @@ -36,8 +40,8 @@ int main() delete b; Pl_Buffer bp2("bp2"); - bp2.write((uc)"moo", 3); - bp2.write((uc)"quack", 6); + bp2.write(uc("moo"), 3); + bp2.write(uc("quack"), 6); try { delete bp2.getBuffer(); diff --git a/libtests/concatenate.cc b/libtests/concatenate.cc index cf4332cb..41f88eb6 100644 --- a/libtests/concatenate.cc +++ b/libtests/concatenate.cc @@ -1,12 +1,13 @@ #include #include #include +#include #include #include 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(); } @@ -25,7 +26,8 @@ int main(int argc, char* argv[]) inflate.write(b1_buf->getBuffer(), b1_buf->getSize()); inflate.finish(); PointerHolder b2_buf = b2.getBuffer(); - std::string result((char const*)b2_buf->getBuffer(), b2_buf->getSize()); + std::string result(reinterpret_cast(b2_buf->getBuffer()), + b2_buf->getSize()); if (result == "-one--two-") { std::cout << "concatenate test passed" << std::endl; diff --git a/libtests/qutil.cc b/libtests/qutil.cc index d1c090fb..ad77f3ea 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -152,7 +152,7 @@ static void print_utf8(unsigned long val) iter != result.end(); ++iter) { char t[3]; - sprintf(t, "%02x", (unsigned char) (*iter)); + sprintf(t, "%02x", static_cast(*iter)); std::cout << " " << t; } } diff --git a/libtests/rc4.cc b/libtests/rc4.cc index 1328fc85..59a9265e 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -17,7 +17,7 @@ int main(int argc, char* argv[]) char* hexkey = argv[1]; char* infilename = argv[2]; char* outfilename = argv[3]; - unsigned int hexkeylen = (unsigned int)strlen(hexkey); + unsigned int hexkeylen = strlen(hexkey); unsigned int keylen = hexkeylen / 2; unsigned char* key = new unsigned char[keylen + 1]; key[keylen] = '\0'; @@ -37,7 +37,7 @@ int main(int argc, char* argv[]) t[2] = '\0'; long val = strtol(t, 0, 16); - key[i/2] = (unsigned char) val; + key[i/2] = static_cast(val); } FILE* outfile = fopen(outfilename, "wb"); diff --git a/libtests/sha2.cc b/libtests/sha2.cc index 2b9aac3c..5110b89f 100644 --- a/libtests/sha2.cc +++ b/libtests/sha2.cc @@ -8,7 +8,7 @@ static void test(Pl_SHA2& sha2, char const* description, int bits, char const* input, std::string const& output) { sha2.resetBits(bits); - sha2.write((unsigned char*) input, strlen(input)); + sha2.write(QUtil::unsigned_char_pointer(input), strlen(input)); sha2.finish(); std::cout << description << ": "; if (output == sha2.getHexDigest()) diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index fe168cf5..7a0a50dd 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -881,7 +881,7 @@ QPDFPageData::QPDFPageData(QPDF* qpdf, char const* range) : qpdf(qpdf), 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, @@ -1012,7 +1012,7 @@ int main(int argc, char* argv[]) // Be lax about -arg vs --arg ++arg; } - char* parameter = (char*)strchr(arg, '='); + char* parameter = const_cast(strchr(arg, '=')); if (parameter) { *parameter++ = 0; diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index a1a467b8..3e780314 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -95,7 +95,8 @@ static std::string getPageContents(QPDFObjectHandle page) { PointerHolder b1 = page.getKey("/Contents").getStreamData(); - return std::string((char *)(b1->getBuffer()), b1->getSize()) + "\0"; + return std::string( + reinterpret_cast(b1->getBuffer()), b1->getSize()) + "\0"; } 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, fopen(filename1, "rb")); fseek(f, 0, SEEK_END); - size_t size = (size_t) QUtil::tell(f); + size_t size = QUtil::tell(f); fseek(f, 0, SEEK_SET); file_buf = PointerHolder(true, new char[size]); 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(); bool cleartext = false; if ((buf->getSize() > 9) && - (strncmp((char const*)data, "(data), + " b = p1.getBuffer(); // 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 PointerHolder buf = new Buffer(20); 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( &pdf, buf); QPDFObjectHandle rstream = QPDFObjectHandle::newStream(&pdf); @@ -824,7 +827,7 @@ void runtest(int n, char const* filename1, char const* arg2) page.replaceKey("/Parent", pages); pages.replaceKey( "/Count", - QPDFObjectHandle::newInteger(1 + (int)all_pages.size())); + QPDFObjectHandle::newInteger(1 + all_pages.size())); kids.appendItem(page); assert(all_pages.size() == 10); pdf.updateAllPagesCache(); @@ -1220,7 +1223,7 @@ void runtest(int n, char const* filename1, char const* arg2) { std::string const& filename = (*iter).first; std::string data = std::string( - (char const*)(*iter).second->getBuffer(), + reinterpret_cast((*iter).second->getBuffer()), (*iter).second->getSize()); bool is_binary = false; 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) { 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(20)); + ++i) { 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); PointerHolder buf = p1.getBuffer(); std::string data = std::string( - (char const*)buf->getBuffer(), buf->getSize()); + reinterpret_cast(buf->getBuffer()), + buf->getSize()); std::cout << stream.getDict().unparse() << filename << ":\n" << data << "--END--\n"; } diff --git a/qpdf/test_large_file.cc b/qpdf/test_large_file.cc index f54adb33..7ba5f6cb 100644 --- a/qpdf/test_large_file.cc +++ b/qpdf/test_large_file.cc @@ -135,7 +135,7 @@ ImageProvider::provideStreamData(int objid, int generation, for (int y = 0; y < nstripes; ++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->finish(); @@ -256,7 +256,8 @@ static void check_page_contents(int pageno, QPDFObjectHandle page) PointerHolder buf = page.getKey("/Contents").getStreamData(); std::string actual_contents = - std::string((char *)(buf->getBuffer()), buf->getSize()); + std::string(reinterpret_cast(buf->getBuffer()), + buf->getSize()); std::string expected_contents = generate_page_contents(pageno); if (expected_contents != actual_contents) { @@ -280,7 +281,7 @@ static void check_pdf(char const* filename) QPDF pdf; pdf.processFile(filename); std::vector const& pages = pdf.getAllPages(); - assert(pages.size() == (size_t)npages); + assert(pages.size() == static_cast(npages)); for (int i = 0; i < npages; ++i) { int pageno = i + 1;