Coverage Report

Created: 2024-02-25 07:20

/src/libagdb/libagdb/libagdb_compressed_blocks_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The compressed blocks stream functions
3
 *
4
 * Copyright (C) 2014-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
#include "libagdb_compressed_blocks_stream.h"
26
#include "libagdb_compressed_blocks_stream_data_handle.h"
27
#include "libagdb_libcerror.h"
28
#include "libagdb_libfcache.h"
29
#include "libagdb_libfdata.h"
30
31
/* Creates a compressed block stream
32
 * Make sure the value compressed_blocks_stream is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libagdb_compressed_blocks_stream_initialize(
36
     libfdata_stream_t **compressed_blocks_stream,
37
     libfdata_list_t *compressed_blocks_list,
38
     libfcache_cache_t *compressed_blocks_cache,
39
     libcerror_error_t **error )
40
1.98k
{
41
1.98k
  libagdb_compressed_blocks_stream_data_handle_t *data_handle = NULL;
42
1.98k
  static char *function                                       = "libagdb_compressed_blocks_stream_initialize";
43
1.98k
  off64_t segment_offset                                      = 0;
44
1.98k
  size64_t segment_size                                       = 0;
45
1.98k
  int element_index                                           = 0;
46
1.98k
  int number_of_elements                                      = 0;
47
1.98k
  int segment_index                                           = 0;
48
49
1.98k
  if( compressed_blocks_stream == NULL )
50
0
  {
51
0
    libcerror_error_set(
52
0
     error,
53
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
54
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
55
0
     "%s: invalid compressed block stream.",
56
0
     function );
57
58
0
    return( -1 );
59
0
  }
60
1.98k
  if( compressed_blocks_list == NULL )
61
0
  {
62
0
    libcerror_error_set(
63
0
     error,
64
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
65
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
66
0
     "%s: invalid compressed blocks list.",
67
0
     function );
68
69
0
    return( -1 );
70
0
  }
71
1.98k
  if( libagdb_compressed_blocks_stream_data_handle_initialize(
72
1.98k
       &data_handle,
73
1.98k
       error ) != 1 )
74
0
  {
75
0
    libcerror_error_set(
76
0
     error,
77
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
78
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
79
0
     "%s: unable to create data handle.",
80
0
     function );
81
82
0
    goto on_error;
83
0
  }
84
1.98k
  if( data_handle == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
89
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
90
0
     "%s: missing data handle.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
1.98k
  data_handle->compressed_blocks_list  = compressed_blocks_list;
96
1.98k
  data_handle->compressed_blocks_cache = compressed_blocks_cache;
97
98
1.98k
  if( libfdata_stream_initialize(
99
1.98k
       compressed_blocks_stream,
100
1.98k
       (intptr_t *) data_handle,
101
1.98k
       (int (*)(intptr_t **, libcerror_error_t **)) &libagdb_compressed_blocks_stream_data_handle_free,
102
1.98k
       NULL,
103
1.98k
       NULL,
104
1.98k
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libagdb_compressed_blocks_stream_data_handle_read_segment_data,
105
1.98k
       NULL,
106
1.98k
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libagdb_compressed_blocks_stream_data_handle_seek_segment_offset,
107
1.98k
       LIBFDATA_DATA_HANDLE_FLAG_MANAGED,
108
1.98k
       error ) != 1 )
109
0
  {
110
0
    libcerror_error_set(
111
0
     error,
112
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
113
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
114
0
     "%s: unable to create stream.",
115
0
     function );
116
117
0
    goto on_error;
118
0
  }
119
1.98k
  if( libfdata_list_get_number_of_elements(
120
1.98k
       compressed_blocks_list,
121
1.98k
       &number_of_elements,
122
1.98k
       error ) != 1 )
123
0
  {
124
0
    libcerror_error_set(
125
0
     error,
126
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
127
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
128
0
     "%s: unable to retrieve number of compressed blocks list elements.",
129
0
     function );
130
131
0
    data_handle = NULL;
132
133
0
    goto on_error;
134
0
  }
135
1.98k
  for( element_index = 0;
136
1.04M
       element_index < number_of_elements;
137
1.04M
       element_index++ )
138
1.04M
  {
139
1.04M
    if( libfdata_list_get_mapped_size_by_index(
140
1.04M
         compressed_blocks_list,
141
1.04M
         element_index,
142
1.04M
         &segment_size,
143
1.04M
         error ) != 1 )
144
0
    {
145
0
      libcerror_error_set(
146
0
       error,
147
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
148
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
149
0
       "%s: unable to retrieve compressed blocks list element: %d mapped size.",
150
0
       function,
151
0
       element_index );
152
153
0
      data_handle = NULL;
154
155
0
      goto on_error;
156
0
    }
157
1.04M
    if( libfdata_stream_append_segment(
158
1.04M
         *compressed_blocks_stream,
159
1.04M
         &segment_index,
160
1.04M
         0,
161
1.04M
         segment_offset,
162
1.04M
         segment_size,
163
1.04M
         0,
164
1.04M
         error ) != 1 )
165
0
    {
166
0
      libcerror_error_set(
167
0
       error,
168
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
169
0
       LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
170
0
       "%s: unable to append compressed blocks stream segment: %d.",
171
0
       function,
172
0
       element_index );
173
174
0
      data_handle = NULL;
175
176
0
      goto on_error;
177
0
    }
178
1.04M
    segment_offset += segment_size;
179
1.04M
  }
180
1.98k
  return( 1 );
181
182
0
on_error:
183
0
  if( *compressed_blocks_stream != NULL )
184
0
  {
185
0
    libfdata_stream_free(
186
0
     compressed_blocks_stream,
187
0
     NULL );
188
0
  }
189
0
  if( data_handle != NULL )
190
0
  {
191
0
    libagdb_compressed_blocks_stream_data_handle_free(
192
0
     &data_handle,
193
0
     NULL );
194
0
  }
195
0
  return( -1 );
196
1.98k
}
197