summaryrefslogtreecommitdiffstats
path: root/src/file_png.c
blob: 3d3b31ffec4163457fa2bf37ed49496622a1b9b5 (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
/*

    File: file_png.c

    Copyright (C) 1998-2009 Christophe GRENIER <grenier@cgsecurity.org>
    Thanks to Holger Klemm for JNG (JPEG Network Graphics) and
    MNG (Multiple-Image Network Graphics) support (2006)
  
    This software 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) 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, write the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */
/*
   MNG (Multiple-image Network Graphics) Format
   http://www.libpng.org/pub/mng/spec/
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include "types.h"
#include "common.h"
#include "filegen.h"

extern const file_hint_t file_hint_doc;

static void register_header_check_png(file_stat_t *file_stat);
static data_check_t data_check_png(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
static data_check_t data_check_mng(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);

const file_hint_t file_hint_png= {
  .extension="png",
  .description="Portable/JPEG/Multiple-Image Network Graphics",
  .max_filesize=PHOTOREC_MAX_FILE_SIZE,
  .recover=1,
  .enable_by_default=1,
  .register_header_check=&register_header_check_png
};

struct png_chunk
{
  uint32_t length;
  uint32_t type;
  char data[0];
  /* data is followed by uint32_t crc; */
} __attribute__ ((gcc_struct, __packed__));

struct png_ihdr
{
  uint32_t width;
  uint32_t height;
  uint8_t  bit_depth;
  uint8_t  color_type;
  uint8_t  compression;
  uint8_t  filter;
  uint8_t  interlace;
} __attribute__ ((gcc_struct, __packed__));

static int header_check_jng(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  if( !((isupper(buffer[8+4]) || islower(buffer[8+4])) &&
	(isupper(buffer[8+5]) || islower(buffer[8+5])) &&
	(isupper(buffer[8+6]) || islower(buffer[8+6])) &&
	(isupper(buffer[8+7]) || islower(buffer[8+7]))))
    return 0;
  reset_file_recovery(file_recovery_new);
  file_recovery_new->extension="jng";
  file_recovery_new->min_filesize=16;
  if(file_recovery_new->blocksize < 8)
    return 1;
  file_recovery_new->calculated_file_size=8;
  file_recovery_new->data_check=&data_check_png;
  file_recovery_new->file_check=&file_check_size;
  return 1;
}

static int header_check_mng(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  if( !((isupper(buffer[8+4]) || islower(buffer[8+4])) &&
	(isupper(buffer[8+5]) || islower(buffer[8+5])) &&
	(isupper(buffer[8+6]) || islower(buffer[8+6])) &&
	(isupper(buffer[8+7]) || islower(buffer[8+7]))))
    return 0;
  reset_file_recovery(file_recovery_new);
  file_recovery_new->extension="mng";
  file_recovery_new->min_filesize=16;
  if(file_recovery_new->blocksize < 8)
    return 1;
  file_recovery_new->calculated_file_size=8;
  file_recovery_new->data_check=&data_check_mng;
  file_recovery_new->file_check=&file_check_size;
  return 1;
}

/* png_check_ihdr: return 1 if valid */
static int png_check_ihdr(const struct png_ihdr *ihdr)
{
  if(be32(ihdr->width)==0 || be32(ihdr->height)==0)
    return 0;
  switch(ihdr->color_type)
  {
    case 0:	/* Greyscale */
      if(ihdr->bit_depth!=1 && ihdr->bit_depth!=2 && ihdr->bit_depth!=4 && ihdr->bit_depth!=8 && ihdr->bit_depth!=16)
	return 0;
      break;
    case 3:	/* Indexed-colour*/
      if(ihdr->bit_depth!=1 && ihdr->bit_depth!=2 && ihdr->bit_depth!=4 && ihdr->bit_depth!=8)
	return 0;
      break;
    case 2:	/* Truecolour */
    case 4:	/* Greyscale with alpha */
    case 6:	/* Truecolour with alpha */
      if(ihdr->bit_depth!=8 && ihdr->bit_depth!=16)
	return 0;
      break;
    default:
      return 0;
  }
  return 1;
}

static void file_check_png(file_recovery_t *fr)
{
  if(fr->file_size<fr->calculated_file_size)
  {
    fr->file_size=0;
    return ;
  }
  fr->file_size=8;
  while(1)
  {
    char buffer[8];
    const struct png_chunk *chunk=(const struct png_chunk *)&buffer;
    if(my_fseek(fr->handle, fr->file_size, SEEK_SET) < 0 ||
	fread(&buffer, sizeof(buffer), 1, fr->handle) != 1)
    {
      fr->file_size=0;
      return ;
    }
    fr->file_size+=(uint64_t)12 + be32(chunk->length);
    if(memcmp(&buffer[4], "IEND", 4)==0)
      return ;
    if(memcmp(&buffer[4], "IHDR", 4) == 0)
    {
      struct png_ihdr ihdr;
      if(fread(&ihdr, sizeof(ihdr), 1, fr->handle) != 1 ||
	  png_check_ihdr(&ihdr)==0)
      {
	fr->file_size=0;
	return ;
      }
    }
  }
}

static int header_check_png(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  if( !((isupper(buffer[8+4]) || islower(buffer[8+4])) &&
	(isupper(buffer[8+5]) || islower(buffer[8+5])) &&
	(isupper(buffer[8+6]) || islower(buffer[8+6])) &&
	(isupper(buffer[8+7]) || islower(buffer[8+7]))))
    return 0;
  if(memcmp(&buffer[8+4], "IHDR", 4) == 0 &&
      png_check_ihdr((const struct png_ihdr *)&buffer[16])==0)
    return 0;
  /* SolidWorks files contains a png */
  if(file_recovery->file_stat!=NULL &&
      file_recovery->file_stat->file_hint==&file_hint_doc)
  {
    if(header_ignored_adv(file_recovery, file_recovery_new)==0)
      return 0;
  }
  reset_file_recovery(file_recovery_new);
  file_recovery_new->extension=file_hint_png.extension;
  file_recovery_new->min_filesize=16;
  if(file_recovery_new->blocksize < 8)
    return 1;
  file_recovery_new->calculated_file_size=8;
  file_recovery_new->data_check=&data_check_png;
  file_recovery_new->file_check=&file_check_png;
  return 1;
}

static data_check_t data_check_mng(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
  static const unsigned char mng_footer[4]= {'M','E','N','D'};
  while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
      file_recovery->calculated_file_size + 8 < file_recovery->file_size + buffer_size/2)
  {
    const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
    const struct png_chunk *chunk=(const struct png_chunk *)&buffer[i];
    if(memcmp(&buffer[i+4], mng_footer, sizeof(mng_footer))==0)
    {
      file_recovery->calculated_file_size+=(uint64_t)12 + be32(chunk->length);
      return DC_STOP;
    }
    if( !((isupper(buffer[i+4]) || islower(buffer[i+4])) &&
	  (isupper(buffer[i+5]) || islower(buffer[i+5])) &&
	  (isupper(buffer[i+6]) || islower(buffer[i+6])) &&
	  (isupper(buffer[i+7]) || islower(buffer[i+7]))))
    {
      file_recovery->offset_error=file_recovery->calculated_file_size+7;
      return DC_ERROR;
    }
    file_recovery->offset_ok=file_recovery->calculated_file_size+7;
    file_recovery->calculated_file_size+=(uint64_t)12 + be32(chunk->length);
  }
  return DC_CONTINUE;
}

static data_check_t data_check_png(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
  while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
      file_recovery->calculated_file_size + 8 < file_recovery->file_size + buffer_size/2)
  {
    const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
    const struct png_chunk *chunk=(const struct png_chunk *)&buffer[i];
    if(memcmp(&buffer[i+4], "IEND", 4)==0)
    {
      file_recovery->calculated_file_size+=(uint64_t)12 + be32(chunk->length);
      return DC_STOP;
    }
// PNG chunk code
// IDAT IHDR PLTE bKGD cHRM fRAc gAMA gIFg gIFt gIFx hIST iCCP
// iTXt oFFs pCAL pHYs sBIT sCAL sPLT sRGB sTER tEXt tRNS zTXt
    if( !((isupper(buffer[i+4]) || islower(buffer[i+4])) &&
	  (isupper(buffer[i+5]) || islower(buffer[i+5])) &&
	  (isupper(buffer[i+6]) || islower(buffer[i+6])) &&
	  (isupper(buffer[i+7]) || islower(buffer[i+7]))))
    {
      file_recovery->offset_error=file_recovery->calculated_file_size+7;
      return DC_ERROR;
    }
    file_recovery->offset_ok=file_recovery->calculated_file_size+7;
    file_recovery->calculated_file_size+=(uint64_t)12 + be32(chunk->length);
  }
  return DC_CONTINUE;
}

static void register_header_check_png(file_stat_t *file_stat)
{
  static const unsigned char png_header[8]= { 0x89, 'P', 'N','G', 0x0d, 0x0a, 0x1a, 0x0a};
  static const unsigned char mng_header[8]= { 0x8a, 'M', 'N','G', 0x0d, 0x0a, 0x1a, 0x0a};
  static const unsigned char jng_header[8]= { 0x8b, 'J', 'N','G', 0x0d, 0x0a, 0x1a, 0x0a};
  register_header_check(0, png_header, sizeof(png_header), &header_check_png, file_stat);
  register_header_check(0, mng_header, sizeof(mng_header), &header_check_mng, file_stat);
  register_header_check(0, jng_header, sizeof(jng_header), &header_check_jng, file_stat);
}