src/file_psb.c, src/file_psd.c: Fix Photoshop file parsers

Add Frama-C annotations
This commit is contained in:
Christophe Grenier 2021-02-28 11:56:04 +01:00
parent 75190887da
commit 2e47a667ea
2 changed files with 330 additions and 196 deletions

View file

@ -2,7 +2,7 @@
File: file_psb.c
Copyright (C) 2006-2009,2013 Christophe GRENIER <grenier@cgsecurity.org>
Copyright (C) 2006-2009,2013,2021 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
@ -32,13 +32,14 @@
#include "filegen.h"
#include "common.h"
#ifdef DEBUG_PSD
#ifdef DEBUG_PHOTOSHOP
#include "log.h"
#endif
/* https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/ */
/*@ requires \valid(file_stat); */
static void register_header_check_psb(file_stat_t *file_stat);
static data_check_t psb_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
static void file_check_psb(file_recovery_t *file_recovery);
const file_hint_t file_hint_psb= {
.extension="psb",
@ -49,8 +50,6 @@ const file_hint_t file_hint_psb= {
.register_header_check=&register_header_check_psb
};
static uint64_t psb_image_data_size_max=0;
struct psb_file_header
{
char signature[4];
@ -63,20 +62,176 @@ struct psb_file_header
uint16_t color_mode; /* Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 */
} __attribute__ ((gcc_struct, __packed__));
/*@
@ requires \valid_read((char *)buffer + (offset .. offset + 3));
@ assigns \nothing;
@*/
static uint32_t get_be32(const void *buffer, const unsigned int offset)
{
const uint32_t *val=(const uint32_t *)((const unsigned char *)buffer+offset);
return be32(*val);
}
/*@
@ requires \valid_read((char *)buffer + (offset .. offset + 7));
@ assigns \nothing;
@*/
static uint64_t get_be64(const void *buffer, const unsigned int offset)
{
const uint64_t *val=(const uint64_t *)((const unsigned char *)buffer+offset);
return be64(*val);
}
/*@
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psb_skip_image_data;
@ ensures file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psb_skip_image_data(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
file_recovery->calculated_file_size+=2;
file_recovery->data_check=NULL;
return DC_CONTINUE;
}
/*@
@ requires buffer_size > 0;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psb_skip_layer_info;
@ ensures file_recovery->data_check==&psb_skip_layer_info || file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE || \result == DC_STOP;
@ assigns file_recovery->data_check, file_recovery->file_check, file_recovery->calculated_file_size;
@*/
static data_check_t psb_skip_layer_info(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 8 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 8 ; */
const uint64_t l=get_be64(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Layer info at 0x%lx, l=0x%lx\n", (long unsigned)file_recovery->calculated_file_size, (long unsigned)l);
#endif
if(l> PHOTOREC_MAX_FILE_SIZE)
return DC_STOP;
file_recovery->calculated_file_size+=l+8;
#ifdef DEBUG_PHOTOSHOP
log_info("Image data at 0x%lx\n", (long unsigned)file_recovery->calculated_file_size);
#endif
file_recovery->data_check=&psb_skip_image_data;
return psb_skip_image_data(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size > 0;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psb_skip_image_resources;
@ ensures file_recovery->data_check==&psb_skip_image_resources || file_recovery->data_check==&psb_skip_layer_info || file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE || \result == DC_STOP;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psb_skip_image_resources(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 4 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 4 ; */
const unsigned int l=get_be32(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Image resource at 0x%lx, l=0x%x\n", (long unsigned)file_recovery->calculated_file_size, l);
#endif
file_recovery->calculated_file_size+=l+4;
#ifdef DEBUG_PHOTOSHOP
log_info("Layer info at 0x%lx\n", (long unsigned)file_recovery->calculated_file_size);
#endif
file_recovery->data_check=&psb_skip_layer_info;
return psb_skip_layer_info(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size >= 32;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires file_recovery->data_check==&psb_skip_color_mode;
@ ensures \result == DC_CONTINUE || \result == DC_ERROR || \result == DC_STOP;
@ ensures file_recovery->data_check==&psb_skip_color_mode || file_recovery->data_check==&psb_skip_image_resources || file_recovery->data_check==&psb_skip_layer_info || file_recovery->data_check==\null;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psb_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
const struct psb_file_header *hdr=(const struct psb_file_header *)&buffer[buffer_size/2];
const unsigned int channels=be16(hdr->channels);
const unsigned int depth=be16(hdr->depth);
const unsigned int height=be32(hdr->height);
const unsigned int width=be32(hdr->width);
if(channels==0 || channels>56 ||
height==0 || height>300000 ||
width==0 || width>300000 ||
(depth!=1 && depth!=8 && depth!=16 && depth!=32))
return DC_ERROR;
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 4 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 4 ; */
const unsigned int l=get_be32(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Color mode at 0x%lx, l=0x%x\n", (long unsigned)file_recovery->calculated_file_size, l);
#endif
if(l!=0 && l<4)
return DC_ERROR;
file_recovery->calculated_file_size+=(uint64_t)l+4;
file_recovery->data_check=&psb_skip_image_resources;
return psb_skip_image_resources(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size >= sizeof(struct psb_file_header);
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires valid_file_recovery(file_recovery);
@ requires \valid(file_recovery_new);
@ requires file_recovery_new->blocksize > 0;
@ requires separation: \separated(&file_hint_psb, buffer+(..), file_recovery, file_recovery_new);
@ ensures \result == 0 || \result == 1;
@ ensures \result!=0 ==> valid_file_recovery(file_recovery_new);
@ assigns *file_recovery_new;
@*/
static int header_check_psb(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 psb_file_header *hdr=(const struct psb_file_header *)buffer;
#ifdef DEBUG_PSD
log_info("channels %u\n", be16(hdr->channels));
log_info("height %u\n", be32(hdr->height));
log_info("width %u\n", be32(hdr->width));
log_info("depth %u\n", be16(hdr->depth));
const unsigned int channels=be16(hdr->channels);
const unsigned int depth=be16(hdr->depth);
const unsigned int height=be32(hdr->height);
const unsigned int width=be32(hdr->width);
#ifdef DEBUG_PHOTOSHOP
log_info("channels %u\n", channels);
log_info("height %u\n", height);
log_info("width %u\n", width);
log_info("depth %u\n", depth);
log_info("color_mode %u\n", be16(hdr->color_mode));
#endif
if(be16(hdr->channels)==0 || be16(hdr->channels)>56 ||
be32(hdr->height)==0 || be32(hdr->height)>300000 ||
be32(hdr->width)==0 || be32(hdr->width)>300000 ||
be16(hdr->depth)==0 || (be16(hdr->depth)!=1 && be16(hdr->depth)%8!=0))
if(channels==0 || channels>56 ||
height==0 || height>300000 ||
width==0 || width>300000 ||
(depth!=1 && depth!=8 && depth!=16 && depth!=32))
return 0;
reset_file_recovery(file_recovery_new);
file_recovery_new->min_filesize=70;
@ -86,92 +241,10 @@ static int header_check_psb(const unsigned char *buffer, const unsigned int buff
/* File header */
file_recovery_new->calculated_file_size=0x1a;
file_recovery_new->data_check=&psb_skip_color_mode;
file_recovery_new->file_check=&file_check_psb;
file_recovery_new->file_check=&file_check_size_min;
return 1;
}
static uint64_t get_be64(const void *buffer, const unsigned int offset)
{
const uint64_t *val=(const uint64_t *)((const unsigned char *)buffer+offset);
return be64(*val);
}
static data_check_t psb_skip_image_data(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
file_recovery->file_check=NULL;
return DC_CONTINUE;
}
static data_check_t psb_skip_layer_info(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be64(buffer, i)+8;
#ifdef DEBUG_PSD
log_info("Image data at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psb_skip_image_data;
return psb_skip_image_data(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static data_check_t psb_skip_image_resources(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be64(buffer, i)+8;
#ifdef DEBUG_PSD
log_info("Layer info at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psb_skip_layer_info;
return psb_skip_layer_info(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static data_check_t psb_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
const struct psb_file_header *psb=(const struct psb_file_header *)&buffer[buffer_size/2];
psb_image_data_size_max=(uint64_t)be16(psb->channels) * be32(psb->height) * be32(psb->width) * be16(psb->depth) / 8;
#ifdef DEBUG_PSD
log_info("psb_image_data_size_max %lu\n", (long unsigned)psb_image_data_size_max);
#endif
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be64(buffer, i)+8;
#ifdef DEBUG_PSD
log_info("Color mode at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psb_skip_image_resources;
return psb_skip_image_resources(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static void file_check_psb(file_recovery_t *file_recovery)
{
if(file_recovery->file_size < file_recovery->calculated_file_size)
file_recovery->file_size=0;
else if(file_recovery->file_size > file_recovery->calculated_file_size + psb_image_data_size_max)
file_recovery->file_size=file_recovery->calculated_file_size + psb_image_data_size_max;
}
static void register_header_check_psb(file_stat_t *file_stat)
{
static const unsigned char psb_header[6]={'8', 'B', 'P', 'S', 0x00, 0x02};

View file

@ -2,7 +2,7 @@
File: file_psd.c
Copyright (C) 2006-2009 Christophe GRENIER <grenier@cgsecurity.org>
Copyright (C) 2006-2009,2021 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
@ -32,13 +32,14 @@
#include "filegen.h"
#include "common.h"
#ifdef DEBUG_PSD
#ifdef DEBUG_PHOTOSHOP
#include "log.h"
#endif
/* https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/ */
/*@ requires \valid(file_stat); */
static void register_header_check_psd(file_stat_t *file_stat);
static data_check_t psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
static void file_check_psd(file_recovery_t *file_recovery);
const file_hint_t file_hint_psd= {
.extension="psd",
@ -49,8 +50,6 @@ const file_hint_t file_hint_psd= {
.register_header_check=&register_header_check_psd
};
static uint64_t psd_image_data_size_max=0;
struct psd_file_header
{
char signature[4];
@ -63,20 +62,164 @@ struct psd_file_header
uint16_t color_mode; /* Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 */
} __attribute__ ((gcc_struct, __packed__));
/*@
@ requires \valid_read((char *)buffer + (offset .. offset + 3));
@ assigns \nothing;
@*/
static uint32_t get_be32(const void *buffer, const unsigned int offset)
{
const uint32_t *val=(const uint32_t *)((const unsigned char *)buffer+offset);
return be32(*val);
}
/*@
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psd_skip_image_data;
@ ensures file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psd_skip_image_data(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
file_recovery->calculated_file_size+=2;
file_recovery->data_check=NULL;
return DC_CONTINUE;
}
/*@
@ requires buffer_size > 0;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psd_skip_layer_info;
@ ensures file_recovery->data_check==&psd_skip_layer_info || file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE || \result == DC_STOP;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psd_skip_layer_info(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 4 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 4 ; */
const unsigned int l=get_be32(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Layer info at 0x%lx, l=0x%x\n", (long unsigned)file_recovery->calculated_file_size, l);
#endif
file_recovery->calculated_file_size+=l+4;
#ifdef DEBUG_PHOTOSHOP
log_info("Image data at 0x%lx\n", (long unsigned)file_recovery->calculated_file_size);
#endif
file_recovery->data_check=&psd_skip_image_data;
return psd_skip_image_data(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size > 0;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires valid_file_recovery(file_recovery);
@ requires file_recovery->data_check==&psd_skip_image_resources;
@ ensures file_recovery->data_check==&psd_skip_image_resources || file_recovery->data_check==&psd_skip_layer_info || file_recovery->data_check==\null;
@ ensures \result == DC_CONTINUE || \result == DC_STOP;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psd_skip_image_resources(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 4 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 4 ; */
const unsigned int l=get_be32(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Image resource at 0x%lx, l=0x%x\n", (long unsigned)file_recovery->calculated_file_size, l);
#endif
file_recovery->calculated_file_size+=l+4;
#ifdef DEBUG_PHOTOSHOP
log_info("Layer info at 0x%lx\n", (long unsigned)file_recovery->calculated_file_size);
#endif
file_recovery->data_check=&psd_skip_layer_info;
return psd_skip_layer_info(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size >= 32;
@ requires (buffer_size&1)==0;
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires \valid(file_recovery);
@ requires file_recovery->data_check==&psd_skip_color_mode;
@ ensures \result == DC_CONTINUE || \result == DC_ERROR || \result == DC_STOP;
@ ensures file_recovery->data_check==&psd_skip_color_mode || file_recovery->data_check==&psd_skip_image_resources || file_recovery->data_check==&psd_skip_layer_info || file_recovery->data_check==\null;
@ assigns file_recovery->data_check, file_recovery->calculated_file_size;
@*/
static data_check_t psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
const struct psd_file_header *hdr=(const struct psd_file_header *)&buffer[buffer_size/2];
const unsigned int channels=be16(hdr->channels);
const unsigned int depth=be16(hdr->depth);
const unsigned int height=be32(hdr->height);
const unsigned int width=be32(hdr->width);
if(channels==0 || channels>56 ||
height==0 || height>30000 ||
width==0 || width>30000 ||
(depth!=1 && depth!=8 && depth!=16 && depth!=32))
return DC_ERROR;
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 4 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size + buffer_size/2 - file_recovery->file_size;
/*@ assert 0 <= i < buffer_size - 4 ; */
const unsigned int l=get_be32(buffer, i);
#ifdef DEBUG_PHOTOSHOP
log_info("Color mode at 0x%lx, l=0x%x\n", (long unsigned)file_recovery->calculated_file_size, l);
#endif
if(l!=0 && l<4)
return DC_ERROR;
file_recovery->calculated_file_size+=(uint64_t)l+4;
file_recovery->data_check=&psd_skip_image_resources;
return psd_skip_image_resources(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
/*@
@ requires buffer_size >= sizeof(struct psd_file_header);
@ requires \valid_read(buffer+(0..buffer_size-1));
@ requires valid_file_recovery(file_recovery);
@ requires \valid(file_recovery_new);
@ requires file_recovery_new->blocksize > 0;
@ requires separation: \separated(&file_hint_psd, buffer+(..), file_recovery, file_recovery_new);
@ ensures \result == 0 || \result == 1;
@ ensures \result!=0 ==> valid_file_recovery(file_recovery_new);
@ assigns *file_recovery_new;
@*/
static int header_check_psd(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 psd_file_header *hdr=(const struct psd_file_header *)buffer;
#ifdef DEBUG_PSD
log_info("channels %u\n", be16(hdr->channels));
log_info("height %u\n", be32(hdr->height));
log_info("width %u\n", be32(hdr->width));
log_info("depth %u\n", be16(hdr->depth));
const unsigned int channels=be16(hdr->channels);
const unsigned int depth=be16(hdr->depth);
const unsigned int height=be32(hdr->height);
const unsigned int width=be32(hdr->width);
#ifdef DEBUG_PHOTOSHOP
log_info("channels %u\n", channels);
log_info("height %u\n", height);
log_info("width %u\n", width);
log_info("depth %u\n", depth);
log_info("color_mode %u\n", be16(hdr->color_mode));
#endif
if(be16(hdr->channels)==0 || be16(hdr->channels)>56 ||
be32(hdr->height)==0 || be32(hdr->height)>30000 ||
be32(hdr->width)==0 || be32(hdr->width)>30000 ||
be16(hdr->depth)==0 || (be16(hdr->depth)!=1 && be16(hdr->depth)%8!=0))
if(channels==0 || channels>56 ||
height==0 || height>30000 ||
width==0 || width>30000 ||
(depth!=1 && depth!=8 && depth!=16 && depth!=32))
return 0;
reset_file_recovery(file_recovery_new);
file_recovery_new->min_filesize=70;
@ -86,92 +229,10 @@ static int header_check_psd(const unsigned char *buffer, const unsigned int buff
/* File header */
file_recovery_new->calculated_file_size=0x1a;
file_recovery_new->data_check=&psd_skip_color_mode;
file_recovery_new->file_check=&file_check_psd;
file_recovery_new->file_check=&file_check_size_min;
return 1;
}
static uint32_t get_be32(const void *buffer, const unsigned int offset)
{
const uint32_t *val=(const uint32_t *)((const unsigned char *)buffer+offset);
return be32(*val);
}
static data_check_t psd_skip_image_data(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
file_recovery->file_check=NULL;
return DC_CONTINUE;
}
static data_check_t psd_skip_layer_info(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be32(buffer, i)+4;
#ifdef DEBUG_PSD
log_info("Image data at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psd_skip_image_data;
return psd_skip_image_data(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static data_check_t psd_skip_image_resources(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be32(buffer, i)+4;
#ifdef DEBUG_PSD
log_info("Layer info at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psd_skip_layer_info;
return psd_skip_layer_info(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static data_check_t psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
const struct psd_file_header *psd=(const struct psd_file_header *)&buffer[buffer_size/2];
psd_image_data_size_max=(uint64_t)be16(psd->channels) * be32(psd->height) * be32(psd->width) * be16(psd->depth) / 8;
#ifdef DEBUG_PSD
log_info("psd_image_data_size_max %lu\n", (long unsigned)psd_image_data_size_max);
#endif
if(file_recovery->calculated_file_size + buffer_size/2 >= file_recovery->file_size &&
file_recovery->calculated_file_size + 16 < file_recovery->file_size + buffer_size/2)
{
const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
const unsigned int l=get_be32(buffer, i)+4;
#ifdef DEBUG_PSD
log_info("Color mode at 0x%lx\n", (long unsigned)(l + file_recovery->calculated_file_size));
#endif
if(l<4)
return DC_STOP;
file_recovery->calculated_file_size+=l;
file_recovery->data_check=&psd_skip_image_resources;
return psd_skip_image_resources(buffer, buffer_size, file_recovery);
}
return DC_CONTINUE;
}
static void file_check_psd(file_recovery_t *file_recovery)
{
if(file_recovery->file_size < file_recovery->calculated_file_size)
file_recovery->file_size=0;
else if(file_recovery->file_size > file_recovery->calculated_file_size + psd_image_data_size_max)
file_recovery->file_size=file_recovery->calculated_file_size + psd_image_data_size_max;
}
static void register_header_check_psd(file_stat_t *file_stat)
{
static const unsigned char psd_header[6]={'8', 'B', 'P', 'S', 0x00, 0x01};