2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_test.h>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_RC4.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2013-02-28 21:45:11 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <iostream>
|
2022-03-07 13:46:53 +00:00
|
|
|
|
2019-11-05 23:44:16 +00:00
|
|
|
static void
|
|
|
|
other_tests()
|
|
|
|
{
|
|
|
|
// Test cases not covered by the pipeline: string as key, convert
|
|
|
|
// in place
|
|
|
|
RC4 r(reinterpret_cast<unsigned char const*>("quack"));
|
2022-02-05 13:15:07 +00:00
|
|
|
auto data = std::make_unique<unsigned char[]>(6);
|
2019-11-05 23:44:16 +00:00
|
|
|
memcpy(data.get(), "potato", 6);
|
2022-05-03 21:43:07 +00:00
|
|
|
r.process(data.get(), 6, data.get());
|
2019-11-05 23:44:16 +00:00
|
|
|
assert(memcmp(data.get(), "\xa5\x6f\xe7\x27\x2b\x5c", 6) == 0);
|
|
|
|
std::cout << "passed" << std::endl;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2019-11-05 23:44:16 +00:00
|
|
|
if ((argc == 2) && (strcmp(argv[1], "other") == 0)) {
|
|
|
|
other_tests();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
if (argc != 4) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << "Usage: rc4 hex-key infile outfile" << std::endl;
|
|
|
|
exit(2);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* hexkey = argv[1];
|
|
|
|
char* infilename = argv[2];
|
|
|
|
char* outfilename = argv[3];
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
|
2012-06-20 15:20:57 +00:00
|
|
|
unsigned int keylen = hexkeylen / 2;
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* key = new unsigned char[keylen + 1];
|
2008-04-29 12:55:25 +00:00
|
|
|
key[keylen] = '\0';
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* infile = QUtil::safe_fopen(infilename, "rb");
|
2008-04-29 12:55:25 +00:00
|
|
|
for (unsigned int i = 0; i < strlen(hexkey); i += 2) {
|
2022-02-08 14:18:08 +00:00
|
|
|
char t[3];
|
|
|
|
t[0] = hexkey[i];
|
|
|
|
t[1] = hexkey[i + 1];
|
|
|
|
t[2] = '\0';
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:46:50 +00:00
|
|
|
long val = strtol(t, nullptr, 16);
|
2022-02-08 14:18:08 +00:00
|
|
|
key[i / 2] = static_cast<unsigned char>(val);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* out = new Pl_StdioFile("stdout", outfile);
|
2008-04-29 12:55:25 +00:00
|
|
|
// Use a small buffer size (64) for testing
|
2023-05-20 12:24:10 +00:00
|
|
|
auto* rc4 = new Pl_RC4("rc4", out, key, QIntC::to_int(keylen), 64U);
|
2008-04-29 12:55:25 +00:00
|
|
|
delete[] key;
|
|
|
|
|
|
|
|
// 64 < buffer size < 512, buffer_size is not a power of 2 for testing
|
|
|
|
unsigned char buf[100];
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), infile);
|
|
|
|
if (len <= 0) {
|
|
|
|
done = true;
|
|
|
|
} else {
|
|
|
|
rc4->write(buf, len);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
rc4->finish();
|
|
|
|
delete rc4;
|
|
|
|
delete out;
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|