Coverage Report

Created: 2025-06-13 07:22

/src/libewf/libewf/libewf_section_data_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The section data handle functions
3
 *
4
 * Copyright (C) 2006-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
#include "libewf_definitions.h"
27
#include "libewf_libbfio.h"
28
#include "libewf_libcerror.h"
29
#include "libewf_section_data_handle.h"
30
#include "libewf_unused.h"
31
32
/* Creates a section data handle
33
 * Make sure the value data_handle is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libewf_section_data_handle_initialize(
37
     libewf_section_data_handle_t **data_handle,
38
     off64_t data_offset,
39
     size64_t data_size,
40
     libcerror_error_t **error )
41
89
{
42
89
  static char *function = "libewf_section_data_handle_initialize";
43
44
89
  if( data_handle == 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 data handle.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
89
  if( *data_handle != 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 data handle value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
89
  *data_handle = memory_allocate_structure(
67
89
                  libewf_section_data_handle_t );
68
69
89
  if( *data_handle == 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 data handle.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
89
  if( memory_set(
81
89
       *data_handle,
82
89
       0,
83
89
       sizeof( libewf_section_data_handle_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 data handle.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
89
  ( *data_handle )->data_offset = data_offset;
95
89
  ( *data_handle )->data_size   = data_size;
96
97
89
  return( 1 );
98
99
0
on_error:
100
0
  if( *data_handle != NULL )
101
0
  {
102
0
    memory_free(
103
0
     *data_handle );
104
105
0
    *data_handle = NULL;
106
0
  }
107
0
  return( -1 );
108
89
}
109
110
/* Frees a section data handle
111
 * Returns 1 if successful or -1 on error
112
 */
113
int libewf_section_data_handle_free(
114
     libewf_section_data_handle_t **data_handle,
115
     libcerror_error_t **error )
116
89
{
117
89
  static char *function = "libewf_section_data_handle_free";
118
119
89
  if( data_handle == NULL )
120
0
  {
121
0
    libcerror_error_set(
122
0
     error,
123
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
124
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
125
0
     "%s: invalid data handle.",
126
0
     function );
127
128
0
    return( -1 );
129
0
  }
130
89
  if( *data_handle != NULL )
131
89
  {
132
89
    memory_free(
133
89
     *data_handle );
134
135
89
    *data_handle = NULL;
136
89
  }
137
89
  return( 1 );
138
89
}
139
140
/* Reads data from the current offset into a buffer
141
 * Callback for the data stream
142
 * Returns the number of bytes read or -1 on error
143
 */
144
ssize_t libewf_section_data_handle_read_segment_data(
145
         libewf_section_data_handle_t *data_handle,
146
         libbfio_pool_t *file_io_pool,
147
         int segment_index,
148
         int file_io_pool_entry,
149
         uint8_t *segment_data,
150
         size_t segment_data_size,
151
         uint32_t segment_flags LIBEWF_ATTRIBUTE_UNUSED,
152
         uint8_t read_flags LIBEWF_ATTRIBUTE_UNUSED,
153
         libcerror_error_t **error )
154
89
{
155
89
  static char *function = "libewf_section_data_handle_read_segment_data";
156
89
  size_t read_size      = 0;
157
89
  ssize_t read_count    = 0;
158
89
  off64_t file_offset   = 0;
159
160
89
  LIBEWF_UNREFERENCED_PARAMETER( segment_flags )
161
89
  LIBEWF_UNREFERENCED_PARAMETER( read_flags )
162
163
89
  if( data_handle == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
168
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
169
0
     "%s: invalid data handle.",
170
0
     function );
171
172
0
    return( -1 );
173
0
  }
174
89
  if( data_handle->current_offset < 0 )
175
0
  {
176
0
    libcerror_error_set(
177
0
     error,
178
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
179
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
180
0
     "%s: invalid data handle - current offset value out of bounds.",
181
0
     function );
182
183
0
    return( -1 );
184
0
  }
185
89
  if( segment_index != 0 )
186
0
  {
187
0
    libcerror_error_set(
188
0
     error,
189
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
190
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
191
0
     "%s: invalid segment index value out of bounds.",
192
0
     function );
193
194
0
    return( -1 );
195
0
  }
196
89
  if( segment_data == NULL )
197
0
  {
198
0
    libcerror_error_set(
199
0
     error,
200
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
201
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
202
0
     "%s: invalid segment data.",
203
0
     function );
204
205
0
    return( -1 );
206
0
  }
207
89
  if( segment_data_size > (size_t) SSIZE_MAX )
208
0
  {
209
0
    libcerror_error_set(
210
0
     error,
211
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
212
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
213
0
     "%s: invalid segment data size value exceeds maximum.",
214
0
     function );
215
216
0
    return( -1 );
217
0
  }
218
89
  if( data_handle->current_offset >= (off64_t) data_handle->data_size )
219
0
  {
220
0
    return( 0 );
221
0
  }
222
89
  read_size = data_handle->data_size - (size_t) data_handle->current_offset;
223
224
89
  if( read_size > segment_data_size )
225
0
  {
226
0
    read_size = segment_data_size;
227
0
  }
228
89
  file_offset = data_handle->data_offset + data_handle->current_offset;
229
230
89
  read_count = libbfio_pool_read_buffer_at_offset(
231
89
                file_io_pool,
232
89
                file_io_pool_entry,
233
89
                segment_data,
234
89
                segment_data_size,
235
89
                file_offset,
236
89
                error );
237
238
89
  if( read_count != (ssize_t) read_size )
239
9
  {
240
9
    libcerror_error_set(
241
9
     error,
242
9
     LIBCERROR_ERROR_DOMAIN_IO,
243
9
     LIBCERROR_IO_ERROR_READ_FAILED,
244
9
     "%s: unable to read section data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
245
9
     function,
246
9
     file_offset,
247
9
     file_offset );
248
249
9
    return( -1 );
250
9
  }
251
80
  data_handle->current_offset += read_size;
252
253
80
  return( (ssize_t) read_size );
254
89
}
255
256
/* Seeks a certain offset of the data
257
 * Callback for the data stream
258
 * Returns the offset if seek is successful or -1 on error
259
 */
260
off64_t libewf_section_data_handle_seek_segment_offset(
261
         libewf_section_data_handle_t *data_handle,
262
         intptr_t *file_io_handle LIBEWF_ATTRIBUTE_UNUSED,
263
         int segment_index,
264
         int segment_file_index LIBEWF_ATTRIBUTE_UNUSED,
265
         off64_t segment_offset,
266
         libcerror_error_t **error )
267
89
{
268
89
  static char *function = "libewf_section_data_handle_seek_segment_offset";
269
270
89
  LIBEWF_UNREFERENCED_PARAMETER( file_io_handle )
271
89
  LIBEWF_UNREFERENCED_PARAMETER( segment_file_index )
272
273
89
  if( data_handle == NULL )
274
0
  {
275
0
    libcerror_error_set(
276
0
     error,
277
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
278
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
279
0
     "%s: invalid data handle.",
280
0
     function );
281
282
0
    return( -1 );
283
0
  }
284
89
  if( segment_index != 0 )
285
0
  {
286
0
    libcerror_error_set(
287
0
     error,
288
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
289
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
290
0
     "%s: invalid segment index value out of bounds.",
291
0
     function );
292
293
0
    return( -1 );
294
0
  }
295
89
  if( segment_offset < 0 )
296
0
  {
297
0
    libcerror_error_set(
298
0
     error,
299
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
300
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
301
0
     "%s: invalid segment offset value out of bounds.",
302
0
     function );
303
304
0
    return( -1 );
305
0
  }
306
89
  data_handle->current_offset = segment_offset;
307
308
89
  return( segment_offset );
309
89
}
310