Coverage Report

Created: 2026-07-25 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libexe/libexe/libexe_section_descriptor.c
Line
Count
Source
1
/*
2
 * Section descriptor functions
3
 *
4
 * Copyright (C) 2011-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 "libexe_io_handle.h"
27
#include "libexe_libcerror.h"
28
#include "libexe_libfcache.h"
29
#include "libexe_libfdata.h"
30
#include "libexe_section_descriptor.h"
31
32
/* Creates a section descriptor
33
 * Make sure the value section_descriptor is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libexe_section_descriptor_initialize(
37
     libexe_section_descriptor_t **section_descriptor,
38
     libcerror_error_t **error )
39
205k
{
40
205k
  static char *function = "libexe_section_descriptor_initialize";
41
42
205k
  if( section_descriptor == NULL )
43
0
  {
44
0
    libcerror_error_set(
45
0
     error,
46
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48
0
     "%s: invalid section descriptor.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
205k
  if( *section_descriptor != NULL )
54
0
  {
55
0
    libcerror_error_set(
56
0
     error,
57
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
58
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
59
0
     "%s: invalid section descriptor value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
205k
  *section_descriptor = memory_allocate_structure(
65
205k
                         libexe_section_descriptor_t );
66
67
205k
  if( *section_descriptor == NULL )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
72
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
73
0
     "%s: unable to create section descriptor.",
74
0
     function );
75
76
0
    goto on_error;
77
0
  }
78
205k
  if( memory_set(
79
205k
       *section_descriptor,
80
205k
       0,
81
205k
       sizeof( libexe_section_descriptor_t ) ) == NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
86
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
87
0
     "%s: unable to clear section descriptor.",
88
0
     function );
89
90
0
    memory_free(
91
0
     *section_descriptor );
92
93
0
    *section_descriptor = NULL;
94
95
0
    return( -1 );
96
0
  }
97
205k
  if( libfdata_stream_initialize(
98
205k
       &( ( *section_descriptor )->data_stream ),
99
205k
       NULL,
100
205k
       NULL,
101
205k
       NULL,
102
205k
       NULL,
103
205k
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libexe_io_handle_read_segment_data,
104
205k
       NULL,
105
205k
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libexe_io_handle_seek_segment_offset,
106
205k
       0,
107
205k
       error ) != 1 )
108
0
  {
109
0
    libcerror_error_set(
110
0
     error,
111
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
112
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
113
0
     "%s: unable to create data stream.",
114
0
     function );
115
116
0
    goto on_error;
117
0
  }
118
205k
  if( libfdata_stream_resize(
119
205k
       ( *section_descriptor )->data_stream,
120
205k
       1,
121
205k
       error ) != 1 )
122
0
  {
123
0
    libcerror_error_set(
124
0
     error,
125
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
126
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
127
0
     "%s: unable to resize data stream.",
128
0
     function );
129
130
0
    goto on_error;
131
0
  }
132
205k
  return( 1 );
133
134
0
on_error:
135
0
  if( *section_descriptor != NULL )
136
0
  {
137
0
    if( ( *section_descriptor )->data_stream != NULL )
138
0
    {
139
0
      libfdata_stream_free(
140
0
       &( ( *section_descriptor )->data_stream ),
141
0
       NULL );
142
0
    }
143
0
    memory_free(
144
0
     *section_descriptor );
145
146
0
    *section_descriptor = NULL;
147
0
  }
148
0
  return( -1 );
149
205k
}
150
151
/* Frees a section descriptor
152
 * Returns 1 if successful or -1 on error
153
 */
154
int libexe_section_descriptor_free(
155
     libexe_section_descriptor_t **section_descriptor,
156
     libcerror_error_t **error )
157
205k
{
158
205k
  static char *function = "libexe_section_descriptor_free";
159
205k
  int result            = 1;
160
161
205k
  if( section_descriptor == NULL )
162
0
  {
163
0
    libcerror_error_set(
164
0
     error,
165
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
166
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
167
0
     "%s: invalid section descriptor.",
168
0
     function );
169
170
0
    return( -1 );
171
0
  }
172
205k
  if( *section_descriptor != NULL )
173
205k
  {
174
205k
    if( libfdata_stream_free(
175
205k
         &( ( *section_descriptor )->data_stream ),
176
205k
         error ) != 1 )
177
0
    {
178
0
      libcerror_error_set(
179
0
       error,
180
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
181
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
182
0
       "%s: unable to free data stream.",
183
0
       function );
184
185
0
      result = -1;
186
0
    }
187
205k
    memory_free(
188
205k
     *section_descriptor );
189
190
205k
    *section_descriptor = NULL;
191
205k
  }
192
205k
  return( result );
193
205k
}
194
195
/* Retrieves the data size
196
 * Returns 1 if successful or -1 on error
197
 */
198
int libexe_section_descriptor_get_data_size(
199
     libexe_section_descriptor_t *section_descriptor,
200
     size64_t *data_size,
201
     libcerror_error_t **error )
202
0
{
203
0
  static char *function = "libexe_section_descriptor_get_data_size";
204
205
0
  if( section_descriptor == NULL )
206
0
  {
207
0
    libcerror_error_set(
208
0
     error,
209
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
210
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
211
0
     "%s: invalid section descriptor.",
212
0
     function );
213
214
0
    return( -1 );
215
0
  }
216
0
  if( libfdata_stream_get_size(
217
0
       section_descriptor->data_stream,
218
0
       data_size,
219
0
       error ) != 1 )
220
0
  {
221
0
    libcerror_error_set(
222
0
     error,
223
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
224
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
225
0
     "%s: unable to retrieve data stream size.",
226
0
     function );
227
228
0
    return( -1 );
229
0
  }
230
0
  return( 1 );
231
0
}
232
233
/* Retrieves the data range
234
 * Returns 1 if successful or -1 on error
235
 */
236
int libexe_section_descriptor_get_data_range(
237
     libexe_section_descriptor_t *section_descriptor,
238
     off64_t *data_offset,
239
     size64_t *data_size,
240
     libcerror_error_t **error )
241
42.8k
{
242
42.8k
  static char *function  = "libexe_section_descriptor_get_data_range";
243
42.8k
  uint32_t segment_flags = 0;
244
42.8k
  int segment_file_index = 0;
245
246
42.8k
  if( section_descriptor == 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 section descriptor.",
253
0
     function );
254
255
0
    return( -1 );
256
0
  }
257
42.8k
  if( libfdata_stream_get_segment_by_index(
258
42.8k
       section_descriptor->data_stream,
259
42.8k
       0,
260
42.8k
       &segment_file_index,
261
42.8k
       data_offset,
262
42.8k
       data_size,
263
42.8k
       &segment_flags,
264
42.8k
       error ) != 1 )
265
0
  {
266
0
    libcerror_error_set(
267
0
     error,
268
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
269
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
270
0
     "%s: unable to retrieve segment from data stream.",
271
0
     function );
272
273
0
    return( -1 );
274
0
  }
275
42.8k
  return( 1 );
276
42.8k
}
277
278
/* Sets the data range
279
 * Returns 1 if successful or -1 on error
280
 */
281
int libexe_section_descriptor_set_data_range(
282
     libexe_section_descriptor_t *section_descriptor,
283
     off64_t data_offset,
284
     size64_t data_size,
285
     libcerror_error_t **error )
286
205k
{
287
205k
  static char *function = "libexe_section_descriptor_set_data_range";
288
289
205k
  if( section_descriptor == NULL )
290
0
  {
291
0
    libcerror_error_set(
292
0
     error,
293
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
294
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
295
0
     "%s: invalid section descriptor.",
296
0
     function );
297
298
0
    return( -1 );
299
0
  }
300
205k
  if( libfdata_stream_set_segment_by_index(
301
205k
       section_descriptor->data_stream,
302
205k
       0,
303
205k
       0,
304
205k
       data_offset,
305
205k
       data_size,
306
205k
       0,
307
205k
       error ) != 1 )
308
0
  {
309
0
    libcerror_error_set(
310
0
     error,
311
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
312
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
313
0
     "%s: unable to set segment in data stream.",
314
0
     function );
315
316
0
    return( -1 );
317
0
  }
318
205k
  return( 1 );
319
205k
}
320