Coverage Report

Created: 2025-07-04 07:01

/src/libfsext/libfsext/libfsext_block_data_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Block data handle 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
#include "libfsext_block_data_handle.h"
27
#include "libfsext_libbfio.h"
28
#include "libfsext_libcerror.h"
29
#include "libfsext_libfdata.h"
30
#include "libfsext_unused.h"
31
32
/* Reads data from the current offset into a buffer
33
 * Callback for the cluster block data stream
34
 * Returns the number of bytes read or -1 on error
35
 */
36
ssize_t libfsext_block_data_handle_read_segment_data(
37
         intptr_t *data_handle LIBFSEXT_ATTRIBUTE_UNUSED,
38
         libbfio_handle_t *file_io_handle,
39
         int segment_index,
40
         int segment_file_index LIBFSEXT_ATTRIBUTE_UNUSED,
41
         uint8_t *segment_data,
42
         size_t segment_data_size,
43
         uint32_t segment_flags,
44
         uint8_t read_flags LIBFSEXT_ATTRIBUTE_UNUSED,
45
         libcerror_error_t **error )
46
1.55k
{
47
1.55k
  static char *function = "libfsext_block_data_handle_read_segment_data";
48
1.55k
  ssize_t read_count    = 0;
49
50
1.55k
  LIBFSEXT_UNREFERENCED_PARAMETER( data_handle )
51
1.55k
  LIBFSEXT_UNREFERENCED_PARAMETER( segment_file_index )
52
1.55k
  LIBFSEXT_UNREFERENCED_PARAMETER( read_flags )
53
54
1.55k
  if( segment_index < 0 )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
60
0
     "%s: invalid segment index value out of bounds.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
1.55k
  if( segment_data == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
71
0
     "%s: invalid segment data.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
1.55k
  if( segment_data_size > (size_t) SSIZE_MAX )
77
0
  {
78
0
    libcerror_error_set(
79
0
     error,
80
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
81
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
82
0
     "%s: invalid segment data size value exceeds maximum.",
83
0
     function );
84
85
0
    return( -1 );
86
0
  }
87
1.55k
  if( ( segment_flags & LIBFDATA_RANGE_FLAG_IS_SPARSE ) != 0 )
88
487
  {
89
487
    if( memory_set(
90
487
         segment_data,
91
487
         0,
92
487
         segment_data_size ) == NULL )
93
0
    {
94
0
      libcerror_error_set(
95
0
       error,
96
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
97
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
98
0
       "%s: unable to clear segment data.",
99
0
       function );
100
101
0
      return( -1 );
102
0
    }
103
487
    read_count = (ssize_t) segment_data_size;
104
487
  }
105
1.06k
  else
106
1.06k
  {
107
1.06k
    read_count = libbfio_handle_read_buffer(
108
1.06k
                  file_io_handle,
109
1.06k
                  segment_data,
110
1.06k
                  segment_data_size,
111
1.06k
                  error );
112
113
1.06k
    if( read_count < 0 )
114
162
    {
115
162
      libcerror_error_set(
116
162
       error,
117
162
       LIBCERROR_ERROR_DOMAIN_IO,
118
162
       LIBCERROR_IO_ERROR_READ_FAILED,
119
162
       "%s: unable to read segment data.",
120
162
       function );
121
122
162
      return( -1 );
123
162
    }
124
1.06k
  }
125
1.39k
  return( read_count );
126
1.55k
}
127
128
/* Seeks a certain offset of the data
129
 * Callback for the cluster block data stream
130
 * Returns the offset if seek is successful or -1 on error
131
 */
132
off64_t libfsext_block_data_handle_seek_segment_offset(
133
         intptr_t *data_handle LIBFSEXT_ATTRIBUTE_UNUSED,
134
         libbfio_handle_t *file_io_handle,
135
         int segment_index,
136
         int segment_file_index LIBFSEXT_ATTRIBUTE_UNUSED,
137
         off64_t segment_offset,
138
         libcerror_error_t **error )
139
1.55k
{
140
1.55k
  static char *function = "libfsext_block_data_handle_seek_segment_offset";
141
142
1.55k
  LIBFSEXT_UNREFERENCED_PARAMETER( data_handle )
143
1.55k
  LIBFSEXT_UNREFERENCED_PARAMETER( segment_file_index )
144
145
1.55k
  if( segment_index < 0 )
146
0
  {
147
0
    libcerror_error_set(
148
0
     error,
149
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
151
0
     "%s: invalid segment index value out of bounds.",
152
0
     function );
153
154
0
    return( -1 );
155
0
  }
156
1.55k
  if( segment_offset < 0 )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
162
0
     "%s: invalid segment offset value out of bounds.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
1.55k
  if( libbfio_handle_seek_offset(
168
1.55k
       file_io_handle,
169
1.55k
       segment_offset,
170
1.55k
       SEEK_SET,
171
1.55k
       error ) == -1 )
172
0
  {
173
0
    libcerror_error_set(
174
0
     error,
175
0
     LIBCERROR_ERROR_DOMAIN_IO,
176
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
177
0
     "%s: unable to seek segment offset: %" PRIi64 " (0x%08" PRIx64 ").",
178
0
     function,
179
0
     segment_offset,
180
0
     segment_offset );
181
182
0
    return( -1 );
183
0
  }
184
1.55k
  return( segment_offset );
185
1.55k
}
186