-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset1_utils.c
202 lines (173 loc) · 4.76 KB
/
set1_utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "set1_utils.h"
#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2
int get_line(FILE *stream, char *prompt, char *buff, size_t s) {
int ch, extra;
// Get line with buffer overrun protection.
if (prompt != NULL) {
fprintf(stdout, "%s", prompt);
fflush(stdout);
}
if (fgets(buff, s, stream) == NULL)
return NO_INPUT;
// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getc(stream)) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '\0';
return OK;
}
/* A table of letter frequencies for English. 'a' is index 0.
* These values roughly add up to 100.
*/
static double english_freq[26] = { 8.12, 1.49, 2.78, 4.25, 12.70, 2.23, 2.01, 6.09,
6.97, 0.15, 0.77, 4.02, 2.41, 6.75, 7.51, 1.93,
0.09, 5.98, 6.33, 9.06, 2.76, 0.98, 2.36, 0.15,
1.98, 0.07 };
double english_score(char *sentence)
{
// Count letter occurrences.
double freq[26] = { 0 };
int chars = 0;
for (char *s = sentence; *s; s++) {
char c = *s;
if (c >= 'a' && c <= 'z') {
chars++;
freq[c - 'a']++;
}
if (c >= 'A' && c <= 'Z') {
chars++;
freq[c - 'A']++;
}
}
// No letters found
if (chars == 0)
return 0;
// Convert absolute frequencies to relative frequencies in percent
for (int i = 0; i < 26; i++)
freq[i] = (freq[i] / chars) * 100;
// Calculate errors
double err = 0;
for (int i = 0; i < 26; i++)
err += fabs(english_freq[i] - freq[i]);
if (err > 100)
err = 100;
return 100 - err;
}
char num_to_base64(char digit)
{
if (digit < 26)
return digit + 'A';
else if (digit < 52)
return digit + 'a' - 26;
else if (digit < 62)
return digit + '0' - 52;
else if (digit == 62)
return '+';
else if (digit == 63)
return '/';
else
return '-';
}
char base64_to_num(char digit)
{
if (digit >= 'A' && digit <= 'Z')
return digit - 'A';
else if (digit >= 'a' && digit <= 'z')
return digit - 'a' + 26;
else if (digit >= '0' && digit <= '9')
return digit - '0' + 52;
else if (digit == '+')
return 62;
else if (digit == '/')
return 63;
else if (digit == '-')
return 64;
else
return 65;
}
int base64_decode(char *input, int len)
{
// NOTE: seems output length is wrong. Theres a decoding off by one error.
// Then the stream gets offset/corrupted.
if (len % 4 != 0)
fprintf(stderr, "Pad the input\n");
int i;
int basei = 2;
for (i = 3; i < len; i += 4) {
char res[4] = { 0 };
res[0] = base64_to_num(input[i - 3]);
res[1] = base64_to_num(input[i - 2]);
res[2] = base64_to_num(input[i - 1]);
res[3] = base64_to_num(input[i]);
input[basei - 2] = (res[0] << 2) | ((res[1] >> 4) & 0x3);
input[basei - 1] = (res[1] << 4) | ((res[2] >> 2) & 0x0f);
input[basei] = (res[2] << 6) | res[3];
basei += 3;
}
return len / 4 * 3;
}
char char_to_hex(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}
char hex_to_char(char hex)
{
// 10 = 'a', 'a' = 97
if ((int)hex < 10)
return hex + 48;
else
return hex + 87;
}
int chars_to_hex(char *chars, int len)
{
// We could pad by zeros, but it would be error prone on the user's end
if (len % 2 != 0)
return -1;
for (int i = 0; i < len / 2; i++) {
char seg0 = char_to_hex(chars[2 * i]);
char seg1 = char_to_hex(chars[2 * i + 1]);
if (seg0 == -1 || seg1 == -1)
return -1;
chars[i] = (seg0 << 4) | seg1;
}
return len / 2;
}
int hex_to_chars(char *hex, int len)
{
for (int i = len * 2 - 1; i > 0; i -= 2) {
char seg0 = (hex[i / 2] & 0xf0) >> 4;
char seg1 = (hex[i / 2] & 0x0f);
hex[i - 1] = hex_to_char(seg0);
hex[i] = hex_to_char(seg1);
}
return len * 2;
}
char *alloc_hex_to_chars(char *hex, int len)
{
char *string;
if ((string = malloc(len * 2 + 1)) == NULL)
return NULL;
string[len * 2] = '\0';
for (int i = 0; i < len; i++) {
char seg0 = (hex[i] & 0xf0) >> 4;
char seg1 = (hex[i] & 0x0f);
string[2 * i] = hex_to_char(seg0);
string[2 * i + 1] = hex_to_char(seg1);
}
return hex;
}