-
Notifications
You must be signed in to change notification settings - Fork 0
/
steg.c
335 lines (274 loc) · 11 KB
/
steg.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
Steg, by Zoe Blade
Convert an ASCII message into a series of sine waves.
This work is made available under the Creative Commons Attribution license.
https://creativecommons.org/licenses/by/4.0/
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "wavfile.c"
#define M_TAU 2 * M_PI
void usage();
FILE *wav;
uint8_t charset[768] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18,
0x00, 0x00, 0x18, 0x00, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00,
0x66, 0x66, 0xff, 0x66, 0xff, 0x66, 0x66, 0x00, 0x18, 0x3e, 0x60, 0x3c,
0x06, 0x7c, 0x18, 0x00, 0x62, 0x66, 0x0c, 0x18, 0x30, 0x66, 0x46, 0x00,
0x3c, 0x66, 0x3c, 0x38, 0x67, 0x66, 0x3f, 0x00, 0x06, 0x0c, 0x18, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x18, 0x0c, 0x00,
0x30, 0x18, 0x0c, 0x0c, 0x0c, 0x18, 0x30, 0x00, 0x00, 0x66, 0x3c, 0xff,
0x3c, 0x66, 0x00, 0x00, 0x00, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30, 0x00, 0x00, 0x00, 0x7e,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x00, 0x3c, 0x66, 0x6e, 0x76,
0x66, 0x66, 0x3c, 0x00, 0x18, 0x18, 0x38, 0x18, 0x18, 0x18, 0x7e, 0x00,
0x3c, 0x66, 0x06, 0x0c, 0x30, 0x60, 0x7e, 0x00, 0x3c, 0x66, 0x06, 0x1c,
0x06, 0x66, 0x3c, 0x00, 0x06, 0x0e, 0x1e, 0x66, 0x7f, 0x06, 0x06, 0x00,
0x7e, 0x60, 0x7c, 0x06, 0x06, 0x66, 0x3c, 0x00, 0x3c, 0x66, 0x60, 0x7c,
0x66, 0x66, 0x3c, 0x00, 0x7e, 0x66, 0x0c, 0x18, 0x18, 0x18, 0x18, 0x00,
0x3c, 0x66, 0x66, 0x3c, 0x66, 0x66, 0x3c, 0x00, 0x3c, 0x66, 0x66, 0x3e,
0x06, 0x66, 0x3c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x00, 0x00,
0x00, 0x00, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30, 0x0e, 0x18, 0x30, 0x60,
0x30, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00, 0x00, 0x00,
0x70, 0x18, 0x0c, 0x06, 0x0c, 0x18, 0x70, 0x00, 0x3c, 0x66, 0x06, 0x0c,
0x18, 0x00, 0x18, 0x00, 0x3c, 0x66, 0x6e, 0x6e, 0x60, 0x62, 0x3c, 0x00,
0x18, 0x3c, 0x66, 0x7e, 0x66, 0x66, 0x66, 0x00, 0x7c, 0x66, 0x66, 0x7c,
0x66, 0x66, 0x7c, 0x00, 0x3c, 0x66, 0x60, 0x60, 0x60, 0x66, 0x3c, 0x00,
0x78, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0x78, 0x00, 0x7e, 0x60, 0x60, 0x78,
0x60, 0x60, 0x7e, 0x00, 0x7e, 0x60, 0x60, 0x78, 0x60, 0x60, 0x60, 0x00,
0x3c, 0x66, 0x60, 0x6e, 0x66, 0x66, 0x3c, 0x00, 0x66, 0x66, 0x66, 0x7e,
0x66, 0x66, 0x66, 0x00, 0x3c, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00,
0x1e, 0x0c, 0x0c, 0x0c, 0x0c, 0x6c, 0x38, 0x00, 0x66, 0x6c, 0x78, 0x70,
0x78, 0x6c, 0x66, 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7e, 0x00,
0x63, 0x77, 0x7f, 0x6b, 0x63, 0x63, 0x63, 0x00, 0x66, 0x76, 0x7e, 0x7e,
0x6e, 0x66, 0x66, 0x00, 0x3c, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x00,
0x7c, 0x66, 0x66, 0x7c, 0x60, 0x60, 0x60, 0x00, 0x3c, 0x66, 0x66, 0x66,
0x66, 0x3c, 0x0e, 0x00, 0x7c, 0x66, 0x66, 0x7c, 0x78, 0x6c, 0x66, 0x00,
0x3c, 0x66, 0x60, 0x3c, 0x06, 0x66, 0x3c, 0x00, 0x7e, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x00, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x00,
0x66, 0x66, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x00, 0x63, 0x63, 0x63, 0x6b,
0x7f, 0x77, 0x63, 0x00, 0x66, 0x66, 0x3c, 0x18, 0x3c, 0x66, 0x66, 0x00,
0x66, 0x66, 0x66, 0x3c, 0x18, 0x18, 0x18, 0x00, 0x7e, 0x06, 0x0c, 0x18,
0x30, 0x60, 0x7e, 0x00, 0x3c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x0c, 0x0c, 0x0c,
0x0c, 0x0c, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x06, 0x3e, 0x66, 0x3e, 0x00,
0x00, 0x60, 0x60, 0x7c, 0x66, 0x66, 0x7c, 0x00, 0x00, 0x00, 0x3c, 0x60,
0x60, 0x60, 0x3c, 0x00, 0x00, 0x06, 0x06, 0x3e, 0x66, 0x66, 0x3e, 0x00,
0x00, 0x00, 0x3c, 0x66, 0x7e, 0x60, 0x3c, 0x00, 0x00, 0x0e, 0x18, 0x3e,
0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x3e, 0x66, 0x66, 0x3e, 0x06, 0x7c,
0x00, 0x60, 0x60, 0x7c, 0x66, 0x66, 0x66, 0x00, 0x00, 0x18, 0x00, 0x38,
0x18, 0x18, 0x3c, 0x00, 0x00, 0x06, 0x00, 0x06, 0x06, 0x06, 0x06, 0x3c,
0x00, 0x60, 0x60, 0x6c, 0x78, 0x6c, 0x66, 0x00, 0x00, 0x38, 0x18, 0x18,
0x18, 0x18, 0x3c, 0x00, 0x00, 0x00, 0x66, 0x7f, 0x7f, 0x6b, 0x63, 0x00,
0x00, 0x00, 0x7c, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x3c, 0x66,
0x66, 0x66, 0x3c, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x66, 0x7c, 0x60, 0x60,
0x00, 0x00, 0x3e, 0x66, 0x66, 0x3e, 0x06, 0x06, 0x00, 0x00, 0x7c, 0x66,
0x60, 0x60, 0x60, 0x00, 0x00, 0x00, 0x3e, 0x60, 0x3c, 0x06, 0x7c, 0x00,
0x00, 0x18, 0x7e, 0x18, 0x18, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x66, 0x66,
0x66, 0x66, 0x3e, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x00,
0x00, 0x00, 0x63, 0x6b, 0x7f, 0x3e, 0x36, 0x00, 0x00, 0x00, 0x66, 0x3c,
0x18, 0x3c, 0x66, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x3e, 0x0c, 0x78,
0x00, 0x00, 0x7e, 0x0c, 0x18, 0x30, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
char c;
int i = 0;
int j;
int messageLength;
uint8_t byte;
uint8_t byteLast;
uint8_t byteNext;
int charOffset;
float sample;
int freq;
int freqNext;
int col;
int row;
int copy;
float pixel;
float mix;
short out;
char var;
int varc = 0;
int value;
int duplicates = 4;
int fundamental = 16000;
int harmonicSpacing = 500;
int harmonicSpacingCurrent = 500;
char output[72];
float samplerate = 44100; /* Ideally, this should be an int, but if I change it to an int, I should check if I need to e.g. multiply it by 1.0 in order to get the formula using it to output a float. */
int width = 11025;
int main(int argc, char *argv[]) {
FILE *filePointer;
strcpy(output, "out.wav");
if (argc == 1) {
/* Require a message to encode */
usage();
}
while ((var = getopt(argc, argv, "d:f:h:o:s:w:")) != -1) {
varc++;
switch (var) {
case 'd':
value = atoi(optarg);
if (value < 1 || value > 4) {
fprintf(stderr, "d should be an integer between 1 and 4.\n");
exit(1);
}
duplicates = value;
break;
case 'f':
value = atoi(optarg);
if (value < 1 || value > 20000) {
fprintf(stderr, "f should be an integer between 1 and 20000.\n");
exit(1);
}
fundamental = value;
break;
case 'h':
if (strcmp(optarg, "exp") == 0) {
harmonicSpacing = -2;
break;
} else if (strcmp(optarg, "lin") == 0) {
harmonicSpacing = -1;
break;
}
value = atoi(optarg);
if (value < 1 || value > 2500) {
fprintf(stderr, "h should be an integer between 1 and 2500, or lin, or exp.\n");
exit(1);
}
harmonicSpacing = value;
break;
case 'o':
strcpy(output, optarg);
break;
case 's':
value = atoi(optarg);
if (value < 8000 || value > 48000) {
fprintf(stderr, "s should be an integer between 8000 and 48000.\n");
exit(1);
}
samplerate = value;
break;
case 'w':
value = atoi(optarg);
if (value < 1 || value > 64000) {
fprintf(stderr, "w should be an integer between 1 and 64000.\n");
exit(1);
}
width = value;
break;
default:
usage();
}
}
wav = wavfile_open(output);
if (!wav) {
fprintf(stderr, "Error: unable to write to %s.\n", output);
exit(1);
}
if (argc != (varc * 2) + 2) {
fprintf(stderr, "Please specify exactly one message to encode.\n");
exit(1);
}
argv += argc - 1;
messageLength = strlen(*argv);
for (j = 0; j < messageLength; j++) {
c = argv[0][j];
/* Only display printable ASCII characters */
if (c < 32 || c >= 126) {
continue;
}
/* The character set file should only contain printable ASCII characters */
charOffset = 8 * (c - 32);
byte = 0;
for (col = 0; col < 8; col++) {
byteLast = byte;
byte = 0;
for (row = 0; row < 8; row++) {
if (charset[charOffset + row] & (1 << (7 - col))) {
byte |= 1 << row;
}
}
byteNext = 0;
if (col != 7) {
for (row = 0; row < 8; row++) {
if (charset[charOffset + row] & (1 << (6 - col))) {
byteNext |= 1 << row;
}
}
}
/* Loop through each sample */
for (sample = 0; sample < width; sample++) { /* Hardwire each pixel width as 1/4 of a CD quality second for now */
mix = 0;
/* Work out which oscillators are on for this column */
for (row = 0; row < 8; row++) {
if (byte & (1 << (7 - row))) { /* Lowest frequency oscillator first */
if (harmonicSpacing == -2) {
// Exponential harmonic spacing
freq = fundamental * pow(2, row);
freqNext = fundamental * pow(2, row + 1);
} else if (harmonicSpacing == -1) {
// Linear harmonic spacing, as a multiple of the fundamental harmonic
freq = fundamental * (row + 1);
freqNext = fundamental * (row + 2);
} else {
// Linear harmonic spacing, as a specified amount
freq = fundamental + (harmonicSpacing * row);
freqNext = fundamental + (harmonicSpacing * (row + 1));
}
// As per the Nyquist theorem, don't reflect back unobtainably high harmonics
if (freq > samplerate / 2) {
freq = samplerate / 2;
}
if (freqNext > samplerate / 2) {
freqNext = samplerate / 2;
}
harmonicSpacingCurrent = freqNext - freq;
pixel = 0;
for (copy = 0; copy < duplicates; copy++) {
pixel += sin((freq + (harmonicSpacingCurrent / duplicates * copy)) * (sample / samplerate) * M_TAU); /* The number of cycles per second is multiplied by the number of seconds. Even though the latter's between 0 and 0.25, the frequencies bring it up. Hardwire CD quality sample rate for now. */
}
pixel /= duplicates;
/* Fuzz off the edges of each off-adjacent pixel to avoid noise bursts */
if (sample < samplerate / 16 && !(byteLast & (1 << (7 - row)))) {
pixel = pixel / (samplerate / 16) * sample;
} else if (sample > (samplerate / 16 * 3) && !(byteNext & (1 << (7 - row)))) {
pixel = pixel / (samplerate / 16) * (width - sample);
}
/* Fuzz off the top and bottom rows too */
if (copy == 0 || copy == duplicates - 1) {
pixel /= 2;
}
mix += (1.0 / 9.0) * pixel; /* Each sine wave should be 1/9th volume, for mixing with headroom */
}
}
out = floor(mix * 32767); /* Convert waveform into signed 16-bit integer */
wavfile_write(wav, &out, 1);
}
}
}
wavfile_close(wav);
return 0;
}
void usage() {
fprintf(stderr, "Steg, by Zoe Blade\n\n");
fprintf(stderr, "usage: steg [-dfhosw] message\n\n");
fprintf(stderr, "OPTIONS:\n");
fprintf(stderr, " -d n duplicates of each line, including original, default 4\n");
fprintf(stderr, " -f n fundamental harmonic in Hz, default 16000\n");
fprintf(stderr, " -h n harmonic spacing in Hz (height of each pixel in Hz), or lin,\n");
fprintf(stderr, " or exp, default 500\n");
fprintf(stderr, " -o filename file to write to, default out.wav\n");
fprintf(stderr, " -s n samplerate in Hz, default 44100\n");
fprintf(stderr, " -w n width of each pixel in samples, default 11025\n\n");
exit(1);
}