Coverage Report

Created: 2026-07-10 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libewf/libewf/libewf_extent.c
Line
Count
Source
1
/*
2
 * Extent functions
3
 *
4
 * Copyright (C) 2006-2026, 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_extent.h"
27
#include "libewf_lef_extent.h"
28
#include "libewf_libcerror.h"
29
#include "libewf_libcthreads.h"
30
#include "libewf_types.h"
31
32
/* Creates an extent
33
 * Make sure the value extent is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libewf_extent_initialize(
37
     libewf_extent_t **extent,
38
     libewf_lef_extent_t *lef_extent,
39
     libcerror_error_t **error )
40
0
{
41
0
  libewf_internal_extent_t *internal_extent = NULL;
42
0
  static char *function                     = "libewf_extent_initialize";
43
44
0
  if( extent == 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 extent.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
0
  if( *extent != 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 extent value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
0
  if( lef_extent == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
72
0
     "%s: invalid extent.",
73
0
     function );
74
75
0
    return( -1 );
76
0
  }
77
0
  internal_extent = memory_allocate_structure(
78
0
                     libewf_internal_extent_t );
79
80
0
  if( internal_extent == NULL )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
85
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
86
0
     "%s: unable to create extent.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
0
  if( memory_set(
92
0
       internal_extent,
93
0
       0,
94
0
       sizeof( libewf_internal_extent_t ) ) == NULL )
95
0
  {
96
0
    libcerror_error_set(
97
0
     error,
98
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
99
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
100
0
     "%s: unable to clear extent.",
101
0
     function );
102
103
0
    memory_free(
104
0
     internal_extent );
105
106
0
    return( -1 );
107
0
  }
108
0
#if defined( HAVE_LIBEWF_MULTI_THREAD_SUPPORT )
109
0
  if( libcthreads_read_write_lock_initialize(
110
0
       &( internal_extent->read_write_lock ),
111
0
       error ) != 1 )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
116
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
117
0
     "%s: unable to initialize read/write lock.",
118
0
     function );
119
120
0
    goto on_error;
121
0
  }
122
0
#endif
123
0
  internal_extent->lef_extent = lef_extent;
124
125
0
  *extent = (libewf_extent_t *) internal_extent;
126
127
0
  return( 1 );
128
129
0
on_error:
130
0
  if( internal_extent != NULL )
131
0
  {
132
0
    memory_free(
133
0
     internal_extent );
134
0
  }
135
0
  return( -1 );
136
0
}
137
138
/* Frees an extent
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libewf_extent_free(
142
     libewf_extent_t **extent,
143
     libcerror_error_t **error )
144
0
{
145
0
  libewf_internal_extent_t *internal_extent = NULL;
146
0
  static char *function                     = "libewf_extent_free";
147
0
  int result                                = 1;
148
149
0
  if( extent == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
155
0
     "%s: invalid extent.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
0
  if( *extent != NULL )
161
0
  {
162
0
    internal_extent = (libewf_internal_extent_t *) *extent;
163
0
    *extent         = NULL;
164
165
0
#if defined( HAVE_LIBEWF_MULTI_THREAD_SUPPORT )
166
0
    if( libcthreads_read_write_lock_free(
167
0
         &( internal_extent->read_write_lock ),
168
0
         error ) != 1 )
169
0
    {
170
0
      libcerror_error_set(
171
0
       error,
172
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
173
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
174
0
       "%s: unable to free read/write lock.",
175
0
       function );
176
177
0
      result = -1;
178
0
    }
179
0
#endif
180
0
    memory_free(
181
0
     internal_extent );
182
0
  }
183
0
  return( result );
184
0
}
185