aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/aes.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-04-02 23:14:10 +0200
committerJay Berkenbilt <ejb@ql.org>2022-04-04 14:10:40 +0200
commit12f1eb15ca3fed6310402847559a7c99d3c77847 (patch)
tree8935675b623c6f3b4914b8b44f7fa5f2816a9241 /libtests/aes.cc
parentf20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff)
downloadqpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst
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
Diffstat (limited to 'libtests/aes.cc')
-rw-r--r--libtests/aes.cc97
1 files changed, 34 insertions, 63 deletions
diff --git a/libtests/aes.cc b/libtests/aes.cc
index 71348744..3ad491cc 100644
--- a/libtests/aes.cc
+++ b/libtests/aes.cc
@@ -1,14 +1,15 @@
#include <qpdf/Pl_AES_PDF.hh>
#include <qpdf/Pl_StdioFile.hh>
-#include <qpdf/QUtil.hh>
#include <qpdf/QIntC.hh>
+#include <qpdf/QUtil.hh>
-#include <stdio.h>
-#include <string.h>
#include <iostream>
+#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
-static void usage()
+static void
+usage()
{
std::cerr << "Usage: aes options hex-key infile outfile" << std::endl
<< " -cbc -- disable CBC mode" << std::endl
@@ -16,13 +17,15 @@ static void usage()
<< " -encrypt -- encrypt" << std::endl
<< " -decrypt -- decrypt CBC mode" << std::endl
<< " -zero-iv -- use zero initialization vector" << std::endl
- << " -static-iv -- use static initialization vector" << std::endl
+ << " -static-iv -- use static initialization vector"
+ << std::endl
<< " -no-padding -- disable padding" << std::endl
<< "Options must precede key and file names." << std::endl;
exit(2);
}
-int main(int argc, char* argv[])
+int
+main(int argc, char* argv[])
{
bool encrypt = true;
bool cbc_mode = true;
@@ -33,58 +36,36 @@ int main(int argc, char* argv[])
bool static_iv = false;
bool disable_padding = false;
- for (int i = 1; i < argc; ++i)
- {
+ for (int i = 1; i < argc; ++i) {
char* arg = argv[i];
- if ((arg[0] == '-') || (arg[0] == '+'))
- {
- if (strcmp(arg, "-cbc") == 0)
- {
+ if ((arg[0] == '-') || (arg[0] == '+')) {
+ if (strcmp(arg, "-cbc") == 0) {
cbc_mode = false;
- }
- else if (strcmp(arg, "+cbc") == 0)
- {
+ } else if (strcmp(arg, "+cbc") == 0) {
cbc_mode = true;
- }
- else if (strcmp(arg, "-decrypt") == 0)
- {
+ } else if (strcmp(arg, "-decrypt") == 0) {
encrypt = false;
- }
- else if (strcmp(arg, "-encrypt") == 0)
- {
+ } else if (strcmp(arg, "-encrypt") == 0) {
encrypt = true;
- }
- else if (strcmp(arg, "-zero-iv") == 0)
- {
+ } else if (strcmp(arg, "-zero-iv") == 0) {
zero_iv = true;
- }
- else if (strcmp(arg, "-static-iv") == 0)
- {
+ } else if (strcmp(arg, "-static-iv") == 0) {
static_iv = true;
- }
- else if (strcmp(arg, "-no-padding") == 0)
- {
+ } else if (strcmp(arg, "-no-padding") == 0) {
disable_padding = true;
- }
- else
- {
+ } else {
usage();
}
- }
- else if (argc == i + 3)
- {
+ } else if (argc == i + 3) {
hexkey = argv[i];
- infilename = argv[i+1];
- outfilename = argv[i+2];
+ infilename = argv[i + 1];
+ outfilename = argv[i + 2];
break;
- }
- else
- {
+ } else {
usage();
}
}
- if (outfilename == 0)
- {
+ if (outfilename == 0) {
usage();
}
@@ -94,50 +75,40 @@ int main(int argc, char* argv[])
FILE* infile = QUtil::safe_fopen(infilename, "rb");
FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
unsigned char* key = new unsigned char[keylen];
- for (unsigned int i = 0; i < strlen(hexkey); i += 2)
- {
+ for (unsigned int i = 0; i < strlen(hexkey); i += 2) {
char t[3];
t[0] = hexkey[i];
t[1] = hexkey[i + 1];
t[2] = '\0';
long val = strtol(t, 0, 16);
- key[i/2] = static_cast<unsigned char>(val);
+ key[i / 2] = static_cast<unsigned char>(val);
}
Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
Pl_AES_PDF* aes = new Pl_AES_PDF("aes_128_cbc", out, encrypt, key, keylen);
- delete [] key;
+ delete[] key;
key = 0;
- if (! cbc_mode)
- {
+ if (!cbc_mode) {
aes->disableCBC();
}
- if (zero_iv)
- {
+ if (zero_iv) {
aes->useZeroIV();
- }
- else if (static_iv)
- {
+ } else if (static_iv) {
aes->useStaticIV();
}
- if (disable_padding)
- {
+ if (disable_padding) {
aes->disablePadding();
}
// 16 < buffer size, buffer_size is not a multiple of 8 for testing
unsigned char buf[83];
bool done = false;
- while (! done)
- {
+ while (!done) {
size_t len = fread(buf, 1, sizeof(buf), infile);
- if (len <= 0)
- {
+ if (len <= 0) {
done = true;
- }
- else
- {
+ } else {
aes->write(buf, len);
}
}