-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_modify.c
199 lines (163 loc) · 4.56 KB
/
file_modify.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#define FILE_SIZE (10 * 1024 * 1024) /* 10M */
#define BMP_HEAD_SIZE 54
#define PNG_HEAD_SIZE 33
#define PNG_WIDTH_OFFSET 22
enum image_type {
jpg = 1,
bmp,
png
};
#pragma pack(1)
typedef struct tag_bmp_head {
unsigned char bfType[2];
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
unsigned int biSize;
unsigned int biWidth;
unsigned int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biComppression;
unsigned int biSizeImage;
}BMP_HEAD_S;
#pragma pack()
int file_type(unsigned char *f) {
if ((0xFF == f[0]) && (0xD8 == f[1]) && (0xFF == f[2]) && (0xE0 == (f[3] & 0xF0))) {
return jpg;
} else if ((0x42 == f[0]) && (0x4D == f[1])) {
return bmp;
} else if ((0x89 == f[0]) && (0x50 == f[1]) && (0x4E == f[2]) && (0x47 == f[3])) {
return png;
}
return 0;
}
int modify_jpg(unsigned char *f, int file_size) {
unsigned char *p_last = NULL;
unsigned char *p_height = NULL;
p_height = memchr(f, 0xFF, file_size);
while (NULL != p_height) {
if (0xC0 == *(p_height + 1)) {
p_last = p_height;
}
p_height = memchr(p_height + 2, 0xFF, file_size - (p_height - f));
}
if (NULL == p_last) {
printf("JPG can't find 0xFFC0\r\n");
return -1;
}
*(p_last + 5) = 0x0A;
return 0;
}
int modify_bmp(unsigned char *f, int file_size) {
unsigned int bitcount = 0;
unsigned int trueheight = 0;
BMP_HEAD_S *p_bmp_head = (BMP_HEAD_S *)f;
if (file_size <= BMP_HEAD_SIZE) {
printf("BMP length less than head size\r\n");
return -1;
}
if (0 == p_bmp_head->biWidth) {
printf("BMP width is zero\r\n");
return -1;
}
bitcount = p_bmp_head->biBitCount / 8;
if (0 == bitcount) {
bitcount = 1;
}
trueheight = ((p_bmp_head->bfSize - p_bmp_head->bfOffBits) / bitcount) / p_bmp_head->biWidth;
if (trueheight != p_bmp_head->biHeight) {
printf("BMP height abnormal, true height: %u, current height: %u\r\n", trueheight, p_bmp_head->biHeight);
}
p_bmp_head->biHeight = trueheight;
return 0;
}
int modify_png(unsigned char *f, int file_size) {
unsigned char *p_height = NULL;
unsigned char newheight;
if (file_size <= PNG_HEAD_SIZE) {
printf("PNG length less than head size\r\n");
return -1;
}
p_height = f + PNG_WIDTH_OFFSET;
if (0 != *p_height) {
newheight = *p_height * 4;
} else {
newheight = 0x02;
}
if (newheight < *p_height) {
newheight = 0xff;
}
*p_height = newheight;
return 0;
}
int main(int argc, char *argv[]) {
int ret = 0;
int f_len = 0;
FILE *orif = NULL;
FILE *newf = NULL;
unsigned char *f = NULL;
char new_fname[64] = "new_";
enum image_type img;
if (2 != argc) {
printf("Miss args\r\n");
return -1;
}
if (NULL == (orif = fopen(argv[1], "rb"))) {
printf("File open error\r\n");
return -1;
}
f = malloc(FILE_SIZE);
if (NULL == f) {
printf("Malloc error\r\n");
fclose(orif);
return -1;
}
f_len = fread(f, 1, FILE_SIZE, orif);
if (f_len + 10 >= FILE_SIZE) {
printf("File too large\r\n");
free(f);
fclose(orif);
return -1;
}
img = file_type(f);
switch (img) {
case jpg:
ret = modify_jpg(f, f_len);
break;
case bmp:
ret = modify_bmp(f, f_len);
break;
case png:
ret = modify_png(f, f_len);
break;
default:
free(f);
fclose(orif);
printf("Unknown image type\r\n");
return -1;
}
if (0 != ret) {
free(f);
fclose(orif);
printf("Modify file error\r\n");
return -1;
}
if (NULL == (newf = fopen(strcat(new_fname, argv[1]), "wb"))) {
free(f);
fclose(orif);
printf("New file open error\r\n");
return -1;
}
(void)fwrite(f, f_len, 1, newf);
free(f);
fclose(orif);
fclose(newf);
printf("Done\r\n");
return 0;
}