Commit 235d8f28f8b7de0f1fea3f8fecc5af6c3917c650
1 parent
b8b273d1
Increase random data provider support
Add a method to get the current random data provider, and document and test the method for resetting it.
Showing
4 changed files
with
40 additions
and
1 deletions
ChangeLog
| 1 | +2013-12-16 Jay Berkenbilt <ejb@ql.org> | ||
| 2 | + | ||
| 3 | + * Document and make explicit that passing null to | ||
| 4 | + QUtil::setRandomDataProvider() resets the random data provider. | ||
| 5 | + | ||
| 6 | + * Provide QUtil::getRandomDataProvider(). | ||
| 7 | + | ||
| 1 | 2013-12-14 Jay Berkenbilt <ejb@ql.org> | 8 | 2013-12-14 Jay Berkenbilt <ejb@ql.org> |
| 2 | 9 | ||
| 3 | * Allow anyspace rather than just newline to follow xref header. | 10 | * Allow anyspace rather than just newline to follow xref header. |
include/qpdf/QUtil.hh
| @@ -142,9 +142,20 @@ namespace QUtil | @@ -142,9 +142,20 @@ namespace QUtil | ||
| 142 | // memory for the RandomDataProvider. This method modifies a | 142 | // memory for the RandomDataProvider. This method modifies a |
| 143 | // static variable. If you are providing your own random data | 143 | // static variable. If you are providing your own random data |
| 144 | // provider, you should call this at the beginning of your program | 144 | // provider, you should call this at the beginning of your program |
| 145 | - // before creating any QPDF objects. | 145 | + // before creating any QPDF objects. Passing a null to this |
| 146 | + // method will reset the library back to whichever of the built-in | ||
| 147 | + // random data handlers is appropriate basedon how qpdf was | ||
| 148 | + // compiled. | ||
| 146 | QPDF_DLL | 149 | QPDF_DLL |
| 147 | void setRandomDataProvider(RandomDataProvider*); | 150 | void setRandomDataProvider(RandomDataProvider*); |
| 151 | + | ||
| 152 | + // This returns the random data provider that would be used the | ||
| 153 | + // next time qpdf needs random data. It will never return null. | ||
| 154 | + // If no random data provider has been provided and the library | ||
| 155 | + // was not compiled with any random data provider available, an | ||
| 156 | + // exception will be thrown. | ||
| 157 | + QPDF_DLL | ||
| 158 | + RandomDataProvider* getRandomDataProvider(); | ||
| 148 | }; | 159 | }; |
| 149 | 160 | ||
| 150 | #endif // __QUTIL_HH__ | 161 | #endif // __QUTIL_HH__ |
libqpdf/QUtil.cc
| @@ -423,6 +423,8 @@ initialize_random_data_provider() | @@ -423,6 +423,8 @@ initialize_random_data_provider() | ||
| 423 | random_data_provider = insecure_random_data_provider; | 423 | random_data_provider = insecure_random_data_provider; |
| 424 | } | 424 | } |
| 425 | } | 425 | } |
| 426 | + // QUtil.hh has comments indicating that getRandomDataProvider(), | ||
| 427 | + // which calls this method, never returns null. | ||
| 426 | if (random_data_provider == 0) | 428 | if (random_data_provider == 0) |
| 427 | { | 429 | { |
| 428 | throw std::logic_error("QPDF has no random data provider"); | 430 | throw std::logic_error("QPDF has no random data provider"); |
| @@ -435,6 +437,13 @@ QUtil::setRandomDataProvider(RandomDataProvider* p) | @@ -435,6 +437,13 @@ QUtil::setRandomDataProvider(RandomDataProvider* p) | ||
| 435 | random_data_provider = p; | 437 | random_data_provider = p; |
| 436 | } | 438 | } |
| 437 | 439 | ||
| 440 | +RandomDataProvider* | ||
| 441 | +QUtil::getRandomDataProvider() | ||
| 442 | +{ | ||
| 443 | + initialize_random_data_provider(); | ||
| 444 | + return random_data_provider; | ||
| 445 | +} | ||
| 446 | + | ||
| 438 | void | 447 | void |
| 439 | QUtil::initializeWithRandomBytes(unsigned char* data, size_t len) | 448 | QUtil::initializeWithRandomBytes(unsigned char* data, size_t len) |
| 440 | { | 449 | { |
libtests/random.cc
| @@ -24,6 +24,7 @@ class BogusRandomDataProvider: public RandomDataProvider | @@ -24,6 +24,7 @@ class BogusRandomDataProvider: public RandomDataProvider | ||
| 24 | 24 | ||
| 25 | int main() | 25 | int main() |
| 26 | { | 26 | { |
| 27 | + RandomDataProvider* orig_rdp = QUtil::getRandomDataProvider(); | ||
| 27 | long r1 = QUtil::random(); | 28 | long r1 = QUtil::random(); |
| 28 | long r2 = QUtil::random(); | 29 | long r2 = QUtil::random(); |
| 29 | if (r1 == r2) | 30 | if (r1 == r2) |
| @@ -48,6 +49,11 @@ int main() | @@ -48,6 +49,11 @@ int main() | ||
| 48 | #endif | 49 | #endif |
| 49 | BogusRandomDataProvider brdp; | 50 | BogusRandomDataProvider brdp; |
| 50 | QUtil::setRandomDataProvider(&brdp); | 51 | QUtil::setRandomDataProvider(&brdp); |
| 52 | + if (QUtil::getRandomDataProvider() != &brdp) | ||
| 53 | + { | ||
| 54 | + std::cout << "fail: getRandomDataProvider didn't" | ||
| 55 | + " return our provider\n"; | ||
| 56 | + } | ||
| 51 | r1 = QUtil::random(); | 57 | r1 = QUtil::random(); |
| 52 | r2 = QUtil::random(); | 58 | r2 = QUtil::random(); |
| 53 | if (r1 != r2) | 59 | if (r1 != r2) |
| @@ -63,6 +69,12 @@ int main() | @@ -63,6 +69,12 @@ int main() | ||
| 63 | { | 69 | { |
| 64 | std::cout << "fail: bogus random didn't provide correct bytes\n"; | 70 | std::cout << "fail: bogus random didn't provide correct bytes\n"; |
| 65 | } | 71 | } |
| 72 | + QUtil::setRandomDataProvider(0); | ||
| 73 | + if (QUtil::getRandomDataProvider() != orig_rdp) | ||
| 74 | + { | ||
| 75 | + std::cout << "fail: passing null to setRandomDataProvider " | ||
| 76 | + "didn't reset the random data provider\n"; | ||
| 77 | + } | ||
| 66 | std::cout << "random: end of tests\n"; | 78 | std::cout << "random: end of tests\n"; |
| 67 | return 0; | 79 | return 0; |
| 68 | } | 80 | } |