summaryrefslogtreecommitdiffstats
path: root/src/file_gz.c
blob: 0d679a908b7e3f6e214ca060d128138f41d63757 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*

    File: file_gz.c

    Copyright (C) 2006-2008 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_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#include "types.h"
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#include "filegen.h"

static void register_header_check_gz(file_stat_t *file_stat);
static int header_check_gz(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_gz= {
  .extension="gz",
  .description="gzip compressed data",
  .min_header_distance=0,
  .max_filesize=PHOTOREC_MAX_FILE_SIZE,
  .recover=1,
  .enable_by_default=1,
  .register_header_check=&register_header_check_gz
};

static const unsigned char gz_header_magic[3]= {0x1F, 0x8B, 0x08};
static const unsigned char tar_header_posix[8]  = { 'u','s','t','a','r',' ',' ',0x00};

static void register_header_check_gz(file_stat_t *file_stat)
{
  register_header_check(0, gz_header_magic,sizeof(gz_header_magic), &header_check_gz, file_stat);
}

static int header_check_gz(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)
{
  /* gzip file format:
   * a 10-byte header, containing a magic number, a version number and a timestamp
   * optional extra headers, such as the original file name,
   * a body, containing a deflate-compressed payload
   * a CRC-32 checksum
   * the length (32 bits) of the original uncompressed data
   */
  /* gzip, deflate */
  if(buffer[0]==0x1F && buffer[1]==0x8B && buffer[2]==0x08 && (buffer[3]&0xe0)==0)
  {
    unsigned int off=10;
    const unsigned int flags=buffer[3];
    int err;
    /* flags:
    bit 0   FTEXT
    bit 1   FHCRC
    bit 2   FEXTRA
    bit 3   FNAME
    bit 4   FCOMMENT
    bit 5   reserved
    bit 6   reserved
    bit 7   reserved
    */
#define GZ_FTEXT 	1
#define GZ_FHCRC	2
#define GZ_FEXTRA	4
#define GZ_FNAME	8
#define GZ_FCOMMENT     0x10
    /*
       4,5,6,7: mtime
       8: xfl/extra flags
       9: OS	3 - Unix, 7 - Macintosh, 11 - NTFS filesystem (NT)
    */
    if((flags&GZ_FEXTRA)!=0)
    {
      off+=2;
      off=+buffer[10]|(buffer[11]<<8);
    }
    if((flags&GZ_FNAME)!=0)
    {
      while(buffer[off++]!='\0');
    }
    if((flags&GZ_FCOMMENT)!=0)
    {
      while(buffer[off++]!='\0');
    }
    if((flags&GZ_FHCRC)!=0)
    {
      off+=2;
    }
#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
    if(buffer_size>off && 512>off)
    {
      const unsigned char *buffer_compr=buffer+off;
      unsigned char buffer_uncompr[512];
      const unsigned int comprLen=(buffer_size<512?buffer_size:512)-off;
      unsigned int uncomprLen=512-1;
      z_stream d_stream; /* decompression stream */
      d_stream.zalloc = (alloc_func)0;
      d_stream.zfree = (free_func)0;
      d_stream.opaque = (voidpf)0;

      d_stream.next_in  = buffer_compr;
      d_stream.avail_in = 0;
      d_stream.next_out = buffer_uncompr;

      err = inflateInit2(&d_stream, -MAX_WBITS);
      if(err!=Z_OK)
	return 0;
      while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
	d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
	err = inflate(&d_stream, Z_NO_FLUSH);
	if (err == Z_STREAM_END) break;
	if(err!=Z_OK)
	  return 0;
      }
      err = inflateEnd(&d_stream);
      if(err!=Z_OK)
	return 0;
      buffer_uncompr[d_stream.total_out]='\0';
      reset_file_recovery(file_recovery_new);
      file_recovery_new->min_filesize=22;
      file_recovery_new->time=buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);
      if(strstr((const char*)&buffer_uncompr, "<!DOCTYPE KMYMONEY-FILE>")!=NULL)
      {
	file_recovery_new->extension="kmy";
	return 1;
      }
      if(memcmp(buffer_uncompr, "PVP ", 4)==0)
      {
	/* php Video Pro */
	file_recovery_new->extension="pvp";
	return 1;
      }
      if(d_stream.total_out>0x110 &&
	memcmp(&buffer_uncompr[0x101],tar_header_posix,sizeof(tar_header_posix))==0)
      {
	file_recovery_new->extension="tar.gz";
	return 1;
      }
      file_recovery_new->extension=file_hint_gz.extension;
      return 1;
    }
#else
    reset_file_recovery(file_recovery_new);
    file_recovery_new->min_filesize=22;
    file_recovery_new->time=buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);
    file_recovery_new->extension=file_hint_gz.extension;
    return 1;
#endif
  }
  return 0;
}