aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/rc4.cc
blob: 306d088289745c071adf74d74f3ed0953b8f3aeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <qpdf/assert_test.h>

#include <qpdf/Pl_RC4.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QIntC.hh>
#include <qpdf/QUtil.hh>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

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"));
    auto data = std::make_unique<unsigned char[]>(6);
    memcpy(data.get(), "potato", 6);
    r.process(data.get(), 6, data.get());
    assert(memcmp(data.get(), "\xa5\x6f\xe7\x27\x2b\x5c", 6) == 0);
    std::cout << "passed" << std::endl;
}

int
main(int argc, char* argv[])
{
    if ((argc == 2) && (strcmp(argv[1], "other") == 0)) {
        other_tests();
        return 0;
    }

    if (argc != 4) {
        std::cerr << "Usage: rc4 hex-key infile outfile" << std::endl;
        exit(2);
    }

    char* hexkey = argv[1];
    char* infilename = argv[2];
    char* outfilename = argv[3];
    unsigned int hexkeylen = QIntC::to_uint(strlen(hexkey));
    unsigned int keylen = hexkeylen / 2;
    auto* key = new unsigned char[keylen + 1];
    key[keylen] = '\0';

    FILE* infile = QUtil::safe_fopen(infilename, "rb");
    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, nullptr, 16);
        key[i / 2] = static_cast<unsigned char>(val);
    }

    FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
    auto* out = new Pl_StdioFile("stdout", outfile);
    // Use a small buffer size (64) for testing
    auto* rc4 = new Pl_RC4("rc4", out, key, QIntC::to_int(keylen), 64U);
    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) {
        size_t len = fread(buf, 1, sizeof(buf), infile);
        if (len <= 0) {
            done = true;
        } else {
            rc4->write(buf, len);
        }
    }
    rc4->finish();
    delete rc4;
    delete out;
    fclose(infile);
    fclose(outfile);
    return 0;
}