Coverage Report

Created: 2024-06-12 07:07

/src/libexe/libexe/libexe_debug_data.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Debug data functions
3
 *
4
 * Copyright (C) 2011-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 "libexe_debug_data.h"
27
#include "libexe_libcerror.h"
28
#include "libexe_libcnotify.h"
29
#include "libexe_libfcache.h"
30
#include "libexe_libfdata.h"
31
32
/* Creates a debug data
33
 * Make sure the value debug_data is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libexe_debug_data_initialize(
37
     libexe_debug_data_t **debug_data,
38
     libcerror_error_t **error )
39
116
{
40
116
  static char *function = "libexe_debug_data_initialize";
41
42
116
  if( debug_data == 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 debug data.",
49
0
     function );
50
51
0
    return( -1 );
52
0
  }
53
116
  if( *debug_data != 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 debug data value already set.",
60
0
     function );
61
62
0
    return( -1 );
63
0
  }
64
116
  *debug_data = memory_allocate_structure(
65
116
                 libexe_debug_data_t );
66
67
116
  if( *debug_data == 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 debug data.",
74
0
     function );
75
76
0
    goto on_error;
77
0
  }
78
116
  if( memory_set(
79
116
       *debug_data,
80
116
       0,
81
116
       sizeof( libexe_debug_data_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 debug data.",
88
0
     function );
89
90
0
    memory_free(
91
0
     *debug_data );
92
93
0
    *debug_data = NULL;
94
95
0
    return( -1 );
96
0
  }
97
116
  return( 1 );
98
99
0
on_error:
100
0
  if( *debug_data != NULL )
101
0
  {
102
0
    memory_free(
103
0
     *debug_data );
104
105
0
    *debug_data = NULL;
106
0
  }
107
0
  return( -1 );
108
116
}
109
110
/* Frees a debug data
111
 * Returns 1 if successful or -1 on error
112
 */
113
int libexe_debug_data_free(
114
     libexe_debug_data_t **debug_data,
115
     libcerror_error_t **error )
116
116
{
117
116
  static char *function = "libexe_debug_data_free";
118
119
116
  if( debug_data == NULL )
120
0
  {
121
0
    libcerror_error_set(
122
0
     error,
123
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
124
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
125
0
     "%s: invalid debug data.",
126
0
     function );
127
128
0
    return( -1 );
129
0
  }
130
116
  if( *debug_data != NULL )
131
116
  {
132
116
    memory_free(
133
116
     *debug_data );
134
135
116
    *debug_data = NULL;
136
116
  }
137
116
  return( 1 );
138
116
}
139
140
/* Reads the debug data
141
 * Returns 1 if successful or -1 on error
142
 */
143
int libexe_debug_data_read(
144
     libexe_debug_data_t *debug_data,
145
     libbfio_handle_t *file_io_handle,
146
     uint32_t file_offset,
147
     uint32_t size,
148
     libcerror_error_t **error )
149
116
{
150
116
  uint8_t *data         = NULL;
151
116
  static char *function = "libexe_debug_data_read";
152
116
  size_t read_count     = 0;
153
154
116
  if( debug_data == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid debug data.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
116
  if( ( size == 0 )
166
116
   || ( size > MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
167
37
  {
168
37
    libcerror_error_set(
169
37
     error,
170
37
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
171
37
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
172
37
     "%s: invalid size value out of bounds.",
173
37
     function );
174
175
37
    return( -1 );
176
37
  }
177
79
  data = (uint8_t *) memory_allocate(
178
79
                      sizeof( uint8_t ) * size );
179
180
79
  if( data == NULL )
181
0
  {
182
0
    libcerror_error_set(
183
0
     error,
184
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
185
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
186
0
     "%s: unable to create debug data.",
187
0
     function );
188
189
0
    goto on_error;
190
0
  }
191
#if defined( HAVE_DEBUG_OUTPUT )
192
  if( libcnotify_verbose != 0 )
193
  {
194
    libcnotify_printf(
195
     "%s: reading debug data at offset: %" PRIu32 " (0x%08" PRIx32 ")\n",
196
     function,
197
     file_offset,
198
     file_offset );
199
  }
200
#endif
201
79
  read_count = libbfio_handle_read_buffer_at_offset(
202
79
                file_io_handle,
203
79
                data,
204
79
                size,
205
79
                (off64_t) file_offset,
206
79
                error );
207
208
79
  if( read_count != (ssize_t) size )
209
75
  {
210
75
    libcerror_error_set(
211
75
     error,
212
75
     LIBCERROR_ERROR_DOMAIN_IO,
213
75
     LIBCERROR_IO_ERROR_READ_FAILED,
214
75
     "%s: unable to read debug data.",
215
75
     function );
216
217
75
    goto on_error;
218
75
  }
219
#if defined( HAVE_DEBUG_OUTPUT )
220
  if( libcnotify_verbose != 0 )
221
  {
222
    libcnotify_printf(
223
     "%s: debug data:\n",
224
     function );
225
    libcnotify_print_data(
226
     data,
227
     (size_t) size,
228
     0 );
229
  }
230
#endif
231
  /* TODO extract values */
232
233
4
  memory_free(
234
4
   data );
235
236
4
  return( 1 );
237
238
75
on_error:
239
75
  if( data != NULL )
240
75
  {
241
75
    memory_free(
242
75
     data );
243
75
  }
244
75
  return( -1 );
245
79
}
246