Commit d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e

Authored by Jay Berkenbilt
1 parent f40ffc9d

Fix sign and conversion warnings (major)

This makes all integer type conversions that have potential data loss
explicit with calls that do range checks and raise an exception. After
this commit, qpdf builds with no warnings when -Wsign-conversion
-Wconversion is used with gcc or clang or when -W3 -Wd4800 is used
with MSVC. This significantly reduces the likelihood of potential
crashes from bogus integer values.

There are some parts of the code that take int when they should take
size_t or an offset. Such places would make qpdf not support files
with more than 2^31 of something that usually wouldn't be so large. In
the event that such a file shows up and is valid, at least qpdf would
raise an error in the right spot so the issue could be legitimately
addressed rather than failing in some weird way because of a silent
overflow condition.
Showing 79 changed files with 882 additions and 694 deletions
ChangeLog
1 1 2019-06-20 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * Fix all integer sign and conversion warnings. This makes all
  4 + integer type conversions that have potential data loss explicit
  5 + with calls that do range checks and raise an exception.
  6 +
3 7 * Change out_bufsize argument to Pl_Flate's constructor for int to
4 8 unsigned int for compatibility with underlying zlib
5 9 implementation.
6 10  
  11 + * Change QPDFObjectHandle::pipeStreamData's encode_flags argument
  12 + from unsigned long to int since int is the underlying type of the
  13 + enumerated type values that are passed to it. This change should
  14 + be invisible to virtually all code unless you are compiling with
  15 + strict warning flags and explicitly casting to unsigned long.
  16 +
7 17 * Add methods to QPDFObjectHandle to return the value of Integer
8 18 objects as int and unsigned int with range checking and fallback
9 19 behavior to avoid silent underflow/overflow conditions.
... ...
NOTICE.md
... ... @@ -10,8 +10,6 @@ Versions of qpdf prior to version 7 were released under the terms of version 2.0
10 10  
11 11 The qpdf distribution includes a copy of [qtest](http://qtest.qbilt.org), which is released under the terms of the [version 2.0 of the Artistic license](https://opensource.org/licenses/Artistic-2.0), which can be found at https://opensource.org/licenses/Artistic-2.0.
12 12  
13   -The standalone fuzz target runner (fuzz/standalone_fuzz_target_runner.cc) is copyright 2017 by Google and is also released under the Apache license, Version 2.0.
14   -
15 13 The Rijndael encryption implementation used as the basis for AES encryption and decryption support comes from Philip J. Erdelsky's public domain implementation. The files `libqpdf/rijndael.cc` and `libqpdf/qpdf/rijndael.h` remain in the public domain. They were obtained from
16 14 * http://www.efgh.com/software/rijndael.htm
17 15 * http://www.efgh.com/software/rijndael.txt
... ...
examples/pdf-create.cc
... ... @@ -14,6 +14,7 @@
14 14 #include <qpdf/Pl_Buffer.hh>
15 15 #include <qpdf/Pl_RunLength.hh>
16 16 #include <qpdf/Pl_DCT.hh>
  17 +#include <qpdf/QIntC.hh>
17 18 #include <iostream>
18 19 #include <string.h>
19 20 #include <stdlib.h>
... ... @@ -30,15 +31,15 @@ class ImageProvider: public QPDFObjectHandle::StreamDataProvider
30 31 virtual ~ImageProvider();
31 32 virtual void provideStreamData(int objid, int generation,
32 33 Pipeline* pipeline);
33   - int getWidth() const;
34   - int getHeight() const;
  34 + size_t getWidth() const;
  35 + size_t getHeight() const;
35 36  
36 37 private:
37   - int width;
38   - int stripe_height;
  38 + size_t width;
  39 + size_t stripe_height;
39 40 std::string color_space;
40 41 std::string filter;
41   - int n_stripes;
  42 + size_t n_stripes;
42 43 std::vector<std::string> stripes;
43 44 J_COLOR_SPACE j_color_space;
44 45 };
... ... @@ -88,13 +89,13 @@ ImageProvider::~ImageProvider()
88 89 {
89 90 }
90 91  
91   -int
  92 +size_t
92 93 ImageProvider::getWidth() const
93 94 {
94 95 return width;
95 96 }
96 97  
97   -int
  98 +size_t
98 99 ImageProvider::getHeight() const
99 100 {
100 101 return stripe_height * n_stripes;
... ... @@ -111,7 +112,8 @@ ImageProvider::provideStreamData(int objid, int generation,
111 112 {
112 113 p = new Pl_DCT(
113 114 "image encoder", pipeline,
114   - width, getHeight(), stripes[0].length(), j_color_space);
  115 + QIntC::to_uint(width), QIntC::to_uint(getHeight()),
  116 + QIntC::to_int(stripes[0].length()), j_color_space);
115 117 to_delete.push_back(p);
116 118 }
117 119 else if (filter == "/RunLengthDecode")
... ... @@ -121,9 +123,9 @@ ImageProvider::provideStreamData(int objid, int generation,
121 123 to_delete.push_back(p);
122 124 }
123 125  
124   - for (int i = 0; i < n_stripes; ++i)
  126 + for (size_t i = 0; i < n_stripes; ++i)
125 127 {
126   - for (int j = 0; j < width * stripe_height; ++j)
  128 + for (size_t j = 0; j < width * stripe_height; ++j)
127 129 {
128 130 p->write(
129 131 QUtil::unsigned_char_pointer(stripes[i].c_str()),
... ... @@ -155,9 +157,9 @@ QPDFObjectHandle newName(std::string const&amp; name)
155 157 return QPDFObjectHandle::newName(name);
156 158 }
157 159  
158   -QPDFObjectHandle newInteger(int val)
  160 +QPDFObjectHandle newInteger(size_t val)
159 161 {
160   - return QPDFObjectHandle::newInteger(val);
  162 + return QPDFObjectHandle::newInteger(QIntC::to_int(val));
161 163 }
162 164  
163 165 void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font,
... ... @@ -173,8 +175,8 @@ void add_page(QPDFPageDocumentHelper&amp; dh, QPDFObjectHandle font,
173 175 // compression.
174 176 ImageProvider* p = new ImageProvider(color_space, filter);
175 177 PointerHolder<QPDFObjectHandle::StreamDataProvider> provider(p);
176   - int width = p->getWidth();
177   - int height = p->getHeight();
  178 + size_t width = p->getWidth();
  179 + size_t height = p->getHeight();
178 180 QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf);
179 181 image.replaceDict(QPDFObjectHandle::parse(
180 182 "<<"
... ... @@ -335,8 +337,8 @@ static void check(char const* filename,
335 337 unsigned int mismatches = 0;
336 338 int tolerance = (
337 339 desired_filter == "/DCTDecode" ? 10 : 0);
338   - unsigned int threshold = (
339   - desired_filter == "/DCTDecode" ? len / 40 : 0);
  340 + size_t threshold = (
  341 + desired_filter == "/DCTDecode" ? len / 40U : 0);
340 342 for (size_t i = 0; i < len; ++i)
341 343 {
342 344 int delta = actual_bytes[i] - desired_bytes[i];
... ...
examples/pdf-double-page-size.cc
... ... @@ -34,7 +34,7 @@ static void doubleBoxSize(QPDFObjectHandle&amp; page, char const* box_name)
34 34 " is not an array of four elements");
35 35 }
36 36 std::vector<QPDFObjectHandle> doubled;
37   - for (unsigned int i = 0; i < 4; ++i)
  37 + for (int i = 0; i < 4; ++i)
38 38 {
39 39 doubled.push_back(
40 40 QPDFObjectHandle::newReal(
... ...
examples/pdf-invert-images.cc
... ... @@ -7,6 +7,7 @@
7 7 #include <qpdf/QUtil.hh>
8 8 #include <qpdf/Buffer.hh>
9 9 #include <qpdf/QPDFWriter.hh>
  10 +#include <qpdf/QIntC.hh>
10 11  
11 12 static char const* whoami = 0;
12 13  
... ... @@ -56,7 +57,7 @@ ImageInverter::provideStreamData(int objid, int generation,
56 57 unsigned char ch;
57 58 for (size_t i = 0; i < size; ++i)
58 59 {
59   - ch = static_cast<unsigned char>(0xff) - buf[i];
  60 + ch = QIntC::to_uchar(0xff - buf[i]);
60 61 pipeline->write(&ch, 1);
61 62 }
62 63 pipeline->finish();
... ...
examples/pdf-parse-content.cc
... ... @@ -6,6 +6,7 @@
6 6 #include <qpdf/QPDFPageDocumentHelper.hh>
7 7 #include <qpdf/QPDFPageObjectHelper.hh>
8 8 #include <qpdf/QUtil.hh>
  9 +#include <qpdf/QIntC.hh>
9 10  
10 11 static char const* whoami = 0;
11 12  
... ... @@ -72,12 +73,12 @@ int main(int argc, char* argv[])
72 73 pdf.processFile(filename);
73 74 std::vector<QPDFPageObjectHelper> pages =
74 75 QPDFPageDocumentHelper(pdf).getAllPages();
75   - if ((pageno < 1) || (static_cast<size_t>(pageno) > pages.size()))
  76 + if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size()))
76 77 {
77 78 usage();
78 79 }
79 80  
80   - QPDFPageObjectHelper& page = pages.at(pageno-1);
  81 + QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno-1));
81 82 ParserCallbacks cb;
82 83 page.parsePageContents(&cb);
83 84 }
... ...
examples/pdf-split-pages.cc
... ... @@ -8,6 +8,7 @@
8 8 #include <qpdf/QPDFPageDocumentHelper.hh>
9 9 #include <qpdf/QPDFWriter.hh>
10 10 #include <qpdf/QUtil.hh>
  11 +#include <qpdf/QIntC.hh>
11 12  
12 13 #include <iostream>
13 14 #include <stdlib.h>
... ... @@ -24,7 +25,8 @@ static void process(char const* whoami,
24 25 inpdf.processFile(infile);
25 26 std::vector<QPDFPageObjectHelper> pages =
26 27 QPDFPageDocumentHelper(inpdf).getAllPages();
27   - int pageno_len = QUtil::int_to_string(pages.size()).length();
  28 + int pageno_len =
  29 + QIntC::to_int(QUtil::uint_to_string(pages.size()).length());
28 30 int pageno = 0;
29 31 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
30 32 iter != pages.end(); ++iter)
... ...
fuzz/standalone_fuzz_target_runner.cc
1   -// Copyright 2017 Google Inc. All Rights Reserved.
2   -// Licensed under the Apache License, Version 2.0 (the "License");
3   -
4   -// Except for formatting, comments, and portability, this was copied
5   -// from projects/example/my-api-repo/standalone_fuzz_target_runner.cpp
6   -// in https://github.com/oss-fuzz
7   -
8   -#include <cassert>
  1 +#include <qpdf/QUtil.hh>
  2 +#include <qpdf/PointerHolder.hh>
  3 +#include <qpdf/QIntC.hh>
9 4 #include <iostream>
10   -#include <fstream>
11   -#include <vector>
  5 +#include <string>
12 6  
13 7 extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size);
14 8  
  9 +static void read_file_into_memory(
  10 + char const* filename,
  11 + PointerHolder<unsigned char>& file_buf, size_t& size)
  12 +{
  13 + FILE* f = QUtil::safe_fopen(filename, "rb");
  14 + fseek(f, 0, SEEK_END);
  15 + size = QIntC::to_size(QUtil::tell(f));
  16 + fseek(f, 0, SEEK_SET);
  17 + file_buf = PointerHolder<unsigned char>(true, new unsigned char[size]);
  18 + unsigned char* buf_p = file_buf.getPointer();
  19 + size_t bytes_read = 0;
  20 + size_t len = 0;
  21 + while ((len = fread(buf_p + bytes_read, 1, size - bytes_read, f)) > 0)
  22 + {
  23 + bytes_read += len;
  24 + }
  25 + if (bytes_read != size)
  26 + {
  27 + throw std::runtime_error(
  28 + std::string("failure reading file ") + filename +
  29 + " into memory: read " +
  30 + QUtil::uint_to_string(bytes_read) + "; wanted " +
  31 + QUtil::uint_to_string(size));
  32 + }
  33 + fclose(f);
  34 +}
  35 +
15 36 int main(int argc, char **argv)
16 37 {
17 38 for (int i = 1; i < argc; i++)
18 39 {
19   - std::ifstream in(argv[i]);
20   - in.seekg(0, in.end);
21   - size_t length = in.tellg();
22   - in.seekg (0, in.beg);
23   - std::cout << "checking " << argv[i] << std::endl;
24   - // Allocate exactly length bytes so that we reliably catch
25   - // buffer overflows.
26   - std::vector<char> bytes(length);
27   - in.read(bytes.data(), bytes.size());
28   - assert(in);
29   - LLVMFuzzerTestOneInput(
30   - reinterpret_cast<unsigned char const*>(bytes.data()),
31   - bytes.size());
  40 + PointerHolder<unsigned char> file_buf;
  41 + size_t size = 0;
  42 + read_file_into_memory(argv[i], file_buf, size);
  43 + LLVMFuzzerTestOneInput(file_buf.getPointer(), size);
32 44 std::cout << argv[i] << " successful" << std::endl;
33 45 }
34 46 return 0;
... ...
include/qpdf/BufferInputSource.hh
... ... @@ -54,6 +54,8 @@ class BufferInputSource: public InputSource
54 54 virtual void unreadCh(char ch);
55 55  
56 56 private:
  57 + qpdf_offset_t const bufSizeAsOffset() const;
  58 +
57 59 bool own_memory;
58 60 std::string description;
59 61 Buffer* buf;
... ...
include/qpdf/Pl_Count.hh
... ... @@ -48,6 +48,8 @@ class Pl_Count: public Pipeline
48 48 unsigned char getLastChar() const;
49 49  
50 50 private:
  51 + // Must be qpdf_offset_t, not size_t, to handle writing more than
  52 + // size_t can handle.
51 53 qpdf_offset_t count;
52 54 unsigned char last_char;
53 55 };
... ...
include/qpdf/QPDF.hh
... ... @@ -32,6 +32,7 @@
32 32 #include <iostream>
33 33 #include <vector>
34 34  
  35 +#include <qpdf/QIntC.hh>
35 36 #include <qpdf/QPDFExc.hh>
36 37 #include <qpdf/QPDFObjectHandle.hh>
37 38 #include <qpdf/QPDFObjGen.hh>
... ... @@ -859,7 +860,7 @@ class QPDF
859 860 bool pipeForeignStreamData(
860 861 PointerHolder<ForeignStreamData>,
861 862 Pipeline*,
862   - unsigned long encode_flags,
  863 + int encode_flags,
863 864 qpdf_stream_decode_level_e decode_level);
864 865 static bool pipeStreamData(PointerHolder<QPDF::EncryptionParameters> encp,
865 866 PointerHolder<InputSource> file,
... ... @@ -1253,7 +1254,7 @@ class QPDF
1253 1254 void dumpHPageOffset();
1254 1255 void dumpHSharedObject();
1255 1256 void dumpHGeneric(HGeneric&);
1256   - int adjusted_offset(int offset);
  1257 + qpdf_offset_t adjusted_offset(qpdf_offset_t offset);
1257 1258 QPDFObjectHandle objGenToIndirect(QPDFObjGen const&);
1258 1259 void calculateLinearizationData(
1259 1260 std::map<int, int> const& object_stream_data);
... ... @@ -1297,6 +1298,20 @@ class QPDF
1297 1298 std::set<QPDFObjGen>& visited, bool top);
1298 1299 void filterCompressedObjects(std::map<int, int> const& object_stream_data);
1299 1300  
  1301 + // Type conversion helper methods
  1302 + template<typename T> static qpdf_offset_t toO(T const& i)
  1303 + {
  1304 + return QIntC::to_offset(i);
  1305 + }
  1306 + template<typename T> static size_t toS(T const& i)
  1307 + {
  1308 + return QIntC::to_size(i);
  1309 + }
  1310 + template<typename T> static int toI(T const& i)
  1311 + {
  1312 + return QIntC::to_int(i);
  1313 + }
  1314 +
1300 1315 class Members
1301 1316 {
1302 1317 friend class QPDF;
... ...
include/qpdf/QPDFWriter.hh
... ... @@ -467,7 +467,7 @@ class QPDFWriter
467 467  
468 468 enum trailer_e { t_normal, t_lin_first, t_lin_second };
469 469  
470   - int bytesNeeded(unsigned long long n);
  470 + unsigned int bytesNeeded(long long n);
471 471 void writeBinary(unsigned long long val, unsigned int bytes);
472 472 void writeString(std::string const& str);
473 473 void writeBuffer(PointerHolder<Buffer>&);
... ... @@ -483,10 +483,8 @@ class QPDFWriter
483 483 void writeTrailer(trailer_e which, int size,
484 484 bool xref_stream, qpdf_offset_t prev,
485 485 int linearization_pass);
486   - void unparseObject(QPDFObjectHandle object, int level,
487   - unsigned int flags);
488   - void unparseObject(QPDFObjectHandle object, int level,
489   - unsigned int flags,
  486 + void unparseObject(QPDFObjectHandle object, int level, int flags);
  487 + void unparseObject(QPDFObjectHandle object, int level, int flags,
490 488 // for stream dictionaries
491 489 size_t stream_length, bool compress);
492 490 void unparseChild(QPDFObjectHandle child, int level, int flags);
... ... @@ -510,7 +508,7 @@ class QPDFWriter
510 508 char const* user_password, char const* owner_password,
511 509 int V, int R, int key_len, std::set<int>& bits_to_clear);
512 510 void setEncryptionParametersInternal(
513   - int V, int R, int key_len, long P,
  511 + int V, int R, int key_len, int P,
514 512 std::string const& O, std::string const& U,
515 513 std::string const& OE, std::string const& UE, std::string const& Perms,
516 514 std::string const& id1, std::string const& user_password,
... ... @@ -554,7 +552,7 @@ class QPDFWriter
554 552 qpdf_offset_t hint_length,
555 553 bool skip_compression,
556 554 int linearization_pass);
557   - int calculateXrefStreamPadding(int xref_bytes);
  555 + int calculateXrefStreamPadding(qpdf_offset_t xref_bytes);
558 556  
559 557 // When filtering subsections, push additional pipelines to the
560 558 // stack. When ready to switch, activate the pipeline stack.
... ...
libqpdf/BitStream.cc
1 1 #include <qpdf/BitStream.hh>
  2 +#include <qpdf/QIntC.hh>
2 3  
3 4 // See comments in bits.cc
4 5 #define BITS_READ 1
5 6 #include "bits.icc"
6 7  
7   -BitStream::BitStream(unsigned char const* p, int nbytes) :
  8 +BitStream::BitStream(unsigned char const* p, size_t nbytes) :
8 9 start(p),
9 10 nbytes(nbytes)
10 11 {
... ... @@ -16,7 +17,7 @@ BitStream::reset()
16 17 {
17 18 p = start;
18 19 bit_offset = 7;
19   - if (static_cast<unsigned int>(nbytes) > static_cast<unsigned int>(-1) / 8)
  20 + if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8)
20 21 {
21 22 throw std::runtime_error("array too large for bitstream");
22 23 }
... ... @@ -24,21 +25,21 @@ BitStream::reset()
24 25 }
25 26  
26 27 unsigned long long
27   -BitStream::getBits(int nbits)
  28 +BitStream::getBits(size_t nbits)
28 29 {
29 30 return read_bits(this->p, this->bit_offset,
30 31 this->bits_available, nbits);
31 32 }
32 33  
33 34 long long
34   -BitStream::getBitsSigned(int nbits)
  35 +BitStream::getBitsSigned(size_t nbits)
35 36 {
36 37 unsigned long long bits = read_bits(this->p, this->bit_offset,
37 38 this->bits_available, nbits);
38 39 long long result = 0;
39   - if (static_cast<long long>(bits) > 1 << (nbits - 1))
  40 + if (static_cast<long long>(bits) > 1LL << (nbits - 1))
40 41 {
41   - result = static_cast<long long>(bits - (1 << nbits));
  42 + result = static_cast<long long>(bits -(1ULL << nbits));
42 43 }
43 44 else
44 45 {
... ... @@ -47,12 +48,21 @@ BitStream::getBitsSigned(int nbits)
47 48 return result;
48 49 }
49 50  
  51 +int
  52 +BitStream::getBitsInt(size_t nbits)
  53 +{
  54 + return static_cast<int>(
  55 + QIntC::to_uint(
  56 + read_bits(this->p, this->bit_offset,
  57 + this->bits_available, nbits)));
  58 +}
  59 +
50 60 void
51 61 BitStream::skipToNextByte()
52 62 {
53 63 if (bit_offset != 7)
54 64 {
55   - unsigned int bits_to_skip = bit_offset + 1;
  65 + size_t bits_to_skip = bit_offset + 1;
56 66 if (bits_available < bits_to_skip)
57 67 {
58 68 throw std::logic_error(
... ...
libqpdf/BitWriter.cc
... ... @@ -12,18 +12,18 @@ BitWriter::BitWriter(Pipeline* pl) :
12 12 }
13 13  
14 14 void
15   -BitWriter::writeBits(unsigned long long val, unsigned int bits)
  15 +BitWriter::writeBits(unsigned long long val, size_t bits)
16 16 {
17 17 write_bits(this->ch, this->bit_offset, val, bits, this->pl);
18 18 }
19 19  
20 20 void
21   -BitWriter::writeBitsSigned(long long val, unsigned int bits)
  21 +BitWriter::writeBitsSigned(long long val, size_t bits)
22 22 {
23 23 unsigned long long uval = 0;
24 24 if (val < 0)
25 25 {
26   - uval = static_cast<unsigned long long>((1 << bits) + val);
  26 + uval = (1ULL << bits) + static_cast<unsigned long long>(val);
27 27 }
28 28 else
29 29 {
... ... @@ -33,11 +33,17 @@ BitWriter::writeBitsSigned(long long val, unsigned int bits)
33 33 }
34 34  
35 35 void
  36 +BitWriter::writeBitsInt(int val, size_t bits)
  37 +{
  38 + writeBits(static_cast<unsigned long long>(val), bits);
  39 +}
  40 +
  41 +void
36 42 BitWriter::flush()
37 43 {
38 44 if (bit_offset < 7)
39 45 {
40   - int bits_to_write = bit_offset + 1;
  46 + size_t bits_to_write = bit_offset + 1;
41 47 write_bits(this->ch, this->bit_offset, 0, bits_to_write, this->pl);
42 48 }
43 49 }
... ...
libqpdf/BufferInputSource.cc
1 1 #include <qpdf/BufferInputSource.hh>
  2 +#include <qpdf/QIntC.hh>
2 3 #include <string.h>
3 4 #include <stdexcept>
4 5 #include <algorithm>
... ... @@ -32,6 +33,12 @@ BufferInputSource::~BufferInputSource()
32 33 }
33 34 }
34 35  
  36 +qpdf_offset_t const
  37 +BufferInputSource::bufSizeAsOffset() const
  38 +{
  39 + return QIntC::to_offset(this->buf->getSize());
  40 +}
  41 +
35 42 qpdf_offset_t
36 43 BufferInputSource::findAndSkipNextEOL()
37 44 {
... ... @@ -39,7 +46,7 @@ BufferInputSource::findAndSkipNextEOL()
39 46 {
40 47 throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
41 48 }
42   - qpdf_offset_t end_pos = this->buf->getSize();
  49 + qpdf_offset_t end_pos = bufSizeAsOffset();
43 50 if (this->cur_offset >= end_pos)
44 51 {
45 52 this->last_offset = end_pos;
... ... @@ -48,7 +55,7 @@ BufferInputSource::findAndSkipNextEOL()
48 55 }
49 56  
50 57 qpdf_offset_t result = 0;
51   - size_t len = end_pos - this->cur_offset;
  58 + size_t len = QIntC::to_size(end_pos - this->cur_offset);
52 59 unsigned char const* buffer = this->buf->getBuffer();
53 60  
54 61 void* start = const_cast<unsigned char*>(buffer) + this->cur_offset;
... ... @@ -97,7 +104,7 @@ BufferInputSource::seek(qpdf_offset_t offset, int whence)
97 104 break;
98 105  
99 106 case SEEK_END:
100   - this->cur_offset = this->buf->getSize() + offset;
  107 + this->cur_offset = bufSizeAsOffset() + offset;
101 108 break;
102 109  
103 110 case SEEK_CUR:
... ... @@ -130,7 +137,7 @@ BufferInputSource::read(char* buffer, size_t length)
130 137 {
131 138 throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
132 139 }
133   - qpdf_offset_t end_pos = this->buf->getSize();
  140 + qpdf_offset_t end_pos = bufSizeAsOffset();
134 141 if (this->cur_offset >= end_pos)
135 142 {
136 143 this->last_offset = end_pos;
... ... @@ -139,9 +146,9 @@ BufferInputSource::read(char* buffer, size_t length)
139 146  
140 147 this->last_offset = this->cur_offset;
141 148 size_t len = std::min(
142   - static_cast<size_t>(end_pos - this->cur_offset), length);
  149 + QIntC::to_size(end_pos - this->cur_offset), length);
143 150 memcpy(buffer, buf->getBuffer() + this->cur_offset, len);
144   - this->cur_offset += len;
  151 + this->cur_offset += QIntC::to_offset(len);
145 152 return len;
146 153 }
147 154  
... ...
libqpdf/FileInputSource.cc
... ... @@ -130,7 +130,7 @@ FileInputSource::read(char* buffer, size_t length)
130 130 this->filename, "",
131 131 this->last_offset,
132 132 std::string("read ") +
133   - QUtil::int_to_string(length) + " bytes");
  133 + QUtil::uint_to_string(length) + " bytes");
134 134 }
135 135 else if (length > 0)
136 136 {
... ...
libqpdf/InputSource.cc
... ... @@ -3,6 +3,7 @@
3 3 #include <stdexcept>
4 4 #include <qpdf/QTC.hh>
5 5 #include <qpdf/PointerHolder.hh>
  6 +#include <qpdf/QIntC.hh>
6 7  
7 8  
8 9 void
... ... @@ -35,7 +36,7 @@ InputSource::readLine(size_t max_line_length)
35 36 this->seek(offset, SEEK_SET);
36 37 qpdf_offset_t eol = this->findAndSkipNextEOL();
37 38 this->last_offset = offset;
38   - size_t line_length = eol - offset;
  39 + size_t line_length = QIntC::to_size(eol - offset);
39 40 if (line_length < max_line_length)
40 41 {
41 42 buf[line_length] = '\0';
... ... @@ -116,7 +117,8 @@ InputSource::findFirst(char const* start_chars,
116 117  
117 118 // Search for the first character.
118 119 if ((p = static_cast<char*>(
119   - memchr(p, start_chars[0], bytes_read - (p - buf)))) != 0)
  120 + memchr(p, start_chars[0],
  121 + bytes_read - QIntC::to_size(p - buf)))) != 0)
120 122 {
121 123 if (p == buf)
122 124 {
... ... @@ -126,7 +128,8 @@ InputSource::findFirst(char const* start_chars,
126 128 if (len != 0)
127 129 {
128 130 // Make sure it's in range.
129   - size_t p_relative_offset = (p - buf) + (buf_offset - offset);
  131 + size_t p_relative_offset =
  132 + QIntC::to_size((p - buf) + (buf_offset - offset));
130 133 if (p_relative_offset >= len)
131 134 {
132 135 // out of range
... ... @@ -198,7 +201,7 @@ InputSource::findLast(char const* start_chars,
198 201 }
199 202 after_found_offset = this->tell();
200 203 cur_offset = after_found_offset;
201   - cur_len = len - (cur_offset - offset);
  204 + cur_len = len - QIntC::to_size((cur_offset - offset));
202 205 }
203 206 if (found)
204 207 {
... ...
libqpdf/InsecureRandomDataProvider.cc
... ... @@ -30,7 +30,8 @@ InsecureRandomDataProvider::random()
30 30 // Seed the random number generator with something simple, but
31 31 // just to be interesting, don't use the unmodified current
32 32 // time. It would be better if this were a more secure seed.
33   - QUtil::srandom(QUtil::get_current_time() ^ 0xcccc);
  33 + QUtil::srandom(static_cast<unsigned int>(
  34 + QUtil::get_current_time() ^ 0xcccc));
34 35 this->seeded_random = true;
35 36 }
36 37  
... ...
libqpdf/JSON.cc
... ... @@ -197,7 +197,7 @@ JSON::encode_string(std::string const&amp; str)
197 197 }
198 198 else
199 199 {
200   - result.append(1, ch);
  200 + result.append(1, static_cast<char>(ch));
201 201 }
202 202 }
203 203 }
... ...
libqpdf/MD5.cc
... ... @@ -29,6 +29,7 @@
29 29  
30 30 #include <qpdf/MD5.hh>
31 31 #include <qpdf/QUtil.hh>
  32 +#include <qpdf/QIntC.hh>
32 33  
33 34 #include <stdio.h>
34 35 #include <memory.h>
... ... @@ -110,7 +111,7 @@ void MD5::init()
110 111 // context.
111 112  
112 113 void MD5::update(unsigned char *input,
113   - unsigned int inputLen)
  114 + size_t inputLen)
114 115 {
115 116 unsigned int i, index, partLen;
116 117  
... ... @@ -268,7 +269,7 @@ void MD5::transform(UINT4 state[4], unsigned char block[64])
268 269  
269 270 // Encodes input (UINT4) into output (unsigned char). Assumes len is a
270 271 // multiple of 4.
271   -void MD5::encode(unsigned char *output, UINT4 *input, unsigned int len)
  272 +void MD5::encode(unsigned char *output, UINT4 *input, size_t len)
272 273 {
273 274 unsigned int i, j;
274 275  
... ... @@ -282,7 +283,7 @@ void MD5::encode(unsigned char *output, UINT4 *input, unsigned int len)
282 283  
283 284 // Decodes input (unsigned char) into output (UINT4). Assumes len is a
284 285 // multiple of 4.
285   -void MD5::decode(UINT4 *output, unsigned char *input, unsigned int len)
  286 +void MD5::decode(UINT4 *output, unsigned char *input, size_t len)
286 287 {
287 288 unsigned int i, j;
288 289  
... ... @@ -308,7 +309,7 @@ void MD5::reset()
308 309  
309 310 void MD5::encodeString(char const* str)
310 311 {
311   - unsigned int len = strlen(str);
  312 + size_t len = strlen(str);
312 313  
313 314 update(QUtil::unsigned_char_pointer(str), len);
314 315 final();
... ... @@ -319,22 +320,27 @@ void MD5::appendString(char const* input_string)
319 320 update(QUtil::unsigned_char_pointer(input_string), strlen(input_string));
320 321 }
321 322  
322   -void MD5::encodeDataIncrementally(char const* data, int len)
  323 +void MD5::encodeDataIncrementally(char const* data, size_t len)
323 324 {
324 325 update(QUtil::unsigned_char_pointer(data), len);
325 326 }
326 327  
327   -void MD5::encodeFile(char const *filename, int up_to_size)
  328 +void MD5::encodeFile(char const *filename, qpdf_offset_t up_to_offset)
328 329 {
329 330 unsigned char buffer[1024];
330 331  
331 332 FILE *file = QUtil::safe_fopen(filename, "rb");
332 333 size_t len;
333   - int so_far = 0;
334   - int to_try = 1024;
  334 + size_t so_far = 0;
  335 + size_t to_try = 1024;
  336 + size_t up_to_size = 0;
  337 + if (up_to_offset >= 0)
  338 + {
  339 + up_to_size = QIntC::to_size(up_to_offset);
  340 + }
335 341 do
336 342 {
337   - if ((up_to_size >= 0) && ((so_far + to_try) > up_to_size))
  343 + if ((up_to_offset >= 0) && ((so_far + to_try) > up_to_size))
338 344 {
339 345 to_try = up_to_size - so_far;
340 346 }
... ... @@ -343,7 +349,7 @@ void MD5::encodeFile(char const *filename, int up_to_size)
343 349 {
344 350 update(buffer, len);
345 351 so_far += len;
346   - if ((up_to_size >= 0) && (so_far >= up_to_size))
  352 + if ((up_to_offset >= 0) && (so_far >= up_to_size))
347 353 {
348 354 break;
349 355 }
... ... @@ -388,7 +394,7 @@ std::string MD5::unparse()
388 394 }
389 395  
390 396 std::string
391   -MD5::getDataChecksum(char const* buf, int len)
  397 +MD5::getDataChecksum(char const* buf, size_t len)
392 398 {
393 399 MD5 m;
394 400 m.encodeDataIncrementally(buf, len);
... ... @@ -396,16 +402,16 @@ MD5::getDataChecksum(char const* buf, int len)
396 402 }
397 403  
398 404 std::string
399   -MD5::getFileChecksum(char const* filename, int up_to_size)
  405 +MD5::getFileChecksum(char const* filename, qpdf_offset_t up_to_offset)
400 406 {
401 407 MD5 m;
402   - m.encodeFile(filename, up_to_size);
  408 + m.encodeFile(filename, up_to_offset);
403 409 return m.unparse();
404 410 }
405 411  
406 412 bool
407 413 MD5::checkDataChecksum(char const* const checksum,
408   - char const* buf, int len)
  414 + char const* buf, size_t len)
409 415 {
410 416 std::string actual_checksum = getDataChecksum(buf, len);
411 417 return (checksum == actual_checksum);
... ... @@ -413,12 +419,12 @@ MD5::checkDataChecksum(char const* const checksum,
413 419  
414 420 bool
415 421 MD5::checkFileChecksum(char const* const checksum,
416   - char const* filename, int up_to_size)
  422 + char const* filename, qpdf_offset_t up_to_offset)
417 423 {
418 424 bool result = false;
419 425 try
420 426 {
421   - std::string actual_checksum = getFileChecksum(filename, up_to_size);
  427 + std::string actual_checksum = getFileChecksum(filename, up_to_offset);
422 428 result = (checksum == actual_checksum);
423 429 }
424 430 catch (std::runtime_error const&)
... ...
libqpdf/Pl_AES_PDF.cc
... ... @@ -4,6 +4,7 @@
4 4 #include <assert.h>
5 5 #include <stdexcept>
6 6 #include <qpdf/rijndael.h>
  7 +#include <qpdf/QIntC.hh>
7 8 #include <string>
8 9 #include <stdlib.h>
9 10  
... ... @@ -11,7 +12,7 @@ bool Pl_AES_PDF::use_static_iv = false;
11 12  
12 13 Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
13 14 bool encrypt, unsigned char const* key,
14   - unsigned int key_bytes) :
  15 + size_t key_bytes) :
15 16 Pipeline(identifier, next),
16 17 encrypt(encrypt),
17 18 cbc_mode(true),
... ... @@ -22,11 +23,11 @@ Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
22 23 use_specified_iv(false),
23 24 disable_padding(false)
24 25 {
25   - unsigned int keybits = 8 * key_bytes;
  26 + size_t keybits = 8 * key_bytes;
26 27 assert(key_bytes == KEYLENGTH(keybits));
27 28 this->key = new unsigned char[key_bytes];
28 29 this->rk = new uint32_t[RKLENGTH(keybits)];
29   - unsigned int rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t);
  30 + size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t);
30 31 std::memcpy(this->key, key, key_bytes);
31 32 std::memset(this->rk, 0, rk_bytes);
32 33 std::memset(this->inbuf, 0, this->buf_size);
... ... @@ -68,7 +69,7 @@ Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
68 69 {
69 70 throw std::logic_error(
70 71 "Pl_AES_PDF: specified initialization vector"
71   - " size in bytes must be " + QUtil::int_to_string(bytes));
  72 + " size in bytes must be " + QUtil::uint_to_string(bytes));
72 73 }
73 74 this->use_specified_iv = true;
74 75 memcpy(this->specified_iv, iv, bytes);
... ... @@ -123,7 +124,7 @@ Pl_AES_PDF::finish()
123 124 // specification, including providing an entire block of padding
124 125 // if the input was a multiple of 16 bytes.
125 126 unsigned char pad =
126   - static_cast<unsigned char>(this->buf_size - this->offset);
  127 + QIntC::to_uchar(this->buf_size - this->offset);
127 128 memset(this->inbuf + this->offset, pad, pad);
128 129 this->offset = this->buf_size;
129 130 flush(false);
... ... @@ -166,7 +167,7 @@ Pl_AES_PDF::initializeVector()
166 167 {
167 168 for (unsigned int i = 0; i < this->buf_size; ++i)
168 169 {
169   - this->cbc_block[i] = 14 * (1 + i);
  170 + this->cbc_block[i] = static_cast<unsigned char>(14U * (1U + i));
170 171 }
171 172 }
172 173 else
... ...
libqpdf/Pl_ASCII85Decoder.cc
... ... @@ -106,7 +106,7 @@ Pl_ASCII85Decoder::flush()
106 106 for (int i = 0; i < 5; ++i)
107 107 {
108 108 lval *= 85;
109   - lval += (this->inbuf[i] - 33);
  109 + lval += (this->inbuf[i] - 33U);
110 110 }
111 111  
112 112 unsigned char outbuf[4];
... ...
libqpdf/Pl_ASCIIHexDecoder.cc
... ... @@ -27,7 +27,7 @@ Pl_ASCIIHexDecoder::write(unsigned char* buf, size_t len)
27 27 }
28 28 for (size_t i = 0; i < len; ++i)
29 29 {
30   - char ch = toupper(buf[i]);
  30 + char ch = static_cast<char>(toupper(buf[i]));
31 31 switch (ch)
32 32 {
33 33 case ' ':
... ...
libqpdf/Pl_Count.cc
1 1 #include <qpdf/Pl_Count.hh>
  2 +#include <qpdf/QIntC.hh>
2 3  
3 4 Pl_Count::Pl_Count(char const* identifier, Pipeline* next) :
4 5 Pipeline(identifier, next),
... ... @@ -16,7 +17,7 @@ Pl_Count::write(unsigned char* buf, size_t len)
16 17 {
17 18 if (len)
18 19 {
19   - this->count += len;
  20 + this->count += QIntC::to_offset(len);
20 21 getNext()->write(buf, len);
21 22 this->last_char = buf[len - 1];
22 23 }
... ...
libqpdf/Pl_DCT.cc
... ... @@ -2,6 +2,7 @@
2 2  
3 3 #include <qpdf/QUtil.hh>
4 4 #include <qpdf/QTC.hh>
  5 +#include <qpdf/QIntC.hh>
5 6  
6 7 #include <setjmp.h>
7 8 #include <stdexcept>
... ... @@ -213,7 +214,7 @@ skip_buffer_input_data(j_decompress_ptr cinfo, long num_bytes)
213 214 "reading jpeg: jpeg library requested"
214 215 " skipping a negative number of bytes");
215 216 }
216   - size_t to_skip = static_cast<size_t>(num_bytes);
  217 + size_t to_skip = QIntC::to_size(num_bytes);
217 218 if ((to_skip > 0) && (to_skip <= cinfo->src->bytes_in_buffer))
218 219 {
219 220 cinfo->src->next_input_byte += to_skip;
... ... @@ -283,15 +284,17 @@ Pl_DCT::compress(void* cinfo_p, Buffer* b)
283 284  
284 285 jpeg_start_compress(cinfo, TRUE);
285 286  
286   - int width = cinfo->image_width * cinfo->input_components;
  287 + unsigned int width = cinfo->image_width *
  288 + QIntC::to_uint(cinfo->input_components);
287 289 size_t expected_size =
288   - cinfo->image_height * cinfo->image_width * cinfo->input_components;
  290 + cinfo->image_height * cinfo->image_width *
  291 + QIntC::to_uint(cinfo->input_components);
289 292 if (b->getSize() != expected_size)
290 293 {
291 294 throw std::runtime_error(
292 295 "Pl_DCT: image buffer size = " +
293   - QUtil::int_to_string(b->getSize()) + "; expected size = " +
294   - QUtil::int_to_string(expected_size));
  296 + QUtil::uint_to_string(b->getSize()) + "; expected size = " +
  297 + QUtil::uint_to_string(expected_size));
295 298 }
296 299 JSAMPROW row_pointer[1];
297 300 unsigned char* buffer = b->getBuffer();
... ... @@ -326,7 +329,8 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b)
326 329 (void) jpeg_read_header(cinfo, TRUE);
327 330 (void) jpeg_calc_output_dimensions(cinfo);
328 331  
329   - int width = cinfo->output_width * cinfo->output_components;
  332 + unsigned int width = cinfo->output_width *
  333 + QIntC::to_uint(cinfo->output_components);
330 334 JSAMPARRAY buffer = (*cinfo->mem->alloc_sarray)
331 335 (reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE, width, 1);
332 336  
... ...
libqpdf/Pl_LZWDecoder.cc
... ... @@ -2,6 +2,7 @@
2 2  
3 3 #include <qpdf/QTC.hh>
4 4 #include <qpdf/QUtil.hh>
  5 +#include <qpdf/QIntC.hh>
5 6 #include <stdexcept>
6 7 #include <string.h>
7 8 #include <assert.h>
... ... @@ -52,22 +53,22 @@ Pl_LZWDecoder::finish()
52 53 void
53 54 Pl_LZWDecoder::sendNextCode()
54 55 {
55   - int high = this->byte_pos;
56   - int med = (this->byte_pos + 1) % 3;
57   - int low = (this->byte_pos + 2) % 3;
  56 + unsigned int high = this->byte_pos;
  57 + unsigned int med = (this->byte_pos + 1) % 3;
  58 + unsigned int low = (this->byte_pos + 2) % 3;
58 59  
59   - int bits_from_high = 8 - this->bit_pos;
60   - int bits_from_med = this->code_size - bits_from_high;
61   - int bits_from_low = 0;
  60 + unsigned int bits_from_high = 8 - this->bit_pos;
  61 + unsigned int bits_from_med = this->code_size - bits_from_high;
  62 + unsigned int bits_from_low = 0;
62 63 if (bits_from_med > 8)
63 64 {
64 65 bits_from_low = bits_from_med - 8;
65 66 bits_from_med = 8;
66 67 }
67   - int high_mask = (1 << bits_from_high) - 1;
68   - int med_mask = 0xff - ((1 << (8 - bits_from_med)) - 1);
69   - int low_mask = 0xff - ((1 << (8 - bits_from_low)) - 1);
70   - int code = 0;
  68 + unsigned int high_mask = (1U << bits_from_high) - 1U;
  69 + unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U);
  70 + unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U);
  71 + unsigned int code = 0;
71 72 code += (this->buf[high] & high_mask) << bits_from_med;
72 73 code += ((this->buf[med] & med_mask) >> (8 - bits_from_med));
73 74 if (bits_from_low)
... ... @@ -94,7 +95,7 @@ Pl_LZWDecoder::sendNextCode()
94 95 }
95 96  
96 97 unsigned char
97   -Pl_LZWDecoder::getFirstChar(int code)
  98 +Pl_LZWDecoder::getFirstChar(unsigned int code)
98 99 {
99 100 unsigned char result = '\0';
100 101 if (code < 256)
... ... @@ -130,7 +131,7 @@ Pl_LZWDecoder::addToTable(unsigned char next)
130 131  
131 132 if (this->last_code < 256)
132 133 {
133   - tmp[0] = this->last_code;
  134 + tmp[0] = static_cast<unsigned char>(this->last_code);
134 135 last_data = tmp;
135 136 last_size = 1;
136 137 }
... ... @@ -144,7 +145,7 @@ Pl_LZWDecoder::addToTable(unsigned char next)
144 145 }
145 146 Buffer& b = table.at(idx);
146 147 last_data = b.getBuffer();
147   - last_size = b.getSize();
  148 + last_size = QIntC::to_uint(b.getSize());
148 149 }
149 150 else
150 151 {
... ... @@ -161,7 +162,7 @@ Pl_LZWDecoder::addToTable(unsigned char next)
161 162 }
162 163  
163 164 void
164   -Pl_LZWDecoder::handleCode(int code)
  165 +Pl_LZWDecoder::handleCode(unsigned int code)
165 166 {
166 167 if (this->eod)
167 168 {
... ... @@ -189,11 +190,11 @@ Pl_LZWDecoder::handleCode(int code)
189 190 // be what we read last plus the first character of what
190 191 // we're reading now.
191 192 unsigned char next = '\0';
192   - unsigned int table_size = table.size();
  193 + unsigned int table_size = QIntC::to_uint(table.size());
193 194 if (code < 256)
194 195 {
195 196 // just read < 256; last time's next was code
196   - next = code;
  197 + next = static_cast<unsigned char>(code);
197 198 }
198 199 else if (code > 257)
199 200 {
... ...
libqpdf/Pl_PNGFilter.cc
... ... @@ -153,7 +153,7 @@ Pl_PNGFilter::decodeSub()
153 153 left = buffer[i - bpp];
154 154 }
155 155  
156   - buffer[i] += left;
  156 + buffer[i] = static_cast<unsigned char>(buffer[i] + left);
157 157 }
158 158 }
159 159  
... ... @@ -167,7 +167,7 @@ Pl_PNGFilter::decodeUp()
167 167 for (unsigned int i = 0; i < this->bytes_per_row; ++i)
168 168 {
169 169 unsigned char up = above_buffer[i];
170   - buffer[i] += up;
  170 + buffer[i] = static_cast<unsigned char>(buffer[i] + up);
171 171 }
172 172 }
173 173  
... ... @@ -190,7 +190,7 @@ Pl_PNGFilter::decodeAverage()
190 190 }
191 191  
192 192 up = above_buffer[i];
193   - buffer[i] += (left+up) / 2;
  193 + buffer[i] = static_cast<unsigned char>(buffer[i] + (left+up) / 2);
194 194 }
195 195 }
196 196  
... ... @@ -214,7 +214,9 @@ Pl_PNGFilter::decodePaeth()
214 214 upper_left = above_buffer[i - bpp];
215 215 }
216 216  
217   - buffer[i] += this->PaethPredictor(left, up, upper_left);
  217 + buffer[i] = static_cast<unsigned char>(
  218 + buffer[i] +
  219 + this->PaethPredictor(left, up, upper_left));
218 220 }
219 221 }
220 222  
... ... @@ -247,7 +249,8 @@ Pl_PNGFilter::encodeRow()
247 249 {
248 250 for (unsigned int i = 0; i < this->bytes_per_row; ++i)
249 251 {
250   - ch = this->cur_row[i] - this->prev_row[i];
  252 + ch = static_cast<unsigned char>(
  253 + this->cur_row[i] - this->prev_row[i]);
251 254 getNext()->write(&ch, 1);
252 255 }
253 256 }
... ...
libqpdf/Pl_RunLength.cc
... ... @@ -85,13 +85,13 @@ Pl_RunLength::decode(unsigned char* data, size_t len)
85 85 if (ch < 128)
86 86 {
87 87 // length represents remaining number of bytes to copy
88   - this->length = 1 + ch;
  88 + this->length = 1U + ch;
89 89 this->state = st_copying;
90 90 }
91 91 else if (ch > 128)
92 92 {
93 93 // length represents number of copies of next byte
94   - this->length = 257 - ch;
  94 + this->length = 257U - ch;
95 95 this->state = st_run;
96 96 }
97 97 else // ch == 128
... ...
libqpdf/QPDF.cc
... ... @@ -491,7 +491,7 @@ QPDF::reconstruct_xref(QPDFExc&amp; e)
491 491 this->m->file->seek(line_start, SEEK_SET);
492 492 QPDFTokenizer::Token t1 = readToken(this->m->file, MAX_LEN);
493 493 qpdf_offset_t token_start =
494   - this->m->file->tell() - t1.getValue().length();
  494 + this->m->file->tell() - toO(t1.getValue().length());
495 495 if (token_start >= next_line_start)
496 496 {
497 497 // don't process yet
... ... @@ -610,7 +610,7 @@ QPDF::read_xref(qpdf_offset_t xref_offset)
610 610 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(), "", 0,
611 611 "unable to find trailer while reading xref");
612 612 }
613   - int size = this->m->trailer.getKey("/Size").getIntValue();
  613 + int size = this->m->trailer.getKey("/Size").getIntValueAsInt();
614 614 int max_obj = 0;
615 615 if (! this->m->xref_table.empty())
616 616 {
... ... @@ -686,7 +686,7 @@ QPDF::parse_xrefFirst(std::string const&amp; line,
686 686 {
687 687 ++p;
688 688 }
689   - bytes = p - start;
  689 + bytes = toI(p - start);
690 690 obj = QUtil::string_to_int(obj_str.c_str());
691 691 num = QUtil::string_to_int(num_str.c_str());
692 692 return true;
... ... @@ -837,11 +837,11 @@ QPDF::read_xrefTable(qpdf_offset_t xref_offset)
837 837 {
838 838 // Save deleted items until after we've checked the
839 839 // XRefStm, if any.
840   - deleted_items.push_back(QPDFObjGen(i, f2));
  840 + deleted_items.push_back(QPDFObjGen(toI(i), f2));
841 841 }
842 842 else
843 843 {
844   - insertXrefEntry(i, 1, f1, f2);
  844 + insertXrefEntry(toI(i), 1, f1, f2);
845 845 }
846 846 }
847 847 qpdf_offset_t pos = this->m->file->tell();
... ... @@ -1006,7 +1006,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1006 1006 int max_bytes = sizeof(qpdf_offset_t);
1007 1007 for (int i = 0; i < 3; ++i)
1008 1008 {
1009   - W[i] = W_obj.getArrayItem(i).getIntValue();
  1009 + W[i] = W_obj.getArrayItem(i).getIntValueAsInt();
1010 1010 if (W[i] > max_bytes)
1011 1011 {
1012 1012 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
... ... @@ -1014,7 +1014,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1014 1014 "Cross-reference stream's /W contains"
1015 1015 " impossibly large values");
1016 1016 }
1017   - entry_size += W[i];
  1017 + entry_size += toS(W[i]);
1018 1018 }
1019 1019 if (entry_size == 0)
1020 1020 {
... ... @@ -1023,7 +1023,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1023 1023 "Cross-reference stream's /W indicates"
1024 1024 " entry size of 0");
1025 1025 }
1026   - long long max_num_entries =
  1026 + unsigned long long max_num_entries =
1027 1027 static_cast<unsigned long long>(-1) / entry_size;
1028 1028  
1029 1029 std::vector<long long> indx;
... ... @@ -1063,20 +1063,20 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1063 1063 indx.push_back(size);
1064 1064 }
1065 1065  
1066   - long long num_entries = 0;
1067   - for (unsigned int i = 1; i < indx.size(); i += 2)
  1066 + size_t num_entries = 0;
  1067 + for (size_t i = 1; i < indx.size(); i += 2)
1068 1068 {
1069   - if (indx.at(i) > max_num_entries - num_entries)
  1069 + if (indx.at(i) > QIntC::to_longlong(max_num_entries - num_entries))
1070 1070 {
1071 1071 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
1072 1072 "xref stream", xref_offset,
1073 1073 "Cross-reference stream claims to contain"
1074 1074 " too many entries: " +
1075 1075 QUtil::int_to_string(indx.at(i)) + " " +
1076   - QUtil::int_to_string(max_num_entries) + " " +
1077   - QUtil::int_to_string(num_entries));
  1076 + QUtil::uint_to_string(max_num_entries) + " " +
  1077 + QUtil::uint_to_string(num_entries));
1078 1078 }
1079   - num_entries += indx.at(i);
  1079 + num_entries += toS(indx.at(i));
1080 1080 }
1081 1081  
1082 1082 // entry_size and num_entries have both been validated to ensure
... ... @@ -1091,8 +1091,8 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1091 1091 QPDFExc x(qpdf_e_damaged_pdf, this->m->file->getName(),
1092 1092 "xref stream", xref_offset,
1093 1093 "Cross-reference stream data has the wrong size;"
1094   - " expected = " + QUtil::int_to_string(expected_size) +
1095   - "; actual = " + QUtil::int_to_string(actual_size));
  1094 + " expected = " + QUtil::uint_to_string(expected_size) +
  1095 + "; actual = " + QUtil::uint_to_string(actual_size));
1096 1096 if (expected_size > actual_size)
1097 1097 {
1098 1098 throw x;
... ... @@ -1103,7 +1103,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1103 1103 }
1104 1104 }
1105 1105  
1106   - int cur_chunk = 0;
  1106 + size_t cur_chunk = 0;
1107 1107 int chunk_count = 0;
1108 1108  
1109 1109 bool saw_first_compressed_object = false;
... ... @@ -1112,7 +1112,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1112 1112 // not overflow any buffers here. We know that entry_size *
1113 1113 // num_entries is equal to the size of the buffer.
1114 1114 unsigned char const* data = bp->getBuffer();
1115   - for (int i = 0; i < num_entries; ++i)
  1115 + for (size_t i = 0; i < num_entries; ++i)
1116 1116 {
1117 1117 // Read this entry
1118 1118 unsigned char const* entry = data + (entry_size * i);
... ... @@ -1129,7 +1129,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1129 1129 for (int k = 0; k < W[j]; ++k)
1130 1130 {
1131 1131 fields[j] <<= 8;
1132   - fields[j] += static_cast<int>(*p++);
  1132 + fields[j] += toI(*p++);
1133 1133 }
1134 1134 }
1135 1135  
... ... @@ -1137,7 +1137,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1137 1137 // based on /Index. The generation number is 0 unless this is
1138 1138 // an uncompressed object record, in which case the generation
1139 1139 // number appears as the third field.
1140   - int obj = indx.at(cur_chunk) + chunk_count;
  1140 + int obj = toI(indx.at(cur_chunk)) + chunk_count;
1141 1141 ++chunk_count;
1142 1142 if (chunk_count >= indx.at(cur_chunk + 1))
1143 1143 {
... ... @@ -1161,8 +1161,8 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle&amp; xref_obj)
1161 1161 // This is needed by checkLinearization()
1162 1162 this->m->first_xref_item_offset = xref_offset;
1163 1163 }
1164   - insertXrefEntry(obj, static_cast<int>(fields[0]),
1165   - fields[1], static_cast<int>(fields[2]));
  1164 + insertXrefEntry(obj, toI(fields[0]),
  1165 + fields[1], toI(fields[2]));
1166 1166 }
1167 1167  
1168 1168 if (! this->m->trailer.isInitialized())
... ... @@ -1395,7 +1395,7 @@ QPDF::getObjectCount()
1395 1395 {
1396 1396 og = (*(this->m->obj_cache.rbegin())).first;
1397 1397 }
1398   - return og.getObj();
  1398 + return toS(og.getObj());
1399 1399 }
1400 1400  
1401 1401 std::vector<QPDFObjectHandle>
... ... @@ -1570,8 +1570,8 @@ QPDF::readObject(PointerHolder&lt;InputSource&gt; input,
1570 1570 "an integer");
1571 1571 }
1572 1572  
1573   - length = length_obj.getIntValue();
1574   - input->seek(stream_offset + length, SEEK_SET);
  1573 + length = toS(length_obj.getUIntValue());
  1574 + input->seek(stream_offset + toO(length), SEEK_SET);
1575 1575 if (! (readToken(input) ==
1576 1576 QPDFTokenizer::Token(
1577 1577 QPDFTokenizer::tt_word, "endstream")))
... ... @@ -1641,7 +1641,7 @@ QPDF::recoverStreamLength(PointerHolder&lt;InputSource&gt; input,
1641 1641 size_t length = 0;
1642 1642 if (this->m->file->findFirst("end", stream_offset, 0, ef))
1643 1643 {
1644   - length = this->m->file->tell() - stream_offset;
  1644 + length = toS(this->m->file->tell() - stream_offset);
1645 1645 // Reread endstream but, if it was endobj, don't skip that.
1646 1646 QPDFTokenizer::Token t = readToken(this->m->file);
1647 1647 if (t.getValue() == "endobj")
... ... @@ -1652,7 +1652,7 @@ QPDF::recoverStreamLength(PointerHolder&lt;InputSource&gt; input,
1652 1652  
1653 1653 if (length)
1654 1654 {
1655   - int this_obj_offset = 0;
  1655 + qpdf_offset_t this_obj_offset = 0;
1656 1656 QPDFObjGen this_obj(0, 0);
1657 1657  
1658 1658 // Make sure this is inside this object
... ... @@ -1700,7 +1700,7 @@ QPDF::recoverStreamLength(PointerHolder&lt;InputSource&gt; input,
1700 1700 warn(QPDFExc(qpdf_e_damaged_pdf, input->getName(),
1701 1701 this->m->last_object_description, stream_offset,
1702 1702 "recovered stream length: " +
1703   - QUtil::int_to_string(length)));
  1703 + QUtil::uint_to_string(length)));
1704 1704 }
1705 1705  
1706 1706 QTC::TC("qpdf", "QPDF recovered stream length");
... ... @@ -2027,8 +2027,8 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
2027 2027 " has incorrect keys");
2028 2028 }
2029 2029  
2030   - int n = dict.getKey("/N").getIntValue();
2031   - int first = dict.getKey("/First").getIntValue();
  2030 + int n = dict.getKey("/N").getIntValueAsInt();
  2031 + int first = dict.getKey("/First").getIntValueAsInt();
2032 2032  
2033 2033 std::map<int, int> offsets;
2034 2034  
... ... @@ -2052,7 +2052,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
2052 2052 }
2053 2053  
2054 2054 int num = QUtil::string_to_int(tnum.getValue().c_str());
2055   - int offset = QUtil::string_to_ll(toffset.getValue().c_str());
  2055 + int offset = QUtil::string_to_int(toffset.getValue().c_str());
2056 2056 offsets[num] = offset + first;
2057 2057 }
2058 2058  
... ... @@ -2087,7 +2087,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
2087 2087 QPDFObjectHandle
2088 2088 QPDF::makeIndirectObject(QPDFObjectHandle oh)
2089 2089 {
2090   - int max_objid = getObjectCount();
  2090 + int max_objid = toI(getObjectCount());
2091 2091 QPDFObjGen next(max_objid + 1, 0);
2092 2092 this->m->obj_cache[next] =
2093 2093 ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1);
... ... @@ -2509,7 +2509,7 @@ QPDF::getExtensionLevel()
2509 2509 obj = obj.getKey("/ExtensionLevel");
2510 2510 if (obj.isInteger())
2511 2511 {
2512   - result = obj.getIntValue();
  2512 + result = obj.getIntValueAsInt();
2513 2513 }
2514 2514 }
2515 2515 }
... ... @@ -2733,8 +2733,9 @@ QPDF::pipeStreamData(int objid, int generation,
2733 2733 bool suppress_warnings,
2734 2734 bool will_retry)
2735 2735 {
2736   - bool is_attachment_stream = this->m->attachment_streams.count(
2737   - QPDFObjGen(objid, generation));
  2736 + bool is_attachment_stream = (
  2737 + this->m->attachment_streams.count(
  2738 + QPDFObjGen(objid, generation)) > 0);
2738 2739 return pipeStreamData(
2739 2740 this->m->encp, this->m->file, *this,
2740 2741 objid, generation, offset, length,
... ... @@ -2746,7 +2747,7 @@ bool
2746 2747 QPDF::pipeForeignStreamData(
2747 2748 PointerHolder<ForeignStreamData> foreign,
2748 2749 Pipeline* pipeline,
2749   - unsigned long encode_flags,
  2750 + int encode_flags,
2750 2751 qpdf_stream_decode_level_e decode_level)
2751 2752 {
2752 2753 if (foreign->encp->encrypted)
... ...
libqpdf/QPDFAcroFormDocumentHelper.cc
... ... @@ -114,9 +114,9 @@ QPDFAcroFormDocumentHelper::analyze()
114 114 // bidirectionally to fields.
115 115  
116 116 std::set<QPDFObjGen> visited;
117   - size_t nfields = fields.getArrayNItems();
  117 + int nfields = fields.getArrayNItems();
118 118 QPDFObjectHandle null(QPDFObjectHandle::newNull());
119   - for (size_t i = 0; i < nfields; ++i)
  119 + for (int i = 0; i < nfields; ++i)
120 120 {
121 121 traverseField(fields.getArrayItem(i), null, 0, visited);
122 122 }
... ... @@ -216,8 +216,8 @@ QPDFAcroFormDocumentHelper::traverseField(
216 216 if (kids.isArray())
217 217 {
218 218 is_field = true;
219   - size_t nkids = kids.getArrayNItems();
220   - for (size_t k = 0; k < nkids; ++k)
  219 + int nkids = kids.getArrayNItems();
  220 + for (int k = 0; k < nkids; ++k)
221 221 {
222 222 traverseField(kids.getArrayItem(k), field, 1 + depth, visited);
223 223 }
... ...
libqpdf/QPDFAnnotationObjectHelper.cc
... ... @@ -52,7 +52,7 @@ int
52 52 QPDFAnnotationObjectHelper::getFlags()
53 53 {
54 54 QPDFObjectHandle flags_obj = this->oh.getKey("/F");
55   - return flags_obj.isInteger() ? flags_obj.getIntValue() : 0;
  55 + return flags_obj.isInteger() ? flags_obj.getIntValueAsInt() : 0;
56 56 }
57 57  
58 58 QPDFObjectHandle
... ...
libqpdf/QPDFFormFieldObjectHelper.cc
... ... @@ -4,6 +4,7 @@
4 4 #include <qpdf/QPDFAnnotationObjectHelper.hh>
5 5 #include <qpdf/QUtil.hh>
6 6 #include <qpdf/Pl_QPDFTokenizer.hh>
  7 +#include <qpdf/QIntC.hh>
7 8 #include <stdlib.h>
8 9  
9 10 QPDFFormFieldObjectHelper::Members::~Members()
... ... @@ -189,7 +190,7 @@ QPDFFormFieldObjectHelper::getQuadding()
189 190 if (fv.isInteger())
190 191 {
191 192 QTC::TC("qpdf", "QPDFFormFieldObjectHelper Q present");
192   - result = static_cast<int>(fv.getIntValue());
  193 + result = QIntC::to_int(fv.getIntValue());
193 194 }
194 195 return result;
195 196 }
... ... @@ -198,7 +199,7 @@ int
198 199 QPDFFormFieldObjectHelper::getFlags()
199 200 {
200 201 QPDFObjectHandle f = getInheritableFieldValue("/Ff");
201   - return f.isInteger() ? f.getIntValue() : 0;
  202 + return f.isInteger() ? f.getIntValueAsInt() : 0;
202 203 }
203 204  
204 205 bool
... ... @@ -245,8 +246,8 @@ QPDFFormFieldObjectHelper::getChoices()
245 246 QPDFObjectHandle opt = getInheritableFieldValue("/Opt");
246 247 if (opt.isArray())
247 248 {
248   - size_t n = opt.getArrayNItems();
249   - for (size_t i = 0; i < n; ++i)
  249 + int n = opt.getArrayNItems();
  250 + for (int i = 0; i < n; ++i)
250 251 {
251 252 QPDFObjectHandle item = opt.getArrayItem(i);
252 253 if (item.isString())
... ... @@ -631,8 +632,8 @@ ValueSetter::writeAppearance()
631 632 {
632 633 // Try to make the found item the second one, but
633 634 // adjust for under/overflow.
634   - int wanted_first = found_idx - 1;
635   - int wanted_last = found_idx + max_rows - 2;
  635 + int wanted_first = QIntC::to_int(found_idx) - 1;
  636 + int wanted_last = QIntC::to_int(found_idx + max_rows) - 2;
636 637 QTC::TC("qpdf", "QPDFFormFieldObjectHelper list found");
637 638 while (wanted_first < 0)
638 639 {
... ... @@ -640,7 +641,7 @@ ValueSetter::writeAppearance()
640 641 ++wanted_first;
641 642 ++wanted_last;
642 643 }
643   - while (wanted_last >= static_cast<int>(nopt))
  644 + while (wanted_last >= QIntC::to_int(nopt))
644 645 {
645 646 QTC::TC("qpdf", "QPDFFormFieldObjectHelper list last too high");
646 647 if (wanted_first > 0)
... ... @@ -650,8 +651,9 @@ ValueSetter::writeAppearance()
650 651 --wanted_last;
651 652 }
652 653 highlight = true;
653   - highlight_idx = found_idx - wanted_first;
654   - for (int i = wanted_first; i <= wanted_last; ++i)
  654 + highlight_idx = found_idx - QIntC::to_size(wanted_first);
  655 + for (size_t i = QIntC::to_size(wanted_first);
  656 + i <= QIntC::to_size(wanted_last); ++i)
655 657 {
656 658 lines.push_back(opt.at(i));
657 659 }
... ... @@ -672,13 +674,15 @@ ValueSetter::writeAppearance()
672 674  
673 675 // Write the lines centered vertically, highlighting if needed
674 676 size_t nlines = lines.size();
675   - double dy = bbox.ury - ((bbox.ury - bbox.lly - (nlines * tfh)) / 2.0);
  677 + double dy = bbox.ury - ((bbox.ury - bbox.lly -
  678 + (static_cast<double>(nlines) * tfh)) / 2.0);
676 679 if (highlight)
677 680 {
678 681 write("q\n0.85 0.85 0.85 rg\n" +
679 682 QUtil::double_to_string(bbox.llx) + " " +
680   - QUtil::double_to_string(bbox.lly + dy -
681   - (tfh * (highlight_idx + 1))) + " " +
  683 + QUtil::double_to_string(
  684 + bbox.lly + dy -
  685 + (tfh * (static_cast<double>(highlight_idx + 1)))) + " " +
682 686 QUtil::double_to_string(bbox.urx - bbox.llx) + " " +
683 687 QUtil::double_to_string(tfh) +
684 688 " re f\nQ\n");
... ... @@ -693,8 +697,10 @@ ValueSetter::writeAppearance()
693 697 // which doesn't seem really worth the effort.
694 698 if (i == 0)
695 699 {
696   - write(QUtil::double_to_string(bbox.llx + dx) + " " +
697   - QUtil::double_to_string(bbox.lly + dy) + " Td\n");
  700 + write(QUtil::double_to_string(bbox.llx + static_cast<double>(dx)) +
  701 + " " +
  702 + QUtil::double_to_string(bbox.lly + static_cast<double>(dy)) +
  703 + " Td\n");
698 704 }
699 705 else
700 706 {
... ...
libqpdf/QPDFNameTreeObjectHelper.cc
... ... @@ -30,8 +30,8 @@ QPDFNameTreeObjectHelper::updateMap(QPDFObjectHandle oh)
30 30 QPDFObjectHandle names = oh.getKey("/Names");
31 31 if (names.isArray())
32 32 {
33   - size_t nitems = names.getArrayNItems();
34   - size_t i = 0;
  33 + int nitems = names.getArrayNItems();
  34 + int i = 0;
35 35 while (i < nitems - 1)
36 36 {
37 37 QPDFObjectHandle name = names.getArrayItem(i);
... ... @@ -47,8 +47,8 @@ QPDFNameTreeObjectHelper::updateMap(QPDFObjectHandle oh)
47 47 QPDFObjectHandle kids = oh.getKey("/Kids");
48 48 if (kids.isArray())
49 49 {
50   - size_t nitems = kids.getArrayNItems();
51   - for (size_t i = 0; i < nitems; ++i)
  50 + int nitems = kids.getArrayNItems();
  51 + for (int i = 0; i < nitems; ++i)
52 52 {
53 53 updateMap(kids.getArrayItem(i));
54 54 }
... ...
libqpdf/QPDFNumberTreeObjectHelper.cc
... ... @@ -26,8 +26,8 @@ QPDFNumberTreeObjectHelper::updateMap(QPDFObjectHandle oh)
26 26 QPDFObjectHandle nums = oh.getKey("/Nums");
27 27 if (nums.isArray())
28 28 {
29   - size_t nitems = nums.getArrayNItems();
30   - size_t i = 0;
  29 + int nitems = nums.getArrayNItems();
  30 + int i = 0;
31 31 while (i < nitems - 1)
32 32 {
33 33 QPDFObjectHandle num = nums.getArrayItem(i);
... ... @@ -43,8 +43,8 @@ QPDFNumberTreeObjectHelper::updateMap(QPDFObjectHandle oh)
43 43 QPDFObjectHandle kids = oh.getKey("/Kids");
44 44 if (kids.isArray())
45 45 {
46   - size_t nitems = kids.getArrayNItems();
47   - for (size_t i = 0; i < nitems; ++i)
  46 + int nitems = kids.getArrayNItems();
  47 + for (int i = 0; i < nitems; ++i)
48 48 {
49 49 updateMap(kids.getArrayItem(i));
50 50 }
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -22,6 +22,7 @@
22 22  
23 23 #include <qpdf/QTC.hh>
24 24 #include <qpdf/QUtil.hh>
  25 +#include <qpdf/QIntC.hh>
25 26  
26 27 #include <stdexcept>
27 28 #include <stdlib.h>
... ... @@ -643,7 +644,7 @@ QPDFObjectHandle::isRectangle()
643 644 {
644 645 return false;
645 646 }
646   - for (size_t i = 0; i < 4; ++i)
  647 + for (int i = 0; i < 4; ++i)
647 648 {
648 649 if (! getArrayItem(i).isNumber())
649 650 {
... ... @@ -664,7 +665,7 @@ QPDFObjectHandle::isMatrix()
664 665 {
665 666 return false;
666 667 }
667   - for (size_t i = 0; i < 6; ++i)
  668 + for (int i = 0; i < 6; ++i)
668 669 {
669 670 if (! getArrayItem(i).isNumber())
670 671 {
... ... @@ -1013,7 +1014,7 @@ QPDFObjectHandle::getUniqueResourceName(std::string const&amp; prefix,
1013 1014 int& min_suffix)
1014 1015 {
1015 1016 std::set<std::string> names = getResourceNames();
1016   - int max_suffix = min_suffix + names.size();
  1017 + int max_suffix = min_suffix + QIntC::to_int(names.size());
1017 1018 while (min_suffix <= max_suffix)
1018 1019 {
1019 1020 std::string candidate = prefix + QUtil::int_to_string(min_suffix);
... ... @@ -1374,7 +1375,7 @@ QPDFObjectHandle::rotatePage(int angle, bool relative)
1374 1375 if (cur_obj.getKey("/Rotate").isInteger())
1375 1376 {
1376 1377 found_rotate = true;
1377   - old_angle = cur_obj.getKey("/Rotate").getIntValue();
  1378 + old_angle = cur_obj.getKey("/Rotate").getIntValueAsInt();
1378 1379 }
1379 1380 else if (cur_obj.getKey("/Parent").isDictionary())
1380 1381 {
... ... @@ -1506,7 +1507,7 @@ QPDFObjectHandle::parse(std::string const&amp; object_str,
1506 1507 bool empty = false;
1507 1508 QPDFObjectHandle result =
1508 1509 parse(input, object_description, tokenizer, empty, 0, 0);
1509   - size_t offset = input->tell();
  1510 + size_t offset = QIntC::to_size(input->tell());
1510 1511 while (offset < object_str.length())
1511 1512 {
1512 1513 if (! isspace(object_str.at(offset)))
... ... @@ -1618,7 +1619,7 @@ QPDFObjectHandle::parseContentStream_data(
1618 1619 QPDFTokenizer tokenizer;
1619 1620 tokenizer.allowEOF();
1620 1621 bool empty = false;
1621   - while (static_cast<size_t>(input->tell()) < length)
  1622 + while (QIntC::to_size(input->tell()) < length)
1622 1623 {
1623 1624 QPDFObjectHandle obj =
1624 1625 parseInternal(input, "content", tokenizer, empty, 0, 0, true);
... ... @@ -1863,8 +1864,8 @@ QPDFObjectHandle::parseInternal(PointerHolder&lt;InputSource&gt; input,
1863 1864 // Try to resolve indirect objects
1864 1865 object = newIndirect(
1865 1866 context,
1866   - olist.at(olist.size() - 2).getIntValue(),
1867   - olist.at(olist.size() - 1).getIntValue());
  1867 + olist.at(olist.size() - 2).getIntValueAsInt(),
  1868 + olist.at(olist.size() - 1).getIntValueAsInt());
1868 1869 olist.pop_back();
1869 1870 olist.pop_back();
1870 1871 }
... ...
libqpdf/QPDFOutlineObjectHelper.cc
... ... @@ -100,7 +100,7 @@ QPDFOutlineObjectHelper::getCount()
100 100 int count = 0;
101 101 if (this->oh.hasKey("/Count"))
102 102 {
103   - count = this->oh.getKey("/Count").getIntValue();
  103 + count = this->oh.getKey("/Count").getIntValueAsInt();
104 104 }
105 105 return count;
106 106 }
... ...
libqpdf/QPDFPageDocumentHelper.cc
... ... @@ -117,7 +117,7 @@ QPDFPageDocumentHelper::flattenAnnotationsForPage(
117 117 page.getObjectHandle().getKey("/Rotate");
118 118 if (rotate_obj.isInteger() && rotate_obj.getIntValue())
119 119 {
120   - rotate = rotate_obj.getIntValue();
  120 + rotate = rotate_obj.getIntValueAsInt();
121 121 }
122 122 int next_fx = 1;
123 123 for (std::vector<QPDFAnnotationObjectHelper>::iterator iter =
... ...
libqpdf/QPDFPageObjectHelper.cc
... ... @@ -6,6 +6,7 @@
6 6 #include <qpdf/QUtil.hh>
7 7 #include <qpdf/QPDFExc.hh>
8 8 #include <qpdf/QPDFMatrix.hh>
  9 +#include <qpdf/QIntC.hh>
9 10  
10 11 class ContentProvider: public QPDFObjectHandle::StreamDataProvider
11 12 {
... ... @@ -236,7 +237,9 @@ InlineImageTracker::handleToken(QPDFTokenizer::Token const&amp; token)
236 237 b.finish();
237 238 QPDFObjectHandle dict =
238 239 convertIIDict(QPDFObjectHandle::parse(dict_str));
239   - dict.replaceKey("/Length", QPDFObjectHandle::newInteger(len));
  240 + dict.replaceKey(
  241 + "/Length",
  242 + QPDFObjectHandle::newInteger(QIntC::to_longlong(len)));
240 243 std::string name = resources.getUniqueResourceName(
241 244 "/IIm", this->min_suffix);
242 245 QPDFObjectHandle image = QPDFObjectHandle::newStream(
... ... @@ -391,8 +394,8 @@ QPDFPageObjectHelper::getAnnotations(std::string const&amp; only_subtype)
391 394 QPDFObjectHandle annots = this->oh.getKey("/Annots");
392 395 if (annots.isArray())
393 396 {
394   - size_t nannots = annots.getArrayNItems();
395   - for (size_t i = 0; i < nannots; ++i)
  397 + int nannots = annots.getArrayNItems();
  398 + for (int i = 0; i < nannots; ++i)
396 399 {
397 400 QPDFObjectHandle annot = annots.getArrayItem(i);
398 401 if (only_subtype.empty() ||
... ... @@ -579,7 +582,7 @@ QPDFPageObjectHelper::getMatrixForTransformations(bool invert)
579 582 ? scale_obj.getNumericValue()
580 583 : 1.0);
581 584 int rotate = (rotate_obj.isInteger()
582   - ? rotate_obj.getIntValue()
  585 + ? rotate_obj.getIntValueAsInt()
583 586 : 0);
584 587 if (invert)
585 588 {
... ...
libqpdf/QPDFTokenizer.cc
... ... @@ -8,6 +8,7 @@
8 8 #include <qpdf/QPDFExc.hh>
9 9 #include <qpdf/QUtil.hh>
10 10 #include <qpdf/QPDFObjectHandle.hh>
  11 +#include <qpdf/QIntC.hh>
11 12  
12 13 #include <stdexcept>
13 14 #include <stdlib.h>
... ... @@ -715,7 +716,7 @@ QPDFTokenizer::findEI(PointerHolder&lt;InputSource&gt; input)
715 716 {
716 717 break;
717 718 }
718   - this->m->inline_image_bytes = input->tell() - pos - 2;
  719 + this->m->inline_image_bytes = QIntC::to_size(input->tell() - pos - 2);
719 720  
720 721 QPDFTokenizer check;
721 722 bool found_bad = false;
... ...
libqpdf/QPDFWriter.cc
... ... @@ -19,6 +19,7 @@
19 19 #include <qpdf/QPDFObjectHandle.hh>
20 20 #include <qpdf/QPDF_Name.hh>
21 21 #include <qpdf/QPDF_String.hh>
  22 +#include <qpdf/QIntC.hh>
22 23  
23 24 #include <algorithm>
24 25 #include <stdlib.h>
... ... @@ -722,11 +723,11 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
722 723 this->m->id1 =
723 724 trailer.getKey("/ID").getArrayItem(0).getStringValue();
724 725 QPDFObjectHandle encrypt = trailer.getKey("/Encrypt");
725   - int V = encrypt.getKey("/V").getIntValue();
  726 + int V = encrypt.getKey("/V").getIntValueAsInt();
726 727 int key_len = 5;
727 728 if (V > 1)
728 729 {
729   - key_len = encrypt.getKey("/Length").getIntValue() / 8;
  730 + key_len = encrypt.getKey("/Length").getIntValueAsInt() / 8;
730 731 }
731 732 if (encrypt.hasKey("/EncryptMetadata") &&
732 733 encrypt.getKey("/EncryptMetadata").isBool())
... ... @@ -763,9 +764,9 @@ QPDFWriter::copyEncryptionParameters(QPDF&amp; qpdf)
763 764  
764 765 setEncryptionParametersInternal(
765 766 V,
766   - encrypt.getKey("/R").getIntValue(),
  767 + encrypt.getKey("/R").getIntValueAsInt(),
767 768 key_len,
768   - encrypt.getKey("/P").getIntValue(),
  769 + encrypt.getKey("/P").getIntValueAsInt(),
769 770 encrypt.getKey("/O").getStringValue(),
770 771 encrypt.getKey("/U").getStringValue(),
771 772 OE,
... ... @@ -884,7 +885,7 @@ QPDFWriter::compareVersions(int major1, int minor1,
884 885  
885 886 void
886 887 QPDFWriter::setEncryptionParametersInternal(
887   - int V, int R, int key_len, long P,
  888 + int V, int R, int key_len, int P,
888 889 std::string const& O, std::string const& U,
889 890 std::string const& OE, std::string const& UE, std::string const& Perms,
890 891 std::string const& id1, std::string const& user_password,
... ... @@ -972,10 +973,10 @@ QPDFWriter::setDataKey(int objid)
972 973 this->m->encrypt_use_aes, this->m->encryption_V, this->m->encryption_R);
973 974 }
974 975  
975   -int
976   -QPDFWriter::bytesNeeded(unsigned long long n)
  976 +unsigned int
  977 +QPDFWriter::bytesNeeded(long long n)
977 978 {
978   - int bytes = 0;
  979 + unsigned int bytes = 0;
979 980 while (n)
980 981 {
981 982 ++bytes;
... ... @@ -1121,7 +1122,7 @@ QPDFWriter::pushEncryptionFilter()
1121 1122 {
1122 1123 p = new Pl_RC4("rc4 stream encryption", this->m->pipeline,
1123 1124 QUtil::unsigned_char_pointer(this->m->cur_data_key),
1124   - this->m->cur_data_key.length());
  1125 + QIntC::to_int(this->m->cur_data_key.length()));
1125 1126 }
1126 1127 pushPipeline(p);
1127 1128 }
... ... @@ -1356,7 +1357,8 @@ QPDFWriter::writeTrailer(trailer_e which, int size, bool xref_stream,
1356 1357 writeString(" /Prev ");
1357 1358 qpdf_offset_t pos = this->m->pipeline->getCount();
1358 1359 writeString(QUtil::int_to_string(prev));
1359   - int nspaces = pos - this->m->pipeline->getCount() + 21;
  1360 + int nspaces =
  1361 + QIntC::to_int(pos - this->m->pipeline->getCount() + 21);
1360 1362 if (nspaces < 0)
1361 1363 {
1362 1364 throw std::logic_error(
... ... @@ -1430,19 +1432,18 @@ QPDFWriter::writeTrailer(trailer_e which, int size, bool xref_stream,
1430 1432 }
1431 1433  
1432 1434 void
1433   -QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1434   - unsigned int flags)
  1435 +QPDFWriter::unparseObject(QPDFObjectHandle object, int level, int flags)
1435 1436 {
1436 1437 unparseObject(object, level, flags, 0, false);
1437 1438 }
1438 1439  
1439 1440 void
1440 1441 QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1441   - unsigned int flags, size_t stream_length,
  1442 + int flags, size_t stream_length,
1442 1443 bool compress)
1443 1444 {
1444 1445 QPDFObjGen old_og = object.getObjGen();
1445   - unsigned int child_flags = flags & ~f_stream;
  1446 + int child_flags = flags & ~f_stream;
1446 1447  
1447 1448 std::string indent;
1448 1449 for (int i = 0; i < level; ++i)
... ... @@ -1687,7 +1688,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1687 1688  
1688 1689 if (this->m->direct_stream_lengths)
1689 1690 {
1690   - writeString(QUtil::int_to_string(stream_length));
  1691 + writeString(QUtil::uint_to_string(stream_length));
1691 1692 }
1692 1693 else
1693 1694 {
... ... @@ -1818,7 +1819,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1818 1819 writeString("\nstream\n");
1819 1820 pushEncryptionFilter();
1820 1821 writeBuffer(stream_data);
1821   - char last_char = this->m->pipeline->getLastChar();
  1822 + unsigned char last_char = this->m->pipeline->getLastChar();
1822 1823 popPipelineStack();
1823 1824  
1824 1825 if (this->m->newline_before_endstream ||
... ... @@ -1861,7 +1862,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
1861 1862 char* tmp = QUtil::copy_string(val);
1862 1863 size_t vlen = val.length();
1863 1864 RC4 rc4(QUtil::unsigned_char_pointer(this->m->cur_data_key),
1864   - this->m->cur_data_key.length());
  1865 + QIntC::to_int(this->m->cur_data_key.length()));
1865 1866 rc4.process(QUtil::unsigned_char_pointer(tmp), vlen);
1866 1867 val = QPDF_String(std::string(tmp, vlen)).unparse();
1867 1868 delete [] tmp;
... ... @@ -1883,14 +1884,14 @@ void
1883 1884 QPDFWriter::writeObjectStreamOffsets(std::vector<qpdf_offset_t>& offsets,
1884 1885 int first_obj)
1885 1886 {
1886   - for (unsigned int i = 0; i < offsets.size(); ++i)
  1887 + for (size_t i = 0; i < offsets.size(); ++i)
1887 1888 {
1888 1889 if (i != 0)
1889 1890 {
1890 1891 writeStringQDF("\n");
1891 1892 writeStringNoQDF(" ");
1892 1893 }
1893   - writeString(QUtil::int_to_string(i + first_obj));
  1894 + writeString(QUtil::uint_to_string(i + QIntC::to_size(first_obj)));
1894 1895 writeString(" ");
1895 1896 writeString(QUtil::int_to_string(offsets.at(i)));
1896 1897 }
... ... @@ -2015,13 +2016,13 @@ QPDFWriter::writeObjectStream(QPDFObjectHandle object)
2015 2016 writeStringQDF("\n ");
2016 2017 size_t length = stream_buffer->getSize();
2017 2018 adjustAESStreamLength(length);
2018   - writeString(" /Length " + QUtil::int_to_string(length));
  2019 + writeString(" /Length " + QUtil::uint_to_string(length));
2019 2020 writeStringQDF("\n ");
2020 2021 if (compressed)
2021 2022 {
2022 2023 writeString(" /Filter /FlateDecode");
2023 2024 }
2024   - writeString(" /N " + QUtil::int_to_string(offsets.size()));
  2025 + writeString(" /N " + QUtil::uint_to_string(offsets.size()));
2025 2026 writeStringQDF("\n ");
2026 2027 writeString(" /First " + QUtil::int_to_string(first));
2027 2028 if (! object.isNull())
... ... @@ -2120,7 +2121,7 @@ QPDFWriter::writeObject(QPDFObjectHandle object, int object_stream_index)
2120 2121 }
2121 2122 }
2122 2123 openObject(new_id + 1);
2123   - writeString(QUtil::int_to_string(this->m->cur_stream_length));
  2124 + writeString(QUtil::uint_to_string(this->m->cur_stream_length));
2124 2125 closeObject(new_id + 1);
2125 2126 }
2126 2127 }
... ... @@ -2308,12 +2309,12 @@ QPDFWriter::generateObjectStreams()
2308 2309  
2309 2310 std::vector<QPDFObjGen> const& eligible =
2310 2311 QPDF::Writer::getCompressibleObjGens(this->m->pdf);
2311   - unsigned int n_object_streams = (eligible.size() + 99) / 100;
  2312 + size_t n_object_streams = (eligible.size() + 99U) / 100U;
2312 2313 if (n_object_streams == 0)
2313 2314 {
2314 2315 return;
2315 2316 }
2316   - unsigned int n_per = eligible.size() / n_object_streams;
  2317 + size_t n_per = eligible.size() / n_object_streams;
2317 2318 if (n_per * n_object_streams < eligible.size())
2318 2319 {
2319 2320 ++n_per;
... ... @@ -2640,7 +2641,7 @@ QPDFWriter::doWriteSetup()
2640 2641 this->m->object_stream_to_objects[stream].insert(obj);
2641 2642 this->m->max_ostream_index =
2642 2643 std::max(this->m->max_ostream_index,
2643   - static_cast<int>(
  2644 + QIntC::to_int(
2644 2645 this->m->object_stream_to_objects[stream].size()) - 1);
2645 2646 }
2646 2647  
... ... @@ -2672,7 +2673,7 @@ QPDFWriter::write()
2672 2673 // files, we write two passes. events_expected is an
2673 2674 // approximation, but it's good enough for progress reporting,
2674 2675 // which is mostly a guess anyway.
2675   - this->m->events_expected = (
  2676 + this->m->events_expected = QIntC::to_int(
2676 2677 this->m->pdf.getObjectCount() * (this->m->linearized ? 3 : 2));
2677 2678  
2678 2679 prepareFileForWrite();
... ... @@ -2785,7 +2786,7 @@ QPDFWriter::writeHintStream(int hint_id)
2785 2786 }
2786 2787 writeString(" /Length ");
2787 2788 adjustAESStreamLength(hlen);
2788   - writeString(QUtil::int_to_string(hlen));
  2789 + writeString(QUtil::uint_to_string(hlen));
2789 2790 writeString(" >>\nstream\n");
2790 2791  
2791 2792 if (this->m->encrypted)
... ... @@ -2794,7 +2795,7 @@ QPDFWriter::writeHintStream(int hint_id)
2794 2795 }
2795 2796 pushEncryptionFilter();
2796 2797 writeBuffer(hint_buffer);
2797   - char last_char = this->m->pipeline->getLastChar();
  2798 + unsigned char last_char = this->m->pipeline->getLastChar();
2798 2799 popPipelineStack();
2799 2800  
2800 2801 if (last_char != '\n')
... ... @@ -2872,11 +2873,11 @@ QPDFWriter::writeXRefStream(int xref_id, int max_id, qpdf_offset_t max_offset,
2872 2873 qpdf_offset_t space_before_zero = xref_offset - 1;
2873 2874  
2874 2875 // field 1 contains offsets and object stream identifiers
2875   - int f1_size = std::max(bytesNeeded(max_offset + hint_length),
2876   - bytesNeeded(max_id));
  2876 + unsigned int f1_size = std::max(bytesNeeded(max_offset + hint_length),
  2877 + bytesNeeded(max_id));
2877 2878  
2878 2879 // field 2 contains object stream indices
2879   - int f2_size = bytesNeeded(this->m->max_ostream_index);
  2880 + unsigned int f2_size = bytesNeeded(this->m->max_ostream_index);
2880 2881  
2881 2882 unsigned int esize = 1 + f1_size + f2_size;
2882 2883  
... ... @@ -2925,15 +2926,15 @@ QPDFWriter::writeXRefStream(int xref_id, int max_id, qpdf_offset_t max_offset,
2925 2926 offset += hint_length;
2926 2927 }
2927 2928 writeBinary(1, 1);
2928   - writeBinary(offset, f1_size);
  2929 + writeBinary(QIntC::to_ulonglong(offset), f1_size);
2929 2930 writeBinary(0, f2_size);
2930 2931 }
2931 2932 break;
2932 2933  
2933 2934 case 2:
2934 2935 writeBinary(2, 1);
2935   - writeBinary(e.getObjStreamNumber(), f1_size);
2936   - writeBinary(e.getObjStreamIndex(), f2_size);
  2936 + writeBinary(QIntC::to_ulonglong(e.getObjStreamNumber()), f1_size);
  2937 + writeBinary(QIntC::to_ulonglong(e.getObjStreamIndex()), f2_size);
2937 2938 break;
2938 2939  
2939 2940 default:
... ... @@ -2949,7 +2950,7 @@ QPDFWriter::writeXRefStream(int xref_id, int max_id, qpdf_offset_t max_offset,
2949 2950 writeStringQDF("\n ");
2950 2951 writeString(" /Type /XRef");
2951 2952 writeStringQDF("\n ");
2952   - writeString(" /Length " + QUtil::int_to_string(xref_data->getSize()));
  2953 + writeString(" /Length " + QUtil::uint_to_string(xref_data->getSize()));
2953 2954 if (compressed)
2954 2955 {
2955 2956 writeStringQDF("\n ");
... ... @@ -2977,7 +2978,7 @@ QPDFWriter::writeXRefStream(int xref_id, int max_id, qpdf_offset_t max_offset,
2977 2978 }
2978 2979  
2979 2980 int
2980   -QPDFWriter::calculateXrefStreamPadding(int xref_bytes)
  2981 +QPDFWriter::calculateXrefStreamPadding(qpdf_offset_t xref_bytes)
2981 2982 {
2982 2983 // This routine is called right after a linearization first pass
2983 2984 // xref stream has been written without compression. Calculate
... ... @@ -2987,7 +2988,7 @@ QPDFWriter::calculateXrefStreamPadding(int xref_bytes)
2987 2988 // input by 6 bytes plus 5 bytes per 16K, and then we'll add 10
2988 2989 // extra bytes for number length increases.
2989 2990  
2990   - return 16 + (5 * ((xref_bytes + 16383) / 16384));
  2991 + return QIntC::to_int(16 + (5 * ((xref_bytes + 16383) / 16384)));
2991 2992 }
2992 2993  
2993 2994 void
... ... @@ -3062,7 +3063,8 @@ QPDFWriter::writeLinearized()
3062 3063 //
3063 3064  
3064 3065 // Second half objects
3065   - int second_half_uncompressed = part7.size() + part8.size() + part9.size();
  3066 + int second_half_uncompressed =
  3067 + QIntC::to_int(part7.size() + part8.size() + part9.size());
3066 3068 int second_half_first_obj = 1;
3067 3069 int after_second_half = 1 + second_half_uncompressed;
3068 3070 this->m->next_objid = after_second_half;
... ... @@ -3093,7 +3095,7 @@ QPDFWriter::writeLinearized()
3093 3095 first_half_xref = this->m->next_objid++;
3094 3096 }
3095 3097 int part4_first_obj = this->m->next_objid;
3096   - this->m->next_objid += part4.size();
  3098 + this->m->next_objid += QIntC::to_int(part4.size());
3097 3099 int after_part4 = this->m->next_objid;
3098 3100 if (this->m->encrypted)
3099 3101 {
... ... @@ -3101,7 +3103,7 @@ QPDFWriter::writeLinearized()
3101 3103 }
3102 3104 int hint_id = this->m->next_objid++;
3103 3105 int part6_first_obj = this->m->next_objid;
3104   - this->m->next_objid += part6.size();
  3106 + this->m->next_objid += QIntC::to_int(part6.size());
3105 3107 int after_part6 = this->m->next_objid;
3106 3108 // Assign numbers to all compressed objects in the first half
3107 3109 std::vector<QPDFObjectHandle>* vecs1[] = {&part4, &part6};
... ... @@ -3188,7 +3190,7 @@ QPDFWriter::writeLinearized()
3188 3190 this->m->pdf.getAllPages();
3189 3191 int first_page_object =
3190 3192 this->m->obj_renumber[pages.at(0).getObjGen()];
3191   - int npages = pages.size();
  3193 + int npages = QIntC::to_int(pages.size());
3192 3194  
3193 3195 writeString(" /Linearized 1 /L ");
3194 3196 writeString(QUtil::int_to_string(file_size + hint_length));
... ... @@ -3211,7 +3213,7 @@ QPDFWriter::writeLinearized()
3211 3213 writeString(" >>");
3212 3214 closeObject(lindict_id);
3213 3215 static int const pad = 200;
3214   - int spaces = (pos - this->m->pipeline->getCount() + pad);
  3216 + int spaces = QIntC::to_int(pos - this->m->pipeline->getCount() + pad);
3215 3217 assert(spaces >= 0);
3216 3218 writePad(spaces);
3217 3219 writeString("\n");
... ... @@ -3263,7 +3265,7 @@ QPDFWriter::writeLinearized()
3263 3265 {
3264 3266 // Pad so that the next object starts at the same
3265 3267 // place as in pass 1.
3266   - writePad(first_xref_end - endpos);
  3268 + writePad(QIntC::to_int(first_xref_end - endpos));
3267 3269  
3268 3270 if (this->m->pipeline->getCount() != first_xref_end)
3269 3271 {
... ... @@ -3346,7 +3348,8 @@ QPDFWriter::writeLinearized()
3346 3348 {
3347 3349 // Make the file size the same.
3348 3350 qpdf_offset_t pos = this->m->pipeline->getCount();
3349   - writePad(second_xref_end + hint_length - 1 - pos);
  3351 + writePad(
  3352 + QIntC::to_int(second_xref_end + hint_length - 1 - pos));
3350 3353 writeString("\n");
3351 3354  
3352 3355 // If this assertion fails, maybe we didn't have
... ... @@ -3396,7 +3399,7 @@ QPDFWriter::writeLinearized()
3396 3399 activatePipelineStack();
3397 3400 writeHintStream(hint_id);
3398 3401 popPipelineStack(&hint_buffer);
3399   - hint_length = hint_buffer->getSize();
  3402 + hint_length = QIntC::to_offset(hint_buffer->getSize());
3400 3403  
3401 3404 // Restore hint offset
3402 3405 this->m->xref[hint_id] = QPDFXRefEntry(1, hint_offset, 0);
... ...
libqpdf/QPDFXRefEntry.cc
1 1 #include <qpdf/QPDFXRefEntry.hh>
2 2 #include <qpdf/QPDFExc.hh>
3 3 #include <qpdf/QUtil.hh>
  4 +#include <qpdf/QIntC.hh>
4 5  
5 6 QPDFXRefEntry::QPDFXRefEntry() :
6 7 type(0),
... ... @@ -46,7 +47,7 @@ QPDFXRefEntry::getObjStreamNumber() const
46 47 throw std::logic_error(
47 48 "getObjStreamNumber called for xref entry of type != 2");
48 49 }
49   - return this->field1;
  50 + return QIntC::to_int(this->field1);
50 51 }
51 52  
52 53 int
... ...
libqpdf/QPDF_Array.cc
1 1 #include <qpdf/QPDF_Array.hh>
2 2 #include <qpdf/QUtil.hh>
  3 +#include <qpdf/QIntC.hh>
3 4 #include <stdexcept>
4 5  
5 6 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& items) :
... ... @@ -68,18 +69,20 @@ QPDF_Array::setDescription(QPDF* qpdf, std::string const&amp; description)
68 69 int
69 70 QPDF_Array::getNItems() const
70 71 {
71   - return this->items.size();
  72 + // This should really return a size_t, but changing it would break
  73 + // a lot of code.
  74 + return QIntC::to_int(this->items.size());
72 75 }
73 76  
74 77 QPDFObjectHandle
75 78 QPDF_Array::getItem(int n) const
76 79 {
77   - if ((n < 0) || (n >= static_cast<int>(this->items.size())))
  80 + if ((n < 0) || (n >= QIntC::to_int(this->items.size())))
78 81 {
79 82 throw std::logic_error(
80 83 "INTERNAL ERROR: bounds error accessing QPDF_Array element");
81 84 }
82   - return this->items.at(n);
  85 + return this->items.at(QIntC::to_size(n));
83 86 }
84 87  
85 88 std::vector<QPDFObjectHandle> const&
... ... @@ -93,7 +96,7 @@ QPDF_Array::setItem(int n, QPDFObjectHandle const&amp; oh)
93 96 {
94 97 // Call getItem for bounds checking
95 98 (void) getItem(n);
96   - this->items.at(n) = oh;
  99 + this->items.at(QIntC::to_size(n)) = oh;
97 100 }
98 101  
99 102 void
... ... @@ -106,7 +109,7 @@ void
106 109 QPDF_Array::insertItem(int at, QPDFObjectHandle const& item)
107 110 {
108 111 // As special case, also allow insert beyond the end
109   - if ((at < 0) || (at > static_cast<int>(this->items.size())))
  112 + if ((at < 0) || (at > QIntC::to_int(this->items.size())))
110 113 {
111 114 throw std::logic_error(
112 115 "INTERNAL ERROR: bounds error accessing QPDF_Array element");
... ...
libqpdf/QPDF_Stream.cc
... ... @@ -18,6 +18,7 @@
18 18 #include <qpdf/QPDF.hh>
19 19 #include <qpdf/QPDFExc.hh>
20 20 #include <qpdf/Pl_QPDFTokenizer.hh>
  21 +#include <qpdf/QIntC.hh>
21 22  
22 23 #include <stdexcept>
23 24  
... ... @@ -199,7 +200,7 @@ QPDF_Stream::understandDecodeParams(
199 200 QPDFObjectHandle predictor_obj = decode_obj.getKey(key);
200 201 if (predictor_obj.isInteger())
201 202 {
202   - predictor = predictor_obj.getIntValue();
  203 + predictor = predictor_obj.getIntValueAsInt();
203 204 if (! ((predictor == 1) || (predictor == 2) ||
204 205 ((predictor >= 10) && (predictor <= 15))))
205 206 {
... ... @@ -216,7 +217,7 @@ QPDF_Stream::understandDecodeParams(
216 217 QPDFObjectHandle earlychange_obj = decode_obj.getKey(key);
217 218 if (earlychange_obj.isInteger())
218 219 {
219   - int earlychange = earlychange_obj.getIntValue();
  220 + int earlychange = earlychange_obj.getIntValueAsInt();
220 221 early_code_change = (earlychange == 1);
221 222 if (! ((earlychange == 0) || (earlychange == 1)))
222 223 {
... ... @@ -235,7 +236,7 @@ QPDF_Stream::understandDecodeParams(
235 236 QPDFObjectHandle param_obj = decode_obj.getKey(key);
236 237 if (param_obj.isInteger())
237 238 {
238   - int val = param_obj.getIntValue();
  239 + int val = param_obj.getIntValueAsInt();
239 240 if (key == "/Columns")
240 241 {
241 242 columns = val;
... ... @@ -550,7 +551,9 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline,
550 551 QTC::TC("qpdf", "QPDF_Stream PNG filter");
551 552 pipeline = new Pl_PNGFilter(
552 553 "png decode", pipeline, Pl_PNGFilter::a_decode,
553   - columns, colors, bits_per_component);
  554 + QIntC::to_uint(columns),
  555 + QIntC::to_uint(colors),
  556 + QIntC::to_uint(bits_per_component));
554 557 to_delete.push_back(pipeline);
555 558 }
556 559 else if (predictor == 2)
... ... @@ -558,7 +561,9 @@ QPDF_Stream::pipeStreamData(Pipeline* pipeline,
558 561 QTC::TC("qpdf", "QPDF_Stream TIFF predictor");
559 562 pipeline = new Pl_TIFFPredictor(
560 563 "tiff decode", pipeline, Pl_TIFFPredictor::a_decode,
561   - columns, colors, bits_per_component);
  564 + QIntC::to_uint(columns),
  565 + QIntC::to_uint(colors),
  566 + QIntC::to_uint(bits_per_component));
562 567 to_delete.push_back(pipeline);
563 568 }
564 569 }
... ... @@ -744,7 +749,8 @@ QPDF_Stream::replaceFilterData(QPDFObjectHandle const&amp; filter,
744 749 else
745 750 {
746 751 this->stream_dict.replaceKey(
747   - "/Length", QPDFObjectHandle::newInteger(length));
  752 + "/Length", QPDFObjectHandle::newInteger(
  753 + QIntC::to_longlong(length)));
748 754 }
749 755 }
750 756  
... ... @@ -756,7 +762,7 @@ QPDF_Stream::replaceDict(QPDFObjectHandle new_dict)
756 762 QPDFObjectHandle length_obj = new_dict.getKey("/Length");
757 763 if (length_obj.isInteger())
758 764 {
759   - this->length = length_obj.getIntValue();
  765 + this->length = QIntC::to_size(length_obj.getUIntValue());
760 766 }
761 767 else
762 768 {
... ...
libqpdf/QPDF_String.cc
... ... @@ -9,13 +9,14 @@
9 9 #include <string.h>
10 10  
11 11 // See above about ctype.
12   -static bool is_ascii_printable(unsigned char ch)
  12 +static bool is_ascii_printable(char ch)
13 13 {
14 14 return ((ch >= 32) && (ch <= 126));
15 15 }
16   -static bool is_iso_latin1_printable(unsigned char ch)
  16 +static bool is_iso_latin1_printable(char ch)
17 17 {
18   - return (((ch >= 32) && (ch <= 126)) || (ch >= 160));
  18 + return (((ch >= 32) && (ch <= 126)) ||
  19 + (static_cast<unsigned char>(ch) >= 160));
19 20 }
20 21  
21 22 QPDF_String::QPDF_String(std::string const& val) :
... ...
libqpdf/QPDF_encryption.cc
... ... @@ -130,9 +130,9 @@ QPDF::EncryptionData::setV5EncryptionParameters(
130 130 static void
131 131 pad_or_truncate_password_V4(std::string const& password, char k1[key_bytes])
132 132 {
133   - int password_bytes = std::min(static_cast<size_t>(key_bytes),
134   - password.length());
135   - int pad_bytes = key_bytes - password_bytes;
  133 + size_t password_bytes = std::min(QIntC::to_size(key_bytes),
  134 + password.length());
  135 + size_t pad_bytes = key_bytes - password_bytes;
136 136 memcpy(k1, password.c_str(), password_bytes);
137 137 memcpy(k1 + password_bytes, padding_string, pad_bytes);
138 138 }
... ... @@ -154,9 +154,10 @@ QPDF::trim_user_password(std::string&amp; user_password)
154 154 char const* p2 = 0;
155 155 while ((p2 = strchr(p1, '\x28')) != 0)
156 156 {
157   - if (memcmp(p2, padding_string, len - (p2 - cstr)) == 0)
  157 + size_t idx = toS(p2 - cstr);
  158 + if (memcmp(p2, padding_string, len - idx) == 0)
158 159 {
159   - user_password = user_password.substr(0, p2 - cstr);
  160 + user_password = user_password.substr(0, idx);
160 161 return;
161 162 }
162 163 else
... ... @@ -183,7 +184,8 @@ truncate_password_V5(std::string const&amp; password)
183 184 }
184 185  
185 186 static void
186   -iterate_md5_digest(MD5& md5, MD5::Digest& digest, int iterations, int key_len)
  187 +iterate_md5_digest(MD5& md5, MD5::Digest& digest,
  188 + int iterations, int key_len)
187 189 {
188 190 md5.digest(digest);
189 191  
... ... @@ -191,26 +193,26 @@ iterate_md5_digest(MD5&amp; md5, MD5::Digest&amp; digest, int iterations, int key_len)
191 193 {
192 194 MD5 m;
193 195 m.encodeDataIncrementally(reinterpret_cast<char*>(digest),
194   - key_len);
  196 + QIntC::to_size(key_len));
195 197 m.digest(digest);
196 198 }
197 199 }
198 200  
199 201  
200 202 static void
201   -iterate_rc4(unsigned char* data, int data_len,
  203 +iterate_rc4(unsigned char* data, size_t data_len,
202 204 unsigned char* okey, int key_len,
203 205 int iterations, bool reverse)
204 206 {
205   - unsigned char* key = new unsigned char[key_len];
  207 + unsigned char* key = new unsigned char[QIntC::to_size(key_len)];
206 208 for (int i = 0; i < iterations; ++i)
207 209 {
208 210 int const xor_value = (reverse ? iterations - 1 - i : i);
209 211 for (int j = 0; j < key_len; ++j)
210 212 {
211   - key[j] = okey[j] ^ xor_value;
  213 + key[j] = static_cast<unsigned char>(okey[j] ^ xor_value);
212 214 }
213   - RC4 rc4(key, key_len);
  215 + RC4 rc4(key, QIntC::to_int(key_len));
214 216 rc4.process(data, data_len);
215 217 }
216 218 delete [] key;
... ... @@ -228,7 +230,7 @@ process_with_aes(std::string const&amp; key,
228 230 Pl_Buffer buffer("buffer");
229 231 Pl_AES_PDF aes("aes", &buffer, encrypt,
230 232 QUtil::unsigned_char_pointer(key),
231   - key.length());
  233 + QIntC::to_uint(key.length()));
232 234 if (iv)
233 235 {
234 236 aes.setIV(iv, iv_length);
... ... @@ -328,7 +330,7 @@ hash_V5(std::string const&amp; password,
328 330 {
329 331 unsigned int ch = static_cast<unsigned char>(*(E.rbegin()));
330 332  
331   - if (ch <= static_cast<unsigned int>(round_number - 32))
  333 + if (ch <= QIntC::to_uint(round_number - 32))
332 334 {
333 335 done = true;
334 336 }
... ... @@ -341,7 +343,7 @@ hash_V5(std::string const&amp; password,
341 343 }
342 344  
343 345 static
344   -void pad_short_parameter(std::string& param, unsigned int max_len)
  346 +void pad_short_parameter(std::string& param, size_t max_len)
345 347 {
346 348 if (param.length() < max_len)
347 349 {
... ... @@ -367,11 +369,11 @@ QPDF::compute_data_key(std::string const&amp; encryption_key,
367 369 }
368 370  
369 371 // Append low three bytes of object ID and low two bytes of generation
370   - result += static_cast<char>(objid & 0xff);
371   - result += static_cast<char>((objid >> 8) & 0xff);
372   - result += static_cast<char>((objid >> 16) & 0xff);
373   - result += static_cast<char>(generation & 0xff);
374   - result += static_cast<char>((generation >> 8) & 0xff);
  372 + result.append(1, static_cast<char>(objid & 0xff));
  373 + result.append(1, static_cast<char>((objid >> 8) & 0xff));
  374 + result.append(1, static_cast<char>((objid >> 16) & 0xff));
  375 + result.append(1, static_cast<char>(generation & 0xff));
  376 + result.append(1, static_cast<char>((generation >> 8) & 0xff));
375 377 if (use_aes)
376 378 {
377 379 result += "sAlT";
... ... @@ -382,7 +384,7 @@ QPDF::compute_data_key(std::string const&amp; encryption_key,
382 384 MD5::Digest digest;
383 385 md5.digest(digest);
384 386 return std::string(reinterpret_cast<char*>(digest),
385   - std::min(result.length(), static_cast<size_t>(16)));
  387 + std::min(result.length(), toS(16)));
386 388 }
387 389  
388 390 std::string
... ... @@ -437,10 +439,9 @@ QPDF::compute_encryption_key_from_password(
437 439 md5.encodeDataIncrementally(bytes, 4);
438 440 }
439 441 MD5::Digest digest;
440   - int key_len = std::min(static_cast<int>(sizeof(digest)),
441   - data.getLengthBytes());
  442 + int key_len = std::min(QIntC::to_int(sizeof(digest)), data.getLengthBytes());
442 443 iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0), key_len);
443   - return std::string(reinterpret_cast<char*>(digest), key_len);
  444 + return std::string(reinterpret_cast<char*>(digest), QIntC::to_size(key_len));
444 445 }
445 446  
446 447 static void
... ... @@ -463,7 +464,7 @@ compute_O_rc4_key(std::string const&amp; user_password,
463 464 md5.encodeDataIncrementally(
464 465 pad_or_truncate_password_V4(password).c_str(), key_bytes);
465 466 MD5::Digest digest;
466   - int key_len = std::min(static_cast<int>(sizeof(digest)),
  467 + int key_len = std::min(QIntC::to_int(sizeof(digest)),
467 468 data.getLengthBytes());
468 469 iterate_md5_digest(md5, digest, ((data.getR() >= 3) ? 50 : 0), key_len);
469 470 memcpy(key, digest, OU_key_bytes_V4);
... ... @@ -482,7 +483,7 @@ compute_O_value(std::string const&amp; user_password,
482 483 char upass[key_bytes];
483 484 pad_or_truncate_password_V4(user_password, upass);
484 485 std::string k1(reinterpret_cast<char*>(O_key), OU_key_bytes_V4);
485   - pad_short_parameter(k1, data.getLengthBytes());
  486 + pad_short_parameter(k1, QIntC::to_size(data.getLengthBytes()));
486 487 iterate_rc4(QUtil::unsigned_char_pointer(upass), key_bytes,
487 488 O_key, data.getLengthBytes(),
488 489 (data.getR() >= 3) ? 20 : 1, false);
... ... @@ -499,7 +500,7 @@ compute_U_value_R2(std::string const&amp; user_password,
499 500 std::string k1 = QPDF::compute_encryption_key(user_password, data);
500 501 char udata[key_bytes];
501 502 pad_or_truncate_password_V4("", udata);
502   - pad_short_parameter(k1, data.getLengthBytes());
  503 + pad_short_parameter(k1, QIntC::to_size(data.getLengthBytes()));
503 504 iterate_rc4(QUtil::unsigned_char_pointer(udata), key_bytes,
504 505 QUtil::unsigned_char_pointer(k1),
505 506 data.getLengthBytes(), 1, false);
... ... @@ -521,7 +522,7 @@ compute_U_value_R3(std::string const&amp; user_password,
521 522 data.getId1().length());
522 523 MD5::Digest digest;
523 524 md5.digest(digest);
524   - pad_short_parameter(k1, data.getLengthBytes());
  525 + pad_short_parameter(k1, QIntC::to_size(data.getLengthBytes()));
525 526 iterate_rc4(digest, sizeof(MD5::Digest),
526 527 QUtil::unsigned_char_pointer(k1),
527 528 data.getLengthBytes(), 20, false);
... ... @@ -555,8 +556,8 @@ check_user_password_V4(std::string const&amp; user_password,
555 556 // Algorithm 3.6 from the PDF 1.7 Reference Manual
556 557  
557 558 std::string u_value = compute_U_value(user_password, data);
558   - int to_compare = ((data.getR() >= 3) ? sizeof(MD5::Digest)
559   - : key_bytes);
  559 + size_t to_compare = ((data.getR() >= 3) ? sizeof(MD5::Digest)
  560 + : key_bytes);
560 561 return (memcmp(data.getU().c_str(), u_value.c_str(), to_compare) == 0);
561 562 }
562 563  
... ... @@ -598,7 +599,7 @@ check_owner_password_V4(std::string&amp; user_password,
598 599 unsigned char O_data[key_bytes];
599 600 memcpy(O_data, QUtil::unsigned_char_pointer(data.getO()), key_bytes);
600 601 std::string k1(reinterpret_cast<char*>(key), OU_key_bytes_V4);
601   - pad_short_parameter(k1, data.getLengthBytes());
  602 + pad_short_parameter(k1, QIntC::to_size(data.getLengthBytes()));
602 603 iterate_rc4(O_data, key_bytes, QUtil::unsigned_char_pointer(k1),
603 604 data.getLengthBytes(),
604 605 (data.getR() >= 3) ? 20 : 1, true);
... ... @@ -694,7 +695,8 @@ compute_Perms_value_V5_clear(std::string const&amp; encryption_key,
694 695 unsigned char k[16])
695 696 {
696 697 // From algorithm 3.10 from the PDF 1.7 extension level 3
697   - unsigned long long extended_perms = 0xffffffff00000000LL | data.getP();
  698 + unsigned long long extended_perms =
  699 + 0xffffffff00000000LL | static_cast<unsigned long long>(data.getP());
698 700 for (int i = 0; i < 8; ++i)
699 701 {
700 702 k[i] = static_cast<unsigned char>(extended_perms & 0xff);
... ... @@ -868,11 +870,11 @@ QPDF::initializeEncryption()
868 870 "or the wrong type");
869 871 }
870 872  
871   - int V = encryption_dict.getKey("/V").getIntValue();
872   - int R = encryption_dict.getKey("/R").getIntValue();
  873 + int V = encryption_dict.getKey("/V").getIntValueAsInt();
  874 + int R = encryption_dict.getKey("/R").getIntValueAsInt();
873 875 std::string O = encryption_dict.getKey("/O").getStringValue();
874 876 std::string U = encryption_dict.getKey("/U").getStringValue();
875   - unsigned int P = encryption_dict.getKey("/P").getIntValue();
  877 + int P = encryption_dict.getKey("/P").getIntValueAsInt();
876 878  
877 879 // If supporting new encryption R/V values, remember to update
878 880 // error message inside this if statement.
... ... @@ -935,7 +937,7 @@ QPDF::initializeEncryption()
935 937 int Length = 40;
936 938 if (encryption_dict.getKey("/Length").isInteger())
937 939 {
938   - Length = encryption_dict.getKey("/Length").getIntValue();
  940 + Length = encryption_dict.getKey("/Length").getIntValueAsInt();
939 941 if (R < 3)
940 942 {
941 943 // Force Length to 40 regardless of what the file says.
... ... @@ -1013,7 +1015,8 @@ QPDF::initializeEncryption()
1013 1015 }
1014 1016 }
1015 1017  
1016   - EncryptionData data(V, R, Length / 8, P, O, U, OE, UE, Perms,
  1018 + EncryptionData data(V, R, Length / 8,
  1019 + P, O, U, OE, UE, Perms,
1017 1020 id1, this->m->encp->encrypt_metadata);
1018 1021 if (this->m->provided_password_is_hex_key)
1019 1022 {
... ... @@ -1154,11 +1157,11 @@ QPDF::decryptString(std::string&amp; str, int objid, int generation)
1154 1157 else
1155 1158 {
1156 1159 QTC::TC("qpdf", "QPDF_encryption rc4 decode string");
1157   - unsigned int vlen = str.length();
  1160 + size_t vlen = str.length();
1158 1161 // Using PointerHolder guarantees that tmp will
1159 1162 // be freed even if rc4.process throws an exception.
1160 1163 PointerHolder<char> tmp(true, QUtil::copy_string(str));
1161   - RC4 rc4(QUtil::unsigned_char_pointer(key), key.length());
  1164 + RC4 rc4(QUtil::unsigned_char_pointer(key), toI(key.length()));
1162 1165 rc4.process(QUtil::unsigned_char_pointer(tmp.getPointer()), vlen);
1163 1166 str = std::string(tmp.getPointer(), vlen);
1164 1167 }
... ... @@ -1313,7 +1316,7 @@ QPDF::decryptStream(PointerHolder&lt;EncryptionParameters&gt; encp,
1313 1316 QTC::TC("qpdf", "QPDF_encryption rc4 decode stream");
1314 1317 pipeline = new Pl_RC4("RC4 stream decryption", pipeline,
1315 1318 QUtil::unsigned_char_pointer(key),
1316   - key.length());
  1319 + toI(key.length()));
1317 1320 }
1318 1321 heap.push_back(pipeline);
1319 1322 }
... ... @@ -1404,9 +1407,9 @@ QPDF::isEncrypted(int&amp; R, int&amp; P, int&amp; V,
1404 1407 QPDFObjectHandle Pkey = encrypt.getKey("/P");
1405 1408 QPDFObjectHandle Rkey = encrypt.getKey("/R");
1406 1409 QPDFObjectHandle Vkey = encrypt.getKey("/V");
1407   - P = Pkey.getIntValue();
1408   - R = Rkey.getIntValue();
1409   - V = Vkey.getIntValue();
  1410 + P = Pkey.getIntValueAsInt();
  1411 + R = Rkey.getIntValueAsInt();
  1412 + V = Vkey.getIntValueAsInt();
1410 1413 stream_method = this->m->encp->cf_stream;
1411 1414 string_method = this->m->encp->cf_string;
1412 1415 file_method = this->m->encp->cf_file;
... ...
libqpdf/QPDF_linearization.cc
... ... @@ -26,15 +26,15 @@ load_vector_int(BitStream&amp; bit_stream, int nitems, std::vector&lt;T&gt;&amp; vec,
26 26 // nitems times, read bits_wanted from the given bit stream,
27 27 // storing results in the ith vector entry.
28 28  
29   - for (int i = 0; i < nitems; ++i)
  29 + for (size_t i = 0; i < QIntC::to_size(nitems); ++i)
30 30 {
31 31 if (append)
32 32 {
33 33 vec.push_back(T());
34 34 }
35   - vec.at(i).*field = bit_stream.getBits(bits_wanted);
  35 + vec.at(i).*field = bit_stream.getBitsInt(QIntC::to_size(bits_wanted));
36 36 }
37   - if (static_cast<int>(vec.size()) != nitems)
  37 + if (QIntC::to_int(vec.size()) != nitems)
38 38 {
39 39 throw std::logic_error("vector has wrong size in load_vector_int");
40 40 }
... ... @@ -51,11 +51,12 @@ load_vector_vector(BitStream&amp; bit_stream,
51 51 {
52 52 // nitems1 times, read nitems2 (from the ith element of vec1) items
53 53 // into the vec2 vector field of the ith item of vec1.
54   - for (int i1 = 0; i1 < nitems1; ++i1)
  54 + for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1)
55 55 {
56 56 for (int i2 = 0; i2 < vec1.at(i1).*nitems2; ++i2)
57 57 {
58   - (vec1.at(i1).*vec2).push_back(bit_stream.getBits(bits_wanted));
  58 + (vec1.at(i1).*vec2).push_back(
  59 + bit_stream.getBitsInt(QIntC::to_size(bits_wanted)));
59 60 }
60 61 }
61 62 bit_stream.skipToNextByte();
... ... @@ -131,7 +132,7 @@ QPDF::isLinearized()
131 132 (t4.getType() == QPDFTokenizer::tt_dict_open))
132 133 {
133 134 lindict_obj =
134   - static_cast<int>(QUtil::string_to_ll(t1.getValue().c_str()));
  135 + QIntC::to_int(QUtil::string_to_ll(t1.getValue().c_str()));
135 136 }
136 137 }
137 138  
... ... @@ -149,7 +150,7 @@ QPDF::isLinearized()
149 150  
150 151 QPDFObjectHandle linkey = candidate.getKey("/Linearized");
151 152 if (! (linkey.isNumber() &&
152   - (static_cast<int>(floor(linkey.getNumericValue())) == 1)))
  153 + (QIntC::to_int(floor(linkey.getNumericValue())) == 1)))
153 154 {
154 155 return false;
155 156 }
... ... @@ -213,7 +214,7 @@ QPDF::readLinearizationData()
213 214 }
214 215  
215 216 // Hint table array: offset length [ offset length ]
216   - unsigned int n_H_items = H.getArrayNItems();
  217 + size_t n_H_items = toS(H.getArrayNItems());
217 218 if (! ((n_H_items == 2) || (n_H_items == 4)))
218 219 {
219 220 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
... ... @@ -223,12 +224,12 @@ QPDF::readLinearizationData()
223 224 }
224 225  
225 226 std::vector<int> H_items;
226   - for (unsigned int i = 0; i < n_H_items; ++i)
  227 + for (size_t i = 0; i < n_H_items; ++i)
227 228 {
228   - QPDFObjectHandle oh(H.getArrayItem(i));
  229 + QPDFObjectHandle oh(H.getArrayItem(toI(i)));
229 230 if (oh.isInteger())
230 231 {
231   - H_items.push_back(oh.getIntValue());
  232 + H_items.push_back(oh.getIntValueAsInt());
232 233 }
233 234 else
234 235 {
... ... @@ -258,7 +259,7 @@ QPDF::readLinearizationData()
258 259 if (P.isInteger())
259 260 {
260 261 QTC::TC("qpdf", "QPDF P present in lindict");
261   - first_page = P.getIntValue();
  262 + first_page = P.getIntValueAsInt();
262 263 }
263 264 else
264 265 {
... ... @@ -279,9 +280,9 @@ QPDF::readLinearizationData()
279 280 }
280 281  
281 282 // file_size initialized by isLinearized()
282   - this->m->linp.first_page_object = O.getIntValue();
  283 + this->m->linp.first_page_object = O.getIntValueAsInt();
283 284 this->m->linp.first_page_end = E.getIntValue();
284   - this->m->linp.npages = N.getIntValue();
  285 + this->m->linp.npages = N.getIntValueAsInt();
285 286 this->m->linp.xref_zero_offset = T.getIntValue();
286 287 this->m->linp.first_page = first_page;
287 288 this->m->linp.H_offset = H0_offset;
... ... @@ -290,10 +291,10 @@ QPDF::readLinearizationData()
290 291 // Read hint streams
291 292  
292 293 Pl_Buffer pb("hint buffer");
293   - QPDFObjectHandle H0 = readHintStream(pb, H0_offset, H0_length);
  294 + QPDFObjectHandle H0 = readHintStream(pb, H0_offset, toS(H0_length));
294 295 if (H1_offset)
295 296 {
296   - (void) readHintStream(pb, H1_offset, H1_length);
  297 + (void) readHintStream(pb, H1_offset, toS(H1_length));
297 298 }
298 299  
299 300 // PDF 1.4 hint tables that we ignore:
... ... @@ -313,31 +314,31 @@ QPDF::readLinearizationData()
313 314 PointerHolder<Buffer> hbp = pb.getBuffer();
314 315 Buffer* hb = hbp.getPointer();
315 316 unsigned char const* h_buf = hb->getBuffer();
316   - int h_size = hb->getSize();
  317 + size_t h_size = hb->getSize();
317 318  
318 319 readHPageOffset(BitStream(h_buf, h_size));
319 320  
320   - int HSi = HS.getIntValue();
321   - if ((HSi < 0) || (HSi >= h_size))
  321 + int HSi = HS.getIntValueAsInt();
  322 + if ((HSi < 0) || (toS(HSi) >= h_size))
322 323 {
323 324 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
324 325 "linearization hint table",
325 326 this->m->file->getLastOffset(),
326 327 "/S (shared object) offset is out of bounds");
327 328 }
328   - readHSharedObject(BitStream(h_buf + HSi, h_size - HSi));
  329 + readHSharedObject(BitStream(h_buf + HSi, h_size - toS(HSi)));
329 330  
330 331 if (HO.isInteger())
331 332 {
332   - int HOi = HO.getIntValue();
333   - if ((HOi < 0) || (HOi >= h_size))
  333 + int HOi = HO.getIntValueAsInt();
  334 + if ((HOi < 0) || (toS(HOi) >= h_size))
334 335 {
335 336 throw QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
336 337 "linearization hint table",
337 338 this->m->file->getLastOffset(),
338 339 "/O (outline) offset is out of bounds");
339 340 }
340   - readHGeneric(BitStream(h_buf + HOi, h_size - HOi),
  341 + readHGeneric(BitStream(h_buf + HOi, h_size - toS(HOi)),
341 342 this->m->outline_hints);
342 343 }
343 344 }
... ... @@ -381,7 +382,7 @@ QPDF::readHintStream(Pipeline&amp; pl, qpdf_offset_t offset, size_t length)
381 382 {
382 383 QTC::TC("qpdf", "QPDF hint table length direct");
383 384 }
384   - qpdf_offset_t computed_end = offset + length;
  385 + qpdf_offset_t computed_end = offset + toO(length);
385 386 if ((computed_end < min_end_offset) ||
386 387 (computed_end > max_end_offset))
387 388 {
... ... @@ -405,23 +406,23 @@ QPDF::readHPageOffset(BitStream h)
405 406  
406 407 HPageOffset& t = this->m->page_offset_hints;
407 408  
408   - t.min_nobjects = h.getBits(32); // 1
409   - t.first_page_offset = h.getBits(32); // 2
410   - t.nbits_delta_nobjects = h.getBits(16); // 3
411   - t.min_page_length = h.getBits(32); // 4
412   - t.nbits_delta_page_length = h.getBits(16); // 5
413   - t.min_content_offset = h.getBits(32); // 6
414   - t.nbits_delta_content_offset = h.getBits(16); // 7
415   - t.min_content_length = h.getBits(32); // 8
416   - t.nbits_delta_content_length = h.getBits(16); // 9
417   - t.nbits_nshared_objects = h.getBits(16); // 10
418   - t.nbits_shared_identifier = h.getBits(16); // 11
419   - t.nbits_shared_numerator = h.getBits(16); // 12
420   - t.shared_denominator = h.getBits(16); // 13
  409 + t.min_nobjects = h.getBitsInt(32); // 1
  410 + t.first_page_offset = h.getBitsInt(32); // 2
  411 + t.nbits_delta_nobjects = h.getBitsInt(16); // 3
  412 + t.min_page_length = h.getBitsInt(32); // 4
  413 + t.nbits_delta_page_length = h.getBitsInt(16); // 5
  414 + t.min_content_offset = h.getBitsInt(32); // 6
  415 + t.nbits_delta_content_offset = h.getBitsInt(16); // 7
  416 + t.min_content_length = h.getBitsInt(32); // 8
  417 + t.nbits_delta_content_length = h.getBitsInt(16); // 9
  418 + t.nbits_nshared_objects = h.getBitsInt(16); // 10
  419 + t.nbits_shared_identifier = h.getBitsInt(16); // 11
  420 + t.nbits_shared_numerator = h.getBitsInt(16); // 12
  421 + t.shared_denominator = h.getBitsInt(16); // 13
421 422  
422 423 std::vector<HPageOffsetEntry>& entries = t.entries;
423 424 entries.clear();
424   - unsigned int nitems = this->m->linp.npages;
  425 + int nitems = this->m->linp.npages;
425 426 load_vector_int(h, nitems, entries,
426 427 t.nbits_delta_nobjects,
427 428 &HPageOffsetEntry::delta_nobjects);
... ... @@ -452,13 +453,13 @@ QPDF::readHSharedObject(BitStream h)
452 453 {
453 454 HSharedObject& t = this->m->shared_object_hints;
454 455  
455   - t.first_shared_obj = h.getBits(32); // 1
456   - t.first_shared_offset = h.getBits(32); // 2
457   - t.nshared_first_page = h.getBits(32); // 3
458   - t.nshared_total = h.getBits(32); // 4
459   - t.nbits_nobjects = h.getBits(16); // 5
460   - t.min_group_length = h.getBits(32); // 6
461   - t.nbits_delta_group_length = h.getBits(16); // 7
  456 + t.first_shared_obj = h.getBitsInt(32); // 1
  457 + t.first_shared_offset = h.getBitsInt(32); // 2
  458 + t.nshared_first_page = h.getBitsInt(32); // 3
  459 + t.nshared_total = h.getBitsInt(32); // 4
  460 + t.nbits_nobjects = h.getBitsInt(16); // 5
  461 + t.min_group_length = h.getBitsInt(32); // 6
  462 + t.nbits_delta_group_length = h.getBitsInt(16); // 7
462 463  
463 464 QTC::TC("qpdf", "QPDF lin nshared_total > nshared_first_page",
464 465 (t.nshared_total > t.nshared_first_page) ? 1 : 0);
... ... @@ -471,7 +472,7 @@ QPDF::readHSharedObject(BitStream h)
471 472 &HSharedObjectEntry::delta_group_length);
472 473 load_vector_int(h, nitems, entries,
473 474 1, &HSharedObjectEntry::signature_present);
474   - for (int i = 0; i < nitems; ++i)
  475 + for (size_t i = 0; i < toS(nitems); ++i)
475 476 {
476 477 if (entries.at(i).signature_present)
477 478 {
... ... @@ -492,10 +493,10 @@ QPDF::readHSharedObject(BitStream h)
492 493 void
493 494 QPDF::readHGeneric(BitStream h, HGeneric& t)
494 495 {
495   - t.first_object = h.getBits(32); // 1
496   - t.first_object_offset = h.getBits(32); // 2
497   - t.nobjects = h.getBits(32); // 3
498   - t.group_length = h.getBits(32); // 4
  496 + t.first_object = h.getBitsInt(32); // 1
  497 + t.first_object_offset = h.getBitsInt(32); // 2
  498 + t.nobjects = h.getBitsInt(32); // 3
  499 + t.group_length = h.getBitsInt(32); // 4
499 500 }
500 501  
501 502 bool
... ... @@ -522,21 +523,21 @@ QPDF::checkLinearizationInternal()
522 523 }
523 524  
524 525 // N: number of pages
525   - int npages = pages.size();
  526 + int npages = toI(pages.size());
526 527 if (p.npages != npages)
527 528 {
528 529 // Not tested in the test suite
529 530 errors.push_back("page count (/N) mismatch");
530 531 }
531 532  
532   - for (int i = 0; i < npages; ++i)
  533 + for (size_t i = 0; i < toS(npages); ++i)
533 534 {
534 535 QPDFObjectHandle const& page = pages.at(i);
535 536 QPDFObjGen og(page.getObjGen());
536 537 if (this->m->xref_table[og].getType() == 2)
537 538 {
538 539 errors.push_back("page dictionary for page " +
539   - QUtil::int_to_string(i) + " is compressed");
  540 + QUtil::uint_to_string(i) + " is compressed");
540 541 }
541 542 }
542 543  
... ... @@ -756,8 +757,8 @@ QPDF::lengthNextN(int first_object, int n,
756 757 stopOnError("found unknown object while"
757 758 " calculating length for linearization data");
758 759 }
759   - length += this->m->obj_cache[og].end_after_space -
760   - getLinearizationOffset(og);
  760 + length += toI(this->m->obj_cache[og].end_after_space -
  761 + getLinearizationOffset(og));
761 762 }
762 763 }
763 764 return length;
... ... @@ -786,8 +787,8 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
786 787 // under a page's /Resources dictionary in with shared objects
787 788 // even when they are private.
788 789  
789   - unsigned int npages = pages.size();
790   - int table_offset = adjusted_offset(
  790 + int npages = toI(pages.size());
  791 + qpdf_offset_t table_offset = adjusted_offset(
791 792 this->m->page_offset_hints.first_page_offset);
792 793 QPDFObjGen first_page_og(pages.at(0).getObjGen());
793 794 if (this->m->xref_table.count(first_page_og) == 0)
... ... @@ -800,9 +801,9 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
800 801 warnings.push_back("first page object offset mismatch");
801 802 }
802 803  
803   - for (unsigned int pageno = 0; pageno < npages; ++pageno)
  804 + for (int pageno = 0; pageno < npages; ++pageno)
804 805 {
805   - QPDFObjGen page_og(pages.at(pageno).getObjGen());
  806 + QPDFObjGen page_og(pages.at(toS(pageno)).getObjGen());
806 807 int first_object = page_og.getObj();
807 808 if (this->m->xref_table.count(page_og) == 0)
808 809 {
... ... @@ -810,8 +811,10 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
810 811 }
811 812 offset = getLinearizationOffset(page_og);
812 813  
813   - HPageOffsetEntry& he = this->m->page_offset_hints.entries.at(pageno);
814   - CHPageOffsetEntry& ce = this->m->c_page_offset_data.entries.at(pageno);
  814 + HPageOffsetEntry& he =
  815 + this->m->page_offset_hints.entries.at(toS(pageno));
  816 + CHPageOffsetEntry& ce =
  817 + this->m->c_page_offset_data.entries.at(toS(pageno));
815 818 int h_nobjects = he.delta_nobjects +
816 819 this->m->page_offset_hints.min_nobjects;
817 820 if (h_nobjects != ce.nobjects)
... ... @@ -827,8 +830,8 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
827 830 // Use value for number of objects in hint table rather than
828 831 // computed value if there is a discrepancy.
829 832 int length = lengthNextN(first_object, h_nobjects, errors);
830   - int h_length = he.delta_page_length +
831   - this->m->page_offset_hints.min_page_length;
  833 + int h_length = toI(he.delta_page_length +
  834 + this->m->page_offset_hints.min_page_length);
832 835 if (length != h_length)
833 836 {
834 837 // This condition almost certainly indicates a bad hint
... ... @@ -854,7 +857,7 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
854 857 warnings.push_back("page 0 has shared identifier entries");
855 858 }
856 859  
857   - for (int i = 0; i < he.nshared_objects; ++i)
  860 + for (size_t i = 0; i < toS(he.nshared_objects); ++i)
858 861 {
859 862 int idx = he.shared_identifiers.at(i);
860 863 if (shared_idx_to_obj.count(idx) == 0)
... ... @@ -866,7 +869,7 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
866 869 hint_shared.insert(shared_idx_to_obj[idx]);
867 870 }
868 871  
869   - for (int i = 0; i < ce.nshared_objects; ++i)
  872 + for (size_t i = 0; i < toS(ce.nshared_objects); ++i)
870 873 {
871 874 int idx = ce.shared_identifiers.at(i);
872 875 if (idx >= this->m->c_shared_object_data.nshared_total)
... ... @@ -874,7 +877,7 @@ QPDF::checkHPageOffset(std::list&lt;std::string&gt;&amp; errors,
874 877 throw std::logic_error(
875 878 "index out of bounds for shared object hint table");
876 879 }
877   - int obj = this->m->c_shared_object_data.entries.at(idx).object;
  880 + int obj = this->m->c_shared_object_data.entries.at(toS(idx)).object;
878 881 computed_shared.insert(obj);
879 882 }
880 883  
... ... @@ -975,8 +978,9 @@ QPDF::checkHSharedObject(std::list&lt;std::string&gt;&amp; errors,
975 978 {
976 979 stopOnError("unknown object in shared object hint table");
977 980 }
978   - int offset = getLinearizationOffset(og);
979   - int h_offset = adjusted_offset(so.first_shared_offset);
  981 + qpdf_offset_t offset = getLinearizationOffset(og);
  982 + qpdf_offset_t h_offset =
  983 + adjusted_offset(so.first_shared_offset);
980 984 if (offset != h_offset)
981 985 {
982 986 errors.push_back(
... ... @@ -987,7 +991,7 @@ QPDF::checkHSharedObject(std::list&lt;std::string&gt;&amp; errors,
987 991 }
988 992  
989 993 idx_to_obj[i] = cur_object;
990   - HSharedObjectEntry& se = so.entries.at(i);
  994 + HSharedObjectEntry& se = so.entries.at(toS(i));
991 995 int nobjects = se.nobjects_minus_one + 1;
992 996 int length = lengthNextN(cur_object, nobjects, errors);
993 997 int h_length = so.min_group_length + se.delta_group_length;
... ... @@ -1041,10 +1045,10 @@ QPDF::checkHOutlines(std::list&lt;std::string&gt;&amp; warnings)
1041 1045 {
1042 1046 stopOnError("unknown object in outlines hint table");
1043 1047 }
1044   - int offset = getLinearizationOffset(og);
  1048 + qpdf_offset_t offset = getLinearizationOffset(og);
1045 1049 ObjUser ou(ObjUser::ou_root_key, "/Outlines");
1046   - int length = maxEnd(ou) - offset;
1047   - int table_offset =
  1050 + int length = toI(maxEnd(ou) - offset);
  1051 + qpdf_offset_t table_offset =
1048 1052 adjusted_offset(this->m->outline_hints.first_object_offset);
1049 1053 if (offset != table_offset)
1050 1054 {
... ... @@ -1124,8 +1128,8 @@ QPDF::dumpLinearizationDataInternal()
1124 1128 }
1125 1129 }
1126 1130  
1127   -int
1128   -QPDF::adjusted_offset(int offset)
  1131 +qpdf_offset_t
  1132 +QPDF::adjusted_offset(qpdf_offset_t offset)
1129 1133 {
1130 1134 // All offsets >= H_offset have to be increased by H_length
1131 1135 // since all hint table location values disregard the hint table
... ... @@ -1170,7 +1174,7 @@ QPDF::dumpHPageOffset()
1170 1174 << "shared_denominator: " << t.shared_denominator
1171 1175 << std::endl;
1172 1176  
1173   - for (int i1 = 0; i1 < this->m->linp.npages; ++i1)
  1177 + for (size_t i1 = 0; i1 < toS(this->m->linp.npages); ++i1)
1174 1178 {
1175 1179 HPageOffsetEntry& pe = t.entries.at(i1);
1176 1180 *this->m->out_stream
... ... @@ -1185,7 +1189,7 @@ QPDF::dumpHPageOffset()
1185 1189 << " content_length: "
1186 1190 << pe.delta_content_length + t.min_content_length << std::endl
1187 1191 << " nshared_objects: " << pe.nshared_objects << std::endl;
1188   - for (int i2 = 0; i2 < pe.nshared_objects; ++i2)
  1192 + for (size_t i2 = 0; i2 < toS(pe.nshared_objects); ++i2)
1189 1193 {
1190 1194 *this->m->out_stream << " identifier " << i2 << ": "
1191 1195 << pe.shared_identifiers.at(i2) << std::endl;
... ... @@ -1215,7 +1219,7 @@ QPDF::dumpHSharedObject()
1215 1219 << "nbits_delta_group_length: " << t.nbits_delta_group_length
1216 1220 << std::endl;
1217 1221  
1218   - for (int i = 0; i < t.nshared_total; ++i)
  1222 + for (size_t i = 0; i < toS(t.nshared_total); ++i)
1219 1223 {
1220 1224 HSharedObjectEntry& se = t.entries.at(i);
1221 1225 *this->m->out_stream
... ... @@ -1516,7 +1520,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1516 1520 pages.push_back(getUncompressedObject(*iter, object_stream_data));
1517 1521 }
1518 1522 }
1519   - unsigned int npages = pages.size();
  1523 + int npages = toI(pages.size());
1520 1524  
1521 1525 // We will be initializing some values of the computed hint
1522 1526 // tables. Specifically, we can initialize any items that deal
... ... @@ -1531,7 +1535,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1531 1535 // reasonable size.
1532 1536 this->m->c_linp.npages = npages;
1533 1537 this->m->c_page_offset_data.entries =
1534   - std::vector<CHPageOffsetEntry>(npages);
  1538 + std::vector<CHPageOffsetEntry>(toS(npages));
1535 1539  
1536 1540 // Part 4: open document objects. We don't care about the order.
1537 1541  
... ... @@ -1594,12 +1598,13 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1594 1598 // in garbage values for all the shared object identifiers on the
1595 1599 // first page.
1596 1600  
1597   - this->m->c_page_offset_data.entries.at(0).nobjects = this->m->part6.size();
  1601 + this->m->c_page_offset_data.entries.at(0).nobjects =
  1602 + toI(this->m->part6.size());
1598 1603  
1599 1604 // Part 7: other pages' private objects
1600 1605  
1601 1606 // For each page in order:
1602   - for (unsigned int i = 1; i < npages; ++i)
  1607 + for (size_t i = 1; i < toS(npages); ++i)
1603 1608 {
1604 1609 // Place this page's page object
1605 1610  
... ... @@ -1609,7 +1614,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1609 1614 throw std::logic_error(
1610 1615 "INTERNAL ERROR: "
1611 1616 "QPDF::calculateLinearizationData: page object for page " +
1612   - QUtil::int_to_string(i) + " not in lc_other_page_private");
  1617 + QUtil::uint_to_string(i) + " not in lc_other_page_private");
1613 1618 }
1614 1619 lc_other_page_private.erase(page_og);
1615 1620 this->m->part7.push_back(pages.at(i));
... ... @@ -1619,7 +1624,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1619 1624  
1620 1625 this->m->c_page_offset_data.entries.at(i).nobjects = 1;
1621 1626  
1622   - ObjUser ou(ObjUser::ou_page, i);
  1627 + ObjUser ou(ObjUser::ou_page, toI(i));
1623 1628 if (this->m->obj_user_to_objects.count(ou) == 0)
1624 1629 {
1625 1630 stopOnError("found unreferenced page while"
... ... @@ -1687,7 +1692,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1687 1692 // Place private thumbnail images in page order. Slightly more
1688 1693 // information would be required if we were going to bother with
1689 1694 // thumbnail hint tables.
1690   - for (unsigned int i = 0; i < npages; ++i)
  1695 + for (size_t i = 0; i < toS(npages); ++i)
1691 1696 {
1692 1697 QPDFObjectHandle thumb = pages.at(i).getKey("/Thumb");
1693 1698 thumb = getUncompressedObject(thumb, object_stream_data);
... ... @@ -1710,7 +1715,8 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1710 1715 // lc_thumbnail_private.
1711 1716 }
1712 1717 std::set<QPDFObjGen>& ogs =
1713   - this->m->obj_user_to_objects[ObjUser(ObjUser::ou_thumb, i)];
  1718 + this->m->obj_user_to_objects[
  1719 + ObjUser(ObjUser::ou_thumb, toI(i))];
1714 1720 for (std::set<QPDFObjGen>::iterator iter = ogs.begin();
1715 1721 iter != ogs.end(); ++iter)
1716 1722 {
... ... @@ -1753,18 +1759,18 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1753 1759  
1754 1760 // Make sure we got everything exactly once.
1755 1761  
1756   - unsigned int num_placed =
  1762 + size_t num_placed =
1757 1763 this->m->part4.size() + this->m->part6.size() + this->m->part7.size() +
1758 1764 this->m->part8.size() + this->m->part9.size();
1759   - unsigned int num_wanted = this->m->object_to_obj_users.size();
  1765 + size_t num_wanted = this->m->object_to_obj_users.size();
1760 1766 if (num_placed != num_wanted)
1761 1767 {
1762 1768 throw std::logic_error(
1763 1769 "INTERNAL ERROR: QPDF::calculateLinearizationData: wrong "
1764 1770 "number of objects placed (num_placed = " +
1765   - QUtil::int_to_string(num_placed) +
  1771 + QUtil::uint_to_string(num_placed) +
1766 1772 "; number of objects: " +
1767   - QUtil::int_to_string(num_wanted));
  1773 + QUtil::uint_to_string(num_wanted));
1768 1774 }
1769 1775  
1770 1776 // Calculate shared object hint table information including
... ... @@ -1780,10 +1786,11 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1780 1786 // can map from object number only without regards to generation.
1781 1787 std::map<int, int> obj_to_index;
1782 1788  
1783   - this->m->c_shared_object_data.nshared_first_page = this->m->part6.size();
  1789 + this->m->c_shared_object_data.nshared_first_page =
  1790 + toI(this->m->part6.size());
1784 1791 this->m->c_shared_object_data.nshared_total =
1785 1792 this->m->c_shared_object_data.nshared_first_page +
1786   - this->m->part8.size();
  1793 + toI(this->m->part8.size());
1787 1794  
1788 1795 std::vector<CHSharedObjectEntry>& shared =
1789 1796 this->m->c_shared_object_data.entries;
... ... @@ -1792,7 +1799,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1792 1799 {
1793 1800 QPDFObjectHandle& oh = *iter;
1794 1801 int obj = oh.getObjectID();
1795   - obj_to_index[obj] = shared.size();
  1802 + obj_to_index[obj] = toI(shared.size());
1796 1803 shared.push_back(CHSharedObjectEntry(obj));
1797 1804 }
1798 1805 QTC::TC("qpdf", "QPDF lin part 8 empty", this->m->part8.empty() ? 1 : 0);
... ... @@ -1806,7 +1813,7 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1806 1813 {
1807 1814 QPDFObjectHandle& oh = *iter;
1808 1815 int obj = oh.getObjectID();
1809   - obj_to_index[obj] = shared.size();
  1816 + obj_to_index[obj] = toI(shared.size());
1810 1817 shared.push_back(CHSharedObjectEntry(obj));
1811 1818 }
1812 1819 }
... ... @@ -1820,10 +1827,10 @@ QPDF::calculateLinearizationData(std::map&lt;int, int&gt; const&amp; object_stream_data)
1820 1827 // Now compute the list of shared objects for each page after the
1821 1828 // first page.
1822 1829  
1823   - for (unsigned int i = 1; i < npages; ++i)
  1830 + for (size_t i = 1; i < toS(npages); ++i)
1824 1831 {
1825 1832 CHPageOffsetEntry& pe = this->m->c_page_offset_data.entries.at(i);
1826   - ObjUser ou(ObjUser::ou_page, i);
  1833 + ObjUser ou(ObjUser::ou_page, toI(i));
1827 1834 if (this->m->obj_user_to_objects.count(ou) == 0)
1828 1835 {
1829 1836 stopOnError("found unreferenced page while"
... ... @@ -1921,7 +1928,7 @@ QPDF::outputLengthNextN(
1921 1928 stopOnError("found item with unknown length"
1922 1929 " while writing linearization data");
1923 1930 }
1924   - length += (*(lengths.find(first + i))).second;
  1931 + length += toI((*(lengths.find(first + toI(i)))).second);
1925 1932 }
1926 1933 return length;
1927 1934 }
... ... @@ -1938,7 +1945,7 @@ QPDF::calculateHPageOffset(
1938 1945 // values.
1939 1946  
1940 1947 std::vector<QPDFObjectHandle> const& pages = getAllPages();
1941   - unsigned int npages = pages.size();
  1948 + size_t npages = pages.size();
1942 1949 CHPageOffset& cph = this->m->c_page_offset_data;
1943 1950 std::vector<CHPageOffsetEntry>& cphe = cph.entries;
1944 1951  
... ... @@ -2001,7 +2008,7 @@ QPDF::calculateHPageOffset(
2001 2008 ph.nbits_delta_content_length = ph.nbits_delta_page_length;
2002 2009 ph.min_content_length = ph.min_page_length;
2003 2010  
2004   - for (unsigned int i = 0; i < npages; ++i)
  2011 + for (size_t i = 0; i < npages; ++i)
2005 2012 {
2006 2013 // Adjust delta entries
2007 2014 if ((phe.at(i).delta_nobjects < min_nobjects) ||
... ... @@ -2014,7 +2021,7 @@ QPDF::calculateHPageOffset(
2014 2021 phe.at(i).delta_page_length -= min_length;
2015 2022 phe.at(i).delta_content_length = phe.at(i).delta_page_length;
2016 2023  
2017   - for (int j = 0; j < cphe.at(i).nshared_objects; ++j)
  2024 + for (size_t j = 0; j < toS(cphe.at(i).nshared_objects); ++j)
2018 2025 {
2019 2026 phe.at(i).shared_identifiers.push_back(
2020 2027 cphe.at(i).shared_identifiers.at(j));
... ... @@ -2039,7 +2046,7 @@ QPDF::calculateHSharedObject(
2039 2046 csoe.at(0).object, 1, lengths, obj_renumber);
2040 2047 int max_length = min_length;
2041 2048  
2042   - for (int i = 0; i < cso.nshared_total; ++i)
  2049 + for (size_t i = 0; i < toS(cso.nshared_total); ++i)
2043 2050 {
2044 2051 // Assign absolute numbers to deltas; adjust later
2045 2052 int length = outputLengthNextN(
... ... @@ -2066,7 +2073,7 @@ QPDF::calculateHSharedObject(
2066 2073 so.min_group_length = min_length;
2067 2074 so.nbits_delta_group_length = nbits(max_length - min_length);
2068 2075  
2069   - for (int i = 0; i < cso.nshared_total; ++i)
  2076 + for (size_t i = 0; i < toS(cso.nshared_total); ++i)
2070 2077 {
2071 2078 // Adjust deltas
2072 2079 if (soe.at(i).delta_group_length < min_length)
... ... @@ -2110,9 +2117,10 @@ write_vector_int(BitWriter&amp; w, int nitems, std::vector&lt;T&gt;&amp; vec,
2110 2117 // nitems times, write bits bits from the given field of the ith
2111 2118 // vector to the given bit writer.
2112 2119  
2113   - for (int i = 0; i < nitems; ++i)
  2120 + for (size_t i = 0; i < QIntC::to_size(nitems); ++i)
2114 2121 {
2115   - w.writeBits(vec.at(i).*field, bits);
  2122 + w.writeBits(QIntC::to_ulonglong(vec.at(i).*field),
  2123 + QIntC::to_size(bits));
2116 2124 }
2117 2125 // The PDF spec says that each hint table starts at a byte
2118 2126 // boundary. Each "row" actually must start on a byte boundary.
... ... @@ -2127,11 +2135,12 @@ write_vector_vector(BitWriter&amp; w,
2127 2135 {
2128 2136 // nitems1 times, write nitems2 (from the ith element of vec1) items
2129 2137 // from the vec2 vector field of the ith item of vec1.
2130   - for (int i1 = 0; i1 < nitems1; ++i1)
  2138 + for (size_t i1 = 0; i1 < QIntC::to_size(nitems1); ++i1)
2131 2139 {
2132   - for (int i2 = 0; i2 < vec1.at(i1).*nitems2; ++i2)
  2140 + for (size_t i2 = 0; i2 < QIntC::to_size(vec1.at(i1).*nitems2); ++i2)
2133 2141 {
2134   - w.writeBits((vec1.at(i1).*vec2).at(i2), bits);
  2142 + w.writeBits(QIntC::to_ulonglong((vec1.at(i1).*vec2).at(i2)),
  2143 + QIntC::to_size(bits));
2135 2144 }
2136 2145 }
2137 2146 w.flush();
... ... @@ -2143,21 +2152,21 @@ QPDF::writeHPageOffset(BitWriter&amp; w)
2143 2152 {
2144 2153 HPageOffset& t = this->m->page_offset_hints;
2145 2154  
2146   - w.writeBits(t.min_nobjects, 32); // 1
2147   - w.writeBits(t.first_page_offset, 32); // 2
2148   - w.writeBits(t.nbits_delta_nobjects, 16); // 3
2149   - w.writeBits(t.min_page_length, 32); // 4
2150   - w.writeBits(t.nbits_delta_page_length, 16); // 5
2151   - w.writeBits(t.min_content_offset, 32); // 6
2152   - w.writeBits(t.nbits_delta_content_offset, 16); // 7
2153   - w.writeBits(t.min_content_length, 32); // 8
2154   - w.writeBits(t.nbits_delta_content_length, 16); // 9
2155   - w.writeBits(t.nbits_nshared_objects, 16); // 10
2156   - w.writeBits(t.nbits_shared_identifier, 16); // 11
2157   - w.writeBits(t.nbits_shared_numerator, 16); // 12
2158   - w.writeBits(t.shared_denominator, 16); // 13
2159   -
2160   - unsigned int nitems = getAllPages().size();
  2155 + w.writeBitsInt(t.min_nobjects, 32); // 1
  2156 + w.writeBitsInt(toI(t.first_page_offset), 32); // 2
  2157 + w.writeBitsInt(t.nbits_delta_nobjects, 16); // 3
  2158 + w.writeBitsInt(t.min_page_length, 32); // 4
  2159 + w.writeBitsInt(t.nbits_delta_page_length, 16); // 5
  2160 + w.writeBitsInt(t.min_content_offset, 32); // 6
  2161 + w.writeBitsInt(t.nbits_delta_content_offset, 16); // 7
  2162 + w.writeBitsInt(t.min_content_length, 32); // 8
  2163 + w.writeBitsInt(t.nbits_delta_content_length, 16); // 9
  2164 + w.writeBitsInt(t.nbits_nshared_objects, 16); // 10
  2165 + w.writeBitsInt(t.nbits_shared_identifier, 16); // 11
  2166 + w.writeBitsInt(t.nbits_shared_numerator, 16); // 12
  2167 + w.writeBitsInt(t.shared_denominator, 16); // 13
  2168 +
  2169 + int nitems = toI(getAllPages().size());
2161 2170 std::vector<HPageOffsetEntry>& entries = t.entries;
2162 2171  
2163 2172 write_vector_int(w, nitems, entries,
... ... @@ -2190,13 +2199,13 @@ QPDF::writeHSharedObject(BitWriter&amp; w)
2190 2199 {
2191 2200 HSharedObject& t = this->m->shared_object_hints;
2192 2201  
2193   - w.writeBits(t.first_shared_obj, 32); // 1
2194   - w.writeBits(t.first_shared_offset, 32); // 2
2195   - w.writeBits(t.nshared_first_page, 32); // 3
2196   - w.writeBits(t.nshared_total, 32); // 4
2197   - w.writeBits(t.nbits_nobjects, 16); // 5
2198   - w.writeBits(t.min_group_length, 32); // 6
2199   - w.writeBits(t.nbits_delta_group_length, 16); // 7
  2202 + w.writeBitsInt(t.first_shared_obj, 32); // 1
  2203 + w.writeBitsInt(toI(t.first_shared_offset), 32); // 2
  2204 + w.writeBitsInt(t.nshared_first_page, 32); // 3
  2205 + w.writeBitsInt(t.nshared_total, 32); // 4
  2206 + w.writeBitsInt(t.nbits_nobjects, 16); // 5
  2207 + w.writeBitsInt(t.min_group_length, 32); // 6
  2208 + w.writeBitsInt(t.nbits_delta_group_length, 16); // 7
2200 2209  
2201 2210 QTC::TC("qpdf", "QPDF lin write nshared_total > nshared_first_page",
2202 2211 (t.nshared_total > t.nshared_first_page) ? 1 : 0);
... ... @@ -2209,7 +2218,7 @@ QPDF::writeHSharedObject(BitWriter&amp; w)
2209 2218 &HSharedObjectEntry::delta_group_length);
2210 2219 write_vector_int(w, nitems, entries,
2211 2220 1, &HSharedObjectEntry::signature_present);
2212   - for (int i = 0; i < nitems; ++i)
  2221 + for (size_t i = 0; i < toS(nitems); ++i)
2213 2222 {
2214 2223 // If signature were present, we'd have to write a 128-bit hash.
2215 2224 if (entries.at(i).signature_present != 0)
... ... @@ -2226,10 +2235,10 @@ QPDF::writeHSharedObject(BitWriter&amp; w)
2226 2235 void
2227 2236 QPDF::writeHGeneric(BitWriter& w, HGeneric& t)
2228 2237 {
2229   - w.writeBits(t.first_object, 32); // 1
2230   - w.writeBits(t.first_object_offset, 32); // 2
2231   - w.writeBits(t.nobjects, 32); // 3
2232   - w.writeBits(t.group_length, 32); // 4
  2238 + w.writeBitsInt(t.first_object, 32); // 1
  2239 + w.writeBitsInt(toI(t.first_object_offset), 32); // 2
  2240 + w.writeBitsInt(t.nobjects, 32); // 3
  2241 + w.writeBitsInt(t.group_length, 32); // 4
2233 2242 }
2234 2243  
2235 2244 void
... ... @@ -2252,12 +2261,12 @@ QPDF::generateHintStream(std::map&lt;int, QPDFXRefEntry&gt; const&amp; xref,
2252 2261 BitWriter w(&c);
2253 2262  
2254 2263 writeHPageOffset(w);
2255   - S = c.getCount();
  2264 + S = toI(c.getCount());
2256 2265 writeHSharedObject(w);
2257 2266 O = 0;
2258 2267 if (this->m->outline_hints.nobjects > 0)
2259 2268 {
2260   - O = c.getCount();
  2269 + O = toI(c.getCount());
2261 2270 writeHGeneric(w, this->m->outline_hints);
2262 2271 }
2263 2272 c.finish();
... ...
libqpdf/QPDF_optimization.cc
... ... @@ -87,11 +87,11 @@ QPDF::optimize(std::map&lt;int, int&gt; const&amp; object_stream_data,
87 87 pushInheritedAttributesToPage(allow_changes, false);
88 88  
89 89 // Traverse pages
90   - int n = this->m->all_pages.size();
  90 + int n = toI(this->m->all_pages.size());
91 91 for (int pageno = 0; pageno < n; ++pageno)
92 92 {
93 93 updateObjectMaps(ObjUser(ObjUser::ou_page, pageno),
94   - this->m->all_pages.at(pageno));
  94 + this->m->all_pages.at(toS(pageno)));
95 95 }
96 96  
97 97 // Traverse document-level items
... ...
libqpdf/QPDF_pages.cc
... ... @@ -154,17 +154,17 @@ QPDF::flattenPagesTree()
154 154  
155 155 QPDFObjectHandle pages = getRoot().getKey("/Pages");
156 156  
157   - int const len = this->m->all_pages.size();
158   - for (int pos = 0; pos < len; ++pos)
  157 + size_t const len = this->m->all_pages.size();
  158 + for (size_t pos = 0; pos < len; ++pos)
159 159 {
160 160 // populate pageobj_to_pages_pos and fix parent pointer
161   - insertPageobjToPage(this->m->all_pages.at(pos), pos, true);
  161 + insertPageobjToPage(this->m->all_pages.at(pos), toI(pos), true);
162 162 this->m->all_pages.at(pos).replaceKey("/Parent", pages);
163 163 }
164 164  
165 165 pages.replaceKey("/Kids", QPDFObjectHandle::newArray(this->m->all_pages));
166 166 // /Count has not changed
167   - if (pages.getKey("/Count").getIntValue() != len)
  167 + if (pages.getKey("/Count").getUIntValue() != len)
168 168 {
169 169 throw std::logic_error("/Count is wrong after flattening pages tree");
170 170 }
... ... @@ -222,26 +222,25 @@ QPDF::insertPage(QPDFObjectHandle newpage, int pos)
222 222  
223 223 QTC::TC("qpdf", "QPDF insert page",
224 224 (pos == 0) ? 0 : // insert at beginning
225   - (pos == static_cast<int>(this->m->all_pages.size())) ? 1 : // at end
  225 + (pos == QIntC::to_int(this->m->all_pages.size())) ? 1 : // at end
226 226 2); // insert in middle
227 227  
228 228 QPDFObjectHandle pages = getRoot().getKey("/Pages");
229 229 QPDFObjectHandle kids = pages.getKey("/Kids");
230   - assert ((pos >= 0) &&
231   - (static_cast<size_t>(pos) <= this->m->all_pages.size()));
  230 + assert ((pos >= 0) && (QIntC::to_size(pos) <= this->m->all_pages.size()));
232 231  
233 232 newpage.replaceKey("/Parent", pages);
234 233 kids.insertItem(pos, newpage);
235 234 int npages = kids.getArrayNItems();
236 235 pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
237 236 this->m->all_pages.insert(this->m->all_pages.begin() + pos, newpage);
238   - assert(this->m->all_pages.size() == static_cast<size_t>(npages));
  237 + assert(this->m->all_pages.size() == QIntC::to_size(npages));
239 238 for (int i = pos + 1; i < npages; ++i)
240 239 {
241   - insertPageobjToPage(this->m->all_pages.at(i), i, false);
  240 + insertPageobjToPage(this->m->all_pages.at(toS(i)), i, false);
242 241 }
243 242 insertPageobjToPage(newpage, pos, true);
244   - assert(this->m->pageobj_to_pages_pos.size() == static_cast<size_t>(npages));
  243 + assert(this->m->pageobj_to_pages_pos.size() == QIntC::to_size(npages));
245 244 }
246 245  
247 246 void
... ... @@ -250,8 +249,7 @@ QPDF::removePage(QPDFObjectHandle page)
250 249 int pos = findPage(page); // also ensures flat /Pages
251 250 QTC::TC("qpdf", "QPDF remove page",
252 251 (pos == 0) ? 0 : // remove at beginning
253   - (pos == static_cast<int>(
254   - this->m->all_pages.size() - 1)) ? 1 : // end
  252 + (pos == QIntC::to_int(this->m->all_pages.size() - 1)) ? 1 : // end
255 253 2); // remove in middle
256 254  
257 255 QPDFObjectHandle pages = getRoot().getKey("/Pages");
... ... @@ -261,12 +259,12 @@ QPDF::removePage(QPDFObjectHandle page)
261 259 int npages = kids.getArrayNItems();
262 260 pages.replaceKey("/Count", QPDFObjectHandle::newInteger(npages));
263 261 this->m->all_pages.erase(this->m->all_pages.begin() + pos);
264   - assert(this->m->all_pages.size() == static_cast<size_t>(npages));
  262 + assert(this->m->all_pages.size() == QIntC::to_size(npages));
265 263 this->m->pageobj_to_pages_pos.erase(page.getObjGen());
266   - assert(this->m->pageobj_to_pages_pos.size() == static_cast<size_t>(npages));
  264 + assert(this->m->pageobj_to_pages_pos.size() == QIntC::to_size(npages));
267 265 for (int i = pos; i < npages; ++i)
268 266 {
269   - insertPageobjToPage(this->m->all_pages.at(i), i, false);
  267 + insertPageobjToPage(this->m->all_pages.at(toS(i)), i, false);
270 268 }
271 269 }
272 270  
... ... @@ -291,8 +289,9 @@ QPDF::addPage(QPDFObjectHandle newpage, bool first)
291 289 }
292 290 else
293 291 {
294   - insertPage(newpage,
295   - getRoot().getKey("/Pages").getKey("/Count").getIntValue());
  292 + insertPage(
  293 + newpage,
  294 + getRoot().getKey("/Pages").getKey("/Count").getIntValueAsInt());
296 295 }
297 296 }
298 297  
... ...
libqpdf/QUtil.cc
... ... @@ -251,15 +251,15 @@ int_to_string_base_internal(T num, int base, int length)
251 251 std::ostringstream buf;
252 252 buf << std::setbase(base) << std::nouppercase << num;
253 253 std::string result;
254   - if ((length > 0) &&
255   - (buf.str().length() < QIntC::to_size(length)))
  254 + int str_length = QIntC::to_int(buf.str().length());
  255 + if ((length > 0) && (str_length < length))
256 256 {
257   - result.append(length - buf.str().length(), '0');
  257 + result.append(QIntC::to_size(length - str_length), '0');
258 258 }
259 259 result += buf.str();
260   - if ((length < 0) && (buf.str().length() < QIntC::to_size(-length)))
  260 + if ((length < 0) && (str_length < -length))
261 261 {
262   - result.append(-length - buf.str().length(), ' ');
  262 + result.append(QIntC::to_size(-length - str_length), ' ');
263 263 }
264 264 return result;
265 265 }
... ... @@ -273,7 +273,7 @@ QUtil::int_to_string(long long num, int length)
273 273 std::string
274 274 QUtil::uint_to_string(unsigned long long num, int length)
275 275 {
276   - return int_to_string_base(num, 10, length);
  276 + return uint_to_string_base(num, 10, length);
277 277 }
278 278  
279 279 std::string
... ... @@ -420,7 +420,7 @@ QUtil::safe_fopen(char const* filename, char const* mode)
420 420 wmode[strlen(mode)] = 0;
421 421 for (size_t i = 0; i < strlen(mode); ++i)
422 422 {
423   - wmode[i] = mode[i];
  423 + wmode[i] = static_cast<wchar_t>(mode[i]);
424 424 }
425 425  
426 426 #ifdef _MSC_VER
... ... @@ -465,9 +465,7 @@ QUtil::seek(FILE* stream, qpdf_offset_t offset, int whence)
465 465 # if defined _MSC_VER || defined __BORLANDC__
466 466 return _fseeki64(stream, offset, whence);
467 467 # else
468   - return fseek(stream,
469   - QIntC::IntConverter<qpdf_offset_t, long>(offset),
470   - whence);
  468 + return fseek(stream, QIntC::to_long(offset), whence);
471 469 # endif
472 470 #endif
473 471 }
... ... @@ -572,17 +570,15 @@ QUtil::hex_decode(std::string const&amp; input)
572 570 bool skip = false;
573 571 if ((*p >= 'A') && (*p <= 'F'))
574 572 {
575   - ch -= 'A';
576   - ch += 10;
  573 + ch = QIntC::to_char(ch - 'A' + 10);
577 574 }
578 575 else if ((*p >= 'a') && (*p <= 'f'))
579 576 {
580   - ch -= 'a';
581   - ch += 10;
  577 + ch = QIntC::to_char(ch - 'a' + 10);
582 578 }
583 579 else if ((*p >= '0') && (*p <= '9'))
584 580 {
585   - ch -= '0';
  581 + ch = QIntC::to_char(ch - '0');
586 582 }
587 583 else
588 584 {
... ... @@ -592,12 +588,12 @@ QUtil::hex_decode(std::string const&amp; input)
592 588 {
593 589 if (pos == 0)
594 590 {
595   - result.push_back(ch << 4);
  591 + result.push_back(static_cast<char>(ch << 4));
596 592 pos = 1;
597 593 }
598 594 else
599 595 {
600   - result[result.length()-1] += ch;
  596 + result[result.length()-1] |= ch;
601 597 pos = 0;
602 598 }
603 599 }
... ... @@ -717,7 +713,7 @@ QUtil::get_current_time()
717 713 uinow.LowPart = filenow.dwLowDateTime;
718 714 uinow.HighPart = filenow.dwHighDateTime;
719 715 ULONGLONG now = uinow.QuadPart;
720   - return ((now / 10000000LL) - 11644473600LL);
  716 + return static_cast<time_t>((now / 10000000ULL) - 11644473600ULL);
721 717 #else
722 718 return time(0);
723 719 #endif
... ... @@ -762,7 +758,7 @@ QUtil::toUTF8(unsigned long uval)
762 758 *cur_byte = static_cast<unsigned char>(0x80 + (uval & 0x3f));
763 759 uval >>= 6;
764 760 // Maximum that will fit in high byte now shrinks by one bit
765   - maxval >>= 1;
  761 + maxval = static_cast<unsigned char>(maxval >> 1);
766 762 // Slide to the left one byte
767 763 if (cur_byte <= bytes)
768 764 {
... ... @@ -773,7 +769,7 @@ QUtil::toUTF8(unsigned long uval)
773 769 // If maxval is k bits long, the high (7 - k) bits of the
774 770 // resulting byte must be high.
775 771 *cur_byte = static_cast<unsigned char>(
776   - (0xff - (1 + (maxval << 1))) + uval);
  772 + QIntC::to_ulong(0xff - (1 + (maxval << 1))) + uval);
777 773  
778 774 result += reinterpret_cast<char*>(cur_byte);
779 775 }
... ... @@ -792,20 +788,22 @@ QUtil::toUTF16(unsigned long uval)
792 788 else if (uval <= 0xffff)
793 789 {
794 790 char out[2];
795   - out[0] = (uval & 0xff00) >> 8;
796   - out[1] = (uval & 0xff);
  791 + out[0] = static_cast<char>((uval & 0xff00) >> 8);
  792 + out[1] = static_cast<char>(uval & 0xff);
797 793 result = std::string(out, 2);
798 794 }
799 795 else if (uval <= 0x10ffff)
800 796 {
801 797 char out[4];
802 798 uval -= 0x10000;
803   - unsigned short high = ((uval & 0xffc00) >> 10) + 0xd800;
804   - unsigned short low = (uval & 0x3ff) + 0xdc00;
805   - out[0] = (high & 0xff00) >> 8;
806   - out[1] = (high & 0xff);
807   - out[2] = (low & 0xff00) >> 8;
808   - out[3] = (low & 0xff);
  799 + unsigned short high =
  800 + static_cast<unsigned short>(((uval & 0xffc00) >> 10) + 0xd800);
  801 + unsigned short low =
  802 + static_cast<unsigned short>((uval & 0x3ff) + 0xdc00);
  803 + out[0] = static_cast<char>((high & 0xff00) >> 8);
  804 + out[1] = static_cast<char>(high & 0xff);
  805 + out[2] = static_cast<char>((low & 0xff00) >> 8);
  806 + out[3] = static_cast<char>(low & 0xff);
809 807 result = std::string(out, 4);
810 808 }
811 809 else
... ... @@ -1172,7 +1170,8 @@ QUtil::parse_numrange(char const* range, int max)
1172 1170 if (p)
1173 1171 {
1174 1172 message = "error at * in numeric range " +
1175   - std::string(range, p - range) + "*" + p + ": " + e.what();
  1173 + std::string(range, QIntC::to_size(p - range)) +
  1174 + "*" + p + ": " + e.what();
1176 1175 }
1177 1176 else
1178 1177 {
... ... @@ -1764,7 +1763,7 @@ unsigned long get_next_utf8_codepoint(
1764 1763 while (ch & bit_check)
1765 1764 {
1766 1765 ++bytes_needed;
1767   - to_clear |= bit_check;
  1766 + to_clear = static_cast<unsigned char>(to_clear | bit_check);
1768 1767 bit_check >>= 1;
1769 1768 }
1770 1769 if (((bytes_needed > 5) || (bytes_needed < 1)) ||
... ... @@ -1774,11 +1773,11 @@ unsigned long get_next_utf8_codepoint(
1774 1773 return 0xfffd;
1775 1774 }
1776 1775  
1777   - unsigned long codepoint = (ch & ~to_clear);
  1776 + unsigned long codepoint = static_cast<unsigned long>(ch & ~to_clear);
1778 1777 while (bytes_needed > 0)
1779 1778 {
1780 1779 --bytes_needed;
1781   - ch = utf8_val.at(++pos);
  1780 + ch = static_cast<unsigned char>(utf8_val.at(++pos));
1782 1781 if ((ch & 0xc0) != 0x80)
1783 1782 {
1784 1783 --pos;
... ... @@ -1823,7 +1822,7 @@ transcode_utf8(std::string const&amp; utf8_val, std::string&amp; result,
1823 1822 char ch = static_cast<char>(codepoint);
1824 1823 if (encoding == e_utf16)
1825 1824 {
1826   - result += QUtil::toUTF16(ch);
  1825 + result += QUtil::toUTF16(QIntC::to_ulong(ch));
1827 1826 }
1828 1827 else
1829 1828 {
... ... @@ -1837,7 +1836,7 @@ transcode_utf8(std::string const&amp; utf8_val, std::string&amp; result,
1837 1836 else if ((codepoint > 160) && (codepoint < 256) &&
1838 1837 ((encoding == e_winansi) || (encoding == e_pdfdoc)))
1839 1838 {
1840   - result.append(1, static_cast<unsigned char>(codepoint & 0xff));
  1839 + result.append(1, static_cast<char>(codepoint & 0xff));
1841 1840 }
1842 1841 else
1843 1842 {
... ... @@ -1859,7 +1858,7 @@ transcode_utf8(std::string const&amp; utf8_val, std::string&amp; result,
1859 1858 okay = false;
1860 1859 ch = static_cast<unsigned char>(unknown);
1861 1860 }
1862   - result.append(1, ch);
  1861 + result.append(1, static_cast<char>(ch));
1863 1862 }
1864 1863 }
1865 1864 return okay;
... ... @@ -1956,7 +1955,7 @@ QUtil::utf16_to_utf8(std::string const&amp; val)
1956 1955 }
1957 1956 // If the string has an odd number of bytes, the last byte is
1958 1957 // ignored.
1959   - for (unsigned int i = start; i < len; i += 2)
  1958 + for (size_t i = start; i < len; i += 2)
1960 1959 {
1961 1960 // Convert from UTF16-BE. If we get a malformed
1962 1961 // codepoint, this code will generate incorrect output
... ... @@ -1965,11 +1964,12 @@ QUtil::utf16_to_utf8(std::string const&amp; val)
1965 1964 // discarded, and a low codepoint not preceded by a high
1966 1965 // codepoint will just get its low 10 bits output.
1967 1966 unsigned short bits =
1968   - (static_cast<unsigned char>(val.at(i)) << 8) +
1969   - static_cast<unsigned char>(val.at(i+1));
  1967 + QIntC::to_ushort(
  1968 + (static_cast<unsigned char>(val.at(i)) << 8) +
  1969 + static_cast<unsigned char>(val.at(i+1)));
1970 1970 if ((bits & 0xFC00) == 0xD800)
1971 1971 {
1972   - codepoint = 0x10000 + ((bits & 0x3FF) << 10);
  1972 + codepoint = 0x10000U + ((bits & 0x3FFU) << 10U);
1973 1973 continue;
1974 1974 }
1975 1975 else if ((bits & 0xFC00) == 0xDC00)
... ...
libqpdf/RC4.cc
1 1 #include <qpdf/RC4.hh>
  2 +#include <qpdf/QIntC.hh>
2 3  
3 4 #include <string.h>
4 5  
... ... @@ -15,12 +16,13 @@ RC4::RC4(unsigned char const* key_data, int key_len)
15 16 {
16 17 if (key_len == -1)
17 18 {
18   - key_len = strlen(reinterpret_cast<char const*>(key_data));
  19 + key_len = QIntC::to_int(
  20 + strlen(reinterpret_cast<char const*>(key_data)));
19 21 }
20 22  
21 23 for (int i = 0; i < 256; ++i)
22 24 {
23   - key.state[i] = i;
  25 + key.state[i] = static_cast<unsigned char>(i);
24 26 }
25 27 key.x = 0;
26 28 key.y = 0;
... ... @@ -36,7 +38,7 @@ RC4::RC4(unsigned char const* key_data, int key_len)
36 38 }
37 39  
38 40 void
39   -RC4::process(unsigned char *in_data, int len, unsigned char* out_data)
  41 +RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data)
40 42 {
41 43 if (out_data == 0)
42 44 {
... ... @@ -44,10 +46,10 @@ RC4::process(unsigned char *in_data, int len, unsigned char* out_data)
44 46 out_data = in_data;
45 47 }
46 48  
47   - for (int i = 0; i < len; ++i)
  49 + for (size_t i = 0; i < len; ++i)
48 50 {
49   - key.x = (key.x + 1) % 256;
50   - key.y = (key.state[key.x] + key.y) % 256;
  51 + key.x = static_cast<unsigned char>((key.x + 1) % 256);
  52 + key.y = static_cast<unsigned char>((key.state[key.x] + key.y) % 256);
51 53 swap_byte(key.state[key.x], key.state[key.y]);
52 54 int xor_index = (key.state[key.x] + key.state[key.y]) % 256;
53 55 out_data[i] = in_data[i] ^ key.state[xor_index];
... ...
libqpdf/SecureRandomDataProvider.cc
... ... @@ -53,6 +53,7 @@ class WindowsCryptProvider
53 53 # pragma GCC diagnostic push
54 54 # pragma GCC diagnostic ignored "-Wold-style-cast"
55 55 # pragma GCC diagnostic ignored "-Wsign-compare"
  56 +# pragma GCC diagnostic ignored "-Wsign-conversion"
56 57 #endif
57 58 if (GetLastError() == NTE_BAD_KEYSET)
58 59 #if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
... ... @@ -94,7 +95,8 @@ SecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
94 95 // Optimization: make the WindowsCryptProvider static as long as
95 96 // it can be done in a thread-safe fashion.
96 97 WindowsCryptProvider c;
97   - if (! CryptGenRandom(c.crypt_prov, len, reinterpret_cast<BYTE*>(data)))
  98 + if (! CryptGenRandom(c.crypt_prov, static_cast<DWORD>(len),
  99 + reinterpret_cast<BYTE*>(data)))
98 100 {
99 101 throw std::runtime_error("unable to generate secure random data");
100 102 }
... ... @@ -112,7 +114,7 @@ SecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
112 114 {
113 115 throw std::runtime_error(
114 116 "unable to read " +
115   - QUtil::int_to_string(len) +
  117 + QUtil::uint_to_string(len) +
116 118 " bytes from " + std::string(RANDOM_DEVICE));
117 119 }
118 120  
... ...
libqpdf/bits.icc
... ... @@ -16,8 +16,8 @@
16 16  
17 17 #ifdef BITS_READ
18 18 static unsigned long long
19   -read_bits(unsigned char const*& p, unsigned int& bit_offset,
20   - unsigned int& bits_available, unsigned int bits_wanted)
  19 +read_bits(unsigned char const*& p, size_t& bit_offset,
  20 + size_t& bits_available, size_t bits_wanted)
21 21 {
22 22 // View p as a stream of bits:
23 23  
... ... @@ -46,11 +46,12 @@ read_bits(unsigned char const*&amp; p, unsigned int&amp; bit_offset,
46 46 {
47 47 // Grab bits from the first byte clearing anything before
48 48 // bit_offset.
49   - unsigned char byte = *p & ((1 << (bit_offset + 1)) - 1);
  49 + unsigned char byte = static_cast<unsigned char>(
  50 + *p & ((1U << (bit_offset + 1U)) - 1U));
50 51  
51 52 // There are bit_offset + 1 bits available in the first byte.
52   - unsigned int to_copy = std::min(bits_wanted, bit_offset + 1);
53   - unsigned int leftover = (bit_offset + 1) - to_copy;
  53 + size_t to_copy = std::min(bits_wanted, bit_offset + 1);
  54 + size_t leftover = (bit_offset + 1) - to_copy;
54 55  
55 56 #ifdef BITS_TESTING
56 57 QTC::TC("libtests", "bits bit_offset",
... ... @@ -61,7 +62,7 @@ read_bits(unsigned char const*&amp; p, unsigned int&amp; bit_offset,
61 62 #endif
62 63  
63 64 // Right shift so that all the bits we want are right justified.
64   - byte >>= leftover;
  65 + byte = static_cast<unsigned char>(byte >> leftover);
65 66  
66 67 // Copy the bits into result
67 68 result <<= to_copy;
... ... @@ -94,8 +95,8 @@ read_bits(unsigned char const*&amp; p, unsigned int&amp; bit_offset,
94 95  
95 96 #ifdef BITS_WRITE
96 97 static void
97   -write_bits(unsigned char& ch, unsigned int& bit_offset,
98   - unsigned long long val, unsigned int bits, Pipeline* pipeline)
  98 +write_bits(unsigned char& ch, size_t& bit_offset,
  99 + unsigned long long val, size_t bits, Pipeline* pipeline)
99 100 {
100 101 if (bits > 32)
101 102 {
... ... @@ -111,11 +112,11 @@ write_bits(unsigned char&amp; ch, unsigned int&amp; bit_offset,
111 112 #endif
112 113 while (bits > 0)
113 114 {
114   - int bits_to_write = std::min(bits, bit_offset + 1);
115   - unsigned char newval =
116   - (val >> (bits - bits_to_write)) & ((1 << bits_to_write) - 1);
117   - int bits_left_in_ch = bit_offset + 1 - bits_to_write;
118   - newval <<= bits_left_in_ch;
  115 + size_t bits_to_write = std::min(bits, bit_offset + 1);
  116 + unsigned char newval = static_cast<unsigned char>(
  117 + (val >> (bits - bits_to_write)) & ((1U << bits_to_write) - 1));
  118 + size_t bits_left_in_ch = bit_offset + 1 - bits_to_write;
  119 + newval = static_cast<unsigned char>(newval << bits_left_in_ch);
119 120 ch |= newval;
120 121 if (bits_left_in_ch == 0)
121 122 {
... ...
libqpdf/qpdf-c.cc
... ... @@ -5,6 +5,7 @@
5 5 #include <qpdf/QTC.hh>
6 6 #include <qpdf/QPDFExc.hh>
7 7 #include <qpdf/Pl_Discard.hh>
  8 +#include <qpdf/QIntC.hh>
8 9  
9 10 #include <list>
10 11 #include <string>
... ... @@ -63,7 +64,7 @@ static void call_read(qpdf_data qpdf)
63 64 static void call_read_memory(qpdf_data qpdf)
64 65 {
65 66 qpdf->qpdf->processMemoryFile(qpdf->filename, qpdf->buffer,
66   - qpdf->size, qpdf->password);
  67 + QIntC::to_size(qpdf->size), qpdf->password);
67 68 }
68 69  
69 70 // must set qpdf->filename
... ... @@ -234,7 +235,7 @@ unsigned long long qpdf_get_error_file_position(qpdf_data qpdf, qpdf_error e)
234 235 {
235 236 return 0;
236 237 }
237   - return e->exc->getFilePosition();
  238 + return QIntC::to_ulonglong(e->exc->getFilePosition());
238 239 }
239 240  
240 241 char const* qpdf_get_error_message_detail(qpdf_data qpdf, qpdf_error e)
... ...
libqpdf/qpdf/BitStream.hh
... ... @@ -4,28 +4,33 @@
4 4 #define BITSTREAM_HH
5 5  
6 6 #include <qpdf/DLL.h>
  7 +#include <stddef.h>
7 8  
8 9 class BitStream
9 10 {
10 11 public:
11 12 QPDF_DLL
12   - BitStream(unsigned char const* p, int nbytes);
  13 + BitStream(unsigned char const* p, size_t nbytes);
13 14 QPDF_DLL
14 15 void reset();
15 16 QPDF_DLL
16   - unsigned long long getBits(int nbits);
  17 + unsigned long long getBits(size_t nbits);
17 18 QPDF_DLL
18   - long long getBitsSigned(int nbits);
  19 + long long getBitsSigned(size_t nbits);
  20 + // Only call getBitsInt when requesting a number of bits that will
  21 + // definitely fit in an int.
  22 + QPDF_DLL
  23 + int getBitsInt(size_t nbits);
19 24 QPDF_DLL
20 25 void skipToNextByte();
21 26  
22 27 private:
23 28 unsigned char const* start;
24   - int nbytes;
  29 + size_t nbytes;
25 30  
26 31 unsigned char const* p;
27   - unsigned int bit_offset;
28   - unsigned int bits_available;
  32 + size_t bit_offset;
  33 + size_t bits_available;
29 34 };
30 35  
31 36 #endif // BITSTREAM_HH
... ...
libqpdf/qpdf/BitWriter.hh
... ... @@ -4,6 +4,7 @@
4 4 #define BITWRITER_HH
5 5  
6 6 #include <qpdf/DLL.h>
  7 +#include <stddef.h>
7 8  
8 9 class Pipeline;
9 10  
... ... @@ -15,9 +16,11 @@ class BitWriter
15 16 QPDF_DLL
16 17 BitWriter(Pipeline* pl);
17 18 QPDF_DLL
18   - void writeBits(unsigned long long val, unsigned int bits);
  19 + void writeBits(unsigned long long val, size_t bits);
19 20 QPDF_DLL
20   - void writeBitsSigned(long long val, unsigned int bits);
  21 + void writeBitsSigned(long long val, size_t bits);
  22 + QPDF_DLL
  23 + void writeBitsInt(int val, size_t bits);
21 24 // Force any partial byte to be written to the pipeline.
22 25 QPDF_DLL
23 26 void flush();
... ... @@ -25,7 +28,7 @@ class BitWriter
25 28 private:
26 29 Pipeline* pl;
27 30 unsigned char ch;
28   - unsigned int bit_offset;
  31 + size_t bit_offset;
29 32 };
30 33  
31 34 #endif // BITWRITER_HH
... ...
libqpdf/qpdf/MD5.hh
... ... @@ -3,6 +3,7 @@
3 3  
4 4 #include <qpdf/DLL.h>
5 5 #include <qpdf/qpdf-config.h>
  6 +#include <qpdf/Types.h>
6 7 #ifdef HAVE_INTTYPES_H
7 8 # include <inttypes.h>
8 9 #endif
... ... @@ -25,9 +26,9 @@ class MD5
25 26 QPDF_DLL
26 27 void encodeString(char const* input_string);
27 28  
28   - // encodes file and finalizes
  29 + // encodes file and finalizes; offset < 0 reads whole file
29 30 QPDF_DLL
30   - void encodeFile(char const* filename, int up_to_size = -1);
  31 + void encodeFile(char const* filename, qpdf_offset_t up_to_offset = -1);
31 32  
32 33 // appends string to current md5 object
33 34 QPDF_DLL
... ... @@ -35,7 +36,7 @@ class MD5
35 36  
36 37 // appends arbitrary data to current md5 object
37 38 QPDF_DLL
38   - void encodeDataIncrementally(char const* input_data, int len);
  39 + void encodeDataIncrementally(char const* input_data, size_t len);
39 40  
40 41 // computes a raw digest
41 42 QPDF_DLL
... ... @@ -52,16 +53,17 @@ class MD5
52 53  
53 54 // Convenience functions
54 55 QPDF_DLL
55   - static std::string getDataChecksum(char const* buf, int len);
  56 + static std::string getDataChecksum(char const* buf, size_t len);
56 57 QPDF_DLL
57 58 static std::string getFileChecksum(char const* filename,
58   - int up_to_size = -1);
  59 + qpdf_offset_t up_to_offset = -1);
59 60 QPDF_DLL
60 61 static bool checkDataChecksum(char const* const checksum,
61   - char const* buf, int len);
  62 + char const* buf, size_t len);
62 63 QPDF_DLL
63 64 static bool checkFileChecksum(char const* const checksum,
64   - char const* filename, int up_to_size = -1);
  65 + char const* filename,
  66 + qpdf_offset_t up_to_offset = -1);
65 67  
66 68 private:
67 69 // POINTER defines a generic pointer type
... ... @@ -74,12 +76,12 @@ class MD5
74 76 typedef uint32_t UINT4;
75 77  
76 78 void init();
77   - void update(unsigned char *, unsigned int);
  79 + void update(unsigned char *, size_t);
78 80 void final();
79 81  
80 82 static void transform(UINT4 [4], unsigned char [64]);
81   - static void encode(unsigned char *, UINT4 *, unsigned int);
82   - static void decode(UINT4 *, unsigned char *, unsigned int);
  83 + static void encode(unsigned char *, UINT4 *, size_t);
  84 + static void decode(UINT4 *, unsigned char *, size_t);
83 85  
84 86 UINT4 state[4]; // state (ABCD)
85 87 UINT4 count[2]; // number of bits, modulo 2^64 (lsb first)
... ...
libqpdf/qpdf/Pl_AES_PDF.hh
... ... @@ -16,7 +16,8 @@ class Pl_AES_PDF: public Pipeline
16 16 QPDF_DLL
17 17 // key should be a pointer to key_bytes bytes of data
18 18 Pl_AES_PDF(char const* identifier, Pipeline* next,
19   - bool encrypt, unsigned char const* key, unsigned int key_bytes);
  19 + bool encrypt, unsigned char const* key,
  20 + size_t key_bytes);
20 21 QPDF_DLL
21 22 virtual ~Pl_AES_PDF();
22 23  
... ...
libqpdf/qpdf/Pl_ASCII85Decoder.hh
... ... @@ -18,7 +18,7 @@ class Pl_ASCII85Decoder: public Pipeline
18 18 private:
19 19 void flush();
20 20  
21   - char inbuf[5];
  21 + unsigned char inbuf[5];
22 22 size_t pos;
23 23 size_t eod;
24 24 };
... ...
libqpdf/qpdf/Pl_LZWDecoder.hh
... ... @@ -21,23 +21,23 @@ class Pl_LZWDecoder: public Pipeline
21 21  
22 22 private:
23 23 void sendNextCode();
24   - void handleCode(int code);
25   - unsigned char getFirstChar(int code);
  24 + void handleCode(unsigned int code);
  25 + unsigned char getFirstChar(unsigned int code);
26 26 void addToTable(unsigned char next);
27 27  
28 28 // members used for converting bits to codes
29 29 unsigned char buf[3];
30   - int code_size;
31   - int next;
32   - int byte_pos;
33   - int bit_pos; // left to right: 01234567
34   - int bits_available;
  30 + unsigned int code_size;
  31 + unsigned int next;
  32 + unsigned int byte_pos;
  33 + unsigned int bit_pos; // left to right: 01234567
  34 + unsigned int bits_available;
35 35  
36 36 // members used for handle LZW decompression
37 37 bool code_change_delta;
38 38 bool eod;
39 39 std::vector<Buffer> table;
40   - int last_code;
  40 + unsigned int last_code;
41 41 };
42 42  
43 43 #endif // PL_LZWDECODER_HH
... ...
libqpdf/qpdf/Pl_RC4.hh
... ... @@ -8,7 +8,7 @@
8 8 class Pl_RC4: public Pipeline
9 9 {
10 10 public:
11   - static int const def_bufsize = 65536;
  11 + static size_t const def_bufsize = 65536;
12 12  
13 13 // key_len of -1 means treat key_data as a null-terminated string
14 14 QPDF_DLL
... ...
libqpdf/qpdf/RC4.hh
1 1 #ifndef RC4_HH
2 2 #define RC4_HH
3 3  
  4 +#include <stddef.h>
  5 +
4 6 class RC4
5 7 {
6 8 public:
... ... @@ -8,7 +10,8 @@ class RC4
8 10 RC4(unsigned char const* key_data, int key_len = -1);
9 11  
10 12 // out_data = 0 means to encrypt/decrypt in place
11   - void process(unsigned char* in_data, int len, unsigned char* out_data = 0);
  13 + void process(unsigned char* in_data, size_t len,
  14 + unsigned char* out_data = 0);
12 15  
13 16 private:
14 17 class RC4Key
... ...
libqpdf/qpdf/rijndael.h
... ... @@ -8,14 +8,15 @@
8 8 #ifdef HAVE_STDINT_H
9 9 # include <stdint.h>
10 10 #endif
  11 +#include <stddef.h>
11 12  
12   -int rijndaelSetupEncrypt(uint32_t *rk, const unsigned char *key,
13   - int keybits);
14   -int rijndaelSetupDecrypt(uint32_t *rk, const unsigned char *key,
15   - int keybits);
16   -void rijndaelEncrypt(const uint32_t *rk, int nrounds,
  13 +unsigned int rijndaelSetupEncrypt(uint32_t *rk, const unsigned char *key,
  14 + size_t keybits);
  15 +unsigned int rijndaelSetupDecrypt(uint32_t *rk, const unsigned char *key,
  16 + size_t keybits);
  17 +void rijndaelEncrypt(const uint32_t *rk, unsigned int nrounds,
17 18 const unsigned char plaintext[16], unsigned char ciphertext[16]);
18   -void rijndaelDecrypt(const uint32_t *rk, int nrounds,
  19 +void rijndaelDecrypt(const uint32_t *rk, unsigned int nrounds,
19 20 const unsigned char ciphertext[16], unsigned char plaintext[16]);
20 21  
21 22 #define KEYLENGTH(keybits) ((keybits)/8)
... ...
libqpdf/rijndael.cc
... ... @@ -710,7 +710,7 @@ static const u32 rcon[] =
710 710 *
711 711 * @return the number of rounds for the given cipher key size.
712 712 */
713   -int rijndaelSetupEncrypt(u32 *rk, const u8 *key, int keybits)
  713 +unsigned int rijndaelSetupEncrypt(u32 *rk, const u8 *key, size_t keybits)
714 714 {
715 715 int i = 0;
716 716 u32 temp;
... ... @@ -799,9 +799,9 @@ int rijndaelSetupEncrypt(u32 *rk, const u8 *key, int keybits)
799 799 *
800 800 * @return the number of rounds for the given cipher key size.
801 801 */
802   -int rijndaelSetupDecrypt(u32 *rk, const u8 *key, int keybits)
  802 +unsigned int rijndaelSetupDecrypt(u32 *rk, const u8 *key, size_t keybits)
803 803 {
804   - int nrounds, i, j;
  804 + unsigned int nrounds, i, j;
805 805 u32 temp;
806 806  
807 807 /* expand the cipher key: */
... ... @@ -842,7 +842,8 @@ int rijndaelSetupDecrypt(u32 *rk, const u8 *key, int keybits)
842 842 return nrounds;
843 843 }
844 844  
845   -void rijndaelEncrypt(const u32 *rk, int nrounds, const u8 plaintext[16],
  845 +void rijndaelEncrypt(const u32 *rk, unsigned int nrounds,
  846 + const u8 plaintext[16],
846 847 u8 ciphertext[16])
847 848 {
848 849 u32 s0, s1, s2, s3, t0, t1, t2, t3;
... ... @@ -1024,7 +1025,8 @@ void rijndaelEncrypt(const u32 *rk, int nrounds, const u8 plaintext[16],
1024 1025 PUTU32(ciphertext + 12, s3);
1025 1026 }
1026 1027  
1027   -void rijndaelDecrypt(const u32 *rk, int nrounds, const u8 ciphertext[16],
  1028 +void rijndaelDecrypt(const u32 *rk, unsigned int nrounds,
  1029 + const u8 ciphertext[16],
1028 1030 u8 plaintext[16])
1029 1031 {
1030 1032 u32 s0, s1, s2, s3, t0, t1, t2, t3;
... ...
libqpdf/sph/md_helper.c
... ... @@ -145,7 +145,7 @@ SPH_XCAT(sph_, HASH)(void *cc, const void *data, size_t len)
145 145  
146 146 clen = SPH_BLEN - current;
147 147 if (clen > len)
148   - clen = len;
  148 + clen = (unsigned)len;
149 149 memcpy(sc->buf + current, data, clen);
150 150 data = (const unsigned char *)data + clen;
151 151 current += clen;
... ... @@ -257,7 +257,7 @@ SPH_XCAT(HASH, _addbits_and_close)(void *cc,
257 257 {
258 258 unsigned z;
259 259  
260   - z = 0x80 >> n;
  260 + z = 0x80U >> n;
261 261 sc->buf[current ++] = ((ub & -z) | z) & 0xFF;
262 262 }
263 263 #endif
... ...
libqpdf/sph/sph_types.h
... ... @@ -1337,8 +1337,8 @@ sph_bswap64(sph_u64 x)
1337 1337 static SPH_INLINE void
1338 1338 sph_enc16be(void *dst, unsigned val)
1339 1339 {
1340   - ((unsigned char *)dst)[0] = (val >> 8);
1341   - ((unsigned char *)dst)[1] = val;
  1340 + ((unsigned char *)dst)[0] = (unsigned char)(val >> 8);
  1341 + ((unsigned char *)dst)[1] = (unsigned char)val;
1342 1342 }
1343 1343  
1344 1344 static SPH_INLINE unsigned
... ... @@ -1351,8 +1351,8 @@ sph_dec16be(const void *src)
1351 1351 static SPH_INLINE void
1352 1352 sph_enc16le(void *dst, unsigned val)
1353 1353 {
1354   - ((unsigned char *)dst)[0] = val;
1355   - ((unsigned char *)dst)[1] = val >> 8;
  1354 + ((unsigned char *)dst)[0] = (unsigned char)val;
  1355 + ((unsigned char *)dst)[1] = (unsigned char)(val >> 8);
1356 1356 }
1357 1357  
1358 1358 static SPH_INLINE unsigned
... ...
libtests/aes.cc
1 1 #include <qpdf/Pl_AES_PDF.hh>
2 2 #include <qpdf/Pl_StdioFile.hh>
3 3 #include <qpdf/QUtil.hh>
  4 +#include <qpdf/QIntC.hh>
4 5  
5 6 #include <stdio.h>
6 7 #include <string.h>
... ... @@ -87,7 +88,7 @@ int main(int argc, char* argv[])
87 88 usage();
88 89 }
89 90  
90   - unsigned int hexkeylen = strlen(hexkey);
  91 + unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
91 92 unsigned int keylen = hexkeylen / 2;
92 93  
93 94 FILE* infile = QUtil::safe_fopen(infilename, "rb");
... ...
libtests/bits.cc
1 1 #include <qpdf/BitStream.hh>
2 2 #include <qpdf/BitWriter.hh>
3 3 #include <qpdf/Pl_Buffer.hh>
  4 +#include <qpdf/QUtil.hh>
  5 +#include <qpdf/QIntC.hh>
4 6 #include <iostream>
5 7 #include <stdio.h>
6 8 #include <stdlib.h>
... ... @@ -12,8 +14,8 @@
12 14 #include "../libqpdf/bits.icc"
13 15  
14 16 static void
15   -print_values(int byte_offset, unsigned int bit_offset,
16   - unsigned int bits_available)
  17 +print_values(long long byte_offset, size_t bit_offset,
  18 + size_t bits_available)
17 19 {
18 20 std::cout << "byte offset = " << byte_offset << ", "
19 21 << "bit offset = " << bit_offset << ", "
... ... @@ -22,11 +24,11 @@ print_values(int byte_offset, unsigned int bit_offset,
22 24  
23 25 static void
24 26 test_read_bits(unsigned char const* buf,
25   - unsigned char const*& p, unsigned int& bit_offset,
26   - unsigned int& bits_available, int bits_wanted)
  27 + unsigned char const*& p, size_t& bit_offset,
  28 + size_t& bits_available, size_t bits_wanted)
27 29 {
28 30 unsigned long result =
29   - read_bits(p, bit_offset, bits_available, bits_wanted);
  31 + QIntC::to_ulong(read_bits(p, bit_offset, bits_available, bits_wanted));
30 32  
31 33 std::cout << "bits read: " << bits_wanted << ", result = " << result
32 34 << std::endl;
... ... @@ -34,12 +36,12 @@ test_read_bits(unsigned char const* buf,
34 36 }
35 37  
36 38 static void
37   -test_write_bits(unsigned char& ch, unsigned int& bit_offset, unsigned long val,
38   - int bits, Pl_Buffer* bp)
  39 +test_write_bits(unsigned char& ch, size_t& bit_offset, unsigned long val,
  40 + size_t bits, Pl_Buffer* bp)
39 41 {
40 42 write_bits(ch, bit_offset, val, bits, bp);
41   - printf("ch = %02x, bit_offset = %d\n",
42   - static_cast<unsigned int>(ch), bit_offset);
  43 + std::cout << "ch = " << QUtil::uint_to_string_base(ch, 16, 2)
  44 + << ", bit_offset = " << bit_offset << std::endl;
43 45 }
44 46  
45 47 static void
... ... @@ -51,10 +53,10 @@ print_buffer(Pl_Buffer* bp)
51 53 size_t l = b->getSize();
52 54 for (unsigned long i = 0; i < l; ++i)
53 55 {
54   - printf("%02x%s", static_cast<unsigned int>(p[i]),
55   - (i == l - 1) ? "\n" : " ");
  56 + std::cout << QUtil::uint_to_string_base(p[i], 16, 2)
  57 + << ((i == l - 1) ? "\n" : " ");
56 58 }
57   - printf("\n");
  59 + std::cout << std::endl;
58 60 delete b;
59 61 }
60 62  
... ... @@ -71,8 +73,8 @@ test()
71 73 };
72 74  
73 75 unsigned char const* p = buf;
74   - unsigned int bit_offset = 7;
75   - unsigned int bits_available = 64;
  76 + size_t bit_offset = 7;
  77 + size_t bits_available = 64;
76 78  
77 79 // 11110:101 0:001010:1 01100101: 01111001
78 80 // 0:00:1:0010 10001001 01110101 01001:011
... ... @@ -163,7 +165,7 @@ test()
163 165 bw.writeBits(30UL, 5);
164 166 bw.flush();
165 167 bw.flush();
166   - bw.writeBits(0xABUL, 8);
  168 + bw.writeBitsInt(0xAB, 8);
167 169 bw.flush();
168 170 print_buffer(bp);
169 171 bw.writeBitsSigned(-1, 3); // 111
... ...
libtests/dct_compress.cc
... ... @@ -42,8 +42,8 @@ int main(int argc, char* argv[])
42 42  
43 43 char* infilename = argv[1];
44 44 char* outfilename = argv[2];
45   - int width = QUtil::string_to_int(argv[3]);
46   - int height = QUtil::string_to_int(argv[4]);
  45 + JDIMENSION width = QUtil::string_to_uint(argv[3]);
  46 + JDIMENSION height = QUtil::string_to_uint(argv[4]);
47 47 char* colorspace = argv[5];
48 48 J_COLOR_SPACE cs =
49 49 ((strcmp(colorspace, "rgb") == 0) ? JCS_RGB :
... ...
libtests/predictors.cc
... ... @@ -2,6 +2,7 @@
2 2 #include <qpdf/Pl_TIFFPredictor.hh>
3 3 #include <qpdf/Pl_StdioFile.hh>
4 4 #include <qpdf/QUtil.hh>
  5 +#include <qpdf/QIntC.hh>
5 6  
6 7 #include <iostream>
7 8 #include <errno.h>
... ... @@ -11,7 +12,7 @@
11 12  
12 13 void run(char const* filename, char const* filter,
13 14 bool encode, unsigned int columns,
14   - int bits_per_sample, int samples_per_pixel)
  15 + unsigned int bits_per_sample, unsigned int samples_per_pixel)
15 16 {
16 17 FILE* in = QUtil::safe_fopen(filename, "rb");
17 18 FILE* o1 = QUtil::safe_fopen("out", "wb");
... ... @@ -89,7 +90,9 @@ int main(int argc, char* argv[])
89 90 try
90 91 {
91 92 run(filename, filter, encode,
92   - columns, bits_per_sample, samples_per_pixel);
  93 + QIntC::to_uint(columns),
  94 + QIntC::to_uint(bits_per_sample),
  95 + QIntC::to_uint(samples_per_pixel));
93 96 }
94 97 catch (std::exception& e)
95 98 {
... ...
libtests/qintc.cc
... ... @@ -48,10 +48,10 @@ int main()
48 48 try_convert(true, QIntC::to_uint<uint64_t>, ul2);
49 49 try_convert(true, QIntC::to_offset<uint32_t>, u1);
50 50 try_convert(true, QIntC::to_offset<int32_t>, i1);
51   - try_convert(false, QIntC::to_size<int32_t>, i1);
  51 + try_convert(false, QIntC::to_ulonglong<int32_t>, i1);
52 52 try_convert(true, QIntC::to_char<int32_t>, i2);
53 53 try_convert(true, QIntC::to_uchar<int32_t>, i2);
54   - try_convert(false, QIntC::to_uchar<char>, c1);
  54 + try_convert(false, QIntC::to_uchar<char>, c1);
55 55  
56 56 return 0;
57 57 }
... ...
libtests/qtest/qintc/qintc.out
... ... @@ -7,7 +7,7 @@ QIntC::to_int&lt;uint64_t&gt;(ul2): 12345 12345 PASSED
7 7 QIntC::to_uint<uint64_t>(ul2): 12345 12345 PASSED
8 8 QIntC::to_offset<uint32_t>(u1): 3141592653 3141592653 PASSED
9 9 QIntC::to_offset<int32_t>(i1): -1153374643 -1153374643 PASSED
10   -QIntC::to_size<int32_t>(i1): integer out of range converting -1153374643 from a 4-byte signed type to a 8-byte unsigned type PASSED
  10 +QIntC::to_ulonglong<int32_t>(i1): integer out of range converting -1153374643 from a 4-byte signed type to a 8-byte unsigned type PASSED
11 11 QIntC::to_char<int32_t>(i2): 81 Q PASSED
12 12 QIntC::to_uchar<int32_t>(i2): 81 Q PASSED
13 13 QIntC::to_uchar<char>(c1): integer out of range converting รท from a 1-byte signed type to a 1-byte unsigned type PASSED
... ...
libtests/qtest/qutil/qutil.out
... ... @@ -12,6 +12,7 @@
12 12 16059
13 13 37273
14 14 3ebb
  15 +5000093552
15 16 one
16 17 7
17 18 compare okay
... ...
libtests/qutil.cc
... ... @@ -93,7 +93,8 @@ void string_conversion_test()
93 93 << QUtil::double_to_string(.000123456, 5) << std::endl
94 94 << QUtil::int_to_string_base(16059, 10) << std::endl
95 95 << QUtil::int_to_string_base(16059, 8) << std::endl
96   - << QUtil::int_to_string_base(16059, 16) << std::endl;
  96 + << QUtil::int_to_string_base(16059, 16) << std::endl
  97 + << QUtil::int_to_string_base(5000093552LL, 10) << std::endl;
97 98  
98 99 std::string embedded_null = "one";
99 100 embedded_null += '\0';
... ...
libtests/rc4.cc
1 1 #include <qpdf/Pl_RC4.hh>
2 2 #include <qpdf/Pl_StdioFile.hh>
3 3 #include <qpdf/QUtil.hh>
  4 +#include <qpdf/QIntC.hh>
4 5  
5 6 #include <stdio.h>
6 7 #include <string.h>
... ... @@ -18,7 +19,7 @@ int main(int argc, char* argv[])
18 19 char* hexkey = argv[1];
19 20 char* infilename = argv[2];
20 21 char* outfilename = argv[3];
21   - unsigned int hexkeylen = strlen(hexkey);
  22 + unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
22 23 unsigned int keylen = hexkeylen / 2;
23 24 unsigned char* key = new unsigned char[keylen + 1];
24 25 key[keylen] = '\0';
... ... @@ -38,7 +39,7 @@ int main(int argc, char* argv[])
38 39 FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
39 40 Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
40 41 // Use a small buffer size (64) for testing
41   - Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64);
  42 + Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, QIntC::to_int(keylen), 64U);
42 43 delete [] key;
43 44  
44 45 // 64 < buffer size < 512, buffer_size is not a power of 2 for testing
... ...
qpdf/qpdf-ctest.c
... ... @@ -391,7 +391,7 @@ static void test16(char const* infile,
391 391 qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress);
392 392 qpdf_write(qpdf);
393 393 f = safe_fopen(outfile, "wb");
394   - buflen = qpdf_get_buffer_length(qpdf);
  394 + buflen = (unsigned long)(qpdf_get_buffer_length(qpdf));
395 395 buf = qpdf_get_buffer(qpdf);
396 396 fwrite(buf, 1, buflen, f);
397 397 fclose(f);
... ...
qpdf/qpdf.cc
... ... @@ -24,6 +24,7 @@
24 24 #include <qpdf/QPDFExc.hh>
25 25  
26 26 #include <qpdf/QPDFWriter.hh>
  27 +#include <qpdf/QIntC.hh>
27 28  
28 29 static int const EXIT_ERROR = 2;
29 30 static int const EXIT_WARNING = 3;
... ... @@ -1809,8 +1810,7 @@ ArgParser::argKeepFilesOpen(char* parameter)
1809 1810 void
1810 1811 ArgParser::argKeepFilesOpenThreshold(char* parameter)
1811 1812 {
1812   - o.keep_files_open_threshold =
1813   - static_cast<size_t>(QUtil::string_to_int(parameter));
  1813 + o.keep_files_open_threshold = QUtil::string_to_uint(parameter);
1814 1814 }
1815 1815  
1816 1816 void
... ... @@ -2039,25 +2039,25 @@ ArgParser::argRemovePageLabels()
2039 2039 void
2040 2040 ArgParser::argOiMinWidth(char* parameter)
2041 2041 {
2042   - o.oi_min_width = QUtil::string_to_int(parameter);
  2042 + o.oi_min_width = QUtil::string_to_uint(parameter);
2043 2043 }
2044 2044  
2045 2045 void
2046 2046 ArgParser::argOiMinHeight(char* parameter)
2047 2047 {
2048   - o.oi_min_height = QUtil::string_to_int(parameter);
  2048 + o.oi_min_height = QUtil::string_to_uint(parameter);
2049 2049 }
2050 2050  
2051 2051 void
2052 2052 ArgParser::argOiMinArea(char* parameter)
2053 2053 {
2054   - o.oi_min_area = QUtil::string_to_int(parameter);
  2054 + o.oi_min_area = QUtil::string_to_uint(parameter);
2055 2055 }
2056 2056  
2057 2057 void
2058 2058 ArgParser::argIiMinBytes(char* parameter)
2059 2059 {
2060   - o.ii_min_bytes = QUtil::string_to_int(parameter);
  2060 + o.ii_min_bytes = QUtil::string_to_uint(parameter);
2061 2061 }
2062 2062  
2063 2063 void
... ... @@ -2318,7 +2318,7 @@ ArgParser::handleArgFileArguments()
2318 2318 {
2319 2319 argv[i] = new_argv.at(i).getPointer();
2320 2320 }
2321   - argc = static_cast<int>(new_argv.size());
  2321 + argc = QIntC::to_int(new_argv.size());
2322 2322 argv[argc] = 0;
2323 2323 }
2324 2324  
... ... @@ -2423,7 +2423,7 @@ ArgParser::handleBashArguments()
2423 2423 {
2424 2424 argv[i] = bash_argv.at(i).getPointer();
2425 2425 }
2426   - argc = static_cast<int>(bash_argv.size());
  2426 + argc = QIntC::to_int(bash_argv.size());
2427 2427 argv[argc] = 0;
2428 2428 }
2429 2429  
... ... @@ -2648,7 +2648,8 @@ QPDFPageData::QPDFPageData(std::string const&amp; filename,
2648 2648 try
2649 2649 {
2650 2650 this->selected_pages =
2651   - QUtil::parse_numrange(range, this->orig_pages.size());
  2651 + QUtil::parse_numrange(range,
  2652 + QIntC::to_int(this->orig_pages.size()));
2652 2653 }
2653 2654 catch (std::runtime_error& e)
2654 2655 {
... ... @@ -2805,8 +2806,8 @@ ArgParser::checkCompletion()
2805 2806 if (QUtil::get_env("COMP_LINE", &bash_line) &&
2806 2807 QUtil::get_env("COMP_POINT", &bash_point_env))
2807 2808 {
2808   - int p = QUtil::string_to_int(bash_point_env.c_str());
2809   - if ((p > 0) && (p <= static_cast<int>(bash_line.length())))
  2809 + size_t p = QUtil::string_to_uint(bash_point_env.c_str());
  2810 + if ((p > 0) && (p <= bash_line.length()))
2810 2811 {
2811 2812 // Truncate the line. We ignore everything at or after the
2812 2813 // cursor for completion purposes.
... ... @@ -2844,7 +2845,7 @@ ArgParser::checkCompletion()
2844 2845 {
2845 2846 // Go back to the last separator and set prev based on
2846 2847 // that.
2847   - int p1 = p;
  2848 + size_t p1 = p;
2848 2849 while (--p1 > 0)
2849 2850 {
2850 2851 char ch = bash_line.at(p1);
... ... @@ -3342,9 +3343,9 @@ static void do_show_pages(QPDF&amp; pdf, Options&amp; o)
3342 3343 QPDFObjectHandle image = (*iter).second;
3343 3344 QPDFObjectHandle dict = image.getDict();
3344 3345 int width =
3345   - dict.getKey("/Width").getIntValue();
  3346 + dict.getKey("/Width").getIntValueAsInt();
3346 3347 int height =
3347   - dict.getKey("/Height").getIntValue();
  3348 + dict.getKey("/Height").getIntValueAsInt();
3348 3349 std::cout << " " << name << ": "
3349 3350 << image.unparse()
3350 3351 << ", " << width << " x " << height
... ... @@ -3410,7 +3411,7 @@ static void do_json_pages(QPDF&amp; pdf, Options&amp; o, JSON&amp; j)
3410 3411 QPDFOutlineDocumentHelper odh(pdf);
3411 3412 pdh.pushInheritedAttributesToPage();
3412 3413 std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
3413   - size_t pageno = 0;
  3414 + int pageno = 0;
3414 3415 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
3415 3416 iter != pages.end(); ++iter, ++pageno)
3416 3417 {
... ... @@ -3503,7 +3504,8 @@ static void do_json_page_labels(QPDF&amp; pdf, Options&amp; o, JSON&amp; j)
3503 3504 if (pldh.hasPageLabels())
3504 3505 {
3505 3506 std::vector<QPDFObjectHandle> labels;
3506   - pldh.getLabelsForPageRange(0, pages.size() - 1, 0, labels);
  3507 + pldh.getLabelsForPageRange(
  3508 + 0, QIntC::to_int(pages.size()) - 1, 0, labels);
3507 3509 for (std::vector<QPDFObjectHandle>::iterator iter = labels.begin();
3508 3510 iter != labels.end(); ++iter)
3509 3511 {
... ... @@ -3869,10 +3871,24 @@ ImageOptimizer::makePipeline(std::string const&amp; description, Pipeline* next)
3869 3871 }
3870 3872 // Files have been seen in the wild whose width and height are
3871 3873 // floating point, which is goofy, but we can deal with it.
3872   - JDIMENSION w = static_cast<JDIMENSION>(
3873   - w_obj.isInteger() ? w_obj.getIntValue() : w_obj.getNumericValue());
3874   - JDIMENSION h = static_cast<JDIMENSION>(
3875   - h_obj.isInteger() ? h_obj.getIntValue() : h_obj.getNumericValue());
  3874 + JDIMENSION w = 0;
  3875 + if (w_obj.isInteger())
  3876 + {
  3877 + w = w_obj.getUIntValueAsUInt();
  3878 + }
  3879 + else
  3880 + {
  3881 + w = static_cast<JDIMENSION>(w_obj.getNumericValue());
  3882 + }
  3883 + JDIMENSION h = 0;
  3884 + if (h_obj.isInteger())
  3885 + {
  3886 + h = h_obj.getUIntValueAsUInt();
  3887 + }
  3888 + else
  3889 + {
  3890 + h = static_cast<JDIMENSION>(h_obj.getNumericValue());
  3891 + }
3876 3892 std::string colorspace = (colorspace_obj.isName() ?
3877 3893 colorspace_obj.getName() :
3878 3894 std::string());
... ... @@ -4119,10 +4135,10 @@ static void validate_under_overlay(QPDF&amp; pdf, UnderOverlay* uo, Options&amp; o)
4119 4135 return;
4120 4136 }
4121 4137 QPDFPageDocumentHelper main_pdh(pdf);
4122   - int main_npages = static_cast<int>(main_pdh.getAllPages().size());
  4138 + int main_npages = QIntC::to_int(main_pdh.getAllPages().size());
4123 4139 uo->pdf = process_file(uo->filename, uo->password, o);
4124 4140 QPDFPageDocumentHelper uo_pdh(*(uo->pdf));
4125   - int uo_npages = static_cast<int>(uo_pdh.getAllPages().size());
  4141 + int uo_npages = QIntC::to_int(uo_pdh.getAllPages().size());
4126 4142 try
4127 4143 {
4128 4144 uo->to_pagenos = QUtil::parse_numrange(uo->to_nr, main_npages);
... ... @@ -4185,7 +4201,7 @@ static void do_under_overlay_for_page(
4185 4201 QPDFPageObjectHelper& dest_page,
4186 4202 bool before)
4187 4203 {
4188   - int pageno = 1 + page_idx;
  4204 + int pageno = 1 + QIntC::to_int(page_idx);
4189 4205 if (! pagenos.count(pageno))
4190 4206 {
4191 4207 return;
... ... @@ -4206,7 +4222,8 @@ static void do_under_overlay_for_page(
4206 4222 {
4207 4223 fo[from_pageno] =
4208 4224 pdf.copyForeignObject(
4209   - pages.at(from_pageno - 1).getFormXObjectForPage());
  4225 + pages.at(QIntC::to_size(from_pageno - 1)).
  4226 + getFormXObjectForPage());
4210 4227 }
4211 4228 // If the same page is overlaid or underlaid multiple times,
4212 4229 // we'll generate multiple names for it, but that's harmless
... ... @@ -4594,7 +4611,8 @@ static void handle_page_specs(QPDF&amp; pdf, Options&amp; o)
4594 4611 int pageno = *pageno_iter - 1;
4595 4612 pldh.getLabelsForPageRange(pageno, pageno, out_pageno,
4596 4613 new_labels);
4597   - QPDFPageObjectHelper to_copy = page_data.orig_pages.at(pageno);
  4614 + QPDFPageObjectHelper to_copy =
  4615 + page_data.orig_pages.at(QIntC::to_size(pageno));
4598 4616 QPDFObjGen to_copy_og = to_copy.getObjectHandle().getObjGen();
4599 4617 unsigned long long from_uuid = page_data.qpdf->getUniqueId();
4600 4618 if (copied_pages[from_uuid].count(to_copy_og))
... ... @@ -4634,7 +4652,7 @@ static void handle_page_specs(QPDF&amp; pdf, Options&amp; o)
4634 4652 // other places, such as the outlines dictionary.
4635 4653 for (size_t pageno = 0; pageno < orig_pages.size(); ++pageno)
4636 4654 {
4637   - if (selected_from_orig.count(pageno) == 0)
  4655 + if (selected_from_orig.count(QIntC::to_int(pageno)) == 0)
4638 4656 {
4639 4657 pdf.replaceObject(
4640 4658 orig_pages.at(pageno).getObjectHandle().getObjGen(),
... ... @@ -4647,7 +4665,7 @@ static void handle_rotations(QPDF&amp; pdf, Options&amp; o)
4647 4665 {
4648 4666 QPDFPageDocumentHelper dh(pdf);
4649 4667 std::vector<QPDFPageObjectHelper> pages = dh.getAllPages();
4650   - int npages = static_cast<int>(pages.size());
  4668 + int npages = QIntC::to_int(pages.size());
4651 4669 for (std::map<std::string, RotationSpec>::iterator iter =
4652 4670 o.rotations.begin();
4653 4671 iter != o.rotations.end(); ++iter)
... ... @@ -4663,7 +4681,8 @@ static void handle_rotations(QPDF&amp; pdf, Options&amp; o)
4663 4681 int pageno = *i2 - 1;
4664 4682 if ((pageno >= 0) && (pageno < npages))
4665 4683 {
4666   - pages.at(pageno).rotatePage(rspec.angle, rspec.relative);
  4684 + pages.at(QIntC::to_size(pageno)).rotatePage(
  4685 + rspec.angle, rspec.relative);
4667 4686 }
4668 4687 }
4669 4688 }
... ... @@ -4957,7 +4976,8 @@ static void write_outfile(QPDF&amp; pdf, Options&amp; o)
4957 4976 if (num_spot != 0)
4958 4977 {
4959 4978 QTC::TC("qpdf", "qpdf split-pages %d");
4960   - before = std::string(o.outfilename, (num_spot - o.outfilename));
  4979 + before = std::string(o.outfilename,
  4980 + QIntC::to_size(num_spot - o.outfilename));
4961 4981 after = num_spot + 2;
4962 4982 }
4963 4983 else if ((len >= 4) &&
... ... @@ -4980,19 +5000,19 @@ static void write_outfile(QPDF&amp; pdf, Options&amp; o)
4980 5000 }
4981 5001 QPDFPageLabelDocumentHelper pldh(pdf);
4982 5002 std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
4983   - int pageno_len = QUtil::int_to_string(pages.size()).length();
4984   - unsigned int num_pages = pages.size();
4985   - for (unsigned int i = 0; i < num_pages; i += o.split_pages)
  5003 + size_t pageno_len = QUtil::uint_to_string(pages.size()).length();
  5004 + size_t num_pages = pages.size();
  5005 + for (size_t i = 0; i < num_pages; i += QIntC::to_size(o.split_pages))
4986 5006 {
4987   - unsigned int first = i + 1;
4988   - unsigned int last = i + o.split_pages;
  5007 + size_t first = i + 1;
  5008 + size_t last = i + QIntC::to_size(o.split_pages);
4989 5009 if (last > num_pages)
4990 5010 {
4991 5011 last = num_pages;
4992 5012 }
4993 5013 QPDF outpdf;
4994 5014 outpdf.emptyPDF();
4995   - for (unsigned int pageno = first; pageno <= last; ++pageno)
  5015 + for (size_t pageno = first; pageno <= last; ++pageno)
4996 5016 {
4997 5017 QPDFObjectHandle page = pages.at(pageno - 1);
4998 5018 outpdf.addPage(page, false);
... ... @@ -5000,17 +5020,22 @@ static void write_outfile(QPDF&amp; pdf, Options&amp; o)
5000 5020 if (pldh.hasPageLabels())
5001 5021 {
5002 5022 std::vector<QPDFObjectHandle> labels;
5003   - pldh.getLabelsForPageRange(first - 1, last - 1, 0, labels);
  5023 + pldh.getLabelsForPageRange(
  5024 + QIntC::to_longlong(first - 1),
  5025 + QIntC::to_longlong(last - 1),
  5026 + 0, labels);
5004 5027 QPDFObjectHandle page_labels =
5005 5028 QPDFObjectHandle::newDictionary();
5006 5029 page_labels.replaceKey(
5007 5030 "/Nums", QPDFObjectHandle::newArray(labels));
5008 5031 outpdf.getRoot().replaceKey("/PageLabels", page_labels);
5009 5032 }
5010   - std::string page_range = QUtil::int_to_string(first, pageno_len);
  5033 + std::string page_range =
  5034 + QUtil::uint_to_string(first, QIntC::to_int(pageno_len));
5011 5035 if (o.split_pages > 1)
5012 5036 {
5013   - page_range += "-" + QUtil::int_to_string(last, pageno_len);
  5037 + page_range += "-" +
  5038 + QUtil::uint_to_string(last, QIntC::to_int(pageno_len));
5014 5039 }
5015 5040 std::string outfile = before + page_range + after;
5016 5041 QPDFWriter w(outpdf, outfile.c_str());
... ... @@ -5117,8 +5142,10 @@ int wmain(int argc, wchar_t* argv[])
5117 5142 for (size_t j = 0; j < wcslen(argv[i]); ++j)
5118 5143 {
5119 5144 unsigned short codepoint = static_cast<unsigned short>(argv[i][j]);
5120   - utf16.append(1, static_cast<unsigned char>(codepoint >> 8));
5121   - utf16.append(1, static_cast<unsigned char>(codepoint & 0xff));
  5145 + utf16.append(1, static_cast<char>(
  5146 + QIntC::to_uchar(codepoint >> 8)));
  5147 + utf16.append(1, static_cast<char>(
  5148 + QIntC::to_uchar(codepoint & 0xff)));
5122 5149 }
5123 5150 std::string utf8 = QUtil::utf16_to_utf8(utf16);
5124 5151 utf8_argv.push_back(
... ... @@ -5131,7 +5158,7 @@ int wmain(int argc, wchar_t* argv[])
5131 5158 {
5132 5159 new_argv[i] = utf8_argv.at(i).getPointer();
5133 5160 }
5134   - argc = static_cast<int>(utf8_argv.size());
  5161 + argc = QIntC::to_int(utf8_argv.size());
5135 5162 new_argv[argc] = 0;
5136 5163 return realmain(argc, new_argv);
5137 5164 }
... ...
qpdf/test_driver.cc
... ... @@ -17,6 +17,7 @@
17 17 #include <qpdf/Pl_Flate.hh>
18 18 #include <qpdf/QPDFWriter.hh>
19 19 #include <qpdf/QPDFSystemError.hh>
  20 +#include <qpdf/QIntC.hh>
20 21 #include <iostream>
21 22 #include <sstream>
22 23 #include <algorithm>
... ... @@ -173,7 +174,7 @@ static void read_file_into_memory(
173 174 {
174 175 FILE* f = QUtil::safe_fopen(filename, "rb");
175 176 fseek(f, 0, SEEK_END);
176   - size = QUtil::tell(f);
  177 + size = QIntC::to_size(QUtil::tell(f));
177 178 fseek(f, 0, SEEK_SET);
178 179 file_buf = PointerHolder<char>(true, new char[size]);
179 180 char* buf_p = file_buf.getPointer();
... ... @@ -280,7 +281,7 @@ void runtest(int n, char const* filename1, char const* arg2)
280 281 char* p = file_buf.getPointer();
281 282 for (size_t i = 0; i < size; ++i)
282 283 {
283   - p[i] ^= 0xcc;
  284 + p[i] = static_cast<char>(p[i] ^ 0xcc);
284 285 }
285 286 pdf.processMemoryFile((std::string(filename1) + ".pdf").c_str(),
286 287 p, size);
... ... @@ -544,8 +545,8 @@ void runtest(int n, char const* filename1, char const* arg2)
544 545 std::string const& name = (*iter).first;
545 546 QPDFObjectHandle image = (*iter).second;
546 547 QPDFObjectHandle dict = image.getDict();
547   - int width = dict.getKey("/Width").getIntValue();
548   - int height = dict.getKey("/Height").getIntValue();
  548 + long long width = dict.getKey("/Width").getIntValue();
  549 + long long height = dict.getKey("/Height").getIntValue();
549 550 std::cout << " " << name
550 551 << ": " << width << " x " << height
551 552 << std::endl;
... ... @@ -929,7 +930,8 @@ void runtest(int n, char const* filename1, char const* arg2)
929 930 page.replaceKey("/Parent", pages);
930 931 pages.replaceKey(
931 932 "/Count",
932   - QPDFObjectHandle::newInteger(1 + all_pages.size()));
  933 + QPDFObjectHandle::newInteger(
  934 + 1 + QIntC::to_longlong(all_pages.size())));
933 935 kids.appendItem(page);
934 936 assert(all_pages.size() == 10);
935 937 pdf.updateAllPagesCache();
... ... @@ -1096,8 +1098,8 @@ void runtest(int n, char const* filename1, char const* arg2)
1096 1098  
1097 1099 // Verify that the previously added reserved keys can be
1098 1100 // dereferenced properly now
1099   - int i1 = res1.getArrayItem(0).getArrayItem(1).getIntValue();
1100   - int i2 = res2.getArrayItem(0).getArrayItem(1).getIntValue();
  1101 + int i1 = res1.getArrayItem(0).getArrayItem(1).getIntValueAsInt();
  1102 + int i2 = res2.getArrayItem(0).getArrayItem(1).getIntValueAsInt();
1101 1103 if ((i1 == 2) && (i2 == 1))
1102 1104 {
1103 1105 std::cout << "circular access and lazy resolution worked" << std::endl;
... ... @@ -1424,7 +1426,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1424 1426 {
1425 1427 std::string t;
1426 1428 for (size_t i = 0;
1427   - i < std::min(data.size(), static_cast<size_t>(20));
  1429 + i < std::min(data.size(), QIntC::to_size(20));
1428 1430 ++i)
1429 1431 {
1430 1432 if ((data.at(i) >= 32) && (data.at(i) <= 126))
... ... @@ -1436,7 +1438,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1436 1438 t += ".";
1437 1439 }
1438 1440 }
1439   - t += " (" + QUtil::int_to_string(data.size()) + " bytes)";
  1441 + t += " (" + QUtil::uint_to_string(data.size()) + " bytes)";
1440 1442 data = t;
1441 1443 }
1442 1444 std::cout << filename << ":\n" << data << "--END--\n";
... ... @@ -1797,7 +1799,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1797 1799 {
1798 1800 // Test page labels.
1799 1801 QPDFPageLabelDocumentHelper pldh(pdf);
1800   - size_t npages = pdf.getRoot().getKey("/Pages").
  1802 + long long npages = pdf.getRoot().getKey("/Pages").
1801 1803 getKey("/Count").getIntValue();
1802 1804 std::vector<QPDFObjectHandle> labels;
1803 1805 pldh.getLabelsForPageRange(0, npages - 1, 1, labels);
... ... @@ -2076,7 +2078,7 @@ void runtest(int n, char const* filename1, char const* arg2)
2076 2078 {
2077 2079 pdf.processMemoryFile("empty", "", 0);
2078 2080 }
2079   - catch (QPDFExc& e)
  2081 + catch (QPDFExc const&)
2080 2082 {
2081 2083 std::cout << "Caught QPDFExc as expected" << std::endl;
2082 2084 }
... ... @@ -2084,7 +2086,7 @@ void runtest(int n, char const* filename1, char const* arg2)
2084 2086 {
2085 2087 QUtil::safe_fopen("/does/not/exist", "r");
2086 2088 }
2087   - catch (QPDFSystemError& e)
  2089 + catch (QPDFSystemError const&)
2088 2090 {
2089 2091 std::cout << "Caught QPDFSystemError as expected" << std::endl;
2090 2092 }
... ... @@ -2092,7 +2094,7 @@ void runtest(int n, char const* filename1, char const* arg2)
2092 2094 {
2093 2095 QUtil::int_to_string_base(0, 12);
2094 2096 }
2095   - catch (std::logic_error& e)
  2097 + catch (std::logic_error const&)
2096 2098 {
2097 2099 std::cout << "Caught logic_error as expected" << std::endl;
2098 2100 }
... ... @@ -2100,7 +2102,7 @@ void runtest(int n, char const* filename1, char const* arg2)
2100 2102 {
2101 2103 QUtil::toUTF8(0xffffffff);
2102 2104 }
2103   - catch (std::runtime_error& e)
  2105 + catch (std::runtime_error const&)
2104 2106 {
2105 2107 std::cout << "Caught runtime_error as expected" << std::endl;
2106 2108 }
... ... @@ -2110,12 +2112,12 @@ void runtest(int n, char const* filename1, char const* arg2)
2110 2112 // Test int size checks. This test will fail if int and long
2111 2113 // long are the same size.
2112 2114 QPDFObjectHandle t = pdf.getTrailer();
2113   - unsigned long long q1_l = 3L * INT_MAX;
2114   - long long q1 = static_cast<long long>(q1_l);
2115   - long long q2_l = 3L * INT_MIN;
2116   - long long q2 = static_cast<long long>(q2_l);
  2115 + unsigned long long q1_l = 3ULL * QIntC::to_ulonglong(INT_MAX);
  2116 + long long q1 = QIntC::to_longlong(q1_l);
  2117 + long long q2_l = 3LL * QIntC::to_longlong(INT_MIN);
  2118 + long long q2 = QIntC::to_longlong(q2_l);
2117 2119 unsigned int q3_i = UINT_MAX;
2118   - long long q3 = static_cast<long long>(q3_i);
  2120 + long long q3 = QIntC::to_longlong(q3_i);
2119 2121 t.replaceKey("/Q1", QPDFObjectHandle::newInteger(q1));
2120 2122 t.replaceKey("/Q2", QPDFObjectHandle::newInteger(q2));
2121 2123 t.replaceKey("/Q3", QPDFObjectHandle::newInteger(q3));
... ...
qpdf/test_large_file.cc
... ... @@ -9,6 +9,7 @@
9 9 #include <qpdf/QPDFWriter.hh>
10 10 #include <qpdf/QPDFObjectHandle.hh>
11 11 #include <qpdf/QUtil.hh>
  12 +#include <qpdf/QIntC.hh>
12 13 #include <iostream>
13 14 #include <string.h>
14 15 #include <stdlib.h>
... ... @@ -36,37 +37,39 @@
36 37 static char const* whoami = 0;
37 38  
38 39 // Height should be a multiple of 10
39   -static int const nstripes = 10;
40   -static int const stripesize_large = 500;
41   -static int const stripesize_small = 5;
42   -static int const npages = 200;
  40 +static size_t const nstripes = 10;
  41 +static size_t const stripesize_large = 500;
  42 +static size_t const stripesize_small = 5;
  43 +static size_t const npages = 200;
43 44  
44 45 // initialized in main
45   -int stripesize = 0;
46   -int width = 0;
47   -int height = 0;
  46 +size_t stripesize = 0;
  47 +size_t width = 0;
  48 +size_t height = 0;
48 49 static unsigned char* buf = 0;
49 50  
50   -static inline unsigned char get_pixel_color(int n, size_t row)
  51 +static inline unsigned char get_pixel_color(size_t n, size_t row)
51 52 {
52   - return (n & (1 << (nstripes - 1 - row))) ? '\xc0' : '\x40';
  53 + return ((n & (1LLU << (nstripes - 1LLU - row)))
  54 + ? static_cast<unsigned char>('\xc0')
  55 + : static_cast<unsigned char>('\x40'));
53 56 }
54 57  
55 58 class ImageChecker: public Pipeline
56 59 {
57 60 public:
58   - ImageChecker(int n);
  61 + ImageChecker(size_t n);
59 62 virtual ~ImageChecker();
60 63 virtual void write(unsigned char* data, size_t len);
61 64 virtual void finish();
62 65  
63 66 private:
64   - int n;
  67 + size_t n;
65 68 size_t offset;
66 69 bool okay;
67 70 };
68 71  
69   -ImageChecker::ImageChecker(int n) :
  72 +ImageChecker::ImageChecker(size_t n) :
70 73 Pipeline("image checker", 0),
71 74 n(n),
72 75 offset(0),
... ... @@ -106,16 +109,16 @@ ImageChecker::finish()
106 109 class ImageProvider: public QPDFObjectHandle::StreamDataProvider
107 110 {
108 111 public:
109   - ImageProvider(int n);
  112 + ImageProvider(size_t n);
110 113 virtual ~ImageProvider();
111 114 virtual void provideStreamData(int objid, int generation,
112 115 Pipeline* pipeline);
113 116  
114 117 private:
115   - int n;
  118 + size_t n;
116 119 };
117 120  
118   -ImageProvider::ImageProvider(int n) :
  121 +ImageProvider::ImageProvider(size_t n) :
119 122 n(n)
120 123 {
121 124 }
... ... @@ -133,7 +136,7 @@ ImageProvider::provideStreamData(int objid, int generation,
133 136 buf = new unsigned char[width * stripesize];
134 137 }
135 138 std::cout << "page " << n << " of " << npages << std::endl;
136   - for (int y = 0; y < nstripes; ++y)
  139 + for (size_t y = 0; y < nstripes; ++y)
137 140 {
138 141 unsigned char color = get_pixel_color(n, y);
139 142 memset(buf, color, width * stripesize);
... ... @@ -156,16 +159,16 @@ static void set_parameters(bool large)
156 159 width = height;
157 160 }
158 161  
159   -std::string generate_page_contents(int pageno)
  162 +std::string generate_page_contents(size_t pageno)
160 163 {
161 164 std::string contents =
162   - "BT /F1 24 Tf 72 720 Td (page " + QUtil::int_to_string(pageno) +
  165 + "BT /F1 24 Tf 72 720 Td (page " + QUtil::uint_to_string(pageno) +
163 166 ") Tj ET\n"
164 167 "q 468 0 0 468 72 72 cm /Im1 Do Q\n";
165 168 return contents;
166 169 }
167 170  
168   -static QPDFObjectHandle create_page_contents(QPDF& pdf, int pageno)
  171 +static QPDFObjectHandle create_page_contents(QPDF& pdf, size_t pageno)
169 172 {
170 173 return QPDFObjectHandle::newStream(&pdf, generate_page_contents(pageno));
171 174 }
... ... @@ -175,9 +178,9 @@ QPDFObjectHandle newName(std::string const&amp; name)
175 178 return QPDFObjectHandle::newName(name);
176 179 }
177 180  
178   -QPDFObjectHandle newInteger(int val)
  181 +QPDFObjectHandle newInteger(size_t val)
179 182 {
180   - return QPDFObjectHandle::newInteger(val);
  183 + return QPDFObjectHandle::newInteger(QIntC::to_longlong(val));
181 184 }
182 185  
183 186 static void create_pdf(char const* filename)
... ... @@ -210,7 +213,7 @@ static void create_pdf(char const* filename)
210 213 mediabox.appendItem(newInteger(792));
211 214  
212 215 QPDFPageDocumentHelper dh(pdf);
213   - for (int pageno = 1; pageno <= npages; ++pageno)
  216 + for (size_t pageno = 1; pageno <= npages; ++pageno)
214 217 {
215 218 QPDFObjectHandle image = QPDFObjectHandle::newStream(&pdf);
216 219 QPDFObjectHandle image_dict = image.getDict();
... ... @@ -253,7 +256,7 @@ static void create_pdf(char const* filename)
253 256 w.write();
254 257 }
255 258  
256   -static void check_page_contents(int pageno, QPDFObjectHandle page)
  259 +static void check_page_contents(size_t pageno, QPDFObjectHandle page)
257 260 {
258 261 PointerHolder<Buffer> buf =
259 262 page.getKey("/Contents").getStreamData();
... ... @@ -270,7 +273,7 @@ static void check_page_contents(int pageno, QPDFObjectHandle page)
270 273 }
271 274 }
272 275  
273   -static void check_image(int pageno, QPDFObjectHandle page)
  276 +static void check_image(size_t pageno, QPDFObjectHandle page)
274 277 {
275 278 QPDFObjectHandle image =
276 279 page.getKey("/Resources").getKey("/XObject").getKey("/Im1");
... ... @@ -283,10 +286,10 @@ static void check_pdf(char const* filename)
283 286 QPDF pdf;
284 287 pdf.processFile(filename);
285 288 std::vector<QPDFObjectHandle> const& pages = pdf.getAllPages();
286   - assert(pages.size() == static_cast<size_t>(npages));
287   - for (int i = 0; i < npages; ++i)
  289 + assert(pages.size() == QIntC::to_size(npages));
  290 + for (size_t i = 0; i < npages; ++i)
288 291 {
289   - int pageno = i + 1;
  292 + size_t pageno = i + 1;
290 293 std::cout << "page " << pageno << " of " << npages << std::endl;
291 294 check_page_contents(pageno, pages.at(i));
292 295 check_image(pageno, pages.at(i));
... ...
qpdf/test_tokenizer.cc
... ... @@ -6,6 +6,7 @@
6 6 #include <qpdf/BufferInputSource.hh>
7 7 #include <qpdf/QPDF.hh>
8 8 #include <qpdf/Pl_Buffer.hh>
  9 +#include <qpdf/QIntC.hh>
9 10 #include <stdlib.h>
10 11 #include <stdio.h>
11 12 #include <string.h>
... ... @@ -46,7 +47,7 @@ Finder::check()
46 47 QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true);
47 48 qpdf_offset_t offset = this->is->tell();
48 49 bool result = (t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, str));
49   - this->is->seek(offset - this->str.length(), SEEK_SET);
  50 + this->is->seek(offset - QIntC::to_offset(this->str.length()), SEEK_SET);
50 51 return result;
51 52 }
52 53  
... ... @@ -286,7 +287,7 @@ int main(int argc, char* argv[])
286 287 {
287 288 usage();
288 289 }
289   - max_len = QUtil::string_to_int(argv[i]);
  290 + max_len = QUtil::string_to_uint(argv[i]);
290 291 }
291 292 else if (strcmp(argv[i], "-no-ignorable") == 0)
292 293 {
... ...