-
Notifications
You must be signed in to change notification settings - Fork 0
/
readPNG.c
176 lines (147 loc) · 4.88 KB
/
readPNG.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
/**
* @author Richard Vong
* @date February 16, 2014
* CS 141 Project 3: PNG Debugger
*
* PNG STRUCTURE
* ====================
* First 8 is PNG SIGNATURE
*
* Next 4 is CHUNK LENGTH
* Next 4 is CHUNK TYPE
* LENGTH = 13 => 13 BYTES OF DATA
* - 4 Bytes Width
* - 4 Bytes Height
* - 1 Byte Bit Depth
* - 1 Byte Color Type
* - 1 Byte Compression method
* - 1 Byte Filter method
* - 1 Byte Interlace method
* Next 4 is CRC CHECK (Chunk Type + Data), not including Chunk Length Unsigned Long
*
* Next 4 is CHUNK LENGTH
* Next 4 is CHUNK TYPE
* ...
* ====================
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "constants.h"
#include "helperFuncts.h"
#include "crcCode.h"
void readPNG(char* path, int verbose);
void processPNG(unsigned char* buffer, long lSize, int verbose);
void readPNG(char* path, int verbose) {
FILE* file = fopen(path, "rb");
if (file == NULL) { printf("error: could not open file. (%s)", path); return; }
fseek(file, 0, SEEK_END); // Go to end
long lSize = ftell(file); // Get pos in stream
rewind(file); // Set pos to beginning
unsigned char* buffer = malloc(sizeof(unsigned char) * lSize);
if (buffer == NULL) { puts("error: memory error"); return; }
size_t result = fread(buffer, 1, lSize, file);
if (result != lSize) { puts("error: read error"); return; }
fclose(file);
//printf("\t----\nfile-path=%s\n", path);
//printf("file-size=%zu bytes\n", lSize);
processPNG(buffer, lSize, verbose);
free(buffer);
buffer = NULL;
}
void processPNG(unsigned char* buffer, long lSize, int verbose) {
int cur = 0;
unsigned char strBuffer[LEN_STR];
//printf("\n0x%08X\tpng-signature=", cur);
getBytes(buffer, cur, LEN_PNGSIG);
puts("");
cur += LEN_PNGSIG;
while (cur < lSize) { // Print (LENGTH, TYPE, CRC) for each.
long length = getNum(buffer, cur, LEN_CKLENGTH);
//printf("\n0x%08X\tchunk-length=0x%08lX\t(%ld)", cur, length, length); // CHUNK LENGTH
cur += LEN_CKLENGTH;
unsigned char type[LEN_HDR];
grabString(buffer, cur, LEN_CKTYPE, type);
//printf("\n0x%08X\tchunk-type=\'%s\'", cur, type); // CHUNK TYPE
int crcBegin = cur;
cur += LEN_CKTYPE;
if (verbose && (strcmp((char*)type, "IHDR") == 0)) { //if verbose, print expanded IHDR
long width = getNum(buffer, cur, LEN_WIDTH);
printf("\n0x%08X\twidth=0x%08lX\t(%ld)", cur, width, width);
cur += LEN_WIDTH;
long height = getNum(buffer, cur, LEN_HEIGHT);
printf("\n0x%08X\theight=0x%08lX\t(%ld)", cur, height, height);
cur += LEN_HEIGHT;
printf("\n0x%08X\tbit-depth=%ld", cur, getNum(buffer, cur, LEN_BITDEPTH));
cur += LEN_BITDEPTH;
long colorType = getNum(buffer, cur, LEN_COLORTYPE);
strColorType(colorType, strBuffer); // Stores type description in strBuffer
printf("\n0x%08X\tcolor-type=%ld \t(%s)", cur, colorType, strBuffer);
cur += LEN_COLORTYPE;
long compressMethod = getNum(buffer, cur, LEN_COMPRESS);
strCompress(compressMethod, strBuffer);
printf("\n0x%08X\tcompression-method=%ld \t(%s)", cur, compressMethod, strBuffer);
cur += LEN_COMPRESS;
long filterMethod = getNum(buffer, cur, LEN_FILTER);
strFilter(filterMethod, strBuffer);
printf("\n0x%08X\tfilter-method=%ld \t(%s)", cur, filterMethod, strBuffer);
cur += LEN_FILTER;
long interlaceMethod = getNum(buffer, cur, LEN_INTERLACE);
strInterlace(interlaceMethod, strBuffer);
printf("\n0x%08X\tinterlace-method=%ld \t(%s)", cur, interlaceMethod, strBuffer);
cur += LEN_INTERLACE;
}
int crcLen = LEN_CKTYPE + length;
unsigned char crcInput[crcLen];
grabBytes(buffer, crcBegin, crcLen, crcInput);
cur = crcBegin + crcLen;
long crcCode = getNum(buffer, cur, LEN_CRC);
unsigned int test = crcCode;
crcCode = test;
//printf("\n0x%08X\tcrc-code=0x%08lX",cur, crcCode1);
//crcCode = crcCode % 100000000;
long crcComputed = crc(crcInput, crcLen);
char* crcResult = "CRC FAILED";
if (crcCode == crcComputed)
{
crcResult = "CRC OK!";
}
else
{
printf("%s", type);
//short curshort = cur % 10000;
printf("\n%08X", cur);
printf("\n%08lX",crcComputed);
//printf("\n>> (CRC CHECK) crc-computed=0x%08lX \t=>\t%s\n\n", crcComputed, crcResult);
exit(0);
}
//printf("\n%08X\tcrc-code=0x%08lX", cur, crcCode);
//printf("\n>> (CRC CHECK) crc-computed=0x%08lX \t=>\t%s\n\n", crcComputed, crcResult);
cur += LEN_CRC;
}
}
int main(int argc, char* argv[]) {
// Test with example.png
//readPNG(PNG_PATH, 1);
/*
printf("Arguments:\n\tARGC: %zu\n", argc);
puts("\tARGV: ");
for (int i = 0; i < argc; i++)
printf("\t\t%s\n", argv[i]);
*/
int verbose = 0;
int i = 1;
if (argc > 1)
verbose = (strcmp(argv[1], "--verbose") == 0);
if ((! verbose && argc > 1) || (verbose && argc > 2)) {
if (verbose && argc > 2) i = 2;
for (; i < argc; i++)
readPNG(argv[i], verbose);
}
else {
printf("Usage: pngDebugger (--verbose) file1.png file2.png file3.png ...\n");
}
return 0;
}