diff --git a/src/file_psb.c b/src/file_psb.c index e4f2ce71..5194a38c 100644 --- a/src/file_psb.c +++ b/src/file_psb.c @@ -52,6 +52,17 @@ const file_hint_t file_hint_psb= { static const unsigned char psb_header[6]={'8', 'B', 'P', 'S', 0x00, 0x02}; static uint64_t psb_image_data_size_max=0; +struct psb_file_header +{ + char signature[4]; + uint16_t version; /* must be 2 */ + char reserved[6]; /* must be 0 */ + uint16_t channels; /* between 1 and 56 */ + uint32_t height; /* max of 300,000 */ + uint32_t width; /* max of 300,000 */ + uint16_t depth; /* 1, 8, 16 or 32 */ + uint16_t color_mode; /* Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 */ +} __attribute__ ((__packed__)); static void register_header_check_psb(file_stat_t *file_stat) { @@ -126,10 +137,8 @@ static int psb_skip_image_resources(const unsigned char *buffer, const unsigned static int psb_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery) { - psb_image_data_size_max=(buffer[buffer_size/2+12]<<8 | buffer[buffer_size/2+13]) * - (buffer[buffer_size/2+14]<<24 | buffer[buffer_size/2+15] <<16 | buffer[buffer_size/2+16]<<8 | buffer[buffer_size/2+17]) * - (buffer[buffer_size/2+18]<<24 | buffer[buffer_size/2+19] <<16 | buffer[buffer_size/2+20]<<8 | buffer[buffer_size/2+21]) * - buffer[buffer_size/2+23] / 8; + const struct psb_file_header *psb=(const struct psb_file_header *)&buffer[buffer_size/2]; + psb_image_data_size_max=(uint64_t)le16(psb->channels) * le32(psb->height) * le32(psb->width) * le16(psb->depth) / 8; #ifdef DEBUG_PSD log_info("psb_image_data_size_max %lu\n", (long unsigned)psb_image_data_size_max); #endif diff --git a/src/file_psd.c b/src/file_psd.c index 357c81eb..83e8be67 100644 --- a/src/file_psd.c +++ b/src/file_psd.c @@ -50,28 +50,30 @@ const file_hint_t file_hint_psd= { .register_header_check=®ister_header_check_psd }; -static const unsigned char psd_header[6]={'8', 'B', 'P', 'S', 0x00, 0x01}; static uint64_t psd_image_data_size_max=0; -static void register_header_check_psd(file_stat_t *file_stat) +struct psd_file_header { - register_header_check(0, psd_header,sizeof(psd_header), &header_check_psd, file_stat); -} + char signature[4]; + uint16_t version; /* must be 1 */ + char reserved[6]; /* must be 0 */ + uint16_t channels; /* between 1 and 56 */ + uint32_t height; /* max of 30,000 */ + uint32_t width; /* max of 30,000 */ + uint16_t depth; /* 1, 8, 16 or 32 */ + uint16_t color_mode; /* Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9 */ +} __attribute__ ((__packed__)); 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) { - if(memcmp(buffer,psd_header,sizeof(psd_header))==0) - { - reset_file_recovery(file_recovery_new); - file_recovery_new->min_filesize=70; - file_recovery_new->extension=file_hint_psd.extension; - /* 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; - return 1; - } - return 0; + reset_file_recovery(file_recovery_new); + file_recovery_new->min_filesize=70; + file_recovery_new->extension=file_hint_psd.extension; + /* 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; + return 1; } static uint32_t get_be32(const void *buffer, const unsigned int offset) @@ -126,10 +128,8 @@ static int psd_skip_image_resources(const unsigned char *buffer, const unsigned static int psd_skip_color_mode(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery) { - psd_image_data_size_max=(buffer[buffer_size/2+12]<<8 | buffer[buffer_size/2+13]) * - (buffer[buffer_size/2+14]<<24 | buffer[buffer_size/2+15] <<16 | buffer[buffer_size/2+16]<<8 | buffer[buffer_size/2+17]) * - (buffer[buffer_size/2+18]<<24 | buffer[buffer_size/2+19] <<16 | buffer[buffer_size/2+20]<<8 | buffer[buffer_size/2+21]) * - buffer[buffer_size/2+23] / 8; + const struct psd_file_header *psd=(const struct psd_file_header *)&buffer[buffer_size/2]; + psd_image_data_size_max=(uint64_t)le16(psd->channels) * le32(psd->height) * le32(psd->width) * le16(psd->depth) / 8; #ifdef DEBUG_PSD log_info("psd_image_data_size_max %lu\n", (long unsigned)psd_image_data_size_max); #endif @@ -157,3 +157,9 @@ static void file_check_psd(file_recovery_t *file_recovery) 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}; + register_header_check(0, psd_header,sizeof(psd_header), &header_check_psd, file_stat); +}