Coverage Report

Created: 2024-02-25 07:20

/src/libfsntfs/libfdata/libfdata_mapped_range.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The (data) mapped range 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 "libfdata_libcerror.h"
27
#include "libfdata_mapped_range.h"
28
29
/* Creates a mapped range
30
 * Make sure the value mapped_range is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libfdata_mapped_range_initialize(
34
     libfdata_mapped_range_t **mapped_range,
35
     libcerror_error_t **error )
36
9.61M
{
37
9.61M
  static char *function = "libfdata_mapped_range_initialize";
38
39
9.61M
  if( mapped_range == NULL )
40
0
  {
41
0
    libcerror_error_set(
42
0
     error,
43
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
44
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
45
0
     "%s: invalid mapped range.",
46
0
     function );
47
48
0
    return( -1 );
49
0
  }
50
9.61M
  if( *mapped_range != NULL )
51
0
  {
52
0
    libcerror_error_set(
53
0
     error,
54
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
55
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
56
0
     "%s: invalid mapped range value already set.",
57
0
     function );
58
59
0
    return( -1 );
60
0
  }
61
9.61M
  *mapped_range = memory_allocate_structure(
62
9.61M
                   libfdata_mapped_range_t );
63
64
9.61M
  if( *mapped_range == NULL )
65
0
  {
66
0
    libcerror_error_set(
67
0
     error,
68
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
69
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
70
0
     "%s: unable to create mapped range.",
71
0
     function );
72
73
0
    goto on_error;
74
0
  }
75
9.61M
  if( memory_set(
76
9.61M
       *mapped_range,
77
9.61M
       0,
78
9.61M
       sizeof( libfdata_mapped_range_t ) ) == NULL )
79
0
  {
80
0
    libcerror_error_set(
81
0
     error,
82
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
83
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
84
0
     "%s: unable to clear mapped range.",
85
0
     function );
86
87
0
    goto on_error;
88
0
  }
89
9.61M
  ( *mapped_range )->offset = (off64_t) -1;
90
91
9.61M
  return( 1 );
92
93
0
on_error:
94
0
  if( *mapped_range != NULL )
95
0
  {
96
0
    memory_free(
97
0
     *mapped_range );
98
99
0
    *mapped_range = NULL;
100
0
  }
101
0
  return( -1 );
102
9.61M
}
103
104
/* Frees a mapped range
105
 * Returns 1 if successful or -1 on error
106
 */
107
int libfdata_mapped_range_free(
108
     libfdata_mapped_range_t **mapped_range,
109
     libcerror_error_t **error )
110
9.61M
{
111
9.61M
  static char *function = "libfdata_mapped_range_free";
112
113
9.61M
  if( mapped_range == NULL )
114
0
  {
115
0
    libcerror_error_set(
116
0
     error,
117
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119
0
     "%s: invalid mapped range.",
120
0
     function );
121
122
0
    return( -1 );
123
0
  }
124
9.61M
  if( *mapped_range != NULL )
125
9.61M
  {
126
9.61M
    memory_free(
127
9.61M
     *mapped_range );
128
129
9.61M
    *mapped_range = NULL;
130
9.61M
  }
131
9.61M
  return( 1 );
132
9.61M
}
133
134
/* Clones (duplicates) the mapped range
135
 * Returns 1 if successful or -1 on error
136
 */
137
int libfdata_mapped_range_clone(
138
     libfdata_mapped_range_t **destination_mapped_range,
139
     libfdata_mapped_range_t *source_mapped_range,
140
     libcerror_error_t **error )
141
0
{
142
0
  static char *function = "libfdata_mapped_range_clone";
143
144
0
  if( destination_mapped_range == NULL )
145
0
  {
146
0
    libcerror_error_set(
147
0
     error,
148
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
149
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
150
0
     "%s: invalid destination mapped range.",
151
0
     function );
152
153
0
    return( -1 );
154
0
  }
155
0
  if( *destination_mapped_range != NULL )
156
0
  {
157
0
    libcerror_error_set(
158
0
     error,
159
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
160
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
161
0
     "%s: invalid destination mapped range value already set.",
162
0
     function );
163
164
0
    return( -1 );
165
0
  }
166
0
  if( source_mapped_range == NULL )
167
0
  {
168
0
    *destination_mapped_range = NULL;
169
170
0
    return( 1 );
171
0
  }
172
0
  *destination_mapped_range = memory_allocate_structure(
173
0
                               libfdata_mapped_range_t );
174
175
0
  if( *destination_mapped_range == NULL )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
180
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
181
0
     "%s: unable to create destination mapped range.",
182
0
     function );
183
184
0
    goto on_error;
185
0
  }
186
0
  if( memory_copy(
187
0
       *destination_mapped_range,
188
0
       source_mapped_range,
189
0
       sizeof( libfdata_mapped_range_t ) ) == NULL )
190
0
  {
191
0
    libcerror_error_set(
192
0
     error,
193
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
194
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
195
0
     "%s: unable to copy source to destination mapped range.",
196
0
     function );
197
198
0
    goto on_error;
199
0
  }
200
0
  return( 1 );
201
202
0
on_error:
203
0
  if( *destination_mapped_range != NULL )
204
0
  {
205
0
    memory_free(
206
0
     *destination_mapped_range );
207
208
0
    *destination_mapped_range = NULL;
209
0
  }
210
0
  return( -1 );
211
0
}
212
213
/* Retrieves the mapped range values
214
 * Returns 1 if successful or -1 on error
215
 */
216
int libfdata_mapped_range_get(
217
     libfdata_mapped_range_t *mapped_range,
218
     off64_t *offset,
219
     size64_t *size,
220
     libcerror_error_t **error )
221
2.18M
{
222
2.18M
  static char *function = "libfdata_mapped_range_get";
223
224
2.18M
  if( mapped_range == NULL )
225
0
  {
226
0
    libcerror_error_set(
227
0
     error,
228
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
230
0
     "%s: invalid mapped range.",
231
0
     function );
232
233
0
    return( -1 );
234
0
  }
235
2.18M
  if( offset == NULL )
236
0
  {
237
0
    libcerror_error_set(
238
0
     error,
239
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
240
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
241
0
     "%s: invalid offset.",
242
0
     function );
243
244
0
    return( -1 );
245
0
  }
246
2.18M
  if( size == NULL )
247
0
  {
248
0
    libcerror_error_set(
249
0
     error,
250
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
251
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
252
0
     "%s: invalid size.",
253
0
     function );
254
255
0
    return( -1 );
256
0
  }
257
2.18M
  *offset = mapped_range->offset;
258
2.18M
  *size   = mapped_range->size;
259
260
2.18M
  return( 1 );
261
2.18M
}
262
263
/* Sets the mapped range values
264
 * Returns 1 if successful or -1 on error
265
 */
266
int libfdata_mapped_range_set(
267
     libfdata_mapped_range_t *mapped_range,
268
     off64_t offset,
269
     size64_t size,
270
     libcerror_error_t **error )
271
8.90M
{
272
8.90M
  static char *function = "libfdata_mapped_range_set";
273
274
8.90M
  if( mapped_range == NULL )
275
0
  {
276
0
    libcerror_error_set(
277
0
     error,
278
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
279
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
280
0
     "%s: invalid mapped range.",
281
0
     function );
282
283
0
    return( -1 );
284
0
  }
285
8.90M
  if( offset < 0 )
286
211
  {
287
211
    libcerror_error_set(
288
211
     error,
289
211
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
290
211
     LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
291
211
     "%s: invalid offset value less than zero.",
292
211
     function );
293
294
211
    return( -1 );
295
211
  }
296
8.90M
  if( size > (size64_t) INT64_MAX )
297
957
  {
298
957
    libcerror_error_set(
299
957
     error,
300
957
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
301
957
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
302
957
     "%s: invalid size value exceeds maximum.",
303
957
     function );
304
305
957
    return( -1 );
306
957
  }
307
8.90M
  mapped_range->offset = offset;
308
8.90M
  mapped_range->size   = size;
309
310
8.90M
  return( 1 );
311
8.90M
}
312