-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSB.c
323 lines (261 loc) · 9.12 KB
/
LSB.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
// Default header size for WAV files
int HEADER_SIZE = 45;
typedef unsigned char uchar;
typedef unsigned long ulong;
int embed_msg(
char *filename,
char *originPath,
char *msg,
char *destinationPath,
char *argBits
);
int embed_file(
char *filename,
char *originPath,
char *messagePath,
char *destinationPath,
char *argBits
);
int embed(
char *filename,
char *originPath,
uchar *messageBuffer,
ulong messageSize,
char *destinationPath,
char *argBits
);
int extract_out(
char *filename,
char *originPath,
char *destinationPath,
char *argBits
);
int extract_print(
char *filename,
char *originPath,
char *argBits
);
int extract(
char *filename,
char *originPath,
char *argBits,
ulong *outSize,
uchar **outBufferPtr
);
int flagErrorFunc(char *flag, char *filename);
int helpFunc(char *filename);
int main(int argc, char *argv[]) {
if (argc < 2) {
return flagErrorFunc("n/a", argv[0]);
}
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
return helpFunc(argv[0]);
if (strcmp(argv[1], "-em") == 0 && argc == 6)
return embed_msg(argv[0], argv[2], argv[3], argv[4], argv[5]);
if (strcmp(argv[1], "-ef") == 0 && argc == 6)
return embed_file(argv[0], argv[2], argv[3], argv[4], argv[5]);
if (strcmp(argv[1], "-xf") == 0 && argc == 5)
return extract_out(argv[0], argv[2], argv[3], argv[4]);
if (strcmp(argv[1], "-xp") == 0 && argc == 4)
return extract_print(argv[0], argv[2], argv[3]);
return flagErrorFunc(argv[1], argv[0]);
}
int flagErrorFunc(char *flag, char *filename){
if(strcmp(flag, "-em") == 0){
printf("usage: %s -em origin message destination <Bits per Byte>\n", filename);
return -2;
}
if(strcmp(flag, "-ef") == 0){
printf("usage: %s -ef origin messagePath destination <Bits per Byte>\n", filename);
return -2;
}
if(strcmp(flag, "-xp") == 0){
printf("usage: %s -xp origin <Bits per byte>\n", filename);
return -2;
}
if(strcmp(flag, "-xf") == 0) {
printf("usage: %s -xf origin output <Bits per byte>\n", filename);
return -2;
}
printf("unknown option: %s\n", flag);
printf("usage: %s ", filename);
printf("{-ef | -em} origin {messagePath | message} destination <Bits per byte>\n");
printf("%*c{-xf | -xp} origin [output] <Bits per byte>\n", (int)strlen(filename)+8, ' ');
printf("Type \"%s -h\" for more information \n", filename);
return -2;
}
int helpFunc(char *filename){
printf("usage: ");
printf("%s { -em | -ef | -xp | -xf | -h } [origin] [{messagePath|message}] [destination] [<Bits per byte>]\n", filename);
printf("\nFlags:\n");
printf("\t-em\tEmbed into wav from text input\n");
printf("\t-ef\tEmbed into wav from file\n");
printf("\t-xp\tExtract from wav and print to stdout\n");
printf("\t-xf\tExtract from wav and output to file\n");
printf("\t-h\tPrint this help message\n");
printf("\nAttributes:\n");
printf("\torigin\t\tPath to the wav file to be embedded/extracted\n");
printf("\tmessagePath\tPath to the text file to be embedded\n");
printf("\tmessage\t\tText to be embedded\n");
printf("\tdestination\tPath to the wav file to be embedded/extracted\n");
printf("\tBits per byte\tBits per byte to use when embedding/extracting (1, 2 or 4)\n");
printf("\nExamples:\n");
printf("\t%s -em ./test.wav \"Hello World\" ./test_out.wav 4\n", filename);
printf("\t%s -ef ./test.wav ./test_msg.txt ./test_out.wav 4\n", filename);
printf("\t%s -xp ./test.wav 4\n", filename);
printf("\t%s -xf ./test.wav ./test_out.wav 4\n", filename);
return 0;
}
int embed_msg(char *filename, char *originPath, char *msg, char *destinationPath, char *argBits) {
uchar *messageBuffer;
ulong messageSize;
messageSize = strlen(msg);
messageBuffer = (uchar *)malloc(sizeof(uchar) * messageSize);
memccpy(messageBuffer, msg, 0, messageSize);
printf("%s\n", messageBuffer);
int res = embed(filename, originPath, messageBuffer, messageSize, destinationPath, argBits);
free(messageBuffer);
return res;
}
int embed_file(char *filename, char *originPath, char *messagePath, char *destinationPath, char *argBits) {
int message;
uchar *messageBuffer;
ulong messageSize;
if ((message = open(messagePath, O_RDONLY)) < 0) {
perror(messagePath);
return -3;
}
messageSize = lseek(message, 0, SEEK_END);
messageBuffer = (uchar *)malloc(sizeof(uchar) * messageSize);
lseek(message, 0, SEEK_SET);
read(message, messageBuffer, messageSize);
printf("%s\n", messageBuffer);
int res = embed(filename, originPath, messageBuffer, messageSize, destinationPath, argBits);
free(messageBuffer);
close(message);
return res;
}
int embed(char *filename, char *originPath, uchar *messageBuffer, ulong messageSize, char *destinationPath, char *argBits) {
int origin, destination;
uchar noOfBits;
if ((origin = open(originPath, O_RDONLY)) < 0) {
perror(originPath);
return -3;
}
if ((destination = open(destinationPath, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
perror(destinationPath);
return -3;
}
if ((noOfBits = atoi(argBits)) && (noOfBits != 1 && noOfBits != 2 && noOfBits != 4)) {
printf("%s: Number of bits must be 1, 2 or 4\n", filename);
return -2;
}
int bytes;
uchar *buffer;
ulong fileSize = lseek(origin, 0, SEEK_END);
buffer = (uchar *)malloc(sizeof(uchar) * fileSize);
lseek(origin, 0, SEEK_SET);
read(origin, buffer, fileSize);
uchar baseShift = (8-noOfBits);
uchar iMod = 8/noOfBits;
uchar byteMask = 15 >> (4-noOfBits);
uchar inputMask = (255 >> noOfBits) << noOfBits;
if(messageSize*(8/noOfBits) > fileSize) {
printf("%s: Message is too long for file %s\n", filename, originPath);
return -2;
}
for (
int i = 0, fileByte = HEADER_SIZE, msgByte = 0;
fileByte < fileSize;
i++, fileByte++
) {
uchar byteWithout2LSB = (buffer[fileByte] & inputMask);
// To get 1, 2 and 4 significant bits from the message
// uchar msgBits = messageBuffer[msgByte] >> (7-(i%8)*1) & 1;
// uchar msgBits = messageBuffer[msgByte] >> (6-(i%4)*2) & 3;
// uchar msgBits = messageBuffer[msgByte] >> (4-(i%2)*4) & 15;
uchar msgBits = (messageBuffer[msgByte] >> (baseShift - ((i%iMod) * noOfBits))) & byteMask;
if(i%8 == 0 && i!=0) msgByte++; // move to next byte in message
buffer[fileByte] = byteWithout2LSB | msgBits;
}
write(destination, buffer, fileSize);
printf("done\n");
free(buffer);
close(origin);
close(destination);
return 0;
}
int extract_out(char *filename, char *originPath, char *destinationPath, char *argBits){
int destination;
ulong fileSize;
uchar *outBuffer = NULL;
int res = extract(filename, originPath, argBits, &fileSize, &outBuffer);
if(res == 0) {
if ((destination = open(destinationPath, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
perror(destinationPath);
return -3;
}
write(destination, outBuffer, fileSize);
}
close(destination);
free(outBuffer);
return res;
}
int extract_print(char *filename, char *originPath, char *argBits){
ulong fileSize;
uchar *outBuffer = NULL;
int res = extract(filename, originPath, argBits, &fileSize, &outBuffer);
if(res == 0) printf("%s\n", outBuffer);
free(outBuffer);
return res;
}
int extract(char *filename, char *originPath, char *argBits, ulong *outSize, uchar **outBufferPtr) {
int origin, destination;
uchar noOfBits;
if ((origin = open(originPath, O_RDONLY)) < 0) {
perror(originPath);
return -3;
}
if ((noOfBits = atoi(argBits)) && (noOfBits != 1 && noOfBits != 2 && noOfBits != 4)) {
printf("%s: Number of bits must be 1, 2 or 4\n", filename);
return -2;
}
uchar *buffer;
uchar *outBuffer = *outBufferPtr;
ulong fileSize = lseek(origin, 0, SEEK_END);
buffer = (uchar *)malloc(sizeof(uchar) * fileSize);
*outSize = (fileSize-HEADER_SIZE)*noOfBits/8;
outBuffer = ((uchar *)malloc(sizeof(uchar) * (*outSize)));
lseek(origin, 0, SEEK_SET);
read(origin, buffer, fileSize);
uchar byteMask = 15 >> (4-noOfBits);
uchar msgByteContent = 0;
for (
int i = 0, fileByte = HEADER_SIZE, msgByte = 0;
fileByte < fileSize;
i++, fileByte++
) {
if(i%8 == 0 && i!=0) {
outBuffer[msgByte] = msgByteContent;
if(msgByteContent == '\0') {
*outSize = msgByte;
break;
};
msgByte++;
msgByteContent = 0;
};
uchar LSBs = (buffer[fileByte] & byteMask);
msgByteContent = (msgByteContent << noOfBits) | LSBs;
}
*outBufferPtr = outBuffer;
free(buffer);
close(origin);
close(destination);
return 0;
}