Commit 00ed5af50216348e170b48d531801f46eac0fbab
1 parent
01d6f04a
remove use of float for version comparison
git-svn-id: svn+q:///qpdf/trunk@984 71b93d88-0707-0410-a8cf-f5a4172ac649
Showing
3 changed files
with
67 additions
and
14 deletions
TODO
include/qpdf/QPDFWriter.hh
| @@ -207,7 +207,9 @@ class QPDFWriter | @@ -207,7 +207,9 @@ class QPDFWriter | ||
| 207 | char const* user_password, char const* owner_password, | 207 | char const* user_password, char const* owner_password, |
| 208 | bool allow_accessibility, bool allow_extract, | 208 | bool allow_accessibility, bool allow_extract, |
| 209 | qpdf_r3_print_e print, qpdf_r3_modify_e modify); | 209 | qpdf_r3_print_e print, qpdf_r3_modify_e modify); |
| 210 | - void disableIncompatbleEncryption(float v); | 210 | + void disableIncompatibleEncryption(int major, int minor); |
| 211 | + void parseVersion(std::string const& version, int& major, int& minor) const; | ||
| 212 | + int compareVersions(int major1, int minor1, int major2, int minor2) const; | ||
| 211 | void setEncryptionParameters( | 213 | void setEncryptionParameters( |
| 212 | char const* user_password, char const* owner_password, | 214 | char const* user_password, char const* owner_password, |
| 213 | int V, int R, int key_len, std::set<int>& bits_to_clear); | 215 | int V, int R, int key_len, std::set<int>& bits_to_clear); |
libqpdf/QPDFWriter.cc
| @@ -112,9 +112,13 @@ QPDFWriter::setMinimumPDFVersion(std::string const& version) | @@ -112,9 +112,13 @@ QPDFWriter::setMinimumPDFVersion(std::string const& version) | ||
| 112 | } | 112 | } |
| 113 | else | 113 | else |
| 114 | { | 114 | { |
| 115 | - float v = atof(version.c_str()); | ||
| 116 | - float mv = atof(this->min_pdf_version.c_str()); | ||
| 117 | - if (v > mv) | 115 | + int old_major = 0; |
| 116 | + int old_minor = 0; | ||
| 117 | + int min_major = 0; | ||
| 118 | + int min_minor = 0; | ||
| 119 | + parseVersion(version, old_major, old_minor); | ||
| 120 | + parseVersion(this->min_pdf_version, min_major, min_minor); | ||
| 121 | + if (compareVersions(old_minor, old_minor, min_minor, min_minor) > 0) | ||
| 118 | { | 122 | { |
| 119 | QTC::TC("qpdf", "QPDFWriter increasing minimum version"); | 123 | QTC::TC("qpdf", "QPDFWriter increasing minimum version"); |
| 120 | set_version = true; | 124 | set_version = true; |
| @@ -354,7 +358,7 @@ QPDFWriter::copyEncryptionParameters() | @@ -354,7 +358,7 @@ QPDFWriter::copyEncryptionParameters() | ||
| 354 | } | 358 | } |
| 355 | 359 | ||
| 356 | void | 360 | void |
| 357 | -QPDFWriter::disableIncompatbleEncryption(float v) | 361 | +QPDFWriter::disableIncompatibleEncryption(int major, int minor) |
| 358 | { | 362 | { |
| 359 | if (! this->encrypted) | 363 | if (! this->encrypted) |
| 360 | { | 364 | { |
| @@ -362,7 +366,7 @@ QPDFWriter::disableIncompatbleEncryption(float v) | @@ -362,7 +366,7 @@ QPDFWriter::disableIncompatbleEncryption(float v) | ||
| 362 | } | 366 | } |
| 363 | 367 | ||
| 364 | bool disable = false; | 368 | bool disable = false; |
| 365 | - if (v < 1.3) | 369 | + if (compareVersions(major, minor, 1, 3) < 0) |
| 366 | { | 370 | { |
| 367 | disable = true; | 371 | disable = true; |
| 368 | } | 372 | } |
| @@ -370,21 +374,21 @@ QPDFWriter::disableIncompatbleEncryption(float v) | @@ -370,21 +374,21 @@ QPDFWriter::disableIncompatbleEncryption(float v) | ||
| 370 | { | 374 | { |
| 371 | int V = atoi(encryption_dictionary["/V"].c_str()); | 375 | int V = atoi(encryption_dictionary["/V"].c_str()); |
| 372 | int R = atoi(encryption_dictionary["/R"].c_str()); | 376 | int R = atoi(encryption_dictionary["/R"].c_str()); |
| 373 | - if (v < 1.4) | 377 | + if (compareVersions(major, minor, 1, 4) < 0) |
| 374 | { | 378 | { |
| 375 | if ((V > 1) || (R > 2)) | 379 | if ((V > 1) || (R > 2)) |
| 376 | { | 380 | { |
| 377 | disable = true; | 381 | disable = true; |
| 378 | } | 382 | } |
| 379 | } | 383 | } |
| 380 | - else if (v < 1.5) | 384 | + else if (compareVersions(major, minor, 1, 5) < 0) |
| 381 | { | 385 | { |
| 382 | if ((V > 2) || (R > 3)) | 386 | if ((V > 2) || (R > 3)) |
| 383 | { | 387 | { |
| 384 | disable = true; | 388 | disable = true; |
| 385 | } | 389 | } |
| 386 | } | 390 | } |
| 387 | - else if (v < 1.6) | 391 | + else if (compareVersions(major, minor, 1, 6) < 0) |
| 388 | { | 392 | { |
| 389 | if (this->encrypt_use_aes) | 393 | if (this->encrypt_use_aes) |
| 390 | { | 394 | { |
| @@ -400,6 +404,53 @@ QPDFWriter::disableIncompatbleEncryption(float v) | @@ -400,6 +404,53 @@ QPDFWriter::disableIncompatbleEncryption(float v) | ||
| 400 | } | 404 | } |
| 401 | 405 | ||
| 402 | void | 406 | void |
| 407 | +QPDFWriter::parseVersion(std::string const& version, | ||
| 408 | + int& major, int& minor) const | ||
| 409 | +{ | ||
| 410 | + major = atoi(version.c_str()); | ||
| 411 | + minor = 0; | ||
| 412 | + size_t p = version.find('.'); | ||
| 413 | + if ((p != std::string::npos) && (version.length() > p)) | ||
| 414 | + { | ||
| 415 | + minor = atoi(version.substr(p + 1).c_str()); | ||
| 416 | + } | ||
| 417 | + std::string tmp = QUtil::int_to_string(major) + "." + | ||
| 418 | + QUtil::int_to_string(minor); | ||
| 419 | + if (tmp != version) | ||
| 420 | + { | ||
| 421 | + throw std::logic_error( | ||
| 422 | + "INTERNAL ERROR: QPDFWriter::parseVersion" | ||
| 423 | + " called with invalid version number " + version); | ||
| 424 | + } | ||
| 425 | +} | ||
| 426 | + | ||
| 427 | +int | ||
| 428 | +QPDFWriter::compareVersions(int major1, int minor1, | ||
| 429 | + int major2, int minor2) const | ||
| 430 | +{ | ||
| 431 | + if (major1 < major2) | ||
| 432 | + { | ||
| 433 | + return -1; | ||
| 434 | + } | ||
| 435 | + else if (major1 > major2) | ||
| 436 | + { | ||
| 437 | + return 1; | ||
| 438 | + } | ||
| 439 | + else if (minor1 < minor2) | ||
| 440 | + { | ||
| 441 | + return -1; | ||
| 442 | + } | ||
| 443 | + else if (minor1 > minor2) | ||
| 444 | + { | ||
| 445 | + return 1; | ||
| 446 | + } | ||
| 447 | + else | ||
| 448 | + { | ||
| 449 | + return 0; | ||
| 450 | + } | ||
| 451 | +} | ||
| 452 | + | ||
| 453 | +void | ||
| 403 | QPDFWriter::setEncryptionParametersInternal( | 454 | QPDFWriter::setEncryptionParametersInternal( |
| 404 | int V, int R, int key_len, long P, | 455 | int V, int R, int key_len, long P, |
| 405 | std::string const& O, std::string const& U, | 456 | std::string const& O, std::string const& U, |
| @@ -1491,9 +1542,11 @@ QPDFWriter::write() | @@ -1491,9 +1542,11 @@ QPDFWriter::write() | ||
| 1491 | 1542 | ||
| 1492 | if (! this->forced_pdf_version.empty()) | 1543 | if (! this->forced_pdf_version.empty()) |
| 1493 | { | 1544 | { |
| 1494 | - float v = atof(this->forced_pdf_version.c_str()); | ||
| 1495 | - disableIncompatbleEncryption(v); | ||
| 1496 | - if (v < 1.5) | 1545 | + int major = 0; |
| 1546 | + int minor = 0; | ||
| 1547 | + parseVersion(this->forced_pdf_version, major, minor); | ||
| 1548 | + disableIncompatibleEncryption(major, minor); | ||
| 1549 | + if (compareVersions(major, minor, 1, 5) < 0) | ||
| 1497 | { | 1550 | { |
| 1498 | QTC::TC("qpdf", "QPDFWriter forcing object stream disable"); | 1551 | QTC::TC("qpdf", "QPDFWriter forcing object stream disable"); |
| 1499 | this->object_stream_mode = qpdf_o_disable; | 1552 | this->object_stream_mode = qpdf_o_disable; |