1 /*
2 
3     File: file_r3d.c
4 
5     Copyright (C) 2009,2014 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 <ctype.h>
31 #include "types.h"
32 #include "filegen.h"
33 #include "common.h"
34 #include "log.h"
35 
36 static void register_header_check_r3d(file_stat_t *file_stat);
37 static int header_check_r3d(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);
38 static void file_rename_r3d(file_recovery_t *file_recovery);
39 
40 const file_hint_t file_hint_r3d= {
41   .extension="r3d",
42   .description="RED r3d camera",
43   .max_filesize=PHOTOREC_MAX_FILE_SIZE,
44   .recover=1,
45   .enable_by_default=1,
46   .register_header_check=&register_header_check_r3d
47 };
48 
49 static const unsigned char r3d_header1[4]=  { 'R' , 'E' , 'D' , '1' };
50 static const unsigned char r3d_header2[4]=  { 'R' , 'E' , 'D' , '2' };
51 
52 struct atom_struct
53 {
54   uint32_t size;
55   uint32_t type;
56 } __attribute__ ((gcc_struct, __packed__));
57 
data_check_r3d(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)58 static data_check_t data_check_r3d(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
59 {
60   while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
61       file_recovery->calculated_file_size + 8 <= file_recovery->file_size + buffer_size/2)
62   {
63     const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
64     const struct atom_struct *atom=(const struct atom_struct*)&buffer[i];
65     uint64_t atom_size=be32(atom->size);
66     if(atom_size<8)
67       return DC_STOP;
68 #ifdef DEBUG_R3D
69     log_trace("file_r3d.c: %s atom %c%c%c%c (0x%02x%02x%02x%02x) size %llu, calculated_file_size %llu\n",
70 	file_recovery->filename,
71         buffer[i+4],buffer[i+5],buffer[i+6],buffer[i+7],
72         buffer[i+4],buffer[i+5],buffer[i+6],buffer[i+7],
73         (long long unsigned)atom_size,
74         (long long unsigned)file_recovery->calculated_file_size);
75 #endif
76     if(buffer[i+4]=='R' && buffer[i+5]=='E' && buffer[i+6]=='O')
77     {
78       /* End of file */
79       file_recovery->calculated_file_size+=atom_size;
80       file_recovery->data_check=NULL;
81       return DC_CONTINUE;
82     }
83     if(buffer[i+4]!='R')
84     {
85       return DC_STOP;
86     }
87     /* REDV1 REDV RPAD RDVO RDVS RDAO RDAS REOB */
88     file_recovery->calculated_file_size+=atom_size;
89   }
90 #ifdef DEBUG_R3D
91   log_trace("file_r3d.c: new calculated_file_size %llu\n",
92       (long long unsigned)file_recovery->calculated_file_size);
93 #endif
94   return DC_CONTINUE;
95 }
96 
header_check_r3d(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)97 static int header_check_r3d(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)
98 {
99   const struct atom_struct *atom=(const struct atom_struct*)buffer;
100   if(be32(atom->size) < 8)
101     return 0;
102   if(buffer[0xa]=='R' && buffer[0xb]=='1')
103   {
104     reset_file_recovery(file_recovery_new);
105     file_recovery_new->extension=file_hint_r3d.extension;
106     file_recovery_new->file_rename=&file_rename_r3d;
107     if(file_recovery_new->blocksize < 8)
108       return 1;
109     file_recovery_new->data_check=&data_check_r3d;
110     file_recovery_new->file_check=&file_check_size;
111     return 1;
112   }
113   return 0;
114 }
115 
header_check_r3d_v2(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)116 static int header_check_r3d_v2(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)
117 {
118   if(buffer[0xa]=='R' && buffer[0xb]=='2')
119   {
120     reset_file_recovery(file_recovery_new);
121     file_recovery_new->extension=file_hint_r3d.extension;
122     return 1;
123   }
124   return 0;
125 }
126 
file_rename_r3d(file_recovery_t * file_recovery)127 static void file_rename_r3d(file_recovery_t *file_recovery)
128 {
129   unsigned char buffer[512];
130   FILE *file;
131   size_t buffer_size;
132   unsigned int i;
133   if((file=fopen(file_recovery->filename, "rb"))==NULL)
134     return;
135   buffer_size=fread(buffer, 1, sizeof(buffer), file);
136   fclose(file);
137   if(buffer_size<0x44)
138     return;
139   for(i=0x43; i< buffer_size && buffer[i]!=0 && buffer[i]!='.'; i++)
140   {
141     if(!isalnum(buffer[i]) && buffer[i]!='_')
142       return ;
143   }
144   file_rename(file_recovery, buffer, i, 0x43, NULL, 1);
145 }
146 
register_header_check_r3d(file_stat_t * file_stat)147 static void register_header_check_r3d(file_stat_t *file_stat)
148 {
149   register_header_check(4, r3d_header1, sizeof(r3d_header1), &header_check_r3d, file_stat);
150   register_header_check(4, r3d_header2, sizeof(r3d_header2), &header_check_r3d_v2, file_stat);
151 }
152