1 /*
2
3 File: file_ibd.c
4
5 Copyright (C) 2015 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_ibd(file_stat_t *file_stat);
36
37 const file_hint_t file_hint_ibd= {
38 .extension="ibd",
39 .description="InnoDB database file",
40 .max_filesize=PHOTOREC_MAX_FILE_SIZE,
41 .recover=1,
42 .enable_by_default=1,
43 .register_header_check=®ister_header_check_ibd
44 };
45
46 #define FSP_HEADER_OFFSET 38
47 #define FSP_SPACE_FLAGS 16 /* fsp_space_t.flags, similar to dict_table_t::flags */
48 #define FIL_PAGE_TYPE_FSP_HDR 8 /* File space header */
49 #define DICT_TF_BITS 6 /* number of flag bits */
50 #define DICT_TF_FORMAT_SHIFT 5 /* file format */
51 #define DICT_TF_FORMAT_MASK \
52 ((~(~0 << (DICT_TF_BITS - DICT_TF_FORMAT_SHIFT))) << DICT_TF_FORMAT_SHIFT)
53
54 #define DICT_TF_FORMAT_ZIP 1 /* InnoDB plugin for 5.1: compressed tables */
55
56 struct innodb_fil_header
57 {
58 uint32_t space_or_chksum;
59 uint32_t offset;
60 uint32_t prev;
61 uint32_t next;
62 uint64_t lsn;
63 uint16_t type;
64 uint64_t file_flush_lsn;
65 uint32_t arch_log_no_or_space_id;
66 } __attribute__ ((gcc_struct, __packed__));
67
header_check_ibd(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)68 static int header_check_ibd(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)
69 {
70 const struct innodb_fil_header *hdr=(const struct innodb_fil_header *)buffer;
71 const uint32_t *flags_ptr=(const uint32_t *)&buffer[FSP_HEADER_OFFSET + FSP_SPACE_FLAGS];
72 const uint32_t flags=be32(*flags_ptr);
73 if(be16(hdr->type)==0)
74 {
75 /* Antelope (pre-5.1.7) */
76 if(flags==0)
77 return 0;
78 }
79 else if(be16(hdr->type)==FIL_PAGE_TYPE_FSP_HDR)
80 {
81 const unsigned int format = (flags & DICT_TF_FORMAT_MASK) >> DICT_TF_FORMAT_SHIFT;
82 if(flags==0)
83 {
84 /* Antelope (5.1.7 or newer) */
85 }
86 else if(format==DICT_TF_FORMAT_ZIP)
87 {
88 /* Barracuda */
89 }
90 else
91 return 0;
92 }
93 else
94 return 0;
95 if(buffer_size >= 0xc078 && memcmp(&buffer[0xc070], "supremum", 8)!=0)
96 return 0;
97 reset_file_recovery(file_recovery_new);
98 file_recovery_new->extension=file_hint_ibd.extension;
99 file_recovery_new->min_filesize=0xc078;
100 return 1;
101 }
102
register_header_check_ibd(file_stat_t * file_stat)103 static void register_header_check_ibd(file_stat_t *file_stat)
104 {
105 register_header_check(0xc063, "infimum", 7, &header_check_ibd, file_stat);
106 }
107