-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNGRipper.cpp
101 lines (85 loc) · 2.55 KB
/
PNGRipper.cpp
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
/***********************************************************************
* Copyright 2007-2010 Michael Drueing <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
#include "GlobalDefs.h"
#include "PNGRipper.h"
#include <cctype>
#include <cstring>
const char *PNGRipper::s_name = "PNG Ripper v1.0";
const HeaderStruct PNGRipper::s_headers[] = {
HS("\x89PNG\x0d\x0a\x1a\x0a", 8)
HS_END
};
#pragma pack(push, 1)
struct ChunkHeader
{
uint32 size;
unsigned char id[4];
uint32 crc;
};
#pragma pack(pop)
static bool validChunkName(unsigned char *n)
{
return isalpha(n[0]) && isalpha(n[1]) && isalpha(n[2]) && isalpha(n[3]);
}
bool PNGRipper::checkLocation(unsigned char *pos, const HeaderStruct *header, FoundStruct *found)
{
bool ihdr_found = false;
bool iend_found = false;
ChunkHeader *chdr;
// save the start offset for later
found->startoffset = pos;
// skip PNG header bytes
pos += 8;
while (pos < m_start + m_length)
{
// get current chunk
chdr = (ChunkHeader *)pos;
if (memcmp(chdr->id, "IHDR", 4) == 0)
{
// did we already find another IHDR chunk?
if (ihdr_found)
return false;
ihdr_found = true;
}
if (memcmp(chdr->id, "IEND", 4) == 0)
{
// check that we encountered IHDR before
if (!ihdr_found)
return false;
// end png processing
iend_found = true;
break;
}
// this *must* be true as IHDR must be the first header
if (!ihdr_found)
return false;
// if we don't have a valid chunk name, we're also screwed
if (!validChunkName(chdr->id))
return false;
// skip to next header
pos += 12 + FROM_BIG_ENDIAN(chdr->size);
}
// if we neither found IHDR nor IEND then we're screwed
if (!(ihdr_found && iend_found))
return false;
// skip over IEND chunk size
pos += 12 + FROM_BIG_ENDIAN(chdr->size);
strcpy(found->extension, "png");
found->criterium = CRIT_STRONG;
found->length = pos - found->startoffset;
return true;
}