Coverage Report

Created: 2026-10-22 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfwevt/libfwevt/libfwevt_map.c
Line
Count
Source
1
/*
2
 * Map 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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfwevt_libcerror.h"
28
#include "libfwevt_libcnotify.h"
29
#include "libfwevt_map.h"
30
#include "libfwevt_types.h"
31
32
#include "fwevt_template.h"
33
34
/* Creates a map
35
 * Make sure the value map is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfwevt_map_initialize(
39
     libfwevt_map_t **map,
40
     libcerror_error_t **error )
41
502k
{
42
502k
  libfwevt_internal_map_t *internal_map = NULL;
43
502k
  static char *function                 = "libfwevt_map_initialize";
44
45
502k
  if( map == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid map.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
502k
  if( *map != NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
61
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
62
0
     "%s: invalid map value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
502k
  internal_map = memory_allocate_structure(
68
502k
                  libfwevt_internal_map_t );
69
70
502k
  if( internal_map == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
75
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
76
0
     "%s: unable to create map.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
502k
  if( memory_set(
82
502k
       internal_map,
83
502k
       0,
84
502k
       sizeof( libfwevt_internal_map_t ) ) == NULL )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
89
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
90
0
     "%s: unable to clear map.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
502k
  *map = (libfwevt_map_t *) internal_map;
96
97
502k
  return( 1 );
98
99
0
on_error:
100
0
  if( internal_map != NULL )
101
0
  {
102
0
    memory_free(
103
0
     internal_map );
104
0
  }
105
0
  return( -1 );
106
502k
}
107
108
/* Frees a map
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfwevt_map_free(
112
     libfwevt_map_t **map,
113
     libcerror_error_t **error )
114
0
{
115
0
  static char *function = "libfwevt_map_free";
116
117
0
  if( map == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid map.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
0
  if( *map != NULL )
129
0
  {
130
0
    *map = NULL;
131
0
  }
132
0
  return( 1 );
133
0
}
134
135
136
/* Frees a map
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libfwevt_internal_map_free(
140
     libfwevt_internal_map_t **internal_map,
141
     libcerror_error_t **error )
142
502k
{
143
502k
  static char *function = "libfwevt_internal_map_free";
144
145
502k
  if( internal_map == 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 map.",
152
0
     function );
153
154
0
    return( -1 );
155
0
  }
156
502k
  if( *internal_map != NULL )
157
502k
  {
158
502k
    memory_free(
159
502k
     *internal_map );
160
161
502k
    *internal_map = NULL;
162
502k
  }
163
502k
  return( 1 );
164
502k
}
165
166
/* Reads the map
167
 * Returns 1 if successful or -1 on error
168
 */
169
int libfwevt_map_read_data(
170
     libfwevt_map_t *map,
171
     const uint8_t *data,
172
     size_t data_size,
173
     size_t data_offset,
174
     libcerror_error_t **error )
175
502k
{
176
502k
  static char *function          = "libfwevt_map_read_data";
177
178
#if defined( HAVE_DEBUG_OUTPUT )
179
  fwevt_template_map_t *wevt_map = NULL;
180
#endif
181
182
502k
  if( map == NULL )
183
0
  {
184
0
    libcerror_error_set(
185
0
     error,
186
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
187
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
188
0
     "%s: invalid map.",
189
0
     function );
190
191
0
    return( -1 );
192
0
  }
193
502k
  if( data == NULL )
194
0
  {
195
0
    libcerror_error_set(
196
0
     error,
197
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
198
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
199
0
     "%s: invalid data.",
200
0
     function );
201
202
0
    return( -1 );
203
0
  }
204
502k
  if( data_size > (size_t) SSIZE_MAX )
205
0
  {
206
0
    libcerror_error_set(
207
0
     error,
208
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
209
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
210
0
     "%s: invalid data size value exceeds maximum.",
211
0
     function );
212
213
0
    return( -1 );
214
0
  }
215
502k
  if( data_offset >= data_size )
216
64
  {
217
64
    libcerror_error_set(
218
64
     error,
219
64
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220
64
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
221
64
     "%s: invalid data offset value out of bounds.",
222
64
     function );
223
224
64
    return( -1 );
225
64
  }
226
502k
  if( ( data_size < sizeof( fwevt_template_map_t ) )
227
502k
   || ( data_offset > ( data_size - sizeof( fwevt_template_map_t ) ) ) )
228
12
  {
229
12
    libcerror_error_set(
230
12
     error,
231
12
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
232
12
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
233
12
     "%s: invalid data value too small.",
234
12
     function );
235
236
12
    return( -1 );
237
12
  }
238
#if defined( HAVE_DEBUG_OUTPUT )
239
  if( libcnotify_verbose != 0 )
240
  {
241
    wevt_map = (fwevt_template_map_t *) &( data[ data_offset ] );
242
243
    libcnotify_printf(
244
     "%s: map data:\n",
245
     function );
246
    libcnotify_print_data(
247
     (uint8_t *) wevt_map,
248
     sizeof( fwevt_template_map_t ),
249
     0 );
250
  }
251
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
252
253
/* TODO implement map support */
254
255
#if defined( HAVE_DEBUG_OUTPUT )
256
  if( libcnotify_verbose != 0 )
257
  {
258
    libcnotify_printf(
259
     "%s: signature\t\t\t\t: %c%c%c%c\n",
260
     function,
261
     wevt_map->signature[ 0 ],
262
     wevt_map->signature[ 1 ],
263
     wevt_map->signature[ 2 ],
264
     wevt_map->signature[ 3 ] );
265
  }
266
#endif
267
/* TODO implement map support */
268
269
#if defined( HAVE_DEBUG_OUTPUT )
270
  if( libcnotify_verbose != 0 )
271
  {
272
    libcnotify_printf(
273
     "\n" );
274
  }
275
#endif
276
502k
  return( 1 );
277
502k
}
278