1 /*
2 
3     File: file_dad.c
4 
5     Copyright (C) 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 "types.h"
31 #include "filegen.h"
32 #include "common.h"
33 #include "log.h"
34 
35 static void register_header_check_dad(file_stat_t *file_stat);
36 
37 const file_hint_t file_hint_dad= {
38   .extension="dad",
39   .description="Micae DVR",
40   .max_filesize=PHOTOREC_MAX_FILE_SIZE,
41   .recover=1,
42   .enable_by_default=1,
43   .register_header_check=&register_header_check_dad
44 };
45 
46 struct dad_header
47 {
48   uint32_t magic;
49   uint32_t unk1;
50   uint32_t unk2;
51   uint32_t size;
52 } __attribute__ ((gcc_struct, __packed__));
53 
data_check_dad(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)54 static data_check_t data_check_dad(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
55 {
56   while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
57       file_recovery->calculated_file_size + 16 <= file_recovery->file_size + buffer_size/2)
58   {
59     const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
60     const struct dad_header *dad=(const struct dad_header *)&buffer[i];
61 #ifdef DEBUG_DAD
62     log_info("%llu magic %08x => %llu\n",
63 	(long long unsigned)file_recovery->calculated_file_size, le32(dad->magic),
64 	(long long unsigned)file_recovery->calculated_file_size + le32(dad->size));
65 #endif
66     if(dad->magic!=le32(0x56414844) || le32(dad->size)<16)
67       return DC_STOP;
68     file_recovery->calculated_file_size+=le32(dad->size);
69   }
70   return DC_CONTINUE;
71 }
72 
header_check_dad(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)73 static int header_check_dad(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)
74 {
75   const struct dad_header *dad=(const struct dad_header *)buffer;
76   if(le32(dad->size)<16)
77     return 0;
78   if(file_recovery->file_stat!=NULL &&
79       file_recovery->file_stat->file_hint==&file_hint_dad &&
80       (file_recovery->calculated_file_size==file_recovery->file_size ||
81        file_recovery->blocksize < 16))
82   {
83     header_ignored(file_recovery_new);
84     return 0;
85   }
86   reset_file_recovery(file_recovery_new);
87   file_recovery_new->extension=file_hint_dad.extension;
88   file_recovery_new->min_filesize=le32(dad->size);
89   if(file_recovery_new->blocksize >= 16)
90   {
91     file_recovery_new->data_check=&data_check_dad;
92     file_recovery_new->file_check=&file_check_size_max;
93   }
94   return 1;
95 }
96 
register_header_check_dad(file_stat_t * file_stat)97 static void register_header_check_dad(file_stat_t *file_stat)
98 {
99   register_header_check(0, "DHAV", 4, &header_check_dad, file_stat);
100 }
101