forked from samuelcouch/c-steganography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadMsg.c
83 lines (68 loc) · 1.67 KB
/
readMsg.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
#include <stdio.h>
#include "stego.h"
int get_msg_length(FILE *);
void decode_message(int, FILE *);
int main(int argc, char **argv) {
FILE *fp;
//Print an error if no args provided
if((fp = fopen(argv[1], "rb")) == NULL) {
printf("\nError: Please provide a file to scan.\n\n");
return 1;
}
if(read_ppm_type(fp)) {
skip_comments(fp);
get_width(fp);
get_height(fp);
if(read_color_depth(fp)) {
int length = get_msg_length(fp);
printf("\nHoly secret message batman! We cracked the code: \n");
decode_message(length, fp);
fclose(fp);
} else {
printf("Error: Invalid Color Depth.");
return 1;
}
} else {
printf("Error: Wrong PPM File Format. Terminating.");
return 1;
}
return 0;
}
//Gets the length of the secret message
int get_msg_length(FILE *fp) {
char temp = 0;
int length = 0;
int i;
for(i=0; i < 8; i++) {
temp = fgetc(fp);
//Delay the shift by one to get the 29, otherwise I'd get 58
if(i > 0) length <<= 1;
length |= (temp & 1);
}
return length;
}
void decode_message(int length, FILE *fp) {
int readTo = length * 8, numRead = 0, i;
unsigned char charBuffer = 0;
char temp;
char secret_message[length + 1];
int idx = 0;
while(numRead <= readTo) {
temp = fgetc(fp);
if(numRead % 8 == 0) {
secret_message[idx] = charBuffer;
idx++;
charBuffer = 0;
} else {
charBuffer <<= 1;
}
charBuffer |= (temp & 1);
numRead++;
}//end while
//Start printing from character 1 because the first char is junk
for(i = 1; i < idx; i++) {
printf("%c", secret_message[i]);
}
printf("\n\n");
return;
}