1 /*
2
3 File: file_psd.c
4
5 Copyright (C) 2006-2009 Christophe GRENIER <grenier@cgsecurity.org>
6
7 This software is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write the Free Software Foundation, Inc., 51
19 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #include <stdio.h>
30 #include "types.h"
31 #include "filegen.h"
32 #include "common.h"
33
34 #ifdef DEBUG_PSD
35 #include "log.h"
36 #endif
37
38 static void register_header_check_psd(file_stat_t *file_stat);
39 static data_check_t psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
40 static void file_check_psd(file_recovery_t *file_recovery);
41
42 const file_hint_t file_hint_psd= {
43 .extension="psd",
44 .description="Adobe Photoshop Image",
45 .max_filesize=PHOTOREC_MAX_FILE_SIZE,
46 .recover=1,
47 .enable_by_default=1,
48 .register_header_check=®ister_header_check_psd
49 };
50
51 static uint64_t psd_image_data_size_max=0;
52
53 struct psd_file_header
54 {
55 char signature[4];
56 uint16_t version; /* must be 1 */
57 char reserved[6]; /* must be 0 */
58 uint16_t channels; /* between 1 and 56 */
59 uint32_t height; /* max of 30,000 */
60 uint32_t width; /* max of 30,000 */
61 uint16_t depth; /* 1, 8, 16 or 32 */
62 uint16_t color_mode; /* Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 */
63 } __attribute__ ((gcc_struct, __packed__));
64
header_check_psd(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)65 static int header_check_psd(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)
66 {
67 const struct psd_file_header *hdr=(const struct psd_file_header *)buffer;
68 #ifdef DEBUG_PSD
69 log_info("channels %u\n", be16(hdr->channels));
70 log_info("height %u\n", be32(hdr->height));
71 log_info("width %u\n", be32(hdr->width));
72 log_info("depth %u\n", be16(hdr->depth));
73 log_info("color_mode %u\n", be16(hdr->color_mode));
74 #endif
75 if(be16(hdr->channels)==0 || be16(hdr->channels)>56 ||
76 be32(hdr->height)==0 || be32(hdr->height)>30000 ||
77 be32(hdr->width)==0 || be32(hdr->width)>30000 ||
78 be16(hdr->depth)==0 || (be16(hdr->depth)!=1 && be16(hdr->depth)%8!=0))
79 return 0;
80 reset_file_recovery(file_recovery_new);
81 file_recovery_new->min_filesize=70;
82 file_recovery_new->extension=file_hint_psd.extension;
83 if(file_recovery_new->blocksize < 16)
84 return 1;
85 /* File header */
86 file_recovery_new->calculated_file_size=0x1a;
87 file_recovery_new->data_check=&psd_skip_color_mode;
88 file_recovery_new->file_check=&file_check_psd;
89 return 1;
90 }
91
get_be32(const void * buffer,const unsigned int offset)92 static uint32_t get_be32(const void *buffer, const unsigned int offset)
93 {
94 const uint32_t *val=(const uint32_t *)((const unsigned char *)buffer+offset);
95 return be32(*val);
96 }
97
psd_skip_image_data(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)98 static data_check_t psd_skip_image_data(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
99 {
100 file_recovery->file_check=NULL;
101 return DC_CONTINUE;
102 }
103
psd_skip_layer_info(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)104 static data_check_t psd_skip_layer_info(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
105 {
106 if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
107 file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
108 {
109 const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
110 const unsigned int l=get_be32(buffer, i)+4;
111 #ifdef DEBUG_PSD
112 log_info("Image data at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
113 #endif
114 if(l<4)
115 return DC_STOP;
116 file_recovery->calculated_file_size+=l;
117 file_recovery->data_check=&psd_skip_image_data;
118 return psd_skip_image_data(buffer, buffer_size, file_recovery);
119 }
120 return DC_CONTINUE;
121 }
122
psd_skip_image_resources(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)123 static data_check_t psd_skip_image_resources(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
124 {
125 if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
126 file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
127 {
128 const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
129 const unsigned int l=get_be32(buffer, i)+4;
130 #ifdef DEBUG_PSD
131 log_info("Layer info at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
132 #endif
133 if(l<4)
134 return DC_STOP;
135 file_recovery->calculated_file_size+=l;
136 file_recovery->data_check=&psd_skip_layer_info;
137 return psd_skip_layer_info(buffer, buffer_size, file_recovery);
138 }
139 return DC_CONTINUE;
140 }
141
psd_skip_color_mode(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)142 static data_check_t psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
143 {
144 const struct psd_file_header *psd=(const struct psd_file_header *)&buffer[buffer_size/2];
145 psd_image_data_size_max=(uint64_t)be16(psd->channels) * be32(psd->height) * be32(psd->width) * be16(psd->depth) / 8;
146 #ifdef DEBUG_PSD
147 log_info("psd_image_data_size_max %lu\n", (long unsigned)psd_image_data_size_max);
148 #endif
149 if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
150 file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
151 {
152 const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
153 const unsigned int l=get_be32(buffer, i)+4;
154 #ifdef DEBUG_PSD
155 log_info("Color mode at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
156 #endif
157 if(l<4)
158 return DC_STOP;
159 file_recovery->calculated_file_size+=l;
160 file_recovery->data_check=&psd_skip_image_resources;
161 return psd_skip_image_resources(buffer, buffer_size, file_recovery);
162 }
163 return DC_CONTINUE;
164 }
165
file_check_psd(file_recovery_t * file_recovery)166 static void file_check_psd(file_recovery_t *file_recovery)
167 {
168 if(file_recovery->file_size < file_recovery->calculated_file_size)
169 file_recovery->file_size=0;
170 else if(file_recovery->file_size > file_recovery->calculated_file_size + psd_image_data_size_max)
171 file_recovery->file_size=file_recovery->calculated_file_size + psd_image_data_size_max;
172 }
173
register_header_check_psd(file_stat_t * file_stat)174 static void register_header_check_psd(file_stat_t *file_stat)
175 {
176 static const unsigned char psd_header[6]={'8', 'B', 'P', 'S', 0x00, 0x01};
177 register_header_check(0, psd_header,sizeof(psd_header), &header_check_psd, file_stat);
178 }
179