1 /*
2 
3     File: file_pct.c
4 
5     Copyright (C) 2008 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
28 #include <stdio.h>
29 #include "types.h"
30 #include "filegen.h"
31 #include "common.h"
32 #include "log.h"
33 
34 extern const file_hint_t file_hint_indd;
35 static void register_header_check_pct(file_stat_t *file_stat);
36 static int header_check_pct(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);
37 static void file_check_pct(file_recovery_t *file_recovery);
38 
39 const file_hint_t file_hint_pct= {
40   .extension="pct",
41   .description="Macintosh Picture",
42   .max_filesize=PHOTOREC_MAX_FILE_SIZE,
43   .recover=1,
44   .enable_by_default=1,
45   .register_header_check=&register_header_check_pct
46 };
47 
48 /* We are searching for PICTv2 files
49    http://www.fileformat.info/format/macpict/
50    SHORT    Version operator (0x0011)
51    SHORT    Version number (0x02ff)
52    SHORT    Header opcode for Version 2 (0C00)
53  */
54 
55 struct pct_file_entry {
56   uint16_t filesize;		/* 0x00 */
57   uint16_t XMin;		/* 0x02 72 DPI */
58   uint16_t YMin;		/* 0x04 */
59   uint16_t XMax;		/* 0x06 */
60   uint16_t YMax;		/* 0x08 */
61   uint16_t VersionOperator;	/* 0x0A 0x0011 */
62   uint16_t VersionNumber;	/* 0x0C 0x02ff */
63   uint16_t HeaderOpcode; 	/* 0x0E 0x0C00 */
64   uint16_t Val;			/* 0x10 0xFFEF or 0xFFEE */
65   uint16_t Reserved; 		/* 0x12 0x0000 */
66   uint32_t HDPI;		/* 0x14 */
67   uint32_t VDPI;		/* 0x18 */
68 #if 0
69   uint16_t OYMax;	// pbmplus format
70   uint16_t OYMin;
71   uint16_t OXMax;
72   uint16_t OXMin;
73 #else
74   uint16_t OXMin;
75   uint16_t OYMin;
76   uint16_t OXMax;
77   uint16_t OYMax;
78 #endif
79   uint32_t Reserved2;		/* 0x24 */
80 } __attribute__ ((gcc_struct, __packed__));
81 
header_check_pct(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)82 static int header_check_pct(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)
83 {
84   const struct pct_file_entry *pct=(const struct pct_file_entry *)(&buffer[0x200]);
85   if(be16(pct->XMin) <= be16(pct->XMax) &&
86       be16(pct->YMin) <= be16(pct->YMax) &&
87       ((be16(pct->OXMin) <= be16(pct->OXMax) &&
88 	be16(pct->OYMin) <= be16(pct->OYMax)) ||
89        (be16(pct->OYMax) <= be16(pct->OXMax) &&	/* pbmplus creates boggus files */
90 	be16(pct->OYMin) <= be16(pct->OXMin))) &&
91 	be16(pct->XMin)==0 &&			/* Reject some valid but uncommon files */
92 	be16(pct->YMin)==0 &&
93 	be16(pct->OYMin)==0 &&
94 	be16(pct->VersionOperator)==0x0011 &&
95 	be16(pct->VersionNumber)==0x02ff)
96   {
97     if(file_recovery->file_stat != NULL &&
98 	file_recovery->file_stat->file_hint==&file_hint_indd)
99     {
100       if(header_ignored_adv(file_recovery, file_recovery_new)==0)
101 	return 0;
102     }
103     reset_file_recovery(file_recovery_new);
104     file_recovery_new->extension=file_hint_pct.extension;
105     /* We only have the low 16bits of the filesystem */
106     file_recovery_new->min_filesize=(buffer[0x200]<<8)+buffer[0x201];
107     file_recovery_new->file_check=&file_check_pct;
108 #ifdef DEBUG_PCT
109     log_info("X %u-%u, Y %u-%u\n",
110 	be16(pct->XMin), be16(pct->XMax),
111 	be16(pct->YMin), be16(pct->YMax));
112     log_info("X %u-%u, Y %u-%u\n",
113 	be16(pct->OXMin), be16(pct->OXMax),
114 	be16(pct->OYMin), be16(pct->OYMax));
115 #endif
116     return 1;
117   }
118   return 0;
119 }
120 
file_check_pct(file_recovery_t * file_recovery)121 static void file_check_pct(file_recovery_t *file_recovery)
122 {
123   if(file_recovery->file_size<0x210 ||
124       file_recovery->file_size<file_recovery->min_filesize)
125   {
126     file_recovery->file_size=0;
127     return ;
128   }
129   file_recovery->file_size-=((file_recovery->file_size-file_recovery->min_filesize)&0xFFFF);
130 }
131 
register_header_check_pct(file_stat_t * file_stat)132 static void register_header_check_pct(file_stat_t *file_stat)
133 {
134   static const unsigned char pct_header[6]= { 0x00, 0x11, 0x02, 0xff, 0x0c, 0x00};
135   register_header_check(0x20a, pct_header,sizeof(pct_header), &header_check_pct, file_stat);
136 }
137