-
Notifications
You must be signed in to change notification settings - Fork 13
/
scrmbl.c
122 lines (105 loc) · 2.9 KB
/
scrmbl.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Copyright (C) 2013 Lars Brinkhoff <lars@nocrew.org>
Copyright (C) 2020 Adam Sampson <ats@offog.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dis.h"
static void
usage (char **argv)
{
fprintf (stderr, "Usage: %s [-d] [-v] [-W<input word format>] [-X<output word format>] <password> <input file> <output file>\n\n", argv[0]);
usage_word_format ();
exit (1);
}
int
main (int argc, char **argv)
{
int i;
word_t *input = NULL;
int input_count = 0;
int input_size = 0;
int decrypt = 0;
FILE *file;
int opt;
word_t *output = NULL;
word_t password;
int verbose = 0;
word_t word;
while ((opt = getopt (argc, argv, "dvW:X:")) != -1)
{
switch (opt)
{
case 'd':
decrypt = 1;
break;
case 'v':
verbose = 1;
break;
case 'W':
if (parse_input_word_format (optarg))
usage (argv);
break;
case 'X':
if (parse_output_word_format (optarg))
usage (argv);
break;
default:
usage (argv);
}
}
if (optind != argc - 3)
usage (argv);
password = ascii_to_sixbit (argv[optind]);
/* Read the whole input into memory. */
file = fopen (argv[optind + 1], "rb");
if (file == NULL)
{
fprintf (stderr, "%s: Error opening %s: %s\n",
argv[0], argv[optind + 1], strerror (errno));
return 1;
}
while ((word = get_word (file)) != -1)
{
if (input_count >= input_size)
{
input_size += 1024;
input = realloc (input, input_size * sizeof (*input));
if (input == NULL)
{
fprintf (stderr, "out of memory\n");
return 1;
}
}
input[input_count++] = word;
}
fclose (file);
output = calloc (input_count, sizeof (*output));
if (output == NULL)
{
fprintf (stderr, "out of memory\n");
return 1;
}
scramble (decrypt, verbose, password, input, output, input_count);
file = fopen (argv[optind + 2], "wb");
for (i = 0; i < input_count; i++)
{
write_word (file, output[i]);
}
flush_word (file);
fclose (file);
free (input);
free (output);
return 0;
}