Coverage Report

Created: 2024-05-21 07:07

/src/libfsntfs/libfsntfs/libfsntfs_compression.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Compression functions
3
 *
4
 * Copyright (C) 2010-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 <memory.h>
24
#include <types.h>
25
26
#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
27
#include <stdlib.h>
28
#endif
29
30
#include "libfsntfs_compression.h"
31
#include "libfsntfs_definitions.h"
32
#include "libfsntfs_libcerror.h"
33
#include "libfsntfs_libfwnt.h"
34
35
/* Decompresses data using the compression method
36
 * Returns 1 on success, 0 on failure or -1 on error
37
 */
38
int libfsntfs_decompress_data(
39
     const uint8_t *compressed_data,
40
     size_t compressed_data_size,
41
     int compression_method,
42
     uint8_t *uncompressed_data,
43
     size_t *uncompressed_data_size,
44
     libcerror_error_t **error )
45
334
{
46
334
  static char *function = "libfsntfs_decompress_data";
47
48
334
  if( compressed_data == NULL )
49
0
  {
50
0
    libcerror_error_set(
51
0
     error,
52
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
53
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
54
0
     "%s: invalid compressed data buffer.",
55
0
     function );
56
57
0
    return( -1 );
58
0
  }
59
334
  if( uncompressed_data == NULL )
60
0
  {
61
0
    libcerror_error_set(
62
0
     error,
63
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
64
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
65
0
     "%s: invalid uncompressed data buffer.",
66
0
     function );
67
68
0
    return( -1 );
69
0
  }
70
334
  if( uncompressed_data == compressed_data )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
75
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
76
0
     "%s: invalid compressed data buffer equals uncompressed data buffer.",
77
0
     function );
78
79
0
    return( -1 );
80
0
  }
81
334
  if( uncompressed_data_size == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
86
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
87
0
     "%s: invalid uncompressed data size.",
88
0
     function );
89
90
0
    return( -1 );
91
0
  }
92
334
  switch( compression_method )
93
334
  {
94
334
    case LIBFSNTFS_COMPRESSION_METHOD_LZNT1:
95
334
      if( libfwnt_lznt1_decompress(
96
334
           compressed_data,
97
334
           compressed_data_size,
98
334
           uncompressed_data,
99
334
           uncompressed_data_size,
100
334
           error ) != 1 )
101
82
      {
102
82
        libcerror_error_set(
103
82
         error,
104
82
         LIBCERROR_ERROR_DOMAIN_COMPRESSION,
105
82
         LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED,
106
82
         "%s: unable to decompress LZNT1 compressed data.",
107
82
         function );
108
109
82
        return( -1 );
110
82
      }
111
252
      break;
112
113
252
    case LIBFSNTFS_COMPRESSION_METHOD_LZX:
114
0
      if( libfwnt_lzx_decompress(
115
0
           compressed_data,
116
0
           compressed_data_size,
117
0
           uncompressed_data,
118
0
           uncompressed_data_size,
119
0
           error ) != 1 )
120
0
      {
121
0
        libcerror_error_set(
122
0
         error,
123
0
         LIBCERROR_ERROR_DOMAIN_COMPRESSION,
124
0
         LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED,
125
0
         "%s: unable to decompress LZX compressed data.",
126
0
         function );
127
128
0
        return( -1 );
129
0
      }
130
0
      break;
131
132
0
    case LIBFSNTFS_COMPRESSION_METHOD_LZXPRESS_HUFFMAN:
133
0
      if( libfwnt_lzxpress_huffman_decompress(
134
0
           compressed_data,
135
0
           compressed_data_size,
136
0
           uncompressed_data,
137
0
           uncompressed_data_size,
138
0
           error ) != 1 )
139
0
      {
140
0
        libcerror_error_set(
141
0
         error,
142
0
         LIBCERROR_ERROR_DOMAIN_COMPRESSION,
143
0
         LIBCERROR_COMPRESSION_ERROR_DECOMPRESS_FAILED,
144
0
         "%s: unable to decompress LZXPRESS Huffman compressed data.",
145
0
         function );
146
147
0
        return( -1 );
148
0
      }
149
0
      break;
150
151
0
    default:
152
0
      libcerror_error_set(
153
0
       error,
154
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
155
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
156
0
       "%s: unsupported compression method.",
157
0
       function );
158
159
0
      return( -1 );
160
334
  }
161
252
  return( 1 );
162
334
}
163