Coverage Report

Created: 2026-01-20 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libagdb/libagdb/libagdb_compressed_file_header.c
Line
Count
Source
1
/*
2
 * Compressed file header functions
3
 *
4
 * Copyright (C) 2014-2025, 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libagdb_compressed_file_header.h"
28
#include "libagdb_definitions.h"
29
#include "libagdb_io_handle.h"
30
#include "libagdb_libbfio.h"
31
#include "libagdb_libcerror.h"
32
#include "libagdb_libcnotify.h"
33
34
/* Creates compressed file header
35
 * Make sure the value compressed_file_header is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libagdb_compressed_file_header_initialize(
39
     libagdb_compressed_file_header_t **compressed_file_header,
40
     libcerror_error_t **error )
41
2.18k
{
42
2.18k
  static char *function = "libagdb_compressed_file_header_initialize";
43
44
2.18k
  if( compressed_file_header == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid compressed file header.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
2.18k
  if( *compressed_file_header != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid compressed file header value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
2.18k
  *compressed_file_header = memory_allocate_structure(
67
2.18k
                             libagdb_compressed_file_header_t );
68
69
2.18k
  if( *compressed_file_header == NULL )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
74
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75
0
     "%s: unable to create compressed file header.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
2.18k
  if( memory_set(
81
2.18k
       *compressed_file_header,
82
2.18k
       0,
83
2.18k
       sizeof( libagdb_compressed_file_header_t ) ) == NULL )
84
0
  {
85
0
    libcerror_error_set(
86
0
     error,
87
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
88
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
89
0
     "%s: unable to clear compressed file header.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
2.18k
  return( 1 );
95
96
0
on_error:
97
0
  if( *compressed_file_header != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *compressed_file_header );
101
102
0
    *compressed_file_header = NULL;
103
0
  }
104
0
  return( -1 );
105
2.18k
}
106
107
/* Frees compressed file header
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libagdb_compressed_file_header_free(
111
     libagdb_compressed_file_header_t **compressed_file_header,
112
     libcerror_error_t **error )
113
2.18k
{
114
2.18k
  static char *function = "libagdb_compressed_file_header_free";
115
116
2.18k
  if( compressed_file_header == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid compressed file header.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
2.18k
  if( *compressed_file_header != NULL )
128
2.18k
  {
129
2.18k
    memory_free(
130
2.18k
     *compressed_file_header );
131
132
2.18k
    *compressed_file_header = NULL;
133
2.18k
  }
134
2.18k
  return( 1 );
135
2.18k
}
136
137
/* Reads the compressed file header data
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libagdb_compressed_file_header_read_data(
141
     libagdb_compressed_file_header_t *compressed_file_header,
142
     const uint8_t *data,
143
     size_t data_size,
144
     libcerror_error_t **error )
145
2.18k
{
146
2.18k
  static char *function = "libagdb_compressed_file_header_read_data";
147
2.18k
  uint32_t value_32bit  = 0;
148
149
2.18k
  if( compressed_file_header == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
155
0
     "%s: invalid compressed file header.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
2.18k
  if( data == NULL )
161
0
  {
162
0
    libcerror_error_set(
163
0
     error,
164
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
165
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
166
0
     "%s: invalid data.",
167
0
     function );
168
169
0
    return( -1 );
170
0
  }
171
2.18k
  if( ( data_size < 8 )
172
2.18k
   || ( data_size > (size_t) SSIZE_MAX ) )
173
0
  {
174
0
    libcerror_error_set(
175
0
     error,
176
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
177
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
178
0
     "%s: invalid data size value out of bounds.",
179
0
     function );
180
181
0
    return( -1 );
182
0
  }
183
#if defined( HAVE_DEBUG_OUTPUT )
184
  if( libcnotify_verbose != 0 )
185
  {
186
    libcnotify_printf(
187
     "%s: compressed file header data:\n",
188
     function );
189
    libcnotify_print_data(
190
     data,
191
     8,
192
     0 );
193
  }
194
#endif
195
2.18k
  if( memory_compare(
196
2.18k
       data,
197
2.18k
       agdb_mem_file_signature_vista,
198
2.18k
       4 ) == 0 )
199
1.46k
  {
200
1.46k
    compressed_file_header->file_type               = LIBAGDB_FILE_TYPE_COMPRESSED_VISTA;
201
1.46k
    compressed_file_header->uncompressed_block_size = 4096;
202
1.46k
  }
203
718
  else if( memory_compare(
204
718
            data,
205
718
            agdb_mem_file_signature_win7,
206
718
            4 ) == 0 )
207
448
  {
208
448
    compressed_file_header->file_type               = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7;
209
448
    compressed_file_header->uncompressed_block_size = 65536;
210
448
  }
211
270
  else if( memory_compare(
212
270
            data,
213
270
            agdb_mam_file_signature_win8,
214
270
            4 ) == 0 )
215
3
  {
216
3
    compressed_file_header->file_type               = LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8;
217
3
    compressed_file_header->uncompressed_block_size = 65536;
218
3
  }
219
267
  else
220
267
  {
221
267
    compressed_file_header->file_type = LIBAGDB_FILE_TYPE_UNCOMPRESSED;
222
267
  }
223
#if defined( HAVE_DEBUG_OUTPUT )
224
  if( libcnotify_verbose != 0 )
225
  {
226
    if( ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_VISTA )
227
     || ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7 ) )
228
    {
229
      libcnotify_printf(
230
       "%s: signature\t\t\t: %c%c%c%c\n",
231
       function,
232
       data[ 0 ],
233
       data[ 1 ],
234
       data[ 2 ],
235
       data[ 3 ] );
236
    }
237
    else if( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS8 )
238
    {
239
      libcnotify_printf(
240
       "%s: signature\t\t\t: %c%c%c\\x%02x\n",
241
       function,
242
       data[ 0 ],
243
       data[ 1 ],
244
       data[ 2 ],
245
       data[ 3 ] );
246
    }
247
  }
248
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
249
250
2.18k
  if( ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_UNCOMPRESSED )
251
1.91k
   || ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_VISTA )
252
451
   || ( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_COMPRESSED_WINDOWS7 ) )
253
2.17k
  {
254
2.17k
    byte_stream_copy_to_uint32_little_endian(
255
2.17k
     &( data[ 0 ] ),
256
2.17k
     value_32bit );
257
258
2.17k
    byte_stream_copy_to_uint32_little_endian(
259
2.17k
     &( data[ 4 ] ),
260
2.17k
     compressed_file_header->uncompressed_data_size );
261
262
2.17k
    if( compressed_file_header->file_type == LIBAGDB_FILE_TYPE_UNCOMPRESSED )
263
267
    {
264
/* TODO improve detection */
265
267
      if( ( ( value_32bit != 0x00000005UL )
266
249
        &&  ( value_32bit != 0x0000000eUL )
267
63
        &&  ( value_32bit != 0x0000000fUL ) )
268
214
       || ( compressed_file_header->file_size != (size64_t) compressed_file_header->uncompressed_data_size ) )
269
104
      {
270
104
        libcerror_error_set(
271
104
         error,
272
104
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
273
104
         LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
274
104
         "%s: invalid signature.",
275
104
         function );
276
277
104
        return( -1 );
278
104
      }
279
267
    }
280
#if defined( HAVE_DEBUG_OUTPUT )
281
    if( libcnotify_verbose != 0 )
282
    {
283
      libcnotify_printf(
284
       "%s: uncompressed data size\t: %" PRIu32 "\n",
285
       function,
286
       compressed_file_header->uncompressed_data_size );
287
    }
288
#endif
289
2.17k
  }
290
#if defined( HAVE_DEBUG_OUTPUT )
291
  if( libcnotify_verbose != 0 )
292
  {
293
    libcnotify_printf(
294
     "\n" );
295
  }
296
#endif
297
2.07k
  return( 1 );
298
2.18k
}
299
300
/* Reads the compressed file header data
301
 * Returns 1 if successful or -1 on error
302
 */
303
int libagdb_compressed_file_header_read_file_io_handle(
304
     libagdb_compressed_file_header_t *compressed_file_header,
305
     libbfio_handle_t *file_io_handle,
306
     libcerror_error_t **error )
307
2.18k
{
308
2.18k
  uint8_t compressed_file_header_data[ 8 ];
309
310
2.18k
  static char *function = "libagdb_compressed_file_header_read_file_io_handle";
311
2.18k
  ssize_t read_count    = 0;
312
313
2.18k
  if( compressed_file_header == NULL )
314
0
  {
315
0
    libcerror_error_set(
316
0
     error,
317
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
318
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
319
0
     "%s: invalid compressed file header.",
320
0
     function );
321
322
0
    return( -1 );
323
0
  }
324
2.18k
  if( libbfio_handle_get_size(
325
2.18k
       file_io_handle,
326
2.18k
       &( compressed_file_header->file_size ),
327
2.18k
       error ) != 1 )
328
0
  {
329
0
    libcerror_error_set(
330
0
     error,
331
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
332
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
333
0
     "%s: unable to retrieve file size.",
334
0
     function );
335
336
0
    return( -1 );
337
0
  }
338
#if defined( HAVE_DEBUG_OUTPUT )
339
  if( libcnotify_verbose != 0 )
340
  {
341
    libcnotify_printf(
342
     "%s: reading compressed file header at offset: 0 (0x00000000)\n",
343
     function );
344
  }
345
#endif
346
2.18k
  read_count = libbfio_handle_read_buffer_at_offset(
347
2.18k
                file_io_handle,
348
2.18k
                compressed_file_header_data,
349
2.18k
                8,
350
2.18k
                0,
351
2.18k
                error );
352
353
2.18k
  if( read_count != (ssize_t) 8 )
354
4
  {
355
4
    libcerror_error_set(
356
4
     error,
357
4
     LIBCERROR_ERROR_DOMAIN_IO,
358
4
     LIBCERROR_IO_ERROR_READ_FAILED,
359
4
     "%s: unable to read compressed file header data at offset: 0 (0x00000000).",
360
4
     function );
361
362
4
    return( -1 );
363
4
  }
364
/* TODO pass file_size as an argument ? */
365
2.18k
  if( libagdb_compressed_file_header_read_data(
366
2.18k
       compressed_file_header,
367
2.18k
       compressed_file_header_data,
368
2.18k
       8,
369
2.18k
       error ) != 1 )
370
104
  {
371
104
    libcerror_error_set(
372
104
     error,
373
104
     LIBCERROR_ERROR_DOMAIN_IO,
374
104
     LIBCERROR_IO_ERROR_READ_FAILED,
375
104
     "%s: unable to read compressed file header.",
376
104
     function );
377
378
104
    return( -1 );
379
104
  }
380
2.07k
  return( 1 );
381
2.18k
}
382