1 /*
2
3 File: file_d2s.c
4
5 Copyright (C) 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 #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 "common.h"
31 #include "filegen.h"
32
33 static void register_header_check_d2s(file_stat_t *file_stat);
34
35 const file_hint_t file_hint_d2s= {
36 .extension="d2s",
37 .description="Diablo II",
38 .max_filesize=PHOTOREC_MAX_FILE_SIZE,
39 .recover=1,
40 .enable_by_default=1,
41 .register_header_check=®ister_header_check_d2s
42 };
43
44 struct d2s_header {
45 char magic[8];
46 uint32_t size;
47 uint32_t unk1;
48 uint32_t unk2;
49 char name[0];
50 } __attribute__ ((gcc_struct, __packed__));
51
file_rename_d2s(file_recovery_t * file_recovery)52 static void file_rename_d2s(file_recovery_t *file_recovery)
53 {
54 unsigned char buffer[512];
55 FILE *file;
56 int buffer_size;
57 if((file=fopen(file_recovery->filename, "rb"))==NULL)
58 return;
59 buffer_size=fread(buffer, 1, sizeof(buffer), file);
60 fclose(file);
61 file_rename(file_recovery, buffer, buffer_size, 0x14, NULL, 1);
62 }
63
header_check_d2s(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)64 static int header_check_d2s(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 {
66 const struct d2s_header *d2s=(const struct d2s_header*)buffer;
67 if(le32(d2s->size) < sizeof(struct d2s_header))
68 return 0;
69 reset_file_recovery(file_recovery_new);
70 file_recovery_new->extension=file_hint_d2s.extension;
71 file_recovery_new->calculated_file_size=le32(d2s->size);
72 file_recovery_new->data_check=&data_check_size;
73 file_recovery_new->file_check=&file_check_size;
74 file_recovery_new->file_rename=&file_rename_d2s;
75 return 1;
76 }
77
register_header_check_d2s(file_stat_t * file_stat)78 static void register_header_check_d2s(file_stat_t *file_stat)
79 {
80 static const unsigned char d2s_header[8]= {
81 0x55, 0xaa, 0x55, 0xaa, 0x60, 0x00, 0x00, 0x00
82 };
83 register_header_check(0, d2s_header,sizeof(d2s_header), &header_check_d2s, file_stat);
84 }
85