Coverage Report

Created: 2024-02-25 07:20

/src/libpff/libpff/libpff_compression.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Compression handling functions
3
 *
4
 * Copyright (C) 2008-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <types.h>
24
25
#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
26
#include <stdlib.h>
27
#endif
28
29
#if defined( HAVE_ZLIB ) || defined( ZLIB_DLL )
30
#include <zlib.h>
31
#endif
32
33
#include "libpff_compression.h"
34
#include "libpff_definitions.h"
35
#include "libpff_deflate.h"
36
#include "libpff_libcerror.h"
37
#include "libpff_libcnotify.h"
38
39
/* Decompresses data using the compression method
40
 * Returns 1 on success, 0 on failure or -1 on error
41
 */
42
int libpff_decompress_data(
43
     const uint8_t *compressed_data,
44
     size_t compressed_data_size,
45
     uint16_t compression_method,
46
     uint8_t *uncompressed_data,
47
     size_t *uncompressed_data_size,
48
     libcerror_error_t **error )
49
0
{
50
0
  static char *function              = "libpff_decompress_data";
51
0
  int result                         = 0;
52
53
#if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL )
54
  uLongf zlib_uncompressed_data_size = 0;
55
#endif
56
57
0
  if( compressed_data == NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
62
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
63
0
     "%s: invalid compressed data buffer.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  if( uncompressed_data == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
73
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
74
0
     "%s: invalid uncompressed data buffer.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
0
  if( uncompressed_data == compressed_data )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
85
0
     "%s: invalid compressed data buffer equals uncompressed data buffer.",
86
0
     function );
87
88
0
    return( -1 );
89
0
  }
90
0
  if( uncompressed_data_size == NULL )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
95
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
96
0
     "%s: invalid uncompressed data size.",
97
0
     function );
98
99
0
    return( -1 );
100
0
  }
101
0
  if( compression_method == LIBPFF_COMPRESSION_METHOD_DEFLATE )
102
0
  {
103
#if ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL )
104
    if( compressed_data_size > (size_t) ULONG_MAX )
105
    {
106
      libcerror_error_set(
107
       error,
108
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
109
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
110
       "%s: invalid compressed data size value exceeds maximum.",
111
       function );
112
113
      return( -1 );
114
    }
115
    if( *uncompressed_data_size > (size_t) ULONG_MAX )
116
    {
117
      libcerror_error_set(
118
       error,
119
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
121
       "%s: invalid uncompressed data size value exceeds maximum.",
122
       function );
123
124
      return( -1 );
125
    }
126
    zlib_uncompressed_data_size = (uLongf) *uncompressed_data_size;
127
128
    result = uncompress(
129
        (Bytef *) uncompressed_data,
130
        &zlib_uncompressed_data_size,
131
        (Bytef *) compressed_data,
132
        (uLong) compressed_data_size );
133
134
    if( result == Z_OK )
135
    {
136
      *uncompressed_data_size = (size_t) zlib_uncompressed_data_size;
137
138
      result = 1;
139
    }
140
    else if( result == Z_DATA_ERROR )
141
    {
142
#if defined( HAVE_DEBUG_OUTPUT )
143
      if( libcnotify_verbose != 0 )
144
      {
145
        libcnotify_printf(
146
         "%s: unable to read compressed data: data error.\n",
147
         function );
148
      }
149
#endif
150
      *uncompressed_data_size = 0;
151
152
      result = -1;
153
    }
154
    else if( result == Z_BUF_ERROR )
155
    {
156
#if defined( HAVE_DEBUG_OUTPUT )
157
      if( libcnotify_verbose != 0 )
158
      {
159
        libcnotify_printf(
160
        "%s: unable to read compressed data: target buffer too small.\n",
161
         function );
162
      }
163
#endif
164
      /* Estimate that a factor 2 enlargement should suffice
165
       */
166
      *uncompressed_data_size *= 2;
167
168
      result = 0;
169
    }
170
    else if( result == Z_MEM_ERROR )
171
    {
172
      libcerror_error_set(
173
       error,
174
       LIBCERROR_ERROR_DOMAIN_MEMORY,
175
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
176
       "%s: unable to read compressed data: insufficient memory.",
177
       function );
178
179
      *uncompressed_data_size = 0;
180
181
      result = -1;
182
    }
183
    else
184
    {
185
      libcerror_error_set(
186
       error,
187
       LIBCERROR_ERROR_DOMAIN_COMPRESSION,
188
       LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED,
189
       "%s: zlib returned undefined error: %d.",
190
       function,
191
       result );
192
193
      *uncompressed_data_size = 0;
194
195
      result = -1;
196
    }
197
#else
198
0
    result = libpff_deflate_decompress_zlib(
199
0
              compressed_data,
200
0
              compressed_data_size,
201
0
              uncompressed_data,
202
0
              uncompressed_data_size,
203
0
              error );
204
205
0
    if( result != 1 )
206
0
    {
207
0
      libcerror_error_set(
208
0
       error,
209
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
210
0
       LIBCERROR_ENCRYPTION_ERROR_GENERIC,
211
0
       "%s: unable to decompress deflate compressed data.",
212
0
       function );
213
214
0
      return( -1 );
215
0
    }
216
0
#endif /* ( defined( HAVE_ZLIB ) && defined( HAVE_ZLIB_UNCOMPRESS ) ) || defined( ZLIB_DLL ) */
217
0
  }
218
0
  else
219
0
  {
220
0
    libcerror_error_set(
221
0
     error,
222
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
223
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
224
0
     "%s: unsupported compression method.",
225
0
     function );
226
227
0
    return( -1 );
228
0
  }
229
0
  return( result );
230
0
}
231