Commit 12f1eb15ca3fed6310402847559a7c99d3c77847

Authored by Jay Berkenbilt
1 parent f20fa61e

Programmatically apply new formatting to code

Run this:

for i in  **/*.cc **/*.c **/*.h **/*.hh; do
  clang-format < $i >| $i.new && mv $i.new $i
done
Showing 244 changed files with 15225 additions and 18966 deletions

Too many changes.

To preserve performance only 100 of 244 files are displayed.

examples/pdf-attach-file.cc
1 1 #include <qpdf/QPDF.hh>
2   -#include <qpdf/QUtil.hh>
3   -#include <qpdf/QPDFWriter.hh>
4 2 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
5 3 #include <qpdf/QPDFFileSpecObjectHelper.hh>
  4 +#include <qpdf/QPDFWriter.hh>
  5 +#include <qpdf/QUtil.hh>
6 6  
7   -#include <iostream>
8 7 #include <cstring>
  8 +#include <iostream>
9 9  
10 10 //
11 11 // This example attaches a file to an input file, adds a page to the
... ... @@ -17,9 +17,11 @@
17 17  
18 18 static char const* whoami = 0;
19 19  
20   -static void usage(std::string const& msg)
  20 +static void
  21 +usage(std::string const& msg)
21 22 {
22   - std::cerr << msg << std::endl << std::endl
  23 + std::cerr << msg << std::endl
  24 + << std::endl
23 25 << "Usage: " << whoami << " options" << std::endl
24 26 << "Options:" << std::endl
25 27 << " --infile infile.pdf" << std::endl
... ... @@ -30,33 +32,36 @@ static void usage(std::string const&amp; msg)
30 32 exit(2);
31 33 }
32 34  
33   -static void process(char const* infilename, char const* password,
34   - char const* attachment, char const* mimetype,
35   - char const* outfilename)
  35 +static void
  36 +process(
  37 + char const* infilename,
  38 + char const* password,
  39 + char const* attachment,
  40 + char const* mimetype,
  41 + char const* outfilename)
36 42 {
37 43 QPDF q;
38 44 q.processFile(infilename, password);
39 45  
40 46 // Create an indirect object for the built-in Helvetica font. This
41 47 // uses the qpdf literal syntax introduced in qpdf 10.6.
42   - auto f1 = q.makeIndirectObject(
43   - "<<"
44   - " /Type /Font"
45   - " /Subtype /Type1"
46   - " /Name /F1"
47   - " /BaseFont /Helvetica"
48   - " /Encoding /WinAnsiEncoding"
49   - ">>"_qpdf);
  48 + auto f1 = q.makeIndirectObject("<<"
  49 + " /Type /Font"
  50 + " /Subtype /Type1"
  51 + " /Name /F1"
  52 + " /BaseFont /Helvetica"
  53 + " /Encoding /WinAnsiEncoding"
  54 + ">>"_qpdf);
50 55  
51 56 // Create a resources dictionary with fonts. This uses the new
52 57 // parse introduced in qpdf 10.2 that takes a QPDF* and allows
53 58 // indirect object references.
54   - auto resources = q.makeIndirectObject(
55   - QPDFObjectHandle::parse(
56   - &q,
57   - "<<"
58   - " /Font <<"
59   - " /F1 " + f1.unparse() +
  59 + auto resources = q.makeIndirectObject(QPDFObjectHandle::parse(
  60 + &q,
  61 + "<<"
  62 + " /Font <<"
  63 + " /F1 " +
  64 + f1.unparse() +
60 65 " >>"
61 66 ">>"));
62 67  
... ... @@ -66,8 +71,7 @@ static void process(char const* infilename, char const* password,
66 71 << std::endl;
67 72 auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);
68 73  
69   - if (mimetype)
70   - {
  74 + if (mimetype) {
71 75 // Get an embedded file stream and set mimetype
72 76 auto ef = QPDFEFStreamObjectHelper(fs.getEmbeddedFileStream());
73 77 ef.setSubtype(mimetype);
... ... @@ -102,17 +106,16 @@ static void process(char const* infilename, char const* password,
102 106 apdict.replaceKey("/Type", "/XObject"_qpdf);
103 107 apdict.replaceKey("/Subtype", "/Form"_qpdf);
104 108 apdict.replaceKey("/BBox", "[ 0 0 20 20 ]"_qpdf);
105   - auto annot = q.makeIndirectObject(
106   - QPDFObjectHandle::parse(
107   - &q,
108   - "<<"
109   - " /AP <<"
110   - " /N " + ap.unparse() +
  109 + auto annot = q.makeIndirectObject(QPDFObjectHandle::parse(
  110 + &q,
  111 + "<<"
  112 + " /AP <<"
  113 + " /N " +
  114 + ap.unparse() +
111 115 " >>"
112   - " /Contents "
113   - + QPDFObjectHandle::newUnicodeString(attachment).unparse() +
114   - " /FS " + fs.getObjectHandle().unparse() +
115   - " /NM " +
  116 + " /Contents " +
  117 + QPDFObjectHandle::newUnicodeString(attachment).unparse() +
  118 + " /FS " + fs.getObjectHandle().unparse() + " /NM " +
116 119 QPDFObjectHandle::newUnicodeString(attachment).unparse() +
117 120 " /Rect [ 72 700 92 720 ]"
118 121 " /Subtype /FileAttachment"
... ... @@ -134,12 +137,16 @@ static void process(char const* infilename, char const* password,
134 137 auto page = QPDFObjectHandle::parse(
135 138 &q,
136 139 "<<"
137   - " /Annots [ " + annot.unparse() + " ]"
138   - " /Contents " + contents.unparse() +
139   - " /MediaBox [0 0 612 792]"
140   - " /Resources " + resources.unparse() +
141   - " /Type /Page"
142   - ">>");
  140 + " /Annots [ " +
  141 + annot.unparse() +
  142 + " ]"
  143 + " /Contents " +
  144 + contents.unparse() +
  145 + " /MediaBox [0 0 612 792]"
  146 + " /Resources " +
  147 + resources.unparse() +
  148 + " /Type /Page"
  149 + ">>");
143 150  
144 151 // Add the page.
145 152 q.addPage(page, true);
... ... @@ -151,7 +158,8 @@ static void process(char const* infilename, char const* password,
151 158 w.write();
152 159 }
153 160  
154   -int main(int argc, char* argv[])
  161 +int
  162 +main(int argc, char* argv[])
155 163 {
156 164 whoami = QUtil::getWhoami(argv[0]);
157 165  
... ... @@ -162,64 +170,46 @@ int main(int argc, char* argv[])
162 170 char const* mimetype = 0;
163 171  
164 172 auto check_arg = [](char const* arg, std::string const& msg) {
165   - if (arg == nullptr)
166   - {
  173 + if (arg == nullptr) {
167 174 usage(msg);
168 175 }
169 176 };
170 177  
171   - for (int i = 1; i < argc; ++i)
172   - {
  178 + for (int i = 1; i < argc; ++i) {
173 179 char* arg = argv[i];
174   - char* next = argv[i+1];
175   - if (strcmp(arg, "--infile") == 0)
176   - {
  180 + char* next = argv[i + 1];
  181 + if (strcmp(arg, "--infile") == 0) {
177 182 check_arg(next, "--infile takes an argument");
178 183 infilename = next;
179 184 ++i;
180   - }
181   - else if (strcmp(arg, "--password") == 0)
182   - {
  185 + } else if (strcmp(arg, "--password") == 0) {
183 186 check_arg(next, "--password takes an argument");
184 187 password = next;
185 188 ++i;
186   - }
187   - else if (strcmp(arg, "--attachment") == 0)
188   - {
  189 + } else if (strcmp(arg, "--attachment") == 0) {
189 190 check_arg(next, "--attachment takes an argument");
190 191 attachment = next;
191 192 ++i;
192   - }
193   - else if (strcmp(arg, "--outfile") == 0)
194   - {
  193 + } else if (strcmp(arg, "--outfile") == 0) {
195 194 check_arg(next, "--outfile takes an argument");
196 195 outfilename = next;
197 196 ++i;
198   - }
199   - else if (strcmp(arg, "--mimetype") == 0)
200   - {
  197 + } else if (strcmp(arg, "--mimetype") == 0) {
201 198 check_arg(next, "--mimetype takes an argument");
202 199 mimetype = next;
203 200 ++i;
204   - }
205   - else
206   - {
  201 + } else {
207 202 usage("unknown argument " + std::string(arg));
208 203 }
209 204 }
210   - if (! (infilename && attachment && outfilename))
211   - {
  205 + if (!(infilename && attachment && outfilename)) {
212 206 usage("required arguments were not provided");
213 207 }
214 208  
215   - try
216   - {
  209 + try {
217 210 process(infilename, password, attachment, mimetype, outfilename);
218   - }
219   - catch (std::exception &e)
220   - {
221   - std::cerr << whoami << " exception: "
222   - << e.what() << std::endl;
  211 + } catch (std::exception& e) {
  212 + std::cerr << whoami << " exception: " << e.what() << std::endl;
223 213 exit(2);
224 214 }
225 215  
... ...
examples/pdf-bookmarks.cc
1   -#include <iostream>
2   -#include <string.h>
3   -#include <stdlib.h>
  1 +#include <qpdf/QIntC.hh>
4 2 #include <qpdf/QPDF.hh>
5   -#include <qpdf/QPDFPageDocumentHelper.hh>
6 3 #include <qpdf/QPDFOutlineDocumentHelper.hh>
7   -#include <qpdf/QUtil.hh>
8   -#include <qpdf/QIntC.hh>
  4 +#include <qpdf/QPDFPageDocumentHelper.hh>
9 5 #include <qpdf/QTC.hh>
  6 +#include <qpdf/QUtil.hh>
  7 +#include <iostream>
  8 +#include <stdlib.h>
  9 +#include <string.h>
10 10  
11 11 // This program demonstrates extraction of bookmarks using the qpdf
12 12 // outlines API. Note that all the information shown by this program
... ... @@ -20,67 +20,62 @@ static bool show_open = false;
20 20 static bool show_targets = false;
21 21 static std::map<QPDFObjGen, int> page_map;
22 22  
23   -void usage()
  23 +void
  24 +usage()
24 25 {
25   - std::cerr << "Usage: " << whoami << " [options] file.pdf [password]"
26   - << std::endl
27   - << "Options:" << std::endl
28   - << " --numbers give bookmarks outline-style numbers"
29   - << std::endl
30   - << " --lines draw lines to show bookmark hierarchy"
31   - << std::endl
32   - << " --show-open indicate whether a bookmark is initially open"
33   - << std::endl
34   - << " --show-targets show target if possible"
35   - << std::endl;
  26 + std::cerr
  27 + << "Usage: " << whoami << " [options] file.pdf [password]" << std::endl
  28 + << "Options:" << std::endl
  29 + << " --numbers give bookmarks outline-style numbers"
  30 + << std::endl
  31 + << " --lines draw lines to show bookmark hierarchy"
  32 + << std::endl
  33 + << " --show-open indicate whether a bookmark is initially open"
  34 + << std::endl
  35 + << " --show-targets show target if possible" << std::endl;
36 36 exit(2);
37 37 }
38 38  
39   -void print_lines(std::vector<int>& numbers)
  39 +void
  40 +print_lines(std::vector<int>& numbers)
40 41 {
41   - for (unsigned int i = 0; i < numbers.size() - 1; ++i)
42   - {
43   - if (numbers.at(i))
44   - {
  42 + for (unsigned int i = 0; i < numbers.size() - 1; ++i) {
  43 + if (numbers.at(i)) {
45 44 std::cout << "| ";
46   - }
47   - else
48   - {
  45 + } else {
49 46 std::cout << " ";
50 47 }
51 48 }
52 49 }
53 50  
54   -void generate_page_map(QPDF& qpdf)
  51 +void
  52 +generate_page_map(QPDF& qpdf)
55 53 {
56 54 QPDFPageDocumentHelper dh(qpdf);
57 55 int n = 0;
58   - for (auto const& page : dh.getAllPages())
59   - {
  56 + for (auto const& page : dh.getAllPages()) {
60 57 page_map[page.getObjectHandle().getObjGen()] = ++n;
61 58 }
62 59 }
63 60  
64   -void show_bookmark_details(QPDFOutlineObjectHelper outline,
65   - std::vector<int> numbers)
  61 +void
  62 +show_bookmark_details(QPDFOutlineObjectHelper outline, std::vector<int> numbers)
66 63 {
67 64 // No default so gcc will warn on missing tag
68   - switch (style)
69   - {
70   - case st_none:
  65 + switch (style) {
  66 + case st_none:
71 67 QTC::TC("examples", "pdf-bookmarks none");
72 68 break;
73 69  
74   - case st_numbers:
  70 + case st_numbers:
75 71 QTC::TC("examples", "pdf-bookmarks numbers");
76   - for (auto const& number : numbers)
77   - {
  72 + for (auto const& number : numbers) {
78 73 std::cout << number << ".";
79 74 }
80 75 std::cout << " ";
81 76 break;
82 77  
83   - case st_lines:
  78 + case st_lines:
84 79 QTC::TC("examples", "pdf-bookmarks lines");
85 80 print_lines(numbers);
86 81 std::cout << "|" << std::endl;
... ... @@ -89,42 +84,32 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline,
89 84 break;
90 85 }
91 86  
92   - if (show_open)
93   - {
  87 + if (show_open) {
94 88 int count = outline.getCount();
95   - if (count)
96   - {
  89 + if (count) {
97 90 QTC::TC("examples", "pdf-bookmarks has count");
98   - if (count > 0)
99   - {
  91 + if (count > 0) {
100 92 // hierarchy is open at this point
101 93 QTC::TC("examples", "pdf-bookmarks open");
102 94 std::cout << "(v) ";
103   - }
104   - else
105   - {
  95 + } else {
106 96 QTC::TC("examples", "pdf-bookmarks closed");
107 97 std::cout << "(>) ";
108 98 }
109   - }
110   - else
111   - {
  99 + } else {
112 100 QTC::TC("examples", "pdf-bookmarks no count");
113 101 std::cout << "( ) ";
114 102 }
115 103 }
116 104  
117   - if (show_targets)
118   - {
  105 + if (show_targets) {
119 106 QTC::TC("examples", "pdf-bookmarks targets");
120 107 std::string target = "unknown";
121 108 QPDFObjectHandle dest_page = outline.getDestPage();
122   - if (! dest_page.isNull())
123   - {
  109 + if (!dest_page.isNull()) {
124 110 QTC::TC("examples", "pdf-bookmarks dest");
125 111 QPDFObjGen og = dest_page.getObjGen();
126   - if (page_map.count(og))
127   - {
  112 + if (page_map.count(og)) {
128 113 target = QUtil::int_to_string(page_map[og]);
129 114 }
130 115 }
... ... @@ -134,8 +119,9 @@ void show_bookmark_details(QPDFOutlineObjectHelper outline,
134 119 std::cout << outline.getTitle() << std::endl;
135 120 }
136 121  
137   -void extract_bookmarks(std::vector<QPDFOutlineObjectHelper> outlines,
138   - std::vector<int>& numbers)
  122 +void
  123 +extract_bookmarks(
  124 + std::vector<QPDFOutlineObjectHelper> outlines, std::vector<int>& numbers)
139 125 {
140 126 // For style == st_numbers, numbers.at(n) contains the numerical
141 127 // label for the outline, so we count up from 1.
... ... @@ -144,8 +130,7 @@ void extract_bookmarks(std::vector&lt;QPDFOutlineObjectHelper&gt; outlines,
144 130 // is, so we count up to zero.
145 131 numbers.push_back(
146 132 (style == st_lines) ? -QIntC::to_int(outlines.size()) : 0);
147   - for (auto& outline : outlines)
148   - {
  133 + for (auto& outline : outlines) {
149 134 ++(numbers.back());
150 135 show_bookmark_details(outline, numbers);
151 136 extract_bookmarks(outline.getKids(), numbers);
... ... @@ -153,87 +138,64 @@ void extract_bookmarks(std::vector&lt;QPDFOutlineObjectHelper&gt; outlines,
153 138 numbers.pop_back();
154 139 }
155 140  
156   -int main(int argc, char* argv[])
  141 +int
  142 +main(int argc, char* argv[])
157 143 {
158 144 whoami = QUtil::getWhoami(argv[0]);
159 145  
160   - if ((argc == 2) && (strcmp(argv[1], "--version") == 0))
161   - {
  146 + if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) {
162 147 std::cout << whoami << " version 1.5" << std::endl;
163 148 exit(0);
164 149 }
165 150  
166 151 int arg;
167   - for (arg = 1; arg < argc; ++arg)
168   - {
169   - if (argv[arg][0] == '-')
170   - {
171   - if (strcmp(argv[arg], "--numbers") == 0)
172   - {
  152 + for (arg = 1; arg < argc; ++arg) {
  153 + if (argv[arg][0] == '-') {
  154 + if (strcmp(argv[arg], "--numbers") == 0) {
173 155 style = st_numbers;
174   - }
175   - else if (strcmp(argv[arg], "--lines") == 0)
176   - {
  156 + } else if (strcmp(argv[arg], "--lines") == 0) {
177 157 style = st_lines;
178   - }
179   - else if (strcmp(argv[arg], "--show-open") == 0)
180   - {
  158 + } else if (strcmp(argv[arg], "--show-open") == 0) {
181 159 show_open = true;
182   - }
183   - else if (strcmp(argv[arg], "--show-targets") == 0)
184   - {
  160 + } else if (strcmp(argv[arg], "--show-targets") == 0) {
185 161 show_targets = true;
186   - }
187   - else
188   - {
  162 + } else {
189 163 usage();
190 164 }
191   - }
192   - else
193   - {
  165 + } else {
194 166 break;
195 167 }
196 168 }
197 169  
198   - if (arg >= argc)
199   - {
  170 + if (arg >= argc) {
200 171 usage();
201 172 }
202 173  
203 174 char const* filename = argv[arg++];
204 175 char const* password = "";
205 176  
206   - if (arg < argc)
207   - {
  177 + if (arg < argc) {
208 178 password = argv[arg++];
209 179 }
210   - if (arg != argc)
211   - {
  180 + if (arg != argc) {
212 181 usage();
213 182 }
214 183  
215   - try
216   - {
  184 + try {
217 185 QPDF qpdf;
218 186 qpdf.processFile(filename, password);
219 187  
220 188 QPDFOutlineDocumentHelper odh(qpdf);
221   - if (odh.hasOutlines())
222   - {
  189 + if (odh.hasOutlines()) {
223 190 std::vector<int> numbers;
224   - if (show_targets)
225   - {
  191 + if (show_targets) {
226 192 generate_page_map(qpdf);
227 193 }
228 194 extract_bookmarks(odh.getTopLevelOutlines(), numbers);
229   - }
230   - else
231   - {
  195 + } else {
232 196 std::cout << filename << " has no bookmarks" << std::endl;
233 197 }
234   - }
235   - catch (std::exception &e)
236   - {
  198 + } catch (std::exception& e) {
237 199 std::cerr << whoami << " processing file " << filename << ": "
238 200 << e.what() << std::endl;
239 201 exit(2);
... ...
examples/pdf-c-objects.c
... ... @@ -10,7 +10,8 @@
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -static void usage()
  13 +static void
  14 +usage()
14 15 {
15 16 fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami);
16 17 exit(2);
... ... @@ -33,12 +34,9 @@ modify_file(qpdf_data qpdf)
33 34 /* 0 is never a valid qpdf_oh */
34 35 qpdf_oh pagemode = 0;
35 36 if (qpdf_oh_is_dictionary(
36   - qpdf, qpdf_oh_get_key(qpdf, root, "/PageLabels")))
37   - {
  37 + qpdf, qpdf_oh_get_key(qpdf, root, "/PageLabels"))) {
38 38 pagemode = qpdf_oh_new_name(qpdf, "/UseOutlines");
39   - }
40   - else
41   - {
  39 + } else {
42 40 pagemode = qpdf_oh_new_null(qpdf);
43 41 }
44 42 qpdf_oh_replace_or_remove_key(qpdf, root, "/PageMode", pagemode);
... ... @@ -46,7 +44,8 @@ modify_file(qpdf_data qpdf)
46 44 return QPDF_TRUE;
47 45 }
48 46  
49   -int main(int argc, char* argv[])
  47 +int
  48 +main(int argc, char* argv[])
50 49 {
51 50 char* infile = NULL;
52 51 char* password = NULL;
... ... @@ -56,21 +55,15 @@ int main(int argc, char* argv[])
56 55 int errors = 0;
57 56 char* p = 0;
58 57  
59   - if ((p = strrchr(argv[0], '/')) != NULL)
60   - {
  58 + if ((p = strrchr(argv[0], '/')) != NULL) {
61 59 whoami = p + 1;
62   - }
63   - else if ((p = strrchr(argv[0], '\\')) != NULL)
64   - {
  60 + } else if ((p = strrchr(argv[0], '\\')) != NULL) {
65 61 whoami = p + 1;
66   - }
67   - else
68   - {
  62 + } else {
69 63 whoami = argv[0];
70 64 }
71 65  
72   - if (argc != 4)
73   - {
  66 + if (argc != 4) {
74 67 usage();
75 68 }
76 69  
... ... @@ -80,33 +73,29 @@ int main(int argc, char* argv[])
80 73  
81 74 if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) &&
82 75 modify_file(qpdf) &&
83   - ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0))
84   - {
  76 + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) {
85 77 /* Use static ID for testing only. For production, a
86 78 * non-static ID is used. See also
87 79 * qpdf_set_deterministic_ID. */
88 80 qpdf_set_static_ID(qpdf, QPDF_TRUE); /* for testing only */
89 81 qpdf_write(qpdf);
90 82 }
91   - while (qpdf_more_warnings(qpdf))
92   - {
  83 + while (qpdf_more_warnings(qpdf)) {
93 84 warnings = 1;
94   - printf("warning: %s\n",
95   - qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf)));
  85 + printf(
  86 + "warning: %s\n",
  87 + qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf)));
96 88 }
97   - if (qpdf_has_error(qpdf))
98   - {
  89 + if (qpdf_has_error(qpdf)) {
99 90 errors = 1;
100   - printf("error: %s\n",
101   - qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
  91 + printf(
  92 + "error: %s\n",
  93 + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
102 94 }
103 95 qpdf_cleanup(&qpdf);
104   - if (errors)
105   - {
  96 + if (errors) {
106 97 return 2;
107   - }
108   - else if (warnings)
109   - {
  98 + } else if (warnings) {
110 99 return 3;
111 100 }
112 101  
... ...
examples/pdf-count-strings.cc
... ... @@ -5,23 +5,23 @@
5 5 //
6 6  
7 7 #include <iostream>
8   -#include <string.h>
9 8 #include <stdlib.h>
  9 +#include <string.h>
10 10  
  11 +#include <qpdf/Pl_StdioFile.hh>
11 12 #include <qpdf/QPDF.hh>
  13 +#include <qpdf/QPDFObjectHandle.hh>
12 14 #include <qpdf/QPDFPageDocumentHelper.hh>
13 15 #include <qpdf/QPDFPageObjectHelper.hh>
14 16 #include <qpdf/QUtil.hh>
15   -#include <qpdf/QPDFObjectHandle.hh>
16   -#include <qpdf/Pl_StdioFile.hh>
17 17  
18 18 static char const* whoami = 0;
19 19  
20   -void usage()
  20 +void
  21 +usage()
21 22 {
22 23 std::cerr << "Usage: " << whoami << " infile" << std::endl
23   - << "Applies token filters to infile"
24   - << std::endl;
  24 + << "Applies token filters to infile" << std::endl;
25 25 exit(2);
26 26 }
27 27  
... ... @@ -47,8 +47,7 @@ void
47 47 StringCounter::handleToken(QPDFTokenizer::Token const& token)
48 48 {
49 49 // Count string tokens
50   - if (token.getType() == QPDFTokenizer::tt_string)
51   - {
  50 + if (token.getType() == QPDFTokenizer::tt_string) {
52 51 ++this->count;
53 52 }
54 53 // Preserve input verbatim by passing each token to any specified
... ... @@ -71,36 +70,31 @@ StringCounter::getCount() const
71 70 return this->count;
72 71 }
73 72  
74   -int main(int argc, char* argv[])
  73 +int
  74 +main(int argc, char* argv[])
75 75 {
76 76 whoami = QUtil::getWhoami(argv[0]);
77 77  
78   - if (argc != 2)
79   - {
  78 + if (argc != 2) {
80 79 usage();
81 80 }
82 81 char const* infilename = argv[1];
83 82  
84   - try
85   - {
  83 + try {
86 84 QPDF pdf;
87 85 pdf.processFile(infilename);
88 86 int pageno = 0;
89   - for (auto& page : QPDFPageDocumentHelper(pdf).getAllPages())
90   - {
  87 + for (auto& page : QPDFPageDocumentHelper(pdf).getAllPages()) {
91 88 ++pageno;
92 89 // Pass the contents of a page through our string counter.
93 90 // If it's an even page, capture the output. This
94 91 // illustrates that you may capture any output generated
95 92 // by the filter, or you may ignore it.
96 93 StringCounter counter;
97   - if (pageno % 2)
98   - {
  94 + if (pageno % 2) {
99 95 // Ignore output for odd pages.
100 96 page.filterContents(&counter);
101   - }
102   - else
103   - {
  97 + } else {
104 98 // Write output to stdout for even pages.
105 99 Pl_StdioFile out("stdout", stdout);
106 100 std::cout << "% Contents of page " << pageno << std::endl;
... ... @@ -110,9 +104,7 @@ int main(int argc, char* argv[])
110 104 std::cout << "Page " << pageno
111 105 << ": strings = " << counter.getCount() << std::endl;
112 106 }
113   - }
114   - catch (std::exception& e)
115   - {
  107 + } catch (std::exception& e) {
116 108 std::cerr << whoami << ": " << e.what() << std::endl;
117 109 exit(2);
118 110 }
... ...
examples/pdf-create.cc
... ... @@ -5,20 +5,20 @@
5 5 // StreamDataProvider with different types of filters.
6 6 //
7 7  
  8 +#include <qpdf/Pl_Buffer.hh>
  9 +#include <qpdf/Pl_DCT.hh>
  10 +#include <qpdf/Pl_RunLength.hh>
  11 +#include <qpdf/QIntC.hh>
8 12 #include <qpdf/QPDF.hh>
  13 +#include <qpdf/QPDFObjectHandle.hh>
9 14 #include <qpdf/QPDFPageDocumentHelper.hh>
10 15 #include <qpdf/QPDFPageObjectHelper.hh>
11 16 #include <qpdf/QPDFWriter.hh>
12   -#include <qpdf/QPDFObjectHandle.hh>
13 17 #include <qpdf/QUtil.hh>
14   -#include <qpdf/Pl_Buffer.hh>
15   -#include <qpdf/Pl_RunLength.hh>
16   -#include <qpdf/Pl_DCT.hh>
17   -#include <qpdf/QIntC.hh>
18 18 #include <iostream>
19 19 #include <memory>
20   -#include <string.h>
21 20 #include <stdlib.h>
  21 +#include <string.h>
22 22  
23 23 static char const* whoami = 0;
24 24  
... ... @@ -27,11 +27,10 @@ static char const* whoami = 0;
27 27 class ImageProvider: public QPDFObjectHandle::StreamDataProvider
28 28 {
29 29 public:
30   - ImageProvider(std::string const& color_space,
31   - std::string const& filter);
  30 + ImageProvider(std::string const& color_space, std::string const& filter);
32 31 virtual ~ImageProvider();
33   - virtual void provideStreamData(int objid, int generation,
34   - Pipeline* pipeline);
  32 + virtual void
  33 + provideStreamData(int objid, int generation, Pipeline* pipeline);
35 34 size_t getWidth() const;
36 35 size_t getHeight() const;
37 36  
... ... @@ -45,8 +44,8 @@ class ImageProvider: public QPDFObjectHandle::StreamDataProvider
45 44 J_COLOR_SPACE j_color_space;
46 45 };
47 46  
48   -ImageProvider::ImageProvider(std::string const& color_space,
49   - std::string const& filter) :
  47 +ImageProvider::ImageProvider(
  48 + std::string const& color_space, std::string const& filter) :
50 49 width(400),
51 50 stripe_height(80),
52 51 color_space(color_space),
... ... @@ -54,8 +53,7 @@ ImageProvider::ImageProvider(std::string const&amp; color_space,
54 53 n_stripes(6),
55 54 j_color_space(JCS_UNKNOWN)
56 55 {
57   - if (color_space == "/DeviceCMYK")
58   - {
  56 + if (color_space == "/DeviceCMYK") {
59 57 j_color_space = JCS_CMYK;
60 58 stripes.push_back(std::string("\xff\x00\x00\x00", 4));
61 59 stripes.push_back(std::string("\x00\xff\x00\x00", 4));
... ... @@ -63,9 +61,7 @@ ImageProvider::ImageProvider(std::string const&amp; color_space,
63 61 stripes.push_back(std::string("\xff\x00\xff\x00", 4));
64 62 stripes.push_back(std::string("\xff\xff\x00\x00", 4));
65 63 stripes.push_back(std::string("\x00\x00\x00\xff", 4));
66   - }
67   - else if (color_space == "/DeviceRGB")
68   - {
  64 + } else if (color_space == "/DeviceRGB") {
69 65 j_color_space = JCS_RGB;
70 66 stripes.push_back(std::string("\xff\x00\x00", 3));
71 67 stripes.push_back(std::string("\x00\xff\x00", 3));
... ... @@ -73,9 +69,7 @@ ImageProvider::ImageProvider(std::string const&amp; color_space,
73 69 stripes.push_back(std::string("\xff\x00\xff", 3));
74 70 stripes.push_back(std::string("\xff\xff\x00", 3));
75 71 stripes.push_back(std::string("\x00\x00\x00", 3));
76   - }
77   - else if (color_space == "/DeviceGray")
78   - {
  72 + } else if (color_space == "/DeviceGray") {
79 73 j_color_space = JCS_GRAYSCALE;
80 74 stripes.push_back(std::string("\xee", 1));
81 75 stripes.push_back(std::string("\xcc", 1));
... ... @@ -103,34 +97,31 @@ ImageProvider::getHeight() const
103 97 }
104 98  
105 99 void
106   -ImageProvider::provideStreamData(int objid, int generation,
107   - Pipeline* pipeline)
  100 +ImageProvider::provideStreamData(int objid, int generation, Pipeline* pipeline)
108 101 {
109 102 std::vector<std::shared_ptr<Pipeline>> to_delete;
110 103 Pipeline* p = pipeline;
111 104 std::shared_ptr<Pipeline> p_new;
112 105  
113   - if (filter == "/DCTDecode")
114   - {
  106 + if (filter == "/DCTDecode") {
115 107 p_new = std::make_shared<Pl_DCT>(
116   - "image encoder", pipeline,
117   - QIntC::to_uint(width), QIntC::to_uint(getHeight()),
118   - QIntC::to_int(stripes[0].length()), j_color_space);
  108 + "image encoder",
  109 + pipeline,
  110 + QIntC::to_uint(width),
  111 + QIntC::to_uint(getHeight()),
  112 + QIntC::to_int(stripes[0].length()),
  113 + j_color_space);
119 114 to_delete.push_back(p_new);
120 115 p = p_new.get();
121   - }
122   - else if (filter == "/RunLengthDecode")
123   - {
  116 + } else if (filter == "/RunLengthDecode") {
124 117 p_new = std::make_shared<Pl_RunLength>(
125 118 "image encoder", pipeline, Pl_RunLength::a_encode);
126 119 to_delete.push_back(p_new);
127 120 p = p_new.get();
128 121 }
129 122  
130   - for (size_t i = 0; i < n_stripes; ++i)
131   - {
132   - for (size_t j = 0; j < width * stripe_height; ++j)
133   - {
  123 + for (size_t i = 0; i < n_stripes; ++i) {
  124 + for (size_t j = 0; j < width * stripe_height; ++j) {
134 125 p->write(
135 126 QUtil::unsigned_char_pointer(stripes[i].c_str()),
136 127 stripes[i].length());
... ... @@ -139,36 +130,43 @@ ImageProvider::provideStreamData(int objid, int generation,
139 130 p->finish();
140 131 }
141 132  
142   -void usage()
  133 +void
  134 +usage()
143 135 {
144 136 std::cerr << "Usage: " << whoami << " filename" << std::endl
145 137 << "Creates a simple PDF and writes it to filename" << std::endl;
146 138 exit(2);
147 139 }
148 140  
149   -static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text)
  141 +static QPDFObjectHandle
  142 +createPageContents(QPDF& pdf, std::string const& text)
150 143 {
151 144 // Create a stream that displays our image and the given text in
152 145 // our font.
153   - std::string contents =
154   - "BT /F1 24 Tf 72 320 Td (" + text + ") Tj ET\n"
  146 + std::string contents = "BT /F1 24 Tf 72 320 Td (" + text +
  147 + ") Tj ET\n"
155 148 "q 244 0 0 144 184 100 cm /Im1 Do Q\n";
156 149 return QPDFObjectHandle::newStream(&pdf, contents);
157 150 }
158 151  
159   -QPDFObjectHandle newName(std::string const& name)
  152 +QPDFObjectHandle
  153 +newName(std::string const& name)
160 154 {
161 155 return QPDFObjectHandle::newName(name);
162 156 }
163 157  
164   -QPDFObjectHandle newInteger(size_t val)
  158 +QPDFObjectHandle
  159 +newInteger(size_t val)
165 160 {
166 161 return QPDFObjectHandle::newInteger(QIntC::to_int(val));
167 162 }
168 163  
169   -void add_page(QPDFPageDocumentHelper& dh, QPDFObjectHandle font,
170   - std::string const& color_space,
171   - std::string const& filter)
  164 +void
  165 +add_page(
  166 + QPDFPageDocumentHelper& dh,
  167 + QPDFObjectHandle font,
  168 + std::string const& color_space,
  169 + std::string const& filter)
172 170 {
173 171 QPDF& pdf(dh.getQPDF());
174 172  
... ... @@ -193,9 +191,8 @@ void add_page(QPDFPageDocumentHelper&amp; dh, QPDFObjectHandle font,
193 191 image.replaceDict(image_dict);
194 192  
195 193 // Provide the stream data.
196   - image.replaceStreamData(provider,
197   - QPDFObjectHandle::parse(filter),
198   - QPDFObjectHandle::newNull());
  194 + image.replaceStreamData(
  195 + provider, QPDFObjectHandle::parse(filter), QPDFObjectHandle::newNull());
199 196  
200 197 // Create direct objects as needed by the page dictionary.
201 198 QPDFObjectHandle procset = "[/PDF /Text /ImageC]"_qpdf;
... ... @@ -212,15 +209,14 @@ void add_page(QPDFPageDocumentHelper&amp; dh, QPDFObjectHandle font,
212 209 resources.replaceKey("/XObject", xobject);
213 210  
214 211 // Create the page content stream
215   - QPDFObjectHandle contents = createPageContents(
216   - pdf, color_space + " with filter " + filter);
  212 + QPDFObjectHandle contents =
  213 + createPageContents(pdf, color_space + " with filter " + filter);
217 214  
218 215 // Create the page dictionary
219   - QPDFObjectHandle page = pdf.makeIndirectObject(
220   - "<<"
221   - " /Type /Page"
222   - " /MediaBox [0 0 612 392]"
223   - ">>"_qpdf);
  216 + QPDFObjectHandle page = pdf.makeIndirectObject("<<"
  217 + " /Type /Page"
  218 + " /MediaBox [0 0 612 392]"
  219 + ">>"_qpdf);
224 220 page.replaceKey("/Contents", contents);
225 221 page.replaceKey("/Resources", resources);
226 222  
... ... @@ -228,9 +224,11 @@ void add_page(QPDFPageDocumentHelper&amp; dh, QPDFObjectHandle font,
228 224 dh.addPage(page, false);
229 225 }
230 226  
231   -static void check(char const* filename,
232   - std::vector<std::string> const& color_spaces,
233   - std::vector<std::string> const& filters)
  227 +static void
  228 +check(
  229 + char const* filename,
  230 + std::vector<std::string> const& color_spaces,
  231 + std::vector<std::string> const& filters)
234 232 {
235 233 // Each stream is compressed the way it is supposed to be. We will
236 234 // add additional tests in qpdf.test to exercise QPDFWriter more
... ... @@ -253,17 +251,14 @@ static void check(char const* filename,
253 251 QPDF pdf;
254 252 pdf.processFile(filename);
255 253 auto pages = QPDFPageDocumentHelper(pdf).getAllPages();
256   - if (n_color_spaces * n_filters != pages.size())
257   - {
  254 + if (n_color_spaces * n_filters != pages.size()) {
258 255 throw std::logic_error("incorrect number of pages");
259 256 }
260 257 size_t pageno = 1;
261 258 bool errors = false;
262   - for (auto& page : pages)
263   - {
  259 + for (auto& page : pages) {
264 260 auto images = page.getImages();
265   - if (images.size() != 1)
266   - {
  261 + if (images.size() != 1) {
267 262 throw std::logic_error("incorrect number of images on page");
268 263 }
269 264  
... ... @@ -273,8 +268,7 @@ static void check(char const* filename,
273 268 std::string desired_filter = filters[(pageno - 1) % n_filters];
274 269 // In the default mode, QPDFWriter will compress with
275 270 // /FlateDecode if no filters are provided.
276   - if (desired_filter == "null")
277   - {
  271 + if (desired_filter == "null") {
278 272 desired_filter = "/FlateDecode";
279 273 }
280 274 QPDFObjectHandle image = images.begin()->second;
... ... @@ -282,40 +276,34 @@ static void check(char const* filename,
282 276 QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
283 277 QPDFObjectHandle filter = image_dict.getKey("/Filter");
284 278 bool this_errors = false;
285   - if (! filter.isNameAndEquals(desired_filter))
286   - {
  279 + if (!filter.isNameAndEquals(desired_filter)) {
287 280 this_errors = errors = true;
288 281 std::cout << "page " << pageno << ": expected filter "
289   - << desired_filter << "; actual filter = "
290   - << filter.unparse() << std::endl;
  282 + << desired_filter
  283 + << "; actual filter = " << filter.unparse() << std::endl;
291 284 }
292   - if (! color_space.isNameAndEquals(desired_color_space))
293   - {
  285 + if (!color_space.isNameAndEquals(desired_color_space)) {
294 286 this_errors = errors = true;
295 287 std::cout << "page " << pageno << ": expected color space "
296   - << desired_color_space << "; actual color space = "
297   - << color_space.unparse() << std::endl;
  288 + << desired_color_space
  289 + << "; actual color space = " << color_space.unparse()
  290 + << std::endl;
298 291 }
299 292  
300   - if (! this_errors)
301   - {
  293 + if (!this_errors) {
302 294 // Check image data
303   - auto actual_data =
304   - image.getStreamData(qpdf_dl_all);
  295 + auto actual_data = image.getStreamData(qpdf_dl_all);
305 296 ImageProvider* p = new ImageProvider(desired_color_space, "null");
306 297 PointerHolder<QPDFObjectHandle::StreamDataProvider> provider(p);
307 298 Pl_Buffer b_p("get image data");
308 299 provider->provideStreamData(0, 0, &b_p);
309 300 PointerHolder<Buffer> desired_data(b_p.getBuffer());
310 301  
311   - if (desired_data->getSize() != actual_data->getSize())
312   - {
313   - std::cout << "page " << pageno
314   - << ": image data length mismatch" << std::endl;
  302 + if (desired_data->getSize() != actual_data->getSize()) {
  303 + std::cout << "page " << pageno << ": image data length mismatch"
  304 + << std::endl;
315 305 this_errors = errors = true;
316   - }
317   - else
318   - {
  306 + } else {
319 307 // Compare bytes. For JPEG, allow a certain number of
320 308 // the bytes to be off desired by more than a given
321 309 // tolerance. Any of the samples may be a little off
... ... @@ -326,25 +314,20 @@ static void check(char const* filename,
326 314 unsigned char const* desired_bytes = desired_data->getBuffer();
327 315 size_t len = actual_data->getSize();
328 316 unsigned int mismatches = 0;
329   - int tolerance = (
330   - desired_filter == "/DCTDecode" ? 10 : 0);
331   - size_t threshold = (
332   - desired_filter == "/DCTDecode" ? len / 40U : 0);
333   - for (size_t i = 0; i < len; ++i)
334   - {
  317 + int tolerance = (desired_filter == "/DCTDecode" ? 10 : 0);
  318 + size_t threshold =
  319 + (desired_filter == "/DCTDecode" ? len / 40U : 0);
  320 + for (size_t i = 0; i < len; ++i) {
335 321 int delta = actual_bytes[i] - desired_bytes[i];
336   - if ((delta > tolerance) || (delta < -tolerance))
337   - {
  322 + if ((delta > tolerance) || (delta < -tolerance)) {
338 323 ++mismatches;
339 324 }
340 325 }
341   - if (mismatches > threshold)
342   - {
343   - std::cout << "page " << pageno
344   - << ": " << desired_color_space << ", "
345   - << desired_filter
346   - << ": mismatches: " << mismatches
347   - << " of " << len << std::endl;
  326 + if (mismatches > threshold) {
  327 + std::cout << "page " << pageno << ": "
  328 + << desired_color_space << ", " << desired_filter
  329 + << ": mismatches: " << mismatches << " of " << len
  330 + << std::endl;
348 331 this_errors = errors = true;
349 332 }
350 333 }
... ... @@ -352,17 +335,15 @@ static void check(char const* filename,
352 335  
353 336 ++pageno;
354 337 }
355   - if (errors)
356   - {
  338 + if (errors) {
357 339 throw std::logic_error("errors found");
358   - }
359   - else
360   - {
  340 + } else {
361 341 std::cout << "all checks passed" << std::endl;
362 342 }
363 343 }
364 344  
365   -static void create_pdf(char const* filename)
  345 +static void
  346 +create_pdf(char const* filename)
366 347 {
367 348 QPDF pdf;
368 349  
... ... @@ -371,14 +352,13 @@ static void create_pdf(char const* filename)
371 352  
372 353 // Add an indirect object to contain a font descriptor for the
373 354 // built-in Helvetica font.
374   - QPDFObjectHandle font = pdf.makeIndirectObject(
375   - "<<"
376   - " /Type /Font"
377   - " /Subtype /Type1"
378   - " /Name /F1"
379   - " /BaseFont /Helvetica"
380   - " /Encoding /WinAnsiEncoding"
381   - ">>"_qpdf);
  355 + QPDFObjectHandle font = pdf.makeIndirectObject("<<"
  356 + " /Type /Font"
  357 + " /Subtype /Type1"
  358 + " /Name /F1"
  359 + " /BaseFont /Helvetica"
  360 + " /Encoding /WinAnsiEncoding"
  361 + ">>"_qpdf);
382 362  
383 363 std::vector<std::string> color_spaces;
384 364 color_spaces.push_back("/DeviceCMYK");
... ... @@ -389,10 +369,8 @@ static void create_pdf(char const* filename)
389 369 filters.push_back("/DCTDecode");
390 370 filters.push_back("/RunLengthDecode");
391 371 QPDFPageDocumentHelper dh(pdf);
392   - for (auto const& color_space : color_spaces)
393   - {
394   - for (auto const& filter : filters)
395   - {
  372 + for (auto const& color_space : color_spaces) {
  373 + for (auto const& filter : filters) {
396 374 add_page(dh, font, color_space, filter);
397 375 }
398 376 }
... ... @@ -405,22 +383,19 @@ static void create_pdf(char const* filename)
405 383 check(filename, color_spaces, filters);
406 384 }
407 385  
408   -int main(int argc, char* argv[])
  386 +int
  387 +main(int argc, char* argv[])
409 388 {
410 389 whoami = QUtil::getWhoami(argv[0]);
411 390  
412   - if (argc != 2)
413   - {
  391 + if (argc != 2) {
414 392 usage();
415 393 }
416 394 char const* filename = argv[1];
417 395  
418   - try
419   - {
  396 + try {
420 397 create_pdf(filename);
421   - }
422   - catch (std::exception& e)
423   - {
  398 + } catch (std::exception& e) {
424 399 std::cerr << e.what() << std::endl;
425 400 exit(2);
426 401 }
... ...
examples/pdf-custom-filter.cc
1 1 #include <qpdf/QPDF.hh>
2   -#include <qpdf/QUtil.hh>
3   -#include <qpdf/QPDFWriter.hh>
4 2 #include <qpdf/QPDFStreamFilter.hh>
  3 +#include <qpdf/QPDFWriter.hh>
  4 +#include <qpdf/QUtil.hh>
5 5  
6 6 #include <cstring>
7 7 #include <exception>
... ... @@ -39,7 +39,6 @@
39 39  
40 40 static char const* whoami = 0;
41 41  
42   -
43 42 class Pl_XOR: public Pipeline
44 43 {
45 44 // This class implements a Pipeline for the made-up XOR decoder.
... ... @@ -66,8 +65,7 @@ Pl_XOR::Pl_XOR(char const* identifier, Pipeline* next, unsigned char key) :
66 65 void
67 66 Pl_XOR::write(unsigned char* data, size_t len)
68 67 {
69   - for (size_t i = 0; i < len; ++i)
70   - {
  68 + for (size_t i = 0; i < len; ++i) {
71 69 unsigned char p = data[i] ^ this->key;
72 70 getNext()->write(&p, 1);
73 71 }
... ... @@ -118,8 +116,7 @@ SF_XORDecode::setDecodeParms(QPDFObjectHandle decode_parms)
118 116 // to handle the /JBIG2Globals key, which points to a stream. See
119 117 // comments in SF_XORDecode::registerStream for additional notes
120 118 // on this.
121   - try
122   - {
  119 + try {
123 120 // Expect /DecodeParms to be a dictionary with a /KeyStream
124 121 // key that points to a one-byte stream whose single byte is
125 122 // the key. If we are successful at retrieving the key, return
... ... @@ -129,17 +126,14 @@ SF_XORDecode::setDecodeParms(QPDFObjectHandle decode_parms)
129 126 // implementations, look at the classes whose names start with
130 127 // SF_ in the qpdf library implementation.
131 128 auto buf = decode_parms.getKey("/KeyStream").getStreamData();
132   - if (buf->getSize() != 1)
133   - {
  129 + if (buf->getSize() != 1) {
134 130 return false;
135 131 }
136 132 this->key = buf->getBuffer()[0];
137 133 return true;
138   - }
139   - catch (std::exception& e)
140   - {
141   - std::cerr << "Error extracting key for /XORDecode: "
142   - << e.what() << std::endl;
  134 + } catch (std::exception& e) {
  135 + std::cerr << "Error extracting key for /XORDecode: " << e.what()
  136 + << std::endl;
143 137 }
144 138 return false;
145 139 }
... ... @@ -206,17 +200,19 @@ class StreamReplacer: public QPDFObjectHandle::StreamDataProvider
206 200 public:
207 201 StreamReplacer(QPDF* pdf);
208 202 virtual ~StreamReplacer() = default;
209   - virtual void provideStreamData(int objid, int generation,
210   - Pipeline* pipeline) override;
  203 + virtual void
  204 + provideStreamData(int objid, int generation, Pipeline* pipeline) override;
211 205  
212 206 void registerStream(
213 207 QPDFObjectHandle stream,
214 208 PointerHolder<QPDFObjectHandle::StreamDataProvider> self);
215 209  
216 210 private:
217   - bool maybeReplace(QPDFObjGen const& og,
218   - QPDFObjectHandle& stream, Pipeline* pipeline,
219   - QPDFObjectHandle* dict_updates);
  211 + bool maybeReplace(
  212 + QPDFObjGen const& og,
  213 + QPDFObjectHandle& stream,
  214 + Pipeline* pipeline,
  215 + QPDFObjectHandle* dict_updates);
220 216  
221 217 // Hang onto a reference to the QPDF object containing the streams
222 218 // we are replacing. We need this to create a new stream.
... ... @@ -238,10 +234,11 @@ StreamReplacer::StreamReplacer(QPDF* pdf) :
238 234 }
239 235  
240 236 bool
241   -StreamReplacer::maybeReplace(QPDFObjGen const& og,
242   - QPDFObjectHandle& stream,
243   - Pipeline* pipeline,
244   - QPDFObjectHandle* dict_updates)
  237 +StreamReplacer::maybeReplace(
  238 + QPDFObjGen const& og,
  239 + QPDFObjectHandle& stream,
  240 + Pipeline* pipeline,
  241 + QPDFObjectHandle* dict_updates)
245 242 {
246 243 // As described in the class comments, this method is called
247 244 // twice. Before writing has started pipeline is nullptr, and
... ... @@ -276,8 +273,7 @@ StreamReplacer::maybeReplace(QPDFObjGen const&amp; og,
276 273 // density.
277 274 auto dict = stream.getDict();
278 275 auto mark = dict.getKey("/DoXOR");
279   - if (! (mark.isBool() && mark.getBoolValue()))
280   - {
  276 + if (!(mark.isBool() && mark.getBoolValue())) {
281 277 return false;
282 278 }
283 279  
... ... @@ -288,17 +284,13 @@ StreamReplacer::maybeReplace(QPDFObjGen const&amp; og,
288 284 // it's a good idea to make sure we can retrieve the filtered data
289 285 // if we are going to need it later.
290 286 PointerHolder<Buffer> out;
291   - try
292   - {
  287 + try {
293 288 out = stream.getStreamData();
294   - }
295   - catch (...)
296   - {
  289 + } catch (...) {
297 290 return false;
298 291 }
299 292  
300   - if (dict_updates)
301   - {
  293 + if (dict_updates) {
302 294 // It's not safe to make any modifications to any objects
303 295 // during the writing process since the updated objects may
304 296 // have already been written. In this mode, when dict_updates
... ... @@ -309,16 +301,15 @@ StreamReplacer::maybeReplace(QPDFObjGen const&amp; og,
309 301 // changes. For example, an image resampler might change the
310 302 // dimensions or other properties of the image.
311 303 dict_updates->replaceKey(
312   - "/OrigLength", QPDFObjectHandle::newInteger(
313   - QIntC::to_longlong(out->getSize())));
  304 + "/OrigLength",
  305 + QPDFObjectHandle::newInteger(QIntC::to_longlong(out->getSize())));
314 306 // We are also storing the "key" that we will access when
315 307 // writing the data.
316 308 this->keys[og] = QIntC::to_uchar(
317 309 (og.getObj() * QIntC::to_int(out->getSize())) & 0xff);
318 310 }
319 311  
320   - if (pipeline)
321   - {
  312 + if (pipeline) {
322 313 unsigned char key = this->keys[og];
323 314 Pl_XOR p("xor", pipeline, key);
324 315 p.write(out->getBuffer(), out->getSize());
... ... @@ -338,8 +329,7 @@ StreamReplacer::registerStream(
338 329 // example, we are just iterating through objects, but if we were
339 330 // doing something like iterating through images on pages, we
340 331 // might realistically encounter the same stream more than once.
341   - if (this->copied_streams.count(og) > 0)
342   - {
  332 + if (this->copied_streams.count(og) > 0) {
343 333 return;
344 334 }
345 335 // Store something in copied_streams so that we don't
... ... @@ -352,19 +342,14 @@ StreamReplacer::registerStream(
352 342 // so, supplies dictionary updates we should make.
353 343 bool should_replace = false;
354 344 QPDFObjectHandle dict_updates = QPDFObjectHandle::newDictionary();
355   - try
356   - {
  345 + try {
357 346 should_replace = maybeReplace(og, stream, nullptr, &dict_updates);
358   - }
359   - catch (std::exception& e)
360   - {
  347 + } catch (std::exception& e) {
361 348 stream.warnIfPossible(
362   - std::string("exception while attempting to replace: ") +
363   - e.what());
  349 + std::string("exception while attempting to replace: ") + e.what());
364 350 }
365 351  
366   - if (should_replace)
367   - {
  352 + if (should_replace) {
368 353 // Copy the stream so we can get to the original data from the
369 354 // stream data provider. This doesn't actually copy any data,
370 355 // but the copy retains the original stream data after the
... ... @@ -372,14 +357,13 @@ StreamReplacer::registerStream(
372 357 this->copied_streams[og] = stream.copyStream();
373 358 // Update the stream dictionary with any changes.
374 359 auto dict = stream.getDict();
375   - for (auto const& k: dict_updates.getKeys())
376   - {
  360 + for (auto const& k : dict_updates.getKeys()) {
377 361 dict.replaceKey(k, dict_updates.getKey(k));
378 362 }
379 363 // Create the key stream that will be referenced from
380 364 // /DecodeParms. We have to do this now since you can't modify
381 365 // or create objects during write.
382   - char p[1] = { static_cast<char>(this->keys[og]) };
  366 + char p[1] = {static_cast<char>(this->keys[og])};
383 367 std::string p_str(p, 1);
384 368 QPDFObjectHandle dp_stream =
385 369 QPDFObjectHandle::newStream(this->pdf, p_str);
... ... @@ -388,23 +372,19 @@ StreamReplacer::registerStream(
388 372 QPDFObjectHandle decode_parms =
389 373 QPDFObjectHandle::newDictionary({{"/KeyStream", dp_stream}});
390 374 stream.replaceStreamData(
391   - self,
392   - QPDFObjectHandle::newName("/XORDecode"),
393   - decode_parms);
  375 + self, QPDFObjectHandle::newName("/XORDecode"), decode_parms);
394 376 // Further, if /ProtectXOR = true, we disable filtering on write
395 377 // so that QPDFWriter will not decode the stream even though we
396 378 // have registered a stream filter for /XORDecode.
397 379 auto protect = dict.getKey("/ProtectXOR");
398   - if (protect.isBool() && protect.getBoolValue())
399   - {
  380 + if (protect.isBool() && protect.getBoolValue()) {
400 381 stream.setFilterOnWrite(false);
401 382 }
402 383 }
403 384 }
404 385  
405 386 void
406   -StreamReplacer::provideStreamData(int objid, int generation,
407   - Pipeline* pipeline)
  387 +StreamReplacer::provideStreamData(int objid, int generation, Pipeline* pipeline)
408 388 {
409 389 QPDFObjGen og(objid, generation);
410 390 QPDFObjectHandle orig = this->copied_streams[og];
... ... @@ -412,8 +392,7 @@ StreamReplacer::provideStreamData(int objid, int generation,
412 392 // dict_updates. In this mode, maybeReplace doesn't make any
413 393 // changes. We have to hand it the original stream data, which we
414 394 // get from copied_streams.
415   - if (! maybeReplace(og, orig, pipeline, nullptr))
416   - {
  395 + if (!maybeReplace(og, orig, pipeline, nullptr)) {
417 396 // Since this only gets called for streams we already
418 397 // determined we are replacing, a false return would indicate
419 398 // a logic error.
... ... @@ -422,8 +401,9 @@ StreamReplacer::provideStreamData(int objid, int generation,
422 401 }
423 402 }
424 403  
425   -static void process(char const* infilename, char const* outfilename,
426   - bool decode_specialized)
  404 +static void
  405 +process(
  406 + char const* infilename, char const* outfilename, bool decode_specialized)
427 407 {
428 408 QPDF qpdf;
429 409 qpdf.processFile(infilename);
... ... @@ -434,10 +414,8 @@ static void process(char const* infilename, char const* outfilename,
434 414 StreamReplacer* replacer = new StreamReplacer(&qpdf);
435 415 PointerHolder<QPDFObjectHandle::StreamDataProvider> p(replacer);
436 416  
437   - for (auto& o: qpdf.getAllObjects())
438   - {
439   - if (o.isStream())
440   - {
  417 + for (auto& o : qpdf.getAllObjects()) {
  418 + if (o.isStream()) {
441 419 // Call registerStream for every stream. Only ones that
442 420 // registerStream decides to replace will actually be
443 421 // replaced.
... ... @@ -446,70 +424,58 @@ static void process(char const* infilename, char const* outfilename,
446 424 }
447 425  
448 426 QPDFWriter w(qpdf, outfilename);
449   - if (decode_specialized)
450   - {
  427 + if (decode_specialized) {
451 428 w.setDecodeLevel(qpdf_dl_specialized);
452 429 }
453 430 // For the test suite, use static IDs.
454 431 w.setStaticID(true); // for testing only
455 432 w.write();
456   - std::cout << whoami << ": new file written to " << outfilename
457   - << std::endl;
  433 + std::cout << whoami << ": new file written to " << outfilename << std::endl;
458 434 }
459 435  
460   -static void usage()
  436 +static void
  437 +usage()
461 438 {
462   - std::cerr
463   - << "\n"
464   - << "Usage: " << whoami << " [--decode-specialized] infile outfile\n"
465   - << std::endl;
  439 + std::cerr << "\n"
  440 + << "Usage: " << whoami
  441 + << " [--decode-specialized] infile outfile\n"
  442 + << std::endl;
466 443 exit(2);
467 444 }
468 445  
469   -int main(int argc, char* argv[])
  446 +int
  447 +main(int argc, char* argv[])
470 448 {
471 449 whoami = QUtil::getWhoami(argv[0]);
472 450  
473 451 char const* infilename = 0;
474 452 char const* outfilename = 0;
475 453 bool decode_specialized = false;
476   - for (int i = 1; i < argc; ++i)
477   - {
478   - if (strcmp(argv[i], "--decode-specialized") == 0)
479   - {
  454 + for (int i = 1; i < argc; ++i) {
  455 + if (strcmp(argv[i], "--decode-specialized") == 0) {
480 456 decode_specialized = true;
481   - }
482   - else if (! infilename)
483   - {
  457 + } else if (!infilename) {
484 458 infilename = argv[i];
485   - }
486   - else if (! outfilename)
487   - {
  459 + } else if (!outfilename) {
488 460 outfilename = argv[i];
489   - }
490   - else
491   - {
  461 + } else {
492 462 usage();
493 463 }
494 464 }
495   - if (! (infilename && outfilename))
496   - {
  465 + if (!(infilename && outfilename)) {
497 466 usage();
498 467 }
499 468  
500   - try
501   - {
  469 + try {
502 470 // Register our fictitious filter. This enables QPDFWriter to
503 471 // decode our streams. This is not a real filter, so no real
504 472 // PDF reading application would be able to interpret it. This
505 473 // is just for illustrative purposes.
506 474 QPDF::registerStreamFilter(
507   - "/XORDecode", []{ return std::make_shared<SF_XORDecode>(); });
  475 + "/XORDecode", [] { return std::make_shared<SF_XORDecode>(); });
508 476 // Do the actual processing.
509 477 process(infilename, outfilename, decode_specialized);
510   - }
511   - catch (std::exception &e)
512   - {
  478 + } catch (std::exception& e) {
513 479 std::cerr << whoami << ": exception: " << e.what() << std::endl;
514 480 exit(2);
515 481 }
... ...
examples/pdf-double-page-size.cc
1   -#include <iostream>
2   -#include <string.h>
3   -#include <stdlib.h>
  1 +#include <qpdf/Buffer.hh>
4 2 #include <qpdf/QPDF.hh>
5 3 #include <qpdf/QPDFPageDocumentHelper.hh>
6 4 #include <qpdf/QPDFPageObjectHelper.hh>
7   -#include <qpdf/QUtil.hh>
8   -#include <qpdf/Buffer.hh>
9 5 #include <qpdf/QPDFWriter.hh>
  6 +#include <qpdf/QUtil.hh>
  7 +#include <iostream>
  8 +#include <stdlib.h>
  9 +#include <string.h>
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -void usage()
  13 +void
  14 +usage()
14 15 {
15 16 std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]"
16 17 << std::endl
... ... @@ -21,45 +22,43 @@ void usage()
21 22  
22 23 // If there is a box of name box_name, replace it with a new box whose
23 24 // elements are double the values of the original box.
24   -static void doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name)
  25 +static void
  26 +doubleBoxSize(QPDFPageObjectHelper& page, char const* box_name)
25 27 {
26 28 // We need to use getAttribute rather than getKey as some boxes could
27 29 // be inherited.
28 30 auto box = page.getAttribute(box_name, true);
29   - if (box.isNull())
30   - {
  31 + if (box.isNull()) {
31 32 return;
32 33 }
33   - if (! box.isRectangle())
34   - {
35   - throw std::runtime_error(std::string("box ") + box_name +
36   - " is not an array of four elements");
  34 + if (!box.isRectangle()) {
  35 + throw std::runtime_error(
  36 + std::string("box ") + box_name +
  37 + " is not an array of four elements");
37 38 }
38 39 std::vector<QPDFObjectHandle> doubled;
39   - for (auto& item : box.aitems())
40   - {
  40 + for (auto& item : box.aitems()) {
41 41 doubled.push_back(
42 42 QPDFObjectHandle::newReal(item.getNumericValue() * 2.0, 2));
43 43 }
44   - page.getObjectHandle()
45   - .replaceKey(box_name, QPDFObjectHandle::newArray(doubled));
  44 + page.getObjectHandle().replaceKey(
  45 + box_name, QPDFObjectHandle::newArray(doubled));
46 46 }
47 47  
48   -int main(int argc, char* argv[])
  48 +int
  49 +main(int argc, char* argv[])
49 50 {
50 51 whoami = QUtil::getWhoami(argv[0]);
51 52  
52 53 // For test suite
53 54 bool static_id = false;
54   - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0))
55   - {
  55 + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) {
56 56 static_id = true;
57 57 --argc;
58 58 ++argv;
59 59 }
60 60  
61   - if (! ((argc == 3) || (argc == 4)))
62   - {
  61 + if (!((argc == 3) || (argc == 4))) {
63 62 usage();
64 63 }
65 64  
... ... @@ -70,13 +69,11 @@ int main(int argc, char* argv[])
70 69 // Text to prepend to each page's contents
71 70 std::string content = "2 0 0 2 0 0 cm\n";
72 71  
73   - try
74   - {
  72 + try {
75 73 QPDF qpdf;
76 74 qpdf.processFile(infilename, password);
77 75  
78   - for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages())
79   - {
  76 + for (auto& page : QPDFPageDocumentHelper(qpdf).getAllPages()) {
80 77 // Prepend the buffer to the page's contents
81 78 page.addPageContents(
82 79 QPDFObjectHandle::newStream(&qpdf, content), true);
... ... @@ -91,8 +88,7 @@ int main(int argc, char* argv[])
91 88  
92 89 // Write out a new file
93 90 QPDFWriter w(qpdf, outfilename);
94   - if (static_id)
95   - {
  91 + if (static_id) {
96 92 // For the test suite, uncompress streams and use static IDs.
97 93 w.setStaticID(true); // for testing only
98 94 w.setStreamDataMode(qpdf_s_uncompress);
... ... @@ -100,9 +96,7 @@ int main(int argc, char* argv[])
100 96 w.write();
101 97 std::cout << whoami << ": new file written to " << outfilename
102 98 << std::endl;
103   - }
104   - catch (std::exception &e)
105   - {
  99 + } catch (std::exception& e) {
106 100 std::cerr << whoami << " processing file " << infilename << ": "
107 101 << e.what() << std::endl;
108 102 exit(2);
... ...
examples/pdf-filter-tokens.cc
... ... @@ -5,22 +5,23 @@
5 5 // QPDFObjectHandle::TokenFilter with filterContents.
6 6 //
7 7  
8   -#include <iostream>
9   -#include <string.h>
10   -#include <stdlib.h>
11 8 #include <algorithm>
12 9 #include <deque>
  10 +#include <iostream>
  11 +#include <stdlib.h>
  12 +#include <string.h>
13 13  
14 14 #include <qpdf/QPDF.hh>
  15 +#include <qpdf/QPDFObjectHandle.hh>
15 16 #include <qpdf/QPDFPageDocumentHelper.hh>
16 17 #include <qpdf/QPDFPageObjectHelper.hh>
17   -#include <qpdf/QUtil.hh>
18 18 #include <qpdf/QPDFWriter.hh>
19   -#include <qpdf/QPDFObjectHandle.hh>
  19 +#include <qpdf/QUtil.hh>
20 20  
21 21 static char const* whoami = 0;
22 22  
23   -void usage()
  23 +void
  24 +usage()
24 25 {
25 26 std::cerr << "Usage: " << whoami << " infile outfile" << std::endl
26 27 << "Applies token filters to infile and writes outfile"
... ... @@ -52,14 +53,11 @@ StringReverser::handleToken(QPDFTokenizer::Token const&amp; token)
52 53 // strings. It's just intended to give a simple example of a
53 54 // pretty minimal filter and to show an example of writing a
54 55 // constructed token.
55   - if (token.getType() == QPDFTokenizer::tt_string)
56   - {
  56 + if (token.getType() == QPDFTokenizer::tt_string) {
57 57 std::string value = token.getValue();
58 58 std::reverse(value.begin(), value.end());
59 59 writeToken(QPDFTokenizer::Token(QPDFTokenizer::tt_string, value));
60   - }
61   - else
62   - {
  60 + } else {
63 61 writeToken(token);
64 62 }
65 63 }
... ... @@ -90,15 +88,17 @@ class ColorToGray: public QPDFObjectHandle::TokenFilter
90 88 bool
91 89 ColorToGray::isNumeric(QPDFTokenizer::token_type_e token_type)
92 90 {
93   - return ((token_type == QPDFTokenizer::tt_integer) ||
94   - (token_type == QPDFTokenizer::tt_real));
  91 + return (
  92 + (token_type == QPDFTokenizer::tt_integer) ||
  93 + (token_type == QPDFTokenizer::tt_real));
95 94 }
96 95  
97 96 bool
98 97 ColorToGray::isIgnorable(QPDFTokenizer::token_type_e token_type)
99 98 {
100   - return ((token_type == QPDFTokenizer::tt_space) ||
101   - (token_type == QPDFTokenizer::tt_comment));
  99 + return (
  100 + (token_type == QPDFTokenizer::tt_space) ||
  101 + (token_type == QPDFTokenizer::tt_comment));
102 102 }
103 103  
104 104 double
... ... @@ -134,33 +134,28 @@ ColorToGray::handleToken(QPDFTokenizer::Token const&amp; token)
134 134 // kinds of operands, replace the command. Flush any additional
135 135 // accumulated tokens to keep the stack only four tokens deep.
136 136  
137   - while ((! this->all_stack.empty()) &&
138   - isIgnorable(this->all_stack.at(0).getType()))
139   - {
  137 + while ((!this->all_stack.empty()) &&
  138 + isIgnorable(this->all_stack.at(0).getType())) {
140 139 writeToken(this->all_stack.at(0));
141 140 this->all_stack.pop_front();
142 141 }
143 142 this->all_stack.push_back(token);
144 143 QPDFTokenizer::token_type_e token_type = token.getType();
145   - if (! isIgnorable(token_type))
146   - {
  144 + if (!isIgnorable(token_type)) {
147 145 this->stack.push_back(token);
148 146 if ((this->stack.size() == 4) &&
149 147 (token == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "rg")) &&
150 148 (isNumeric(this->stack.at(0).getType())) &&
151 149 (isNumeric(this->stack.at(1).getType())) &&
152   - (isNumeric(this->stack.at(2).getType())))
153   - {
  150 + (isNumeric(this->stack.at(2).getType()))) {
154 151 double r = numericValue(this->stack.at(0));
155 152 double g = numericValue(this->stack.at(1));
156 153 double b = numericValue(this->stack.at(2));
157 154 double gray = ((0.3 * r) + (0.59 * b) + (0.11 * g));
158   - if (gray > 1.0)
159   - {
  155 + if (gray > 1.0) {
160 156 gray = 1.0;
161 157 }
162   - if (gray < 0.0)
163   - {
  158 + if (gray < 0.0) {
164 159 gray = 0.0;
165 160 }
166 161 write(QUtil::double_to_string(gray, 3));
... ... @@ -169,8 +164,7 @@ ColorToGray::handleToken(QPDFTokenizer::Token const&amp; token)
169 164 this->all_stack.clear();
170 165 }
171 166 }
172   - if (this->stack.size() == 4)
173   - {
  167 + if (this->stack.size() == 4) {
174 168 writeToken(this->all_stack.at(0));
175 169 this->all_stack.pop_front();
176 170 this->stack.pop_front();
... ... @@ -181,33 +175,31 @@ void
181 175 ColorToGray::handleEOF()
182 176 {
183 177 // Flush out any remaining accumulated tokens.
184   - while (! this->all_stack.empty())
185   - {
  178 + while (!this->all_stack.empty()) {
186 179 writeToken(this->all_stack.at(0));
187 180 this->all_stack.pop_front();
188 181 }
189 182 }
190 183  
191   -int main(int argc, char* argv[])
  184 +int
  185 +main(int argc, char* argv[])
192 186 {
193 187 whoami = QUtil::getWhoami(argv[0]);
194 188  
195   - if (argc != 3)
196   - {
  189 + if (argc != 3) {
197 190 usage();
198 191 }
199 192 char const* infilename = argv[1];
200 193 char const* outfilename = argv[2];
201 194  
202   - try
203   - {
  195 + try {
204 196 QPDF pdf;
205 197 pdf.processFile(infilename);
206 198 std::vector<QPDFPageObjectHelper> pages =
207 199 QPDFPageDocumentHelper(pdf).getAllPages();
208 200 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
209   - iter != pages.end(); ++iter)
210   - {
  201 + iter != pages.end();
  202 + ++iter) {
211 203 // Attach two token filters to each page of this file.
212 204 // When the file is written, or when the pages' contents
213 205 // are retrieved in any other way, the filters will be
... ... @@ -222,11 +214,9 @@ int main(int argc, char* argv[])
222 214 }
223 215  
224 216 QPDFWriter w(pdf, outfilename);
225   - w.setStaticID(true); // for testing only
  217 + w.setStaticID(true); // for testing only
226 218 w.write();
227   - }
228   - catch (std::exception& e)
229   - {
  219 + } catch (std::exception& e) {
230 220 std::cerr << whoami << ": " << e.what() << std::endl;
231 221 exit(2);
232 222 }
... ...
examples/pdf-invert-images.cc
1   -#include <iostream>
2   -#include <string.h>
3   -#include <stdlib.h>
  1 +#include <qpdf/Buffer.hh>
  2 +#include <qpdf/QIntC.hh>
4 3 #include <qpdf/QPDF.hh>
5 4 #include <qpdf/QPDFPageDocumentHelper.hh>
6 5 #include <qpdf/QPDFPageObjectHelper.hh>
7   -#include <qpdf/QUtil.hh>
8   -#include <qpdf/Buffer.hh>
9 6 #include <qpdf/QPDFWriter.hh>
10   -#include <qpdf/QIntC.hh>
  7 +#include <qpdf/QUtil.hh>
  8 +#include <iostream>
  9 +#include <stdlib.h>
  10 +#include <string.h>
11 11  
12 12 static char const* whoami = 0;
13 13  
14   -void usage()
  14 +void
  15 +usage()
15 16 {
16 17 std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf [in-password]"
17 18 << std::endl
... ... @@ -35,8 +36,8 @@ class ImageInverter: public QPDFObjectHandle::StreamDataProvider
35 36 virtual ~ImageInverter()
36 37 {
37 38 }
38   - virtual void provideStreamData(int objid, int generation,
39   - Pipeline* pipeline) override;
  39 + virtual void
  40 + provideStreamData(int objid, int generation, Pipeline* pipeline) override;
40 41  
41 42 void registerImage(
42 43 QPDFObjectHandle image,
... ... @@ -64,8 +65,7 @@ ImageInverter::registerImage(
64 65 // generation number. Recall that a single image object may be
65 66 // used more than once, so no need to update the same stream
66 67 // multiple times.
67   - if (this->copied_images.count(og) > 0)
68   - {
  68 + if (this->copied_images.count(og) > 0) {
69 69 return;
70 70 }
71 71 this->copied_images[og] = image.copyStream();
... ... @@ -79,14 +79,12 @@ ImageInverter::registerImage(
79 79 // filterable in the input QPDF object, so we don't have to deal
80 80 // with it explicitly here. We could explicitly use /DCTDecode and
81 81 // write through a DCT filter if we wanted.
82   - image.replaceStreamData(self,
83   - QPDFObjectHandle::newNull(),
84   - QPDFObjectHandle::newNull());
  82 + image.replaceStreamData(
  83 + self, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
85 84 }
86 85  
87 86 void
88   -ImageInverter::provideStreamData(int objid, int generation,
89   - Pipeline* pipeline)
  87 +ImageInverter::provideStreamData(int objid, int generation, Pipeline* pipeline)
90 88 {
91 89 // Use the object and generation number supplied to look up the
92 90 // image data. Then invert the image data and write the inverted
... ... @@ -97,29 +95,27 @@ ImageInverter::provideStreamData(int objid, int generation,
97 95 size_t size = data->getSize();
98 96 unsigned char* buf = data->getBuffer();
99 97 unsigned char ch;
100   - for (size_t i = 0; i < size; ++i)
101   - {
  98 + for (size_t i = 0; i < size; ++i) {
102 99 ch = QIntC::to_uchar(0xff - buf[i]);
103 100 pipeline->write(&ch, 1);
104 101 }
105 102 pipeline->finish();
106 103 }
107 104  
108   -int main(int argc, char* argv[])
  105 +int
  106 +main(int argc, char* argv[])
109 107 {
110 108 whoami = QUtil::getWhoami(argv[0]);
111 109  
112 110 // For test suite
113 111 bool static_id = false;
114   - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0))
115   - {
  112 + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) {
116 113 static_id = true;
117 114 --argc;
118 115 ++argv;
119 116 }
120 117  
121   - if (! ((argc == 3) || (argc == 4)))
122   - {
  118 + if (!((argc == 3) || (argc == 4))) {
123 119 usage();
124 120 }
125 121  
... ... @@ -127,8 +123,7 @@ int main(int argc, char* argv[])
127 123 char const* outfilename = argv[2];
128 124 char const* password = (argc == 4) ? argv[3] : "";
129 125  
130   - try
131   - {
  126 + try {
132 127 QPDF qpdf;
133 128 qpdf.processFile(infilename, password);
134 129  
... ... @@ -139,18 +134,15 @@ int main(int argc, char* argv[])
139 134 std::vector<QPDFPageObjectHelper> pages =
140 135 QPDFPageDocumentHelper(qpdf).getAllPages();
141 136 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
142   - iter != pages.end(); ++iter)
143   - {
  137 + iter != pages.end();
  138 + ++iter) {
144 139 QPDFPageObjectHelper& page(*iter);
145 140 // Get all images on the page.
146   - std::map<std::string, QPDFObjectHandle> images =
147   - page.getImages();
148   - for (auto& iter2: images)
149   - {
  141 + std::map<std::string, QPDFObjectHandle> images = page.getImages();
  142 + for (auto& iter2 : images) {
150 143 QPDFObjectHandle& image = iter2.second;
151 144 QPDFObjectHandle image_dict = image.getDict();
152   - QPDFObjectHandle color_space =
153   - image_dict.getKey("/ColorSpace");
  145 + QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
154 146 QPDFObjectHandle bits_per_component =
155 147 image_dict.getKey("/BitsPerComponent");
156 148  
... ... @@ -159,12 +151,10 @@ int main(int argc, char* argv[])
159 151 // pipeStreamData with a null pipeline to determine
160 152 // whether the image is filterable. Directly inspect
161 153 // keys to determine the image type.
162   - if (image.pipeStreamData(0, qpdf_ef_compress,
163   - qpdf_dl_all) &&
  154 + if (image.pipeStreamData(0, qpdf_ef_compress, qpdf_dl_all) &&
164 155 color_space.isNameAndEquals("/DeviceGray") &&
165 156 bits_per_component.isInteger() &&
166   - (bits_per_component.getIntValue() == 8))
167   - {
  157 + (bits_per_component.getIntValue() == 8)) {
168 158 inv->registerImage(image, p);
169 159 }
170 160 }
... ... @@ -172,8 +162,7 @@ int main(int argc, char* argv[])
172 162  
173 163 // Write out a new file
174 164 QPDFWriter w(qpdf, outfilename);
175   - if (static_id)
176   - {
  165 + if (static_id) {
177 166 // For the test suite, uncompress streams and use static
178 167 // IDs.
179 168 w.setStaticID(true); // for testing only
... ... @@ -181,9 +170,7 @@ int main(int argc, char* argv[])
181 170 w.write();
182 171 std::cout << whoami << ": new file written to " << outfilename
183 172 << std::endl;
184   - }
185   - catch (std::exception &e)
186   - {
  173 + } catch (std::exception& e) {
187 174 std::cerr << whoami << " processing file " << infilename << ": "
188 175 << e.what() << std::endl;
189 176 exit(2);
... ...
examples/pdf-linearize.c
... ... @@ -9,18 +9,21 @@
9 9  
10 10 static char const* whoami = 0;
11 11  
12   -static void usage()
  12 +static void
  13 +usage()
13 14 {
14 15 fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami);
15 16 exit(2);
16 17 }
17 18  
18   -static void write_progress(int percent, void* data)
  19 +static void
  20 +write_progress(int percent, void* data)
19 21 {
20 22 printf("%s progress: %d%%\n", (char const*)(data), percent);
21 23 }
22 24  
23   -int main(int argc, char* argv[])
  25 +int
  26 +main(int argc, char* argv[])
24 27 {
25 28 char* infile = NULL;
26 29 char* password = NULL;
... ... @@ -30,21 +33,15 @@ int main(int argc, char* argv[])
30 33 int errors = 0;
31 34 char* p = 0;
32 35  
33   - if ((p = strrchr(argv[0], '/')) != NULL)
34   - {
  36 + if ((p = strrchr(argv[0], '/')) != NULL) {
35 37 whoami = p + 1;
36   - }
37   - else if ((p = strrchr(argv[0], '\\')) != NULL)
38   - {
  38 + } else if ((p = strrchr(argv[0], '\\')) != NULL) {
39 39 whoami = p + 1;
40   - }
41   - else
42   - {
  40 + } else {
43 41 whoami = argv[0];
44 42 }
45 43  
46   - if (argc != 4)
47   - {
  44 + if (argc != 4) {
48 45 usage();
49 46 }
50 47  
... ... @@ -53,8 +50,7 @@ int main(int argc, char* argv[])
53 50 outfile = argv[3];
54 51  
55 52 if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) &&
56   - ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0))
57   - {
  53 + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) {
58 54 /* Use static ID for testing only. For production, a
59 55 * non-static ID is used. See also
60 56 * qpdf_set_deterministic_ID. */
... ... @@ -63,25 +59,22 @@ int main(int argc, char* argv[])
63 59 qpdf_register_progress_reporter(qpdf, write_progress, infile);
64 60 qpdf_write(qpdf);
65 61 }
66   - while (qpdf_more_warnings(qpdf))
67   - {
  62 + while (qpdf_more_warnings(qpdf)) {
68 63 warnings = 1;
69   - printf("warning: %s\n",
70   - qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf)));
  64 + printf(
  65 + "warning: %s\n",
  66 + qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf)));
71 67 }
72   - if (qpdf_has_error(qpdf))
73   - {
  68 + if (qpdf_has_error(qpdf)) {
74 69 errors = 1;
75   - printf("error: %s\n",
76   - qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
  70 + printf(
  71 + "error: %s\n",
  72 + qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
77 73 }
78 74 qpdf_cleanup(&qpdf);
79   - if (errors)
80   - {
  75 + if (errors) {
81 76 return 2;
82   - }
83   - else if (warnings)
84   - {
  77 + } else if (warnings) {
85 78 return 3;
86 79 }
87 80  
... ...
examples/pdf-mod-info.cc
1 1 // Author: Vitaliy Pavlyuk
2 2  
3 3 #include <qpdf/QPDF.hh>
4   -#include <qpdf/QPDFWriter.hh>
5 4 #include <qpdf/QPDFObjectHandle.hh>
6   -#include <qpdf/QUtil.hh>
  5 +#include <qpdf/QPDFWriter.hh>
7 6 #include <qpdf/QTC.hh>
  7 +#include <qpdf/QUtil.hh>
8 8 #include <iostream>
9   -#include <string.h>
10   -#include <stdlib.h>
11 9 #include <stdio.h>
  10 +#include <stdlib.h>
  11 +#include <string.h>
12 12  
13 13 static char const* version = "1.1";
14 14 static char const* whoami = 0;
15 15  
16   -void usage()
  16 +void
  17 +usage()
17 18 {
18   - std::cerr
19   - << "Usage: " << whoami
20   - << " --in in_file [--out out_file] [--key key [--val val]?]+\n"
21   - << "Modifies/Adds/Removes PDF /Info entries in the in_file\n"
22   - << "and stores the result in out_file\n"
23   - << "Special mode: " << whoami << " --dump file\n"
24   - << "dumps all /Info entries to stdout\n";
  19 + std::cerr << "Usage: " << whoami
  20 + << " --in in_file [--out out_file] [--key key [--val val]?]+\n"
  21 + << "Modifies/Adds/Removes PDF /Info entries in the in_file\n"
  22 + << "and stores the result in out_file\n"
  23 + << "Special mode: " << whoami << " --dump file\n"
  24 + << "dumps all /Info entries to stdout\n";
25 25 exit(2);
26 26 }
27 27  
28   -void dumpInfoDict(QPDF& pdf,
29   - std::ostream& os = std::cout,
30   - std::string const& sep = ":\t")
  28 +void
  29 +dumpInfoDict(
  30 + QPDF& pdf, std::ostream& os = std::cout, std::string const& sep = ":\t")
31 31 {
32 32 QPDFObjectHandle trailer = pdf.getTrailer();
33   - if (trailer.hasKey("/Info"))
34   - {
35   - for (auto& it: trailer.getKey("/Info").ditems())
36   - {
  33 + if (trailer.hasKey("/Info")) {
  34 + for (auto& it : trailer.getKey("/Info").ditems()) {
37 35 std::string val;
38   - if (it.second.isString())
39   - {
  36 + if (it.second.isString()) {
40 37 val = it.second.getStringValue();
41   - }
42   - else if (it.second.isName())
43   - {
  38 + } else if (it.second.isName()) {
44 39 val = it.second.getName();
45   - }
46   - else // according to PDF Spec 1.5, shouldn't happen
  40 + } else // according to PDF Spec 1.5, shouldn't happen
47 41 {
48 42 val = it.second.unparseResolved();
49 43 }
... ... @@ -52,36 +46,32 @@ void dumpInfoDict(QPDF&amp; pdf,
52 46 }
53 47 }
54 48  
55   -void pdfDumpInfoDict(char const* fname)
  49 +void
  50 +pdfDumpInfoDict(char const* fname)
56 51 {
57   - try
58   - {
  52 + try {
59 53 QPDF pdf;
60 54 pdf.processFile(fname);
61 55 dumpInfoDict(pdf);
62   - }
63   - catch (std::exception& e)
64   - {
  56 + } catch (std::exception& e) {
65 57 std::cerr << e.what() << std::endl;
66 58 exit(2);
67 59 }
68 60 }
69 61  
70   -int main(int argc, char* argv[])
  62 +int
  63 +main(int argc, char* argv[])
71 64 {
72   -
73 65 bool static_id = false;
74 66 std::map<std::string, std::string> Keys;
75 67  
76 68 whoami = QUtil::getWhoami(argv[0]);
77 69  
78   - if ((argc == 2) && (! strcmp(argv[1], "--version")) )
79   - {
  70 + if ((argc == 2) && (!strcmp(argv[1], "--version"))) {
80 71 std::cout << whoami << " version " << version << std::endl;
81 72 exit(0);
82 73 }
83   - if ((argc == 3) && (! strcmp(argv[1], "--dump")))
84   - {
  74 + if ((argc == 3) && (!strcmp(argv[1], "--dump"))) {
85 75 QTC::TC("examples", "pdf-mod-info --dump");
86 76 pdfDumpInfoDict(argv[2]);
87 77 exit(0);
... ... @@ -91,59 +81,43 @@ int main(int argc, char* argv[])
91 81 char* fl_out = 0;
92 82 std::string cur_key;
93 83  
94   - for (int i = 1; i < argc; ++i)
95   - {
96   - if ((! strcmp(argv[i], "--in")) && (++i < argc))
97   - {
  84 + for (int i = 1; i < argc; ++i) {
  85 + if ((!strcmp(argv[i], "--in")) && (++i < argc)) {
98 86 fl_in = argv[i];
99   - }
100   - else if ((! strcmp(argv[i], "--out")) && (++i < argc))
101   - {
  87 + } else if ((!strcmp(argv[i], "--out")) && (++i < argc)) {
102 88 fl_out = argv[i];
103   - }
104   - else if (! strcmp(argv[i], "--static-id")) // don't document
  89 + } else if (!strcmp(argv[i], "--static-id")) // don't document
105 90 {
106 91 static_id = true; // this should be used in test suites only
107   - }
108   - else if ((! strcmp(argv[i], "--key")) && (++i < argc))
109   - {
  92 + } else if ((!strcmp(argv[i], "--key")) && (++i < argc)) {
110 93 QTC::TC("examples", "pdf-mod-info -key");
111 94 cur_key = argv[i];
112   - if (! ((cur_key.length() > 0) && (cur_key.at(0) == '/')))
113   - {
  95 + if (!((cur_key.length() > 0) && (cur_key.at(0) == '/'))) {
114 96 cur_key = "/" + cur_key;
115 97 }
116 98 Keys[cur_key] = "";
117   - }
118   - else if ((! strcmp(argv[i], "--val")) && (++i < argc))
119   - {
120   - if (cur_key.empty())
121   - {
  99 + } else if ((!strcmp(argv[i], "--val")) && (++i < argc)) {
  100 + if (cur_key.empty()) {
122 101 QTC::TC("examples", "pdf-mod-info usage wrong val");
123 102 usage();
124 103 }
125 104 QTC::TC("examples", "pdf-mod-info -val");
126 105 Keys[cur_key] = argv[i];
127 106 cur_key.clear();
128   - }
129   - else
130   - {
  107 + } else {
131 108 QTC::TC("examples", "pdf-mod-info usage junk");
132 109 usage();
133 110 }
134 111 }
135   - if (! fl_in)
136   - {
  112 + if (!fl_in) {
137 113 QTC::TC("examples", "pdf-mod-info no in file");
138 114 usage();
139 115 }
140   - if (! fl_out)
141   - {
  116 + if (!fl_out) {
142 117 QTC::TC("examples", "pdf-mod-info in-place");
143 118 fl_out = fl_in;
144 119 }
145   - if (Keys.size() == 0)
146   - {
  120 + if (Keys.size() == 0) {
147 121 QTC::TC("examples", "pdf-mod-info no keys");
148 122 usage();
149 123 }
... ... @@ -151,8 +125,7 @@ int main(int argc, char* argv[])
151 125 std::string fl_tmp = fl_out;
152 126 fl_tmp += ".tmp";
153 127  
154   - try
155   - {
  128 + try {
156 129 QPDF file;
157 130 file.processFile(fl_in);
158 131  
... ... @@ -160,28 +133,22 @@ int main(int argc, char* argv[])
160 133 QPDFObjectHandle fileinfo;
161 134  
162 135 for (std::map<std::string, std::string>::const_iterator it =
163   - Keys.begin(); Keys.end() != it; ++it)
164   - {
165   - if (! fileinfo.isInitialized())
166   - {
167   - if (filetrailer.hasKey("/Info"))
168   - {
  136 + Keys.begin();
  137 + Keys.end() != it;
  138 + ++it) {
  139 + if (!fileinfo.isInitialized()) {
  140 + if (filetrailer.hasKey("/Info")) {
169 141 QTC::TC("examples", "pdf-mod-info has info");
170 142 fileinfo = filetrailer.getKey("/Info");
171   - }
172   - else
173   - {
  143 + } else {
174 144 QTC::TC("examples", "pdf-mod-info file no info");
175 145 fileinfo = QPDFObjectHandle::newDictionary();
176 146 filetrailer.replaceKey("/Info", fileinfo);
177 147 }
178 148 }
179   - if (it->second == "")
180   - {
  149 + if (it->second == "") {
181 150 fileinfo.removeKey(it->first);
182   - }
183   - else
184   - {
  151 + } else {
185 152 QPDFObjectHandle elt = fileinfo.newString(it->second);
186 153 elt.makeDirect();
187 154 fileinfo.replaceKey(it->first, elt);
... ... @@ -192,21 +159,17 @@ int main(int argc, char* argv[])
192 159 w.setLinearization(true);
193 160 w.setStaticID(static_id); // for testing only
194 161 w.write();
195   - }
196   - catch (std::exception& e)
197   - {
  162 + } catch (std::exception& e) {
198 163 std::cerr << e.what() << std::endl;
199 164 exit(2);
200 165 }
201 166  
202   - try
203   - {
204   - (void) remove(fl_out);
205   - QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out),
206   - rename(fl_tmp.c_str(), fl_out));
207   - }
208   - catch (std::exception& e)
209   - {
  167 + try {
  168 + (void)remove(fl_out);
  169 + QUtil::os_wrapper(
  170 + "rename " + fl_tmp + " " + std::string(fl_out),
  171 + rename(fl_tmp.c_str(), fl_out));
  172 + } catch (std::exception& e) {
210 173 std::cerr << e.what() << std::endl;
211 174 exit(2);
212 175 }
... ...
examples/pdf-name-number-tree.cc
... ... @@ -3,26 +3,26 @@
3 3 #include <qpdf/QPDFNumberTreeObjectHelper.hh>
4 4 #include <qpdf/QPDFWriter.hh>
5 5 #include <qpdf/QUtil.hh>
6   -#include <iostream>
7 6 #include <cstring>
  7 +#include <iostream>
8 8  
9 9 static char const* whoami = 0;
10 10  
11   -void usage()
  11 +void
  12 +usage()
12 13 {
13   - std::cerr << "Usage: " << whoami << " outfile.pdf"
14   - << std::endl
  14 + std::cerr << "Usage: " << whoami << " outfile.pdf" << std::endl
15 15 << "Create some name/number trees and write to a file"
16 16 << std::endl;
17 17 exit(2);
18 18 }
19 19  
20   -int main(int argc, char* argv[])
  20 +int
  21 +main(int argc, char* argv[])
21 22 {
22 23 whoami = QUtil::getWhoami(argv[0]);
23 24  
24   - if (argc != 2)
25   - {
  25 + if (argc != 2) {
26 26 usage();
27 27 }
28 28  
... ... @@ -62,8 +62,8 @@ int main(int argc, char* argv[])
62 62 name_tree.insert("R", QPDFObjectHandle::newUnicodeString("rook"));
63 63 name_tree.insert("B", QPDFObjectHandle::newUnicodeString("bishop"));
64 64 name_tree.insert("N", QPDFObjectHandle::newUnicodeString("knight"));
65   - auto iter = name_tree.insert(
66   - "P", QPDFObjectHandle::newUnicodeString("pawn"));
  65 + auto iter =
  66 + name_tree.insert("P", QPDFObjectHandle::newUnicodeString("pawn"));
67 67 // Look at the iterator
68 68 std::cout << "just inserted " << iter->first << " -> "
69 69 << iter->second.unparse() << std::endl;
... ... @@ -77,28 +77,24 @@ int main(int argc, char* argv[])
77 77  
78 78 // Use range-for iteration
79 79 std::cout << "Name tree items:" << std::endl;
80   - for (auto i: name_tree)
81   - {
82   - std::cout << " " << i.first << " -> "
83   - << i.second.unparse() << std::endl;
  80 + for (auto i : name_tree) {
  81 + std::cout << " " << i.first << " -> " << i.second.unparse()
  82 + << std::endl;
84 83 }
85 84  
86 85 // This is a small tree, so everything will be at the root. We can
87 86 // look at it using dictionary and array iterators.
88 87 std::cout << "Keys in name tree object:" << std::endl;
89 88 QPDFObjectHandle names;
90   - for (auto const& i: name_tree_oh.ditems())
91   - {
  89 + for (auto const& i : name_tree_oh.ditems()) {
92 90 std::cout << i.first << std::endl;
93   - if (i.first == "/Names")
94   - {
  91 + if (i.first == "/Names") {
95 92 names = i.second;
96 93 }
97 94 }
98 95 // Values in names array:
99 96 std::cout << "Values in names:" << std::endl;
100   - for (auto& i: names.aitems())
101   - {
  97 + for (auto& i : names.aitems()) {
102 98 std::cout << " " << i.unparse() << std::endl;
103 99 }
104 100  
... ... @@ -112,8 +108,8 @@ int main(int argc, char* argv[])
112 108  
113 109 // 10.2 API
114 110 iter = name_tree.find("Q");
115   - std::cout << "Q: " << iter->first << " -> "
116   - << iter->second.unparse() << std::endl;
  111 + std::cout << "Q: " << iter->first << " -> " << iter->second.unparse()
  112 + << std::endl;
117 113 iter = name_tree.find("W");
118 114 std::cout << "W found: " << (iter != name_tree.end()) << std::endl;
119 115 // Allow find to return predecessor
... ... @@ -146,22 +142,18 @@ int main(int argc, char* argv[])
146 142 auto number_tree_oh = number_tree.getObjectHandle();
147 143 example.replaceKey("/NumberTree", number_tree_oh);
148 144 auto iter2 = number_tree.begin();
149   - for (int i = 7; i <= 350; i += 7)
150   - {
151   - iter2.insertAfter(i, QPDFObjectHandle::newString(
152   - "-" + QUtil::int_to_string(i) + "-"));
  145 + for (int i = 7; i <= 350; i += 7) {
  146 + iter2.insertAfter(
  147 + i,
  148 + QPDFObjectHandle::newString("-" + QUtil::int_to_string(i) + "-"));
153 149 }
154 150 std::cout << "Numbers:" << std::endl;
155 151 int n = 1;
156   - for (auto& i: number_tree)
157   - {
  152 + for (auto& i : number_tree) {
158 153 std::cout << i.first << " -> " << i.second.getUTF8Value();
159   - if (n % 5)
160   - {
  154 + if (n % 5) {
161 155 std::cout << ", ";
162   - }
163   - else
164   - {
  156 + } else {
165 157 std::cout << std::endl;
166 158 }
167 159 ++n;
... ... @@ -171,28 +163,20 @@ int main(int argc, char* argv[])
171 163 // advances. This makes it possible to filter while iterating.
172 164 // Remove all items that are multiples of 5.
173 165 iter2 = number_tree.begin();
174   - while (iter2 != number_tree.end())
175   - {
176   - if (iter2->first % 5 == 0)
177   - {
178   - iter2.remove(); // also advances
179   - }
180   - else
181   - {
  166 + while (iter2 != number_tree.end()) {
  167 + if (iter2->first % 5 == 0) {
  168 + iter2.remove(); // also advances
  169 + } else {
182 170 ++iter2;
183 171 }
184 172 }
185 173 std::cout << "Numbers after filtering:" << std::endl;
186 174 n = 1;
187   - for (auto& i: number_tree)
188   - {
  175 + for (auto& i : number_tree) {
189 176 std::cout << i.first << " -> " << i.second.getUTF8Value();
190   - if (n % 5)
191   - {
  177 + if (n % 5) {
192 178 std::cout << ", ";
193   - }
194   - else
195   - {
  179 + } else {
196 180 std::cout << std::endl;
197 181 }
198 182 ++n;
... ...
examples/pdf-npages.cc
1 1 #include <iostream>
2   -#include <string.h>
3 2 #include <stdlib.h>
  3 +#include <string.h>
4 4  
5 5 #include <qpdf/QPDF.hh>
6 6 #include <qpdf/QUtil.hh>
7 7  
8 8 static char const* whoami = 0;
9 9  
10   -void usage()
  10 +void
  11 +usage()
11 12 {
12 13 std::cerr << "Usage: " << whoami << " filename" << std::endl
13 14 << "Prints the number of pages in filename" << std::endl;
14 15 exit(2);
15 16 }
16 17  
17   -int main(int argc, char* argv[])
  18 +int
  19 +main(int argc, char* argv[])
18 20 {
19 21 whoami = QUtil::getWhoami(argv[0]);
20 22  
21   - if ((argc == 2) && (strcmp(argv[1], "--version") == 0))
22   - {
  23 + if ((argc == 2) && (strcmp(argv[1], "--version") == 0)) {
23 24 std::cout << whoami << " version 1.3" << std::endl;
24 25 exit(0);
25 26 }
26 27  
27   - if (argc != 2)
28   - {
  28 + if (argc != 2) {
29 29 usage();
30 30 }
31 31 char const* filename = argv[1];
32 32  
33   - try
34   - {
  33 + try {
35 34 QPDF pdf;
36 35 pdf.processFile(filename);
37 36 QPDFObjectHandle root = pdf.getRoot();
38 37 QPDFObjectHandle pages = root.getKey("/Pages");
39 38 QPDFObjectHandle count = pages.getKey("/Count");
40 39 std::cout << count.getIntValue() << std::endl;
41   - }
42   - catch (std::exception& e)
43   - {
  40 + } catch (std::exception& e) {
44 41 std::cerr << whoami << ": " << e.what() << std::endl;
45 42 exit(2);
46 43 }
... ...
examples/pdf-overlay-page.cc
1   -#include <iostream>
2   -#include <string.h>
3   -#include <stdlib.h>
4 1 #include <qpdf/QPDF.hh>
5 2 #include <qpdf/QPDFPageDocumentHelper.hh>
6 3 #include <qpdf/QPDFPageObjectHelper.hh>
7 4 #include <qpdf/QPDFWriter.hh>
8 5 #include <qpdf/QUtil.hh>
  6 +#include <iostream>
  7 +#include <stdlib.h>
  8 +#include <string.h>
9 9  
10 10 // This program demonstrates use of form XObjects to overlay a page
11 11 // from one file onto all pages of another file. The qpdf program's
... ... @@ -14,19 +14,17 @@
14 14  
15 15 static char const* whoami = 0;
16 16  
17   -void usage()
  17 +void
  18 +usage()
18 19 {
19   - std::cerr << "Usage: " << whoami << " infile pagefile outfile"
20   - << std::endl
  20 + std::cerr << "Usage: " << whoami << " infile pagefile outfile" << std::endl
21 21 << "Stamp page 1 of pagefile on every page of infile,"
22   - << " writing to outfile"
23   - << std::endl;
  22 + << " writing to outfile" << std::endl;
24 23 exit(2);
25 24 }
26 25  
27   -static void stamp_page(char const* infile,
28   - char const* stampfile,
29   - char const* outfile)
  26 +static void
  27 +stamp_page(char const* infile, char const* stampfile, char const* outfile)
30 28 {
31 29 QPDF inpdf;
32 30 inpdf.processFile(infile);
... ... @@ -45,8 +43,8 @@ static void stamp_page(char const* infile,
45 43 std::vector<QPDFPageObjectHelper> pages =
46 44 QPDFPageDocumentHelper(inpdf).getAllPages();
47 45 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
48   - iter != pages.end(); ++iter)
49   - {
  46 + iter != pages.end();
  47 + ++iter) {
50 48 QPDFPageObjectHelper& ph = *iter;
51 49  
52 50 // Find a unique resource name for the new form XObject
... ... @@ -57,11 +55,9 @@ static void stamp_page(char const* infile,
57 55 // Generate content to place the form XObject centered within
58 56 // destination page's trim box.
59 57 QPDFMatrix m;
60   - std::string content =
61   - ph.placeFormXObject(
62   - stamp_fo, name, ph.getTrimBox().getArrayAsRectangle(), m);
63   - if (! content.empty())
64   - {
  58 + std::string content = ph.placeFormXObject(
  59 + stamp_fo, name, ph.getTrimBox().getArrayAsRectangle(), m);
  60 + if (!content.empty()) {
65 61 // Append the content to the page's content. Surround the
66 62 // original content with q...Q to the new content from the
67 63 // page's original content.
... ... @@ -80,28 +76,25 @@ static void stamp_page(char const* infile,
80 76 }
81 77  
82 78 QPDFWriter w(inpdf, outfile);
83   - w.setStaticID(true); // for testing only
  79 + w.setStaticID(true); // for testing only
84 80 w.write();
85 81 }
86 82  
87   -int main(int argc, char* argv[])
  83 +int
  84 +main(int argc, char* argv[])
88 85 {
89 86 whoami = QUtil::getWhoami(argv[0]);
90 87  
91   - if (argc != 4)
92   - {
  88 + if (argc != 4) {
93 89 usage();
94 90 }
95 91 char const* infile = argv[1];
96 92 char const* stampfile = argv[2];
97 93 char const* outfile = argv[3];
98 94  
99   - try
100   - {
  95 + try {
101 96 stamp_page(infile, stampfile, outfile);
102   - }
103   - catch (std::exception &e)
104   - {
  97 + } catch (std::exception& e) {
105 98 std::cerr << whoami << ": " << e.what() << std::endl;
106 99 exit(2);
107 100 }
... ...
examples/pdf-parse-content.cc
1 1 #include <iostream>
2   -#include <string.h>
3 2 #include <stdlib.h>
  3 +#include <string.h>
4 4  
  5 +#include <qpdf/QIntC.hh>
5 6 #include <qpdf/QPDF.hh>
6 7 #include <qpdf/QPDFPageDocumentHelper.hh>
7 8 #include <qpdf/QPDFPageObjectHelper.hh>
8 9 #include <qpdf/QUtil.hh>
9   -#include <qpdf/QIntC.hh>
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -void usage()
  13 +void
  14 +usage()
14 15 {
15 16 std::cerr << "Usage: " << whoami << " filename page-number" << std::endl
16 17 << "Prints a dump of the objects in the content streams"
... ... @@ -38,17 +39,14 @@ ParserCallbacks::contentSize(size_t size)
38 39 }
39 40  
40 41 void
41   -ParserCallbacks::handleObject(QPDFObjectHandle obj,
42   - size_t offset, size_t length)
  42 +ParserCallbacks::handleObject(
  43 + QPDFObjectHandle obj, size_t offset, size_t length)
43 44 {
44 45 std::cout << obj.getTypeName() << ", offset=" << offset
45 46 << ", length=" << length << ": ";
46   - if (obj.isInlineImage())
47   - {
  47 + if (obj.isInlineImage()) {
48 48 std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
49   - }
50   - else
51   - {
  49 + } else {
52 50 std::cout << obj.unparse() << std::endl;
53 51 }
54 52 }
... ... @@ -59,34 +57,30 @@ ParserCallbacks::handleEOF()
59 57 std::cout << "-EOF-" << std::endl;
60 58 }
61 59  
62   -int main(int argc, char* argv[])
  60 +int
  61 +main(int argc, char* argv[])
63 62 {
64 63 whoami = QUtil::getWhoami(argv[0]);
65 64  
66   - if (argc != 3)
67   - {
  65 + if (argc != 3) {
68 66 usage();
69 67 }
70 68 char const* filename = argv[1];
71 69 int pageno = QUtil::string_to_int(argv[2]);
72 70  
73   - try
74   - {
  71 + try {
75 72 QPDF pdf;
76 73 pdf.processFile(filename);
77 74 std::vector<QPDFPageObjectHelper> pages =
78 75 QPDFPageDocumentHelper(pdf).getAllPages();
79   - if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size()))
80   - {
  76 + if ((pageno < 1) || (QIntC::to_size(pageno) > pages.size())) {
81 77 usage();
82 78 }
83 79  
84   - QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno-1));
  80 + QPDFPageObjectHelper& page = pages.at(QIntC::to_size(pageno - 1));
85 81 ParserCallbacks cb;
86 82 page.parseContents(&cb);
87   - }
88   - catch (std::exception& e)
89   - {
  83 + } catch (std::exception& e) {
90 84 std::cerr << whoami << ": " << e.what() << std::endl;
91 85 exit(2);
92 86 }
... ...
examples/pdf-set-form-values.cc
1   -#include <stdio.h>
2   -#include <string.h>
3   -#include <iostream>
4   -#include <stdlib.h>
5 1 #include <qpdf/QPDF.hh>
6   -#include <qpdf/QPDFPageDocumentHelper.hh>
7 2 #include <qpdf/QPDFAcroFormDocumentHelper.hh>
  3 +#include <qpdf/QPDFPageDocumentHelper.hh>
8 4 #include <qpdf/QPDFWriter.hh>
9 5 #include <qpdf/QUtil.hh>
  6 +#include <iostream>
  7 +#include <stdio.h>
  8 +#include <stdlib.h>
  9 +#include <string.h>
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -void usage()
  13 +void
  14 +usage()
14 15 {
15 16 std::cerr << "Usage: " << whoami << " infile.pdf outfile.pdf value"
16 17 << std::endl
... ... @@ -19,13 +20,12 @@ void usage()
19 20 exit(2);
20 21 }
21 22  
22   -
23   -int main(int argc, char* argv[])
  23 +int
  24 +main(int argc, char* argv[])
24 25 {
25 26 whoami = QUtil::getWhoami(argv[0]);
26 27  
27   - if (argc != 4)
28   - {
  28 + if (argc != 4) {
29 29 usage();
30 30 }
31 31  
... ... @@ -39,8 +39,7 @@ int main(int argc, char* argv[])
39 39 // to illustrate use of the helper classes around interactive
40 40 // forms.
41 41  
42   - try
43   - {
  42 + try {
44 43 QPDF qpdf;
45 44 qpdf.processFile(infilename);
46 45  
... ... @@ -55,8 +54,8 @@ int main(int argc, char* argv[])
55 54 std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
56 55 for (std::vector<QPDFPageObjectHelper>::iterator page_iter =
57 56 pages.begin();
58   - page_iter != pages.end(); ++page_iter)
59   - {
  57 + page_iter != pages.end();
  58 + ++page_iter) {
60 59 // Get all widget annotations for each page. Widget
61 60 // annotations are the ones that contain the details of
62 61 // what's in a form field.
... ... @@ -64,14 +63,13 @@ int main(int argc, char* argv[])
64 63 afdh.getWidgetAnnotationsForPage(*page_iter);
65 64 for (std::vector<QPDFAnnotationObjectHelper>::iterator annot_iter =
66 65 annotations.begin();
67   - annot_iter != annotations.end(); ++annot_iter)
68   - {
  66 + annot_iter != annotations.end();
  67 + ++annot_iter) {
69 68 // For each annotation, find its associated field. If
70 69 // it's a text field, set its value.
71 70 QPDFFormFieldObjectHelper ffh =
72 71 afdh.getFieldForAnnotation(*annot_iter);
73   - if (ffh.getFieldType() == "/Tx")
74   - {
  72 + if (ffh.getFieldType() == "/Tx") {
75 73 // Set the value. Passing false as the second
76 74 // value prevents qpdf from setting
77 75 // /NeedAppearances to true (but will not turn it
... ... @@ -92,9 +90,7 @@ int main(int argc, char* argv[])
92 90 QPDFWriter w(qpdf, outfilename);
93 91 w.setStaticID(true); // for testing only
94 92 w.write();
95   - }
96   - catch (std::exception &e)
97   - {
  93 + } catch (std::exception& e) {
98 94 std::cerr << whoami << " processing file " << infilename << ": "
99 95 << e.what() << std::endl;
100 96 exit(2);
... ...
examples/pdf-split-pages.cc
... ... @@ -4,22 +4,21 @@
4 4 // does.
5 5 //
6 6  
  7 +#include <qpdf/QIntC.hh>
7 8 #include <qpdf/QPDF.hh>
8 9 #include <qpdf/QPDFPageDocumentHelper.hh>
9 10 #include <qpdf/QPDFWriter.hh>
10 11 #include <qpdf/QUtil.hh>
11   -#include <qpdf/QIntC.hh>
12 12  
  13 +#include <cstring>
13 14 #include <iostream>
14 15 #include <stdlib.h>
15 16 #include <string>
16   -#include <cstring>
17 17  
18 18 static bool static_id = false;
19 19  
20   -static void process(char const* whoami,
21   - char const* infile,
22   - std::string outprefix)
  20 +static void
  21 +process(char const* whoami, char const* infile, std::string outprefix)
23 22 {
24 23 QPDF inpdf;
25 24 inpdf.processFile(infile);
... ... @@ -29,8 +28,8 @@ static void process(char const* whoami,
29 28 QIntC::to_int(QUtil::uint_to_string(pages.size()).length());
30 29 int pageno = 0;
31 30 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
32   - iter != pages.end(); ++iter)
33   - {
  31 + iter != pages.end();
  32 + ++iter) {
34 33 QPDFPageObjectHelper& page(*iter);
35 34 std::string outfile =
36 35 outprefix + QUtil::int_to_string(++pageno, pageno_len) + ".pdf";
... ... @@ -38,8 +37,7 @@ static void process(char const* whoami,
38 37 outpdf.emptyPDF();
39 38 QPDFPageDocumentHelper(outpdf).addPage(page, false);
40 39 QPDFWriter outpdfw(outpdf, outfile.c_str());
41   - if (static_id)
42   - {
  40 + if (static_id) {
43 41 // For the test suite, uncompress streams and use static
44 42 // IDs.
45 43 outpdfw.setStaticID(true); // for testing only
... ... @@ -49,34 +47,31 @@ static void process(char const* whoami,
49 47 }
50 48 }
51 49  
52   -void usage(char const* whoami)
  50 +void
  51 +usage(char const* whoami)
53 52 {
54 53 std::cerr << "Usage: " << whoami << " infile outprefix" << std::endl;
55 54 exit(2);
56 55 }
57 56  
58   -int main(int argc, char* argv[])
  57 +int
  58 +main(int argc, char* argv[])
59 59 {
60 60 char const* whoami = QUtil::getWhoami(argv[0]);
61 61  
62 62 // For test suite
63   - if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0))
64   - {
  63 + if ((argc > 1) && (strcmp(argv[1], " --static-id") == 0)) {
65 64 static_id = true;
66 65 --argc;
67 66 ++argv;
68 67 }
69 68  
70   - if (argc != 3)
71   - {
  69 + if (argc != 3) {
72 70 usage(whoami);
73 71 }
74   - try
75   - {
  72 + try {
76 73 process(whoami, argv[1], argv[2]);
77   - }
78   - catch (std::exception const& e)
79   - {
  74 + } catch (std::exception const& e) {
80 75 std::cerr << whoami << ": exception: " << e.what() << std::endl;
81 76 return 2;
82 77 }
... ...
examples/qpdf-job.cc
  1 +#include <qpdf/QIntC.hh>
1 2 #include <qpdf/QPDFJob.hh>
2 3 #include <qpdf/QUtil.hh>
3   -#include <qpdf/QIntC.hh>
4 4  
5   -#include <iostream>
6 5 #include <cstring>
  6 +#include <iostream>
7 7  
8 8 // This program is a simple demonstration of different ways to use the
9 9 // QPDFJob API.
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -static void usage()
  13 +static void
  14 +usage()
14 15 {
15   - std::cerr
16   - << "Usage: " << whoami << std::endl
17   - << "This program linearizes the first page of in.pdf to out1.pdf,"
18   - << " out2.pdf, and"
19   - << std::endl
20   - << " out3.pdf, each demonstrating a different way to use the"
21   - << " QPDFJob API"
22   - << std::endl;
  16 + std::cerr << "Usage: " << whoami << std::endl
  17 + << "This program linearizes the first page of in.pdf to out1.pdf,"
  18 + << " out2.pdf, and" << std::endl
  19 + << " out3.pdf, each demonstrating a different way to use the"
  20 + << " QPDFJob API" << std::endl;
23 21 exit(2);
24 22 }
25 23  
26   -int main(int argc, char* argv[])
  24 +int
  25 +main(int argc, char* argv[])
27 26 {
28 27 whoami = QUtil::getWhoami(argv[0]);
29 28  
30   - if (argc != 1)
31   - {
  29 + if (argc != 1) {
32 30 usage();
33 31 }
34 32  
... ... @@ -39,8 +37,7 @@ int main(int argc, char* argv[])
39 37  
40 38 // Note that staticId is used for testing only.
41 39  
42   - try
43   - {
  40 + try {
44 41 // Use the config API
45 42 QPDFJob j;
46 43 j.config()
... ... @@ -54,15 +51,12 @@ int main(int argc, char* argv[])
54 51 ->checkConfiguration();
55 52 j.run();
56 53 std::cout << "out1 status: " << j.getExitCode() << std::endl;
57   - }
58   - catch (std::exception& e)
59   - {
  54 + } catch (std::exception& e) {
60 55 std::cerr << "exception: " << e.what() << std::endl;
61 56 return 2;
62 57 }
63 58  
64   - try
65   - {
  59 + try {
66 60 char const* new_argv[] = {
67 61 whoami,
68 62 "in.pdf",
... ... @@ -73,21 +67,17 @@ int main(int argc, char* argv[])
73 67 "1",
74 68 "--",
75 69 "--static-id",
76   - nullptr
77   - };
  70 + nullptr};
78 71 QPDFJob j;
79 72 j.initializeFromArgv(new_argv);
80 73 j.run();
81 74 std::cout << "out2 status: " << j.getExitCode() << std::endl;
82   - }
83   - catch (std::exception& e)
84   - {
  75 + } catch (std::exception& e) {
85 76 std::cerr << "exception: " << e.what() << std::endl;
86 77 return 2;
87 78 }
88 79  
89   - try
90   - {
  80 + try {
91 81 // Use the JSON API
92 82 QPDFJob j;
93 83 j.initializeFromJson(R"({
... ... @@ -105,9 +95,7 @@ int main(int argc, char* argv[])
105 95 )");
106 96 j.run();
107 97 std::cout << "out3 status: " << j.getExitCode() << std::endl;
108   - }
109   - catch (std::exception& e)
110   - {
  98 + } catch (std::exception& e) {
111 99 std::cerr << "exception: " << e.what() << std::endl;
112 100 return 2;
113 101 }
... ...
examples/qpdfjob-c.c
... ... @@ -10,13 +10,15 @@
10 10  
11 11 static char const* whoami = 0;
12 12  
13   -static void usage()
  13 +static void
  14 +usage()
14 15 {
15 16 fprintf(stderr, "Usage: %s infile outfile\n", whoami);
16 17 exit(2);
17 18 }
18 19  
19   -int main(int argc, char* argv[])
  20 +int
  21 +main(int argc, char* argv[])
20 22 {
21 23 char* infile = NULL;
22 24 char* outfile = NULL;
... ... @@ -24,21 +26,15 @@ int main(int argc, char* argv[])
24 26 int r = 0;
25 27 char* p = 0;
26 28  
27   - if ((p = strrchr(argv[0], '/')) != NULL)
28   - {
  29 + if ((p = strrchr(argv[0], '/')) != NULL) {
29 30 whoami = p + 1;
30   - }
31   - else if ((p = strrchr(argv[0], '\\')) != NULL)
32   - {
  31 + } else if ((p = strrchr(argv[0], '\\')) != NULL) {
33 32 whoami = p + 1;
34   - }
35   - else
36   - {
  33 + } else {
37 34 whoami = argv[0];
38 35 }
39 36  
40   - if (argc != 3)
41   - {
  37 + if (argc != 3) {
42 38 usage();
43 39 }
44 40  
... ...
fuzz/ascii85_fuzzer.cc
1   -#include <qpdf/Pl_Discard.hh>
2 1 #include <qpdf/Pl_ASCII85Decoder.hh>
  2 +#include <qpdf/Pl_Discard.hh>
3 3 #include <iostream>
4 4 #include <stdexcept>
5 5  
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/dct_fuzzer.cc
1   -#include <qpdf/Pl_Discard.hh>
2 1 #include <qpdf/Pl_DCT.hh>
  2 +#include <qpdf/Pl_Discard.hh>
  3 +#include <cstdlib>
3 4 #include <iostream>
4 5 #include <stdexcept>
5   -#include <cstdlib>
6 6  
7 7 class FuzzHelper
8 8 {
... ... @@ -35,17 +35,15 @@ FuzzHelper::doChecks()
35 35 void
36 36 FuzzHelper::run()
37 37 {
38   - try
39   - {
  38 + try {
40 39 doChecks();
41   - }
42   - catch (std::runtime_error const& e)
43   - {
  40 + } catch (std::runtime_error const& e) {
44 41 std::cerr << "runtime_error: " << e.what() << std::endl;
45 42 }
46 43 }
47 44  
48   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  45 +extern "C" int
  46 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
49 47 {
50 48 #ifndef _WIN32
51 49 // Used by jpeg library to work around false positives in memory
... ...
fuzz/flate_fuzzer.cc
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/hex_fuzzer.cc
1   -#include <qpdf/Pl_Discard.hh>
2 1 #include <qpdf/Pl_ASCIIHexDecoder.hh>
  2 +#include <qpdf/Pl_Discard.hh>
3 3 #include <iostream>
4 4 #include <stdexcept>
5 5  
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/lzw_fuzzer.cc
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/pngpredictor_fuzzer.cc
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/qpdf_fuzzer.cc
1   -#include <qpdf/QPDF.hh>
2   -#include <qpdf/QPDFWriter.hh>
3   -#include <qpdf/QUtil.hh>
4   -#include <qpdf/BufferInputSource.hh>
5 1 #include <qpdf/Buffer.hh>
  2 +#include <qpdf/BufferInputSource.hh>
6 3 #include <qpdf/Pl_Discard.hh>
  4 +#include <qpdf/QPDF.hh>
  5 +#include <qpdf/QPDFAcroFormDocumentHelper.hh>
  6 +#include <qpdf/QPDFOutlineDocumentHelper.hh>
7 7 #include <qpdf/QPDFPageDocumentHelper.hh>
8   -#include <qpdf/QPDFPageObjectHelper.hh>
9 8 #include <qpdf/QPDFPageLabelDocumentHelper.hh>
10   -#include <qpdf/QPDFOutlineDocumentHelper.hh>
11   -#include <qpdf/QPDFAcroFormDocumentHelper.hh>
  9 +#include <qpdf/QPDFPageObjectHelper.hh>
  10 +#include <qpdf/QPDFWriter.hh>
  11 +#include <qpdf/QUtil.hh>
12 12 #include <cstdlib>
13 13  
14 14 class DiscardContents: public QPDFObjectHandle::ParserCallbacks
15 15 {
16 16 public:
17   - virtual ~DiscardContents() {}
18   - virtual void handleObject(QPDFObjectHandle) {}
19   - virtual void handleEOF() {}
  17 + virtual ~DiscardContents()
  18 + {
  19 + }
  20 + virtual void
  21 + handleObject(QPDFObjectHandle)
  22 + {
  23 + }
  24 + virtual void
  25 + handleEOF()
  26 + {
  27 + }
20 28 };
21 29  
22 30 class FuzzHelper
... ... @@ -66,16 +74,11 @@ FuzzHelper::getWriter(PointerHolder&lt;QPDF&gt; qpdf)
66 74 void
67 75 FuzzHelper::doWrite(PointerHolder<QPDFWriter> w)
68 76 {
69   - try
70   - {
  77 + try {
71 78 w->write();
72   - }
73   - catch (QPDFExc const& e)
74   - {
  79 + } catch (QPDFExc const& e) {
75 80 std::cerr << e.what() << std::endl;
76   - }
77   - catch (std::runtime_error const& e)
78   - {
  81 + } catch (std::runtime_error const& e) {
79 82 std::cerr << e.what() << std::endl;
80 83 }
81 84 }
... ... @@ -133,14 +136,12 @@ FuzzHelper::testPages()
133 136 std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
134 137 DiscardContents discard_contents;
135 138 int pageno = 0;
136   - for (std::vector<QPDFPageObjectHelper>::iterator iter =
137   - pages.begin();
138   - iter != pages.end(); ++iter)
139   - {
  139 + for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
  140 + iter != pages.end();
  141 + ++iter) {
140 142 QPDFPageObjectHelper& page(*iter);
141 143 ++pageno;
142   - try
143   - {
  144 + try {
144 145 page.coalesceContentStreams();
145 146 page.parseContents(&discard_contents);
146 147 page.getImages();
... ... @@ -153,16 +154,13 @@ FuzzHelper::testPages()
153 154 afdh.getWidgetAnnotationsForPage(page);
154 155 for (std::vector<QPDFAnnotationObjectHelper>::iterator annot_iter =
155 156 annotations.begin();
156   - annot_iter != annotations.end(); ++annot_iter)
157   - {
  157 + annot_iter != annotations.end();
  158 + ++annot_iter) {
158 159 QPDFAnnotationObjectHelper& aoh = *annot_iter;
159 160 afdh.getFieldForAnnotation(aoh);
160 161 }
161   - }
162   - catch (QPDFExc& e)
163   - {
164   - std::cerr << "page " << pageno << ": "
165   - << e.what() << std::endl;
  162 + } catch (QPDFExc& e) {
  163 + std::cerr << "page " << pageno << ": " << e.what() << std::endl;
166 164 }
167 165 }
168 166 }
... ... @@ -171,16 +169,15 @@ void
171 169 FuzzHelper::testOutlines()
172 170 {
173 171 PointerHolder<QPDF> q = getQpdf();
174   - std::list<std::vector<QPDFOutlineObjectHelper> > queue;
  172 + std::list<std::vector<QPDFOutlineObjectHelper>> queue;
175 173 QPDFOutlineDocumentHelper odh(*q);
176 174 queue.push_back(odh.getTopLevelOutlines());
177   - while (! queue.empty())
178   - {
  175 + while (!queue.empty()) {
179 176 std::vector<QPDFOutlineObjectHelper>& outlines = *(queue.begin());
180 177 for (std::vector<QPDFOutlineObjectHelper>::iterator iter =
181 178 outlines.begin();
182   - iter != outlines.end(); ++iter)
183   - {
  179 + iter != outlines.end();
  180 + ++iter) {
184 181 QPDFOutlineObjectHelper& ol = *iter;
185 182 ol.getDestPage();
186 183 queue.push_back(ol.getKids());
... ... @@ -208,21 +205,17 @@ FuzzHelper::run()
208 205 // std::runtime_error. Throwing any other kind of exception,
209 206 // segfaulting, or having a memory error (when built with
210 207 // appropriate sanitizers) will all cause abnormal exit.
211   - try
212   - {
  208 + try {
213 209 doChecks();
214   - }
215   - catch (QPDFExc const& e)
216   - {
  210 + } catch (QPDFExc const& e) {
217 211 std::cerr << "QPDFExc: " << e.what() << std::endl;
218   - }
219   - catch (std::runtime_error const& e)
220   - {
  212 + } catch (std::runtime_error const& e) {
221 213 std::cerr << "runtime_error: " << e.what() << std::endl;
222 214 }
223 215 }
224 216  
225   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  217 +extern "C" int
  218 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
226 219 {
227 220 #ifndef _WIN32
228 221 // Used by jpeg library to work around false positives in memory
... ...
fuzz/runlength_fuzzer.cc
... ... @@ -34,17 +34,15 @@ FuzzHelper::doChecks()
34 34 void
35 35 FuzzHelper::run()
36 36 {
37   - try
38   - {
  37 + try {
39 38 doChecks();
40   - }
41   - catch (std::runtime_error const& e)
42   - {
  39 + } catch (std::runtime_error const& e) {
43 40 std::cerr << "runtime_error: " << e.what() << std::endl;
44 41 }
45 42 }
46 43  
47   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  44 +extern "C" int
  45 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
48 46 {
49 47 FuzzHelper f(data, size);
50 48 f.run();
... ...
fuzz/standalone_fuzz_target_runner.cc
... ... @@ -4,10 +4,10 @@
4 4  
5 5 extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size);
6 6  
7   -int main(int argc, char **argv)
  7 +int
  8 +main(int argc, char** argv)
8 9 {
9   - for (int i = 1; i < argc; i++)
10   - {
  10 + for (int i = 1; i < argc; i++) {
11 11 PointerHolder<char> file_buf;
12 12 size_t size = 0;
13 13 QUtil::read_file_into_memory(argv[i], file_buf, size);
... ...
fuzz/tiffpredictor_fuzzer.cc
... ... @@ -26,8 +26,8 @@ void
26 26 FuzzHelper::doChecks()
27 27 {
28 28 Pl_Discard discard;
29   - Pl_TIFFPredictor p("decoder", &discard,
30   - Pl_TIFFPredictor::a_decode, 16, 1, 8);
  29 + Pl_TIFFPredictor p(
  30 + "decoder", &discard, Pl_TIFFPredictor::a_decode, 16, 1, 8);
31 31 p.write(const_cast<unsigned char*>(data), size);
32 32 p.finish();
33 33 }
... ... @@ -35,17 +35,15 @@ FuzzHelper::doChecks()
35 35 void
36 36 FuzzHelper::run()
37 37 {
38   - try
39   - {
  38 + try {
40 39 doChecks();
41   - }
42   - catch (std::runtime_error const& e)
43   - {
  40 + } catch (std::runtime_error const& e) {
44 41 std::cerr << "runtime_error: " << e.what() << std::endl;
45 42 }
46 43 }
47 44  
48   -extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
  45 +extern "C" int
  46 +LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
49 47 {
50 48 FuzzHelper f(data, size);
51 49 f.run();
... ...
include/qpdf/Buffer.hh
... ... @@ -25,8 +25,8 @@
25 25 #include <qpdf/DLL.h>
26 26 #include <qpdf/PointerHolder.hh>
27 27  
28   -#include <stddef.h>
29 28 #include <memory>
  29 +#include <stddef.h>
30 30  
31 31 class Buffer
32 32 {
... ...
include/qpdf/BufferInputSource.hh
... ... @@ -22,8 +22,8 @@
22 22 #ifndef QPDF_BUFFERINPUTSOURCE_HH
23 23 #define QPDF_BUFFERINPUTSOURCE_HH
24 24  
25   -#include <qpdf/InputSource.hh>
26 25 #include <qpdf/Buffer.hh>
  26 +#include <qpdf/InputSource.hh>
27 27  
28 28 class BufferInputSource: public InputSource
29 29 {
... ... @@ -31,11 +31,11 @@ class BufferInputSource: public InputSource
31 31 // If own_memory is true, BufferInputSource will delete the buffer
32 32 // when finished with it. Otherwise, the caller owns the memory.
33 33 QPDF_DLL
34   - BufferInputSource(std::string const& description, Buffer* buf,
35   - bool own_memory = false);
  34 + BufferInputSource(
  35 + std::string const& description, Buffer* buf, bool own_memory = false);
36 36 QPDF_DLL
37   - BufferInputSource(std::string const& description,
38   - std::string const& contents);
  37 + BufferInputSource(
  38 + std::string const& description, std::string const& contents);
39 39 QPDF_DLL
40 40 virtual ~BufferInputSource();
41 41 QPDF_DLL
... ...
include/qpdf/Constants.h
... ... @@ -29,16 +29,15 @@
29 29  
30 30 /* Error Codes */
31 31  
32   -enum qpdf_error_code_e
33   -{
  32 +enum qpdf_error_code_e {
34 33 qpdf_e_success = 0,
35   - qpdf_e_internal, /* logic/programming error -- indicates bug */
36   - qpdf_e_system, /* I/O error, memory error, etc. */
37   - qpdf_e_unsupported, /* PDF feature not (yet) supported by qpdf */
38   - qpdf_e_password, /* incorrect password for encrypted file */
39   - qpdf_e_damaged_pdf, /* syntax errors or other damage in PDF */
40   - qpdf_e_pages, /* erroneous or unsupported pages structure */
41   - qpdf_e_object, /* type/bounds errors accessing objects */
  34 + qpdf_e_internal, /* logic/programming error -- indicates bug */
  35 + qpdf_e_system, /* I/O error, memory error, etc. */
  36 + qpdf_e_unsupported, /* PDF feature not (yet) supported by qpdf */
  37 + qpdf_e_password, /* incorrect password for encrypted file */
  38 + qpdf_e_damaged_pdf, /* syntax errors or other damage in PDF */
  39 + qpdf_e_pages, /* erroneous or unsupported pages structure */
  40 + qpdf_e_object, /* type/bounds errors accessing objects */
42 41 };
43 42  
44 43 /* Object Types */
... ... @@ -75,62 +74,56 @@ enum qpdf_object_type_e {
75 74  
76 75 /* Write Parameters. See QPDFWriter.hh for details. */
77 76  
78   -enum qpdf_object_stream_e
79   -{
80   - qpdf_o_disable = 0, /* disable object streams */
81   - qpdf_o_preserve, /* preserve object streams */
82   - qpdf_o_generate /* generate object streams */
  77 +enum qpdf_object_stream_e {
  78 + qpdf_o_disable = 0, /* disable object streams */
  79 + qpdf_o_preserve, /* preserve object streams */
  80 + qpdf_o_generate /* generate object streams */
83 81 };
84   -enum qpdf_stream_data_e
85   -{
86   - qpdf_s_uncompress = 0, /* uncompress stream data */
87   - qpdf_s_preserve, /* preserve stream data compression */
88   - qpdf_s_compress /* compress stream data */
  82 +enum qpdf_stream_data_e {
  83 + qpdf_s_uncompress = 0, /* uncompress stream data */
  84 + qpdf_s_preserve, /* preserve stream data compression */
  85 + qpdf_s_compress /* compress stream data */
89 86 };
90 87  
91 88 /* Stream data flags */
92 89  
93 90 /* See pipeStreamData in QPDFObjectHandle.hh for details on these flags. */
94   -enum qpdf_stream_encode_flags_e
95   -{
96   - qpdf_ef_compress = 1 << 0, /* compress uncompressed streams */
  91 +enum qpdf_stream_encode_flags_e {
  92 + qpdf_ef_compress = 1 << 0, /* compress uncompressed streams */
97 93 qpdf_ef_normalize = 1 << 1, /* normalize content stream */
98 94 };
99   -enum qpdf_stream_decode_level_e
100   -{
  95 +enum qpdf_stream_decode_level_e {
101 96 /* These must be in order from less to more decoding. */
102   - qpdf_dl_none = 0, /* preserve all stream filters */
103   - qpdf_dl_generalized, /* decode general-purpose filters */
104   - qpdf_dl_specialized, /* also decode other non-lossy filters */
105   - qpdf_dl_all /* also decode lossy filters */
  97 + qpdf_dl_none = 0, /* preserve all stream filters */
  98 + qpdf_dl_generalized, /* decode general-purpose filters */
  99 + qpdf_dl_specialized, /* also decode other non-lossy filters */
  100 + qpdf_dl_all /* also decode lossy filters */
106 101 };
107 102  
108 103 /* R3 Encryption Parameters */
109 104  
110   -enum qpdf_r3_print_e
111   -{
112   - qpdf_r3p_full = 0, /* allow all printing */
113   - qpdf_r3p_low, /* allow only low-resolution printing */
114   - qpdf_r3p_none /* allow no printing */
  105 +enum qpdf_r3_print_e {
  106 + qpdf_r3p_full = 0, /* allow all printing */
  107 + qpdf_r3p_low, /* allow only low-resolution printing */
  108 + qpdf_r3p_none /* allow no printing */
115 109 };
116 110  
117 111 /* qpdf_r3_modify_e doesn't allow the full flexibility of the spec. It
118 112 * corresponds to options in Acrobat 5's menus. The new interface in
119 113 * QPDFWriter offers more granularity and no longer uses this type.
120 114 */
121   -enum qpdf_r3_modify_e /* Allowed changes: */
  115 +enum qpdf_r3_modify_e /* Allowed changes: */
122 116 {
123   - qpdf_r3m_all = 0, /* All editing */
124   - qpdf_r3m_annotate, /* Comments, fill forms, signing, assembly */
125   - qpdf_r3m_form, /* Fill forms, signing, assembly */
126   - qpdf_r3m_assembly, /* Only document assembly */
127   - qpdf_r3m_none /* No modifications */
  117 + qpdf_r3m_all = 0, /* All editing */
  118 + qpdf_r3m_annotate, /* Comments, fill forms, signing, assembly */
  119 + qpdf_r3m_form, /* Fill forms, signing, assembly */
  120 + qpdf_r3m_assembly, /* Only document assembly */
  121 + qpdf_r3m_none /* No modifications */
128 122 };
129 123  
130 124 /* Form field flags from the PDF spec */
131 125  
132   -enum pdf_form_field_flag_e
133   -{
  126 +enum pdf_form_field_flag_e {
134 127 /* flags that apply to all form fields */
135 128 ff_all_read_only = 1 << 0,
136 129 ff_all_required = 1 << 1,
... ... @@ -162,8 +155,7 @@ enum pdf_form_field_flag_e
162 155  
163 156 /* Annotation flags from the PDF spec */
164 157  
165   -enum pdf_annotation_flag_e
166   -{
  158 +enum pdf_annotation_flag_e {
167 159 an_invisible = 1 << 0,
168 160 an_hidden = 1 << 1,
169 161 an_print = 1 << 2,
... ... @@ -177,8 +169,7 @@ enum pdf_annotation_flag_e
177 169 };
178 170  
179 171 /* Encryption/password status for QPDFJob */
180   -enum qpdf_encryption_status_e
181   -{
  172 +enum qpdf_encryption_status_e {
182 173 qpdf_es_encrypted = 1 << 0,
183 174 qpdf_es_password_incorrect = 1 << 1
184 175 };
... ...
include/qpdf/DLL.h
... ... @@ -32,7 +32,7 @@
32 32 #if (defined _WIN32 || defined __CYGWIN__) && defined(DLL_EXPORT)
33 33 # define QPDF_DLL __declspec(dllexport)
34 34 #elif defined __GNUC__
35   -# define QPDF_DLL __attribute__ ((visibility ("default")))
  35 +# define QPDF_DLL __attribute__((visibility("default")))
36 36 #else
37 37 # define QPDF_DLL
38 38 #endif
... ...
include/qpdf/InputSource.hh
... ... @@ -23,12 +23,12 @@
23 23 #define QPDF_INPUTSOURCE_HH
24 24  
25 25 #include <qpdf/DLL.h>
26   -#include <qpdf/Types.h>
27 26 #include <qpdf/PointerHolder.hh>
  27 +#include <qpdf/Types.h>
28 28  
  29 +#include <memory>
29 30 #include <stdio.h>
30 31 #include <string>
31   -#include <memory>
32 32  
33 33 class QPDF_DLL_CLASS InputSource
34 34 {
... ... @@ -71,13 +71,17 @@ class QPDF_DLL_CLASS InputSource
71 71 // methods return true and leave the input source positioned
72 72 // wherever check() left it at the end of the matching pattern.
73 73 QPDF_DLL
74   - bool findFirst(char const* start_chars,
75   - qpdf_offset_t offset, size_t len,
76   - Finder& finder);
  74 + bool findFirst(
  75 + char const* start_chars,
  76 + qpdf_offset_t offset,
  77 + size_t len,
  78 + Finder& finder);
77 79 QPDF_DLL
78   - bool findLast(char const* start_chars,
79   - qpdf_offset_t offset, size_t len,
80   - Finder& finder);
  80 + bool findLast(
  81 + char const* start_chars,
  82 + qpdf_offset_t offset,
  83 + size_t len,
  84 + Finder& finder);
81 85  
82 86 virtual qpdf_offset_t findAndSkipNextEOL() = 0;
83 87 virtual std::string const& getName() const = 0;
... ...
include/qpdf/JSON.hh
... ... @@ -38,12 +38,12 @@
38 38 #include <qpdf/DLL.h>
39 39 #include <qpdf/PointerHolder.hh>
40 40  
41   -#include <string>
42   -#include <map>
43   -#include <vector>
44   -#include <list>
45 41 #include <functional>
  42 +#include <list>
  43 +#include <map>
46 44 #include <memory>
  45 +#include <string>
  46 +#include <vector>
47 47  
48 48 class JSON
49 49 {
... ... @@ -134,14 +134,13 @@ class JSON
134 134 f_optional = 1 << 0,
135 135 };
136 136 QPDF_DLL
137   - bool checkSchema(JSON schema, unsigned long flags,
138   - std::list<std::string>& errors);
  137 + bool checkSchema(
  138 + JSON schema, unsigned long flags, std::list<std::string>& errors);
139 139  
140 140 // Same as passing 0 for flags
141 141 QPDF_DLL
142 142 bool checkSchema(JSON schema, std::list<std::string>& errors);
143 143  
144   -
145 144 // Create a JSON object from a string.
146 145 QPDF_DLL
147 146 static JSON parse(std::string const&);
... ... @@ -198,11 +197,12 @@ class JSON
198 197  
199 198 JSON(std::shared_ptr<JSON_value>);
200 199  
201   - static bool
202   - checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
203   - unsigned long flags,
204   - std::list<std::string>& errors,
205   - std::string prefix);
  200 + static bool checkSchemaInternal(
  201 + JSON_value* this_v,
  202 + JSON_value* sch_v,
  203 + unsigned long flags,
  204 + std::list<std::string>& errors,
  205 + std::string prefix);
206 206  
207 207 class Members
208 208 {
... ... @@ -222,5 +222,4 @@ class JSON
222 222 PointerHolder<Members> m;
223 223 };
224 224  
225   -
226 225 #endif // JSON_HH
... ...
include/qpdf/Pipeline.hh
... ... @@ -47,8 +47,8 @@
47 47 #include <qpdf/DLL.h>
48 48 #include <qpdf/PointerHolder.hh>
49 49  
50   -#include <string>
51 50 #include <memory>
  51 +#include <string>
52 52  
53 53 class QPDF_DLL_CLASS Pipeline
54 54 {
... ...
include/qpdf/Pl_Buffer.hh
... ... @@ -33,9 +33,9 @@
33 33 // For this pipeline, "next" may be null. If a next pointer is
34 34 // provided, this pipeline will also pass the data through to it.
35 35  
  36 +#include <qpdf/Buffer.hh>
36 37 #include <qpdf/Pipeline.hh>
37 38 #include <qpdf/PointerHolder.hh>
38   -#include <qpdf/Buffer.hh>
39 39  
40 40 #include <memory>
41 41  
... ... @@ -68,7 +68,7 @@ class Pl_Buffer: public Pipeline
68 68 // a buffer of size *len allocated with malloc(). It is the
69 69 // caller's responsibility to call free() on the buffer.
70 70 QPDF_DLL
71   - void getMallocBuffer(unsigned char **buf, size_t* len);
  71 + void getMallocBuffer(unsigned char** buf, size_t* len);
72 72  
73 73 private:
74 74 class Members
... ...
include/qpdf/Pl_Count.hh
... ... @@ -25,8 +25,8 @@
25 25 // This pipeline is reusable; i.e., it is safe to call write() after
26 26 // calling finish().
27 27  
28   -#include <qpdf/Types.h>
29 28 #include <qpdf/Pipeline.hh>
  29 +#include <qpdf/Types.h>
30 30  
31 31 class Pl_Count: public Pipeline
32 32 {
... ...
include/qpdf/Pl_DCT.hh
... ... @@ -52,12 +52,14 @@ class Pl_DCT: public Pipeline
52 52  
53 53 // Constructor for compressing image data
54 54 QPDF_DLL
55   - Pl_DCT(char const* identifier, Pipeline* next,
56   - JDIMENSION image_width,
57   - JDIMENSION image_height,
58   - int components,
59   - J_COLOR_SPACE color_space,
60   - CompressConfig* config_callback = 0);
  55 + Pl_DCT(
  56 + char const* identifier,
  57 + Pipeline* next,
  58 + JDIMENSION image_width,
  59 + JDIMENSION image_height,
  60 + int components,
  61 + J_COLOR_SPACE color_space,
  62 + CompressConfig* config_callback = 0);
61 63  
62 64 QPDF_DLL
63 65 virtual ~Pl_DCT();
... ... @@ -82,13 +84,14 @@ class Pl_DCT: public Pipeline
82 84 ~Members();
83 85  
84 86 private:
85   - Members(action_e action,
86   - char const* buf_description,
87   - JDIMENSION image_width = 0,
88   - JDIMENSION image_height = 0,
89   - int components = 1,
90   - J_COLOR_SPACE color_space = JCS_GRAYSCALE,
91   - CompressConfig* config_callback = 0);
  87 + Members(
  88 + action_e action,
  89 + char const* buf_description,
  90 + JDIMENSION image_width = 0,
  91 + JDIMENSION image_height = 0,
  92 + int components = 1,
  93 + J_COLOR_SPACE color_space = JCS_GRAYSCALE,
  94 + CompressConfig* config_callback = 0);
92 95 Members(Members const&);
93 96  
94 97 action_e action;
... ...
include/qpdf/Pl_Flate.hh
... ... @@ -35,8 +35,11 @@ class Pl_Flate: public Pipeline
35 35 enum action_e { a_inflate, a_deflate };
36 36  
37 37 QPDF_DLL
38   - Pl_Flate(char const* identifier, Pipeline* next,
39   - action_e action, unsigned int out_bufsize = def_bufsize);
  38 + Pl_Flate(
  39 + char const* identifier,
  40 + Pipeline* next,
  41 + action_e action,
  42 + unsigned int out_bufsize = def_bufsize);
40 43 QPDF_DLL
41 44 virtual ~Pl_Flate();
42 45  
... ...
include/qpdf/Pl_QPDFTokenizer.hh
... ... @@ -24,10 +24,10 @@
24 24  
25 25 #include <qpdf/Pipeline.hh>
26 26  
27   -#include <qpdf/QPDFTokenizer.hh>
  27 +#include <qpdf/Pl_Buffer.hh>
28 28 #include <qpdf/PointerHolder.hh>
29 29 #include <qpdf/QPDFObjectHandle.hh>
30   -#include <qpdf/Pl_Buffer.hh>
  30 +#include <qpdf/QPDFTokenizer.hh>
31 31  
32 32 #include <memory>
33 33  
... ... @@ -48,9 +48,10 @@ class Pl_QPDFTokenizer: public Pipeline
48 48 // pipeline that the token filter writes to. If next is not
49 49 // provided, any output written by the filter will be discarded.
50 50 QPDF_DLL
51   - Pl_QPDFTokenizer(char const* identifier,
52   - QPDFObjectHandle::TokenFilter* filter,
53   - Pipeline* next = 0);
  51 + Pl_QPDFTokenizer(
  52 + char const* identifier,
  53 + QPDFObjectHandle::TokenFilter* filter,
  54 + Pipeline* next = 0);
54 55 QPDF_DLL
55 56 virtual ~Pl_QPDFTokenizer();
56 57 QPDF_DLL
... ...
include/qpdf/Pl_RunLength.hh
... ... @@ -30,8 +30,7 @@ class Pl_RunLength: public Pipeline
30 30 enum action_e { a_encode, a_decode };
31 31  
32 32 QPDF_DLL
33   - Pl_RunLength(char const* identifier, Pipeline* next,
34   - action_e action);
  33 + Pl_RunLength(char const* identifier, Pipeline* next, action_e action);
35 34 QPDF_DLL
36 35 virtual ~Pl_RunLength();
37 36  
... ...
include/qpdf/PointerHolder.hh
... ... @@ -187,18 +187,16 @@ class PointerHolder
187 187 }
188 188 ~Data()
189 189 {
190   - if (array)
191   - {
192   - delete [] this->pointer;
193   - }
194   - else
195   - {
  190 + if (array) {
  191 + delete[] this->pointer;
  192 + } else {
196 193 delete this->pointer;
197 194 }
198 195 }
199 196 T* pointer;
200 197 bool array;
201 198 int refcount;
  199 +
202 200 private:
203 201 Data(Data const&) = delete;
204 202 Data& operator=(Data const&) = delete;
... ... @@ -208,7 +206,7 @@ class PointerHolder
208 206 #if POINTERHOLDER_TRANSITION >= 1
209 207 explicit
210 208 #endif // POINTERHOLDER_TRANSITION >= 1
211   - PointerHolder(T* pointer = 0)
  209 + PointerHolder(T* pointer = 0)
212 210 {
213 211 this->init(new Data(pointer, false));
214 212 }
... ... @@ -222,16 +220,17 @@ class PointerHolder
222 220 {
223 221 this->copy(rhs);
224 222 }
225   - PointerHolder& operator=(PointerHolder const& rhs)
  223 + PointerHolder&
  224 + operator=(PointerHolder const& rhs)
226 225 {
227   - if (this != &rhs)
228   - {
  226 + if (this != &rhs) {
229 227 this->destroy();
230 228 this->copy(rhs);
231 229 }
232 230 return *this;
233 231 }
234   - PointerHolder& operator=(decltype(nullptr))
  232 + PointerHolder&
  233 + operator=(decltype(nullptr))
235 234 {
236 235 this->operator=(PointerHolder<T>());
237 236 return *this;
... ... @@ -240,21 +239,25 @@ class PointerHolder
240 239 {
241 240 this->destroy();
242 241 }
243   - bool operator==(PointerHolder const& rhs) const
  242 + bool
  243 + operator==(PointerHolder const& rhs) const
244 244 {
245 245 return this->data->pointer == rhs.data->pointer;
246 246 }
247   - bool operator==(decltype(nullptr)) const
  247 + bool
  248 + operator==(decltype(nullptr)) const
248 249 {
249 250 return this->data->pointer == nullptr;
250 251 }
251   - bool operator<(PointerHolder const& rhs) const
  252 + bool
  253 + operator<(PointerHolder const& rhs) const
252 254 {
253 255 return this->data->pointer < rhs.data->pointer;
254 256 }
255 257  
256 258 // get() is for interface compatibility with std::shared_ptr
257   - T* get() const
  259 + T*
  260 + get() const
258 261 {
259 262 return this->data->pointer;
260 263 }
... ... @@ -264,70 +267,79 @@ class PointerHolder
264 267 #if POINTERHOLDER_TRANSITION >= 2
265 268 [[deprecated("use PointerHolder<T>::get() instead of getPointer()")]]
266 269 #endif // POINTERHOLDER_TRANSITION >= 2
267   - T* getPointer()
  270 + T*
  271 + getPointer()
268 272 {
269 273 return this->data->pointer;
270 274 }
271 275 #if POINTERHOLDER_TRANSITION >= 2
272 276 [[deprecated("use PointerHolder<T>::get() instead of getPointer()")]]
273 277 #endif // POINTERHOLDER_TRANSITION >= 2
274   - T const* getPointer() const
  278 + T const*
  279 + getPointer() const
275 280 {
276 281 return this->data->pointer;
277 282 }
278 283 #if POINTERHOLDER_TRANSITION >= 2
279 284 [[deprecated("use use_count() instead of getRefcount()")]]
280 285 #endif // POINTERHOLDER_TRANSITION >= 2
281   - int getRefcount() const
  286 + int
  287 + getRefcount() const
282 288 {
283 289 return this->data->refcount;
284 290 }
285 291  
286 292 // use_count() is for compatibility with std::shared_ptr
287   - long use_count()
  293 + long
  294 + use_count()
288 295 {
289 296 return static_cast<long>(this->data->refcount);
290 297 }
291 298  
292   - T const& operator*() const
  299 + T const&
  300 + operator*() const
293 301 {
294 302 return *this->data->pointer;
295 303 }
296   - T& operator*()
  304 + T&
  305 + operator*()
297 306 {
298 307 return *this->data->pointer;
299 308 }
300 309  
301   - T const* operator->() const
  310 + T const*
  311 + operator->() const
302 312 {
303 313 return this->data->pointer;
304 314 }
305   - T* operator->()
  315 + T*
  316 + operator->()
306 317 {
307 318 return this->data->pointer;
308 319 }
309 320  
310 321 private:
311   - void init(Data* data)
  322 + void
  323 + init(Data* data)
312 324 {
313 325 this->data = data;
314 326 ++this->data->refcount;
315 327 }
316   - void copy(PointerHolder const& rhs)
  328 + void
  329 + copy(PointerHolder const& rhs)
317 330 {
318 331 this->init(rhs.data);
319 332 }
320   - void destroy()
  333 + void
  334 + destroy()
321 335 {
322 336 bool gone = false;
323 337 {
324   - if (--this->data->refcount == 0)
325   - {
  338 + if (--this->data->refcount == 0) {
326 339 gone = true;
327 340 }
328 341 }
329   - if (gone)
330   - {
  342 + if (gone) {
331 343 delete this->data;
332 344 }
333 345 }
... ... @@ -335,7 +347,7 @@ class PointerHolder
335 347 Data* data;
336 348 };
337 349  
338   -template<typename T, typename... _Args>
  350 +template <typename T, typename... _Args>
339 351 inline PointerHolder<T>
340 352 make_pointer_holder(_Args&&... __args)
341 353 {
... ...
include/qpdf/QIntC.hh
... ... @@ -24,12 +24,12 @@
24 24  
25 25 #include <qpdf/DLL.h>
26 26 #include <qpdf/Types.h>
27   -#include <stdexcept>
  27 +#include <cassert>
28 28 #include <iostream>
29 29 #include <limits>
30   -#include <sstream>
31   -#include <cassert>
32 30 #include <locale>
  31 +#include <sstream>
  32 +#include <stdexcept>
33 33 #include <type_traits>
34 34  
35 35 // This namespace provides safe integer conversion that detects
... ... @@ -51,9 +51,11 @@ namespace QIntC // QIntC = qpdf Integer Conversion
51 51 // throws a range_error otherwise. This class is specialized for
52 52 // each permutation of signed/unsigned for the From and To
53 53 // classes.
54   - template <typename From, typename To,
55   - bool From_signed = std::numeric_limits<From>::is_signed,
56   - bool To_signed = std::numeric_limits<To>::is_signed>
  54 + template <
  55 + typename From,
  56 + typename To,
  57 + bool From_signed = std::numeric_limits<From>::is_signed,
  58 + bool To_signed = std::numeric_limits<To>::is_signed>
57 59 class IntConverter
58 60 {
59 61 };
... ... @@ -62,17 +64,16 @@ namespace QIntC // QIntC = qpdf Integer Conversion
62 64 class IntConverter<From, To, false, false>
63 65 {
64 66 public:
65   - static To convert(From const& i)
  67 + static To
  68 + convert(From const& i)
66 69 {
67 70 // From and To are both unsigned.
68   - if (i > std::numeric_limits<To>::max())
69   - {
  71 + if (i > std::numeric_limits<To>::max()) {
70 72 std::ostringstream msg;
71 73 msg.imbue(std::locale::classic());
72   - msg << "integer out of range converting " << i
73   - << " from a "
74   - << sizeof(From) << "-byte unsigned type to a "
75   - << sizeof(To) << "-byte unsigned type";
  74 + msg << "integer out of range converting " << i << " from a "
  75 + << sizeof(From) << "-byte unsigned type to a " << sizeof(To)
  76 + << "-byte unsigned type";
76 77 throw std::range_error(msg.str());
77 78 }
78 79 return static_cast<To>(i);
... ... @@ -83,18 +84,17 @@ namespace QIntC // QIntC = qpdf Integer Conversion
83 84 class IntConverter<From, To, true, true>
84 85 {
85 86 public:
86   - static To convert(From const& i)
  87 + static To
  88 + convert(From const& i)
87 89 {
88 90 // From and To are both signed.
89 91 if ((i < std::numeric_limits<To>::min()) ||
90   - (i > std::numeric_limits<To>::max()))
91   - {
  92 + (i > std::numeric_limits<To>::max())) {
92 93 std::ostringstream msg;
93 94 msg.imbue(std::locale::classic());
94   - msg << "integer out of range converting " << i
95   - << " from a "
96   - << sizeof(From) << "-byte signed type to a "
97   - << sizeof(To) << "-byte signed type";
  95 + msg << "integer out of range converting " << i << " from a "
  96 + << sizeof(From) << "-byte signed type to a " << sizeof(To)
  97 + << "-byte signed type";
98 98 throw std::range_error(msg.str());
99 99 }
100 100 return static_cast<To>(i);
... ... @@ -105,20 +105,19 @@ namespace QIntC // QIntC = qpdf Integer Conversion
105 105 class IntConverter<From, To, true, false>
106 106 {
107 107 public:
108   - static To convert(From const& i)
  108 + static To
  109 + convert(From const& i)
109 110 {
110 111 // From is signed, and To is unsigned. If i > 0, it's safe to
111 112 // convert it to the corresponding unsigned type and to
112 113 // compare with To's max.
113 114 auto ii = static_cast<typename to_u<From>::type>(i);
114   - if ((i < 0) || (ii > std::numeric_limits<To>::max()))
115   - {
  115 + if ((i < 0) || (ii > std::numeric_limits<To>::max())) {
116 116 std::ostringstream msg;
117 117 msg.imbue(std::locale::classic());
118   - msg << "integer out of range converting " << i
119   - << " from a "
120   - << sizeof(From) << "-byte signed type to a "
121   - << sizeof(To) << "-byte unsigned type";
  118 + msg << "integer out of range converting " << i << " from a "
  119 + << sizeof(From) << "-byte signed type to a " << sizeof(To)
  120 + << "-byte unsigned type";
122 121 throw std::range_error(msg.str());
123 122 }
124 123 return static_cast<To>(i);
... ... @@ -129,20 +128,19 @@ namespace QIntC // QIntC = qpdf Integer Conversion
129 128 class IntConverter<From, To, false, true>
130 129 {
131 130 public:
132   - static To convert(From const& i)
  131 + static To
  132 + convert(From const& i)
133 133 {
134 134 // From is unsigned, and to is signed. Convert To's max to the
135 135 // unsigned version of To and compare i against that.
136 136 auto maxval = static_cast<typename to_u<To>::type>(
137 137 std::numeric_limits<To>::max());
138   - if (i > maxval)
139   - {
  138 + if (i > maxval) {
140 139 std::ostringstream msg;
141 140 msg.imbue(std::locale::classic());
142   - msg << "integer out of range converting " << i
143   - << " from a "
144   - << sizeof(From) << "-byte unsigned type to a "
145   - << sizeof(To) << "-byte signed type";
  141 + msg << "integer out of range converting " << i << " from a "
  142 + << sizeof(From) << "-byte unsigned type to a " << sizeof(To)
  143 + << "-byte signed type";
146 144 throw std::range_error(msg.str());
147 145 }
148 146 return static_cast<To>(i);
... ... @@ -152,97 +150,105 @@ namespace QIntC // QIntC = qpdf Integer Conversion
152 150 // Specific converters. The return type of each function must match
153 151 // the second template parameter to IntConverter.
154 152 template <typename T>
155   - char to_char(T const& i)
  153 + char
  154 + to_char(T const& i)
156 155 {
157 156 return IntConverter<T, char>::convert(i);
158 157 }
159 158  
160 159 template <typename T>
161   - unsigned char to_uchar(T const& i)
  160 + unsigned char
  161 + to_uchar(T const& i)
162 162 {
163 163 return IntConverter<T, unsigned char>::convert(i);
164 164 }
165 165  
166 166 template <typename T>
167   - short to_short(T const& i)
  167 + short
  168 + to_short(T const& i)
168 169 {
169 170 return IntConverter<T, short>::convert(i);
170 171 }
171 172  
172 173 template <typename T>
173   - unsigned short to_ushort(T const& i)
  174 + unsigned short
  175 + to_ushort(T const& i)
174 176 {
175 177 return IntConverter<T, unsigned short>::convert(i);
176 178 }
177 179  
178 180 template <typename T>
179   - int to_int(T const& i)
  181 + int
  182 + to_int(T const& i)
180 183 {
181 184 return IntConverter<T, int>::convert(i);
182 185 }
183 186  
184 187 template <typename T>
185   - unsigned int to_uint(T const& i)
  188 + unsigned int
  189 + to_uint(T const& i)
186 190 {
187 191 return IntConverter<T, unsigned int>::convert(i);
188 192 }
189 193  
190 194 template <typename T>
191   - size_t to_size(T const& i)
  195 + size_t
  196 + to_size(T const& i)
192 197 {
193 198 return IntConverter<T, size_t>::convert(i);
194 199 }
195 200  
196 201 template <typename T>
197   - qpdf_offset_t to_offset(T const& i)
  202 + qpdf_offset_t
  203 + to_offset(T const& i)
198 204 {
199 205 return IntConverter<T, qpdf_offset_t>::convert(i);
200 206 }
201 207  
202 208 template <typename T>
203   - long to_long(T const& i)
  209 + long
  210 + to_long(T const& i)
204 211 {
205   - return IntConverter<T, long >::convert(i);
  212 + return IntConverter<T, long>::convert(i);
206 213 }
207 214  
208 215 template <typename T>
209   - unsigned long to_ulong(T const& i)
  216 + unsigned long
  217 + to_ulong(T const& i)
210 218 {
211   - return IntConverter<T, unsigned long >::convert(i);
  219 + return IntConverter<T, unsigned long>::convert(i);
212 220 }
213 221  
214 222 template <typename T>
215   - long long to_longlong(T const& i)
  223 + long long
  224 + to_longlong(T const& i)
216 225 {
217 226 return IntConverter<T, long long>::convert(i);
218 227 }
219 228  
220 229 template <typename T>
221   - unsigned long long to_ulonglong(T const& i)
  230 + unsigned long long
  231 + to_ulonglong(T const& i)
222 232 {
223 233 return IntConverter<T, unsigned long long>::convert(i);
224 234 }
225 235  
226 236 template <typename T>
227   - void range_check(T const& cur, T const& delta)
  237 + void
  238 + range_check(T const& cur, T const& delta)
228 239 {
229   - if ((delta > 0) != (cur > 0))
230   - {
  240 + if ((delta > 0) != (cur > 0)) {
231 241 return;
232 242 }
233 243  
234   - if ((delta > 0) &&
235   - ((std::numeric_limits<T>::max() - cur) < delta))
236   - {
  244 + if ((delta > 0) && ((std::numeric_limits<T>::max() - cur) < delta)) {
237 245 std::ostringstream msg;
238 246 msg.imbue(std::locale::classic());
239 247 msg << "adding " << delta << " to " << cur
240 248 << " would cause an integer overflow";
241 249 throw std::range_error(msg.str());
242   - }
243   - else if ((delta < 0) &&
244   - ((std::numeric_limits<T>::min() - cur) > delta))
245   - {
  250 + } else if (
  251 + (delta < 0) && ((std::numeric_limits<T>::min() - cur) > delta)) {
246 252 std::ostringstream msg;
247 253 msg.imbue(std::locale::classic());
248 254 msg << "adding " << delta << " to " << cur
... ... @@ -252,25 +258,21 @@ namespace QIntC // QIntC = qpdf Integer Conversion
252 258 }
253 259  
254 260 template <typename T>
255   - void range_check_substract(T const& cur, T const& delta)
  261 + void
  262 + range_check_substract(T const& cur, T const& delta)
256 263 {
257   - if ((delta >= 0) == (cur >= 0))
258   - {
  264 + if ((delta >= 0) == (cur >= 0)) {
259 265 return;
260 266 }
261 267  
262   - if ((delta > 0) &&
263   - ((std::numeric_limits<T>::min() + delta) > cur))
264   - {
  268 + if ((delta > 0) && ((std::numeric_limits<T>::min() + delta) > cur)) {
265 269 std::ostringstream msg;
266 270 msg.imbue(std::locale::classic());
267 271 msg << "subtracting " << delta << " from " << cur
268 272 << " would cause an integer underflow";
269 273 throw std::range_error(msg.str());
270   - }
271   - else if ((delta < 0) &&
272   - ((std::numeric_limits<T>::max() + delta) < cur))
273   - {
  274 + } else if (
  275 + (delta < 0) && ((std::numeric_limits<T>::max() + delta) < cur)) {
274 276 std::ostringstream msg;
275 277 msg.imbue(std::locale::classic());
276 278 msg << "subtracting " << delta << " from " << cur
... ... @@ -278,6 +280,6 @@ namespace QIntC // QIntC = qpdf Integer Conversion
278 280 throw std::range_error(msg.str());
279 281 }
280 282 }
281   -};
  283 +}; // namespace QIntC
282 284  
283 285 #endif // QINTC_HH
... ...
include/qpdf/QPDF.hh
... ... @@ -25,26 +25,25 @@
25 25 #include <qpdf/DLL.h>
26 26 #include <qpdf/Types.h>
27 27  
  28 +#include <functional>
  29 +#include <iostream>
  30 +#include <list>
  31 +#include <map>
  32 +#include <memory>
28 33 #include <stdio.h>
29 34 #include <string>
30   -#include <map>
31   -#include <list>
32   -#include <iostream>
33 35 #include <vector>
34   -#include <functional>
35   -#include <memory>
36 36  
  37 +#include <qpdf/Buffer.hh>
  38 +#include <qpdf/InputSource.hh>
  39 +#include <qpdf/PDFVersion.hh>
37 40 #include <qpdf/QIntC.hh>
38 41 #include <qpdf/QPDFExc.hh>
39   -#include <qpdf/QPDFObjectHandle.hh>
40 42 #include <qpdf/QPDFObjGen.hh>
41   -#include <qpdf/QPDFXRefEntry.hh>
42 43 #include <qpdf/QPDFObjectHandle.hh>
43   -#include <qpdf/QPDFTokenizer.hh>
44 44 #include <qpdf/QPDFStreamFilter.hh>
45   -#include <qpdf/Buffer.hh>
46   -#include <qpdf/InputSource.hh>
47   -#include <qpdf/PDFVersion.hh>
  45 +#include <qpdf/QPDFTokenizer.hh>
  46 +#include <qpdf/QPDFXRefEntry.hh>
48 47  
49 48 class QPDF_Stream;
50 49 class BitStream;
... ... @@ -87,24 +86,29 @@ class QPDF
87 86 // file will be closed at the end. Otherwise, the caller is
88 87 // responsible for closing the file.
89 88 QPDF_DLL
90   - void processFile(char const* description, FILE* file,
91   - bool close_file, char const* password = 0);
  89 + void processFile(
  90 + char const* description,
  91 + FILE* file,
  92 + bool close_file,
  93 + char const* password = 0);
92 94  
93 95 // Parse a PDF file loaded into a memory buffer. This works
94 96 // exactly like processFile except that the PDF file is in memory
95 97 // instead of on disk. The description appears in any warning or
96 98 // error message in place of the file name.
97 99 QPDF_DLL
98   - void processMemoryFile(char const* description,
99   - char const* buf, size_t length,
100   - char const* password = 0);
  100 + void processMemoryFile(
  101 + char const* description,
  102 + char const* buf,
  103 + size_t length,
  104 + char const* password = 0);
101 105  
102 106 // Parse a PDF file loaded from a custom InputSource. If you have
103 107 // your own method of retrieving a PDF file, you can subclass
104 108 // InputSource and use this method.
105 109 QPDF_DLL
106   - void processInputSource(PointerHolder<InputSource>,
107   - char const* password = 0);
  110 + void
  111 + processInputSource(PointerHolder<InputSource>, char const* password = 0);
108 112  
109 113 // Close or otherwise release the input source. Once this has been
110 114 // called, no other methods of qpdf can be called safely except
... ... @@ -149,7 +153,7 @@ class QPDF
149 153 QPDF_DLL
150 154 static void registerStreamFilter(
151 155 std::string const& filter_name,
152   - std::function<std::shared_ptr<QPDFStreamFilter> ()> factory);
  156 + std::function<std::shared_ptr<QPDFStreamFilter>()> factory);
153 157  
154 158 // Parameter settings
155 159  
... ... @@ -312,8 +316,7 @@ class QPDF
312 316 QPDF_DLL
313 317 void swapObjects(QPDFObjGen const& og1, QPDFObjGen const& og2);
314 318 QPDF_DLL
315   - void swapObjects(int objid1, int generation1,
316   - int objid2, int generation2);
  319 + void swapObjects(int objid1, int generation1, int objid2, int generation2);
317 320  
318 321 // Replace a reserved object. This is a wrapper around
319 322 // replaceObject but it guarantees that the underlying object is a
... ... @@ -321,8 +324,7 @@ class QPDF
321 324 // to replacement.
322 325 QPDF_DLL
323 326 void
324   - replaceReserved(QPDFObjectHandle reserved,
325   - QPDFObjectHandle replacement);
  327 + replaceReserved(QPDFObjectHandle reserved, QPDFObjectHandle replacement);
326 328  
327 329 // Copy an object from another QPDF to this one. Starting with
328 330 // qpdf version 8.3.0, it is no longer necessary to keep the
... ... @@ -377,13 +379,19 @@ class QPDF
377 379 class EncryptionData
378 380 {
379 381 public:
380   -
381 382 // This class holds data read from the encryption dictionary.
382   - EncryptionData(int V, int R, int Length_bytes, int P,
383   - std::string const& O, std::string const& U,
384   - std::string const& OE, std::string const& UE,
385   - std::string const& Perms,
386   - std::string const& id1, bool encrypt_metadata) :
  383 + EncryptionData(
  384 + int V,
  385 + int R,
  386 + int Length_bytes,
  387 + int P,
  388 + std::string const& O,
  389 + std::string const& U,
  390 + std::string const& OE,
  391 + std::string const& UE,
  392 + std::string const& Perms,
  393 + std::string const& id1,
  394 + bool encrypt_metadata) :
387 395 V(V),
388 396 R(R),
389 397 Length_bytes(Length_bytes),
... ... @@ -412,11 +420,12 @@ class QPDF
412 420  
413 421 void setO(std::string const&);
414 422 void setU(std::string const&);
415   - void setV5EncryptionParameters(std::string const& O,
416   - std::string const& OE,
417   - std::string const& U,
418   - std::string const& UE,
419   - std::string const& Perms);
  423 + void setV5EncryptionParameters(
  424 + std::string const& O,
  425 + std::string const& OE,
  426 + std::string const& U,
  427 + std::string const& UE,
  428 + std::string const& Perms);
420 429  
421 430 private:
422 431 EncryptionData(EncryptionData const&) = delete;
... ... @@ -442,10 +451,13 @@ class QPDF
442 451 bool isEncrypted(int& R, int& P);
443 452  
444 453 QPDF_DLL
445   - bool isEncrypted(int& R, int& P, int& V,
446   - encryption_method_e& stream_method,
447   - encryption_method_e& string_method,
448   - encryption_method_e& file_method);
  454 + bool isEncrypted(
  455 + int& R,
  456 + int& P,
  457 + int& V,
  458 + encryption_method_e& stream_method,
  459 + encryption_method_e& string_method,
  460 + encryption_method_e& file_method);
449 461  
450 462 QPDF_DLL
451 463 bool ownerPasswordMatched() const;
... ... @@ -480,26 +492,44 @@ class QPDF
480 492 static void trim_user_password(std::string& user_password);
481 493 QPDF_DLL
482 494 static std::string compute_data_key(
483   - std::string const& encryption_key, int objid, int generation,
484   - bool use_aes, int encryption_V, int encryption_R);
  495 + std::string const& encryption_key,
  496 + int objid,
  497 + int generation,
  498 + bool use_aes,
  499 + int encryption_V,
  500 + int encryption_R);
485 501 QPDF_DLL
486 502 static std::string compute_encryption_key(
487 503 std::string const& password, EncryptionData const& data);
488 504  
489 505 QPDF_DLL
490 506 static void compute_encryption_O_U(
491   - char const* user_password, char const* owner_password,
492   - int V, int R, int key_len, int P, bool encrypt_metadata,
  507 + char const* user_password,
  508 + char const* owner_password,
  509 + int V,
  510 + int R,
  511 + int key_len,
  512 + int P,
  513 + bool encrypt_metadata,
493 514 std::string const& id1,
494   - std::string& O, std::string& U);
  515 + std::string& O,
  516 + std::string& U);
495 517 QPDF_DLL
496 518 static void compute_encryption_parameters_V5(
497   - char const* user_password, char const* owner_password,
498   - int V, int R, int key_len, int P, bool encrypt_metadata,
  519 + char const* user_password,
  520 + char const* owner_password,
  521 + int V,
  522 + int R,
  523 + int key_len,
  524 + int P,
  525 + bool encrypt_metadata,
499 526 std::string const& id1,
500 527 std::string& encryption_key,
501   - std::string& O, std::string& U,
502   - std::string& OE, std::string& UE, std::string& Perms);
  528 + std::string& O,
  529 + std::string& U,
  530 + std::string& OE,
  531 + std::string& UE,
  532 + std::string& Perms);
503 533 // Return the full user password as stored in the PDF file. For
504 534 // files encrypted with 40-bit or 128-bit keys, the user password
505 535 // can be recovered when the file is opened using the owner
... ... @@ -587,8 +617,9 @@ class QPDF
587 617 // keys. This is used by QPDFWriter to avoid creation of dangling
588 618 // objects for stream dictionary keys it will be regenerating.
589 619 QPDF_DLL
590   - void optimize(std::map<int, int> const& object_stream_data,
591   - bool allow_changes = true);
  620 + void optimize(
  621 + std::map<int, int> const& object_stream_data,
  622 + bool allow_changes = true);
592 623 // ABI: make function optional and merge overloaded versions
593 624 QPDF_DLL
594 625 void optimize(
... ... @@ -650,8 +681,8 @@ class QPDF
650 681 QPDF_DLL
651 682 void addPage(QPDFObjectHandle newpage, bool first);
652 683 QPDF_DLL
653   - void addPageAt(QPDFObjectHandle newpage, bool before,
654   - QPDFObjectHandle refpage);
  684 + void
  685 + addPageAt(QPDFObjectHandle newpage, bool before, QPDFObjectHandle refpage);
655 686 QPDF_DLL
656 687 void removePage(QPDFObjectHandle page);
657 688 // End legacy page helpers
... ... @@ -661,9 +692,10 @@ class QPDF
661 692 class Writer
662 693 {
663 694 friend class QPDFWriter;
664   - private:
665 695  
666   - static void getLinearizedParts(
  696 + private:
  697 + static void
  698 + getLinearizedParts(
667 699 QPDF& qpdf,
668 700 std::map<int, int> const& object_stream_data,
669 701 std::vector<QPDFObjectHandle>& part4,
... ... @@ -672,28 +704,32 @@ class QPDF
672 704 std::vector<QPDFObjectHandle>& part8,
673 705 std::vector<QPDFObjectHandle>& part9)
674 706 {
675   - qpdf.getLinearizedParts(object_stream_data,
676   - part4, part6, part7, part8, part9);
  707 + qpdf.getLinearizedParts(
  708 + object_stream_data, part4, part6, part7, part8, part9);
677 709 }
678 710  
679   - static void generateHintStream(
  711 + static void
  712 + generateHintStream(
680 713 QPDF& qpdf,
681 714 std::map<int, QPDFXRefEntry> const& xref,
682 715 std::map<int, qpdf_offset_t> const& lengths,
683 716 std::map<int, int> const& obj_renumber,
684 717 PointerHolder<Buffer>& hint_stream,
685   - int& S, int& O)
  718 + int& S,
  719 + int& O)
686 720 {
687   - return qpdf.generateHintStream(xref, lengths, obj_renumber,
688   - hint_stream, S, O);
  721 + return qpdf.generateHintStream(
  722 + xref, lengths, obj_renumber, hint_stream, S, O);
689 723 }
690 724  
691   - static void getObjectStreamData(QPDF& qpdf, std::map<int, int>& omap)
  725 + static void
  726 + getObjectStreamData(QPDF& qpdf, std::map<int, int>& omap)
692 727 {
693 728 qpdf.getObjectStreamData(omap);
694 729 }
695 730  
696   - static std::vector<QPDFObjGen> getCompressibleObjGens(QPDF& qpdf)
  731 + static std::vector<QPDFObjGen>
  732 + getCompressibleObjGens(QPDF& qpdf)
697 733 {
698 734 return qpdf.getCompressibleObjGens();
699 735 }
... ... @@ -704,13 +740,15 @@ class QPDF
704 740 class Resolver
705 741 {
706 742 friend class QPDFObjectHandle;
  743 +
707 744 private:
708   - static PointerHolder<QPDFObject> resolve(
709   - QPDF* qpdf, int objid, int generation)
  745 + static PointerHolder<QPDFObject>
  746 + resolve(QPDF* qpdf, int objid, int generation)
710 747 {
711 748 return qpdf->resolve(objid, generation);
712 749 }
713   - static bool objectChanged(
  750 + static bool
  751 + objectChanged(
714 752 QPDF* qpdf, QPDFObjGen const& og, PointerHolder<QPDFObject>& oph)
715 753 {
716 754 return qpdf->objectChanged(og, oph);
... ... @@ -723,10 +761,13 @@ class QPDF
723 761 class StreamCopier
724 762 {
725 763 friend class QPDFObjectHandle;
  764 +
726 765 private:
727   - static void copyStreamData(QPDF* qpdf,
728   - QPDFObjectHandle const& dest,
729   - QPDFObjectHandle const& src)
  766 + static void
  767 + copyStreamData(
  768 + QPDF* qpdf,
  769 + QPDFObjectHandle const& dest,
  770 + QPDFObjectHandle const& src)
730 771 {
731 772 qpdf->copyStreamData(dest, src);
732 773 }
... ... @@ -738,19 +779,18 @@ class QPDF
738 779 class ParseGuard
739 780 {
740 781 friend class QPDFObjectHandle;
  782 +
741 783 private:
742 784 ParseGuard(QPDF* qpdf) :
743 785 qpdf(qpdf)
744 786 {
745   - if (qpdf)
746   - {
  787 + if (qpdf) {
747 788 qpdf->inParse(true);
748 789 }
749 790 }
750 791 ~ParseGuard()
751 792 {
752   - if (qpdf)
753   - {
  793 + if (qpdf) {
754 794 qpdf->inParse(false);
755 795 }
756 796 }
... ... @@ -762,17 +802,29 @@ class QPDF
762 802 class Pipe
763 803 {
764 804 friend class QPDF_Stream;
  805 +
765 806 private:
766   - static bool pipeStreamData(QPDF* qpdf, int objid, int generation,
767   - qpdf_offset_t offset, size_t length,
768   - QPDFObjectHandle dict,
769   - Pipeline* pipeline,
770   - bool suppress_warnings,
771   - bool will_retry)
  807 + static bool
  808 + pipeStreamData(
  809 + QPDF* qpdf,
  810 + int objid,
  811 + int generation,
  812 + qpdf_offset_t offset,
  813 + size_t length,
  814 + QPDFObjectHandle dict,
  815 + Pipeline* pipeline,
  816 + bool suppress_warnings,
  817 + bool will_retry)
772 818 {
773 819 return qpdf->pipeStreamData(
774   - objid, generation, offset, length, dict, pipeline,
775   - suppress_warnings, will_retry);
  820 + objid,
  821 + generation,
  822 + offset,
  823 + length,
  824 + dict,
  825 + pipeline,
  826 + suppress_warnings,
  827 + will_retry);
776 828 }
777 829 };
778 830 friend class Pipe;
... ... @@ -788,9 +840,10 @@ class QPDF
788 840 end_after_space(0)
789 841 {
790 842 }
791   - ObjCache(PointerHolder<QPDFObject> object,
792   - qpdf_offset_t end_before_space,
793   - qpdf_offset_t end_after_space) :
  843 + ObjCache(
  844 + PointerHolder<QPDFObject> object,
  845 + qpdf_offset_t end_before_space,
  846 + qpdf_offset_t end_after_space) :
794 847 object(object),
795 848 end_before_space(end_before_space),
796 849 end_after_space(end_after_space)
... ... @@ -813,6 +866,7 @@ class QPDF
813 866 class EncryptionParameters
814 867 {
815 868 friend class QPDF;
  869 +
816 870 public:
817 871 EncryptionParameters();
818 872  
... ... @@ -839,6 +893,7 @@ class QPDF
839 893 class ForeignStreamData
840 894 {
841 895 friend class QPDF;
  896 +
842 897 public:
843 898 ForeignStreamData(
844 899 PointerHolder<EncryptionParameters> encp,
... ... @@ -867,18 +922,21 @@ class QPDF
867 922 {
868 923 }
869 924 virtual bool provideStreamData(
870   - int objid, int generation, Pipeline* pipeline,
871   - bool suppress_warnings, bool will_retry) override;
872   - void registerForeignStream(QPDFObjGen const& local_og,
873   - QPDFObjectHandle foreign_stream);
874   - void registerForeignStream(QPDFObjGen const& local_og,
875   - PointerHolder<ForeignStreamData>);
  925 + int objid,
  926 + int generation,
  927 + Pipeline* pipeline,
  928 + bool suppress_warnings,
  929 + bool will_retry) override;
  930 + void registerForeignStream(
  931 + QPDFObjGen const& local_og, QPDFObjectHandle foreign_stream);
  932 + void registerForeignStream(
  933 + QPDFObjGen const& local_og, PointerHolder<ForeignStreamData>);
876 934  
877 935 private:
878 936 QPDF& destination_qpdf;
879 937 std::map<QPDFObjGen, QPDFObjectHandle> foreign_streams;
880   - std::map<QPDFObjGen,
881   - PointerHolder<ForeignStreamData> > foreign_stream_data;
  938 + std::map<QPDFObjGen, PointerHolder<ForeignStreamData>>
  939 + foreign_stream_data;
882 940 };
883 941  
884 942 class StringDecrypter: public QPDFObjectHandle::StringDecrypter
... ... @@ -911,6 +969,7 @@ class QPDF
911 969 {
912 970 this->qpdf->m->resolving.erase(og);
913 971 }
  972 +
914 973 private:
915 974 QPDF* qpdf;
916 975 QPDFObjGen og;
... ... @@ -922,56 +981,72 @@ class QPDF
922 981 void setTrailer(QPDFObjectHandle obj);
923 982 void read_xref(qpdf_offset_t offset);
924 983 void reconstruct_xref(QPDFExc& e);
925   - bool parse_xrefFirst(std::string const& line,
926   - int& obj, int& num, int& bytes);
927   - bool parse_xrefEntry(std::string const& line,
928   - qpdf_offset_t& f1, int& f2, char& type);
  984 + bool
  985 + parse_xrefFirst(std::string const& line, int& obj, int& num, int& bytes);
  986 + bool parse_xrefEntry(
  987 + std::string const& line, qpdf_offset_t& f1, int& f2, char& type);
929 988 qpdf_offset_t read_xrefTable(qpdf_offset_t offset);
930 989 qpdf_offset_t read_xrefStream(qpdf_offset_t offset);
931   - qpdf_offset_t processXRefStream(
932   - qpdf_offset_t offset, QPDFObjectHandle& xref_stream);
933   - void insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2,
934   - bool overwrite = false);
935   - void setLastObjectDescription(std::string const& description,
936   - int objid, int generation);
  990 + qpdf_offset_t
  991 + processXRefStream(qpdf_offset_t offset, QPDFObjectHandle& xref_stream);
  992 + void insertXrefEntry(
  993 + int obj, int f0, qpdf_offset_t f1, int f2, bool overwrite = false);
  994 + void setLastObjectDescription(
  995 + std::string const& description, int objid, int generation);
937 996 QPDFObjectHandle readObject(
938   - PointerHolder<InputSource>, std::string const& description,
939   - int objid, int generation, bool in_object_stream);
  997 + PointerHolder<InputSource>,
  998 + std::string const& description,
  999 + int objid,
  1000 + int generation,
  1001 + bool in_object_stream);
940 1002 size_t recoverStreamLength(
941   - PointerHolder<InputSource> input, int objid, int generation,
  1003 + PointerHolder<InputSource> input,
  1004 + int objid,
  1005 + int generation,
942 1006 qpdf_offset_t stream_offset);
943   - QPDFTokenizer::Token readToken(PointerHolder<InputSource>,
944   - size_t max_len = 0);
  1007 + QPDFTokenizer::Token
  1008 + readToken(PointerHolder<InputSource>, size_t max_len = 0);
945 1009  
946 1010 QPDFObjectHandle readObjectAtOffset(
947 1011 bool attempt_recovery,
948   - qpdf_offset_t offset, std::string const& description,
949   - int exp_objid, int exp_generation,
950   - int& act_objid, int& act_generation);
  1012 + qpdf_offset_t offset,
  1013 + std::string const& description,
  1014 + int exp_objid,
  1015 + int exp_generation,
  1016 + int& act_objid,
  1017 + int& act_generation);
951 1018 bool objectChanged(QPDFObjGen const& og, PointerHolder<QPDFObject>& oph);
952 1019 PointerHolder<QPDFObject> resolve(int objid, int generation);
953 1020 void resolveObjectsInStream(int obj_stream_number);
954 1021 void stopOnError(std::string const& message);
955 1022  
956 1023 // Calls finish() on the pipeline when done but does not delete it
957   - bool pipeStreamData(int objid, int generation,
958   - qpdf_offset_t offset, size_t length,
959   - QPDFObjectHandle dict,
960   - Pipeline* pipeline,
961   - bool suppress_warnings,
962   - bool will_retry);
  1024 + bool pipeStreamData(
  1025 + int objid,
  1026 + int generation,
  1027 + qpdf_offset_t offset,
  1028 + size_t length,
  1029 + QPDFObjectHandle dict,
  1030 + Pipeline* pipeline,
  1031 + bool suppress_warnings,
  1032 + bool will_retry);
963 1033 bool pipeForeignStreamData(
964 1034 PointerHolder<ForeignStreamData>,
965   - Pipeline*, bool suppress_warnings, bool will_retry);
966   - static bool pipeStreamData(PointerHolder<QPDF::EncryptionParameters> encp,
967   - PointerHolder<InputSource> file,
968   - QPDF& qpdf_for_warning,
969   - int objid, int generation,
970   - qpdf_offset_t offset, size_t length,
971   - QPDFObjectHandle dict,
972   - Pipeline* pipeline,
973   - bool suppress_warnings,
974   - bool will_retry);
  1035 + Pipeline*,
  1036 + bool suppress_warnings,
  1037 + bool will_retry);
  1038 + static bool pipeStreamData(
  1039 + PointerHolder<QPDF::EncryptionParameters> encp,
  1040 + PointerHolder<InputSource> file,
  1041 + QPDF& qpdf_for_warning,
  1042 + int objid,
  1043 + int generation,
  1044 + qpdf_offset_t offset,
  1045 + size_t length,
  1046 + QPDFObjectHandle dict,
  1047 + Pipeline* pipeline,
  1048 + bool suppress_warnings,
  1049 + bool will_retry);
975 1050  
976 1051 // For QPDFWriter:
977 1052  
... ... @@ -985,11 +1060,13 @@ class QPDF
985 1060 std::vector<QPDFObjectHandle>& part8,
986 1061 std::vector<QPDFObjectHandle>& part9);
987 1062  
988   - void generateHintStream(std::map<int, QPDFXRefEntry> const& xref,
989   - std::map<int, qpdf_offset_t> const& lengths,
990   - std::map<int, int> const& obj_renumber,
991   - PointerHolder<Buffer>& hint_stream,
992   - int& S, int& O);
  1063 + void generateHintStream(
  1064 + std::map<int, QPDFXRefEntry> const& xref,
  1065 + std::map<int, qpdf_offset_t> const& lengths,
  1066 + std::map<int, int> const& obj_renumber,
  1067 + PointerHolder<Buffer>& hint_stream,
  1068 + int& S,
  1069 + int& O);
993 1070  
994 1071 // Map object to object stream that contains it
995 1072 void getObjectStreamData(std::map<int, int>&);
... ... @@ -1000,45 +1077,51 @@ class QPDF
1000 1077  
1001 1078 // methods to support page handling
1002 1079  
1003   - void getAllPagesInternal(QPDFObjectHandle cur_pages,
1004   - std::vector<QPDFObjectHandle>& result,
1005   - std::set<QPDFObjGen>& visited,
1006   - std::set<QPDFObjGen>& seen);
  1080 + void getAllPagesInternal(
  1081 + QPDFObjectHandle cur_pages,
  1082 + std::vector<QPDFObjectHandle>& result,
  1083 + std::set<QPDFObjGen>& visited,
  1084 + std::set<QPDFObjGen>& seen);
1007 1085 void insertPage(QPDFObjectHandle newpage, int pos);
1008 1086 void flattenPagesTree();
1009   - void insertPageobjToPage(QPDFObjectHandle const& obj, int pos,
1010   - bool check_duplicate);
  1087 + void insertPageobjToPage(
  1088 + QPDFObjectHandle const& obj, int pos, bool check_duplicate);
1011 1089  
1012 1090 // methods to support encryption -- implemented in QPDF_encryption.cc
1013   - static encryption_method_e interpretCF(
1014   - PointerHolder<EncryptionParameters> encp, QPDFObjectHandle);
  1091 + static encryption_method_e
  1092 + interpretCF(PointerHolder<EncryptionParameters> encp, QPDFObjectHandle);
1015 1093 void initializeEncryption();
1016 1094 static std::string getKeyForObject(
1017 1095 PointerHolder<EncryptionParameters> encp,
1018   - int objid, int generation, bool use_aes);
  1096 + int objid,
  1097 + int generation,
  1098 + bool use_aes);
1019 1099 void decryptString(std::string&, int objid, int generation);
1020 1100 static std::string compute_encryption_key_from_password(
1021 1101 std::string const& password, EncryptionData const& data);
1022 1102 static std::string recover_encryption_key_with_password(
1023 1103 std::string const& password, EncryptionData const& data);
1024 1104 static std::string recover_encryption_key_with_password(
1025   - std::string const& password, EncryptionData const& data,
  1105 + std::string const& password,
  1106 + EncryptionData const& data,
1026 1107 bool& perms_valid);
1027 1108 static void decryptStream(
1028 1109 PointerHolder<EncryptionParameters> encp,
1029 1110 PointerHolder<InputSource> file,
1030   - QPDF& qpdf_for_warning, Pipeline*& pipeline,
1031   - int objid, int generation,
  1111 + QPDF& qpdf_for_warning,
  1112 + Pipeline*& pipeline,
  1113 + int objid,
  1114 + int generation,
1032 1115 QPDFObjectHandle& stream_dict,
1033 1116 std::vector<std::shared_ptr<Pipeline>>& heap);
1034 1117  
1035 1118 // Methods to support object copying
1036   - void reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier,
1037   - bool top);
  1119 + void
  1120 + reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top);
1038 1121 QPDFObjectHandle replaceForeignIndirectObjects(
1039 1122 QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top);
1040   - void copyStreamData(
1041   - QPDFObjectHandle dest_stream, QPDFObjectHandle src_stream);
  1123 + void
  1124 + copyStreamData(QPDFObjectHandle dest_stream, QPDFObjectHandle src_stream);
1042 1125  
1043 1126 // Linearization Hint table structures.
1044 1127 // Naming conventions:
... ... @@ -1065,14 +1148,14 @@ class QPDF
1065 1148 {
1066 1149 }
1067 1150  
1068   - int delta_nobjects; // 1
1069   - qpdf_offset_t delta_page_length; // 2
1070   - int nshared_objects; // 3
  1151 + int delta_nobjects; // 1
  1152 + qpdf_offset_t delta_page_length; // 2
  1153 + int nshared_objects; // 3
1071 1154 // vectors' sizes = nshared_objects
1072   - std::vector<int> shared_identifiers; // 4
1073   - std::vector<int> shared_numerators; // 5
1074   - qpdf_offset_t delta_content_offset; // 6
1075   - qpdf_offset_t delta_content_length; // 7
  1155 + std::vector<int> shared_identifiers; // 4
  1156 + std::vector<int> shared_numerators; // 5
  1157 + qpdf_offset_t delta_content_offset; // 6
  1158 + qpdf_offset_t delta_content_length; // 7
1076 1159 };
1077 1160  
1078 1161 // PDF 1.4: Table F.3
... ... @@ -1095,19 +1178,19 @@ class QPDF
1095 1178 {
1096 1179 }
1097 1180  
1098   - int min_nobjects; // 1
1099   - qpdf_offset_t first_page_offset; // 2
1100   - int nbits_delta_nobjects; // 3
1101   - int min_page_length; // 4
1102   - int nbits_delta_page_length; // 5
1103   - int min_content_offset; // 6
1104   - int nbits_delta_content_offset; // 7
1105   - int min_content_length; // 8
1106   - int nbits_delta_content_length; // 9
1107   - int nbits_nshared_objects; // 10
1108   - int nbits_shared_identifier; // 11
1109   - int nbits_shared_numerator; // 12
1110   - int shared_denominator; // 13
  1181 + int min_nobjects; // 1
  1182 + qpdf_offset_t first_page_offset; // 2
  1183 + int nbits_delta_nobjects; // 3
  1184 + int min_page_length; // 4
  1185 + int nbits_delta_page_length; // 5
  1186 + int min_content_offset; // 6
  1187 + int nbits_delta_content_offset; // 7
  1188 + int min_content_length; // 8
  1189 + int nbits_delta_content_length; // 9
  1190 + int nbits_nshared_objects; // 10
  1191 + int nbits_shared_identifier; // 11
  1192 + int nbits_shared_numerator; // 12
  1193 + int shared_denominator; // 13
1111 1194 // vector size is npages
1112 1195 std::vector<HPageOffsetEntry> entries;
1113 1196 };
... ... @@ -1123,9 +1206,9 @@ class QPDF
1123 1206 }
1124 1207  
1125 1208 // Item 3 is a 128-bit signature (unsupported by Acrobat)
1126   - int delta_group_length; // 1
1127   - int signature_present; // 2 -- always 0
1128   - int nobjects_minus_one; // 4 -- always 0
  1209 + int delta_group_length; // 1
  1210 + int signature_present; // 2 -- always 0
  1211 + int nobjects_minus_one; // 4 -- always 0
1129 1212 };
1130 1213  
1131 1214 // PDF 1.4: Table F.5
... ... @@ -1142,13 +1225,13 @@ class QPDF
1142 1225 {
1143 1226 }
1144 1227  
1145   - int first_shared_obj; // 1
1146   - qpdf_offset_t first_shared_offset; // 2
1147   - int nshared_first_page; // 3
1148   - int nshared_total; // 4
1149   - int nbits_nobjects; // 5
1150   - int min_group_length; // 6
1151   - int nbits_delta_group_length; // 7
  1228 + int first_shared_obj; // 1
  1229 + qpdf_offset_t first_shared_offset; // 2
  1230 + int nshared_first_page; // 3
  1231 + int nshared_total; // 4
  1232 + int nbits_nobjects; // 5
  1233 + int min_group_length; // 6
  1234 + int nbits_delta_group_length; // 7
1152 1235 // vector size is nshared_total
1153 1236 std::vector<HSharedObjectEntry> entries;
1154 1237 };
... ... @@ -1164,10 +1247,10 @@ class QPDF
1164 1247 {
1165 1248 }
1166 1249  
1167   - int first_object; // 1
1168   - qpdf_offset_t first_object_offset; // 2
1169   - int nobjects; // 3
1170   - int group_length; // 4
  1250 + int first_object; // 1
  1251 + qpdf_offset_t first_object_offset; // 2
  1252 + int nobjects; // 3
  1253 + int group_length; // 4
1171 1254 };
1172 1255  
1173 1256 // Other linearization data structures
... ... @@ -1261,15 +1344,13 @@ class QPDF
1261 1344  
1262 1345 // No need for CHGeneric -- HGeneric is fine as is.
1263 1346  
1264   -
1265 1347 // Data structures to support optimization -- implemented in
1266 1348 // QPDF_optimization.cc
1267 1349  
1268 1350 class ObjUser
1269 1351 {
1270 1352 public:
1271   - enum user_e
1272   - {
  1353 + enum user_e {
1273 1354 ou_bad,
1274 1355 ou_page,
1275 1356 ou_thumb,
... ... @@ -1293,8 +1374,8 @@ class QPDF
1293 1374 bool operator<(ObjUser const&) const;
1294 1375  
1295 1376 user_e ou_type;
1296   - int pageno; // if ou_page;
1297   - std::string key; // if ou_trailer_key or ou_root_key
  1377 + int pageno; // if ou_page;
  1378 + std::string key; // if ou_trailer_key or ou_root_key
1298 1379 };
1299 1380  
1300 1381 class PatternFinder: public InputSource::Finder
... ... @@ -1308,7 +1389,8 @@ class QPDF
1308 1389 virtual ~PatternFinder()
1309 1390 {
1310 1391 }
1311   - virtual bool check()
  1392 + virtual bool
  1393 + check()
1312 1394 {
1313 1395 return (this->qpdf.*checker)();
1314 1396 }
... ... @@ -1328,8 +1410,8 @@ class QPDF
1328 1410 void readLinearizationData();
1329 1411 bool checkLinearizationInternal();
1330 1412 void dumpLinearizationDataInternal();
1331   - QPDFObjectHandle readHintStream(
1332   - Pipeline&, qpdf_offset_t offset, size_t length);
  1413 + QPDFObjectHandle
  1414 + readHintStream(Pipeline&, qpdf_offset_t offset, size_t length);
1333 1415 void readHPageOffset(BitStream);
1334 1416 void readHSharedObject(BitStream);
1335 1417 void readHGeneric(BitStream, HGeneric&);
... ... @@ -1337,30 +1419,32 @@ class QPDF
1337 1419 qpdf_offset_t getLinearizationOffset(QPDFObjGen const&);
1338 1420 QPDFObjectHandle getUncompressedObject(
1339 1421 QPDFObjectHandle&, std::map<int, int> const& object_stream_data);
1340   - int lengthNextN(int first_object, int n,
1341   - std::list<std::string>& errors);
1342   - void checkHPageOffset(std::list<std::string>& errors,
1343   - std::list<std::string>& warnings,
1344   - std::vector<QPDFObjectHandle> const& pages,
1345   - std::map<int, int>& idx_to_obj);
1346   - void checkHSharedObject(std::list<std::string>& warnings,
1347   - std::list<std::string>& errors,
1348   - std::vector<QPDFObjectHandle> const& pages,
1349   - std::map<int, int>& idx_to_obj);
  1422 + int lengthNextN(int first_object, int n, std::list<std::string>& errors);
  1423 + void checkHPageOffset(
  1424 + std::list<std::string>& errors,
  1425 + std::list<std::string>& warnings,
  1426 + std::vector<QPDFObjectHandle> const& pages,
  1427 + std::map<int, int>& idx_to_obj);
  1428 + void checkHSharedObject(
  1429 + std::list<std::string>& warnings,
  1430 + std::list<std::string>& errors,
  1431 + std::vector<QPDFObjectHandle> const& pages,
  1432 + std::map<int, int>& idx_to_obj);
1350 1433 void checkHOutlines(std::list<std::string>& warnings);
1351 1434 void dumpHPageOffset();
1352 1435 void dumpHSharedObject();
1353 1436 void dumpHGeneric(HGeneric&);
1354 1437 qpdf_offset_t adjusted_offset(qpdf_offset_t offset);
1355 1438 QPDFObjectHandle objGenToIndirect(QPDFObjGen const&);
1356   - void calculateLinearizationData(
1357   - std::map<int, int> const& object_stream_data);
  1439 + void
  1440 + calculateLinearizationData(std::map<int, int> const& object_stream_data);
1358 1441 void pushOutlinesToPart(
1359 1442 std::vector<QPDFObjectHandle>& part,
1360 1443 std::set<QPDFObjGen>& lc_outlines,
1361 1444 std::map<int, int> const& object_stream_data);
1362 1445 int outputLengthNextN(
1363   - int in_object, int n,
  1446 + int in_object,
  1447 + int n,
1364 1448 std::map<int, qpdf_offset_t> const& lengths,
1365 1449 std::map<int, int> const& obj_renumber);
1366 1450 void calculateHPageOffset(
... ... @@ -1379,37 +1463,46 @@ class QPDF
1379 1463 void writeHSharedObject(BitWriter&);
1380 1464 void writeHGeneric(BitWriter&, HGeneric&);
1381 1465  
1382   -
1383 1466 // Methods to support optimization
1384 1467  
1385   - void pushInheritedAttributesToPage(bool allow_changes,
1386   - bool warn_skipped_keys);
  1468 + void
  1469 + pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys);
1387 1470 void pushInheritedAttributesToPageInternal(
1388 1471 QPDFObjectHandle,
1389   - std::map<std::string, std::vector<QPDFObjectHandle> >&,
  1472 + std::map<std::string, std::vector<QPDFObjectHandle>>&,
1390 1473 std::vector<QPDFObjectHandle>& all_pages,
1391   - bool allow_changes, bool warn_skipped_keys,
  1474 + bool allow_changes,
  1475 + bool warn_skipped_keys,
1392 1476 std::set<QPDFObjGen>& visited);
1393 1477 void updateObjectMaps(
1394   - ObjUser const& ou, QPDFObjectHandle oh,
  1478 + ObjUser const& ou,
  1479 + QPDFObjectHandle oh,
1395 1480 std::function<int(QPDFObjectHandle&)> skip_stream_parameters);
1396 1481 void updateObjectMapsInternal(
1397   - ObjUser const& ou, QPDFObjectHandle oh,
  1482 + ObjUser const& ou,
  1483 + QPDFObjectHandle oh,
1398 1484 std::function<int(QPDFObjectHandle&)> skip_stream_parameters,
1399   - std::set<QPDFObjGen>& visited, bool top,
  1485 + std::set<QPDFObjGen>& visited,
  1486 + bool top,
1400 1487 int depth);
1401 1488 void filterCompressedObjects(std::map<int, int> const& object_stream_data);
1402 1489  
1403 1490 // Type conversion helper methods
1404   - template<typename T> static qpdf_offset_t toO(T const& i)
  1491 + template <typename T>
  1492 + static qpdf_offset_t
  1493 + toO(T const& i)
1405 1494 {
1406 1495 return QIntC::to_offset(i);
1407 1496 }
1408   - template<typename T> static size_t toS(T const& i)
  1497 + template <typename T>
  1498 + static size_t
  1499 + toS(T const& i)
1409 1500 {
1410 1501 return QIntC::to_size(i);
1411 1502 }
1412   - template<typename T> static int toI(T const& i)
  1503 + template <typename T>
  1504 + static int
  1505 + toI(T const& i)
1413 1506 {
1414 1507 return QIntC::to_int(i);
1415 1508 }
... ... @@ -1490,8 +1583,8 @@ class QPDF
1490 1583 std::vector<QPDFObjectHandle> part9;
1491 1584  
1492 1585 // Optimization data
1493   - std::map<ObjUser, std::set<QPDFObjGen> > obj_user_to_objects;
1494   - std::map<QPDFObjGen, std::set<ObjUser> > object_to_obj_users;
  1586 + std::map<ObjUser, std::set<QPDFObjGen>> obj_user_to_objects;
  1587 + std::map<QPDFObjGen, std::set<ObjUser>> object_to_obj_users;
1495 1588 };
1496 1589  
1497 1590 // Keep all member variables inside the Members object, which we
... ...
include/qpdf/QPDFAcroFormDocumentHelper.hh
... ... @@ -67,7 +67,6 @@
67 67 // the "/AcroForm" field entry until you find the annotation
68 68 // dictionary.
69 69  
70   -
71 70 #include <qpdf/QPDFDocumentHelper.hh>
72 71  
73 72 #include <qpdf/DLL.h>
... ... @@ -144,8 +143,7 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
144 143 // information to be accurate, any changes to field names must be
145 144 // done through setFormFieldName() above.
146 145 QPDF_DLL
147   - std::set<QPDFObjGen>
148   - getFieldsWithQualifiedName(std::string const& name);
  146 + std::set<QPDFObjGen> getFieldsWithQualifiedName(std::string const& name);
149 147  
150 148 // Return the annotations associated with a terminal field. Note
151 149 // that in the case of a field having a single annotation, the
... ... @@ -153,17 +151,17 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
153 151 // object for the field.
154 152 QPDF_DLL
155 153 std::vector<QPDFAnnotationObjectHelper>
156   - getAnnotationsForField(QPDFFormFieldObjectHelper);
  154 + getAnnotationsForField(QPDFFormFieldObjectHelper);
157 155  
158 156 // Return annotations of subtype /Widget for a page.
159 157 QPDF_DLL
160 158 std::vector<QPDFAnnotationObjectHelper>
161   - getWidgetAnnotationsForPage(QPDFPageObjectHelper);
  159 + getWidgetAnnotationsForPage(QPDFPageObjectHelper);
162 160  
163 161 // Return top-level form fields for a page.
164 162 QPDF_DLL
165 163 std::vector<QPDFFormFieldObjectHelper>
166   - getFormFieldsForPage(QPDFPageObjectHelper);
  164 + getFormFieldsForPage(QPDFPageObjectHelper);
167 165  
168 166 // Return the terminal field that is associated with this
169 167 // annotation. If the annotation dictionary is merged with the
... ... @@ -173,8 +171,7 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
173 171 // not be an associated field, and this method will return a
174 172 // helper associated with a null object (isNull() == true).
175 173 QPDF_DLL
176   - QPDFFormFieldObjectHelper
177   - getFieldForAnnotation(QPDFAnnotationObjectHelper);
  174 + QPDFFormFieldObjectHelper getFieldForAnnotation(QPDFAnnotationObjectHelper);
178 175  
179 176 // Return the current value of /NeedAppearances. If
180 177 // /NeedAppearances is missing, return false as that is how PDF
... ... @@ -260,30 +257,33 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
260 257 // happens with qpdf --split-pages.
261 258 [[deprecated("Use fixCopiedAnnotations instead")]]
262 259 // ABI: delete this method
263   - QPDF_DLL
264   - void copyFieldsFromForeignPage(
  260 + QPDF_DLL void
  261 + copyFieldsFromForeignPage(
265 262 QPDFPageObjectHelper foreign_page,
266 263 QPDFAcroFormDocumentHelper& foreign_afdh,
267 264 std::vector<QPDFObjectHandle>* copied_fields = nullptr);
268 265  
269 266 private:
270 267 void analyze();
271   - void traverseField(QPDFObjectHandle field,
272   - QPDFObjectHandle parent,
273   - int depth, std::set<QPDFObjGen>& visited);
  268 + void traverseField(
  269 + QPDFObjectHandle field,
  270 + QPDFObjectHandle parent,
  271 + int depth,
  272 + std::set<QPDFObjGen>& visited);
274 273 QPDFObjectHandle getOrCreateAcroForm();
275 274 void adjustInheritedFields(
276 275 QPDFObjectHandle obj,
277   - bool override_da, std::string const& from_default_da,
278   - bool override_q, int from_default_q);
  276 + bool override_da,
  277 + std::string const& from_default_da,
  278 + bool override_q,
  279 + int from_default_q);
279 280 void adjustDefaultAppearances(
280 281 QPDFObjectHandle obj,
281   - std::map<std::string,
282   - std::map<std::string, std::string>> const& dr_map);
  282 + std::map<std::string, std::map<std::string, std::string>> const&
  283 + dr_map);
283 284 void adjustAppearanceStream(
284 285 QPDFObjectHandle stream,
285   - std::map<std::string,
286   - std::map<std::string, std::string>> dr_map);
  286 + std::map<std::string, std::map<std::string, std::string>> dr_map);
287 287  
288 288 class Members
289 289 {
... ... @@ -298,9 +298,8 @@ class QPDFAcroFormDocumentHelper: public QPDFDocumentHelper
298 298 Members(Members const&);
299 299  
300 300 bool cache_valid;
301   - std::map<QPDFObjGen,
302   - std::vector<QPDFAnnotationObjectHelper>
303   - > field_to_annotations;
  301 + std::map<QPDFObjGen, std::vector<QPDFAnnotationObjectHelper>>
  302 + field_to_annotations;
304 303 std::map<QPDFObjGen, QPDFFormFieldObjectHelper> annotation_to_field;
305 304 std::map<QPDFObjGen, std::string> field_to_name;
306 305 std::map<std::string, std::set<QPDFObjGen>> name_to_fields;
... ...
include/qpdf/QPDFAnnotationObjectHelper.hh
... ... @@ -22,8 +22,8 @@
22 22 #ifndef QPDFANNOTATIONOBJECTHELPER_HH
23 23 #define QPDFANNOTATIONOBJECTHELPER_HH
24 24  
25   -#include <qpdf/QPDFObjectHelper.hh>
26 25 #include <qpdf/Constants.h>
  26 +#include <qpdf/QPDFObjectHelper.hh>
27 27  
28 28 #include <qpdf/DLL.h>
29 29  
... ... @@ -78,8 +78,8 @@ class QPDFAnnotationObjectHelper: public QPDFObjectHelper
78 78 // which appearance stream is desired. If not specified, the
79 79 // appearance state in "/AS" will used.
80 80 QPDF_DLL
81   - QPDFObjectHandle getAppearanceStream(std::string const& which,
82   - std::string const& state = "");
  81 + QPDFObjectHandle getAppearanceStream(
  82 + std::string const& which, std::string const& state = "");
83 83  
84 84 // Generate text suitable for addition to the containing page's
85 85 // content stream that draws this annotation's appearance stream
... ... @@ -96,7 +96,8 @@ class QPDFAnnotationObjectHelper: public QPDFObjectHelper
96 96 // preparing to print.
97 97 QPDF_DLL
98 98 std::string getPageContentForAppearance(
99   - std::string const& name, int rotate,
  99 + std::string const& name,
  100 + int rotate,
100 101 int required_flags = 0,
101 102 int forbidden_flags = an_invisible | an_hidden);
102 103  
... ...
include/qpdf/QPDFCryptoImpl.hh
... ... @@ -77,19 +77,22 @@ class QPDF_DLL_CLASS QPDFCryptoImpl
77 77 virtual void RC4_init(unsigned char const* key_data, int key_len = -1) = 0;
78 78 // out_data = 0 means to encrypt/decrypt in place
79 79 QPDF_DLL
80   - virtual void RC4_process(unsigned char* in_data, size_t len,
81   - unsigned char* out_data = 0) = 0;
  80 + virtual void RC4_process(
  81 + unsigned char* in_data, size_t len, unsigned char* out_data = 0) = 0;
82 82 QPDF_DLL
83 83 virtual void RC4_finalize() = 0;
84 84  
85 85 static size_t constexpr rijndael_buf_size = 16;
86 86 QPDF_DLL
87 87 virtual void rijndael_init(
88   - bool encrypt, unsigned char const* key_data, size_t key_len,
89   - bool cbc_mode, unsigned char* cbc_block) = 0;
90   - QPDF_DLL
91   - virtual void rijndael_process(
92   - unsigned char* in_data, unsigned char* out_data) = 0;
  88 + bool encrypt,
  89 + unsigned char const* key_data,
  90 + size_t key_len,
  91 + bool cbc_mode,
  92 + unsigned char* cbc_block) = 0;
  93 + QPDF_DLL
  94 + virtual void
  95 + rijndael_process(unsigned char* in_data, unsigned char* out_data) = 0;
93 96 QPDF_DLL
94 97 virtual void rijndael_finalize() = 0;
95 98 };
... ...
include/qpdf/QPDFCryptoProvider.hh
... ... @@ -24,11 +24,11 @@
24 24  
25 25 #include <qpdf/DLL.h>
26 26 #include <qpdf/QPDFCryptoImpl.hh>
27   -#include <string>
  27 +#include <functional>
28 28 #include <map>
29   -#include <set>
30 29 #include <memory>
31   -#include <functional>
  30 +#include <set>
  31 +#include <string>
32 32  
33 33 // This class is part of qpdf's pluggable crypto provider support.
34 34 // Most users won't need to know or care about this class, but you can
... ... @@ -38,7 +38,6 @@
38 38 class QPDFCryptoProvider
39 39 {
40 40 public:
41   -
42 41 // Methods for getting and registering crypto implementations.
43 42 // These methods are not thread-safe.
44 43  
... ... @@ -50,15 +49,13 @@ class QPDFCryptoProvider
50 49 // Return an instance of the crypto provider registered using the
51 50 // given name.
52 51 QPDF_DLL
53   - static std::shared_ptr<QPDFCryptoImpl>
54   - getImpl(std::string const& name);
  52 + static std::shared_ptr<QPDFCryptoImpl> getImpl(std::string const& name);
55 53  
56 54 // Register the given type (T) as a crypto implementation. T must
57 55 // be derived from QPDFCryptoImpl and must have a constructor that
58 56 // takes no arguments.
59   - template<typename T>
60   - QPDF_DLL
61   - static void registerImpl(std::string const& name);
  57 + template <typename T>
  58 + QPDF_DLL static void registerImpl(std::string const& name);
62 59  
63 60 // Set the crypto provider registered with the given name as the
64 61 // default crypto implementation.
... ... @@ -83,7 +80,7 @@ class QPDFCryptoProvider
83 80  
84 81 std::shared_ptr<QPDFCryptoImpl>
85 82 getImpl_internal(std::string const& name) const;
86   - template<typename T>
  83 + template <typename T>
87 84 void registerImpl_internal(std::string const& name);
88 85 void setDefaultProvider_internal(std::string const& name);
89 86  
... ... @@ -100,7 +97,7 @@ class QPDFCryptoProvider
100 97 Members(Members const&) = delete;
101 98 Members& operator=(Members const&) = delete;
102 99  
103   - typedef std::function<std::shared_ptr<QPDFCryptoImpl> ()> provider_fn;
  100 + typedef std::function<std::shared_ptr<QPDFCryptoImpl>()> provider_fn;
104 101 std::string default_provider;
105 102 std::map<std::string, provider_fn> providers;
106 103 };
... ...
include/qpdf/QPDFDocumentHelper.hh
... ... @@ -49,12 +49,14 @@ class QPDFDocumentHelper
49 49 {
50 50 }
51 51 QPDF_DLL
52   - QPDF& getQPDF()
  52 + QPDF&
  53 + getQPDF()
53 54 {
54 55 return this->qpdf;
55 56 }
56 57 QPDF_DLL
57   - QPDF const& getQPDF() const
  58 + QPDF const&
  59 + getQPDF() const
58 60 {
59 61 return this->qpdf;
60 62 }
... ...
include/qpdf/QPDFEmbeddedFileDocumentHelper.hh
... ... @@ -24,13 +24,13 @@
24 24  
25 25 #include <qpdf/QPDFDocumentHelper.hh>
26 26  
  27 +#include <qpdf/DLL.h>
27 28 #include <qpdf/QPDF.hh>
28   -#include <qpdf/QPDFNameTreeObjectHelper.hh>
29 29 #include <qpdf/QPDFFileSpecObjectHelper.hh>
30   -#include <qpdf/DLL.h>
  30 +#include <qpdf/QPDFNameTreeObjectHelper.hh>
31 31  
32   -#include <memory>
33 32 #include <map>
  33 +#include <memory>
34 34  
35 35 // This class provides a higher level interface around document-level
36 36 // file attachments, also known as embedded files. These are discussed
... ... @@ -48,8 +48,8 @@ class QPDFEmbeddedFileDocumentHelper: public QPDFDocumentHelper
48 48 bool hasEmbeddedFiles() const;
49 49  
50 50 QPDF_DLL
51   - std::map<std::string,
52   - std::shared_ptr<QPDFFileSpecObjectHelper>> getEmbeddedFiles();
  51 + std::map<std::string, std::shared_ptr<QPDFFileSpecObjectHelper>>
  52 + getEmbeddedFiles();
53 53  
54 54 // If an embedded file with the given name exists, return a
55 55 // (shared) pointer to it. Otherwise, return nullptr.
... ...
include/qpdf/QPDFExc.hh
... ... @@ -22,22 +22,23 @@
22 22 #ifndef QPDFEXC_HH
23 23 #define QPDFEXC_HH
24 24  
  25 +#include <qpdf/Constants.h>
25 26 #include <qpdf/DLL.h>
26 27 #include <qpdf/Types.h>
27   -#include <qpdf/Constants.h>
28 28  
29   -#include <string>
30 29 #include <stdexcept>
  30 +#include <string>
31 31  
32 32 class QPDF_DLL_CLASS QPDFExc: public std::runtime_error
33 33 {
34 34 public:
35 35 QPDF_DLL
36   - QPDFExc(qpdf_error_code_e error_code,
37   - std::string const& filename,
38   - std::string const& object,
39   - qpdf_offset_t offset,
40   - std::string const& message);
  36 + QPDFExc(
  37 + qpdf_error_code_e error_code,
  38 + std::string const& filename,
  39 + std::string const& object,
  40 + qpdf_offset_t offset,
  41 + std::string const& message);
41 42 QPDF_DLL
42 43 virtual ~QPDFExc() noexcept
43 44 {
... ... @@ -65,10 +66,11 @@ class QPDF_DLL_CLASS QPDFExc: public std::runtime_error
65 66 std::string const& getMessageDetail() const;
66 67  
67 68 private:
68   - static std::string createWhat(std::string const& filename,
69   - std::string const& object,
70   - qpdf_offset_t offset,
71   - std::string const& message);
  69 + static std::string createWhat(
  70 + std::string const& filename,
  71 + std::string const& object,
  72 + qpdf_offset_t offset,
  73 + std::string const& message);
72 74  
73 75 // This class does not use the Members pattern to avoid needless
74 76 // memory allocations during exception handling.
... ...
include/qpdf/QPDFFileSpecObjectHelper.hh
... ... @@ -26,8 +26,8 @@
26 26  
27 27 #include <qpdf/DLL.h>
28 28  
29   -#include <qpdf/QPDFObjectHandle.hh>
30 29 #include <qpdf/QPDFEFStreamObjectHelper.hh>
  30 +#include <qpdf/QPDFObjectHandle.hh>
31 31  
32 32 // This class provides a higher level interface around File
33 33 // Specification dictionaries, which are discussed in section 7.11 of
... ... @@ -77,22 +77,16 @@ class QPDFFileSpecObjectHelper: public QPDFObjectHelper
77 77 // filename, and attach the contents of the specified file as data
78 78 // in an embedded file stream.
79 79 QPDF_DLL
80   - static
81   - QPDFFileSpecObjectHelper createFileSpec(
82   - QPDF& qpdf,
83   - std::string const& filename,
84   - std::string const& fullpath);
  80 + static QPDFFileSpecObjectHelper createFileSpec(
  81 + QPDF& qpdf, std::string const& filename, std::string const& fullpath);
85 82  
86 83 // Create a new filespec as an indirect object with the given
87 84 // unicode filename and embedded file stream. The file name will
88 85 // be used as both /UF and /F. If you need to override, call
89 86 // setFilename.
90 87 QPDF_DLL
91   - static
92   - QPDFFileSpecObjectHelper createFileSpec(
93   - QPDF& qpdf,
94   - std::string const& filename,
95   - QPDFEFStreamObjectHelper);
  88 + static QPDFFileSpecObjectHelper createFileSpec(
  89 + QPDF& qpdf, std::string const& filename, QPDFEFStreamObjectHelper);
96 90  
97 91 QPDF_DLL
98 92 QPDFFileSpecObjectHelper& setDescription(std::string const&);
... ... @@ -103,8 +97,7 @@ class QPDFFileSpecObjectHelper: public QPDFObjectHelper
103 97 // might happen to have.
104 98 QPDF_DLL
105 99 QPDFFileSpecObjectHelper& setFilename(
106   - std::string const& unicode_name,
107   - std::string const& compat_name = "");
  100 + std::string const& unicode_name, std::string const& compat_name = "");
108 101  
109 102 private:
110 103 class Members
... ...
include/qpdf/QPDFFormFieldObjectHelper.hh
... ... @@ -185,8 +185,8 @@ class QPDFFormFieldObjectHelper: public QPDFObjectHelper
185 185 // of a field, use QPDFAcroFormDocumentHelper::setFormFieldName
186 186 // instead.
187 187 QPDF_DLL
188   - void setFieldAttribute(std::string const& key,
189   - std::string const& utf8_value);
  188 + void
  189 + setFieldAttribute(std::string const& key, std::string const& utf8_value);
190 190  
191 191 // Set /V (field value) to the given value. If need_appearances is
192 192 // true and the field type is either /Tx (text) or /Ch (choice),
... ...
include/qpdf/QPDFJob.hh
... ... @@ -22,22 +22,21 @@
22 22 #ifndef QPDFJOB_HH
23 23 #define QPDFJOB_HH
24 24  
25   -#include <qpdf/DLL.h>
26 25 #include <qpdf/Constants.h>
  26 +#include <qpdf/DLL.h>
  27 +#include <qpdf/PDFVersion.hh>
27 28 #include <qpdf/QPDF.hh>
28 29 #include <qpdf/QPDFPageObjectHelper.hh>
29   -#include <qpdf/PDFVersion.hh>
30 30  
31   -#include <memory>
32   -#include <string>
  31 +#include <functional>
  32 +#include <iostream>
33 33 #include <list>
34   -#include <vector>
35   -#include <set>
36 34 #include <map>
37   -#include <iostream>
38   -#include <functional>
39 35 #include <memory>
  36 +#include <set>
40 37 #include <stdexcept>
  38 +#include <string>
  39 +#include <vector>
41 40  
42 41 class QPDFWriter;
43 42  
... ... @@ -79,8 +78,8 @@ class QPDFJob
79 78 // about converting arguments to UTF-8. This method will mutate
80 79 // arguments that are passed to it.
81 80 QPDF_DLL
82   - void initializeFromArgv(char const* const argv[],
83   - char const* progname_env = nullptr);
  81 + void initializeFromArgv(
  82 + char const* const argv[], char const* progname_env = nullptr);
84 83  
85 84 // Initialize a QPDFJob from json. Passing partial = true prevents
86 85 // this method from doing the final checks (calling
... ... @@ -155,9 +154,10 @@ class QPDFJob
155 154  
156 155 struct PageSpec
157 156 {
158   - PageSpec(std::string const& filename,
159   - char const* password,
160   - std::string const& range);
  157 + PageSpec(
  158 + std::string const& filename,
  159 + char const* password,
  160 + std::string const& range);
161 161  
162 162 std::string filename;
163 163 std::shared_ptr<char> password;
... ... @@ -203,13 +203,14 @@ class QPDFJob
203 203 {
204 204 friend class QPDFJob;
205 205 friend class Config;
  206 +
206 207 public:
207 208 QPDF_DLL
208 209 Config* endAddAttachment();
209 210 QPDF_DLL
210 211 AttConfig* file(std::string const& parameter);
211 212  
212   -# include <qpdf/auto_job_c_att.hh>
  213 +#include <qpdf/auto_job_c_att.hh>
213 214  
214 215 private:
215 216 AttConfig(Config*);
... ... @@ -223,13 +224,14 @@ class QPDFJob
223 224 {
224 225 friend class QPDFJob;
225 226 friend class Config;
  227 +
226 228 public:
227 229 QPDF_DLL
228 230 Config* endCopyAttachmentsFrom();
229 231 QPDF_DLL
230 232 CopyAttConfig* file(std::string const& parameter);
231 233  
232   -# include <qpdf/auto_job_c_copy_att.hh>
  234 +#include <qpdf/auto_job_c_copy_att.hh>
233 235  
234 236 private:
235 237 CopyAttConfig(Config*);
... ... @@ -243,15 +245,17 @@ class QPDFJob
243 245 {
244 246 friend class QPDFJob;
245 247 friend class Config;
  248 +
246 249 public:
247 250 QPDF_DLL
248 251 Config* endPages();
249 252 QPDF_DLL
250   - PagesConfig* pageSpec(std::string const& filename,
251   - std::string const& range,
252   - char const* password = nullptr);
  253 + PagesConfig* pageSpec(
  254 + std::string const& filename,
  255 + std::string const& range,
  256 + char const* password = nullptr);
253 257  
254   -# include <qpdf/auto_job_c_pages.hh>
  258 +#include <qpdf/auto_job_c_pages.hh>
255 259  
256 260 private:
257 261 PagesConfig(Config*);
... ... @@ -264,13 +268,14 @@ class QPDFJob
264 268 {
265 269 friend class QPDFJob;
266 270 friend class Config;
  271 +
267 272 public:
268 273 QPDF_DLL
269 274 Config* endUnderlayOverlay();
270 275 QPDF_DLL
271 276 UOConfig* file(std::string const& parameter);
272 277  
273   -# include <qpdf/auto_job_c_uo.hh>
  278 +#include <qpdf/auto_job_c_uo.hh>
274 279  
275 280 private:
276 281 UOConfig(Config*);
... ... @@ -283,13 +288,14 @@ class QPDFJob
283 288 {
284 289 friend class QPDFJob;
285 290 friend class Config;
  291 +
286 292 public:
287 293 QPDF_DLL
288 294 Config* endEncrypt();
289 295 QPDF_DLL
290 296 EncConfig* file(std::string const& parameter);
291 297  
292   -# include <qpdf/auto_job_c_enc.hh>
  298 +#include <qpdf/auto_job_c_enc.hh>
293 299  
294 300 private:
295 301 EncConfig(Config*);
... ... @@ -301,6 +307,7 @@ class QPDFJob
301 307 class Config
302 308 {
303 309 friend class QPDFJob;
  310 +
304 311 public:
305 312 // Proxy to QPDFJob::checkConfiguration()
306 313 QPDF_DLL
... ... @@ -331,7 +338,7 @@ class QPDFJob
331 338 std::string const& user_password,
332 339 std::string const& owner_password);
333 340  
334   -# include <qpdf/auto_job_c_main.hh>
  341 +#include <qpdf/auto_job_c_main.hh>
335 342  
336 343 private:
337 344 Config() = delete;
... ... @@ -444,32 +451,37 @@ class QPDFJob
444 451  
445 452 // Basic file processing
446 453 std::shared_ptr<QPDF> processFile(
447   - char const* filename, char const* password,
448   - bool used_for_input);
  454 + char const* filename, char const* password, bool used_for_input);
449 455 std::shared_ptr<QPDF> processInputSource(
450   - PointerHolder<InputSource> is, char const* password,
  456 + PointerHolder<InputSource> is,
  457 + char const* password,
451 458 bool used_for_input);
452 459 std::shared_ptr<QPDF> doProcess(
453 460 std::function<void(QPDF*, char const*)> fn,
454   - char const* password, bool empty, bool used_for_input);
  461 + char const* password,
  462 + bool empty,
  463 + bool used_for_input);
455 464 std::shared_ptr<QPDF> doProcessOnce(
456 465 std::function<void(QPDF*, char const*)> fn,
457   - char const* password, bool empty, bool used_for_input);
  466 + char const* password,
  467 + bool empty,
  468 + bool used_for_input);
458 469  
459 470 // Transformations
460 471 void setQPDFOptions(QPDF& pdf);
461 472 void handlePageSpecs(
462   - QPDF& pdf, bool& warnings,
  473 + QPDF& pdf,
  474 + bool& warnings,
463 475 std::vector<std::shared_ptr<QPDF>>& page_heap);
464 476 bool shouldRemoveUnreferencedResources(QPDF& pdf);
465 477 void handleRotations(QPDF& pdf);
466   - void getUOPagenos(UnderOverlay& uo,
467   - std::map<int, std::vector<int> >& pagenos);
  478 + void
  479 + getUOPagenos(UnderOverlay& uo, std::map<int, std::vector<int>>& pagenos);
468 480 void handleUnderOverlay(QPDF& pdf);
469 481 void doUnderOverlayForPage(
470 482 QPDF& pdf,
471 483 UnderOverlay& uo,
472   - std::map<int, std::vector<int> >& pagenos,
  484 + std::map<int, std::vector<int>>& pagenos,
473 485 size_t page_idx,
474 486 std::map<int, QPDFObjectHandle>& fo,
475 487 std::vector<QPDFPageObjectHelper>& pages,
... ...
include/qpdf/QPDFMatrix.hh
... ... @@ -22,8 +22,8 @@
22 22 #ifndef QPDFMATRIX_HH
23 23 #define QPDFMATRIX_HH
24 24  
25   -#include <qpdf/QPDFObjectHandle.hh>
26 25 #include <qpdf/DLL.h>
  26 +#include <qpdf/QPDFObjectHandle.hh>
27 27 #include <string>
28 28  
29 29 // This class represents a PDF transformation matrix using a tuple
... ... @@ -41,8 +41,7 @@ class QPDFMatrix
41 41 QPDF_DLL
42 42 QPDFMatrix();
43 43 QPDF_DLL
44   - QPDFMatrix(double a, double b, double c,
45   - double d, double e, double f);
  44 + QPDFMatrix(double a, double b, double c, double d, double e, double f);
46 45 QPDF_DLL
47 46 QPDFMatrix(QPDFObjectHandle::Matrix const&);
48 47  
... ... @@ -83,21 +82,22 @@ class QPDFMatrix
83 82 // bounds the polygon resulting from transforming the four
84 83 // corners.
85 84 QPDF_DLL
86   - QPDFObjectHandle::Rectangle transformRectangle(
87   - QPDFObjectHandle::Rectangle r) const;
  85 + QPDFObjectHandle::Rectangle
  86 + transformRectangle(QPDFObjectHandle::Rectangle r) const;
88 87 // ABI: delete non-const version
89 88 QPDF_DLL
90   - QPDFObjectHandle::Rectangle transformRectangle(
91   - QPDFObjectHandle::Rectangle r);
  89 + QPDFObjectHandle::Rectangle
  90 + transformRectangle(QPDFObjectHandle::Rectangle r);
92 91  
93 92 // operator== tests for exact equality, not considering deltas for
94 93 // floating point.
95 94 QPDF_DLL
96 95 bool operator==(QPDFMatrix const& rhs) const;
97 96 QPDF_DLL
98   - bool operator!=(QPDFMatrix const& rhs) const
  97 + bool
  98 + operator!=(QPDFMatrix const& rhs) const
99 99 {
100   - return ! operator==(rhs);
  100 + return !operator==(rhs);
101 101 }
102 102  
103 103 double a;
... ...
include/qpdf/QPDFNameTreeObjectHelper.hh
... ... @@ -22,11 +22,11 @@
22 22 #ifndef QPDFNAMETREEOBJECTHELPER_HH
23 23 #define QPDFNAMETREEOBJECTHELPER_HH
24 24  
25   -#include <qpdf/QPDFObjectHelper.hh>
26 25 #include <qpdf/QPDFObjGen.hh>
  26 +#include <qpdf/QPDFObjectHelper.hh>
  27 +#include <iterator>
27 28 #include <map>
28 29 #include <memory>
29   -#include <iterator>
30 30  
31 31 #include <qpdf/DLL.h>
32 32  
... ... @@ -48,16 +48,14 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
48 48 // The qpdf object is required so that this class can issue
49 49 // warnings, attempt repairs, and add indirect objects.
50 50 QPDF_DLL
51   - QPDFNameTreeObjectHelper(QPDFObjectHandle, QPDF&,
52   - bool auto_repair = true);
  51 + QPDFNameTreeObjectHelper(QPDFObjectHandle, QPDF&, bool auto_repair = true);
53 52  
54 53 // ABI: Legacy Constructor will be removed in QPDF 11. A
55 54 // QPDFNameTreeObjectHelper constructed in this way can't be
56 55 // modified or repaired and will silently ignore problems in the
57 56 // structure.
58   - [[deprecated("use constructor that takes QPDF&")]]
59   - QPDF_DLL
60   - QPDFNameTreeObjectHelper(QPDFObjectHandle);
  57 + [[deprecated("use constructor that takes QPDF&")]] QPDF_DLL
  58 + QPDFNameTreeObjectHelper(QPDFObjectHandle);
61 59  
62 60 // Create an empty name tree
63 61 QPDF_DLL
... ... @@ -80,6 +78,7 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
80 78 class iterator
81 79 {
82 80 friend class QPDFNameTreeObjectHelper;
  81 +
83 82 public:
84 83 typedef std::pair<std::string, QPDFObjectHandle> T;
85 84 using iterator_category = std::bidirectional_iterator_tag;
... ... @@ -94,7 +93,8 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
94 93 QPDF_DLL
95 94 iterator& operator++();
96 95 QPDF_DLL
97   - iterator operator++(int)
  96 + iterator
  97 + operator++(int)
98 98 {
99 99 iterator t = *this;
100 100 ++(*this);
... ... @@ -103,7 +103,8 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
103 103 QPDF_DLL
104 104 iterator& operator--();
105 105 QPDF_DLL
106   - iterator operator--(int)
  106 + iterator
  107 + operator--(int)
107 108 {
108 109 iterator t = *this;
109 110 --(*this);
... ... @@ -116,9 +117,10 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
116 117 QPDF_DLL
117 118 bool operator==(iterator const& other) const;
118 119 QPDF_DLL
119   - bool operator!=(iterator const& other) const
  120 + bool
  121 + operator!=(iterator const& other) const
120 122 {
121   - return ! operator==(other);
  123 + return !operator==(other);
122 124 }
123 125  
124 126 // DANGER: this method can create inconsistent trees if not
... ... @@ -162,8 +164,8 @@ class QPDFNameTreeObjectHelper: public QPDFObjectHelper
162 164 // Find the entry with the given key. If return_prev_if_not_found
163 165 // is true and the item is not found, return the next lower item.
164 166 QPDF_DLL
165   - iterator find(std::string const& key,
166   - bool return_prev_if_not_found = false);
  167 + iterator
  168 + find(std::string const& key, bool return_prev_if_not_found = false);
167 169  
168 170 // Insert a new item. If the key already exists, it is replaced.
169 171 QPDF_DLL
... ...
include/qpdf/QPDFNumberTreeObjectHelper.hh
... ... @@ -22,8 +22,8 @@
22 22 #ifndef QPDFNUMBERTREEOBJECTHELPER_HH
23 23 #define QPDFNUMBERTREEOBJECTHELPER_HH
24 24  
25   -#include <qpdf/QPDFObjectHelper.hh>
26 25 #include <qpdf/QPDFObjGen.hh>
  26 +#include <qpdf/QPDFObjectHelper.hh>
27 27 #include <map>
28 28 #include <memory>
29 29  
... ... @@ -45,16 +45,15 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper
45 45 // The qpdf object is required so that this class can issue
46 46 // warnings, attempt repairs, and add indirect objects.
47 47 QPDF_DLL
48   - QPDFNumberTreeObjectHelper(QPDFObjectHandle, QPDF&,
49   - bool auto_repair = true);
  48 + QPDFNumberTreeObjectHelper(
  49 + QPDFObjectHandle, QPDF&, bool auto_repair = true);
50 50  
51 51 // ABI: Legacy Constructor will be removed in QPDF 11. A
52 52 // QPDFNumberTreeObjectHelper constructed in this way can't be
53 53 // modified or repaired and will silently ignore problems in the
54 54 // structure.
55   - [[deprecated("use constructor that takes QPDF&")]]
56   - QPDF_DLL
57   - QPDFNumberTreeObjectHelper(QPDFObjectHandle);
  55 + [[deprecated("use constructor that takes QPDF&")]] QPDF_DLL
  56 + QPDFNumberTreeObjectHelper(QPDFObjectHandle);
58 57  
59 58 // ABI: = default
60 59 QPDF_DLL
... ... @@ -93,12 +92,13 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper
93 92 // oh to the value with index 3, and set offset to 2 (5 - 3). See
94 93 // also find().
95 94 QPDF_DLL
96   - bool findObjectAtOrBelow(numtree_number idx, QPDFObjectHandle& oh,
97   - numtree_number& offset);
  95 + bool findObjectAtOrBelow(
  96 + numtree_number idx, QPDFObjectHandle& oh, numtree_number& offset);
98 97  
99 98 class iterator
100 99 {
101 100 friend class QPDFNumberTreeObjectHelper;
  101 +
102 102 public:
103 103 typedef std::pair<numtree_number, QPDFObjectHandle> T;
104 104 using iterator_category = std::bidirectional_iterator_tag;
... ... @@ -113,7 +113,8 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper
113 113 QPDF_DLL
114 114 iterator& operator++();
115 115 QPDF_DLL
116   - iterator operator++(int)
  116 + iterator
  117 + operator++(int)
117 118 {
118 119 iterator t = *this;
119 120 ++(*this);
... ... @@ -122,7 +123,8 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper
122 123 QPDF_DLL
123 124 iterator& operator--();
124 125 QPDF_DLL
125   - iterator operator--(int)
  126 + iterator
  127 + operator--(int)
126 128 {
127 129 iterator t = *this;
128 130 --(*this);
... ... @@ -135,9 +137,10 @@ class QPDFNumberTreeObjectHelper: public QPDFObjectHelper
135 137 QPDF_DLL
136 138 bool operator==(iterator const& other) const;
137 139 QPDF_DLL
138   - bool operator!=(iterator const& other) const
  140 + bool
  141 + operator!=(iterator const& other) const
139 142 {
140   - return ! operator==(other);
  143 + return !operator==(other);
141 144 }
142 145  
143 146 // DANGER: this method can create inconsistent trees if not
... ...
include/qpdf/QPDFObject.hh
... ... @@ -22,10 +22,10 @@
22 22 #ifndef QPDFOBJECT_HH
23 23 #define QPDFOBJECT_HH
24 24  
  25 +#include <qpdf/Constants.h>
25 26 #include <qpdf/DLL.h>
26   -#include <qpdf/Types.h>
27 27 #include <qpdf/JSON.hh>
28   -#include <qpdf/Constants.h>
  28 +#include <qpdf/Types.h>
29 29  
30 30 #include <string>
31 31  
... ... @@ -62,7 +62,9 @@ class QPDF_DLL_CLASS QPDFObject
62 62 static constexpr object_type_e ot_operator = ::ot_operator;
63 63 static constexpr object_type_e ot_inlineimage = ::ot_inlineimage;
64 64  
65   - virtual ~QPDFObject() {}
  65 + virtual ~QPDFObject()
  66 + {
  67 + }
66 68 virtual std::string unparse() = 0;
67 69 virtual JSON getJSON() = 0;
68 70  
... ... @@ -78,11 +80,12 @@ class QPDF_DLL_CLASS QPDFObject
78 80 {
79 81 friend class QPDF;
80 82 friend class QPDFObjectHandle;
  83 +
81 84 private:
82   - static void releaseResolved(QPDFObject* o)
  85 + static void
  86 + releaseResolved(QPDFObject* o)
83 87 {
84   - if (o)
85   - {
  88 + if (o) {
86 89 o->releaseResolved();
87 90 }
88 91 }
... ... @@ -97,7 +100,10 @@ class QPDF_DLL_CLASS QPDFObject
97 100 qpdf_offset_t getParsedOffset();
98 101  
99 102 protected:
100   - virtual void releaseResolved() {}
  103 + virtual void
  104 + releaseResolved()
  105 + {
  106 + }
101 107  
102 108 private:
103 109 QPDFObject(QPDFObject const&) = delete;
... ...
include/qpdf/QPDFObjectHandle.hh
... ... @@ -22,21 +22,21 @@
22 22 #ifndef QPDFOBJECTHANDLE_HH
23 23 #define QPDFOBJECTHANDLE_HH
24 24  
  25 +#include <qpdf/Constants.h>
25 26 #include <qpdf/DLL.h>
26 27 #include <qpdf/Types.h>
27   -#include <qpdf/Constants.h>
28 28  
29   -#include <string>
30   -#include <vector>
31   -#include <set>
32   -#include <map>
33 29 #include <functional>
  30 +#include <map>
34 31 #include <memory>
  32 +#include <set>
  33 +#include <string>
  34 +#include <vector>
35 35  
36   -#include <qpdf/QPDFObjGen.hh>
37   -#include <qpdf/PointerHolder.hh>
38 36 #include <qpdf/Buffer.hh>
39 37 #include <qpdf/InputSource.hh>
  38 +#include <qpdf/PointerHolder.hh>
  39 +#include <qpdf/QPDFObjGen.hh>
40 40 #include <qpdf/QPDFTokenizer.hh>
41 41  
42 42 #include <qpdf/QPDFObject.hh>
... ... @@ -117,12 +117,15 @@ class QPDFObjectHandle
117 117 // version of the method, which should also return a boolean
118 118 // indicating whether it ran without errors.
119 119 QPDF_DLL
120   - virtual void provideStreamData(int objid, int generation,
121   - Pipeline* pipeline);
  120 + virtual void
  121 + provideStreamData(int objid, int generation, Pipeline* pipeline);
122 122 QPDF_DLL
123 123 virtual bool provideStreamData(
124   - int objid, int generation, Pipeline* pipeline,
125   - bool suppress_warnings, bool will_retry);
  124 + int objid,
  125 + int generation,
  126 + Pipeline* pipeline,
  127 + bool suppress_warnings,
  128 + bool will_retry);
126 129 QPDF_DLL
127 130 bool supportsRetry();
128 131  
... ... @@ -183,8 +186,10 @@ class QPDFObjectHandle
183 186 class PipelineAccessor
184 187 {
185 188 friend class Pl_QPDFTokenizer;
  189 +
186 190 private:
187   - static void setPipeline(TokenFilter* f, Pipeline* p)
  191 + static void
  192 + setPipeline(TokenFilter* f, Pipeline* p)
188 193 {
189 194 f->setPipeline(p);
190 195 }
... ... @@ -230,8 +235,8 @@ class QPDFObjectHandle
230 235 QPDF_DLL
231 236 virtual void handleObject(QPDFObjectHandle);
232 237 QPDF_DLL
233   - virtual void handleObject(
234   - QPDFObjectHandle, size_t offset, size_t length);
  238 + virtual void
  239 + handleObject(QPDFObjectHandle, size_t offset, size_t length);
235 240  
236 241 virtual void handleEOF() = 0;
237 242  
... ... @@ -261,8 +266,7 @@ class QPDFObjectHandle
261 266 ury(0.0)
262 267 {
263 268 }
264   - Rectangle(double llx, double lly,
265   - double urx, double ury) :
  269 + Rectangle(double llx, double lly, double urx, double ury) :
266 270 llx(llx),
267 271 lly(lly),
268 272 urx(urx),
... ... @@ -292,8 +296,7 @@ class QPDFObjectHandle
292 296 f(0.0)
293 297 {
294 298 }
295   - Matrix(double a, double b, double c,
296   - double d, double e, double f) :
  299 + Matrix(double a, double b, double c, double d, double e, double f) :
297 300 a(a),
298 301 b(b),
299 302 c(c),
... ... @@ -316,8 +319,7 @@ class QPDFObjectHandle
316 319 QPDF_DLL
317 320 QPDFObjectHandle(QPDFObjectHandle const&) = default;
318 321 QPDF_DLL
319   - QPDFObjectHandle&
320   - operator=(QPDFObjectHandle const&) = default;
  322 + QPDFObjectHandle& operator=(QPDFObjectHandle const&) = default;
321 323 QPDF_DLL
322 324 bool isInitialized() const;
323 325  
... ... @@ -381,14 +383,14 @@ class QPDFObjectHandle
381 383 // True if the object is a dictionary of the specified type and
382 384 // subtype, if any.
383 385 QPDF_DLL
384   - bool isDictionaryOfType(std::string const& type,
385   - std::string const& subtype = "");
  386 + bool isDictionaryOfType(
  387 + std::string const& type, std::string const& subtype = "");
386 388  
387 389 // True if the object is a stream of the specified type and
388 390 // subtype, if any.
389 391 QPDF_DLL
390   - bool isStreamOfType(std::string const& type,
391   - std::string const& subtype = "");
  392 + bool
  393 + isStreamOfType(std::string const& type, std::string const& subtype = "");
392 394  
393 395 // Public factory methods
394 396  
... ... @@ -406,8 +408,9 @@ class QPDFObjectHandle
406 408 // in the message of any QPDFExc exception thrown for invalid
407 409 // syntax. See also the global `operator ""_qpdf` defined below.
408 410 QPDF_DLL
409   - static QPDFObjectHandle parse(std::string const& object_str,
410   - std::string const& object_description = "");
  411 + static QPDFObjectHandle parse(
  412 + std::string const& object_str,
  413 + std::string const& object_description = "");
411 414  
412 415 // Construct an object of any type from a string representation of
413 416 // the object. Indirect object syntax (obj gen R) is allowed and
... ... @@ -419,9 +422,10 @@ class QPDFObjectHandle
419 422 // object, which will just be the first number and will report
420 423 // that there is trailing data at the end of the string.
421 424 QPDF_DLL
422   - static QPDFObjectHandle parse(QPDF* context,
423   - std::string const& object_str,
424   - std::string const& object_description = "");
  425 + static QPDFObjectHandle parse(
  426 + QPDF* context,
  427 + std::string const& object_str,
  428 + std::string const& object_description = "");
425 429  
426 430 // Construct an object as above by reading from the given
427 431 // InputSource at its current position and using the tokenizer you
... ... @@ -429,11 +433,13 @@ class QPDFObjectHandle
429 433 // This method is intended to be called by QPDF for parsing
430 434 // objects that are ready from the object's input stream.
431 435 QPDF_DLL
432   - static QPDFObjectHandle parse(PointerHolder<InputSource> input,
433   - std::string const& object_description,
434   - QPDFTokenizer&, bool& empty,
435   - StringDecrypter* decrypter,
436   - QPDF* context);
  436 + static QPDFObjectHandle parse(
  437 + PointerHolder<InputSource> input,
  438 + std::string const& object_description,
  439 + QPDFTokenizer&,
  440 + bool& empty,
  441 + StringDecrypter* decrypter,
  442 + QPDF* context);
437 443  
438 444 // Return the offset where the object was found when parsed. A
439 445 // negative value means that the object was created without
... ... @@ -449,8 +455,8 @@ class QPDFObjectHandle
449 455 // error messages will also be more useful because the page object
450 456 // information will be known.
451 457 QPDF_DLL
452   - static void parseContentStream(QPDFObjectHandle stream_or_array,
453   - ParserCallbacks* callbacks);
  458 + static void parseContentStream(
  459 + QPDFObjectHandle stream_or_array, ParserCallbacks* callbacks);
454 460  
455 461 // When called on a stream or stream array that is some page's
456 462 // content streams, do the same as pipePageContents. This method
... ... @@ -469,8 +475,10 @@ class QPDFObjectHandle
469 475 // Pl_Concatenate and then call manualFinish() on the
470 476 // Pl_Concatenate pipeline at the end.
471 477 QPDF_DLL
472   - void pipeContentStreams(Pipeline* p, std::string const& description,
473   - std::string& all_description);
  478 + void pipeContentStreams(
  479 + Pipeline* p,
  480 + std::string const& description,
  481 + std::string& all_description);
474 482  
475 483 // As of qpdf 8, it is possible to add custom token filters to a
476 484 // stream. The tokenized stream data is passed through the token
... ... @@ -525,8 +533,8 @@ class QPDFObjectHandle
525 533 // ABI: combine with other newReal by adding trim_trailing_zeroes
526 534 // above as an optional parameter with a default of true.
527 535 QPDF_DLL
528   - static QPDFObjectHandle newReal(double value, int decimal_places,
529   - bool trim_trailing_zeroes);
  536 + static QPDFObjectHandle
  537 + newReal(double value, int decimal_places, bool trim_trailing_zeroes);
530 538 // Note about name objects: qpdf's internal representation of a
531 539 // PDF name is a sequence of bytes, excluding the NUL character,
532 540 // and starting with a slash. Name objects as represented in the
... ... @@ -562,8 +570,8 @@ class QPDFObjectHandle
562 570 QPDF_DLL
563 571 static QPDFObjectHandle newArray();
564 572 QPDF_DLL
565   - static QPDFObjectHandle newArray(
566   - std::vector<QPDFObjectHandle> const& items);
  573 + static QPDFObjectHandle
  574 + newArray(std::vector<QPDFObjectHandle> const& items);
567 575 QPDF_DLL
568 576 static QPDFObjectHandle newArray(Rectangle const&);
569 577 QPDF_DLL
... ... @@ -573,8 +581,8 @@ class QPDFObjectHandle
573 581 QPDF_DLL
574 582 static QPDFObjectHandle newDictionary();
575 583 QPDF_DLL
576   - static QPDFObjectHandle newDictionary(
577   - std::map<std::string, QPDFObjectHandle> const& items);
  584 + static QPDFObjectHandle
  585 + newDictionary(std::map<std::string, QPDFObjectHandle> const& items);
578 586  
579 587 // Create an array from a rectangle. Equivalent to the rectangle
580 588 // form of newArray.
... ... @@ -646,8 +654,8 @@ class QPDFObjectHandle
646 654 // with proper context in some cases where it would otherwise
647 655 // raise exceptions.
648 656 QPDF_DLL
649   - void setObjectDescription(QPDF* owning_qpdf,
650   - std::string const& object_description);
  657 + void setObjectDescription(
  658 + QPDF* owning_qpdf, std::string const& object_description);
651 659 QPDF_DLL
652 660 bool hasObjectDescription();
653 661  
... ... @@ -959,8 +967,8 @@ class QPDFObjectHandle
959 967 // ABI: remove this version and make resource_names default to
960 968 // nullptr.
961 969 QPDF_DLL
962   - std::string getUniqueResourceName(std::string const& prefix,
963   - int& min_suffix);
  970 + std::string
  971 + getUniqueResourceName(std::string const& prefix, int& min_suffix);
964 972  
965 973 // Return the QPDF object that owns an indirect object. Returns
966 974 // null for a direct object.
... ... @@ -1081,8 +1089,8 @@ class QPDFObjectHandle
1081 1089 // Returns filtered (uncompressed) stream data. Throws an
1082 1090 // exception if the stream is filtered and we can't decode it.
1083 1091 QPDF_DLL
1084   - PointerHolder<Buffer> getStreamData(
1085   - qpdf_stream_decode_level_e level = qpdf_dl_generalized);
  1092 + PointerHolder<Buffer>
  1093 + getStreamData(qpdf_stream_decode_level_e level = qpdf_dl_generalized);
1086 1094  
1087 1095 // Returns unfiltered (raw) stream data.
1088 1096 QPDF_DLL
... ... @@ -1146,21 +1154,24 @@ class QPDFObjectHandle
1146 1154 // Return value is overall success, even if filtering is not
1147 1155 // requested.
1148 1156 QPDF_DLL
1149   - bool pipeStreamData(Pipeline*, bool* filtering_attempted,
1150   - int encode_flags,
1151   - qpdf_stream_decode_level_e decode_level,
1152   - bool suppress_warnings = false,
1153   - bool will_retry = false);
  1157 + bool pipeStreamData(
  1158 + Pipeline*,
  1159 + bool* filtering_attempted,
  1160 + int encode_flags,
  1161 + qpdf_stream_decode_level_e decode_level,
  1162 + bool suppress_warnings = false,
  1163 + bool will_retry = false);
1154 1164  
1155 1165 // Legacy version. Return value is whether filtering was
1156 1166 // attempted. There is no way to determine success if filtering
1157 1167 // was not attempted.
1158 1168 QPDF_DLL
1159   - bool pipeStreamData(Pipeline*,
1160   - int encode_flags,
1161   - qpdf_stream_decode_level_e decode_level,
1162   - bool suppress_warnings = false,
1163   - bool will_retry = false);
  1169 + bool pipeStreamData(
  1170 + Pipeline*,
  1171 + int encode_flags,
  1172 + qpdf_stream_decode_level_e decode_level,
  1173 + bool suppress_warnings = false,
  1174 + bool will_retry = false);
1164 1175  
1165 1176 // Legacy pipeStreamData. This maps to the the flags-based
1166 1177 // pipeStreamData as follows:
... ... @@ -1170,8 +1181,7 @@ class QPDFObjectHandle
1170 1181 // compress = true -> encode_flags |= qpdf_sf_compress
1171 1182 // Return value is whether filtering was attempted.
1172 1183 QPDF_DLL
1173   - bool pipeStreamData(Pipeline*, bool filter,
1174   - bool normalize, bool compress);
  1184 + bool pipeStreamData(Pipeline*, bool filter, bool normalize, bool compress);
1175 1185  
1176 1186 // Replace a stream's dictionary. The new dictionary must be
1177 1187 // consistent with the stream's data. This is most appropriately
... ... @@ -1190,18 +1200,20 @@ class QPDFObjectHandle
1190 1200 // interpreted as if the data read from the file, after any
1191 1201 // decryption filters have been applied, is as presented.
1192 1202 QPDF_DLL
1193   - void replaceStreamData(PointerHolder<Buffer> data,
1194   - QPDFObjectHandle const& filter,
1195   - QPDFObjectHandle const& decode_parms);
  1203 + void replaceStreamData(
  1204 + PointerHolder<Buffer> data,
  1205 + QPDFObjectHandle const& filter,
  1206 + QPDFObjectHandle const& decode_parms);
1196 1207  
1197 1208 // Replace the stream's stream data with the given string.
1198 1209 // This method will create a copy of the data rather than using
1199 1210 // the user-provided buffer as in the PointerHolder<Buffer> version
1200 1211 // of replaceStreamData.
1201 1212 QPDF_DLL
1202   - void replaceStreamData(std::string const& data,
1203   - QPDFObjectHandle const& filter,
1204   - QPDFObjectHandle const& decode_parms);
  1213 + void replaceStreamData(
  1214 + std::string const& data,
  1215 + QPDFObjectHandle const& filter,
  1216 + QPDFObjectHandle const& decode_parms);
1205 1217  
1206 1218 // As above, replace this stream's stream data. Instead of
1207 1219 // directly providing a buffer with the stream data, call the
... ... @@ -1229,9 +1241,10 @@ class QPDFObjectHandle
1229 1241 // parameter. You can also simplify your code by not having to
1230 1242 // compute the length in advance.
1231 1243 QPDF_DLL
1232   - void replaceStreamData(PointerHolder<StreamDataProvider> provider,
1233   - QPDFObjectHandle const& filter,
1234   - QPDFObjectHandle const& decode_parms);
  1244 + void replaceStreamData(
  1245 + PointerHolder<StreamDataProvider> provider,
  1246 + QPDFObjectHandle const& filter,
  1247 + QPDFObjectHandle const& decode_parms);
1235 1248  
1236 1249 // Starting in qpdf 10.2, you can use C++-11 function objects
1237 1250 // instead of StreamDataProvider.
... ... @@ -1240,16 +1253,16 @@ class QPDFObjectHandle
1240 1253 // a one-liner to replace stream data with the contents of a file,
1241 1254 // pass QUtil::file_provider(filename) as provider.
1242 1255 QPDF_DLL
1243   - void replaceStreamData(std::function<void(Pipeline*)> provider,
1244   - QPDFObjectHandle const& filter,
1245   - QPDFObjectHandle const& decode_parms);
  1256 + void replaceStreamData(
  1257 + std::function<void(Pipeline*)> provider,
  1258 + QPDFObjectHandle const& filter,
  1259 + QPDFObjectHandle const& decode_parms);
1246 1260 // The provider should write the stream data to the pipeline,
1247 1261 // returning true if it succeeded without errors.
1248 1262 QPDF_DLL
1249 1263 void replaceStreamData(
1250   - std::function<bool(Pipeline*,
1251   - bool suppress_warnings,
1252   - bool will_retry)> provider,
  1264 + std::function<bool(Pipeline*, bool suppress_warnings, bool will_retry)>
  1265 + provider,
1253 1266 QPDFObjectHandle const& filter,
1254 1267 QPDFObjectHandle const& decode_parms);
1255 1268  
... ... @@ -1321,8 +1334,8 @@ class QPDFObjectHandle
1321 1334 // descriptions. See comments on setObjectDescription for
1322 1335 // additional details.
1323 1336 QPDF_DLL
1324   - void warnIfPossible(std::string const& warning,
1325   - bool throw_if_no_description = false);
  1337 + void warnIfPossible(
  1338 + std::string const& warning, bool throw_if_no_description = false);
1326 1339  
1327 1340 // Initializers for objects. This Factory class gives the QPDF
1328 1341 // class specific permission to call factory methods without
... ... @@ -1330,16 +1343,22 @@ class QPDFObjectHandle
1330 1343 class Factory
1331 1344 {
1332 1345 friend class QPDF;
  1346 +
1333 1347 private:
1334   - static QPDFObjectHandle newIndirect(QPDF* qpdf,
1335   - int objid, int generation)
  1348 + static QPDFObjectHandle
  1349 + newIndirect(QPDF* qpdf, int objid, int generation)
1336 1350 {
1337 1351 return QPDFObjectHandle::newIndirect(qpdf, objid, generation);
1338 1352 }
1339 1353 // object must be dictionary object
1340   - static QPDFObjectHandle newStream(
1341   - QPDF* qpdf, int objid, int generation,
1342   - QPDFObjectHandle stream_dict, qpdf_offset_t offset, size_t length)
  1354 + static QPDFObjectHandle
  1355 + newStream(
  1356 + QPDF* qpdf,
  1357 + int objid,
  1358 + int generation,
  1359 + QPDFObjectHandle stream_dict,
  1360 + qpdf_offset_t offset,
  1361 + size_t length)
1343 1362 {
1344 1363 return QPDFObjectHandle::newStream(
1345 1364 qpdf, objid, generation, stream_dict, offset, length);
... ... @@ -1352,8 +1371,10 @@ class QPDFObjectHandle
1352 1371 class ObjAccessor
1353 1372 {
1354 1373 friend class QPDF;
  1374 +
1355 1375 private:
1356   - static PointerHolder<QPDFObject> getObject(QPDFObjectHandle& o)
  1376 + static PointerHolder<QPDFObject>
  1377 + getObject(QPDFObjectHandle& o)
1357 1378 {
1358 1379 o.dereference();
1359 1380 return o.obj;
... ... @@ -1368,8 +1389,10 @@ class QPDFObjectHandle
1368 1389 friend class QPDF_Dictionary;
1369 1390 friend class QPDF_Stream;
1370 1391 friend class SparseOHArray;
  1392 +
1371 1393 private:
1372   - static void releaseResolved(QPDFObjectHandle& o)
  1394 + static void
  1395 + releaseResolved(QPDFObjectHandle& o)
1373 1396 {
1374 1397 o.releaseResolved();
1375 1398 }
... ... @@ -1433,14 +1456,13 @@ class QPDFObjectHandle
1433 1456 // Indicate if this is an image. If exclude_imagemask is true,
1434 1457 // don't count image masks as images.
1435 1458 QPDF_DLL
1436   - bool isImage(bool exclude_imagemask=true);
  1459 + bool isImage(bool exclude_imagemask = true);
1437 1460  
1438 1461 private:
1439 1462 QPDFObjectHandle(QPDF*, int objid, int generation);
1440 1463 QPDFObjectHandle(QPDFObject*);
1441 1464  
1442   - enum parser_state_e
1443   - {
  1465 + enum parser_state_e {
1444 1466 st_top,
1445 1467 st_start,
1446 1468 st_stop,
... ... @@ -1452,31 +1474,41 @@ class QPDFObjectHandle
1452 1474 // Private object factory methods
1453 1475 static QPDFObjectHandle newIndirect(QPDF*, int objid, int generation);
1454 1476 static QPDFObjectHandle newStream(
1455   - QPDF* qpdf, int objid, int generation,
1456   - QPDFObjectHandle stream_dict, qpdf_offset_t offset, size_t length);
1457   -
1458   - void typeWarning(char const* expected_type,
1459   - std::string const& warning);
  1477 + QPDF* qpdf,
  1478 + int objid,
  1479 + int generation,
  1480 + QPDFObjectHandle stream_dict,
  1481 + qpdf_offset_t offset,
  1482 + size_t length);
  1483 +
  1484 + void typeWarning(char const* expected_type, std::string const& warning);
1460 1485 void objectWarning(std::string const& warning);
1461 1486 void assertType(char const* type_name, bool istype);
1462 1487 void dereference();
1463   - void copyObject(std::set<QPDFObjGen>& visited, bool cross_indirect,
1464   - bool first_level_only, bool stop_at_streams);
  1488 + void copyObject(
  1489 + std::set<QPDFObjGen>& visited,
  1490 + bool cross_indirect,
  1491 + bool first_level_only,
  1492 + bool stop_at_streams);
1465 1493 void shallowCopyInternal(QPDFObjectHandle& oh, bool first_level_only);
1466 1494 void releaseResolved();
1467 1495 static void setObjectDescriptionFromInput(
1468   - QPDFObjectHandle, QPDF*, std::string const&,
1469   - PointerHolder<InputSource>, qpdf_offset_t);
  1496 + QPDFObjectHandle,
  1497 + QPDF*,
  1498 + std::string const&,
  1499 + PointerHolder<InputSource>,
  1500 + qpdf_offset_t);
1470 1501 static QPDFObjectHandle parseInternal(
1471 1502 PointerHolder<InputSource> input,
1472 1503 std::string const& object_description,
1473   - QPDFTokenizer& tokenizer, bool& empty,
1474   - StringDecrypter* decrypter, QPDF* context,
  1504 + QPDFTokenizer& tokenizer,
  1505 + bool& empty,
  1506 + StringDecrypter* decrypter,
  1507 + QPDF* context,
1475 1508 bool content_stream);
1476 1509 void setParsedOffset(qpdf_offset_t offset);
1477 1510 void parseContentStream_internal(
1478   - std::string const& description,
1479   - ParserCallbacks* callbacks);
  1511 + std::string const& description, ParserCallbacks* callbacks);
1480 1512 static void parseContentStream_data(
1481 1513 PointerHolder<Buffer>,
1482 1514 std::string const& description,
... ... @@ -1493,7 +1525,7 @@ class QPDFObjectHandle
1493 1525 // a substantial performance penalty since QPDFObjectHandle
1494 1526 // objects are copied around so frequently.
1495 1527 QPDF* qpdf;
1496   - int objid; // 0 for direct object
  1528 + int objid; // 0 for direct object
1497 1529 int generation;
1498 1530 PointerHolder<QPDFObject> obj;
1499 1531 bool reserved;
... ... @@ -1507,7 +1539,7 @@ class QPDFObjectHandle
1507 1539 // If this is causing problems in your code, define
1508 1540 // QPDF_NO_QPDF_STRING to prevent the declaration from being here.
1509 1541 QPDF_DLL
1510   -QPDFObjectHandle operator ""_qpdf(char const* v, size_t len);
  1542 +QPDFObjectHandle operator""_qpdf(char const* v, size_t len);
1511 1543 #endif // QPDF_NO_QPDF_STRING
1512 1544  
1513 1545 class QPDFObjectHandle::QPDFDictItems
... ... @@ -1531,6 +1563,7 @@ class QPDFObjectHandle::QPDFDictItems
1531 1563 class iterator
1532 1564 {
1533 1565 friend class QPDFDictItems;
  1566 +
1534 1567 public:
1535 1568 typedef std::pair<std::string, QPDFObjectHandle> T;
1536 1569 using iterator_category = std::bidirectional_iterator_tag;
... ... @@ -1544,7 +1577,8 @@ class QPDFObjectHandle::QPDFDictItems
1544 1577 QPDF_DLL
1545 1578 iterator& operator++();
1546 1579 QPDF_DLL
1547   - iterator operator++(int)
  1580 + iterator
  1581 + operator++(int)
1548 1582 {
1549 1583 iterator t = *this;
1550 1584 ++(*this);
... ... @@ -1553,7 +1587,8 @@ class QPDFObjectHandle::QPDFDictItems
1553 1587 QPDF_DLL
1554 1588 iterator& operator--();
1555 1589 QPDF_DLL
1556   - iterator operator--(int)
  1590 + iterator
  1591 + operator--(int)
1557 1592 {
1558 1593 iterator t = *this;
1559 1594 --(*this);
... ... @@ -1566,9 +1601,10 @@ class QPDFObjectHandle::QPDFDictItems
1566 1601 QPDF_DLL
1567 1602 bool operator==(iterator const& other) const;
1568 1603 QPDF_DLL
1569   - bool operator!=(iterator const& other) const
  1604 + bool
  1605 + operator!=(iterator const& other) const
1570 1606 {
1571   - return ! operator==(other);
  1607 + return !operator==(other);
1572 1608 }
1573 1609  
1574 1610 private:
... ... @@ -1626,6 +1662,7 @@ class QPDFObjectHandle::QPDFArrayItems
1626 1662 class iterator
1627 1663 {
1628 1664 friend class QPDFArrayItems;
  1665 +
1629 1666 public:
1630 1667 typedef QPDFObjectHandle T;
1631 1668 using iterator_category = std::bidirectional_iterator_tag;
... ... @@ -1639,7 +1676,8 @@ class QPDFObjectHandle::QPDFArrayItems
1639 1676 QPDF_DLL
1640 1677 iterator& operator++();
1641 1678 QPDF_DLL
1642   - iterator operator++(int)
  1679 + iterator
  1680 + operator++(int)
1643 1681 {
1644 1682 iterator t = *this;
1645 1683 ++(*this);
... ... @@ -1648,7 +1686,8 @@ class QPDFObjectHandle::QPDFArrayItems
1648 1686 QPDF_DLL
1649 1687 iterator& operator--();
1650 1688 QPDF_DLL
1651   - iterator operator--(int)
  1689 + iterator
  1690 + operator--(int)
1652 1691 {
1653 1692 iterator t = *this;
1654 1693 --(*this);
... ... @@ -1661,9 +1700,10 @@ class QPDFObjectHandle::QPDFArrayItems
1661 1700 QPDF_DLL
1662 1701 bool operator==(iterator const& other) const;
1663 1702 QPDF_DLL
1664   - bool operator!=(iterator const& other) const
  1703 + bool
  1704 + operator!=(iterator const& other) const
1665 1705 {
1666   - return ! operator==(other);
  1706 + return !operator==(other);
1667 1707 }
1668 1708  
1669 1709 private:
... ... @@ -1700,5 +1740,4 @@ class QPDFObjectHandle::QPDFArrayItems
1700 1740 QPDFObjectHandle oh;
1701 1741 };
1702 1742  
1703   -
1704 1743 #endif // QPDFOBJECTHANDLE_HH
... ...
include/qpdf/QPDFObjectHelper.hh
... ... @@ -50,12 +50,14 @@ class QPDFObjectHelper
50 50 {
51 51 }
52 52 QPDF_DLL
53   - QPDFObjectHandle getObjectHandle()
  53 + QPDFObjectHandle
  54 + getObjectHandle()
54 55 {
55 56 return this->oh;
56 57 }
57 58 QPDF_DLL
58   - QPDFObjectHandle const getObjectHandle() const
  59 + QPDFObjectHandle const
  60 + getObjectHandle() const
59 61 {
60 62 return this->oh;
61 63 }
... ...
include/qpdf/QPDFOutlineDocumentHelper.hh
... ... @@ -23,8 +23,8 @@
23 23 #define QPDFOUTLINEDOCUMENTHELPER_HH
24 24  
25 25 #include <qpdf/QPDFDocumentHelper.hh>
26   -#include <qpdf/QPDFOutlineObjectHelper.hh>
27 26 #include <qpdf/QPDFNameTreeObjectHelper.hh>
  27 +#include <qpdf/QPDFOutlineObjectHelper.hh>
28 28  
29 29 #include <qpdf/QPDF.hh>
30 30 #include <map>
... ... @@ -58,8 +58,7 @@ class QPDFOutlineDocumentHelper: public QPDFDocumentHelper
58 58 // the name tree pointed to by the /Dests key of the names
59 59 // dictionary.
60 60 QPDF_DLL
61   - QPDFObjectHandle
62   - resolveNamedDest(QPDFObjectHandle name);
  61 + QPDFObjectHandle resolveNamedDest(QPDFObjectHandle name);
63 62  
64 63 // Return a list outlines that are known to target the specified
65 64 // page
... ... @@ -99,7 +98,7 @@ class QPDFOutlineDocumentHelper: public QPDFDocumentHelper
99 98 std::set<QPDFObjGen> seen;
100 99 QPDFObjectHandle dest_dict;
101 100 PointerHolder<QPDFNameTreeObjectHelper> names_dest;
102   - std::map<QPDFObjGen, std::vector<QPDFOutlineObjectHelper> > by_page;
  101 + std::map<QPDFObjGen, std::vector<QPDFOutlineObjectHelper>> by_page;
103 102 };
104 103  
105 104 PointerHolder<Members> m;
... ...
include/qpdf/QPDFOutlineObjectHelper.hh
... ... @@ -22,8 +22,8 @@
22 22 #ifndef QPDFOUTLINEOBJECTHELPER_HH
23 23 #define QPDFOUTLINEOBJECTHELPER_HH
24 24  
25   -#include <qpdf/QPDFObjectHelper.hh>
26 25 #include <qpdf/QPDFObjGen.hh>
  26 +#include <qpdf/QPDFObjectHelper.hh>
27 27 #include <vector>
28 28  
29 29 class QPDFOutlineDocumentHelper;
... ...
include/qpdf/QPDFPageDocumentHelper.hh
... ... @@ -22,9 +22,9 @@
22 22 #ifndef QPDFPAGEDOCUMENTHELPER_HH
23 23 #define QPDFPAGEDOCUMENTHELPER_HH
24 24  
  25 +#include <qpdf/Constants.h>
25 26 #include <qpdf/QPDFDocumentHelper.hh>
26 27 #include <qpdf/QPDFPageObjectHelper.hh>
27   -#include <qpdf/Constants.h>
28 28  
29 29 #include <qpdf/DLL.h>
30 30  
... ... @@ -106,8 +106,10 @@ class QPDFPageDocumentHelper: public QPDFDocumentHelper
106 106 // Add new page before or after refpage. See comments for addPage
107 107 // for details about what newpage should be.
108 108 QPDF_DLL
109   - void addPageAt(QPDFPageObjectHelper newpage, bool before,
110   - QPDFPageObjectHelper refpage);
  109 + void addPageAt(
  110 + QPDFPageObjectHelper newpage,
  111 + bool before,
  112 + QPDFPageObjectHelper refpage);
111 113  
112 114 // Remove page from the pdf.
113 115 QPDF_DLL
... ... @@ -126,8 +128,7 @@ class QPDFPageDocumentHelper: public QPDFDocumentHelper
126 128 // those flags.
127 129 QPDF_DLL
128 130 void flattenAnnotations(
129   - int required_flags = 0,
130   - int forbidden_flags = an_invisible | an_hidden);
  131 + int required_flags = 0, int forbidden_flags = an_invisible | an_hidden);
131 132  
132 133 private:
133 134 void flattenAnnotationsForPage(
... ...
include/qpdf/QPDFPageLabelDocumentHelper.hh
... ... @@ -77,10 +77,11 @@ class QPDFPageLabelDocumentHelper: public QPDFDocumentHelper
77 77 // behavior facilitates using this function to incrementally build
78 78 // up a page labels tree when merging files.
79 79 QPDF_DLL
80   - void
81   - getLabelsForPageRange(long long start_idx, long long end_idx,
82   - long long new_start_idx,
83   - std::vector<QPDFObjectHandle>& new_labels);
  80 + void getLabelsForPageRange(
  81 + long long start_idx,
  82 + long long end_idx,
  83 + long long new_start_idx,
  84 + std::vector<QPDFObjectHandle>& new_labels);
84 85  
85 86 private:
86 87 class Members
... ...
include/qpdf/QPDFPageObjectHelper.hh
... ... @@ -22,9 +22,9 @@
22 22 #ifndef QPDFPAGEOBJECTHELPER_HH
23 23 #define QPDFPAGEOBJECTHELPER_HH
24 24  
25   -#include <qpdf/QPDFObjectHelper.hh>
26 25 #include <qpdf/QPDFAnnotationObjectHelper.hh>
27 26 #include <qpdf/QPDFMatrix.hh>
  27 +#include <qpdf/QPDFObjectHelper.hh>
28 28  
29 29 #include <qpdf/DLL.h>
30 30  
... ... @@ -57,23 +57,19 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
57 57 // going to modify the returned object and want the modifications
58 58 // to apply to the current page/form XObject only.
59 59 QPDF_DLL
60   - QPDFObjectHandle
61   - getAttribute(std::string const& name, bool copy_if_shared);
  60 + QPDFObjectHandle getAttribute(std::string const& name, bool copy_if_shared);
62 61  
63 62 // Return the TrimBox. If not defined, fall back to CropBox
64 63 QPDF_DLL
65   - QPDFObjectHandle
66   - getTrimBox(bool copy_if_shared = false);
  64 + QPDFObjectHandle getTrimBox(bool copy_if_shared = false);
67 65  
68 66 // Return the CropBox. If not defined, fall back to MediaBox
69 67 QPDF_DLL
70   - QPDFObjectHandle
71   - getCropBox(bool copy_if_shared = false);
  68 + QPDFObjectHandle getCropBox(bool copy_if_shared = false);
72 69  
73 70 // Return the MediaBox
74 71 QPDF_DLL
75   - QPDFObjectHandle
76   - getMediaBox(bool copy_if_shared = false);
  72 + QPDFObjectHandle getMediaBox(bool copy_if_shared = false);
77 73  
78 74 // Iterate through XObjects, possibly recursing into form
79 75 // XObjects. This works with pages or form XObjects. Call action
... ... @@ -86,24 +82,27 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
86 82 QPDF_DLL
87 83 void forEachXObject(
88 84 bool recursive,
89   - std::function<void(QPDFObjectHandle& obj,
90   - QPDFObjectHandle& xobj_dict,
91   - std::string const& key)> action,
92   - std::function<bool(QPDFObjectHandle)> selector=nullptr);
  85 + std::function<void(
  86 + QPDFObjectHandle& obj,
  87 + QPDFObjectHandle& xobj_dict,
  88 + std::string const& key)> action,
  89 + std::function<bool(QPDFObjectHandle)> selector = nullptr);
93 90 // Only call action for images
94 91 QPDF_DLL
95 92 void forEachImage(
96 93 bool recursive,
97   - std::function<void(QPDFObjectHandle& obj,
98   - QPDFObjectHandle& xobj_dict,
99   - std::string const& key)> action);
  94 + std::function<void(
  95 + QPDFObjectHandle& obj,
  96 + QPDFObjectHandle& xobj_dict,
  97 + std::string const& key)> action);
100 98 // Only call action for form XObjects
101 99 QPDF_DLL
102 100 void forEachFormXObject(
103 101 bool recursive,
104   - std::function<void(QPDFObjectHandle& obj,
105   - QPDFObjectHandle& xobj_dict,
106   - std::string const& key)> action);
  102 + std::function<void(
  103 + QPDFObjectHandle& obj,
  104 + QPDFObjectHandle& xobj_dict,
  105 + std::string const& key)> action);
107 106  
108 107 // Returns an empty map if there are no images or no resources.
109 108 // Prior to qpdf 8.4.0, this function did not support inherited
... ... @@ -142,8 +141,8 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
142 141 // only_subtype is non-empty, only include annotations of the
143 142 // given subtype.
144 143 QPDF_DLL
145   - std::vector<QPDFAnnotationObjectHelper> getAnnotations(
146   - std::string const& only_subtype = "");
  144 + std::vector<QPDFAnnotationObjectHelper>
  145 + getAnnotations(std::string const& only_subtype = "");
147 146  
148 147 // Returns a vector of stream objects representing the content
149 148 // streams for the given page. This routine allows the caller to
... ... @@ -203,13 +202,13 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
203 202 // contents, as happens with addContentTokenFilter. See
204 203 // examples/pdf-count-strings.cc for an example.
205 204 QPDF_DLL
206   - void filterContents(QPDFObjectHandle::TokenFilter* filter,
207   - Pipeline* next = 0);
  205 + void
  206 + filterContents(QPDFObjectHandle::TokenFilter* filter, Pipeline* next = 0);
208 207  
209 208 // Old name -- calls filterContents()
210 209 QPDF_DLL
211   - void filterPageContents(QPDFObjectHandle::TokenFilter* filter,
212   - Pipeline* next = 0);
  210 + void filterPageContents(
  211 + QPDFObjectHandle::TokenFilter* filter, Pipeline* next = 0);
213 212  
214 213 // Pipe a page's contents through the given pipeline. This method
215 214 // works whether the contents are a single stream or an array of
... ... @@ -303,7 +302,8 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
303 302 // behavior.
304 303 QPDF_DLL
305 304 std::string placeFormXObject(
306   - QPDFObjectHandle fo, std::string const& name,
  305 + QPDFObjectHandle fo,
  306 + std::string const& name,
307 307 QPDFObjectHandle::Rectangle rect,
308 308 bool invert_transformations = true,
309 309 bool allow_shrink = true,
... ... @@ -313,7 +313,8 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
313 313 // matrix that was used.
314 314 QPDF_DLL
315 315 std::string placeFormXObject(
316   - QPDFObjectHandle fo, std::string const& name,
  316 + QPDFObjectHandle fo,
  317 + std::string const& name,
317 318 QPDFObjectHandle::Rectangle rect,
318 319 QPDFMatrix& cm,
319 320 bool invert_transformations = true,
... ... @@ -326,9 +327,11 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
326 327 // placeFormXObject.
327 328 QPDF_DLL
328 329 QPDFMatrix getMatrixForFormXObjectPlacement(
329   - QPDFObjectHandle fo, QPDFObjectHandle::Rectangle rect,
  330 + QPDFObjectHandle fo,
  331 + QPDFObjectHandle::Rectangle rect,
330 332 bool invert_transformations = true,
331   - bool allow_shrink = true, bool allow_expand = false);
  333 + bool allow_shrink = true,
  334 + bool allow_expand = false);
332 335  
333 336 // If a page is rotated using /Rotate in the page's dictionary,
334 337 // instead rotate the page by the same amount by altering the
... ... @@ -372,13 +375,13 @@ class QPDFPageObjectHelper: public QPDFObjectHelper
372 375 // these outside and pass them in.
373 376 QPDF_DLL
374 377 void copyAnnotations(
375   - QPDFPageObjectHelper from_page, QPDFMatrix const& cm = QPDFMatrix(),
  378 + QPDFPageObjectHelper from_page,
  379 + QPDFMatrix const& cm = QPDFMatrix(),
376 380 QPDFAcroFormDocumentHelper* afdh = nullptr,
377 381 QPDFAcroFormDocumentHelper* from_afdh = nullptr);
378 382  
379 383 private:
380   - static bool
381   - removeUnreferencedResourcesHelper(
  384 + static bool removeUnreferencedResourcesHelper(
382 385 QPDFPageObjectHelper ph, std::set<std::string>& unresolved);
383 386  
384 387 class Members
... ...
include/qpdf/QPDFStreamFilter.hh
... ... @@ -23,8 +23,8 @@
23 23 #define QPDFSTREAMFILTER_HH
24 24  
25 25 #include <qpdf/DLL.h>
26   -#include <qpdf/QPDFObjectHandle.hh>
27 26 #include <qpdf/Pipeline.hh>
  27 +#include <qpdf/QPDFObjectHandle.hh>
28 28  
29 29 class QPDF_DLL_CLASS QPDFStreamFilter
30 30 {
... ...
include/qpdf/QPDFSystemError.hh
... ... @@ -22,19 +22,18 @@
22 22 #ifndef QPDFSYSTEMERROR_HH
23 23 #define QPDFSYSTEMERROR_HH
24 24  
  25 +#include <qpdf/Constants.h>
25 26 #include <qpdf/DLL.h>
26 27 #include <qpdf/Types.h>
27   -#include <qpdf/Constants.h>
28 28  
29   -#include <string>
30 29 #include <stdexcept>
  30 +#include <string>
31 31  
32 32 class QPDF_DLL_CLASS QPDFSystemError: public std::runtime_error
33 33 {
34 34 public:
35 35 QPDF_DLL
36   - QPDFSystemError(std::string const& description,
37   - int system_errno);
  36 + QPDFSystemError(std::string const& description, int system_errno);
38 37 QPDF_DLL
39 38 virtual ~QPDFSystemError() noexcept;
40 39  
... ... @@ -48,8 +47,8 @@ class QPDF_DLL_CLASS QPDFSystemError: public std::runtime_error
48 47 int getErrno() const;
49 48  
50 49 private:
51   - static std::string createWhat(std::string const& description,
52   - int system_errno);
  50 + static std::string
  51 + createWhat(std::string const& description, int system_errno);
53 52  
54 53 // This class does not use the Members pattern to avoid needless
55 54 // memory allocations during exception handling.
... ...
include/qpdf/QPDFTokenizer.hh
... ... @@ -27,9 +27,9 @@
27 27 #include <qpdf/InputSource.hh>
28 28 #include <qpdf/PointerHolder.hh>
29 29  
30   -#include <string>
31   -#include <stdio.h>
32 30 #include <memory>
  31 +#include <stdio.h>
  32 +#include <string>
33 33  
34 34 class QPDFTokenizer
35 35 {
... ... @@ -38,8 +38,7 @@ class QPDFTokenizer
38 38 // the tokenizer. tt_eof was introduced in QPDF version 4.1.
39 39 // tt_space, tt_comment, and tt_inline_image were added in QPDF
40 40 // version 8.
41   - enum token_type_e
42   - {
  41 + enum token_type_e {
43 42 tt_bad,
44 43 tt_array_close,
45 44 tt_array_open,
... ... @@ -63,39 +62,50 @@ class QPDFTokenizer
63 62 class Token
64 63 {
65 64 public:
66   - Token() : type(tt_bad) {}
  65 + Token() :
  66 + type(tt_bad)
  67 + {
  68 + }
67 69 QPDF_DLL
68 70 Token(token_type_e type, std::string const& value);
69   - Token(token_type_e type, std::string const& value,
70   - std::string raw_value, std::string error_message) :
  71 + Token(
  72 + token_type_e type,
  73 + std::string const& value,
  74 + std::string raw_value,
  75 + std::string error_message) :
71 76 type(type),
72 77 value(value),
73 78 raw_value(raw_value),
74 79 error_message(error_message)
75 80 {
76 81 }
77   - token_type_e getType() const
  82 + token_type_e
  83 + getType() const
78 84 {
79 85 return this->type;
80 86 }
81   - std::string const& getValue() const
  87 + std::string const&
  88 + getValue() const
82 89 {
83 90 return this->value;
84 91 }
85   - std::string const& getRawValue() const
  92 + std::string const&
  93 + getRawValue() const
86 94 {
87 95 return this->raw_value;
88 96 }
89   - std::string const& getErrorMessage() const
  97 + std::string const&
  98 + getErrorMessage() const
90 99 {
91 100 return this->error_message;
92 101 }
93   - bool operator==(Token const& rhs) const
  102 + bool
  103 + operator==(Token const& rhs) const
94 104 {
95 105 // Ignore fields other than type and value
96   - return ((this->type != tt_bad) &&
97   - (this->type == rhs.type) &&
98   - (this->value == rhs.value));
  106 + return (
  107 + (this->type != tt_bad) && (this->type == rhs.type) &&
  108 + (this->value == rhs.value));
99 109 }
100 110  
101 111 private:
... ... @@ -162,10 +172,11 @@ class QPDFTokenizer
162 172 // offset" as returned by input->getLastOffset() points to the
163 173 // beginning of the token.
164 174 QPDF_DLL
165   - Token readToken(PointerHolder<InputSource> input,
166   - std::string const& context,
167   - bool allow_bad = false,
168   - size_t max_len = 0);
  175 + Token readToken(
  176 + PointerHolder<InputSource> input,
  177 + std::string const& context,
  178 + bool allow_bad = false,
  179 + size_t max_len = 0);
169 180  
170 181 // Calling this method puts the tokenizer in a state for reading
171 182 // inline images. You should call this method after reading the
... ... @@ -188,8 +199,16 @@ class QPDFTokenizer
188 199 void findEI(PointerHolder<InputSource> input);
189 200  
190 201 enum state_e {
191   - st_top, st_in_space, st_in_comment, st_in_string, st_lt, st_gt,
192   - st_literal, st_in_hexstring, st_inline_image, st_token_ready
  202 + st_top,
  203 + st_in_space,
  204 + st_in_comment,
  205 + st_in_string,
  206 + st_lt,
  207 + st_gt,
  208 + st_literal,
  209 + st_in_hexstring,
  210 + st_inline_image,
  211 + st_token_ready
193 212 };
194 213  
195 214 class Members
... ...
include/qpdf/QPDFUsage.hh
... ... @@ -24,8 +24,8 @@
24 24  
25 25 #include <qpdf/DLL.h>
26 26  
27   -#include <string>
28 27 #include <stdexcept>
  28 +#include <string>
29 29  
30 30 class QPDF_DLL_CLASS QPDFUsage: public std::runtime_error
31 31 {
... ...
include/qpdf/QPDFWriter.hh
... ... @@ -29,24 +29,24 @@
29 29 #include <qpdf/DLL.h>
30 30 #include <qpdf/Types.h>
31 31  
32   -#include <stdio.h>
33   -#include <string>
34 32 #include <list>
35   -#include <vector>
36   -#include <set>
37 33 #include <map>
38 34 #include <memory>
  35 +#include <set>
  36 +#include <stdio.h>
  37 +#include <string>
  38 +#include <vector>
39 39  
40 40 #include <qpdf/Constants.h>
41 41  
42   -#include <qpdf/QPDFObjectHandle.hh>
43   -#include <qpdf/QPDFObjGen.hh>
44   -#include <qpdf/QPDFXRefEntry.hh>
45   -#include <qpdf/Pl_Buffer.hh>
46   -#include <qpdf/PointerHolder.hh>
47   -#include <qpdf/Pipeline.hh>
48 42 #include <qpdf/Buffer.hh>
49 43 #include <qpdf/PDFVersion.hh>
  44 +#include <qpdf/Pipeline.hh>
  45 +#include <qpdf/Pl_Buffer.hh>
  46 +#include <qpdf/PointerHolder.hh>
  47 +#include <qpdf/QPDFObjGen.hh>
  48 +#include <qpdf/QPDFObjectHandle.hh>
  49 +#include <qpdf/QPDFXRefEntry.hh>
50 50  
51 51 class QPDF;
52 52 class Pl_Count;
... ... @@ -372,72 +372,104 @@ class QPDFWriter
372 372 // it unless you have to.
373 373 QPDF_DLL
374 374 void setR2EncryptionParameters(
375   - char const* user_password, char const* owner_password,
376   - bool allow_print, bool allow_modify,
377   - bool allow_extract, bool allow_annotate);
  375 + char const* user_password,
  376 + char const* owner_password,
  377 + bool allow_print,
  378 + bool allow_modify,
  379 + bool allow_extract,
  380 + bool allow_annotate);
378 381 // R3 uses RC4, which is a weak cryptographic algorithm. Don't use
379 382 // it unless you have to.
380 383 QPDF_DLL
381 384 void setR3EncryptionParameters(
382   - char const* user_password, char const* owner_password,
383   - bool allow_accessibility, bool allow_extract,
384   - bool allow_assemble, bool allow_annotate_and_form,
385   - bool allow_form_filling, bool allow_modify_other,
  385 + char const* user_password,
  386 + char const* owner_password,
  387 + bool allow_accessibility,
  388 + bool allow_extract,
  389 + bool allow_assemble,
  390 + bool allow_annotate_and_form,
  391 + bool allow_form_filling,
  392 + bool allow_modify_other,
386 393 qpdf_r3_print_e print);
387 394 // R4 uses RC4, which is a weak cryptographic algorithm, when
388 395 // use_aes=false. Don't use it unless you have to.
389 396 QPDF_DLL
390 397 void setR4EncryptionParameters(
391   - char const* user_password, char const* owner_password,
392   - bool allow_accessibility, bool allow_extract,
393   - bool allow_assemble, bool allow_annotate_and_form,
394   - bool allow_form_filling, bool allow_modify_other,
395   - qpdf_r3_print_e print, bool encrypt_metadata, bool use_aes);
  398 + char const* user_password,
  399 + char const* owner_password,
  400 + bool allow_accessibility,
  401 + bool allow_extract,
  402 + bool allow_assemble,
  403 + bool allow_annotate_and_form,
  404 + bool allow_form_filling,
  405 + bool allow_modify_other,
  406 + qpdf_r3_print_e print,
  407 + bool encrypt_metadata,
  408 + bool use_aes);
396 409 // R5 is deprecated. Do not use it for production use. Writing
397 410 // R5 is supported by qpdf primarily to generate test files for
398 411 // applications that may need to test R5 support.
399 412 QPDF_DLL
400 413 void setR5EncryptionParameters(
401   - char const* user_password, char const* owner_password,
402   - bool allow_accessibility, bool allow_extract,
403   - bool allow_assemble, bool allow_annotate_and_form,
404   - bool allow_form_filling, bool allow_modify_other,
405   - qpdf_r3_print_e print, bool encrypt_metadata);
  414 + char const* user_password,
  415 + char const* owner_password,
  416 + bool allow_accessibility,
  417 + bool allow_extract,
  418 + bool allow_assemble,
  419 + bool allow_annotate_and_form,
  420 + bool allow_form_filling,
  421 + bool allow_modify_other,
  422 + qpdf_r3_print_e print,
  423 + bool encrypt_metadata);
406 424 QPDF_DLL
407 425 void setR6EncryptionParameters(
408   - char const* user_password, char const* owner_password,
409   - bool allow_accessibility, bool allow_extract,
410   - bool allow_assemble, bool allow_annotate_and_form,
411   - bool allow_form_filling, bool allow_modify_other,
412   - qpdf_r3_print_e print, bool encrypt_metadata_aes);
  426 + char const* user_password,
  427 + char const* owner_password,
  428 + bool allow_accessibility,
  429 + bool allow_extract,
  430 + bool allow_assemble,
  431 + bool allow_annotate_and_form,
  432 + bool allow_form_filling,
  433 + bool allow_modify_other,
  434 + qpdf_r3_print_e print,
  435 + bool encrypt_metadata_aes);
413 436  
414 437 // Pre qpdf 8.4.0 API
415   - [[deprecated("see newer API above")]]
416   - QPDF_DLL
417   - void setR3EncryptionParameters(
418   - char const* user_password, char const* owner_password,
419   - bool allow_accessibility, bool allow_extract,
420   - qpdf_r3_print_e print, qpdf_r3_modify_e modify);
421   - [[deprecated("see newer API above")]]
422   - QPDF_DLL
423   - void setR4EncryptionParameters(
424   - char const* user_password, char const* owner_password,
425   - bool allow_accessibility, bool allow_extract,
426   - qpdf_r3_print_e print, qpdf_r3_modify_e modify,
427   - bool encrypt_metadata, bool use_aes);
428   - [[deprecated("see newer API above")]]
429   - QPDF_DLL
430   - void setR5EncryptionParameters(
431   - char const* user_password, char const* owner_password,
432   - bool allow_accessibility, bool allow_extract,
433   - qpdf_r3_print_e print, qpdf_r3_modify_e modify,
  438 + [[deprecated("see newer API above")]] QPDF_DLL void
  439 + setR3EncryptionParameters(
  440 + char const* user_password,
  441 + char const* owner_password,
  442 + bool allow_accessibility,
  443 + bool allow_extract,
  444 + qpdf_r3_print_e print,
  445 + qpdf_r3_modify_e modify);
  446 + [[deprecated("see newer API above")]] QPDF_DLL void
  447 + setR4EncryptionParameters(
  448 + char const* user_password,
  449 + char const* owner_password,
  450 + bool allow_accessibility,
  451 + bool allow_extract,
  452 + qpdf_r3_print_e print,
  453 + qpdf_r3_modify_e modify,
  454 + bool encrypt_metadata,
  455 + bool use_aes);
  456 + [[deprecated("see newer API above")]] QPDF_DLL void
  457 + setR5EncryptionParameters(
  458 + char const* user_password,
  459 + char const* owner_password,
  460 + bool allow_accessibility,
  461 + bool allow_extract,
  462 + qpdf_r3_print_e print,
  463 + qpdf_r3_modify_e modify,
434 464 bool encrypt_metadata);
435   - [[deprecated("see newer API above")]]
436   - QPDF_DLL
437   - void setR6EncryptionParameters(
438   - char const* user_password, char const* owner_password,
439   - bool allow_accessibility, bool allow_extract,
440   - qpdf_r3_print_e print, qpdf_r3_modify_e modify,
  465 + [[deprecated("see newer API above")]] QPDF_DLL void
  466 + setR6EncryptionParameters(
  467 + char const* user_password,
  468 + char const* owner_password,
  469 + bool allow_accessibility,
  470 + bool allow_extract,
  471 + qpdf_r3_print_e print,
  472 + qpdf_r3_modify_e modify,
441 473 bool encrypt_metadata_aes);
442 474  
443 475 // Create linearized output. Disables qdf mode, content
... ... @@ -496,11 +528,11 @@ class QPDFWriter
496 528  
497 529 private:
498 530 // flags used by unparseObject
499   - static int const f_stream = 1 << 0;
500   - static int const f_filtered = 1 << 1;
501   - static int const f_in_ostream = 1 << 2;
502   - static int const f_hex_string = 1 << 3;
503   - static int const f_no_encryption = 1 << 4;
  531 + static int const f_stream = 1 << 0;
  532 + static int const f_filtered = 1 << 1;
  533 + static int const f_in_ostream = 1 << 2;
  534 + static int const f_hex_string = 1 << 3;
  535 + static int const f_no_encryption = 1 << 4;
504 536  
505 537 enum trailer_e { t_normal, t_lin_first, t_lin_second };
506 538  
... ... @@ -516,9 +548,9 @@ class QPDFWriter
516 548 class PipelinePopper
517 549 {
518 550 friend class QPDFWriter;
  551 +
519 552 public:
520   - PipelinePopper(QPDFWriter* qw,
521   - PointerHolder<Buffer>* bp = 0) :
  553 + PipelinePopper(QPDFWriter* qw, PointerHolder<Buffer>* bp = 0) :
522 554 qw(qw),
523 555 bp(bp)
524 556 {
... ... @@ -545,15 +577,24 @@ class QPDFWriter
545 577 std::vector<qpdf_offset_t>& offsets, int first_obj);
546 578 void writeObjectStream(QPDFObjectHandle object);
547 579 void writeObject(QPDFObjectHandle object, int object_stream_index = -1);
548   - void writeTrailer(trailer_e which, int size,
549   - bool xref_stream, qpdf_offset_t prev,
550   - int linearization_pass);
551   - bool willFilterStream(QPDFObjectHandle stream,
552   - bool& compress_stream, bool& is_metadata,
553   - PointerHolder<Buffer>* stream_data);
554   - void unparseObject(QPDFObjectHandle object, int level, int flags,
555   - // for stream dictionaries
556   - size_t stream_length = 0, bool compress = false);
  580 + void writeTrailer(
  581 + trailer_e which,
  582 + int size,
  583 + bool xref_stream,
  584 + qpdf_offset_t prev,
  585 + int linearization_pass);
  586 + bool willFilterStream(
  587 + QPDFObjectHandle stream,
  588 + bool& compress_stream,
  589 + bool& is_metadata,
  590 + PointerHolder<Buffer>* stream_data);
  591 + void unparseObject(
  592 + QPDFObjectHandle object,
  593 + int level,
  594 + int flags,
  595 + // for stream dictionaries
  596 + size_t stream_length = 0,
  597 + bool compress = false);
557 598 void unparseChild(QPDFObjectHandle child, int level, int flags);
558 599 void initializeSpecialStreams();
559 600 void preserveObjectStreams();
... ... @@ -562,23 +603,39 @@ class QPDFWriter
562 603 void generateID();
563 604 void interpretR3EncryptionParameters(
564 605 std::set<int>& bits_to_clear,
565   - char const* user_password, char const* owner_password,
566   - bool allow_accessibility, bool allow_extract,
567   - bool allow_assemble, bool allow_annotate_and_form,
568   - bool allow_form_filling, bool allow_modify_other,
569   - qpdf_r3_print_e print, qpdf_r3_modify_e modify);
570   - void disableIncompatibleEncryption(int major, int minor,
571   - int extension_level);
  606 + char const* user_password,
  607 + char const* owner_password,
  608 + bool allow_accessibility,
  609 + bool allow_extract,
  610 + bool allow_assemble,
  611 + bool allow_annotate_and_form,
  612 + bool allow_form_filling,
  613 + bool allow_modify_other,
  614 + qpdf_r3_print_e print,
  615 + qpdf_r3_modify_e modify);
  616 + void
  617 + disableIncompatibleEncryption(int major, int minor, int extension_level);
572 618 void parseVersion(std::string const& version, int& major, int& minor) const;
573 619 int compareVersions(int major1, int minor1, int major2, int minor2) const;
574 620 void setEncryptionParameters(
575   - char const* user_password, char const* owner_password,
576   - int V, int R, int key_len, std::set<int>& bits_to_clear);
  621 + char const* user_password,
  622 + char const* owner_password,
  623 + int V,
  624 + int R,
  625 + int key_len,
  626 + std::set<int>& bits_to_clear);
577 627 void setEncryptionParametersInternal(
578   - int V, int R, int key_len, int P,
579   - std::string const& O, std::string const& U,
580   - std::string const& OE, std::string const& UE, std::string const& Perms,
581   - std::string const& id1, std::string const& user_password,
  628 + int V,
  629 + int R,
  630 + int key_len,
  631 + int P,
  632 + std::string const& O,
  633 + std::string const& U,
  634 + std::string const& OE,
  635 + std::string const& UE,
  636 + std::string const& Perms,
  637 + std::string const& id1,
  638 + std::string const& user_password,
582 639 std::string const& encryption_key);
583 640 void setDataKey(int objid);
584 641 int openObject(int objid = 0);
... ... @@ -595,10 +652,13 @@ class QPDFWriter
595 652 void doWriteSetup();
596 653 void writeHeader();
597 654 void writeHintStream(int hint_id);
  655 + qpdf_offset_t
  656 + writeXRefTable(trailer_e which, int first, int last, int size);
598 657 qpdf_offset_t writeXRefTable(
599   - trailer_e which, int first, int last, int size);
600   - qpdf_offset_t writeXRefTable(
601   - trailer_e which, int first, int last, int size,
  658 + trailer_e which,
  659 + int first,
  660 + int last,
  661 + int size,
602 662 // for linearization
603 663 qpdf_offset_t prev,
604 664 bool suppress_offsets,
... ... @@ -607,11 +667,21 @@ class QPDFWriter
607 667 qpdf_offset_t hint_length,
608 668 int linearization_pass);
609 669 qpdf_offset_t writeXRefStream(
610   - int objid, int max_id, qpdf_offset_t max_offset,
611   - trailer_e which, int first, int last, int size);
  670 + int objid,
  671 + int max_id,
  672 + qpdf_offset_t max_offset,
  673 + trailer_e which,
  674 + int first,
  675 + int last,
  676 + int size);
612 677 qpdf_offset_t writeXRefStream(
613   - int objid, int max_id, qpdf_offset_t max_offset,
614   - trailer_e which, int first, int last, int size,
  678 + int objid,
  679 + int max_id,
  680 + qpdf_offset_t max_offset,
  681 + trailer_e which,
  682 + int first,
  683 + int last,
  684 + int size,
615 685 // for linearization
616 686 qpdf_offset_t prev,
617 687 int hint_id,
... ... @@ -627,7 +697,7 @@ class QPDFWriter
627 697 // popped.
628 698 Pipeline* pushPipeline(Pipeline*);
629 699 void activatePipelineStack(PipelinePopper&);
630   - void initializePipelineStack(Pipeline *);
  700 + void initializePipelineStack(Pipeline*);
631 701  
632 702 void adjustAESStreamLength(size_t& length);
633 703 void pushEncryptionFilter(PipelinePopper&);
... ... @@ -635,8 +705,8 @@ class QPDFWriter
635 705 void pushMD5Pipeline(PipelinePopper&);
636 706 void computeDeterministicIDData();
637 707  
638   - void discardGeneration(std::map<QPDFObjGen, int> const& in,
639   - std::map<int, int>& out);
  708 + void discardGeneration(
  709 + std::map<QPDFObjGen, int> const& in, std::map<int, int>& out);
640 710  
641 711 class Members
642 712 {
... ... @@ -681,8 +751,8 @@ class QPDFWriter
681 751 int encryption_V;
682 752 int encryption_R;
683 753  
684   - std::string id1; // for /ID key of
685   - std::string id2; // trailer dictionary
  754 + std::string id1; // for /ID key of
  755 + std::string id2; // trailer dictionary
686 756 std::string final_pdf_version;
687 757 int final_extension_level;
688 758 std::string min_pdf_version;
... ... @@ -707,7 +777,7 @@ class QPDFWriter
707 777 std::map<QPDFObjGen, int> page_object_to_seq;
708 778 std::map<QPDFObjGen, int> contents_to_page_seq;
709 779 std::map<QPDFObjGen, int> object_to_object_stream;
710   - std::map<int, std::set<QPDFObjGen> > object_stream_to_objects;
  780 + std::map<int, std::set<QPDFObjGen>> object_stream_to_objects;
711 781 std::list<Pipeline*> pipeline_stack;
712 782 unsigned long long next_stack_id;
713 783 bool deterministic_id;
... ...
include/qpdf/QPDFXRefEntry.hh
... ... @@ -42,11 +42,11 @@ class QPDFXRefEntry
42 42 QPDF_DLL
43 43 int getType() const;
44 44 QPDF_DLL
45   - qpdf_offset_t getOffset() const; // only for type 1
  45 + qpdf_offset_t getOffset() const; // only for type 1
46 46 QPDF_DLL
47   - int getObjStreamNumber() const; // only for type 2
  47 + int getObjStreamNumber() const; // only for type 2
48 48 QPDF_DLL
49   - int getObjStreamIndex() const; // only for type 2
  49 + int getObjStreamIndex() const; // only for type 2
50 50  
51 51 private:
52 52 // This class does not use the Members pattern to avoid a memory
... ...
include/qpdf/QTC.hh
... ... @@ -28,6 +28,6 @@ namespace QTC
28 28 {
29 29 QPDF_DLL
30 30 void TC(char const* const scope, char const* const ccase, int n = 0);
31   -};
  31 +}; // namespace QTC
32 32  
33 33 #endif // QTC_HH
... ...
include/qpdf/QUtil.hh
... ... @@ -23,16 +23,16 @@
23 23 #define QUTIL_HH
24 24  
25 25 #include <qpdf/DLL.h>
26   -#include <qpdf/Types.h>
27 26 #include <qpdf/PointerHolder.hh>
28   -#include <string>
29   -#include <list>
30   -#include <vector>
31   -#include <stdexcept>
  27 +#include <qpdf/Types.h>
32 28 #include <functional>
  29 +#include <list>
33 30 #include <memory>
  31 +#include <stdexcept>
34 32 #include <stdio.h>
  33 +#include <string>
35 34 #include <time.h>
  35 +#include <vector>
36 36  
37 37 class RandomDataProvider;
38 38 class Pipeline;
... ... @@ -48,16 +48,16 @@ namespace QUtil
48 48 QPDF_DLL
49 49 std::string int_to_string_base(long long, int base, int length = 0);
50 50 QPDF_DLL
51   - std::string uint_to_string_base(unsigned long long, int base,
52   - int length = 0);
  51 + std::string
  52 + uint_to_string_base(unsigned long long, int base, int length = 0);
53 53 QPDF_DLL
54 54 std::string double_to_string(double, int decimal_places = 0);
55 55 // ABI: combine with other double_to_string by adding
56 56 // trim_trailing_zeroes above as an optional parameter with a
57 57 // default of true.
58 58 QPDF_DLL
59   - std::string double_to_string(double, int decimal_places,
60   - bool trim_trailing_zeroes);
  59 + std::string
  60 + double_to_string(double, int decimal_places, bool trim_trailing_zeroes);
61 61  
62 62 // These string to number methods throw std::runtime_error on
63 63 // underflow/overflow.
... ... @@ -217,8 +217,14 @@ namespace QUtil
217 217 QPDFTime() = default;
218 218 QPDFTime(QPDFTime const&) = default;
219 219 QPDFTime& operator=(QPDFTime const&) = default;
220   - QPDFTime(int year, int month, int day, int hour,
221   - int minute, int second, int tz_delta) :
  220 + QPDFTime(
  221 + int year,
  222 + int month,
  223 + int day,
  224 + int hour,
  225 + int minute,
  226 + int second,
  227 + int tz_delta) :
222 228 year(year),
223 229 month(month),
224 230 day(day),
... ... @@ -228,13 +234,13 @@ namespace QUtil
228 234 tz_delta(tz_delta)
229 235 {
230 236 }
231   - int year; // actual year, no 1900 stuff
232   - int month; // 1--12
233   - int day; // 1--31
  237 + int year; // actual year, no 1900 stuff
  238 + int month; // 1--12
  239 + int day; // 1--31
234 240 int hour;
235 241 int minute;
236 242 int second;
237   - int tz_delta; // minutes before UTC
  243 + int tz_delta; // minutes before UTC
238 244 };
239 245  
240 246 QPDF_DLL
... ... @@ -284,17 +290,16 @@ namespace QUtil
284 290 // encoding system by replacing all unsupported characters with
285 291 // the given unknown_char.
286 292 QPDF_DLL
287   - std::string utf8_to_ascii(
288   - std::string const& utf8, char unknown_char = '?');
  293 + std::string utf8_to_ascii(std::string const& utf8, char unknown_char = '?');
289 294 QPDF_DLL
290   - std::string utf8_to_win_ansi(
291   - std::string const& utf8, char unknown_char = '?');
  295 + std::string
  296 + utf8_to_win_ansi(std::string const& utf8, char unknown_char = '?');
292 297 QPDF_DLL
293   - std::string utf8_to_mac_roman(
294   - std::string const& utf8, char unknown_char = '?');
  298 + std::string
  299 + utf8_to_mac_roman(std::string const& utf8, char unknown_char = '?');
295 300 QPDF_DLL
296   - std::string utf8_to_pdf_doc(
297   - std::string const& utf8, char unknown_char = '?');
  301 + std::string
  302 + utf8_to_pdf_doc(std::string const& utf8, char unknown_char = '?');
298 303  
299 304 // These versions return true if the conversion was successful and
300 305 // false if any unrepresentable characters were found and had to
... ... @@ -338,10 +343,11 @@ namespace QUtil
338 343 // the PDF spec requires UTF-16 to be UTF-16BE, qpdf (and just
339 344 // about everything else) accepts UTF-16LE (as of 10.6.2).
340 345 QPDF_DLL
341   - void analyze_encoding(std::string const& str,
342   - bool& has_8bit_chars,
343   - bool& is_valid_utf8,
344   - bool& is_utf16);
  346 + void analyze_encoding(
  347 + std::string const& str,
  348 + bool& has_8bit_chars,
  349 + bool& is_valid_utf8,
  350 + bool& is_utf16);
345 351  
346 352 // Try to compensate for previously incorrectly encoded strings.
347 353 // We want to compensate for the following errors:
... ... @@ -398,14 +404,14 @@ namespace QUtil
398 404 // Filename is UTF-8 encoded, even on Windows, as described in the
399 405 // comments for safe_fopen.
400 406 QPDF_DLL
401   - std::list<std::string> read_lines_from_file(
402   - char const* filename, bool preserve_eol = false);
  407 + std::list<std::string>
  408 + read_lines_from_file(char const* filename, bool preserve_eol = false);
403 409 QPDF_DLL
404   - std::list<std::string> read_lines_from_file(
405   - std::istream&, bool preserve_eol = false);
  410 + std::list<std::string>
  411 + read_lines_from_file(std::istream&, bool preserve_eol = false);
406 412 QPDF_DLL
407   - std::list<std::string> read_lines_from_file(
408   - FILE*, bool preserve_eol = false);
  413 + std::list<std::string>
  414 + read_lines_from_file(FILE*, bool preserve_eol = false);
409 415 QPDF_DLL
410 416 void read_lines_from_file(
411 417 std::function<bool(char&)> next_char,
... ... @@ -420,7 +426,7 @@ namespace QUtil
420 426 // platforms, so we have to give it a name that is not likely to
421 427 // be a macro anywhere.
422 428 QPDF_DLL
423   - int str_compare_nocase(char const *, char const *);
  429 + int str_compare_nocase(char const*, char const*);
424 430  
425 431 // These routines help the tokenizer recognize certain character
426 432 // classes without using ctype, which we avoid because of locale
... ... @@ -454,13 +460,13 @@ namespace QUtil
454 460 // another main.
455 461 QPDF_DLL
456 462 int call_main_from_wmain(
457   - int argc, wchar_t* argv[],
458   - std::function<int(int, char*[])> realmain);
  463 + int argc, wchar_t* argv[], std::function<int(int, char*[])> realmain);
459 464 QPDF_DLL
460 465 int call_main_from_wmain(
461   - int argc, wchar_t const* const argv[],
  466 + int argc,
  467 + wchar_t const* const argv[],
462 468 std::function<int(int, char const* const[])> realmain);
463 469 #endif // QPDF_NO_WCHAR_T
464   -};
  470 +}; // namespace QUtil
465 471  
466 472 #endif // QUTIL_HH
... ...
include/qpdf/qpdf-c.h
... ... @@ -137,9 +137,9 @@
137 137 * https://github.com/qpdf/qpdf/issues/new.
138 138 */
139 139  
  140 +#include <qpdf/Constants.h>
140 141 #include <qpdf/DLL.h>
141 142 #include <qpdf/Types.h>
142   -#include <qpdf/Constants.h>
143 143 #include <string.h>
144 144  
145 145 #ifdef __cplusplus
... ... @@ -154,13 +154,13 @@ extern &quot;C&quot; {
154 154 * that the values below can be logically orred together.
155 155 */
156 156 typedef int QPDF_ERROR_CODE;
157   -# define QPDF_SUCCESS 0
158   -# define QPDF_WARNINGS 1 << 0
159   -# define QPDF_ERRORS 1 << 1
  157 +#define QPDF_SUCCESS 0
  158 +#define QPDF_WARNINGS 1 << 0
  159 +#define QPDF_ERRORS 1 << 1
160 160  
161 161 typedef int QPDF_BOOL;
162   -# define QPDF_TRUE 1
163   -# define QPDF_FALSE 0
  162 +#define QPDF_TRUE 1
  163 +#define QPDF_FALSE 0
164 164  
165 165 /* From qpdf 10.5: call this method to signal to the library that
166 166 * you are explicitly handling errors from functions that don't
... ... @@ -273,8 +273,8 @@ extern &quot;C&quot; {
273 273 * pointer or an empty string as the password.
274 274 */
275 275 QPDF_DLL
276   - QPDF_ERROR_CODE qpdf_read(qpdf_data qpdf, char const* filename,
277   - char const* password);
  276 + QPDF_ERROR_CODE
  277 + qpdf_read(qpdf_data qpdf, char const* filename, char const* password);
278 278  
279 279 /* Calling qpdf_read_memory causes processMemoryFile to be called
280 280 * in the C++ API. Otherwise, it behaves in the same way as
... ... @@ -283,11 +283,12 @@ extern &quot;C&quot; {
283 283 * library.
284 284 */
285 285 QPDF_DLL
286   - QPDF_ERROR_CODE qpdf_read_memory(qpdf_data qpdf,
287   - char const* description,
288   - char const* buffer,
289   - unsigned long long size,
290   - char const* password);
  286 + QPDF_ERROR_CODE qpdf_read_memory(
  287 + qpdf_data qpdf,
  288 + char const* description,
  289 + char const* buffer,
  290 + unsigned long long size,
  291 + char const* password);
291 292  
292 293 /* Calling qpdf_empty_pdf initializes this qpdf object with an
293 294 * empty PDF, making it possible to create a PDF from scratch
... ... @@ -409,24 +410,23 @@ extern &quot;C&quot; {
409 410 unsigned char const* qpdf_get_buffer(qpdf_data qpdf);
410 411  
411 412 QPDF_DLL
412   - void qpdf_set_object_stream_mode(qpdf_data qpdf,
413   - enum qpdf_object_stream_e mode);
  413 + void
  414 + qpdf_set_object_stream_mode(qpdf_data qpdf, enum qpdf_object_stream_e mode);
414 415  
415 416 QPDF_DLL
416   - void qpdf_set_stream_data_mode(qpdf_data qpdf,
417   - enum qpdf_stream_data_e mode);
  417 + void
  418 + qpdf_set_stream_data_mode(qpdf_data qpdf, enum qpdf_stream_data_e mode);
418 419  
419 420 QPDF_DLL
420 421 void qpdf_set_compress_streams(qpdf_data qpdf, QPDF_BOOL value);
421 422  
422   -
423 423 QPDF_DLL
424   - void qpdf_set_decode_level(qpdf_data qpdf,
425   - enum qpdf_stream_decode_level_e level);
  424 + void qpdf_set_decode_level(
  425 + qpdf_data qpdf, enum qpdf_stream_decode_level_e level);
426 426  
427 427 QPDF_DLL
428   - void qpdf_set_preserve_unreferenced_objects(
429   - qpdf_data qpdf, QPDF_BOOL value);
  428 + void
  429 + qpdf_set_preserve_unreferenced_objects(qpdf_data qpdf, QPDF_BOOL value);
430 430  
431 431 QPDF_DLL
432 432 void qpdf_set_newline_before_endstream(qpdf_data qpdf, QPDF_BOOL value);
... ... @@ -453,77 +453,120 @@ extern &quot;C&quot; {
453 453 void qpdf_set_static_aes_IV(qpdf_data qpdf, QPDF_BOOL value);
454 454  
455 455 QPDF_DLL
456   - void qpdf_set_suppress_original_object_IDs(
457   - qpdf_data qpdf, QPDF_BOOL value);
  456 + void qpdf_set_suppress_original_object_IDs(qpdf_data qpdf, QPDF_BOOL value);
458 457  
459 458 QPDF_DLL
460 459 void qpdf_set_preserve_encryption(qpdf_data qpdf, QPDF_BOOL value);
461 460  
462 461 QPDF_DLL
463 462 void qpdf_set_r2_encryption_parameters(
464   - qpdf_data qpdf, char const* user_password, char const* owner_password,
465   - QPDF_BOOL allow_print, QPDF_BOOL allow_modify,
466   - QPDF_BOOL allow_extract, QPDF_BOOL allow_annotate);
  463 + qpdf_data qpdf,
  464 + char const* user_password,
  465 + char const* owner_password,
  466 + QPDF_BOOL allow_print,
  467 + QPDF_BOOL allow_modify,
  468 + QPDF_BOOL allow_extract,
  469 + QPDF_BOOL allow_annotate);
467 470  
468 471 QPDF_DLL
469 472 void qpdf_set_r3_encryption_parameters2(
470   - qpdf_data qpdf, char const* user_password, char const* owner_password,
471   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
472   - QPDF_BOOL allow_assemble, QPDF_BOOL allow_annotate_and_form,
473   - QPDF_BOOL allow_form_filling, QPDF_BOOL allow_modify_other,
  473 + qpdf_data qpdf,
  474 + char const* user_password,
  475 + char const* owner_password,
  476 + QPDF_BOOL allow_accessibility,
  477 + QPDF_BOOL allow_extract,
  478 + QPDF_BOOL allow_assemble,
  479 + QPDF_BOOL allow_annotate_and_form,
  480 + QPDF_BOOL allow_form_filling,
  481 + QPDF_BOOL allow_modify_other,
474 482 enum qpdf_r3_print_e print);
475 483  
476 484 QPDF_DLL
477 485 void qpdf_set_r4_encryption_parameters2(
478   - qpdf_data qpdf, char const* user_password, char const* owner_password,
479   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
480   - QPDF_BOOL allow_assemble, QPDF_BOOL allow_annotate_and_form,
481   - QPDF_BOOL allow_form_filling, QPDF_BOOL allow_modify_other,
  486 + qpdf_data qpdf,
  487 + char const* user_password,
  488 + char const* owner_password,
  489 + QPDF_BOOL allow_accessibility,
  490 + QPDF_BOOL allow_extract,
  491 + QPDF_BOOL allow_assemble,
  492 + QPDF_BOOL allow_annotate_and_form,
  493 + QPDF_BOOL allow_form_filling,
  494 + QPDF_BOOL allow_modify_other,
482 495 enum qpdf_r3_print_e print,
483   - QPDF_BOOL encrypt_metadata, QPDF_BOOL use_aes);
  496 + QPDF_BOOL encrypt_metadata,
  497 + QPDF_BOOL use_aes);
484 498  
485 499 QPDF_DLL
486 500 void qpdf_set_r5_encryption_parameters2(
487   - qpdf_data qpdf, char const* user_password, char const* owner_password,
488   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
489   - QPDF_BOOL allow_assemble, QPDF_BOOL allow_annotate_and_form,
490   - QPDF_BOOL allow_form_filling, QPDF_BOOL allow_modify_other,
491   - enum qpdf_r3_print_e print, QPDF_BOOL encrypt_metadata);
  501 + qpdf_data qpdf,
  502 + char const* user_password,
  503 + char const* owner_password,
  504 + QPDF_BOOL allow_accessibility,
  505 + QPDF_BOOL allow_extract,
  506 + QPDF_BOOL allow_assemble,
  507 + QPDF_BOOL allow_annotate_and_form,
  508 + QPDF_BOOL allow_form_filling,
  509 + QPDF_BOOL allow_modify_other,
  510 + enum qpdf_r3_print_e print,
  511 + QPDF_BOOL encrypt_metadata);
492 512  
493 513 QPDF_DLL
494 514 void qpdf_set_r6_encryption_parameters2(
495   - qpdf_data qpdf, char const* user_password, char const* owner_password,
496   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
497   - QPDF_BOOL allow_assemble, QPDF_BOOL allow_annotate_and_form,
498   - QPDF_BOOL allow_form_filling, QPDF_BOOL allow_modify_other,
499   - enum qpdf_r3_print_e print, QPDF_BOOL encrypt_metadata);
  515 + qpdf_data qpdf,
  516 + char const* user_password,
  517 + char const* owner_password,
  518 + QPDF_BOOL allow_accessibility,
  519 + QPDF_BOOL allow_extract,
  520 + QPDF_BOOL allow_assemble,
  521 + QPDF_BOOL allow_annotate_and_form,
  522 + QPDF_BOOL allow_form_filling,
  523 + QPDF_BOOL allow_modify_other,
  524 + enum qpdf_r3_print_e print,
  525 + QPDF_BOOL encrypt_metadata);
500 526  
501 527 /* Pre 8.4.0 encryption API */
502 528 QPDF_DLL
503 529 void qpdf_set_r3_encryption_parameters(
504   - qpdf_data qpdf, char const* user_password, char const* owner_password,
505   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
506   - enum qpdf_r3_print_e print, enum qpdf_r3_modify_e modify);
  530 + qpdf_data qpdf,
  531 + char const* user_password,
  532 + char const* owner_password,
  533 + QPDF_BOOL allow_accessibility,
  534 + QPDF_BOOL allow_extract,
  535 + enum qpdf_r3_print_e print,
  536 + enum qpdf_r3_modify_e modify);
507 537  
508 538 QPDF_DLL
509 539 void qpdf_set_r4_encryption_parameters(
510   - qpdf_data qpdf, char const* user_password, char const* owner_password,
511   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
512   - enum qpdf_r3_print_e print, enum qpdf_r3_modify_e modify,
513   - QPDF_BOOL encrypt_metadata, QPDF_BOOL use_aes);
  540 + qpdf_data qpdf,
  541 + char const* user_password,
  542 + char const* owner_password,
  543 + QPDF_BOOL allow_accessibility,
  544 + QPDF_BOOL allow_extract,
  545 + enum qpdf_r3_print_e print,
  546 + enum qpdf_r3_modify_e modify,
  547 + QPDF_BOOL encrypt_metadata,
  548 + QPDF_BOOL use_aes);
514 549  
515 550 QPDF_DLL
516 551 void qpdf_set_r5_encryption_parameters(
517   - qpdf_data qpdf, char const* user_password, char const* owner_password,
518   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
519   - enum qpdf_r3_print_e print, enum qpdf_r3_modify_e modify,
  552 + qpdf_data qpdf,
  553 + char const* user_password,
  554 + char const* owner_password,
  555 + QPDF_BOOL allow_accessibility,
  556 + QPDF_BOOL allow_extract,
  557 + enum qpdf_r3_print_e print,
  558 + enum qpdf_r3_modify_e modify,
520 559 QPDF_BOOL encrypt_metadata);
521 560  
522 561 QPDF_DLL
523 562 void qpdf_set_r6_encryption_parameters(
524   - qpdf_data qpdf, char const* user_password, char const* owner_password,
525   - QPDF_BOOL allow_accessibility, QPDF_BOOL allow_extract,
526   - enum qpdf_r3_print_e print, enum qpdf_r3_modify_e modify,
  563 + qpdf_data qpdf,
  564 + char const* user_password,
  565 + char const* owner_password,
  566 + QPDF_BOOL allow_accessibility,
  567 + QPDF_BOOL allow_extract,
  568 + enum qpdf_r3_print_e print,
  569 + enum qpdf_r3_modify_e modify,
527 570 QPDF_BOOL encrypt_metadata);
528 571  
529 572 QPDF_DLL
... ... @@ -648,8 +691,8 @@ extern &quot;C&quot; {
648 691 QPDF_DLL
649 692 qpdf_oh qpdf_make_indirect_object(qpdf_data qpdf, qpdf_oh oh);
650 693 QPDF_DLL
651   - void qpdf_replace_object(
652   - qpdf_data qpdf, int objid, int generation, qpdf_oh oh);
  694 + void
  695 + qpdf_replace_object(qpdf_data qpdf, int objid, int generation, qpdf_oh oh);
653 696  
654 697 /* Wrappers around QPDFObjectHandle methods. Be sure to read
655 698 * corresponding comments in QPDFObjectHandle.hh to understand
... ... @@ -690,8 +733,8 @@ extern &quot;C&quot; {
690 733 QPDF_BOOL qpdf_oh_is_scalar(qpdf_data qpdf, qpdf_oh oh);
691 734  
692 735 QPDF_DLL
693   - QPDF_BOOL qpdf_oh_is_name_and_equals(
694   - qpdf_data qpdf, qpdf_oh oh, char const* name);
  736 + QPDF_BOOL
  737 + qpdf_oh_is_name_and_equals(qpdf_data qpdf, qpdf_oh oh, char const* name);
695 738  
696 739 QPDF_DLL
697 740 QPDF_BOOL qpdf_oh_is_dictionary_of_type(
... ... @@ -711,19 +754,18 @@ extern &quot;C&quot; {
711 754 QPDF_DLL
712 755 QPDF_BOOL qpdf_oh_get_bool_value(qpdf_data qpdf, qpdf_oh oh);
713 756 QPDF_DLL
714   - QPDF_BOOL qpdf_oh_get_value_as_bool(
715   - qpdf_data qpdf, qpdf_oh oh, QPDF_BOOL* value);
  757 + QPDF_BOOL
  758 + qpdf_oh_get_value_as_bool(qpdf_data qpdf, qpdf_oh oh, QPDF_BOOL* value);
716 759  
717 760 QPDF_DLL
718 761 long long qpdf_oh_get_int_value(qpdf_data qpdf, qpdf_oh oh);
719 762 QPDF_DLL
720   - QPDF_BOOL qpdf_oh_get_value_as_longlong(
721   - qpdf_data qpdf, qpdf_oh oh, long long* value);
  763 + QPDF_BOOL
  764 + qpdf_oh_get_value_as_longlong(qpdf_data qpdf, qpdf_oh oh, long long* value);
722 765 QPDF_DLL
723 766 int qpdf_oh_get_int_value_as_int(qpdf_data qpdf, qpdf_oh oh);
724 767 QPDF_DLL
725   - QPDF_BOOL qpdf_oh_get_value_as_int(
726   - qpdf_data qpdf, qpdf_oh oh, int* value);
  768 + QPDF_BOOL qpdf_oh_get_value_as_int(qpdf_data qpdf, qpdf_oh oh, int* value);
727 769 QPDF_DLL
728 770 unsigned long long qpdf_oh_get_uint_value(qpdf_data qpdf, qpdf_oh oh);
729 771 QPDF_DLL
... ... @@ -732,8 +774,8 @@ extern &quot;C&quot; {
732 774 QPDF_DLL
733 775 unsigned int qpdf_oh_get_uint_value_as_uint(qpdf_data qpdf, qpdf_oh oh);
734 776 QPDF_DLL
735   - QPDF_BOOL qpdf_oh_get_value_as_uint(
736   - qpdf_data qpdf, qpdf_oh oh, unsigned int* value);
  777 + QPDF_BOOL
  778 + qpdf_oh_get_value_as_uint(qpdf_data qpdf, qpdf_oh oh, unsigned int* value);
737 779  
738 780 QPDF_DLL
739 781 char const* qpdf_oh_get_real_value(qpdf_data qpdf, qpdf_oh oh);
... ... @@ -746,8 +788,8 @@ extern &quot;C&quot; {
746 788 QPDF_DLL
747 789 double qpdf_oh_get_numeric_value(qpdf_data qpdf, qpdf_oh oh);
748 790 QPDF_DLL
749   - QPDF_BOOL qpdf_oh_get_value_as_number(
750   - qpdf_data qpdf, qpdf_oh oh, double* value);
  791 + QPDF_BOOL
  792 + qpdf_oh_get_value_as_number(qpdf_data qpdf, qpdf_oh oh, double* value);
751 793  
752 794 QPDF_DLL
753 795 char const* qpdf_oh_get_name(qpdf_data qpdf, qpdf_oh oh);
... ... @@ -782,11 +824,11 @@ extern &quot;C&quot; {
782 824 QPDF_BOOL qpdf_oh_get_value_as_utf8(
783 825 qpdf_data qpdf, qpdf_oh oh, char const** value, size_t* length);
784 826 QPDF_DLL
785   - char const* qpdf_oh_get_binary_string_value(
786   - qpdf_data qpdf, qpdf_oh oh, size_t* length);
  827 + char const*
  828 + qpdf_oh_get_binary_string_value(qpdf_data qpdf, qpdf_oh oh, size_t* length);
787 829 QPDF_DLL
788   - char const* qpdf_oh_get_binary_utf8_value(
789   - qpdf_data qpdf, qpdf_oh oh, size_t* length);
  830 + char const*
  831 + qpdf_oh_get_binary_utf8_value(qpdf_data qpdf, qpdf_oh oh, size_t* length);
790 832  
791 833 QPDF_DLL
792 834 int qpdf_oh_get_array_n_items(qpdf_data qpdf, qpdf_oh oh);
... ... @@ -822,12 +864,12 @@ extern &quot;C&quot; {
822 864 QPDF_DLL
823 865 qpdf_oh qpdf_oh_get_key(qpdf_data qpdf, qpdf_oh oh, char const* key);
824 866 QPDF_DLL
825   - qpdf_oh qpdf_oh_get_key_if_dict(
826   - qpdf_data qpdf, qpdf_oh oh, char const* key);
  867 + qpdf_oh
  868 + qpdf_oh_get_key_if_dict(qpdf_data qpdf, qpdf_oh oh, char const* key);
827 869  
828 870 QPDF_DLL
829   - QPDF_BOOL qpdf_oh_is_or_has_name(
830   - qpdf_data qpdf, qpdf_oh oh, char const* key);
  871 + QPDF_BOOL
  872 + qpdf_oh_is_or_has_name(qpdf_data qpdf, qpdf_oh oh, char const* key);
831 873  
832 874 QPDF_DLL
833 875 qpdf_oh qpdf_oh_new_uninitialized(qpdf_data qpdf);
... ... @@ -840,8 +882,8 @@ extern &quot;C&quot; {
840 882 QPDF_DLL
841 883 qpdf_oh qpdf_oh_new_real_from_string(qpdf_data qpdf, char const* value);
842 884 QPDF_DLL
843   - qpdf_oh qpdf_oh_new_real_from_double(qpdf_data qpdf,
844   - double value, int decimal_places);
  885 + qpdf_oh qpdf_oh_new_real_from_double(
  886 + qpdf_data qpdf, double value, int decimal_places);
845 887 QPDF_DLL
846 888 qpdf_oh qpdf_oh_new_name(qpdf_data qpdf, char const* name);
847 889 QPDF_DLL
... ... @@ -852,9 +894,9 @@ extern &quot;C&quot; {
852 894 * contain atrbitary binary data including embedded null characters.
853 895 */
854 896 QPDF_DLL
855   - qpdf_oh qpdf_oh_new_binary_string(
856   - qpdf_data qpdf, char const* str, size_t length);
857   - QPDF_DLL
  897 + qpdf_oh
  898 + qpdf_oh_new_binary_string(qpdf_data qpdf, char const* str, size_t length);
  899 + QPDF_DLL
858 900 qpdf_oh qpdf_oh_new_binary_unicode_string(
859 901 qpdf_data qpdf, char const* str, size_t length);
860 902 QPDF_DLL
... ... @@ -867,7 +909,7 @@ extern &quot;C&quot; {
867 909 * comments in QPDFObjectHandle.hh for newStream() for additional
868 910 * notes. You must call qpdf_oh_replace_stream_data to provide
869 911 * data for the stream. See STREAM FUNCTIONS below.
870   - */
  912 + */
871 913 QPDF_DLL
872 914 qpdf_oh qpdf_oh_new_stream(qpdf_data qpdf);
873 915  
... ... @@ -875,8 +917,8 @@ extern &quot;C&quot; {
875 917 void qpdf_oh_make_direct(qpdf_data qpdf, qpdf_oh oh);
876 918  
877 919 QPDF_DLL
878   - void qpdf_oh_set_array_item(qpdf_data qpdf, qpdf_oh oh,
879   - int at, qpdf_oh item);
  920 + void
  921 + qpdf_oh_set_array_item(qpdf_data qpdf, qpdf_oh oh, int at, qpdf_oh item);
880 922 QPDF_DLL
881 923 void qpdf_oh_insert_item(qpdf_data qpdf, qpdf_oh oh, int at, qpdf_oh item);
882 924 QPDF_DLL
... ... @@ -885,13 +927,13 @@ extern &quot;C&quot; {
885 927 void qpdf_oh_erase_item(qpdf_data qpdf, qpdf_oh oh, int at);
886 928  
887 929 QPDF_DLL
888   - void qpdf_oh_replace_key(qpdf_data qpdf, qpdf_oh oh,
889   - char const* key, qpdf_oh item);
  930 + void qpdf_oh_replace_key(
  931 + qpdf_data qpdf, qpdf_oh oh, char const* key, qpdf_oh item);
890 932 QPDF_DLL
891 933 void qpdf_oh_remove_key(qpdf_data qpdf, qpdf_oh oh, char const* key);
892 934 QPDF_DLL
893   - void qpdf_oh_replace_or_remove_key(qpdf_data qpdf, qpdf_oh oh,
894   - char const* key, qpdf_oh item);
  935 + void qpdf_oh_replace_or_remove_key(
  936 + qpdf_data qpdf, qpdf_oh oh, char const* key, qpdf_oh item);
895 937  
896 938 QPDF_DLL
897 939 qpdf_oh qpdf_oh_get_dict(qpdf_data qpdf, qpdf_oh oh);
... ... @@ -946,9 +988,12 @@ extern &quot;C&quot; {
946 988 */
947 989 QPDF_DLL
948 990 QPDF_ERROR_CODE qpdf_oh_get_stream_data(
949   - qpdf_data qpdf, qpdf_oh stream_oh,
950   - enum qpdf_stream_decode_level_e decode_level, QPDF_BOOL* filtered,
951   - unsigned char** bufp, size_t* len);
  991 + qpdf_data qpdf,
  992 + qpdf_oh stream_oh,
  993 + enum qpdf_stream_decode_level_e decode_level,
  994 + QPDF_BOOL* filtered,
  995 + unsigned char** bufp,
  996 + size_t* len);
952 997  
953 998 /* This function returns the concatenation of all of a page's
954 999 * content streams as a single, dynamically allocated buffer. As
... ... @@ -957,17 +1002,19 @@ extern &quot;C&quot; {
957 1002 */
958 1003 QPDF_DLL
959 1004 QPDF_ERROR_CODE qpdf_oh_get_page_content_data(
960   - qpdf_data qpdf, qpdf_oh page_oh,
961   - unsigned char** bufp, size_t* len);
  1005 + qpdf_data qpdf, qpdf_oh page_oh, unsigned char** bufp, size_t* len);
962 1006  
963 1007 /* The data pointed to by bufp will be copied by the library. It
964 1008 * does not need to remain valid after the call returns.
965 1009 */
966 1010 QPDF_DLL
967 1011 void qpdf_oh_replace_stream_data(
968   - qpdf_data qpdf, qpdf_oh stream_oh,
969   - unsigned char const* buf, size_t len,
970   - qpdf_oh filter, qpdf_oh decode_parms);
  1012 + qpdf_data qpdf,
  1013 + qpdf_oh stream_oh,
  1014 + unsigned char const* buf,
  1015 + size_t len,
  1016 + qpdf_oh filter,
  1017 + qpdf_oh decode_parms);
971 1018  
972 1019 /* PAGE FUNCTIONS */
973 1020  
... ... @@ -1018,14 +1065,17 @@ extern &quot;C&quot; {
1018 1065 QPDF_DLL
1019 1066 QPDF_ERROR_CODE qpdf_add_page(
1020 1067 qpdf_data qpdf,
1021   - qpdf_data newpage_qpdf, qpdf_oh newpage,
  1068 + qpdf_data newpage_qpdf,
  1069 + qpdf_oh newpage,
1022 1070 QPDF_BOOL first);
1023 1071 /* addPageAt() */
1024 1072 QPDF_DLL
1025 1073 QPDF_ERROR_CODE qpdf_add_page_at(
1026 1074 qpdf_data qpdf,
1027   - qpdf_data newpage_qpdf, qpdf_oh newpage,
1028   - QPDF_BOOL before, qpdf_oh refpage);
  1075 + qpdf_data newpage_qpdf,
  1076 + qpdf_oh newpage,
  1077 + QPDF_BOOL before,
  1078 + qpdf_oh refpage);
1029 1079 /* removePage() */
1030 1080 QPDF_DLL
1031 1081 QPDF_ERROR_CODE qpdf_remove_page(qpdf_data qpdf, qpdf_oh page);
... ... @@ -1033,5 +1083,4 @@ extern &quot;C&quot; {
1033 1083 }
1034 1084 #endif
1035 1085  
1036   -
1037 1086 #endif /* QPDF_C_H */
... ...
include/qpdf/qpdfjob-c.h
... ... @@ -75,5 +75,4 @@ extern &quot;C&quot; {
75 75 }
76 76 #endif
77 77  
78   -
79 78 #endif /* QPDFJOB_C_H */
... ...
libqpdf/AES_PDF_native.cc
1 1 #include <qpdf/AES_PDF_native.hh>
2 2  
  3 +#include <qpdf/QIntC.hh>
  4 +#include <qpdf/QPDFCryptoImpl.hh>
3 5 #include <qpdf/QUtil.hh>
4   -#include <cstring>
  6 +#include <qpdf/rijndael.h>
5 7 #include <assert.h>
  8 +#include <cstring>
6 9 #include <stdexcept>
7   -#include <qpdf/rijndael.h>
8   -#include <qpdf/QIntC.hh>
9   -#include <string>
10 10 #include <stdlib.h>
11   -#include <qpdf/QPDFCryptoImpl.hh>
  11 +#include <string>
12 12  
13   -AES_PDF_native::AES_PDF_native(bool encrypt, unsigned char const* key,
14   - size_t key_bytes, bool cbc_mode,
15   - unsigned char* cbc_block) :
  13 +AES_PDF_native::AES_PDF_native(
  14 + bool encrypt,
  15 + unsigned char const* key,
  16 + size_t key_bytes,
  17 + bool cbc_mode,
  18 + unsigned char* cbc_block) :
16 19 encrypt(encrypt),
17 20 cbc_mode(cbc_mode),
18 21 cbc_block(cbc_block),
... ... @@ -24,15 +27,12 @@ AES_PDF_native::AES_PDF_native(bool encrypt, unsigned char const* key,
24 27 size_t rk_bytes = RKLENGTH(keybits) * sizeof(uint32_t);
25 28 std::memcpy(this->key.get(), key, key_bytes);
26 29 std::memset(this->rk.get(), 0, rk_bytes);
27   - if (encrypt)
28   - {
29   - this->nrounds = rijndaelSetupEncrypt(
30   - this->rk.get(), this->key.get(), keybits);
31   - }
32   - else
33   - {
34   - this->nrounds = rijndaelSetupDecrypt(
35   - this->rk.get(), this->key.get(), keybits);
  30 + if (encrypt) {
  31 + this->nrounds =
  32 + rijndaelSetupEncrypt(this->rk.get(), this->key.get(), keybits);
  33 + } else {
  34 + this->nrounds =
  35 + rijndaelSetupDecrypt(this->rk.get(), this->key.get(), keybits);
36 36 }
37 37 }
38 38  
... ... @@ -43,35 +43,24 @@ AES_PDF_native::~AES_PDF_native()
43 43 void
44 44 AES_PDF_native::update(unsigned char* in_data, unsigned char* out_data)
45 45 {
46   - if (this->encrypt)
47   - {
48   - if (this->cbc_mode)
49   - {
50   - for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i)
51   - {
  46 + if (this->encrypt) {
  47 + if (this->cbc_mode) {
  48 + for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) {
52 49 in_data[i] ^= this->cbc_block[i];
53 50 }
54 51 }
55   - rijndaelEncrypt(this->rk.get(),
56   - this->nrounds, in_data, out_data);
57   - if (this->cbc_mode)
58   - {
59   - memcpy(this->cbc_block, out_data,
60   - QPDFCryptoImpl::rijndael_buf_size);
  52 + rijndaelEncrypt(this->rk.get(), this->nrounds, in_data, out_data);
  53 + if (this->cbc_mode) {
  54 + memcpy(
  55 + this->cbc_block, out_data, QPDFCryptoImpl::rijndael_buf_size);
61 56 }
62   - }
63   - else
64   - {
65   - rijndaelDecrypt(this->rk.get(),
66   - this->nrounds, in_data, out_data);
67   - if (this->cbc_mode)
68   - {
69   - for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i)
70   - {
  57 + } else {
  58 + rijndaelDecrypt(this->rk.get(), this->nrounds, in_data, out_data);
  59 + if (this->cbc_mode) {
  60 + for (size_t i = 0; i < QPDFCryptoImpl::rijndael_buf_size; ++i) {
71 61 out_data[i] ^= this->cbc_block[i];
72 62 }
73   - memcpy(this->cbc_block, in_data,
74   - QPDFCryptoImpl::rijndael_buf_size);
  63 + memcpy(this->cbc_block, in_data, QPDFCryptoImpl::rijndael_buf_size);
75 64 }
76 65 }
77 66 }
... ...
libqpdf/BitStream.cc
... ... @@ -18,8 +18,7 @@ BitStream::reset()
18 18 {
19 19 p = start;
20 20 bit_offset = 7;
21   - if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8)
22   - {
  21 + if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8) {
23 22 throw std::runtime_error("array too large for bitstream");
24 23 }
25 24 bits_available = 8 * nbytes;
... ... @@ -28,22 +27,18 @@ BitStream::reset()
28 27 unsigned long long
29 28 BitStream::getBits(size_t nbits)
30 29 {
31   - return read_bits(this->p, this->bit_offset,
32   - this->bits_available, nbits);
  30 + return read_bits(this->p, this->bit_offset, this->bits_available, nbits);
33 31 }
34 32  
35 33 long long
36 34 BitStream::getBitsSigned(size_t nbits)
37 35 {
38   - unsigned long long bits = read_bits(this->p, this->bit_offset,
39   - this->bits_available, nbits);
  36 + unsigned long long bits =
  37 + read_bits(this->p, this->bit_offset, this->bits_available, nbits);
40 38 long long result = 0;
41   - if (static_cast<long long>(bits) > 1LL << (nbits - 1))
42   - {
43   - result = static_cast<long long>(bits -(1ULL << nbits));
44   - }
45   - else
46   - {
  39 + if (static_cast<long long>(bits) > 1LL << (nbits - 1)) {
  40 + result = static_cast<long long>(bits - (1ULL << nbits));
  41 + } else {
47 42 result = static_cast<long long>(bits);
48 43 }
49 44 return result;
... ... @@ -52,20 +47,16 @@ BitStream::getBitsSigned(size_t nbits)
52 47 int
53 48 BitStream::getBitsInt(size_t nbits)
54 49 {
55   - return static_cast<int>(
56   - QIntC::to_uint(
57   - read_bits(this->p, this->bit_offset,
58   - this->bits_available, nbits)));
  50 + return static_cast<int>(QIntC::to_uint(
  51 + read_bits(this->p, this->bit_offset, this->bits_available, nbits)));
59 52 }
60 53  
61 54 void
62 55 BitStream::skipToNextByte()
63 56 {
64   - if (bit_offset != 7)
65   - {
  57 + if (bit_offset != 7) {
66 58 size_t bits_to_skip = bit_offset + 1;
67   - if (bits_available < bits_to_skip)
68   - {
  59 + if (bits_available < bits_to_skip) {
69 60 throw std::logic_error(
70 61 "INTERNAL ERROR: overflow skipping to next byte in bitstream");
71 62 }
... ...
libqpdf/BitWriter.cc
... ... @@ -21,12 +21,9 @@ void
21 21 BitWriter::writeBitsSigned(long long val, size_t bits)
22 22 {
23 23 unsigned long long uval = 0;
24   - if (val < 0)
25   - {
  24 + if (val < 0) {
26 25 uval = (1ULL << bits) + static_cast<unsigned long long>(val);
27   - }
28   - else
29   - {
  26 + } else {
30 27 uval = static_cast<unsigned long long>(val);
31 28 }
32 29 writeBits(uval, bits);
... ... @@ -41,8 +38,7 @@ BitWriter::writeBitsInt(int val, size_t bits)
41 38 void
42 39 BitWriter::flush()
43 40 {
44   - if (bit_offset < 7)
45   - {
  41 + if (bit_offset < 7) {
46 42 size_t bits_to_write = bit_offset + 1;
47 43 write_bits(this->ch, this->bit_offset, 0, bits_to_write, this->pl);
48 44 }
... ...
libqpdf/Buffer.cc
... ... @@ -7,21 +7,17 @@ Buffer::Members::Members(size_t size, unsigned char* buf, bool own_memory) :
7 7 size(size),
8 8 buf(0)
9 9 {
10   - if (own_memory)
11   - {
  10 + if (own_memory) {
12 11 this->buf = (size ? new unsigned char[size] : 0);
13   - }
14   - else
15   - {
  12 + } else {
16 13 this->buf = buf;
17 14 }
18 15 }
19 16  
20 17 Buffer::Members::~Members()
21 18 {
22   - if (this->own_memory)
23   - {
24   - delete [] this->buf;
  19 + if (this->own_memory) {
  20 + delete[] this->buf;
25 21 }
26 22 }
27 23  
... ... @@ -55,11 +51,9 @@ Buffer::operator=(Buffer const&amp; rhs)
55 51 void
56 52 Buffer::copy(Buffer const& rhs)
57 53 {
58   - if (this != &rhs)
59   - {
  54 + if (this != &rhs) {
60 55 this->m = PointerHolder<Members>(new Members(rhs.m->size, 0, true));
61   - if (this->m->size)
62   - {
  56 + if (this->m->size) {
63 57 memcpy(this->m->buf, rhs.m->buf, this->m->size);
64 58 }
65 59 }
... ...
libqpdf/BufferInputSource.cc
1 1 #include <qpdf/BufferInputSource.hh>
2 2  
3 3 #include <qpdf/QIntC.hh>
4   -#include <string.h>
5   -#include <stdexcept>
6 4 #include <algorithm>
7 5 #include <limits>
8 6 #include <sstream>
  7 +#include <stdexcept>
  8 +#include <string.h>
9 9  
10   -BufferInputSource::Members::Members(bool own_memory,
11   - std::string const& description,
12   - Buffer* buf) :
  10 +BufferInputSource::Members::Members(
  11 + bool own_memory, std::string const& description, Buffer* buf) :
13 12 own_memory(own_memory),
14 13 description(description),
15 14 buf(buf),
... ... @@ -22,14 +21,14 @@ BufferInputSource::Members::~Members()
22 21 {
23 22 }
24 23  
25   -BufferInputSource::BufferInputSource(std::string const& description,
26   - Buffer* buf, bool own_memory) :
  24 +BufferInputSource::BufferInputSource(
  25 + std::string const& description, Buffer* buf, bool own_memory) :
27 26 m(new Members(own_memory, description, buf))
28 27 {
29 28 }
30 29  
31   -BufferInputSource::BufferInputSource(std::string const& description,
32   - std::string const& contents) :
  30 +BufferInputSource::BufferInputSource(
  31 + std::string const& description, std::string const& contents) :
33 32 m(new Members(true, description, 0))
34 33 {
35 34 this->m->buf = new Buffer(contents.length());
... ... @@ -40,8 +39,7 @@ BufferInputSource::BufferInputSource(std::string const&amp; description,
40 39  
41 40 BufferInputSource::~BufferInputSource()
42 41 {
43   - if (this->m->own_memory)
44   - {
  42 + if (this->m->own_memory) {
45 43 delete this->m->buf;
46 44 }
47 45 }
... ... @@ -49,13 +47,11 @@ BufferInputSource::~BufferInputSource()
49 47 qpdf_offset_t
50 48 BufferInputSource::findAndSkipNextEOL()
51 49 {
52   - if (this->m->cur_offset < 0)
53   - {
  50 + if (this->m->cur_offset < 0) {
54 51 throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
55 52 }
56 53 qpdf_offset_t end_pos = this->m->max_offset;
57   - if (this->m->cur_offset >= end_pos)
58   - {
  54 + if (this->m->cur_offset >= end_pos) {
59 55 this->last_offset = end_pos;
60 56 this->m->cur_offset = end_pos;
61 57 return end_pos;
... ... @@ -66,24 +62,19 @@ BufferInputSource::findAndSkipNextEOL()
66 62 unsigned char const* end = buffer + end_pos;
67 63 unsigned char const* p = buffer + this->m->cur_offset;
68 64  
69   - while ((p < end) && !((*p == '\r') || (*p == '\n')))
70   - {
  65 + while ((p < end) && !((*p == '\r') || (*p == '\n'))) {
71 66 ++p;
72 67 }
73   - if (p < end)
74   - {
  68 + if (p < end) {
75 69 result = p - buffer;
76 70 this->m->cur_offset = result + 1;
77 71 ++p;
78 72 while ((this->m->cur_offset < end_pos) &&
79   - ((*p == '\r') || (*p == '\n')))
80   - {
  73 + ((*p == '\r') || (*p == '\n'))) {
81 74 ++p;
82 75 ++this->m->cur_offset;
83 76 }
84   - }
85   - else
86   - {
  77 + } else {
87 78 this->m->cur_offset = end_pos;
88 79 result = end_pos;
89 80 }
... ... @@ -105,30 +96,28 @@ BufferInputSource::tell()
105 96 void
106 97 BufferInputSource::seek(qpdf_offset_t offset, int whence)
107 98 {
108   - switch (whence)
109   - {
110   - case SEEK_SET:
  99 + switch (whence) {
  100 + case SEEK_SET:
111 101 this->m->cur_offset = offset;
112 102 break;
113 103  
114   - case SEEK_END:
  104 + case SEEK_END:
115 105 QIntC::range_check(this->m->max_offset, offset);
116 106 this->m->cur_offset = this->m->max_offset + offset;
117 107 break;
118 108  
119   - case SEEK_CUR:
  109 + case SEEK_CUR:
120 110 QIntC::range_check(this->m->cur_offset, offset);
121 111 this->m->cur_offset += offset;
122 112 break;
123 113  
124   - default:
  114 + default:
125 115 throw std::logic_error(
126 116 "INTERNAL ERROR: invalid argument to BufferInputSource::seek");
127 117 break;
128 118 }
129 119  
130   - if (this->m->cur_offset < 0)
131   - {
  120 + if (this->m->cur_offset < 0) {
132 121 throw std::runtime_error(
133 122 this->m->description + ": seek before beginning of buffer");
134 123 }
... ... @@ -143,20 +132,18 @@ BufferInputSource::rewind()
143 132 size_t
144 133 BufferInputSource::read(char* buffer, size_t length)
145 134 {
146   - if (this->m->cur_offset < 0)
147   - {
  135 + if (this->m->cur_offset < 0) {
148 136 throw std::logic_error("INTERNAL ERROR: BufferInputSource offset < 0");
149 137 }
150 138 qpdf_offset_t end_pos = this->m->max_offset;
151   - if (this->m->cur_offset >= end_pos)
152   - {
  139 + if (this->m->cur_offset >= end_pos) {
153 140 this->last_offset = end_pos;
154 141 return 0;
155 142 }
156 143  
157 144 this->last_offset = this->m->cur_offset;
158   - size_t len = std::min(
159   - QIntC::to_size(end_pos - this->m->cur_offset), length);
  145 + size_t len =
  146 + std::min(QIntC::to_size(end_pos - this->m->cur_offset), length);
160 147 memcpy(buffer, this->m->buf->getBuffer() + this->m->cur_offset, len);
161 148 this->m->cur_offset += QIntC::to_offset(len);
162 149 return len;
... ... @@ -165,8 +152,7 @@ BufferInputSource::read(char* buffer, size_t length)
165 152 void
166 153 BufferInputSource::unreadCh(char ch)
167 154 {
168   - if (this->m->cur_offset > 0)
169   - {
  155 + if (this->m->cur_offset > 0) {
170 156 --this->m->cur_offset;
171 157 }
172 158 }
... ...
libqpdf/ClosedFileInputSource.cc
... ... @@ -25,8 +25,7 @@ ClosedFileInputSource::~ClosedFileInputSource()
25 25 void
26 26 ClosedFileInputSource::before()
27 27 {
28   - if (0 == this->m->fis.get())
29   - {
  28 + if (0 == this->m->fis.get()) {
30 29 this->m->fis = make_pointer_holder<FileInputSource>();
31 30 this->m->fis->setFilename(this->m->filename.c_str());
32 31 this->m->fis->seek(this->m->offset, SEEK_SET);
... ... @@ -39,8 +38,7 @@ ClosedFileInputSource::after()
39 38 {
40 39 this->last_offset = this->m->fis->getLastOffset();
41 40 this->m->offset = this->m->fis->tell();
42   - if (this->m->stay_open)
43   - {
  41 + if (this->m->stay_open) {
44 42 return;
45 43 }
46 44 this->m->fis = 0;
... ... @@ -82,8 +80,7 @@ void
82 80 ClosedFileInputSource::rewind()
83 81 {
84 82 this->m->offset = 0;
85   - if (this->m->fis.get())
86   - {
  83 + if (this->m->fis.get()) {
87 84 this->m->fis->rewind();
88 85 }
89 86 }
... ... @@ -110,8 +107,7 @@ void
110 107 ClosedFileInputSource::stayOpen(bool val)
111 108 {
112 109 this->m->stay_open = val;
113   - if ((! val) && this->m->fis.get())
114   - {
  110 + if ((!val) && this->m->fis.get()) {
115 111 after();
116 112 }
117 113 }
... ...
libqpdf/ContentNormalizer.cc
... ... @@ -18,57 +18,46 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token)
18 18 std::string value = token.getRawValue();
19 19 QPDFTokenizer::token_type_e token_type = token.getType();
20 20  
21   - if (token_type == QPDFTokenizer::tt_bad)
22   - {
  21 + if (token_type == QPDFTokenizer::tt_bad) {
23 22 this->any_bad_tokens = true;
24 23 this->last_token_was_bad = true;
25   - }
26   - else if (token_type != QPDFTokenizer::tt_eof)
27   - {
  24 + } else if (token_type != QPDFTokenizer::tt_eof) {
28 25 this->last_token_was_bad = false;
29 26 }
30 27  
31   - switch (token_type)
32   - {
33   - case QPDFTokenizer::tt_space:
  28 + switch (token_type) {
  29 + case QPDFTokenizer::tt_space:
34 30 {
35 31 size_t len = value.length();
36   - for (size_t i = 0; i < len; ++i)
37   - {
  32 + for (size_t i = 0; i < len; ++i) {
38 33 char ch = value.at(i);
39   - if (ch == '\r')
40   - {
41   - if ((i + 1 < len) && (value.at(i + 1) == '\n'))
42   - {
  34 + if (ch == '\r') {
  35 + if ((i + 1 < len) && (value.at(i + 1) == '\n')) {
43 36 // ignore
44   - }
45   - else
46   - {
  37 + } else {
47 38 write("\n");
48 39 }
49   - }
50   - else
51   - {
  40 + } else {
52 41 write(&ch, 1);
53 42 }
54 43 }
55 44 }
56 45 break;
57 46  
58   - case QPDFTokenizer::tt_string:
  47 + case QPDFTokenizer::tt_string:
59 48 // Replacing string and name tokens in this way normalizes
60 49 // their representation as this will automatically handle
61 50 // quoting of unprintable characters, etc.
62   - writeToken(QPDFTokenizer::Token(
63   - QPDFTokenizer::tt_string, token.getValue()));
  51 + writeToken(
  52 + QPDFTokenizer::Token(QPDFTokenizer::tt_string, token.getValue()));
64 53 break;
65 54  
66   - case QPDFTokenizer::tt_name:
67   - writeToken(QPDFTokenizer::Token(
68   - QPDFTokenizer::tt_name, token.getValue()));
  55 + case QPDFTokenizer::tt_name:
  56 + writeToken(
  57 + QPDFTokenizer::Token(QPDFTokenizer::tt_name, token.getValue()));
69 58 break;
70 59  
71   - default:
  60 + default:
72 61 writeToken(token);
73 62 break;
74 63 }
... ... @@ -77,8 +66,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const&amp; token)
77 66 if (((token_type == QPDFTokenizer::tt_string) ||
78 67 (token_type == QPDFTokenizer::tt_name)) &&
79 68 ((value.find('\r') != std::string::npos) ||
80   - (value.find('\n') != std::string::npos)))
81   - {
  69 + (value.find('\n') != std::string::npos))) {
82 70 write("\n");
83 71 }
84 72 }
... ... @@ -90,7 +78,7 @@ ContentNormalizer::anyBadTokens() const
90 78 }
91 79  
92 80 bool
93   -ContentNormalizer::lastTokenWasBad()const
  81 +ContentNormalizer::lastTokenWasBad() const
94 82 {
95 83 return this->last_token_was_bad;
96 84 }
... ...
libqpdf/FileInputSource.cc
1 1 #include <qpdf/FileInputSource.hh>
2 2  
3   -#include <string.h>
4   -#include <qpdf/QUtil.hh>
5 3 #include <qpdf/QPDFExc.hh>
  4 +#include <qpdf/QUtil.hh>
6 5 #include <algorithm>
  6 +#include <string.h>
7 7  
8 8 FileInputSource::Members::Members(bool close_file) :
9 9 close_file(close_file),
... ... @@ -13,8 +13,7 @@ FileInputSource::Members::Members(bool close_file) :
13 13  
14 14 FileInputSource::Members::~Members()
15 15 {
16   - if (this->file && this->close_file)
17   - {
  16 + if (this->file && this->close_file) {
18 17 fclose(this->file);
19 18 }
20 19 }
... ... @@ -33,8 +32,7 @@ FileInputSource::setFilename(char const* filename)
33 32 }
34 33  
35 34 void
36   -FileInputSource::setFile(
37   - char const* description, FILE* filep, bool close_file)
  35 +FileInputSource::setFile(char const* description, FILE* filep, bool close_file)
38 36 {
39 37 this->m = PointerHolder<Members>(new Members(close_file));
40 38 this->m->filename = description;
... ... @@ -52,35 +50,26 @@ FileInputSource::findAndSkipNextEOL()
52 50 qpdf_offset_t result = 0;
53 51 bool done = false;
54 52 char buf[10240];
55   - while (! done)
56   - {
  53 + while (!done) {
57 54 qpdf_offset_t cur_offset = QUtil::tell(this->m->file);
58 55 size_t len = this->read(buf, sizeof(buf));
59   - if (len == 0)
60   - {
  56 + if (len == 0) {
61 57 done = true;
62 58 result = this->tell();
63   - }
64   - else
65   - {
  59 + } else {
66 60 char* p1 = static_cast<char*>(memchr(buf, '\r', len));
67 61 char* p2 = static_cast<char*>(memchr(buf, '\n', len));
68 62 char* p = (p1 && p2) ? std::min(p1, p2) : p1 ? p1 : p2;
69   - if (p)
70   - {
  63 + if (p) {
71 64 result = cur_offset + (p - buf);
72 65 // We found \r or \n. Keep reading until we get past
73 66 // \r and \n characters.
74 67 this->seek(result + 1, SEEK_SET);
75 68 char ch;
76   - while (! done)
77   - {
78   - if (this->read(&ch, 1) == 0)
79   - {
  69 + while (!done) {
  70 + if (this->read(&ch, 1) == 0) {
80 71 done = true;
81   - }
82   - else if (! ((ch == '\r') || (ch == '\n')))
83   - {
  72 + } else if (!((ch == '\r') || (ch == '\n'))) {
84 73 this->unreadCh(ch);
85 74 done = true;
86 75 }
... ... @@ -106,11 +95,11 @@ FileInputSource::tell()
106 95 void
107 96 FileInputSource::seek(qpdf_offset_t offset, int whence)
108 97 {
109   - QUtil::os_wrapper(std::string("seek to ") +
110   - this->m->filename + ", offset " +
111   - QUtil::int_to_string(offset) + " (" +
112   - QUtil::int_to_string(whence) + ")",
113   - QUtil::seek(this->m->file, offset, whence));
  98 + QUtil::os_wrapper(
  99 + std::string("seek to ") + this->m->filename + ", offset " +
  100 + QUtil::int_to_string(offset) + " (" + QUtil::int_to_string(whence) +
  101 + ")",
  102 + QUtil::seek(this->m->file, offset, whence));
114 103 }
115 104  
116 105 void
... ... @@ -124,18 +113,16 @@ FileInputSource::read(char* buffer, size_t length)
124 113 {
125 114 this->last_offset = this->tell();
126 115 size_t len = fread(buffer, 1, length, this->m->file);
127   - if (len == 0)
128   - {
129   - if (ferror(this->m->file))
130   - {
131   - throw QPDFExc(qpdf_e_system,
132   - this->m->filename, "",
133   - this->last_offset,
134   - std::string("read ") +
135   - QUtil::uint_to_string(length) + " bytes");
136   - }
137   - else if (length > 0)
138   - {
  116 + if (len == 0) {
  117 + if (ferror(this->m->file)) {
  118 + throw QPDFExc(
  119 + qpdf_e_system,
  120 + this->m->filename,
  121 + "",
  122 + this->last_offset,
  123 + std::string("read ") + QUtil::uint_to_string(length) +
  124 + " bytes");
  125 + } else if (length > 0) {
139 126 this->seek(0, SEEK_END);
140 127 this->last_offset = this->tell();
141 128 }
... ... @@ -146,6 +133,7 @@ FileInputSource::read(char* buffer, size_t length)
146 133 void
147 134 FileInputSource::unreadCh(char ch)
148 135 {
149   - QUtil::os_wrapper(this->m->filename + ": unread character",
150   - ungetc(static_cast<unsigned char>(ch), this->m->file));
  136 + QUtil::os_wrapper(
  137 + this->m->filename + ": unread character",
  138 + ungetc(static_cast<unsigned char>(ch), this->m->file));
151 139 }
... ...
libqpdf/InputSource.cc
1 1 #include <qpdf/InputSource.hh>
2 2  
3   -#include <string.h>
4   -#include <stdexcept>
5   -#include <qpdf/QTC.hh>
6 3 #include <qpdf/PointerHolder.hh>
7 4 #include <qpdf/QIntC.hh>
  5 +#include <qpdf/QTC.hh>
  6 +#include <stdexcept>
  7 +#include <string.h>
8 8  
9 9 InputSource::Members::Members()
10 10 {
... ... @@ -45,17 +45,15 @@ InputSource::readLine(size_t max_line_length)
45 45 qpdf_offset_t eol = this->findAndSkipNextEOL();
46 46 this->last_offset = offset;
47 47 size_t line_length = QIntC::to_size(eol - offset);
48   - if (line_length < max_line_length)
49   - {
  48 + if (line_length < max_line_length) {
50 49 buf[line_length] = '\0';
51 50 }
52 51 return std::string(buf);
53 52 }
54 53  
55 54 bool
56   -InputSource::findFirst(char const* start_chars,
57   - qpdf_offset_t offset, size_t len,
58   - Finder& finder)
  55 +InputSource::findFirst(
  56 + char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder)
59 57 {
60 58 // Basic approach: search for the first character of start_chars
61 59 // starting from offset but not going past len (if len != 0). Once
... ... @@ -74,8 +72,7 @@ InputSource::findFirst(char const* start_chars,
74 72 // To enable us to guarantee null-termination, save an extra byte
75 73 // so that buf[size] is valid memory.
76 74 size_t size = sizeof(buf) - 1;
77   - if ((strlen(start_chars) < 1) || (strlen(start_chars) > size))
78   - {
  75 + if ((strlen(start_chars) < 1) || (strlen(start_chars) > size)) {
79 76 throw std::logic_error(
80 77 "InputSource::findSource called with"
81 78 " too small or too large of a character sequence");
... ... @@ -90,8 +87,7 @@ InputSource::findFirst(char const* start_chars,
90 87 // that will cause return on the next pass. Eventually we will
91 88 // either be out of range or hit EOF, either of which forces us to
92 89 // return.
93   - while (true)
94   - {
  90 + while (true) {
95 91 // Do we need to read more data? Pretend size = 5, buf starts
96 92 // at 0, and start_chars has 3 characters. buf[5] is valid and
97 93 // null. If p == 2, start_chars could be buf[2] through
... ... @@ -99,12 +95,12 @@ InputSource::findFirst(char const* start_chars,
99 95 // If p points to buf[size], since strlen(start_chars) is
100 96 // always >= 1, this overflow test will be correct for that
101 97 // case regardless of start_chars.
102   - if ((p == 0) || ((p + strlen(start_chars)) > (buf + bytes_read)))
103   - {
104   - if (p)
105   - {
106   - QTC::TC("libtests", "InputSource read next block",
107   - ((p == buf + bytes_read) ? 0 : 1));
  98 + if ((p == 0) || ((p + strlen(start_chars)) > (buf + bytes_read))) {
  99 + if (p) {
  100 + QTC::TC(
  101 + "libtests",
  102 + "InputSource read next block",
  103 + ((p == buf + bytes_read) ? 0 : 1));
108 104 buf_offset += (p - buf);
109 105 }
110 106 this->seek(buf_offset, SEEK_SET);
... ... @@ -113,10 +109,11 @@ InputSource::findFirst(char const* start_chars,
113 109 // we could guarantee null termination as an extra
114 110 // protection against overrun when using string functions.
115 111 bytes_read = this->read(buf, size);
116   - if (bytes_read < strlen(start_chars))
117   - {
118   - QTC::TC("libtests", "InputSource find EOF",
119   - bytes_read == 0 ? 0 : 1);
  112 + if (bytes_read < strlen(start_chars)) {
  113 + QTC::TC(
  114 + "libtests",
  115 + "InputSource find EOF",
  116 + bytes_read == 0 ? 0 : 1);
120 117 return false;
121 118 }
122 119 memset(buf + bytes_read, '\0', 1 + (size - bytes_read));
... ... @@ -124,29 +121,24 @@ InputSource::findFirst(char const* start_chars,
124 121 }
125 122  
126 123 // Search for the first character.
127   - if ((p = static_cast<char*>(
128   - memchr(p, start_chars[0],
129   - bytes_read - QIntC::to_size(p - buf)))) != 0)
130   - {
131   - if (p == buf)
132   - {
  124 + if ((p = static_cast<char*>(memchr(
  125 + p, start_chars[0], bytes_read - QIntC::to_size(p - buf)))) !=
  126 + 0) {
  127 + if (p == buf) {
133 128 QTC::TC("libtests", "InputSource found match at buf[0]");
134 129 }
135 130 // Found first letter.
136   - if (len != 0)
137   - {
  131 + if (len != 0) {
138 132 // Make sure it's in range.
139 133 size_t p_relative_offset =
140 134 QIntC::to_size((p - buf) + (buf_offset - offset));
141   - if (p_relative_offset >= len)
142   - {
  135 + if (p_relative_offset >= len) {
143 136 // out of range
144 137 QTC::TC("libtests", "InputSource out of range");
145 138 return false;
146 139 }
147 140 }
148   - if ((p + strlen(start_chars)) > (buf + bytes_read))
149   - {
  141 + if ((p + strlen(start_chars)) > (buf + bytes_read)) {
150 142 // If there are not enough bytes left in the file for
151 143 // start_chars, we will detect this on the next pass
152 144 // as EOF and return.
... ... @@ -157,30 +149,26 @@ InputSource::findFirst(char const* start_chars,
157 149 // See if p points to a sequence matching start_chars. We
158 150 // already checked above to make sure we are not going to
159 151 // overrun memory.
160   - if (strncmp(p, start_chars, strlen(start_chars)) == 0)
161   - {
  152 + if (strncmp(p, start_chars, strlen(start_chars)) == 0) {
162 153 // Call finder.check() with the input source
163 154 // positioned to the point of the match.
164 155 this->seek(buf_offset + (p - buf), SEEK_SET);
165   - if (finder.check())
166   - {
  156 + if (finder.check()) {
167 157 return true;
  158 + } else {
  159 + QTC::TC(
  160 + "libtests",
  161 + "InputSource start_chars matched but not check");
168 162 }
169   - else
170   - {
171   - QTC::TC("libtests", "InputSource start_chars matched but not check");
172   - }
173   - }
174   - else
175   - {
176   - QTC::TC("libtests", "InputSource first char matched but not string");
  163 + } else {
  164 + QTC::TC(
  165 + "libtests",
  166 + "InputSource first char matched but not string");
177 167 }
178 168 // This occurrence of the first character wasn't a match.
179 169 // Skip over it and keep searching.
180 170 ++p;
181   - }
182   - else
183   - {
  171 + } else {
184 172 // Trigger reading the next block
185 173 p = buf + bytes_read;
186 174 }
... ... @@ -189,30 +177,24 @@ InputSource::findFirst(char const* start_chars,
189 177 }
190 178  
191 179 bool
192   -InputSource::findLast(char const* start_chars,
193   - qpdf_offset_t offset, size_t len,
194   - Finder& finder)
  180 +InputSource::findLast(
  181 + char const* start_chars, qpdf_offset_t offset, size_t len, Finder& finder)
195 182 {
196 183 bool found = false;
197 184 qpdf_offset_t after_found_offset = 0;
198 185 qpdf_offset_t cur_offset = offset;
199 186 size_t cur_len = len;
200   - while (this->findFirst(start_chars, cur_offset, cur_len, finder))
201   - {
202   - if (found)
203   - {
  187 + while (this->findFirst(start_chars, cur_offset, cur_len, finder)) {
  188 + if (found) {
204 189 QTC::TC("libtests", "InputSource findLast found more than one");
205   - }
206   - else
207   - {
  190 + } else {
208 191 found = true;
209 192 }
210 193 after_found_offset = this->tell();
211 194 cur_offset = after_found_offset;
212 195 cur_len = len - QIntC::to_size((cur_offset - offset));
213 196 }
214   - if (found)
215   - {
  197 + if (found) {
216 198 this->seek(after_found_offset, SEEK_SET);
217 199 }
218 200 return found;
... ...
libqpdf/InsecureRandomDataProvider.cc
1 1 #include <qpdf/InsecureRandomDataProvider.hh>
2 2  
3   -#include <qpdf/qpdf-config.h>
4 3 #include <qpdf/QUtil.hh>
  4 +#include <qpdf/qpdf-config.h>
5 5 #include <stdlib.h>
6 6  
7 7 InsecureRandomDataProvider::InsecureRandomDataProvider() :
... ... @@ -16,8 +16,7 @@ InsecureRandomDataProvider::~InsecureRandomDataProvider()
16 16 void
17 17 InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
18 18 {
19   - for (size_t i = 0; i < len; ++i)
20   - {
  19 + for (size_t i = 0; i < len; ++i) {
21 20 data[i] = static_cast<unsigned char>((this->random() & 0xff0) >> 4);
22 21 }
23 22 }
... ... @@ -25,13 +24,12 @@ InsecureRandomDataProvider::provideRandomData(unsigned char* data, size_t len)
25 24 long
26 25 InsecureRandomDataProvider::random()
27 26 {
28   - if (! this->seeded_random)
29   - {
  27 + if (!this->seeded_random) {
30 28 // Seed the random number generator with something simple, but
31 29 // just to be interesting, don't use the unmodified current
32 30 // time. It would be better if this were a more secure seed.
33   - unsigned int seed = static_cast<unsigned int>(
34   - QUtil::get_current_time() ^ 0xcccc);
  31 + unsigned int seed =
  32 + static_cast<unsigned int>(QUtil::get_current_time() ^ 0xcccc);
35 33 #ifdef HAVE_RANDOM
36 34 ::srandom(seed);
37 35 #else
... ... @@ -40,11 +38,11 @@ InsecureRandomDataProvider::random()
40 38 this->seeded_random = true;
41 39 }
42 40  
43   -# ifdef HAVE_RANDOM
  41 +#ifdef HAVE_RANDOM
44 42 return ::random();
45   -# else
  43 +#else
46 44 return rand();
47   -# endif
  45 +#endif
48 46 }
49 47  
50 48 RandomDataProvider*
... ...
libqpdf/JSON.cc
1 1 #include <qpdf/JSON.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4 3 #include <qpdf/QTC.hh>
5   -#include <stdexcept>
  4 +#include <qpdf/QUtil.hh>
6 5 #include <cstring>
  6 +#include <stdexcept>
7 7  
8 8 JSON::Members::~Members()
9 9 {
... ... @@ -27,27 +27,23 @@ JSON::JSON_dictionary::~JSON_dictionary()
27 27 {
28 28 }
29 29  
30   -std::string JSON::JSON_dictionary::unparse(size_t depth) const
  30 +std::string
  31 +JSON::JSON_dictionary::unparse(size_t depth) const
31 32 {
32 33 std::string result = "{";
33 34 bool first = true;
34   - for (auto const& iter: members)
35   - {
36   - if (first)
37   - {
  35 + for (auto const& iter : members) {
  36 + if (first) {
38 37 first = false;
39   - }
40   - else
41   - {
  38 + } else {
42 39 result.append(1, ',');
43 40 }
44 41 result.append(1, '\n');
45 42 result.append(2 * (1 + depth), ' ');
46   - result += ("\"" + iter.first + "\": " +
47   - iter.second->unparse(1 + depth));
  43 + result +=
  44 + ("\"" + iter.first + "\": " + iter.second->unparse(1 + depth));
48 45 }
49   - if (! first)
50   - {
  46 + if (!first) {
51 47 result.append(1, '\n');
52 48 result.append(2 * depth, ' ');
53 49 }
... ... @@ -59,26 +55,22 @@ JSON::JSON_array::~JSON_array()
59 55 {
60 56 }
61 57  
62   -std::string JSON::JSON_array::unparse(size_t depth) const
  58 +std::string
  59 +JSON::JSON_array::unparse(size_t depth) const
63 60 {
64 61 std::string result = "[";
65 62 bool first = true;
66   - for (auto const& element: elements)
67   - {
68   - if (first)
69   - {
  63 + for (auto const& element : elements) {
  64 + if (first) {
70 65 first = false;
71   - }
72   - else
73   - {
  66 + } else {
74 67 result.append(1, ',');
75 68 }
76 69 result.append(1, '\n');
77 70 result.append(2 * (1 + depth), ' ');
78 71 result += element->unparse(1 + depth);
79 72 }
80   - if (! first)
81   - {
  73 + if (!first) {
82 74 result.append(1, '\n');
83 75 result.append(2 * depth, ' ');
84 76 }
... ... @@ -96,7 +88,8 @@ JSON::JSON_string::~JSON_string()
96 88 {
97 89 }
98 90  
99   -std::string JSON::JSON_string::unparse(size_t) const
  91 +std::string
  92 +JSON::JSON_string::unparse(size_t) const
100 93 {
101 94 return "\"" + encoded + "\"";
102 95 }
... ... @@ -120,7 +113,8 @@ JSON::JSON_number::~JSON_number()
120 113 {
121 114 }
122 115  
123   -std::string JSON::JSON_number::unparse(size_t) const
  116 +std::string
  117 +JSON::JSON_number::unparse(size_t) const
124 118 {
125 119 return encoded;
126 120 }
... ... @@ -134,7 +128,8 @@ JSON::JSON_bool::~JSON_bool()
134 128 {
135 129 }
136 130  
137   -std::string JSON::JSON_bool::unparse(size_t) const
  131 +std::string
  132 +JSON::JSON_bool::unparse(size_t) const
138 133 {
139 134 return value ? "true" : "false";
140 135 }
... ... @@ -143,7 +138,8 @@ JSON::JSON_null::~JSON_null()
143 138 {
144 139 }
145 140  
146   -std::string JSON::JSON_null::unparse(size_t) const
  141 +std::string
  142 +JSON::JSON_null::unparse(size_t) const
147 143 {
148 144 return "null";
149 145 }
... ... @@ -151,12 +147,9 @@ std::string JSON::JSON_null::unparse(size_t) const
151 147 std::string
152 148 JSON::unparse() const
153 149 {
154   - if (0 == this->m->value.get())
155   - {
  150 + if (0 == this->m->value.get()) {
156 151 return "null";
157   - }
158   - else
159   - {
  152 + } else {
160 153 return this->m->value->unparse(0);
161 154 }
162 155 }
... ... @@ -166,39 +159,34 @@ JSON::encode_string(std::string const&amp; str)
166 159 {
167 160 std::string result;
168 161 size_t len = str.length();
169   - for (size_t i = 0; i < len; ++i)
170   - {
  162 + for (size_t i = 0; i < len; ++i) {
171 163 unsigned char ch = static_cast<unsigned char>(str.at(i));
172   - switch (ch)
173   - {
174   - case '\\':
  164 + switch (ch) {
  165 + case '\\':
175 166 result += "\\\\";
176 167 break;
177   - case '\"':
  168 + case '\"':
178 169 result += "\\\"";
179 170 break;
180   - case '\b':
  171 + case '\b':
181 172 result += "\\b";
182 173 break;
183   - case '\f':
  174 + case '\f':
184 175 result += "\\f";
185 176 break;
186   - case '\n':
  177 + case '\n':
187 178 result += "\\n";
188 179 break;
189   - case '\r':
  180 + case '\r':
190 181 result += "\\r";
191 182 break;
192   - case '\t':
  183 + case '\t':
193 184 result += "\\t";
194 185 break;
195   - default:
196   - if (ch < 32)
197   - {
  186 + default:
  187 + if (ch < 32) {
198 188 result += "\\u" + QUtil::int_to_string_base(ch, 16, 4);
199   - }
200   - else
201   - {
  189 + } else {
202 190 result.append(1, static_cast<char>(ch));
203 191 }
204 192 }
... ... @@ -215,19 +203,14 @@ JSON::makeDictionary()
215 203 JSON
216 204 JSON::addDictionaryMember(std::string const& key, JSON const& val)
217 205 {
218   - JSON_dictionary* obj = dynamic_cast<JSON_dictionary*>(
219   - this->m->value.get());
220   - if (0 == obj)
221   - {
  206 + JSON_dictionary* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get());
  207 + if (0 == obj) {
222 208 throw std::runtime_error(
223 209 "JSON::addDictionaryMember called on non-dictionary");
224 210 }
225   - if (val.m->value.get())
226   - {
  211 + if (val.m->value.get()) {
227 212 obj->members[encode_string(key)] = val.m->value;
228   - }
229   - else
230   - {
  213 + } else {
231 214 obj->members[encode_string(key)] = std::make_shared<JSON_null>();
232 215 }
233 216 return obj->members[encode_string(key)];
... ... @@ -242,18 +225,13 @@ JSON::makeArray()
242 225 JSON
243 226 JSON::addArrayElement(JSON const& val)
244 227 {
245   - JSON_array* arr = dynamic_cast<JSON_array*>(
246   - this->m->value.get());
247   - if (0 == arr)
248   - {
  228 + JSON_array* arr = dynamic_cast<JSON_array*>(this->m->value.get());
  229 + if (0 == arr) {
249 230 throw std::runtime_error("JSON::addArrayElement called on non-array");
250 231 }
251   - if (val.m->value.get())
252   - {
  232 + if (val.m->value.get()) {
253 233 arr->elements.push_back(val.m->value);
254   - }
255   - else
256   - {
  234 + } else {
257 235 arr->elements.push_back(std::make_shared<JSON_null>());
258 236 }
259 237 return arr->elements.back();
... ... @@ -298,23 +276,21 @@ JSON::makeNull()
298 276 bool
299 277 JSON::isArray() const
300 278 {
301   - return nullptr != dynamic_cast<JSON_array const*>(
302   - this->m->value.get());
  279 + return nullptr != dynamic_cast<JSON_array const*>(this->m->value.get());
303 280 }
304 281  
305 282 bool
306 283 JSON::isDictionary() const
307 284 {
308   - return nullptr != dynamic_cast<JSON_dictionary const*>(
309   - this->m->value.get());
  285 + return nullptr !=
  286 + dynamic_cast<JSON_dictionary const*>(this->m->value.get());
310 287 }
311 288  
312 289 bool
313 290 JSON::getString(std::string& utf8) const
314 291 {
315 292 auto v = dynamic_cast<JSON_string const*>(this->m->value.get());
316   - if (v == nullptr)
317   - {
  293 + if (v == nullptr) {
318 294 return false;
319 295 }
320 296 utf8 = v->utf8;
... ... @@ -325,8 +301,7 @@ bool
325 301 JSON::getNumber(std::string& value) const
326 302 {
327 303 auto v = dynamic_cast<JSON_number const*>(this->m->value.get());
328   - if (v == nullptr)
329   - {
  304 + if (v == nullptr) {
330 305 return false;
331 306 }
332 307 value = v->encoded;
... ... @@ -337,8 +312,7 @@ bool
337 312 JSON::getBool(bool& value) const
338 313 {
339 314 auto v = dynamic_cast<JSON_bool const*>(this->m->value.get());
340   - if (v == nullptr)
341   - {
  315 + if (v == nullptr) {
342 316 return false;
343 317 }
344 318 value = v->value;
... ... @@ -348,8 +322,7 @@ JSON::getBool(bool&amp; value) const
348 322 bool
349 323 JSON::isNull() const
350 324 {
351   - if (dynamic_cast<JSON_null const*>(this->m->value.get()))
352   - {
  325 + if (dynamic_cast<JSON_null const*>(this->m->value.get())) {
353 326 return true;
354 327 }
355 328 return false;
... ... @@ -360,12 +333,10 @@ JSON::forEachDictItem(
360 333 std::function<void(std::string const& key, JSON value)> fn) const
361 334 {
362 335 auto v = dynamic_cast<JSON_dictionary const*>(this->m->value.get());
363   - if (v == nullptr)
364   - {
  336 + if (v == nullptr) {
365 337 return false;
366 338 }
367   - for (auto const& k: v->members)
368   - {
  339 + for (auto const& k : v->members) {
369 340 fn(k.first, JSON(k.second));
370 341 }
371 342 return true;
... ... @@ -375,12 +346,10 @@ bool
375 346 JSON::forEachArrayItem(std::function<void(JSON value)> fn) const
376 347 {
377 348 auto v = dynamic_cast<JSON_array const*>(this->m->value.get());
378   - if (v == nullptr)
379   - {
  349 + if (v == nullptr) {
380 350 return false;
381 351 }
382   - for (auto const& i: v->elements)
383   - {
  352 + for (auto const& i : v->elements) {
384 353 fn(JSON(i));
385 354 }
386 355 return true;
... ... @@ -389,25 +358,25 @@ JSON::forEachArrayItem(std::function&lt;void(JSON value)&gt; fn) const
389 358 bool
390 359 JSON::checkSchema(JSON schema, std::list<std::string>& errors)
391 360 {
392   - return checkSchemaInternal(this->m->value.get(),
393   - schema.m->value.get(),
394   - 0, errors, "");
  361 + return checkSchemaInternal(
  362 + this->m->value.get(), schema.m->value.get(), 0, errors, "");
395 363 }
396 364  
397 365 bool
398   -JSON::checkSchema(JSON schema, unsigned long flags,
399   - std::list<std::string>& errors)
  366 +JSON::checkSchema(
  367 + JSON schema, unsigned long flags, std::list<std::string>& errors)
400 368 {
401   - return checkSchemaInternal(this->m->value.get(),
402   - schema.m->value.get(),
403   - flags, errors, "");
  369 + return checkSchemaInternal(
  370 + this->m->value.get(), schema.m->value.get(), flags, errors, "");
404 371 }
405 372  
406 373 bool
407   -JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
408   - unsigned long flags,
409   - std::list<std::string>& errors,
410   - std::string prefix)
  374 +JSON::checkSchemaInternal(
  375 + JSON_value* this_v,
  376 + JSON_value* sch_v,
  377 + unsigned long flags,
  378 + std::list<std::string>& errors,
  379 + std::string prefix)
411 380 {
412 381 JSON_array* this_arr = dynamic_cast<JSON_array*>(this_v);
413 382 JSON_dictionary* this_dict = dynamic_cast<JSON_dictionary*>(this_v);
... ... @@ -418,20 +387,15 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
418 387 JSON_string* sch_str = dynamic_cast<JSON_string*>(sch_v);
419 388  
420 389 std::string err_prefix;
421   - if (prefix.empty())
422   - {
  390 + if (prefix.empty()) {
423 391 err_prefix = "top-level object";
424   - }
425   - else
426   - {
  392 + } else {
427 393 err_prefix = "json key \"" + prefix + "\"";
428 394 }
429 395  
430 396 std::string pattern_key;
431   - if (sch_dict)
432   - {
433   - if (! this_dict)
434   - {
  397 + if (sch_dict) {
  398 + if (!this_dict) {
435 399 QTC::TC("libtests", "JSON wanted dictionary");
436 400 errors.push_back(err_prefix + " is supposed to be a dictionary");
437 401 return false;
... ... @@ -440,44 +404,36 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
440 404 std::string key;
441 405 if ((members.size() == 1) &&
442 406 ((key = members.begin()->first, key.length() > 2) &&
443   - (key.at(0) == '<') &&
444   - (key.at(key.length() - 1) == '>')))
445   - {
  407 + (key.at(0) == '<') && (key.at(key.length() - 1) == '>'))) {
446 408 pattern_key = key;
447 409 }
448 410 }
449 411  
450   - if (sch_dict && (! pattern_key.empty()))
451   - {
  412 + if (sch_dict && (!pattern_key.empty())) {
452 413 auto pattern_schema = sch_dict->members[pattern_key].get();
453   - for (auto const& iter: this_dict->members)
454   - {
  414 + for (auto const& iter : this_dict->members) {
455 415 std::string const& key = iter.first;
456 416 checkSchemaInternal(
457   - this_dict->members[key].get(), pattern_schema,
458   - flags, errors, prefix + "." + key);
  417 + this_dict->members[key].get(),
  418 + pattern_schema,
  419 + flags,
  420 + errors,
  421 + prefix + "." + key);
459 422 }
460   - }
461   - else if (sch_dict)
462   - {
463   - for (auto& iter: sch_dict->members)
464   - {
  423 + } else if (sch_dict) {
  424 + for (auto& iter : sch_dict->members) {
465 425 std::string const& key = iter.first;
466   - if (this_dict->members.count(key))
467   - {
  426 + if (this_dict->members.count(key)) {
468 427 checkSchemaInternal(
469 428 this_dict->members[key].get(),
470 429 iter.second.get(),
471   - flags, errors, prefix + "." + key);
472   - }
473   - else
474   - {
475   - if (flags & f_optional)
476   - {
  430 + flags,
  431 + errors,
  432 + prefix + "." + key);
  433 + } else {
  434 + if (flags & f_optional) {
477 435 QTC::TC("libtests", "JSON optional key");
478   - }
479   - else
480   - {
  436 + } else {
481 437 QTC::TC("libtests", "JSON key missing in object");
482 438 errors.push_back(
483 439 err_prefix + ": key \"" + key +
... ... @@ -485,55 +441,49 @@ JSON::checkSchemaInternal(JSON_value* this_v, JSON_value* sch_v,
485 441 }
486 442 }
487 443 }
488   - for (auto const& iter: this_dict->members)
489   - {
  444 + for (auto const& iter : this_dict->members) {
490 445 std::string const& key = iter.first;
491   - if (sch_dict->members.count(key) == 0)
492   - {
  446 + if (sch_dict->members.count(key) == 0) {
493 447 QTC::TC("libtests", "JSON key extra in object");
494 448 errors.push_back(
495 449 err_prefix + ": key \"" + key +
496 450 "\" is not present in schema but appears in object");
497 451 }
498 452 }
499   - }
500   - else if (sch_arr)
501   - {
502   - if (! this_arr)
503   - {
  453 + } else if (sch_arr) {
  454 + if (!this_arr) {
504 455 QTC::TC("libtests", "JSON wanted array");
505 456 errors.push_back(err_prefix + " is supposed to be an array");
506 457 return false;
507 458 }
508   - if (sch_arr->elements.size() != 1)
509   - {
  459 + if (sch_arr->elements.size() != 1) {
510 460 QTC::TC("libtests", "JSON schema array error");
511   - errors.push_back(err_prefix +
512   - " schema array contains other than one item");
  461 + errors.push_back(
  462 + err_prefix + " schema array contains other than one item");
513 463 return false;
514 464 }
515 465 int i = 0;
516   - for (auto const& element: this_arr->elements)
517   - {
  466 + for (auto const& element : this_arr->elements) {
518 467 checkSchemaInternal(
519 468 element.get(),
520 469 sch_arr->elements.at(0).get(),
521   - flags, errors, prefix + "." + QUtil::int_to_string(i));
  470 + flags,
  471 + errors,
  472 + prefix + "." + QUtil::int_to_string(i));
522 473 ++i;
523 474 }
524   - }
525   - else if (! sch_str)
526   - {
  475 + } else if (!sch_str) {
527 476 QTC::TC("libtests", "JSON schema other type");
528   - errors.push_back(err_prefix +
529   - " schema value is not dictionary, array, or string");
  477 + errors.push_back(
  478 + err_prefix + " schema value is not dictionary, array, or string");
530 479 return false;
531 480 }
532 481  
533 482 return errors.empty();
534 483 }
535 484  
536   -namespace {
  485 +namespace
  486 +{
537 487 class JSONParser
538 488 {
539 489 public:
... ... @@ -597,7 +547,7 @@ namespace {
597 547 std::vector<parser_state_e> ps_stack;
598 548 std::string dict_key;
599 549 };
600   -}
  550 +} // namespace
601 551  
602 552 std::string
603 553 JSONParser::decode_string(std::string const& str)
... ... @@ -606,8 +556,7 @@ JSONParser::decode_string(std::string const&amp; str)
606 556 // is called, so errors are logic errors instead of runtime
607 557 // errors.
608 558 size_t len = str.length();
609   - if ((len < 2) || (str.at(0) != '"') || (str.at(len-1) != '"'))
610   - {
  559 + if ((len < 2) || (str.at(0) != '"') || (str.at(len - 1) != '"')) {
611 560 throw std::logic_error(
612 561 "JSON Parse: decode_string called with other than \"...\"");
613 562 }
... ... @@ -616,48 +565,43 @@ JSONParser::decode_string(std::string const&amp; str)
616 565 ++s;
617 566 len -= 2;
618 567 std::string result;
619   - for (size_t i = 0; i < len; ++i)
620   - {
621   - if (s[i] == '\\')
622   - {
623   - if (i + 1 >= len)
624   - {
  568 + for (size_t i = 0; i < len; ++i) {
  569 + if (s[i] == '\\') {
  570 + if (i + 1 >= len) {
625 571 throw std::logic_error("JSON parse: nothing after \\");
626 572 }
627 573 char ch = s[++i];
628   - switch (ch)
629   - {
630   - case '\\':
631   - case '\"':
632   - case '/':
  574 + switch (ch) {
  575 + case '\\':
  576 + case '\"':
  577 + case '/':
633 578 // \/ is allowed in json input, but so is /, so we
634 579 // don't map / to \/ in output.
635 580 result.append(1, ch);
636 581 break;
637   - case 'b':
  582 + case 'b':
638 583 result.append(1, '\b');
639 584 break;
640   - case 'f':
  585 + case 'f':
641 586 result.append(1, '\f');
642 587 break;
643   - case 'n':
  588 + case 'n':
644 589 result.append(1, '\n');
645 590 break;
646   - case 'r':
  591 + case 'r':
647 592 result.append(1, '\r');
648 593 break;
649   - case 't':
  594 + case 't':
650 595 result.append(1, '\t');
651 596 break;
652   - case 'u':
653   - if (i + 4 >= len)
654   - {
  597 + case 'u':
  598 + if (i + 4 >= len) {
655 599 throw std::logic_error(
656 600 "JSON parse: not enough characters after \\u");
657 601 }
658 602 {
659 603 std::string hex =
660   - QUtil::hex_decode(std::string(s+i+1, s+i+5));
  604 + QUtil::hex_decode(std::string(s + i + 1, s + i + 5));
661 605 i += 4;
662 606 unsigned char high = static_cast<unsigned char>(hex.at(0));
663 607 unsigned char low = static_cast<unsigned char>(hex.at(1));
... ... @@ -667,52 +611,40 @@ JSONParser::decode_string(std::string const&amp; str)
667 611 result += QUtil::toUTF8(codepoint);
668 612 }
669 613 break;
670   - default:
671   - throw std::logic_error(
672   - "JSON parse: bad character after \\");
  614 + default:
  615 + throw std::logic_error("JSON parse: bad character after \\");
673 616 break;
674 617 }
675   - }
676   - else
677   - {
  618 + } else {
678 619 result.append(1, s[i]);
679 620 }
680 621 }
681 622 return result;
682 623 }
683 624  
684   -void JSONParser::getToken()
  625 +void
  626 +JSONParser::getToken()
685 627 {
686   - while (p < end)
687   - {
688   - if (*p == 0)
689   - {
  628 + while (p < end) {
  629 + if (*p == 0) {
690 630 QTC::TC("libtests", "JSON parse null character");
691 631 throw std::runtime_error(
692 632 "JSON: null character at offset " +
693 633 QUtil::int_to_string(p - cstr));
694 634 }
695   - switch (lex_state)
696   - {
697   - case ls_top:
698   - if (*p == '"')
699   - {
  635 + switch (lex_state) {
  636 + case ls_top:
  637 + if (*p == '"') {
700 638 tok_start = p;
701 639 tok_end = nullptr;
702 640 lex_state = ls_string;
703   - }
704   - else if (QUtil::is_space(*p))
705   - {
  641 + } else if (QUtil::is_space(*p)) {
706 642 // ignore
707   - }
708   - else if ((*p >= 'a') && (*p <= 'z'))
709   - {
  643 + } else if ((*p >= 'a') && (*p <= 'z')) {
710 644 tok_start = p;
711 645 tok_end = nullptr;
712 646 lex_state = ls_alpha;
713   - }
714   - else if (*p == '-')
715   - {
  647 + } else if (*p == '-') {
716 648 tok_start = p;
717 649 tok_end = nullptr;
718 650 lex_state = ls_number;
... ... @@ -721,9 +653,7 @@ void JSONParser::getToken()
721 653 number_after_e = 0;
722 654 number_saw_point = false;
723 655 number_saw_e = false;
724   - }
725   - else if ((*p >= '0') && (*p <= '9'))
726   - {
  656 + } else if ((*p >= '0') && (*p <= '9')) {
727 657 tok_start = p;
728 658 tok_end = nullptr;
729 659 lex_state = ls_number;
... ... @@ -732,9 +662,7 @@ void JSONParser::getToken()
732 662 number_after_e = 0;
733 663 number_saw_point = false;
734 664 number_saw_e = false;
735   - }
736   - else if (*p == '.')
737   - {
  665 + } else if (*p == '.') {
738 666 tok_start = p;
739 667 tok_end = nullptr;
740 668 lex_state = ls_number;
... ... @@ -743,14 +671,10 @@ void JSONParser::getToken()
743 671 number_after_e = 0;
744 672 number_saw_point = true;
745 673 number_saw_e = false;
746   - }
747   - else if (strchr("{}[]:,", *p))
748   - {
  674 + } else if (strchr("{}[]:,", *p)) {
749 675 tok_start = p;
750 676 tok_end = p + 1;
751   - }
752   - else
753   - {
  677 + } else {
754 678 QTC::TC("libtests", "JSON parse bad character");
755 679 throw std::runtime_error(
756 680 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -758,82 +682,53 @@ void JSONParser::getToken()
758 682 }
759 683 break;
760 684  
761   - case ls_number:
762   - if ((*p >= '0') && (*p <= '9'))
763   - {
764   - if (number_saw_e)
765   - {
  685 + case ls_number:
  686 + if ((*p >= '0') && (*p <= '9')) {
  687 + if (number_saw_e) {
766 688 ++number_after_e;
767   - }
768   - else if (number_saw_point)
769   - {
  689 + } else if (number_saw_point) {
770 690 ++number_after_point;
771   - }
772   - else
773   - {
  691 + } else {
774 692 ++number_before_point;
775 693 }
776   - }
777   - else if (*p == '.')
778   - {
779   - if (number_saw_e)
780   - {
  694 + } else if (*p == '.') {
  695 + if (number_saw_e) {
781 696 QTC::TC("libtests", "JSON parse point after e");
782 697 throw std::runtime_error(
783 698 "JSON: offset " + QUtil::int_to_string(p - cstr) +
784 699 ": numeric literal: decimal point after e");
785   - }
786   - else if (number_saw_point)
787   - {
  700 + } else if (number_saw_point) {
788 701 QTC::TC("libtests", "JSON parse duplicate point");
789 702 throw std::runtime_error(
790 703 "JSON: offset " + QUtil::int_to_string(p - cstr) +
791 704 ": numeric literal: decimal point already seen");
792   - }
793   - else
794   - {
  705 + } else {
795 706 number_saw_point = true;
796 707 }
797   - }
798   - else if (*p == 'e')
799   - {
800   - if (number_saw_e)
801   - {
  708 + } else if (*p == 'e') {
  709 + if (number_saw_e) {
802 710 QTC::TC("libtests", "JSON parse duplicate e");
803 711 throw std::runtime_error(
804 712 "JSON: offset " + QUtil::int_to_string(p - cstr) +
805 713 ": numeric literal: e already seen");
806   - }
807   - else
808   - {
  714 + } else {
809 715 number_saw_e = true;
810 716 }
811   - }
812   - else if ((*p == '+') || (*p == '-'))
813   - {
814   - if (number_saw_e && (number_after_e == 0))
815   - {
  717 + } else if ((*p == '+') || (*p == '-')) {
  718 + if (number_saw_e && (number_after_e == 0)) {
816 719 // okay
817   - }
818   - else
819   - {
  720 + } else {
820 721 QTC::TC("libtests", "JSON parse unexpected sign");
821 722 throw std::runtime_error(
822 723 "JSON: offset " + QUtil::int_to_string(p - cstr) +
823 724 ": numeric literal: unexpected sign");
824 725 }
825   - }
826   - else if (QUtil::is_space(*p))
827   - {
  726 + } else if (QUtil::is_space(*p)) {
828 727 tok_end = p;
829   - }
830   - else if (strchr("{}[]:,", *p))
831   - {
  728 + } else if (strchr("{}[]:,", *p)) {
832 729 tok_end = p;
833 730 --p;
834   - }
835   - else
836   - {
  731 + } else {
837 732 QTC::TC("libtests", "JSON parse numeric bad character");
838 733 throw std::runtime_error(
839 734 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -842,22 +737,15 @@ void JSONParser::getToken()
842 737 }
843 738 break;
844 739  
845   - case ls_alpha:
846   - if ((*p >= 'a') && (*p <= 'z'))
847   - {
  740 + case ls_alpha:
  741 + if ((*p >= 'a') && (*p <= 'z')) {
848 742 // okay
849   - }
850   - else if (QUtil::is_space(*p))
851   - {
  743 + } else if (QUtil::is_space(*p)) {
852 744 tok_end = p;
853   - }
854   - else if (strchr("{}[]:,", *p))
855   - {
  745 + } else if (strchr("{}[]:,", *p)) {
856 746 tok_end = p;
857 747 --p;
858   - }
859   - else
860   - {
  748 + } else {
861 749 QTC::TC("libtests", "JSON parse keyword bad character");
862 750 throw std::runtime_error(
863 751 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -865,36 +753,27 @@ void JSONParser::getToken()
865 753 }
866 754 break;
867 755  
868   - case ls_string:
869   - if (*p == '"')
870   - {
  756 + case ls_string:
  757 + if (*p == '"') {
871 758 tok_end = p + 1;
872   - }
873   - else if (*p == '\\')
874   - {
  759 + } else if (*p == '\\') {
875 760 lex_state = ls_backslash;
876 761 }
877 762 break;
878 763  
879   - case ls_backslash:
  764 + case ls_backslash:
880 765 /* cSpell: ignore bfnrt */
881   - if (strchr("\\\"/bfnrt", *p))
882   - {
  766 + if (strchr("\\\"/bfnrt", *p)) {
883 767 lex_state = ls_string;
884   - }
885   - else if (*p == 'u')
886   - {
887   - if (p + 4 >= end)
888   - {
  768 + } else if (*p == 'u') {
  769 + if (p + 4 >= end) {
889 770 QTC::TC("libtests", "JSON parse premature end of u");
890 771 throw std::runtime_error(
891 772 "JSON: offset " + QUtil::int_to_string(p - cstr) +
892 773 ": \\u must be followed by four characters");
893 774 }
894   - for (size_t i = 1; i <= 4; ++i)
895   - {
896   - if (! QUtil::is_hex_digit(p[i]))
897   - {
  775 + for (size_t i = 1; i <= 4; ++i) {
  776 + if (!QUtil::is_hex_digit(p[i])) {
898 777 QTC::TC("libtests", "JSON parse bad hex after u");
899 778 throw std::runtime_error(
900 779 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -903,9 +782,7 @@ void JSONParser::getToken()
903 782 }
904 783 p += 4;
905 784 lex_state = ls_string;
906   - }
907   - else
908   - {
  785 + } else {
909 786 QTC::TC("libtests", "JSON parse backslash bad character");
910 787 throw std::runtime_error(
911 788 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -915,31 +792,27 @@ void JSONParser::getToken()
915 792 break;
916 793 }
917 794 ++p;
918   - if (tok_start && tok_end)
919   - {
  795 + if (tok_start && tok_end) {
920 796 break;
921 797 }
922 798 }
923   - if (p == end)
924   - {
925   - if (tok_start && (! tok_end))
926   - {
927   - switch (lex_state)
928   - {
929   - case ls_top:
  799 + if (p == end) {
  800 + if (tok_start && (!tok_end)) {
  801 + switch (lex_state) {
  802 + case ls_top:
930 803 // Can't happen
931 804 throw std::logic_error(
932 805 "tok_start set in ls_top while parsing " +
933 806 std::string(cstr));
934 807 break;
935 808  
936   - case ls_number:
937   - case ls_alpha:
  809 + case ls_number:
  810 + case ls_alpha:
938 811 tok_end = p;
939 812 break;
940 813  
941   - case ls_string:
942   - case ls_backslash:
  814 + case ls_string:
  815 + case ls_backslash:
943 816 QTC::TC("libtests", "JSON parse unterminated string");
944 817 throw std::runtime_error(
945 818 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -953,16 +826,14 @@ void JSONParser::getToken()
953 826 void
954 827 JSONParser::handleToken()
955 828 {
956   - if (! (tok_start && tok_end))
957   - {
  829 + if (!(tok_start && tok_end)) {
958 830 return;
959 831 }
960 832  
961 833 // Get token value.
962 834 std::string value(tok_start, tok_end);
963 835  
964   - if (parser_state == ps_done)
965   - {
  836 + if (parser_state == ps_done) {
966 837 QTC::TC("libtests", "JSON parse junk after object");
967 838 throw std::runtime_error(
968 839 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -971,11 +842,9 @@ JSONParser::handleToken()
971 842  
972 843 // Git string value
973 844 std::string s_value;
974   - if (lex_state == ls_string)
975   - {
  845 + if (lex_state == ls_string) {
976 846 // Token includes the quotation marks
977   - if (tok_end - tok_start < 2)
978   - {
  847 + if (tok_end - tok_start < 2) {
979 848 throw std::logic_error("JSON string length < 2");
980 849 }
981 850 s_value = decode_string(value);
... ... @@ -986,28 +855,25 @@ JSONParser::handleToken()
986 855  
987 856 std::shared_ptr<JSON> item;
988 857 char delimiter = '\0';
989   - switch (lex_state)
990   - {
991   - case ls_top:
992   - switch (*tok_start)
993   - {
994   - case '{':
  858 + switch (lex_state) {
  859 + case ls_top:
  860 + switch (*tok_start) {
  861 + case '{':
995 862 item = std::make_shared<JSON>(JSON::makeDictionary());
996 863 break;
997 864  
998   - case '[':
  865 + case '[':
999 866 item = std::make_shared<JSON>(JSON::makeArray());
1000 867 break;
1001 868  
1002   - default:
  869 + default:
1003 870 delimiter = *tok_start;
1004 871 break;
1005 872 }
1006 873 break;
1007 874  
1008   - case ls_number:
1009   - if (number_saw_point && (number_after_point == 0))
1010   - {
  875 + case ls_number:
  876 + if (number_saw_point && (number_after_point == 0)) {
1011 877 QTC::TC("libtests", "JSON parse decimal with no digits");
1012 878 throw std::runtime_error(
1013 879 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -1015,15 +881,13 @@ JSONParser::handleToken()
1015 881 }
1016 882 if ((number_before_point > 1) &&
1017 883 ((tok_start[0] == '0') ||
1018   - ((tok_start[0] == '-') && (tok_start[1] == '0'))))
1019   - {
  884 + ((tok_start[0] == '-') && (tok_start[1] == '0')))) {
1020 885 QTC::TC("libtests", "JSON parse leading zero");
1021 886 throw std::runtime_error(
1022 887 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1023 888 ": number with leading zero");
1024 889 }
1025   - if ((number_before_point == 0) && (number_after_point == 0))
1026   - {
  890 + if ((number_before_point == 0) && (number_after_point == 0)) {
1027 891 QTC::TC("libtests", "JSON parse number no digits");
1028 892 throw std::runtime_error(
1029 893 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -1032,21 +896,14 @@ JSONParser::handleToken()
1032 896 item = std::make_shared<JSON>(JSON::makeNumber(value));
1033 897 break;
1034 898  
1035   - case ls_alpha:
1036   - if (value == "true")
1037   - {
  899 + case ls_alpha:
  900 + if (value == "true") {
1038 901 item = std::make_shared<JSON>(JSON::makeBool(true));
1039   - }
1040   - else if (value == "false")
1041   - {
  902 + } else if (value == "false") {
1042 903 item = std::make_shared<JSON>(JSON::makeBool(false));
1043   - }
1044   - else if (value == "null")
1045   - {
  904 + } else if (value == "null") {
1046 905 item = std::make_shared<JSON>(JSON::makeNull());
1047   - }
1048   - else
1049   - {
  906 + } else {
1050 907 QTC::TC("libtests", "JSON parse invalid keyword");
1051 908 throw std::runtime_error(
1052 909 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -1054,18 +911,16 @@ JSONParser::handleToken()
1054 911 }
1055 912 break;
1056 913  
1057   - case ls_string:
  914 + case ls_string:
1058 915 item = std::make_shared<JSON>(JSON::makeString(s_value));
1059 916 break;
1060 917  
1061   - case ls_backslash:
1062   - throw std::logic_error(
1063   - "tok_end is set while state = ls_backslash");
  918 + case ls_backslash:
  919 + throw std::logic_error("tok_end is set while state = ls_backslash");
1064 920 break;
1065 921 }
1066 922  
1067   - if ((item.get() == nullptr) == (delimiter == '\0'))
1068   - {
  923 + if ((item.get() == nullptr) == (delimiter == '\0')) {
1069 924 throw std::logic_error(
1070 925 "JSONParser::handleToken: logic error: exactly one of item"
1071 926 " or delimiter must be set");
... ... @@ -1073,39 +928,36 @@ JSONParser::handleToken()
1073 928  
1074 929 // See whether what we have is allowed at this point.
1075 930  
1076   - if (item.get())
1077   - {
1078   - switch (parser_state)
1079   - {
1080   - case ps_done:
  931 + if (item.get()) {
  932 + switch (parser_state) {
  933 + case ps_done:
1081 934 throw std::logic_error("can't happen; ps_done already handled");
1082 935 break;
1083 936  
1084   - case ps_dict_after_key:
  937 + case ps_dict_after_key:
1085 938 QTC::TC("libtests", "JSON parse expected colon");
1086 939 throw std::runtime_error(
1087 940 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1088 941 ": expected ':'");
1089 942 break;
1090 943  
1091   - case ps_dict_after_item:
  944 + case ps_dict_after_item:
1092 945 QTC::TC("libtests", "JSON parse expected , or }");
1093 946 throw std::runtime_error(
1094 947 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1095 948 ": expected ',' or '}'");
1096 949 break;
1097 950  
1098   - case ps_array_after_item:
  951 + case ps_array_after_item:
1099 952 QTC::TC("libtests", "JSON parse expected, or ]");
1100 953 throw std::runtime_error(
1101 954 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1102 955 ": expected ',' or ']'");
1103 956 break;
1104 957  
1105   - case ps_dict_begin:
1106   - case ps_dict_after_comma:
1107   - if (lex_state != ls_string)
1108   - {
  958 + case ps_dict_begin:
  959 + case ps_dict_after_comma:
  960 + if (lex_state != ls_string) {
1109 961 QTC::TC("libtests", "JSON parse string as dict key");
1110 962 throw std::runtime_error(
1111 963 "JSON: offset " + QUtil::int_to_string(p - cstr) +
... ... @@ -1113,18 +965,16 @@ JSONParser::handleToken()
1113 965 }
1114 966 break;
1115 967  
1116   - case ps_top:
1117   - case ps_dict_after_colon:
1118   - case ps_array_begin:
1119   - case ps_array_after_comma:
  968 + case ps_top:
  969 + case ps_dict_after_colon:
  970 + case ps_array_begin:
  971 + case ps_array_after_comma:
1120 972 break;
1121 973 // okay
1122 974 }
1123   - }
1124   - else if (delimiter == '}')
1125   - {
1126   - if (! ((parser_state == ps_dict_begin) ||
1127   - (parser_state == ps_dict_after_item)))
  975 + } else if (delimiter == '}') {
  976 + if (!((parser_state == ps_dict_begin) ||
  977 + (parser_state == ps_dict_after_item)))
1128 978  
1129 979 {
1130 980 QTC::TC("libtests", "JSON parse unexpected }");
... ... @@ -1132,11 +982,9 @@ JSONParser::handleToken()
1132 982 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1133 983 ": unexpected dictionary end delimiter");
1134 984 }
1135   - }
1136   - else if (delimiter == ']')
1137   - {
1138   - if (! ((parser_state == ps_array_begin) ||
1139   - (parser_state == ps_array_after_item)))
  985 + } else if (delimiter == ']') {
  986 + if (!((parser_state == ps_array_begin) ||
  987 + (parser_state == ps_array_after_item)))
1140 988  
1141 989 {
1142 990 QTC::TC("libtests", "JSON parse unexpected ]");
... ... @@ -1144,30 +992,22 @@ JSONParser::handleToken()
1144 992 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1145 993 ": unexpected array end delimiter");
1146 994 }
1147   - }
1148   - else if (delimiter == ':')
1149   - {
1150   - if (parser_state != ps_dict_after_key)
1151   - {
  995 + } else if (delimiter == ':') {
  996 + if (parser_state != ps_dict_after_key) {
1152 997 QTC::TC("libtests", "JSON parse unexpected :");
1153 998 throw std::runtime_error(
1154 999 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1155 1000 ": unexpected colon");
1156 1001 }
1157   - }
1158   - else if (delimiter == ',')
1159   - {
1160   - if (! ((parser_state == ps_dict_after_item) ||
1161   - (parser_state == ps_array_after_item)))
1162   - {
  1002 + } else if (delimiter == ',') {
  1003 + if (!((parser_state == ps_dict_after_item) ||
  1004 + (parser_state == ps_array_after_item))) {
1163 1005 QTC::TC("libtests", "JSON parse unexpected ,");
1164 1006 throw std::runtime_error(
1165 1007 "JSON: offset " + QUtil::int_to_string(p - cstr) +
1166 1008 ": unexpected comma");
1167 1009 }
1168   - }
1169   - else if (delimiter != '\0')
1170   - {
  1010 + } else if (delimiter != '\0') {
1171 1011 throw std::logic_error("JSONParser::handleToken: bad delimiter");
1172 1012 }
1173 1013  
... ... @@ -1175,103 +1015,77 @@ JSONParser::handleToken()
1175 1015 // whatever we need to do with it.
1176 1016  
1177 1017 parser_state_e next_state = ps_top;
1178   - if (delimiter == ':')
1179   - {
  1018 + if (delimiter == ':') {
1180 1019 next_state = ps_dict_after_colon;
1181   - }
1182   - else if (delimiter == ',')
1183   - {
1184   - if (parser_state == ps_dict_after_item)
1185   - {
  1020 + } else if (delimiter == ',') {
  1021 + if (parser_state == ps_dict_after_item) {
1186 1022 next_state = ps_dict_after_comma;
1187   - }
1188   - else if (parser_state == ps_array_after_item)
1189   - {
  1023 + } else if (parser_state == ps_array_after_item) {
1190 1024 next_state = ps_array_after_comma;
  1025 + } else {
  1026 + throw std::logic_error("JSONParser::handleToken: unexpected parser"
  1027 + " state for comma");
1191 1028 }
1192   - else
1193   - {
1194   - throw std::logic_error(
1195   - "JSONParser::handleToken: unexpected parser"
1196   - " state for comma");
1197   - }
1198   - }
1199   - else if ((delimiter == '}') || (delimiter == ']'))
1200   - {
  1029 + } else if ((delimiter == '}') || (delimiter == ']')) {
1201 1030 next_state = ps_stack.back();
1202 1031 ps_stack.pop_back();
1203   - if (next_state != ps_done)
1204   - {
  1032 + if (next_state != ps_done) {
1205 1033 stack.pop_back();
1206 1034 }
1207   - }
1208   - else if (delimiter != '\0')
1209   - {
  1035 + } else if (delimiter != '\0') {
1210 1036 throw std::logic_error(
1211 1037 "JSONParser::handleToken: unexpected delimiter in transition");
1212   - }
1213   - else if (item.get())
1214   - {
  1038 + } else if (item.get()) {
1215 1039 std::shared_ptr<JSON> tos;
1216   - if (! stack.empty())
1217   - {
  1040 + if (!stack.empty()) {
1218 1041 tos = stack.back();
1219 1042 }
1220   - switch (parser_state)
1221   - {
1222   - case ps_dict_begin:
1223   - case ps_dict_after_comma:
  1043 + switch (parser_state) {
  1044 + case ps_dict_begin:
  1045 + case ps_dict_after_comma:
1224 1046 this->dict_key = s_value;
1225 1047 item = nullptr;
1226 1048 next_state = ps_dict_after_key;
1227 1049 break;
1228 1050  
1229   - case ps_dict_after_colon:
  1051 + case ps_dict_after_colon:
1230 1052 tos->addDictionaryMember(dict_key, *item);
1231 1053 next_state = ps_dict_after_item;
1232 1054 break;
1233 1055  
1234   - case ps_array_begin:
1235   - case ps_array_after_comma:
  1056 + case ps_array_begin:
  1057 + case ps_array_after_comma:
1236 1058 next_state = ps_array_after_item;
1237 1059 tos->addArrayElement(*item);
1238 1060 break;
1239 1061  
1240   - case ps_top:
  1062 + case ps_top:
1241 1063 next_state = ps_done;
1242 1064 break;
1243 1065  
1244   - case ps_dict_after_key:
1245   - case ps_dict_after_item:
1246   - case ps_array_after_item:
1247   - case ps_done:
  1066 + case ps_dict_after_key:
  1067 + case ps_dict_after_item:
  1068 + case ps_array_after_item:
  1069 + case ps_done:
1248 1070 throw std::logic_error(
1249 1071 "JSONParser::handleToken: unexpected parser state");
1250 1072 }
1251   - }
1252   - else
1253   - {
  1073 + } else {
1254 1074 throw std::logic_error(
1255 1075 "JSONParser::handleToken: unexpected null item in transition");
1256 1076 }
1257 1077  
1258 1078 // Prepare for next token
1259   - if (item.get())
1260   - {
1261   - if (item->isDictionary())
1262   - {
  1079 + if (item.get()) {
  1080 + if (item->isDictionary()) {
1263 1081 stack.push_back(item);
1264 1082 ps_stack.push_back(next_state);
1265 1083 next_state = ps_dict_begin;
1266   - }
1267   - else if (item->isArray())
1268   - {
  1084 + } else if (item->isArray()) {
1269 1085 stack.push_back(item);
1270 1086 ps_stack.push_back(next_state);
1271 1087 next_state = ps_array_begin;
1272   - }
1273   - else if (parser_state == ps_top)
1274   - {
  1088 + } else if (parser_state == ps_top) {
1275 1089 stack.push_back(item);
1276 1090 }
1277 1091 }
... ... @@ -1288,13 +1102,11 @@ JSONParser::parse(std::string const&amp; s)
1288 1102 end = cstr + s.length();
1289 1103 p = cstr;
1290 1104  
1291   - while (p < end)
1292   - {
  1105 + while (p < end) {
1293 1106 getToken();
1294 1107 handleToken();
1295 1108 }
1296   - if (parser_state != ps_done)
1297   - {
  1109 + if (parser_state != ps_done) {
1298 1110 QTC::TC("libtests", "JSON parse premature EOF");
1299 1111 throw std::runtime_error("JSON: premature end of input");
1300 1112 }
... ...
libqpdf/JSONHandler.cc
1 1 #include <qpdf/JSONHandler.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4   -#include <qpdf/QTC.hh>
5 3 #include <qpdf/QPDFUsage.hh>
  4 +#include <qpdf/QTC.hh>
  5 +#include <qpdf/QUtil.hh>
6 6  
7 7 JSONHandler::JSONHandler() :
8 8 m(new Members())
... ... @@ -70,9 +70,10 @@ JSONHandler::addFallbackDictHandler(std::shared_ptr&lt;JSONHandler&gt; fdh)
70 70 }
71 71  
72 72 void
73   -JSONHandler::addArrayHandlers(json_handler_t start_fn,
74   - void_handler_t end_fn,
75   - std::shared_ptr<JSONHandler> ah)
  73 +JSONHandler::addArrayHandlers(
  74 + json_handler_t start_fn,
  75 + void_handler_t end_fn,
  76 + std::shared_ptr<JSONHandler> ah)
76 77 {
77 78 this->m->h.array_start_handler = start_fn;
78 79 this->m->h.array_end_handler = end_fn;
... ... @@ -82,69 +83,55 @@ JSONHandler::addArrayHandlers(json_handler_t start_fn,
82 83 void
83 84 JSONHandler::handle(std::string const& path, JSON j)
84 85 {
85   - if (this->m->h.any_handler)
86   - {
  86 + if (this->m->h.any_handler) {
87 87 this->m->h.any_handler(path, j);
88 88 return;
89 89 }
90 90 bool handled = false;
91 91 bool bvalue = false;
92 92 std::string s_value;
93   - if (this->m->h.null_handler && j.isNull())
94   - {
  93 + if (this->m->h.null_handler && j.isNull()) {
95 94 this->m->h.null_handler(path);
96 95 handled = true;
97 96 }
98   - if (this->m->h.string_handler && j.getString(s_value))
99   - {
  97 + if (this->m->h.string_handler && j.getString(s_value)) {
100 98 this->m->h.string_handler(path, s_value);
101 99 handled = true;
102 100 }
103   - if (this->m->h.number_handler && j.getNumber(s_value))
104   - {
  101 + if (this->m->h.number_handler && j.getNumber(s_value)) {
105 102 this->m->h.number_handler(path, s_value);
106 103 handled = true;
107 104 }
108   - if (this->m->h.bool_handler && j.getBool(bvalue))
109   - {
  105 + if (this->m->h.bool_handler && j.getBool(bvalue)) {
110 106 this->m->h.bool_handler(path, bvalue);
111 107 handled = true;
112 108 }
113   - if (this->m->h.dict_start_handler && j.isDictionary())
114   - {
  109 + if (this->m->h.dict_start_handler && j.isDictionary()) {
115 110 this->m->h.dict_start_handler(path, j);
116 111 std::string path_base = path;
117   - if (path_base != ".")
118   - {
  112 + if (path_base != ".") {
119 113 path_base += ".";
120 114 }
121 115 j.forEachDictItem([&path, &path_base, this](
122 116 std::string const& k, JSON v) {
123 117 auto i = this->m->h.dict_handlers.find(k);
124   - if (i == this->m->h.dict_handlers.end())
125   - {
126   - if (this->m->h.fallback_dict_handler.get())
127   - {
128   - this->m->h.fallback_dict_handler->handle(
129   - path_base + k, v);
130   - }
131   - else
132   - {
  118 + if (i == this->m->h.dict_handlers.end()) {
  119 + if (this->m->h.fallback_dict_handler.get()) {
  120 + this->m->h.fallback_dict_handler->handle(path_base + k, v);
  121 + } else {
133 122 QTC::TC("libtests", "JSONHandler unexpected key");
134   - usage("JSON handler found unexpected key " + k +
135   - " in object at " + path);
  123 + usage(
  124 + "JSON handler found unexpected key " + k +
  125 + " in object at " + path);
136 126 }
137   - }
138   - else
139   - {
  127 + } else {
140 128 i->second->handle(path_base + k, v);
141 129 }
142 130 });
143 131 this->m->h.dict_end_handler(path);
144 132 handled = true;
145 133 }
146   - if (this->m->h.array_start_handler && j.isArray())
147   - {
  134 + if (this->m->h.array_start_handler && j.isArray()) {
148 135 this->m->h.array_start_handler(path, j);
149 136 size_t i = 0;
150 137 j.forEachArrayItem([&i, &path, this](JSON v) {
... ... @@ -156,8 +143,7 @@ JSONHandler::handle(std::string const&amp; path, JSON j)
156 143 handled = true;
157 144 }
158 145  
159   - if (! handled)
160   - {
  146 + if (!handled) {
161 147 // It would be nice to include information about what type the
162 148 // object was and what types were allowed, but we're relying
163 149 // on schema validation to make sure input is properly
... ...
libqpdf/MD5.cc
1 1 #include <qpdf/MD5.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4 3 #include <qpdf/QIntC.hh>
5 4 #include <qpdf/QPDFCryptoProvider.hh>
  5 +#include <qpdf/QUtil.hh>
6 6  
7   -#include <stdio.h>
  7 +#include <errno.h>
8 8 #include <memory.h>
  9 +#include <stdio.h>
9 10 #include <stdlib.h>
10 11 #include <string.h>
11   -#include <errno.h>
12 12  
13 13 MD5::MD5()
14 14 {
... ... @@ -28,12 +28,14 @@ MD5::finalize()
28 28 this->crypto->MD5_finalize();
29 29 }
30 30  
31   -void MD5::reset()
  31 +void
  32 +MD5::reset()
32 33 {
33 34 init();
34 35 }
35 36  
36   -void MD5::encodeString(char const* str)
  37 +void
  38 +MD5::encodeString(char const* str)
37 39 {
38 40 size_t len = strlen(str);
39 41 crypto->MD5_init();
... ... @@ -41,79 +43,78 @@ void MD5::encodeString(char const* str)
41 43 crypto->MD5_finalize();
42 44 }
43 45  
44   -void MD5::appendString(char const* input_string)
  46 +void
  47 +MD5::appendString(char const* input_string)
45 48 {
46 49 encodeDataIncrementally(input_string, strlen(input_string));
47 50 }
48 51  
49   -void MD5::encodeDataIncrementally(char const* data, size_t len)
  52 +void
  53 +MD5::encodeDataIncrementally(char const* data, size_t len)
50 54 {
51 55 this->crypto->MD5_update(QUtil::unsigned_char_pointer(data), len);
52 56 }
53 57  
54   -void MD5::encodeFile(char const *filename, qpdf_offset_t up_to_offset)
  58 +void
  59 +MD5::encodeFile(char const* filename, qpdf_offset_t up_to_offset)
55 60 {
56 61 char buffer[1024];
57 62  
58   - FILE *file = QUtil::safe_fopen(filename, "rb");
  63 + FILE* file = QUtil::safe_fopen(filename, "rb");
59 64 size_t len;
60 65 size_t so_far = 0;
61 66 size_t to_try = 1024;
62 67 size_t up_to_size = 0;
63   - if (up_to_offset >= 0)
64   - {
  68 + if (up_to_offset >= 0) {
65 69 up_to_size = QIntC::to_size(up_to_offset);
66 70 }
67   - do
68   - {
69   - if ((up_to_offset >= 0) && ((so_far + to_try) > up_to_size))
70   - {
  71 + do {
  72 + if ((up_to_offset >= 0) && ((so_far + to_try) > up_to_size)) {
71 73 to_try = up_to_size - so_far;
72 74 }
73 75 len = fread(buffer, 1, to_try, file);
74   - if (len > 0)
75   - {
  76 + if (len > 0) {
76 77 encodeDataIncrementally(buffer, len);
77 78 so_far += len;
78   - if ((up_to_offset >= 0) && (so_far >= up_to_size))
79   - {
  79 + if ((up_to_offset >= 0) && (so_far >= up_to_size)) {
80 80 break;
81 81 }
82 82 }
83 83 } while (len > 0);
84   - if (ferror(file))
85   - {
  84 + if (ferror(file)) {
86 85 // Assume, perhaps incorrectly, that errno was set by the
87 86 // underlying call to read....
88   - (void) fclose(file);
  87 + (void)fclose(file);
89 88 QUtil::throw_system_error(
90 89 std::string("MD5: read error on ") + filename);
91 90 }
92   - (void) fclose(file);
  91 + (void)fclose(file);
93 92  
94 93 this->crypto->MD5_finalize();
95 94 }
96 95  
97   -void MD5::digest(Digest result)
  96 +void
  97 +MD5::digest(Digest result)
98 98 {
99 99 this->crypto->MD5_finalize();
100 100 this->crypto->MD5_digest(result);
101 101 }
102 102  
103   -void MD5::print()
  103 +void
  104 +MD5::print()
104 105 {
105 106 Digest digest_val;
106 107 digest(digest_val);
107 108  
108 109 unsigned int i;
109   - for (i = 0; i < 16; ++i)
110   - {
  110 + for (i = 0; i < 16; ++i) {
111 111 printf("%02x", digest_val[i]);
112 112 }
113 113 printf("\n");
114 114 }
115 115  
116   -std::string MD5::unparse()
  116 +std::string
  117 +MD5::unparse()
117 118 {
118 119 this->crypto->MD5_finalize();
119 120 Digest digest_val;
... ... @@ -139,25 +140,23 @@ MD5::getFileChecksum(char const* filename, qpdf_offset_t up_to_offset)
139 140 }
140 141  
141 142 bool
142   -MD5::checkDataChecksum(char const* const checksum,
143   - char const* buf, size_t len)
  143 +MD5::checkDataChecksum(char const* const checksum, char const* buf, size_t len)
144 144 {
145 145 std::string actual_checksum = getDataChecksum(buf, len);
146 146 return (checksum == actual_checksum);
147 147 }
148 148  
149 149 bool
150   -MD5::checkFileChecksum(char const* const checksum,
151   - char const* filename, qpdf_offset_t up_to_offset)
  150 +MD5::checkFileChecksum(
  151 + char const* const checksum,
  152 + char const* filename,
  153 + qpdf_offset_t up_to_offset)
152 154 {
153 155 bool result = false;
154   - try
155   - {
  156 + try {
156 157 std::string actual_checksum = getFileChecksum(filename, up_to_offset);
157 158 result = (checksum == actual_checksum);
158   - }
159   - catch (std::runtime_error const&)
160   - {
  159 + } catch (std::runtime_error const&) {
161 160 // Ignore -- return false
162 161 }
163 162 return result;
... ...
libqpdf/NNTree.cc
... ... @@ -9,8 +9,7 @@ static std::string
9 9 get_description(QPDFObjectHandle& node)
10 10 {
11 11 std::string result("Name/Number tree node");
12   - if (node.isIndirect())
13   - {
  12 + if (node.isIndirect()) {
14 13 result += " (object " + QUtil::int_to_string(node.getObjectID()) + ")";
15 14 }
16 15 return result;
... ... @@ -21,11 +20,13 @@ warn(QPDF* qpdf, QPDFObjectHandle&amp; node, std::string const&amp; msg)
21 20 {
22 21 // ABI: in qpdf 11, change to a reference.
23 22  
24   - if (qpdf)
25   - {
  23 + if (qpdf) {
26 24 qpdf->warn(QPDFExc(
27   - qpdf_e_damaged_pdf,
28   - qpdf->getFilename(), get_description(node), 0, msg));
  25 + qpdf_e_damaged_pdf,
  26 + qpdf->getFilename(),
  27 + get_description(node),
  28 + 0,
  29 + msg));
29 30 }
30 31 }
31 32  
... ... @@ -34,13 +35,14 @@ error(QPDF* qpdf, QPDFObjectHandle&amp; node, std::string const&amp; msg)
34 35 {
35 36 // ABI: in qpdf 11, change to a reference.
36 37  
37   - if (qpdf)
38   - {
39   - throw QPDFExc(qpdf_e_damaged_pdf,
40   - qpdf->getFilename(), get_description(node), 0, msg);
41   - }
42   - else
43   - {
  38 + if (qpdf) {
  39 + throw QPDFExc(
  40 + qpdf_e_damaged_pdf,
  41 + qpdf->getFilename(),
  42 + get_description(node),
  43 + 0,
  44 + msg);
  45 + } else {
44 46 throw std::runtime_error(get_description(node) + ": " + msg);
45 47 }
46 48 }
... ... @@ -73,29 +75,21 @@ NNTreeIterator::updateIValue(bool allow_invalid)
73 75 // measure, we also call updateIValue in operator* and operator->.
74 76  
75 77 bool okay = false;
76   - if ((item_number >= 0) &&
77   - this->node.isInitialized() &&
78   - this->node.isDictionary())
79   - {
  78 + if ((item_number >= 0) && this->node.isInitialized() &&
  79 + this->node.isDictionary()) {
80 80 auto items = this->node.getKey(impl.details.itemsKey());
81   - if (this->item_number + 1 < items.getArrayNItems())
82   - {
  81 + if (this->item_number + 1 < items.getArrayNItems()) {
83 82 okay = true;
84 83 this->ivalue.first = items.getArrayItem(this->item_number);
85   - this->ivalue.second = items.getArrayItem(1+this->item_number);
86   - }
87   - else
88   - {
  84 + this->ivalue.second = items.getArrayItem(1 + this->item_number);
  85 + } else {
89 86 error(impl.qpdf, node, "update ivalue: items array is too short");
90 87 }
91 88 }
92   - if (! okay)
93   - {
94   - if (! allow_invalid)
95   - {
96   - throw std::logic_error(
97   - "attempt made to dereference an invalid"
98   - " name/number tree iterator");
  89 + if (!okay) {
  90 + if (!allow_invalid) {
  91 + throw std::logic_error("attempt made to dereference an invalid"
  92 + " name/number tree iterator");
99 93 }
100 94 this->ivalue.first = QPDFObjectHandle();
101 95 this->ivalue.second = QPDFObjectHandle();
... ... @@ -114,29 +108,24 @@ NNTreeIterator::getNextKid(PathElement&amp; pe, bool backward)
114 108 {
115 109 QPDFObjectHandle result;
116 110 bool found = false;
117   - while (! found)
118   - {
  111 + while (!found) {
119 112 pe.kid_number += backward ? -1 : 1;
120 113 auto kids = pe.node.getKey("/Kids");
121   - if ((pe.kid_number >= 0) && (pe.kid_number < kids.getArrayNItems()))
122   - {
  114 + if ((pe.kid_number >= 0) && (pe.kid_number < kids.getArrayNItems())) {
123 115 result = kids.getArrayItem(pe.kid_number);
124 116 if (result.isDictionary() &&
125 117 (result.hasKey("/Kids") ||
126   - result.hasKey(impl.details.itemsKey())))
127   - {
  118 + result.hasKey(impl.details.itemsKey()))) {
128 119 found = true;
129   - }
130   - else
131   - {
  120 + } else {
132 121 QTC::TC("qpdf", "NNTree skip invalid kid");
133   - warn(impl.qpdf, pe.node,
134   - "skipping over invalid kid at index " +
135   - QUtil::int_to_string(pe.kid_number));
  122 + warn(
  123 + impl.qpdf,
  124 + pe.node,
  125 + "skipping over invalid kid at index " +
  126 + QUtil::int_to_string(pe.kid_number));
136 127 }
137   - }
138   - else
139   - {
  128 + } else {
140 129 result = QPDFObjectHandle::newNull();
141 130 found = true;
142 131 }
... ... @@ -153,55 +142,46 @@ NNTreeIterator::valid() const
153 142 void
154 143 NNTreeIterator::increment(bool backward)
155 144 {
156   - if (this->item_number < 0)
157   - {
  145 + if (this->item_number < 0) {
158 146 QTC::TC("qpdf", "NNTree increment end()");
159   - deepen(impl.oh, ! backward, true);
  147 + deepen(impl.oh, !backward, true);
160 148 return;
161 149 }
162 150 bool found_valid_key = false;
163   - while (valid() && (! found_valid_key))
164   - {
  151 + while (valid() && (!found_valid_key)) {
165 152 this->item_number += backward ? -2 : 2;
166 153 auto items = this->node.getKey(impl.details.itemsKey());
167 154 if ((this->item_number < 0) ||
168   - (this->item_number >= items.getArrayNItems()))
169   - {
  155 + (this->item_number >= items.getArrayNItems())) {
170 156 bool found = false;
171 157 setItemNumber(QPDFObjectHandle(), -1);
172   - while (! (found || this->path.empty()))
173   - {
  158 + while (!(found || this->path.empty())) {
174 159 auto& element = this->path.back();
175 160 auto pe_node = getNextKid(element, backward);
176   - if (pe_node.isNull())
177   - {
  161 + if (pe_node.isNull()) {
178 162 this->path.pop_back();
179   - }
180   - else
181   - {
182   - found = deepen(pe_node, ! backward, false);
  163 + } else {
  164 + found = deepen(pe_node, !backward, false);
183 165 }
184 166 }
185 167 }
186   - if (this->item_number >= 0)
187   - {
  168 + if (this->item_number >= 0) {
188 169 items = this->node.getKey(impl.details.itemsKey());
189   - if (this->item_number + 1 >= items.getArrayNItems())
190   - {
  170 + if (this->item_number + 1 >= items.getArrayNItems()) {
191 171 QTC::TC("qpdf", "NNTree skip item at end of short items");
192   - warn(impl.qpdf, this->node,
193   - "items array doesn't have enough elements");
194   - }
195   - else if (! impl.details.keyValid(
196   - items.getArrayItem(this->item_number)))
197   - {
  172 + warn(
  173 + impl.qpdf,
  174 + this->node,
  175 + "items array doesn't have enough elements");
  176 + } else if (!impl.details.keyValid(
  177 + items.getArrayItem(this->item_number))) {
198 178 QTC::TC("qpdf", "NNTree skip invalid key");
199   - warn(impl.qpdf, this->node,
200   - "item " + QUtil::int_to_string(this->item_number) +
201   - " has the wrong type");
202   - }
203   - else
204   - {
  179 + warn(
  180 + impl.qpdf,
  181 + this->node,
  182 + "item " + QUtil::int_to_string(this->item_number) +
  183 + " has the wrong type");
  184 + } else {
205 185 found_valid_key = true;
206 186 }
207 187 }
... ... @@ -209,14 +189,12 @@ NNTreeIterator::increment(bool backward)
209 189 }
210 190  
211 191 void
212   -NNTreeIterator::resetLimits(QPDFObjectHandle node,
213   - std::list<PathElement>::iterator parent)
  192 +NNTreeIterator::resetLimits(
  193 + QPDFObjectHandle node, std::list<PathElement>::iterator parent)
214 194 {
215 195 bool done = false;
216   - while (! done)
217   - {
218   - if (parent == this->path.end())
219   - {
  196 + while (!done) {
  197 + if (parent == this->path.end()) {
220 198 QTC::TC("qpdf", "NNTree remove limits from root");
221 199 node.removeKey("/Limits");
222 200 done = true;
... ... @@ -230,65 +208,51 @@ NNTreeIterator::resetLimits(QPDFObjectHandle node,
230 208 bool changed = true;
231 209 QPDFObjectHandle first;
232 210 QPDFObjectHandle last;
233   - if (nitems >= 2)
234   - {
  211 + if (nitems >= 2) {
235 212 first = items.getArrayItem(0);
236 213 last = items.getArrayItem((nitems - 1) & ~1);
237   - }
238   - else if (nkids > 0)
239   - {
  214 + } else if (nkids > 0) {
240 215 auto first_kid = kids.getArrayItem(0);
241 216 auto last_kid = kids.getArrayItem(nkids - 1);
242   - if (first_kid.isDictionary() && last_kid.isDictionary())
243   - {
  217 + if (first_kid.isDictionary() && last_kid.isDictionary()) {
244 218 auto first_limits = first_kid.getKey("/Limits");
245 219 auto last_limits = last_kid.getKey("/Limits");
246 220 if (first_limits.isArray() &&
247 221 (first_limits.getArrayNItems() >= 2) &&
248 222 last_limits.isArray() &&
249   - (last_limits.getArrayNItems() >= 2))
250   - {
  223 + (last_limits.getArrayNItems() >= 2)) {
251 224 first = first_limits.getArrayItem(0);
252 225 last = last_limits.getArrayItem(1);
253 226 }
254 227 }
255 228 }
256   - if (first.isInitialized() && last.isInitialized())
257   - {
  229 + if (first.isInitialized() && last.isInitialized()) {
258 230 auto limits = QPDFObjectHandle::newArray();
259 231 limits.appendItem(first);
260 232 limits.appendItem(last);
261 233 auto olimits = node.getKey("/Limits");
262   - if (olimits.isArray() && (olimits.getArrayNItems() == 2))
263   - {
  234 + if (olimits.isArray() && (olimits.getArrayNItems() == 2)) {
264 235 auto ofirst = olimits.getArrayItem(0);
265 236 auto olast = olimits.getArrayItem(1);
266 237 if (impl.details.keyValid(ofirst) &&
267 238 impl.details.keyValid(olast) &&
268 239 (impl.details.compareKeys(first, ofirst) == 0) &&
269   - (impl.details.compareKeys(last, olast) == 0))
270   - {
  240 + (impl.details.compareKeys(last, olast) == 0)) {
271 241 QTC::TC("qpdf", "NNTree limits didn't change");
272 242 changed = false;
273 243 }
274 244 }
275   - if (changed)
276   - {
  245 + if (changed) {
277 246 node.replaceKey("/Limits", limits);
278 247 }
279   - }
280   - else
281   - {
  248 + } else {
282 249 QTC::TC("qpdf", "NNTree unable to determine limits");
283 250 warn(impl.qpdf, node, "unable to determine limits");
284 251 }
285 252  
286   - if ((! changed) || (parent == this->path.begin()))
287   - {
  253 + if ((!changed) || (parent == this->path.begin())) {
288 254 done = true;
289   - }
290   - else
291   - {
  255 + } else {
292 256 node = parent->node;
293 257 --parent;
294 258 }
... ... @@ -296,8 +260,8 @@ NNTreeIterator::resetLimits(QPDFObjectHandle node,
296 260 }
297 261  
298 262 void
299   -NNTreeIterator::split(QPDFObjectHandle to_split,
300   - std::list<PathElement>::iterator parent)
  263 +NNTreeIterator::split(
  264 + QPDFObjectHandle to_split, std::list<PathElement>::iterator parent)
301 265 {
302 266 // Split some node along the path to the item pointed to by this
303 267 // iterator, and adjust the iterator so it points to the same
... ... @@ -327,13 +291,10 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
327 291 // node: A
328 292 // item_number: 0
329 293  
330   - if (! this->impl.qpdf)
331   - {
332   - throw std::logic_error(
333   - "NNTreeIterator::split called with null qpdf");
  294 + if (!this->impl.qpdf) {
  295 + throw std::logic_error("NNTreeIterator::split called with null qpdf");
334 296 }
335   - if (! valid())
336   - {
  297 + if (!valid()) {
337 298 throw std::logic_error(
338 299 "NNTreeIterator::split called an invalid iterator");
339 300 }
... ... @@ -349,29 +310,23 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
349 310 int n = 0;
350 311 std::string key;
351 312 int threshold = 0;
352   - if (nkids > 0)
353   - {
  313 + if (nkids > 0) {
354 314 QTC::TC("qpdf", "NNTree split kids");
355 315 first_half = kids;
356 316 n = nkids;
357 317 threshold = impl.split_threshold;
358 318 key = "/Kids";
359   - }
360   - else if (nitems > 0)
361   - {
  319 + } else if (nitems > 0) {
362 320 QTC::TC("qpdf", "NNTree split items");
363 321 first_half = items;
364 322 n = nitems;
365 323 threshold = 2 * impl.split_threshold;
366 324 key = impl.details.itemsKey();
367   - }
368   - else
369   - {
  325 + } else {
370 326 throw std::logic_error("NNTreeIterator::split called on invalid node");
371 327 }
372 328  
373   - if (n <= threshold)
374   - {
  329 + if (n <= threshold) {
375 330 return;
376 331 }
377 332  
... ... @@ -381,8 +336,7 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
381 336 // CURRENT STATE: tree is in original state; iterator is valid and
382 337 // unchanged.
383 338  
384   - if (is_root)
385   - {
  339 + if (is_root) {
386 340 // What we want to do is to create a new node for the second
387 341 // half of the items and put it in the parent's /Kids array
388 342 // right after the element that points to the current to_split
... ... @@ -404,21 +358,18 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
404 358 // non-root case so remaining logic can handle them in the
405 359 // same way.
406 360  
407   - auto first_node = impl.qpdf->makeIndirectObject(
408   - QPDFObjectHandle::newDictionary());
  361 + auto first_node =
  362 + impl.qpdf->makeIndirectObject(QPDFObjectHandle::newDictionary());
409 363 first_node.replaceKey(key, first_half);
410 364 QPDFObjectHandle new_kids = QPDFObjectHandle::newArray();
411 365 new_kids.appendItem(first_node);
412 366 to_split.removeKey("/Limits"); // already shouldn't be there for root
413 367 to_split.removeKey(impl.details.itemsKey());
414 368 to_split.replaceKey("/Kids", new_kids);
415   - if (is_leaf)
416   - {
  369 + if (is_leaf) {
417 370 QTC::TC("qpdf", "NNTree split root + leaf");
418 371 this->node = first_node;
419   - }
420   - else
421   - {
  372 + } else {
422 373 QTC::TC("qpdf", "NNTree split root + !leaf");
423 374 auto next = this->path.begin();
424 375 next->node = first_node;
... ... @@ -436,16 +387,15 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
436 387 // items into the second half array.
437 388 QPDFObjectHandle second_half = QPDFObjectHandle::newArray();
438 389 int start_idx = ((n / 2) & ~1);
439   - while (first_half.getArrayNItems() > start_idx)
440   - {
  390 + while (first_half.getArrayNItems() > start_idx) {
441 391 second_half.appendItem(first_half.getArrayItem(start_idx));
442 392 first_half.eraseItem(start_idx);
443 393 }
444 394 resetLimits(to_split, parent);
445 395  
446 396 // Create a new node to contain the second half
447   - QPDFObjectHandle second_node = impl.qpdf->makeIndirectObject(
448   - QPDFObjectHandle::newDictionary());
  397 + QPDFObjectHandle second_node =
  398 + impl.qpdf->makeIndirectObject(QPDFObjectHandle::newDictionary());
449 399 second_node.replaceKey(key, second_half);
450 400 resetLimits(second_node, parent);
451 401  
... ... @@ -464,23 +414,18 @@ NNTreeIterator::split(QPDFObjectHandle to_split,
464 414 auto cur_elem = parent;
465 415 ++cur_elem; // points to end() for leaf nodes
466 416 int old_idx = (is_leaf ? this->item_number : cur_elem->kid_number);
467   - if (old_idx >= start_idx)
468   - {
  417 + if (old_idx >= start_idx) {
469 418 ++parent->kid_number;
470   - if (is_leaf)
471   - {
  419 + if (is_leaf) {
472 420 QTC::TC("qpdf", "NNTree split second half item");
473 421 setItemNumber(second_node, this->item_number - start_idx);
474   - }
475   - else
476   - {
  422 + } else {
477 423 QTC::TC("qpdf", "NNTree split second half kid");
478 424 cur_elem->node = second_node;
479 425 cur_elem->kid_number -= start_idx;
480 426 }
481 427 }
482   - if (! is_root)
483   - {
  428 + if (!is_root) {
484 429 QTC::TC("qpdf", "NNTree split parent");
485 430 auto next = parent->node;
486 431 resetLimits(next, parent);
... ... @@ -493,8 +438,7 @@ std::list&lt;NNTreeIterator::PathElement&gt;::iterator
493 438 NNTreeIterator::lastPathElement()
494 439 {
495 440 auto result = this->path.end();
496   - if (! this->path.empty())
497   - {
  441 + if (!this->path.empty()) {
498 442 --result;
499 443 }
500 444 return result;
... ... @@ -503,8 +447,7 @@ NNTreeIterator::lastPathElement()
503 447 void
504 448 NNTreeIterator::insertAfter(QPDFObjectHandle key, QPDFObjectHandle value)
505 449 {
506   - if (! valid())
507   - {
  450 + if (!valid()) {
508 451 QTC::TC("qpdf", "NNTree insertAfter inserts first");
509 452 impl.insertFirst(key, value);
510 453 deepen(impl.oh, true, false);
... ... @@ -512,12 +455,10 @@ NNTreeIterator::insertAfter(QPDFObjectHandle key, QPDFObjectHandle value)
512 455 }
513 456  
514 457 auto items = this->node.getKey(impl.details.itemsKey());
515   - if (! items.isArray())
516   - {
  458 + if (!items.isArray()) {
517 459 error(impl.qpdf, node, "node contains no items array");
518 460 }
519   - if (items.getArrayNItems() < this->item_number + 2)
520   - {
  461 + if (items.getArrayNItems() < this->item_number + 2) {
521 462 error(impl.qpdf, node, "insert: items array is too short");
522 463 }
523 464 items.insertItem(this->item_number + 2, key);
... ... @@ -533,51 +474,44 @@ NNTreeIterator::remove()
533 474 // Remove this item, leaving the tree valid and this iterator
534 475 // pointing to the next item.
535 476  
536   - if (! valid())
537   - {
  477 + if (!valid()) {
538 478 throw std::logic_error("attempt made to remove an invalid iterator");
539 479 }
540 480 auto items = this->node.getKey(impl.details.itemsKey());
541 481 int nitems = items.getArrayNItems();
542   - if (this->item_number + 2 > nitems)
543   - {
544   - error(impl.qpdf, this->node,
545   - "found short items array while removing an item");
  482 + if (this->item_number + 2 > nitems) {
  483 + error(
  484 + impl.qpdf,
  485 + this->node,
  486 + "found short items array while removing an item");
546 487 }
547 488  
548 489 items.eraseItem(this->item_number);
549 490 items.eraseItem(this->item_number);
550 491 nitems -= 2;
551 492  
552   - if (nitems > 0)
553   - {
  493 + if (nitems > 0) {
554 494 // There are still items left
555 495  
556   - if ((this->item_number == 0) || (this->item_number == nitems))
557   - {
  496 + if ((this->item_number == 0) || (this->item_number == nitems)) {
558 497 // We removed either the first or last item of an items array
559 498 // that remains non-empty, so we have to adjust limits.
560 499 QTC::TC("qpdf", "NNTree remove reset limits");
561 500 resetLimits(this->node, lastPathElement());
562 501 }
563 502  
564   - if (this->item_number == nitems)
565   - {
  503 + if (this->item_number == nitems) {
566 504 // We removed the last item of a non-empty items array, so
567 505 // advance to the successor of the previous item.
568 506 QTC::TC("qpdf", "NNTree erased last item");
569 507 this->item_number -= 2;
570 508 increment(false);
571   - }
572   - else if (this->item_number < nitems)
573   - {
  509 + } else if (this->item_number < nitems) {
574 510 // We don't have to do anything since the removed item's
575 511 // successor now occupies its former location.
576 512 QTC::TC("qpdf", "NNTree erased non-last item");
577 513 updateIValue();
578   - }
579   - else
580   - {
  514 + } else {
581 515 // We already checked to ensure this condition would not
582 516 // happen.
583 517 throw std::logic_error(
... ... @@ -586,8 +520,7 @@ NNTreeIterator::remove()
586 520 return;
587 521 }
588 522  
589   - if (this->path.empty())
590   - {
  523 + if (this->path.empty()) {
591 524 // Special case: if this is the root node, we can leave it
592 525 // empty.
593 526 QTC::TC("qpdf", "NNTree erased all items on leaf/root");
... ... @@ -601,64 +534,50 @@ NNTreeIterator::remove()
601 534 // remove this node from the parent on up the tree. Then we need
602 535 // to position ourselves at the removed item's successor.
603 536 bool done = false;
604   - while (! done)
605   - {
  537 + while (!done) {
606 538 auto element = lastPathElement();
607 539 auto parent = element;
608 540 --parent;
609 541 auto kids = element->node.getKey("/Kids");
610 542 kids.eraseItem(element->kid_number);
611 543 auto nkids = kids.getArrayNItems();
612   - if (nkids > 0)
613   - {
  544 + if (nkids > 0) {
614 545 // The logic here is similar to the items case.
615   - if ((element->kid_number == 0) || (element->kid_number == nkids))
616   - {
  546 + if ((element->kid_number == 0) || (element->kid_number == nkids)) {
617 547 QTC::TC("qpdf", "NNTree erased first or last kid");
618 548 resetLimits(element->node, parent);
619 549 }
620   - if (element->kid_number == nkids)
621   - {
  550 + if (element->kid_number == nkids) {
622 551 // Move to the successor of the last child of the
623 552 // previous kid.
624 553 setItemNumber(QPDFObjectHandle(), -1);
625 554 --element->kid_number;
626 555 deepen(kids.getArrayItem(element->kid_number), false, true);
627   - if (valid())
628   - {
  556 + if (valid()) {
629 557 increment(false);
630   - if (! valid())
631   - {
  558 + if (!valid()) {
632 559 QTC::TC("qpdf", "NNTree erased last item in tree");
633   - }
634   - else
635   - {
  560 + } else {
636 561 QTC::TC("qpdf", "NNTree erased last kid");
637 562 }
638 563 }
639   - }
640   - else
641   - {
  564 + } else {
642 565 // Next kid is in deleted kid's position
643 566 QTC::TC("qpdf", "NNTree erased non-last kid");
644 567 deepen(kids.getArrayItem(element->kid_number), true, true);
645 568 }
646 569 done = true;
647   - }
648   - else if (parent == this->path.end())
649   - {
  570 + } else if (parent == this->path.end()) {
650 571 // We erased the very last item. Convert the root to an
651 572 // empty items array.
652 573 QTC::TC("qpdf", "NNTree non-flat tree is empty after remove");
653 574 element->node.removeKey("/Kids");
654   - element->node.replaceKey(impl.details.itemsKey(),
655   - QPDFObjectHandle::newArray());
  575 + element->node.replaceKey(
  576 + impl.details.itemsKey(), QPDFObjectHandle::newArray());
656 577 this->path.clear();
657 578 setItemNumber(impl.oh, -1);
658 579 done = true;
659   - }
660   - else
661   - {
  580 + } else {
662 581 // Walk up the tree and continue
663 582 QTC::TC("qpdf", "NNTree remove walking up tree");
664 583 this->path.pop_back();
... ... @@ -697,27 +616,22 @@ NNTreeIterator::operator-&gt;()
697 616 bool
698 617 NNTreeIterator::operator==(NNTreeIterator const& other) const
699 618 {
700   - if ((this->item_number == -1) && (other.item_number == -1))
701   - {
  619 + if ((this->item_number == -1) && (other.item_number == -1)) {
702 620 return true;
703 621 }
704   - if (this->path.size() != other.path.size())
705   - {
  622 + if (this->path.size() != other.path.size()) {
706 623 return false;
707 624 }
708 625 auto tpi = this->path.begin();
709 626 auto opi = other.path.begin();
710   - while (tpi != this->path.end())
711   - {
712   - if (tpi->kid_number != opi->kid_number)
713   - {
  627 + while (tpi != this->path.end()) {
  628 + if (tpi->kid_number != opi->kid_number) {
714 629 return false;
715 630 }
716 631 ++tpi;
717 632 ++opi;
718 633 }
719   - if (this->item_number != other.item_number)
720   - {
  634 + if (this->item_number != other.item_number) {
721 635 return false;
722 636 }
723 637 return true;
... ... @@ -732,8 +646,7 @@ NNTreeIterator::setItemNumber(QPDFObjectHandle const&amp; node, int n)
732 646 }
733 647  
734 648 void
735   -NNTreeIterator::addPathElement(QPDFObjectHandle const& node,
736   - int kid_number)
  649 +NNTreeIterator::addPathElement(QPDFObjectHandle const& node, int kid_number)
737 650 {
738 651 this->path.push_back(PathElement(node, kid_number));
739 652 }
... ... @@ -749,33 +662,31 @@ NNTreeIterator::deepen(QPDFObjectHandle node, bool first, bool allow_empty)
749 662 bool failed = false;
750 663  
751 664 std::set<QPDFObjGen> seen;
752   - for (auto i: this->path)
753   - {
754   - if (i.node.isIndirect())
755   - {
  665 + for (auto i : this->path) {
  666 + if (i.node.isIndirect()) {
756 667 seen.insert(i.node.getObjGen());
757 668 }
758 669 }
759   - while (! failed)
760   - {
761   - if (node.isIndirect())
762   - {
  670 + while (!failed) {
  671 + if (node.isIndirect()) {
763 672 auto og = node.getObjGen();
764   - if (seen.count(og))
765   - {
  673 + if (seen.count(og)) {
766 674 QTC::TC("qpdf", "NNTree deepen: loop");
767   - warn(impl.qpdf, node,
768   - "loop detected while traversing name/number tree");
  675 + warn(
  676 + impl.qpdf,
  677 + node,
  678 + "loop detected while traversing name/number tree");
769 679 failed = true;
770 680 break;
771 681 }
772 682 seen.insert(og);
773 683 }
774   - if (! node.isDictionary())
775   - {
  684 + if (!node.isDictionary()) {
776 685 QTC::TC("qpdf", "NNTree node is not a dictionary");
777   - warn(impl.qpdf, node,
778   - "non-dictionary node while traversing name/number tree");
  686 + warn(
  687 + impl.qpdf,
  688 + node,
  689 + "non-dictionary node while traversing name/number tree");
779 690 failed = true;
780 691 break;
781 692 }
... ... @@ -784,66 +695,61 @@ NNTreeIterator::deepen(QPDFObjectHandle node, bool first, bool allow_empty)
784 695 int nkids = kids.isArray() ? kids.getArrayNItems() : 0;
785 696 auto items = node.getKey(impl.details.itemsKey());
786 697 int nitems = items.isArray() ? items.getArrayNItems() : 0;
787   - if (nitems > 0)
788   - {
  698 + if (nitems > 0) {
789 699 setItemNumber(node, first ? 0 : nitems - 2);
790 700 break;
791   - }
792   - else if (nkids > 0)
793   - {
  701 + } else if (nkids > 0) {
794 702 int kid_number = first ? 0 : nkids - 1;
795 703 addPathElement(node, kid_number);
796 704 auto next = kids.getArrayItem(kid_number);
797   - if (! next.isIndirect())
798   - {
799   - if (impl.qpdf && impl.auto_repair)
800   - {
  705 + if (!next.isIndirect()) {
  706 + if (impl.qpdf && impl.auto_repair) {
801 707 QTC::TC("qpdf", "NNTree fix indirect kid");
802   - warn(impl.qpdf, node,
803   - "converting kid number " +
804   - QUtil::int_to_string(kid_number) +
805   - " to an indirect object");
  708 + warn(
  709 + impl.qpdf,
  710 + node,
  711 + "converting kid number " +
  712 + QUtil::int_to_string(kid_number) +
  713 + " to an indirect object");
806 714 next = impl.qpdf->makeIndirectObject(next);
807 715 kids.setArrayItem(kid_number, next);
808   - }
809   - else
810   - {
  716 + } else {
811 717 QTC::TC("qpdf", "NNTree warn indirect kid");
812   - warn(impl.qpdf, node,
813   - "kid number " + QUtil::int_to_string(kid_number) +
814   - " is not an indirect object");
  718 + warn(
  719 + impl.qpdf,
  720 + node,
  721 + "kid number " + QUtil::int_to_string(kid_number) +
  722 + " is not an indirect object");
815 723 }
816 724 }
817 725 node = next;
818   - }
819   - else if (allow_empty && items.isArray())
820   - {
  726 + } else if (allow_empty && items.isArray()) {
821 727 QTC::TC("qpdf", "NNTree deepen found empty");
822 728 setItemNumber(node, -1);
823 729 break;
824   - }
825   - else
826   - {
  730 + } else {
827 731 QTC::TC("qpdf", "NNTree deepen: invalid node");
828   - warn(impl.qpdf, node,
829   - "name/number tree node has neither non-empty " +
830   - impl.details.itemsKey() + " nor /Kids");
  732 + warn(
  733 + impl.qpdf,
  734 + node,
  735 + "name/number tree node has neither non-empty " +
  736 + impl.details.itemsKey() + " nor /Kids");
831 737 failed = true;
832 738 break;
833 739 }
834 740 }
835   - if (failed)
836   - {
  741 + if (failed) {
837 742 this->path = opath;
838 743 return false;
839 744 }
840 745 return true;
841 746 }
842 747  
843   -NNTreeImpl::NNTreeImpl(NNTreeDetails const& details,
844   - QPDF* qpdf,
845   - QPDFObjectHandle& oh,
846   - bool auto_repair) :
  748 +NNTreeImpl::NNTreeImpl(
  749 + NNTreeDetails const& details,
  750 + QPDF* qpdf,
  751 + QPDFObjectHandle& oh,
  752 + bool auto_repair) :
847 753 details(details),
848 754 qpdf(qpdf),
849 755 split_threshold(32),
... ... @@ -887,19 +793,13 @@ NNTreeImpl::withinLimits(QPDFObjectHandle key, QPDFObjectHandle node)
887 793 auto limits = node.getKey("/Limits");
888 794 if (limits.isArray() && (limits.getArrayNItems() >= 2) &&
889 795 details.keyValid(limits.getArrayItem(0)) &&
890   - details.keyValid(limits.getArrayItem(1)))
891   - {
892   - if (details.compareKeys(key, limits.getArrayItem(0)) < 0)
893   - {
  796 + details.keyValid(limits.getArrayItem(1))) {
  797 + if (details.compareKeys(key, limits.getArrayItem(0)) < 0) {
894 798 result = -1;
895   - }
896   - else if (details.compareKeys(key, limits.getArrayItem(1)) > 0)
897   - {
  799 + } else if (details.compareKeys(key, limits.getArrayItem(1)) > 0) {
898 800 result = 1;
899 801 }
900   - }
901   - else
902   - {
  802 + } else {
903 803 QTC::TC("qpdf", "NNTree missing limits");
904 804 error(qpdf, node, "node is missing /Limits");
905 805 }
... ... @@ -908,15 +808,15 @@ NNTreeImpl::withinLimits(QPDFObjectHandle key, QPDFObjectHandle node)
908 808  
909 809 int
910 810 NNTreeImpl::binarySearch(
911   - QPDFObjectHandle key, QPDFObjectHandle items,
912   - int num_items, bool return_prev_if_not_found,
913   - int (NNTreeImpl::*compare)(QPDFObjectHandle& key,
914   - QPDFObjectHandle& arr,
915   - int item))
  811 + QPDFObjectHandle key,
  812 + QPDFObjectHandle items,
  813 + int num_items,
  814 + bool return_prev_if_not_found,
  815 + int (NNTreeImpl::*compare)(
  816 + QPDFObjectHandle& key, QPDFObjectHandle& arr, int item))
916 817 {
917 818 int max_idx = 1;
918   - while (max_idx < num_items)
919   - {
  819 + while (max_idx < num_items) {
920 820 max_idx <<= 1;
921 821 }
922 822  
... ... @@ -928,56 +828,40 @@ NNTreeImpl::binarySearch(
928 828 bool found_leq = false;
929 829 int status = 0;
930 830  
931   - while ((! found) && (checks > 0))
932   - {
933   - if (idx < num_items)
934   - {
  831 + while ((!found) && (checks > 0)) {
  832 + if (idx < num_items) {
935 833 status = (this->*compare)(key, items, idx);
936   - if (status >= 0)
937   - {
  834 + if (status >= 0) {
938 835 found_leq = true;
939 836 found_idx = idx;
940 837 }
941   - }
942   - else
943   - {
  838 + } else {
944 839 // consider item to be below anything after the top
945 840 status = -1;
946 841 }
947 842  
948   - if (status == 0)
949   - {
  843 + if (status == 0) {
950 844 found = true;
951   - }
952   - else
953   - {
  845 + } else {
954 846 checks >>= 1;
955   - if (checks > 0)
956   - {
  847 + if (checks > 0) {
957 848 step >>= 1;
958   - if (step == 0)
959   - {
  849 + if (step == 0) {
960 850 step = 1;
961 851 }
962 852  
963   - if (status < 0)
964   - {
  853 + if (status < 0) {
965 854 idx -= step;
966   - }
967   - else
968   - {
  855 + } else {
969 856 idx += step;
970 857 }
971 858 }
972 859 }
973 860 }
974 861  
975   - if (found || (found_leq && return_prev_if_not_found))
976   - {
  862 + if (found || (found_leq && return_prev_if_not_found)) {
977 863 return found_idx;
978   - }
979   - else
980   - {
  864 + } else {
981 865 return -1;
982 866 }
983 867 }
... ... @@ -986,13 +870,14 @@ int
986 870 NNTreeImpl::compareKeyItem(
987 871 QPDFObjectHandle& key, QPDFObjectHandle& items, int idx)
988 872 {
989   - if (! ((items.isArray() && (items.getArrayNItems() > (2 * idx)) &&
990   - details.keyValid(items.getArrayItem(2 * idx)))))
991   - {
  873 + if (!((items.isArray() && (items.getArrayNItems() > (2 * idx)) &&
  874 + details.keyValid(items.getArrayItem(2 * idx))))) {
992 875 QTC::TC("qpdf", "NNTree item is wrong type");
993   - error(qpdf, this->oh,
994   - "item at index " + QUtil::int_to_string(2 * idx) +
995   - " is not the right type");
  876 + error(
  877 + qpdf,
  878 + this->oh,
  879 + "item at index " + QUtil::int_to_string(2 * idx) +
  880 + " is not the right type");
996 881 }
997 882 return details.compareKeys(key, items.getArrayItem(2 * idx));
998 883 }
... ... @@ -1001,25 +886,24 @@ int
1001 886 NNTreeImpl::compareKeyKid(
1002 887 QPDFObjectHandle& key, QPDFObjectHandle& kids, int idx)
1003 888 {
1004   - if (! (kids.isArray() && (idx < kids.getArrayNItems()) &&
1005   - kids.getArrayItem(idx).isDictionary()))
1006   - {
  889 + if (!(kids.isArray() && (idx < kids.getArrayNItems()) &&
  890 + kids.getArrayItem(idx).isDictionary())) {
1007 891 QTC::TC("qpdf", "NNTree kid is invalid");
1008   - error(qpdf, this->oh,
1009   - "invalid kid at index " + QUtil::int_to_string(idx));
  892 + error(
  893 + qpdf,
  894 + this->oh,
  895 + "invalid kid at index " + QUtil::int_to_string(idx));
1010 896 }
1011 897 return withinLimits(key, kids.getArrayItem(idx));
1012 898 }
1013 899  
1014   -
1015 900 void
1016 901 NNTreeImpl::repair()
1017 902 {
1018 903 auto new_node = QPDFObjectHandle::newDictionary();
1019 904 new_node.replaceKey(details.itemsKey(), QPDFObjectHandle::newArray());
1020 905 NNTreeImpl repl(details, qpdf, new_node, false);
1021   - for (auto const& i: *this)
1022   - {
  906 + for (auto const& i : *this) {
1023 907 repl.insert(i.first, i.second);
1024 908 }
1025 909 this->oh.replaceKey("/Kids", new_node.getKey("/Kids"));
... ... @@ -1030,22 +914,18 @@ NNTreeImpl::repair()
1030 914 NNTreeImpl::iterator
1031 915 NNTreeImpl::find(QPDFObjectHandle key, bool return_prev_if_not_found)
1032 916 {
1033   - try
1034   - {
  917 + try {
1035 918 return findInternal(key, return_prev_if_not_found);
1036   - }
1037   - catch (QPDFExc& e)
1038   - {
1039   - if (this->auto_repair)
1040   - {
  919 + } catch (QPDFExc& e) {
  920 + if (this->auto_repair) {
1041 921 QTC::TC("qpdf", "NNTree repair");
1042   - warn(qpdf, this->oh,
1043   - std::string("attempting to repair after error: ") + e.what());
  922 + warn(
  923 + qpdf,
  924 + this->oh,
  925 + std::string("attempting to repair after error: ") + e.what());
1044 926 repair();
1045 927 return findInternal(key, return_prev_if_not_found);
1046   - }
1047   - else
1048   - {
  928 + } else {
1049 929 throw e;
1050 930 }
1051 931 }
... ... @@ -1056,29 +936,21 @@ NNTreeImpl::findInternal(QPDFObjectHandle key, bool return_prev_if_not_found)
1056 936 {
1057 937 auto first_item = begin();
1058 938 auto last_item = end();
1059   - if (first_item == end())
1060   - {
  939 + if (first_item == end()) {
1061 940 // Empty
1062 941 return end();
1063   - }
1064   - else if (first_item.valid() &&
1065   - details.keyValid(first_item->first) &&
1066   - details.compareKeys(key, first_item->first) < 0)
1067   - {
  942 + } else if (
  943 + first_item.valid() && details.keyValid(first_item->first) &&
  944 + details.compareKeys(key, first_item->first) < 0) {
1068 945 // Before the first key
1069 946 return end();
1070   - }
1071   - else if (last_item.valid() &&
1072   - details.keyValid(last_item->first) &&
1073   - details.compareKeys(key, last_item->first) > 0)
1074   - {
  947 + } else if (
  948 + last_item.valid() && details.keyValid(last_item->first) &&
  949 + details.compareKeys(key, last_item->first) > 0) {
1075 950 // After the last key
1076   - if (return_prev_if_not_found)
1077   - {
  951 + if (return_prev_if_not_found) {
1078 952 return last_item;
1079   - }
1080   - else
1081   - {
  953 + } else {
1082 954 return end();
1083 955 }
1084 956 }
... ... @@ -1087,11 +959,9 @@ NNTreeImpl::findInternal(QPDFObjectHandle key, bool return_prev_if_not_found)
1087 959 auto node = this->oh;
1088 960 iterator result(*this);
1089 961  
1090   - while (true)
1091   - {
  962 + while (true) {
1092 963 auto og = node.getObjGen();
1093   - if (seen.count(og))
1094   - {
  964 + if (seen.count(og)) {
1095 965 QTC::TC("qpdf", "NNTree loop in find");
1096 966 error(qpdf, node, "loop detected in find");
1097 967 }
... ... @@ -1101,34 +971,31 @@ NNTreeImpl::findInternal(QPDFObjectHandle key, bool return_prev_if_not_found)
1101 971 int nkids = kids.isArray() ? kids.getArrayNItems() : 0;
1102 972 auto items = node.getKey(details.itemsKey());
1103 973 int nitems = items.isArray() ? items.getArrayNItems() : 0;
1104   - if (nitems > 0)
1105   - {
  974 + if (nitems > 0) {
1106 975 int idx = binarySearch(
1107   - key, items, nitems / 2, return_prev_if_not_found,
  976 + key,
  977 + items,
  978 + nitems / 2,
  979 + return_prev_if_not_found,
1108 980 &NNTreeImpl::compareKeyItem);
1109   - if (idx >= 0)
1110   - {
  981 + if (idx >= 0) {
1111 982 result.setItemNumber(node, 2 * idx);
1112 983 }
1113 984 break;
1114   - }
1115   - else if (nkids > 0)
1116   - {
  985 + } else if (nkids > 0) {
1117 986 int idx = binarySearch(
1118   - key, kids, nkids, true,
1119   - &NNTreeImpl::compareKeyKid);
1120   - if (idx == -1)
1121   - {
  987 + key, kids, nkids, true, &NNTreeImpl::compareKeyKid);
  988 + if (idx == -1) {
1122 989 QTC::TC("qpdf", "NNTree -1 in binary search");
1123   - error(qpdf, node,
1124   - "unexpected -1 from binary search of kids;"
1125   - " limits may by wrong");
  990 + error(
  991 + qpdf,
  992 + node,
  993 + "unexpected -1 from binary search of kids;"
  994 + " limits may by wrong");
1126 995 }
1127 996 result.addPathElement(node, idx);
1128 997 node = kids.getArrayItem(idx);
1129   - }
1130   - else
1131   - {
  998 + } else {
1132 999 QTC::TC("qpdf", "NNTree bad node during find");
1133 1000 error(qpdf, node, "bad node during find");
1134 1001 }
... ... @@ -1142,13 +1009,10 @@ NNTreeImpl::insertFirst(QPDFObjectHandle key, QPDFObjectHandle value)
1142 1009 {
1143 1010 auto iter = begin();
1144 1011 QPDFObjectHandle items;
1145   - if (iter.node.isInitialized() &&
1146   - iter.node.isDictionary())
1147   - {
  1012 + if (iter.node.isInitialized() && iter.node.isDictionary()) {
1148 1013 items = iter.node.getKey(details.itemsKey());
1149 1014 }
1150   - if (! (items.isInitialized() && items.isArray()))
1151   - {
  1015 + if (!(items.isInitialized() && items.isArray())) {
1152 1016 QTC::TC("qpdf", "NNTree no valid items node in insertFirst");
1153 1017 error(qpdf, this->oh, "unable to find a valid items node");
1154 1018 }
... ... @@ -1164,20 +1028,15 @@ NNTreeImpl::iterator
1164 1028 NNTreeImpl::insert(QPDFObjectHandle key, QPDFObjectHandle value)
1165 1029 {
1166 1030 auto iter = find(key, true);
1167   - if (! iter.valid())
1168   - {
  1031 + if (!iter.valid()) {
1169 1032 QTC::TC("qpdf", "NNTree insert inserts first");
1170 1033 return insertFirst(key, value);
1171   - }
1172   - else if (details.compareKeys(key, iter->first) == 0)
1173   - {
  1034 + } else if (details.compareKeys(key, iter->first) == 0) {
1174 1035 QTC::TC("qpdf", "NNTree insert replaces");
1175 1036 auto items = iter.node.getKey(details.itemsKey());
1176 1037 items.setArrayItem(iter.item_number + 1, value);
1177 1038 iter.updateIValue();
1178   - }
1179   - else
1180   - {
  1039 + } else {
1181 1040 QTC::TC("qpdf", "NNTree insert inserts after");
1182 1041 iter.insertAfter(key, value);
1183 1042 }
... ... @@ -1188,13 +1047,11 @@ bool
1188 1047 NNTreeImpl::remove(QPDFObjectHandle key, QPDFObjectHandle* value)
1189 1048 {
1190 1049 auto iter = find(key, false);
1191   - if (! iter.valid())
1192   - {
  1050 + if (!iter.valid()) {
1193 1051 QTC::TC("qpdf", "NNTree remove not found");
1194 1052 return false;
1195 1053 }
1196   - if (value)
1197   - {
  1054 + if (value) {
1198 1055 *value = iter->second;
1199 1056 }
1200 1057 iter.remove();
... ...
libqpdf/OffsetInputSource.cc
... ... @@ -4,13 +4,12 @@
4 4 #include <sstream>
5 5 #include <stdexcept>
6 6  
7   -OffsetInputSource::OffsetInputSource(PointerHolder<InputSource> proxied,
8   - qpdf_offset_t global_offset) :
  7 +OffsetInputSource::OffsetInputSource(
  8 + PointerHolder<InputSource> proxied, qpdf_offset_t global_offset) :
9 9 proxied(proxied),
10 10 global_offset(global_offset)
11 11 {
12   - if (global_offset < 0)
13   - {
  12 + if (global_offset < 0) {
14 13 throw std::logic_error(
15 14 "OffsetInputSource constructed with negative offset");
16 15 }
... ... @@ -43,25 +42,19 @@ OffsetInputSource::tell()
43 42 void
44 43 OffsetInputSource::seek(qpdf_offset_t offset, int whence)
45 44 {
46   - if (whence == SEEK_SET)
47   - {
48   - if (offset > this->max_safe_offset)
49   - {
  45 + if (whence == SEEK_SET) {
  46 + if (offset > this->max_safe_offset) {
50 47 std::ostringstream msg;
51 48 msg.imbue(std::locale::classic());
52   - msg << "seeking to " << offset
53   - << " offset by " << global_offset
  49 + msg << "seeking to " << offset << " offset by " << global_offset
54 50 << " would cause an overflow of the offset type";
55 51 throw std::range_error(msg.str());
56 52 }
57 53 this->proxied->seek(offset + global_offset, whence);
58   - }
59   - else
60   - {
  54 + } else {
61 55 this->proxied->seek(offset, whence);
62 56 }
63   - if (tell() < 0)
64   - {
  57 + if (tell() < 0) {
65 58 throw std::runtime_error(
66 59 "offset input source: seek before beginning of file");
67 60 }
... ...
libqpdf/PDFVersion.cc
... ... @@ -7,7 +7,8 @@ PDFVersion::PDFVersion() :
7 7 {
8 8 }
9 9  
10   -PDFVersion::PDFVersion(int major_version, int minor_version, int extension_level) :
  10 +PDFVersion::PDFVersion(
  11 + int major_version, int minor_version, int extension_level) :
11 12 major_version(major_version),
12 13 minor_version(minor_version),
13 14 extension_level(extension_level)
... ... @@ -17,27 +18,28 @@ PDFVersion::PDFVersion(int major_version, int minor_version, int extension_level
17 18 bool
18 19 PDFVersion::operator<(PDFVersion const& rhs) const
19 20 {
20   - return ((this->major_version < rhs.major_version) ? true :
21   - (this->major_version > rhs.major_version) ? false :
22   - (this->minor_version < rhs.minor_version) ? true :
23   - (this->minor_version > rhs.minor_version) ? false :
24   - (this->extension_level < rhs.extension_level) ? true :
25   - false);
  21 + return (
  22 + (this->major_version < rhs.major_version) ? true
  23 + : (this->major_version > rhs.major_version) ? false
  24 + : (this->minor_version < rhs.minor_version) ? true
  25 + : (this->minor_version > rhs.minor_version) ? false
  26 + : (this->extension_level < rhs.extension_level) ? true
  27 + : false);
26 28 }
27 29  
28 30 bool
29 31 PDFVersion::operator==(PDFVersion const& rhs) const
30 32 {
31   - return ((this->major_version == rhs.major_version) &&
32   - (this->minor_version == rhs.minor_version) &&
33   - (this->extension_level == rhs.extension_level));
  33 + return (
  34 + (this->major_version == rhs.major_version) &&
  35 + (this->minor_version == rhs.minor_version) &&
  36 + (this->extension_level == rhs.extension_level));
34 37 }
35 38  
36 39 void
37 40 PDFVersion::updateIfGreater(PDFVersion const& other)
38 41 {
39   - if (*this < other)
40   - {
  42 + if (*this < other) {
41 43 *this = other;
42 44 }
43 45 }
... ...
libqpdf/Pipeline.cc
... ... @@ -15,8 +15,7 @@ Pipeline::~Pipeline()
15 15 Pipeline*
16 16 Pipeline::getNext(bool allow_null)
17 17 {
18   - if ((this->next == 0) && (! allow_null))
19   - {
  18 + if ((this->next == 0) && (!allow_null)) {
20 19 throw std::logic_error(
21 20 this->identifier +
22 21 ": Pipeline::getNext() called on pipeline with no next");
... ...
libqpdf/Pl_AES_PDF.cc
1 1 #include <qpdf/Pl_AES_PDF.hh>
2 2  
  3 +#include <qpdf/QIntC.hh>
  4 +#include <qpdf/QPDFCryptoProvider.hh>
3 5 #include <qpdf/QUtil.hh>
4   -#include <cstring>
5 6 #include <assert.h>
  7 +#include <cstring>
6 8 #include <stdexcept>
7   -#include <qpdf/QIntC.hh>
8   -#include <qpdf/QPDFCryptoProvider.hh>
9   -#include <string>
10 9 #include <stdlib.h>
  10 +#include <string>
11 11  
12 12 bool Pl_AES_PDF::use_static_iv = false;
13 13  
14   -Pl_AES_PDF::Pl_AES_PDF(char const* identifier, Pipeline* next,
15   - bool encrypt, unsigned char const* key,
16   - size_t key_bytes) :
  14 +Pl_AES_PDF::Pl_AES_PDF(
  15 + char const* identifier,
  16 + Pipeline* next,
  17 + bool encrypt,
  18 + unsigned char const* key,
  19 + size_t key_bytes) :
17 20 Pipeline(identifier, next),
18 21 crypto(QPDFCryptoProvider::getImpl()),
19 22 encrypt(encrypt),
... ... @@ -51,11 +54,11 @@ Pl_AES_PDF::disablePadding()
51 54 void
52 55 Pl_AES_PDF::setIV(unsigned char const* iv, size_t bytes)
53 56 {
54   - if (bytes != this->buf_size)
55   - {
  57 + if (bytes != this->buf_size) {
56 58 throw std::logic_error(
57 59 "Pl_AES_PDF: specified initialization vector"
58   - " size in bytes must be " + QUtil::uint_to_string(bytes));
  60 + " size in bytes must be " +
  61 + QUtil::uint_to_string(bytes));
59 62 }
60 63 this->use_specified_iv = true;
61 64 memcpy(this->specified_iv, iv, bytes);
... ... @@ -79,10 +82,8 @@ Pl_AES_PDF::write(unsigned char* data, size_t len)
79 82 size_t bytes_left = len;
80 83 unsigned char* p = data;
81 84  
82   - while (bytes_left > 0)
83   - {
84   - if (this->offset == this->buf_size)
85   - {
  85 + while (bytes_left > 0) {
  86 + if (this->offset == this->buf_size) {
86 87 flush(false);
87 88 }
88 89  
... ... @@ -98,39 +99,32 @@ Pl_AES_PDF::write(unsigned char* data, size_t len)
98 99 void
99 100 Pl_AES_PDF::finish()
100 101 {
101   - if (this->encrypt)
102   - {
103   - if (this->offset == this->buf_size)
104   - {
  102 + if (this->encrypt) {
  103 + if (this->offset == this->buf_size) {
105 104 flush(false);
106 105 }
107   - if (! this->disable_padding)
108   - {
  106 + if (!this->disable_padding) {
109 107 // Pad as described in section 3.5.1 of version 1.7 of the PDF
110 108 // specification, including providing an entire block of padding
111 109 // if the input was a multiple of 16 bytes.
112   - unsigned char pad =
113   - QIntC::to_uchar(this->buf_size - this->offset);
  110 + unsigned char pad = QIntC::to_uchar(this->buf_size - this->offset);
114 111 memset(this->inbuf + this->offset, pad, pad);
115 112 this->offset = this->buf_size;
116 113 flush(false);
117 114 }
118   - }
119   - else
120   - {
121   - if (this->offset != this->buf_size)
122   - {
  115 + } else {
  116 + if (this->offset != this->buf_size) {
123 117 // This is never supposed to happen as the output is
124 118 // always supposed to be padded. However, we have
125 119 // encountered files for which the output is not a
126 120 // multiple of the block size. In this case, pad with
127 121 // zeroes and hope for the best.
128 122 assert(this->buf_size > this->offset);
129   - std::memset(this->inbuf + this->offset, 0,
130   - this->buf_size - this->offset);
  123 + std::memset(
  124 + this->inbuf + this->offset, 0, this->buf_size - this->offset);
131 125 this->offset = this->buf_size;
132 126 }
133   - flush(! this->disable_padding);
  127 + flush(!this->disable_padding);
134 128 }
135 129 this->crypto->rijndael_finalize();
136 130 getNext()->finish();
... ... @@ -139,26 +133,17 @@ Pl_AES_PDF::finish()
139 133 void
140 134 Pl_AES_PDF::initializeVector()
141 135 {
142   - if (use_zero_iv)
143   - {
144   - for (unsigned int i = 0; i < this->buf_size; ++i)
145   - {
  136 + if (use_zero_iv) {
  137 + for (unsigned int i = 0; i < this->buf_size; ++i) {
146 138 this->cbc_block[i] = 0;
147 139 }
148   - }
149   - else if (use_specified_iv)
150   - {
  140 + } else if (use_specified_iv) {
151 141 std::memcpy(this->cbc_block, this->specified_iv, this->buf_size);
152   - }
153   - else if (use_static_iv)
154   - {
155   - for (unsigned int i = 0; i < this->buf_size; ++i)
156   - {
  142 + } else if (use_static_iv) {
  143 + for (unsigned int i = 0; i < this->buf_size; ++i) {
157 144 this->cbc_block[i] = static_cast<unsigned char>(14U * (1U + i));
158 145 }
159   - }
160   - else
161   - {
  146 + } else {
162 147 QUtil::initializeWithRandomBytes(this->cbc_block, this->buf_size);
163 148 }
164 149 }
... ... @@ -168,30 +153,22 @@ Pl_AES_PDF::flush(bool strip_padding)
168 153 {
169 154 assert(this->offset == this->buf_size);
170 155  
171   - if (first)
172   - {
  156 + if (first) {
173 157 first = false;
174 158 bool return_after_init = false;
175   - if (this->cbc_mode)
176   - {
177   - if (encrypt)
178   - {
  159 + if (this->cbc_mode) {
  160 + if (encrypt) {
179 161 // Set cbc_block to the initialization vector, and if
180 162 // not zero, write it to the output stream.
181 163 initializeVector();
182   - if (! (this->use_zero_iv || this->use_specified_iv))
183   - {
  164 + if (!(this->use_zero_iv || this->use_specified_iv)) {
184 165 getNext()->write(this->cbc_block, this->buf_size);
185 166 }
186   - }
187   - else if (this->use_zero_iv || this->use_specified_iv)
188   - {
  167 + } else if (this->use_zero_iv || this->use_specified_iv) {
189 168 // Initialize vector with zeroes; zero vector was not
190 169 // written to the beginning of the input file.
191 170 initializeVector();
192   - }
193   - else
194   - {
  171 + } else {
195 172 // Take the first block of input as the initialization
196 173 // vector. There's nothing to write at this time.
197 174 memcpy(this->cbc_block, this->inbuf, this->buf_size);
... ... @@ -200,32 +177,29 @@ Pl_AES_PDF::flush(bool strip_padding)
200 177 }
201 178 }
202 179 this->crypto->rijndael_init(
203   - encrypt, this->key.get(), key_bytes,
204   - this->cbc_mode, this->cbc_block);
205   - if (return_after_init)
206   - {
  180 + encrypt,
  181 + this->key.get(),
  182 + key_bytes,
  183 + this->cbc_mode,
  184 + this->cbc_block);
  185 + if (return_after_init) {
207 186 return;
208 187 }
209 188 }
210 189  
211 190 this->crypto->rijndael_process(this->inbuf, this->outbuf);
212 191 unsigned int bytes = this->buf_size;
213   - if (strip_padding)
214   - {
  192 + if (strip_padding) {
215 193 unsigned char last = this->outbuf[this->buf_size - 1];
216   - if (last <= this->buf_size)
217   - {
  194 + if (last <= this->buf_size) {
218 195 bool strip = true;
219   - for (unsigned int i = 1; i <= last; ++i)
220   - {
221   - if (this->outbuf[this->buf_size - i] != last)
222   - {
  196 + for (unsigned int i = 1; i <= last; ++i) {
  197 + if (this->outbuf[this->buf_size - i] != last) {
223 198 strip = false;
224 199 break;
225 200 }
226 201 }
227   - if (strip)
228   - {
  202 + if (strip) {
229 203 bytes -= last;
230 204 }
231 205 }
... ...
libqpdf/Pl_ASCII85Decoder.cc
... ... @@ -19,55 +19,41 @@ Pl_ASCII85Decoder::~Pl_ASCII85Decoder()
19 19 void
20 20 Pl_ASCII85Decoder::write(unsigned char* buf, size_t len)
21 21 {
22   - if (eod > 1)
23   - {
  22 + if (eod > 1) {
24 23 return;
25 24 }
26   - for (size_t i = 0; i < len; ++i)
27   - {
28   - if (eod > 1)
29   - {
  25 + for (size_t i = 0; i < len; ++i) {
  26 + if (eod > 1) {
30 27 break;
31   - }
32   - else if (eod == 1)
33   - {
34   - if (buf[i] == '>')
35   - {
  28 + } else if (eod == 1) {
  29 + if (buf[i] == '>') {
36 30 flush();
37 31 eod = 2;
38   - }
39   - else
40   - {
  32 + } else {
41 33 throw std::runtime_error(
42 34 "broken end-of-data sequence in base 85 data");
43 35 }
44   - }
45   - else
46   - {
47   - switch (buf[i])
48   - {
49   - case ' ':
50   - case '\f':
51   - case '\v':
52   - case '\t':
53   - case '\r':
54   - case '\n':
  36 + } else {
  37 + switch (buf[i]) {
  38 + case ' ':
  39 + case '\f':
  40 + case '\v':
  41 + case '\t':
  42 + case '\r':
  43 + case '\n':
55 44 QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
56 45 // ignore whitespace
57 46 break;
58 47  
59   - case '~':
  48 + case '~':
60 49 eod = 1;
61 50 break;
62 51  
63   - case 'z':
64   - if (pos != 0)
65   - {
  52 + case 'z':
  53 + if (pos != 0) {
66 54 throw std::runtime_error(
67 55 "unexpected z during base 85 decode");
68   - }
69   - else
70   - {
  56 + } else {
71 57 QTC::TC("libtests", "Pl_ASCII85Decoder read z");
72 58 unsigned char zeroes[4];
73 59 memset(zeroes, '\0', 4);
... ... @@ -75,17 +61,13 @@ Pl_ASCII85Decoder::write(unsigned char* buf, size_t len)
75 61 }
76 62 break;
77 63  
78   - default:
79   - if ((buf[i] < 33) || (buf[i] > 117))
80   - {
  64 + default:
  65 + if ((buf[i] < 33) || (buf[i] > 117)) {
81 66 throw std::runtime_error(
82 67 "character out of range during base 85 decode");
83   - }
84   - else
85   - {
  68 + } else {
86 69 this->inbuf[this->pos++] = buf[i];
87   - if (pos == 5)
88   - {
  70 + if (pos == 5) {
89 71 flush();
90 72 }
91 73 }
... ... @@ -98,28 +80,27 @@ Pl_ASCII85Decoder::write(unsigned char* buf, size_t len)
98 80 void
99 81 Pl_ASCII85Decoder::flush()
100 82 {
101   - if (this->pos == 0)
102   - {
  83 + if (this->pos == 0) {
103 84 QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
104 85 return;
105 86 }
106 87 unsigned long lval = 0;
107   - for (int i = 0; i < 5; ++i)
108   - {
  88 + for (int i = 0; i < 5; ++i) {
109 89 lval *= 85;
110 90 lval += (this->inbuf[i] - 33U);
111 91 }
112 92  
113 93 unsigned char outbuf[4];
114 94 memset(outbuf, 0, 4);
115   - for (int i = 3; i >= 0; --i)
116   - {
  95 + for (int i = 3; i >= 0; --i) {
117 96 outbuf[i] = lval & 0xff;
118 97 lval >>= 8;
119 98 }
120 99  
121   - QTC::TC("libtests", "Pl_ASCII85Decoder partial flush",
122   - (this->pos == 5) ? 0 : 1);
  100 + QTC::TC(
  101 + "libtests",
  102 + "Pl_ASCII85Decoder partial flush",
  103 + (this->pos == 5) ? 0 : 1);
123 104 // Reset before calling getNext()->write in case that throws an
124 105 // exception.
125 106 auto t = this->pos - 1;
... ...
libqpdf/Pl_ASCIIHexDecoder.cc
1 1 #include <qpdf/Pl_ASCIIHexDecoder.hh>
2 2  
3 3 #include <qpdf/QTC.hh>
  4 +#include <ctype.h>
4 5 #include <stdexcept>
5 6 #include <string.h>
6   -#include <ctype.h>
7 7  
8 8 Pl_ASCIIHexDecoder::Pl_ASCIIHexDecoder(char const* identifier, Pipeline* next) :
9 9 Pipeline(identifier, next),
... ... @@ -22,53 +22,45 @@ Pl_ASCIIHexDecoder::~Pl_ASCIIHexDecoder()
22 22 void
23 23 Pl_ASCIIHexDecoder::write(unsigned char* buf, size_t len)
24 24 {
25   - if (this->eod)
26   - {
  25 + if (this->eod) {
27 26 return;
28 27 }
29   - for (size_t i = 0; i < len; ++i)
30   - {
  28 + for (size_t i = 0; i < len; ++i) {
31 29 char ch = static_cast<char>(toupper(buf[i]));
32   - switch (ch)
33   - {
34   - case ' ':
35   - case '\f':
36   - case '\v':
37   - case '\t':
38   - case '\r':
39   - case '\n':
  30 + switch (ch) {
  31 + case ' ':
  32 + case '\f':
  33 + case '\v':
  34 + case '\t':
  35 + case '\r':
  36 + case '\n':
40 37 QTC::TC("libtests", "Pl_ASCIIHexDecoder ignore space");
41 38 // ignore whitespace
42 39 break;
43 40  
44   - case '>':
  41 + case '>':
45 42 this->eod = true;
46 43 flush();
47 44 break;
48 45  
49   - default:
50   - if (((ch >= '0') && (ch <= '9')) ||
51   - ((ch >= 'A') && (ch <= 'F')))
52   - {
  46 + default:
  47 + if (((ch >= '0') && (ch <= '9')) || ((ch >= 'A') && (ch <= 'F'))) {
53 48 this->inbuf[this->pos++] = ch;
54   - if (this->pos == 2)
55   - {
  49 + if (this->pos == 2) {
56 50 flush();
57 51 }
58   - }
59   - else
60   - {
  52 + } else {
61 53 char t[2];
62 54 t[0] = ch;
63 55 t[1] = 0;
64 56 throw std::runtime_error(
65 57 std::string("character out of range"
66   - " during base Hex decode: ") + t);
  58 + " during base Hex decode: ") +
  59 + t);
67 60 }
68 61 break;
69 62 }
70   - if (this->eod)
71   - {
  63 + if (this->eod) {
72 64 break;
73 65 }
74 66 }
... ... @@ -77,27 +69,24 @@ Pl_ASCIIHexDecoder::write(unsigned char* buf, size_t len)
77 69 void
78 70 Pl_ASCIIHexDecoder::flush()
79 71 {
80   - if (this->pos == 0)
81   - {
  72 + if (this->pos == 0) {
82 73 QTC::TC("libtests", "Pl_ASCIIHexDecoder no-op flush");
83 74 return;
84 75 }
85 76 int b[2];
86   - for (int i = 0; i < 2; ++i)
87   - {
88   - if (this->inbuf[i] >= 'A')
89   - {
  77 + for (int i = 0; i < 2; ++i) {
  78 + if (this->inbuf[i] >= 'A') {
90 79 b[i] = this->inbuf[i] - 'A' + 10;
91   - }
92   - else
93   - {
  80 + } else {
94 81 b[i] = this->inbuf[i] - '0';
95 82 }
96 83 }
97 84 unsigned char ch = static_cast<unsigned char>((b[0] << 4) + b[1]);
98 85  
99   - QTC::TC("libtests", "Pl_ASCIIHexDecoder partial flush",
100   - (this->pos == 2) ? 0 : 1);
  86 + QTC::TC(
  87 + "libtests",
  88 + "Pl_ASCIIHexDecoder partial flush",
  89 + (this->pos == 2) ? 0 : 1);
101 90 // Reset before calling getNext()->write in case that throws an
102 91 // exception.
103 92 this->pos = 0;
... ...
libqpdf/Pl_Buffer.cc
1 1 #include <qpdf/Pl_Buffer.hh>
2 2  
3   -#include <stdexcept>
4 3 #include <algorithm>
5 4 #include <assert.h>
6   -#include <string.h>
  5 +#include <stdexcept>
7 6 #include <stdlib.h>
  7 +#include <string.h>
8 8  
9 9 Pl_Buffer::Members::Members() :
10 10 ready(true),
... ... @@ -29,28 +29,24 @@ Pl_Buffer::~Pl_Buffer()
29 29 void
30 30 Pl_Buffer::write(unsigned char* buf, size_t len)
31 31 {
32   - if (this->m->data.get() == 0)
33   - {
  32 + if (this->m->data.get() == 0) {
34 33 this->m->data = make_pointer_holder<Buffer>(len);
35 34 }
36 35 size_t cur_size = this->m->data->getSize();
37 36 size_t left = cur_size - this->m->total_size;
38   - if (left < len)
39   - {
  37 + if (left < len) {
40 38 size_t new_size = std::max(this->m->total_size + len, 2 * cur_size);
41 39 auto b = make_pointer_holder<Buffer>(new_size);
42 40 memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size);
43 41 this->m->data = b;
44 42 }
45   - if (len)
46   - {
  43 + if (len) {
47 44 memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len);
48 45 this->m->total_size += len;
49 46 }
50 47 this->m->ready = false;
51 48  
52   - if (getNext(true))
53   - {
  49 + if (getNext(true)) {
54 50 getNext()->write(buf, len);
55 51 }
56 52 }
... ... @@ -59,8 +55,7 @@ void
59 55 Pl_Buffer::finish()
60 56 {
61 57 this->m->ready = true;
62   - if (getNext(true))
63   - {
  58 + if (getNext(true)) {
64 59 getNext()->finish();
65 60 }
66 61 }
... ... @@ -68,14 +63,12 @@ Pl_Buffer::finish()
68 63 Buffer*
69 64 Pl_Buffer::getBuffer()
70 65 {
71   - if (! this->m->ready)
72   - {
  66 + if (!this->m->ready) {
73 67 throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
74 68 }
75 69  
76 70 Buffer* b = new Buffer(this->m->total_size);
77   - if (this->m->total_size > 0)
78   - {
  71 + if (this->m->total_size > 0) {
79 72 unsigned char* p = b->getBuffer();
80 73 memcpy(p, this->m->data->getBuffer(), this->m->total_size);
81 74 }
... ... @@ -90,22 +83,18 @@ Pl_Buffer::getBufferSharedPointer()
90 83 }
91 84  
92 85 void
93   -Pl_Buffer::getMallocBuffer(unsigned char **buf, size_t* len)
  86 +Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
94 87 {
95   - if (! this->m->ready)
96   - {
  88 + if (!this->m->ready) {
97 89 throw std::logic_error(
98 90 "Pl_Buffer::getMallocBuffer() called when not ready");
99 91 }
100 92  
101 93 *len = this->m->total_size;
102   - if (this->m->total_size > 0)
103   - {
  94 + if (this->m->total_size > 0) {
104 95 *buf = reinterpret_cast<unsigned char*>(malloc(this->m->total_size));
105 96 memcpy(*buf, this->m->data->getBuffer(), this->m->total_size);
106   - }
107   - else
108   - {
  97 + } else {
109 98 *buf = nullptr;
110 99 }
111 100 this->m = PointerHolder<Members>(new Members());
... ...
libqpdf/Pl_Concatenate.cc
... ... @@ -33,4 +33,3 @@ Pl_Concatenate::manualFinish()
33 33 {
34 34 getNext()->finish();
35 35 }
36   -
... ...
libqpdf/Pl_Count.cc
... ... @@ -25,8 +25,7 @@ Pl_Count::~Pl_Count()
25 25 void
26 26 Pl_Count::write(unsigned char* buf, size_t len)
27 27 {
28   - if (len)
29   - {
  28 + if (len) {
30 29 this->m->count += QIntC::to_offset(len);
31 30 this->m->last_char = buf[len - 1];
32 31 getNext()->write(buf, len);
... ...
libqpdf/Pl_DCT.cc
1 1 #include <qpdf/Pl_DCT.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4   -#include <qpdf/QTC.hh>
5 3 #include <qpdf/QIntC.hh>
  4 +#include <qpdf/QTC.hh>
  5 +#include <qpdf/QUtil.hh>
6 6  
  7 +#include <cstring>
7 8 #include <setjmp.h>
8 9 #include <stdexcept>
9 10 #include <stdlib.h>
10 11 #include <string>
11   -#include <cstring>
12 12  
13 13 #if BITS_IN_JSAMPLE != 8
14 14 # error "qpdf does not support libjpeg built with BITS_IN_JSAMPLE != 8"
... ... @@ -32,13 +32,14 @@ error_handler(j_common_ptr cinfo)
32 32 longjmp(jerr->jmpbuf, 1);
33 33 }
34 34  
35   -Pl_DCT::Members::Members(action_e action,
36   - char const* buf_description,
37   - JDIMENSION image_width,
38   - JDIMENSION image_height,
39   - int components,
40   - J_COLOR_SPACE color_space,
41   - CompressConfig* config_callback) :
  35 +Pl_DCT::Members::Members(
  36 + action_e action,
  37 + char const* buf_description,
  38 + JDIMENSION image_width,
  39 + JDIMENSION image_height,
  40 + int components,
  41 + J_COLOR_SPACE color_space,
  42 + CompressConfig* config_callback) :
42 43 action(action),
43 44 buf(buf_description),
44 45 image_width(image_width),
... ... @@ -59,15 +60,23 @@ Pl_DCT::Pl_DCT(char const* identifier, Pipeline* next) :
59 60 {
60 61 }
61 62  
62   -Pl_DCT::Pl_DCT(char const* identifier, Pipeline* next,
63   - JDIMENSION image_width,
64   - JDIMENSION image_height,
65   - int components,
66   - J_COLOR_SPACE color_space,
67   - CompressConfig* config_callback) :
  63 +Pl_DCT::Pl_DCT(
  64 + char const* identifier,
  65 + Pipeline* next,
  66 + JDIMENSION image_width,
  67 + JDIMENSION image_height,
  68 + int components,
  69 + J_COLOR_SPACE color_space,
  70 + CompressConfig* config_callback) :
68 71 Pipeline(identifier, next),
69   - m(new Members(a_compress, "DCT uncompressed image",
70   - image_width, image_height, components, color_space, config_callback))
  72 + m(new Members(
  73 + a_compress,
  74 + "DCT uncompressed image",
  75 + image_width,
  76 + image_height,
  77 + components,
  78 + color_space,
  79 + config_callback))
71 80 {
72 81 }
73 82  
... ... @@ -90,8 +99,7 @@ Pl_DCT::finish()
90 99 // and decompress causes a memory leak with setjmp/longjmp. Just
91 100 // use a pointer and delete it.
92 101 Buffer* b = this->m->buf.getBuffer();
93   - if (b->getSize() == 0)
94   - {
  102 + if (b->getSize() == 0) {
95 103 // Special case: empty data will never succeed and probably
96 104 // means we're calling finish a second time from an exception
97 105 // handler.
... ... @@ -111,44 +119,32 @@ Pl_DCT::finish()
111 119 bool error = false;
112 120 // The jpeg library is a "C" library, so we use setjmp and longjmp
113 121 // for exception handling.
114   - if (setjmp(jerr.jmpbuf) == 0)
115   - {
116   - try
117   - {
118   - if (this->m->action == a_compress)
119   - {
  122 + if (setjmp(jerr.jmpbuf) == 0) {
  123 + try {
  124 + if (this->m->action == a_compress) {
120 125 compress(reinterpret_cast<void*>(&cinfo_compress), b);
121   - }
122   - else
123   - {
  126 + } else {
124 127 decompress(reinterpret_cast<void*>(&cinfo_decompress), b);
125 128 }
126   - }
127   - catch (std::exception& e)
128   - {
  129 + } catch (std::exception& e) {
129 130 // Convert an exception back to a longjmp so we can ensure
130 131 // that the right cleanup happens. This will get converted
131 132 // back to an exception.
132 133 jerr.msg = e.what();
133 134 longjmp(jerr.jmpbuf, 1);
134 135 }
135   - }
136   - else
137   - {
  136 + } else {
138 137 error = true;
139 138 }
140 139 delete b;
141 140  
142   - if (this->m->action == a_compress)
143   - {
  141 + if (this->m->action == a_compress) {
144 142 jpeg_destroy_compress(&cinfo_compress);
145 143 }
146   - if (this->m->action == a_decompress)
147   - {
  144 + if (this->m->action == a_decompress) {
148 145 jpeg_destroy_decompress(&cinfo_decompress);
149 146 }
150   - if (error)
151   - {
  147 + if (error) {
152 148 throw std::runtime_error(jerr.msg);
153 149 }
154 150 }
... ... @@ -170,8 +166,7 @@ static boolean
170 166 empty_pipeline_output_buffer(j_compress_ptr cinfo)
171 167 {
172 168 QTC::TC("libtests", "Pl_DCT empty_pipeline_output_buffer");
173   - dct_pipeline_dest* dest =
174   - reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
  169 + dct_pipeline_dest* dest = reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
175 170 dest->next->write(dest->buffer, dest->size);
176 171 dest->pub.next_output_byte = dest->buffer;
177 172 dest->pub.free_in_buffer = dest->size;
... ... @@ -182,22 +177,20 @@ static void
182 177 term_pipeline_destination(j_compress_ptr cinfo)
183 178 {
184 179 QTC::TC("libtests", "Pl_DCT term_pipeline_destination");
185   - dct_pipeline_dest* dest =
186   - reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
  180 + dct_pipeline_dest* dest = reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
187 181 dest->next->write(dest->buffer, dest->size - dest->pub.free_in_buffer);
188 182 }
189 183  
190 184 static void
191   -jpeg_pipeline_dest(j_compress_ptr cinfo,
192   - unsigned char* outbuffer, size_t size,
193   - Pipeline* next)
  185 +jpeg_pipeline_dest(
  186 + j_compress_ptr cinfo, unsigned char* outbuffer, size_t size, Pipeline* next)
194 187 {
195   - cinfo->dest = static_cast<struct jpeg_destination_mgr *>(
196   - (*cinfo->mem->alloc_small)(reinterpret_cast<j_common_ptr>(cinfo),
197   - JPOOL_PERMANENT,
198   - sizeof(dct_pipeline_dest)));
199   - dct_pipeline_dest* dest =
200   - reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
  188 + cinfo->dest =
  189 + static_cast<struct jpeg_destination_mgr*>((*cinfo->mem->alloc_small)(
  190 + reinterpret_cast<j_common_ptr>(cinfo),
  191 + JPOOL_PERMANENT,
  192 + sizeof(dct_pipeline_dest)));
  193 + dct_pipeline_dest* dest = reinterpret_cast<dct_pipeline_dest*>(cinfo->dest);
201 194 dest->pub.init_destination = init_pipeline_destination;
202 195 dest->pub.empty_output_buffer = empty_pipeline_output_buffer;
203 196 dest->pub.term_destination = term_pipeline_destination;
... ... @@ -224,20 +217,15 @@ fill_buffer_input_buffer(j_decompress_ptr)
224 217 static void
225 218 skip_buffer_input_data(j_decompress_ptr cinfo, long num_bytes)
226 219 {
227   - if (num_bytes < 0)
228   - {
229   - throw std::runtime_error(
230   - "reading jpeg: jpeg library requested"
231   - " skipping a negative number of bytes");
  220 + if (num_bytes < 0) {
  221 + throw std::runtime_error("reading jpeg: jpeg library requested"
  222 + " skipping a negative number of bytes");
232 223 }
233 224 size_t to_skip = QIntC::to_size(num_bytes);
234   - if ((to_skip > 0) && (to_skip <= cinfo->src->bytes_in_buffer))
235   - {
  225 + if ((to_skip > 0) && (to_skip <= cinfo->src->bytes_in_buffer)) {
236 226 cinfo->src->next_input_byte += to_skip;
237 227 cinfo->src->bytes_in_buffer -= to_skip;
238   - }
239   - else if (to_skip != 0)
240   - {
  228 + } else if (to_skip != 0) {
241 229 cinfo->src->next_input_byte += cinfo->src->bytes_in_buffer;
242 230 cinfo->src->bytes_in_buffer = 0;
243 231 }
... ... @@ -251,10 +239,10 @@ term_buffer_source(j_decompress_ptr)
251 239 static void
252 240 jpeg_buffer_src(j_decompress_ptr cinfo, Buffer* buffer)
253 241 {
254   - cinfo->src = reinterpret_cast<jpeg_source_mgr *>(
255   - (*cinfo->mem->alloc_small)(reinterpret_cast<j_common_ptr>(cinfo),
256   - JPOOL_PERMANENT,
257   - sizeof(jpeg_source_mgr)));
  242 + cinfo->src = reinterpret_cast<jpeg_source_mgr*>((*cinfo->mem->alloc_small)(
  243 + reinterpret_cast<j_common_ptr>(cinfo),
  244 + JPOOL_PERMANENT,
  245 + sizeof(jpeg_source_mgr)));
258 246  
259 247 jpeg_source_mgr* src = cinfo->src;
260 248 src->init_source = init_buffer_source;
... ... @@ -272,15 +260,17 @@ Pl_DCT::compress(void* cinfo_p, Buffer* b)
272 260 struct jpeg_compress_struct* cinfo =
273 261 reinterpret_cast<jpeg_compress_struct*>(cinfo_p);
274 262  
275   -#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
276   - defined(__clang__))
277   -# pragma GCC diagnostic push
278   -# pragma GCC diagnostic ignored "-Wold-style-cast"
  263 +#if ( \
  264 + (defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
  265 + defined(__clang__))
  266 +# pragma GCC diagnostic push
  267 +# pragma GCC diagnostic ignored "-Wold-style-cast"
279 268 #endif
280 269 jpeg_create_compress(cinfo);
281   -#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
282   - defined(__clang__))
283   -# pragma GCC diagnostic pop
  270 +#if ( \
  271 + (defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
  272 + defined(__clang__))
  273 +# pragma GCC diagnostic pop
284 274 #endif
285 275 static int const BUF_SIZE = 65536;
286 276 auto outbuffer_ph = std::make_unique<unsigned char[]>(BUF_SIZE);
... ... @@ -292,33 +282,29 @@ Pl_DCT::compress(void* cinfo_p, Buffer* b)
292 282 cinfo->input_components = this->m->components;
293 283 cinfo->in_color_space = this->m->color_space;
294 284 jpeg_set_defaults(cinfo);
295   - if (this->m->config_callback)
296   - {
  285 + if (this->m->config_callback) {
297 286 this->m->config_callback->apply(cinfo);
298 287 }
299 288  
300 289 jpeg_start_compress(cinfo, TRUE);
301 290  
302   - unsigned int width = cinfo->image_width *
303   - QIntC::to_uint(cinfo->input_components);
304   - size_t expected_size =
305   - QIntC::to_size(cinfo->image_height) *
  291 + unsigned int width =
  292 + cinfo->image_width * QIntC::to_uint(cinfo->input_components);
  293 + size_t expected_size = QIntC::to_size(cinfo->image_height) *
306 294 QIntC::to_size(cinfo->image_width) *
307 295 QIntC::to_size(cinfo->input_components);
308   - if (b->getSize() != expected_size)
309   - {
  296 + if (b->getSize() != expected_size) {
310 297 throw std::runtime_error(
311 298 "Pl_DCT: image buffer size = " +
312   - QUtil::uint_to_string(b->getSize()) + "; expected size = " +
313   - QUtil::uint_to_string(expected_size));
  299 + QUtil::uint_to_string(b->getSize()) +
  300 + "; expected size = " + QUtil::uint_to_string(expected_size));
314 301 }
315 302 JSAMPROW row_pointer[1];
316 303 unsigned char* buffer = b->getBuffer();
317   - while (cinfo->next_scanline < cinfo->image_height)
318   - {
  304 + while (cinfo->next_scanline < cinfo->image_height) {
319 305 // We already verified that the buffer is big enough.
320 306 row_pointer[0] = &buffer[cinfo->next_scanline * width];
321   - (void) jpeg_write_scanlines(cinfo, row_pointer, 1);
  307 + (void)jpeg_write_scanlines(cinfo, row_pointer, 1);
322 308 }
323 309 jpeg_finish_compress(cinfo);
324 310 this->getNext()->finish();
... ... @@ -330,33 +316,35 @@ Pl_DCT::decompress(void* cinfo_p, Buffer* b)
330 316 struct jpeg_decompress_struct* cinfo =
331 317 reinterpret_cast<jpeg_decompress_struct*>(cinfo_p);
332 318  
333   -#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
334   - defined(__clang__))
335   -# pragma GCC diagnostic push
336   -# pragma GCC diagnostic ignored "-Wold-style-cast"
  319 +#if ( \
  320 + (defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
  321 + defined(__clang__))
  322 +# pragma GCC diagnostic push
  323 +# pragma GCC diagnostic ignored "-Wold-style-cast"
337 324 #endif
338 325 jpeg_create_decompress(cinfo);
339   -#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
340   - defined(__clang__))
341   -# pragma GCC diagnostic pop
  326 +#if ( \
  327 + (defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
  328 + defined(__clang__))
  329 +# pragma GCC diagnostic pop
342 330 #endif
343 331 jpeg_buffer_src(cinfo, b);
344 332  
345   - (void) jpeg_read_header(cinfo, TRUE);
346   - (void) jpeg_calc_output_dimensions(cinfo);
  333 + (void)jpeg_read_header(cinfo, TRUE);
  334 + (void)jpeg_calc_output_dimensions(cinfo);
347 335  
348   - unsigned int width = cinfo->output_width *
349   - QIntC::to_uint(cinfo->output_components);
350   - JSAMPARRAY buffer = (*cinfo->mem->alloc_sarray)
351   - (reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE, width, 1);
  336 + unsigned int width =
  337 + cinfo->output_width * QIntC::to_uint(cinfo->output_components);
  338 + JSAMPARRAY buffer = (*cinfo->mem->alloc_sarray)(
  339 + reinterpret_cast<j_common_ptr>(cinfo), JPOOL_IMAGE, width, 1);
352 340  
353   - (void) jpeg_start_decompress(cinfo);
354   - while (cinfo->output_scanline < cinfo->output_height)
355   - {
356   - (void) jpeg_read_scanlines(cinfo, buffer, 1);
357   - this->getNext()->write(reinterpret_cast<unsigned char*>(buffer[0]),
358   - width * sizeof(buffer[0][0]));
  341 + (void)jpeg_start_decompress(cinfo);
  342 + while (cinfo->output_scanline < cinfo->output_height) {
  343 + (void)jpeg_read_scanlines(cinfo, buffer, 1);
  344 + this->getNext()->write(
  345 + reinterpret_cast<unsigned char*>(buffer[0]),
  346 + width * sizeof(buffer[0][0]));
359 347 }
360   - (void) jpeg_finish_decompress(cinfo);
  348 + (void)jpeg_finish_decompress(cinfo);
361 349 this->getNext()->finish();
362 350 }
... ...