Coverage Report

Created: 2024-02-25 07:19

/src/libesedb/libfdata/libfdata_range.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The 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_range.h"
28
29
/* Creates a range
30
 * Make sure the value range is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libfdata_range_initialize(
34
     libfdata_range_t **range,
35
     libcerror_error_t **error )
36
1.19k
{
37
1.19k
  static char *function = "libfdata_range_initialize";
38
39
1.19k
  if( 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 range.",
46
0
     function );
47
48
0
    return( -1 );
49
0
  }
50
1.19k
  if( *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 range value already set.",
57
0
     function );
58
59
0
    return( -1 );
60
0
  }
61
1.19k
  *range = memory_allocate_structure(
62
1.19k
            libfdata_range_t );
63
64
1.19k
  if( *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 range.",
71
0
     function );
72
73
0
    goto on_error;
74
0
  }
75
1.19k
  if( memory_set(
76
1.19k
       *range,
77
1.19k
       0,
78
1.19k
       sizeof( libfdata_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 range.",
85
0
     function );
86
87
0
    goto on_error;
88
0
  }
89
1.19k
  ( *range )->file_index = -1;
90
1.19k
  ( *range )->offset     = (off64_t) -1;
91
92
1.19k
  return( 1 );
93
94
0
on_error:
95
0
  if( *range != NULL )
96
0
  {
97
0
    memory_free(
98
0
     *range );
99
100
0
    *range = NULL;
101
0
  }
102
0
  return( -1 );
103
1.19k
}
104
105
/* Frees a range
106
 * Returns 1 if successful or -1 on error
107
 */
108
int libfdata_range_free(
109
     libfdata_range_t **range,
110
     libcerror_error_t **error )
111
1.19k
{
112
1.19k
  static char *function = "libfdata_range_free";
113
114
1.19k
  if( range == NULL )
115
0
  {
116
0
    libcerror_error_set(
117
0
     error,
118
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
119
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
120
0
     "%s: invalid range.",
121
0
     function );
122
123
0
    return( -1 );
124
0
  }
125
1.19k
  if( *range != NULL )
126
1.19k
  {
127
1.19k
    memory_free(
128
1.19k
     *range );
129
130
1.19k
    *range = NULL;
131
1.19k
  }
132
1.19k
  return( 1 );
133
1.19k
}
134
135
/* Clones (duplicates) the range
136
 * Returns 1 if successful or -1 on error
137
 */
138
int libfdata_range_clone(
139
     libfdata_range_t **destination_range,
140
     libfdata_range_t *source_range,
141
     libcerror_error_t **error )
142
0
{
143
0
  static char *function = "libfdata_range_clone";
144
145
0
  if( destination_range == NULL )
146
0
  {
147
0
    libcerror_error_set(
148
0
     error,
149
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
151
0
     "%s: invalid destination range.",
152
0
     function );
153
154
0
    return( -1 );
155
0
  }
156
0
  if( *destination_range != NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
161
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
162
0
     "%s: invalid destination range value already set.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  if( source_range == NULL )
168
0
  {
169
0
    *destination_range = NULL;
170
171
0
    return( 1 );
172
0
  }
173
0
  *destination_range = memory_allocate_structure(
174
0
                        libfdata_range_t );
175
176
0
  if( *destination_range == NULL )
177
0
  {
178
0
    libcerror_error_set(
179
0
     error,
180
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
181
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
182
0
     "%s: unable to create destination range.",
183
0
     function );
184
185
0
    goto on_error;
186
0
  }
187
0
  if( memory_copy(
188
0
       *destination_range,
189
0
       source_range,
190
0
       sizeof( libfdata_range_t ) ) == NULL )
191
0
  {
192
0
    libcerror_error_set(
193
0
     error,
194
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
195
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
196
0
     "%s: unable to copy source to destination range.",
197
0
     function );
198
199
0
    goto on_error;
200
0
  }
201
0
  return( 1 );
202
203
0
on_error:
204
0
  if( *destination_range != NULL )
205
0
  {
206
0
    memory_free(
207
0
     *destination_range );
208
209
0
    *destination_range = NULL;
210
0
  }
211
0
  return( -1 );
212
0
}
213
214
/* Retrieves the range values
215
 * Returns 1 if successful or -1 on error
216
 */
217
int libfdata_range_get(
218
     libfdata_range_t *range,
219
     int *file_index,
220
     off64_t *offset,
221
     size64_t *size,
222
     uint32_t *flags,
223
     libcerror_error_t **error )
224
0
{
225
0
  static char *function = "libfdata_range_get";
226
227
0
  if( range == NULL )
228
0
  {
229
0
    libcerror_error_set(
230
0
     error,
231
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
233
0
     "%s: invalid range.",
234
0
     function );
235
236
0
    return( -1 );
237
0
  }
238
0
  if( file_index == NULL )
239
0
  {
240
0
    libcerror_error_set(
241
0
     error,
242
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
243
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
244
0
     "%s: invalid file index.",
245
0
     function );
246
247
0
    return( -1 );
248
0
  }
249
0
  if( offset == NULL )
250
0
  {
251
0
    libcerror_error_set(
252
0
     error,
253
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
254
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
255
0
     "%s: invalid offset.",
256
0
     function );
257
258
0
    return( -1 );
259
0
  }
260
0
  if( size == NULL )
261
0
  {
262
0
    libcerror_error_set(
263
0
     error,
264
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
265
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
266
0
     "%s: invalid size.",
267
0
     function );
268
269
0
    return( -1 );
270
0
  }
271
0
  if( flags == NULL )
272
0
  {
273
0
    libcerror_error_set(
274
0
     error,
275
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
276
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
277
0
     "%s: invalid flags.",
278
0
     function );
279
280
0
    return( -1 );
281
0
  }
282
0
  *file_index = range->file_index;
283
0
  *offset     = range->offset;
284
0
  *size       = range->size;
285
0
  *flags      = range->flags;
286
287
0
  return( 1 );
288
0
}
289
290
/* Retrieves the size
291
 * Returns 1 if successful or -1 on error
292
 */
293
int libfdata_range_get_size(
294
     libfdata_range_t *range,
295
     size64_t *size,
296
     libcerror_error_t **error )
297
8.79k
{
298
8.79k
  static char *function = "libfdata_range_get_size";
299
300
8.79k
  if( range == NULL )
301
0
  {
302
0
    libcerror_error_set(
303
0
     error,
304
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
305
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
306
0
     "%s: invalid range.",
307
0
     function );
308
309
0
    return( -1 );
310
0
  }
311
8.79k
  if( size == NULL )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
316
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
317
0
     "%s: invalid size.",
318
0
     function );
319
320
0
    return( -1 );
321
0
  }
322
8.79k
  *size = range->size;
323
324
8.79k
  return( 1 );
325
8.79k
}
326
327
/* Sets the range values
328
 * Returns 1 if successful or -1 on error
329
 */
330
int libfdata_range_set(
331
     libfdata_range_t *range,
332
     int file_index,
333
     off64_t offset,
334
     size64_t size,
335
     uint32_t flags,
336
     libcerror_error_t **error )
337
1.19k
{
338
1.19k
  static char *function = "libfdata_range_set";
339
340
1.19k
  if( range == NULL )
341
0
  {
342
0
    libcerror_error_set(
343
0
     error,
344
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
345
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
346
0
     "%s: invalid range.",
347
0
     function );
348
349
0
    return( -1 );
350
0
  }
351
1.19k
  range->file_index = file_index;
352
1.19k
  range->offset     = offset;
353
1.19k
  range->size       = size;
354
1.19k
  range->flags      = flags;
355
356
1.19k
  return( 1 );
357
1.19k
}
358