1 /*
2 
3     File: file_wv.c
4 
5     Copyright (C) 2008 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 
34 static void register_header_check_wv(file_stat_t *file_stat);
35 static data_check_t data_check_wv(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery);
36 
37 const file_hint_t file_hint_wv= {
38   .extension="wv",
39   .description="WavPack, Hybrid Lossless Wavefile Compressor",
40   .max_filesize=100*1024*1024,
41   .recover=1,
42   .enable_by_default=1,
43   .register_header_check=&register_header_check_wv
44 };
45 
46 /* See http://www.wavpack.com/file_format.txt for file format description */
47 
48 static const unsigned char wv_header[4]=  { 'w', 'v', 'p', 'k'};
49 typedef struct {
50     char ckID [4];              // "wvpk"
51     uint32_t ckSize;            // size of entire block (minus 8, of course)
52     uint16_t version;           // 0x402 to 0x410 are currently valid for decode
53     unsigned char track_no;     // track number (0 if not used, like now)
54     unsigned char index_no;     // track sub-index (0 if not used, like now)
55     uint32_t total_samples;     // total samples for entire file, but this is
56                                 // only valid if block_index == 0 and a value of
57                                 // -1 indicates unknown length
58     uint32_t block_index;       // index of first sample in block relative to
59                                 // beginning of file (normally this would start
60                                 // at 0 for the first block)
61     uint32_t block_samples;     // number of samples in this block (0 = no audio)
62     uint32_t flags;             // various flags for id and decoding
63     uint32_t crc;               // crc for actual decoded data
64 } WavpackHeader;
65 
header_check_wv(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)66 static int header_check_wv(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)
67 {
68   const WavpackHeader *wv=(const WavpackHeader*)buffer;
69   if(le32(wv->block_index)!=0)
70     return 0;
71   if(sizeof(WavpackHeader) > le32(wv->ckSize)+8)
72     return 0;
73   reset_file_recovery(file_recovery_new);
74   file_recovery_new->extension=file_hint_wv.extension;
75   file_recovery_new->min_filesize=le32(wv->ckSize)+8;
76   if(file_recovery_new->blocksize < 8)
77     return 1;
78   file_recovery_new->data_check=&data_check_wv;
79   file_recovery_new->file_check=&file_check_size;
80   return 1;
81 }
82 
data_check_wv(const unsigned char * buffer,const unsigned int buffer_size,file_recovery_t * file_recovery)83 static data_check_t data_check_wv(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
84 {
85   while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
86       file_recovery->calculated_file_size + 8 < file_recovery->file_size + buffer_size/2)
87   {
88     const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
89     const WavpackHeader *wv=(const WavpackHeader*)&buffer[i];
90     if(memcmp(wv, wv_header, sizeof(wv_header))==0)
91     {
92       file_recovery->calculated_file_size+=(uint64_t)8+le32(wv->ckSize);
93     }
94     else if(buffer[i]=='A' && buffer[i+1]=='P' && buffer[i+2]=='E' && buffer[i+3]=='T' && buffer[i+4]=='A' && buffer[i+5]=='G' && buffer[i+6]=='E' && buffer[i+7]=='X')
95     { /* APE Tagv2 (APE Tagv1 has no header) http://wiki.hydrogenaudio.org/index.php?title=APE_Tags_Header */
96       const unsigned int ape_tag_size = (buffer[i+12] + (buffer[i+13]<<8) + (buffer[i+14]<<16) + (buffer[i+15]<<24))+32;
97       file_recovery->calculated_file_size+=ape_tag_size;
98     }
99     else if(buffer[i]=='T' && buffer[i+1]=='A' && buffer[i+2]=='G')
100     { /* http://www.id3.org/ID3v1 TAGv1 size = 128 bytes with header "TAG" */
101       file_recovery->calculated_file_size+=128;
102     }
103     else if(file_recovery->calculated_file_size > file_recovery->file_size)
104     {
105       return DC_CONTINUE;
106     }
107     else
108     {
109       return DC_STOP;
110     }
111   }
112   return DC_CONTINUE;
113 }
114 
register_header_check_wv(file_stat_t * file_stat)115 static void register_header_check_wv(file_stat_t *file_stat)
116 {
117   register_header_check(0, wv_header,  sizeof(wv_header),  &header_check_wv, file_stat);
118 }
119