summaryrefslogtreecommitdiffstats
path: root/src/file_fat.c
blob: 1f2926a3b9b8825fbe8298481fa6f2af16937ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*

    File: file_fat.c

    Copyright (C) 2009 Christophe GRENIER <grenier@cgsecurity.org>
  
    This software is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
  
    You should have received a copy of the GNU General Public License along
    with this program; if not, write the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#include "types.h"
#include "common.h"
#include "filegen.h"
#include "log.h"
#include "memmem.h"
#include "fat.h"
#include "fat_common.h"

static void register_header_check_fat(file_stat_t *file_stat);
static int header_check_fat(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);

const file_hint_t file_hint_fat= {
  .extension="fat",
  .description="FAT",
  .min_header_distance=0,
  .max_filesize=PHOTOREC_MAX_FILE_SIZE,
  .recover=1,
  .enable_by_default=0,
  .register_header_check=&register_header_check_fat
};

static const unsigned char fat_sign[2]= { 0x55, 0xAA};

static void register_header_check_fat(file_stat_t *file_stat)
{
  register_header_check(0x1fe, fat_sign, sizeof(fat_sign), &header_check_fat, file_stat);
}

static int header_check_fat(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)
{
  const struct fat_boot_sector *fat_header=(const struct fat_boot_sector *)buffer;
  uint64_t start_fat1,start_data,part_size;
  unsigned long int no_of_cluster,fat_length,fat_length_calc;
  if(!(le16(fat_header->marker)==0xAA55
        && (fat_header->ignored[0]==0xeb || fat_header->ignored[0]==0xe9)
        && (fat_header->fats==1 || fat_header->fats==2)))
    return 0;   /* Obviously not a FAT */
  if(!((fat_header->ignored[0]==0xeb && fat_header->ignored[2]==0x90)||fat_header->ignored[0]==0xe9))
    return 0;
  if(fat_sector_size(fat_header)==0 || fat_sector_size(fat_header)%512!=0)
    return 0;
  switch(fat_header->sectors_per_cluster)
  {
    case 1:
    case 2:
    case 4:
    case 8:
    case 16:
    case 32:
    case 64:
    case 128:
      break;
    default:
      return 0;
  }
  if(fat_header->fats!=1 && fat_header->fats!=2)
    return 0;
  if(fat_header->media!=0xF0 && fat_header->media<0xF8)
    return 0;
  fat_length=le16(fat_header->fat_length)>0?le16(fat_header->fat_length):le32(fat_header->fat32_length);
  part_size=(fat_sectors(fat_header)>0?fat_sectors(fat_header):le32(fat_header->total_sect));
  start_fat1=le16(fat_header->reserved);
  start_data=start_fat1+fat_header->fats*fat_length+(get_dir_entries(fat_header)*32+fat_sector_size(fat_header)-1)/fat_sector_size(fat_header);
  if(part_size < start_data)
    return 0;
  no_of_cluster=(part_size-start_data)/fat_header->sectors_per_cluster;
  if(no_of_cluster<4085)
  {
    /* FAT12 */
    if((get_dir_entries(fat_header)==0)||(get_dir_entries(fat_header)%16!=0))
      return 0;
    if((le16(fat_header->fat_length)>256)||(le16(fat_header->fat_length)==0))
      return 0;
    fat_length_calc=((no_of_cluster+2+fat_sector_size(fat_header)*2/3-1)*3/2/fat_sector_size(fat_header));
  }
  else if(no_of_cluster<65525)
  {
    /* FAT16 */
    if(le16(fat_header->fat_length)==0)
      return 0;
    if((get_dir_entries(fat_header)==0)||(get_dir_entries(fat_header)%16!=0))
      return 0;
    fat_length_calc=((no_of_cluster+2+fat_sector_size(fat_header)/2-1)*2/fat_sector_size(fat_header));
  }
  else
  {
    /* FAT32 */
    if(fat_sectors(fat_header)!=0)
      return 0;
    if(get_dir_entries(fat_header)!=0)
      return 0;
    if((le32(fat_header->root_cluster)<2) ||(le32(fat_header->root_cluster)>=2+no_of_cluster))
      return 0;
    fat_length_calc=((no_of_cluster+2+fat_sector_size(fat_header)/4-1)*4/fat_sector_size(fat_header));
  }
  if(fat_length<fat_length_calc)
    return 0;
  reset_file_recovery(file_recovery_new);
  file_recovery_new->extension=file_hint_fat.extension;
  file_recovery_new->calculated_file_size=(uint64_t)
    (fat_sectors(fat_header)>0?fat_sectors(fat_header):le32(fat_header->total_sect)) *
    fat_sector_size(fat_header);
  file_recovery_new->data_check=&data_check_size;
  file_recovery_new->file_check=&file_check_size;
  return 1;
}