1 /*
2 
3     File: file_x3f.c
4 
5     Copyright (C) 1998-2005,2007 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 extern const file_hint_t file_hint_x3i;
35 static void register_header_check_x3f(file_stat_t *file_stat);
36 static int header_check_x3f(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 
38 const file_hint_t file_hint_x3f= {
39   .extension="x3f",
40   .description="Sigma/Foveon X3 raw picture",
41   .max_filesize=PHOTOREC_MAX_FILE_SIZE,
42   .recover=1,
43   .enable_by_default=1,
44   .register_header_check=&register_header_check_x3f
45 };
46 
47 struct x3f_header
48 {
49   uint32_t	id;
50   uint32_t	version;
51   unsigned char uuid[16];
52   uint32_t	mark;
53   uint32_t	columns;
54   uint32_t	rows;
55   uint32_t	rotation;
56   /* version 2.1 and later have additional fields */
57 } __attribute__ ((gcc_struct, __packed__));
58 
header_check_x3f(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)59 static int header_check_x3f(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)
60 {
61   const struct x3f_header *h=(const struct x3f_header *)buffer;
62   const unsigned int rotation=le32(h->rotation);
63   if(le32(h->rows)==0 || le32(h->columns)==0)
64     return 0;
65   if(rotation!=0 && rotation!=90 && rotation!=180 && rotation!=270)
66     return 0;
67   if(file_recovery->file_stat!=NULL &&
68       file_recovery->file_stat->file_hint==&file_hint_x3i &&
69       safe_header_only==0)
70   {
71     return 0;
72   }
73   reset_file_recovery(file_recovery_new);
74   file_recovery_new->extension=file_hint_x3f.extension;
75   file_recovery_new->min_filesize=1024;
76   return 1;
77 }
78 
register_header_check_x3f(file_stat_t * file_stat)79 static void register_header_check_x3f(file_stat_t *file_stat)
80 {
81   static const unsigned char x3f_header[4]= {'F','O','V','b'};
82   register_header_check(0, x3f_header,sizeof(x3f_header), &header_check_x3f, file_stat);
83 }
84