-
Notifications
You must be signed in to change notification settings - Fork 5
/
giffy.c
251 lines (211 loc) · 6.94 KB
/
giffy.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
#include <stdio.h>
#include <stdlib.h>
#include "giffy.h"
///// E N C O D E R /////
void write_secret_message_symbols(FILE* giffy)
{
flockfile(giffy);
for (int i = 0; i < 4; ++i) {
putc_unlocked('_', giffy);
}
}
void write_comment_end(FILE* giffy)
{
// if NETSCAPE 2.0 can do it, i can sign my work too
char signature[7] = "vipyne";
flockfile(giffy);
for (int i = 0; signature[i] != '\0'; ++i ) {
putc_unlocked(signature[i], giffy);
};
fputc(0x00, giffy);
}
void copy_gif_file(FILE* source, FILE* giffy)
{
fseek(source, 0, SEEK_END);
int source_size = ftell(source);
rewind(source);
char *source_buffer = (char*)malloc(sizeof(char) * source_size);
for (int i = 0; i < source_size; ++i) {
fread(source_buffer, sizeof(char), source_size, source);
fputc(source_buffer[i], giffy);
}
}
void write_header(FILE* giffy)
{
// G I F H E A D E R
char gif_header[6] = {'G', 'I' ,'F' , '8', '9', 'a'};
for (int i = 0; i < 6; ++i) {
fputc(gif_header[i], giffy);
}
/*short int canvas_width; // TODO: ensure this is little endian
short int canvas_height;
char packed_field;
char pixel_aspect_ratio;*/
// L O G I C A L S C R E E N D E S C R I P T O R
fputc(0x03, giffy);
fputc(0x00, giffy);// width
fputc(0x03, giffy);
fputc(0x00, giffy);// height
// _ color table bool | ___ resolution | _ sort flag | ___ size of table
// size of table == 000 => 2 colors;
fputc(0x81, giffy);// packed /// color table options - whether or not has
fputc(0x00, giffy);// background color index
fputc(0x00, giffy);// pixel aspect ratio
// C O L O R T A B L E
// global color table
// TODO: iterate through the color_table you just made
fputc(0x00, giffy);
fputc(0xFF, giffy);
fputc(0xFF, giffy); // 0 Cyan
fputc(0x88, giffy);
fputc(0x11, giffy);
fputc(0x66, giffy); // 1 Purple
fputc(0x00, giffy);
fputc(0x00, giffy);
fputc(0x00, giffy); // 2 Black
fputc(0x00, giffy);
fputc(0xFF, giffy);
fputc(0x00, giffy); // 3 Green
// end color table
// I M A G E D E S C R I P T O R
fputc(0x2C, giffy);
fputc(0x00, giffy);
fputc(0x00, giffy);
fputc(0x00, giffy);
fputc(0x00, giffy);// width
fputc(0x03, giffy);
fputc(0x00, giffy);// length
fputc(0x03, giffy);
fputc(0x00, giffy);
fputc(0x00, giffy); // image_descriptor packed_field
// // not used yet, hard coded for now
// gif_image_descriptor image_descriptor;
// image_descriptor.image_width = 0xFF; // need to get image dim... ffmpeg? vips?
// image_descriptor.image_height = 0xFF;
}
void write_compressed_image_data(FILE* source, FILE* giffy)
{
// int pixel_color_indeces[] = {1,3,1,3,2,3,1,3,1};
// unsigned char *binary_arr = calloc(1,sizeof(char)*9);
// binary_arr[0] = 1;
// binary_arr[1] = 3;
// binary_arr[2] = 1;
// struct packed_struct {
// unsigned char one : 1;
// unsigned char two : 1;
// unsigned char three : 1;
// unsigned char four : 1;
// unsigned char five : 1;
// unsigned char six : 1;
// unsigned char seven : 1;
// unsigned char eight : 1;
// }
// how to create a binary stream?
// I M A G E D A T A
fputc(0x02, giffy); // LZW min code size - 2
fputc(0x0A, giffy); // number of bytes in data sub-block
fputc(0xCC, giffy); //
fputc(0xB2, giffy); //
fputc(0x2C, giffy); //
fputc(0xCB, giffy); //
fputc(0xB6, giffy); //
fputc(0x6D, giffy); //
fputc(0xDB, giffy); //
fputc(0xB2, giffy); //
fputc(0x6C, giffy); //
fputc(0xCB, giffy); //
fputc(0xA2, giffy); //
fputc(0x00, giffy); // end image data | end data sub block
// fputc(0x00, giffy); // end image data | end data sub block
}
void compress_image(FILE* giffy, char* index_stream)
{
// // LZW //////
}
int long write_secret_message(FILE* giffy, char* secret_message)
{
int long i;
for (i = 0; secret_message[i] != '\0'; ++i) {
fputc(secret_message[i], giffy);
}
return i;
}
void write_entire_comment(FILE* giffy, char* secret_message)
{
// fseek(giffy, -1L, SEEK_END); // rewind and start to write over original trailer
/*// P L A I N T E X T E X T E N S I O N
fputc(0x21, giffy);// plain text extension start
fputc(0x01, giffy);// plain text label
fputc(0x00, giffy); // block size (let's try 0 for now but could add something later:shrug_emoji:)
fputc(0x00, giffy);
// A P P L I C A T I O N E X T E N S I O N
/ *byte 1 : 33 (hex 0x21) GIF Extension code
byte 2 : 255 (hex 0xFF) Application Extension Label
byte 3 : 11 (hex 0x0B) Length of Application Block
(eleven bytes of data to follow)
bytes 4 to 11 : "NETSCAPE"
bytes 12 to 14 : "2.0"
byte 15 : 3 (hex 0x03) Length of Data Sub-Block
(three bytes of data to follow)
byte 16 : 1 (hex 0x01)
bytes 17 to 18 : 0 to 65535, an unsigned integer in
little-endian byte format. This specifies the
number of times the loop should
be executed.
byte 19 : 0 (hex 0x00) a Data Sub-Block Terminator. * /
char app_extension[19] = {0x21, 0xFF, 0x0B, 'N', 'E', 'T', 'S', 'C', 'A', 'P', 'E', '2', '.', '0', '3', '1', '0', '0', '0'};
for (int i = 0; i < 19; ++i) {
fputc(app_extension[i], giffy);
};*/
// C O M M E N T E X T E N S I O N
fputc(0x21, giffy); // extension start
fputc(0xFE, giffy); // comment label
fputc(0x00, giffy); // will be overwritten with length of secret message
int long length = write_secret_message(giffy, secret_message);
write_secret_message_symbols(giffy);
write_comment_end(giffy);
// the -16 is to account for `____vipyne0x00`
fseek(giffy, -12 - length, SEEK_CUR); // rewind to length char
fputc(length, giffy);
fseek(giffy, 0L, SEEK_END);
// T R A I L E R
fputc(0x3B, giffy);
}
///// D E C O D E R /////
void parse_out_secret_message(FILE* source)
{
char c;
char last_c;
char *message_buffer;
while (!feof(source)) {
c = fgetc(source);
if (c != '\0') {
if (last_c == (signed char)0x21 && c == (signed char)0xFE) {
int length = fgetc(source); // this byte tells us length of message
message_buffer = (char*)malloc(sizeof(char) * length);
for(int i = 0; i < length; ++i) {
c = fgetc(source);
message_buffer[i] = c;
}
}
last_c = c;
}
}
printf("\n");
printf("%s", message_buffer); // only print the last comment - that is the one we added
printf("\n");
printf("\n");
};
///// U T I L /////
void print_directions(void) {
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("To encode a message:\n");
printf("$ ./giffy.exe e [input-image-file] [output-filename] [message-string]\n");
printf("Ensure `message-string` is wrapped in quotes\n");
printf("\n");
printf("To decode an image:\n");
printf("$ ./giffy.exe d [input-image-file]\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\n");
}