Coverage Report

Created: 2025-06-13 07:22

/src/libfwevt/libfwevt/libfwevt_map.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Map 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 <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
679k
{
42
679k
  libfwevt_internal_map_t *internal_map = NULL;
43
679k
  static char *function                 = "libfwevt_map_initialize";
44
45
679k
  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
679k
  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
679k
  internal_map = memory_allocate_structure(
68
679k
                  libfwevt_internal_map_t );
69
70
679k
  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
679k
  if( memory_set(
82
679k
       internal_map,
83
679k
       0,
84
679k
       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
679k
  *map = (libfwevt_map_t *) internal_map;
96
97
679k
  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
679k
}
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
679k
{
143
679k
  static char *function = "libfwevt_internal_map_free";
144
145
679k
  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
679k
  if( *internal_map != NULL )
157
679k
  {
158
679k
    memory_free(
159
679k
     *internal_map );
160
161
679k
    *internal_map = NULL;
162
679k
  }
163
679k
  return( 1 );
164
679k
}
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
679k
{
176
679k
  fwevt_template_map_t *wevt_map = NULL;
177
679k
  static char *function          = "libfwevt_map_read_data";
178
179
679k
  if( map == NULL )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
184
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
185
0
     "%s: invalid map.",
186
0
     function );
187
188
0
    return( -1 );
189
0
  }
190
679k
  if( data == NULL )
191
0
  {
192
0
    libcerror_error_set(
193
0
     error,
194
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
195
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
196
0
     "%s: invalid data.",
197
0
     function );
198
199
0
    return( -1 );
200
0
  }
201
679k
  if( data_size > (size_t) SSIZE_MAX )
202
0
  {
203
0
    libcerror_error_set(
204
0
     error,
205
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
206
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
207
0
     "%s: invalid data size value exceeds maximum.",
208
0
     function );
209
210
0
    return( -1 );
211
0
  }
212
679k
  if( data_offset >= data_size )
213
105
  {
214
105
    libcerror_error_set(
215
105
     error,
216
105
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
217
105
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
218
105
     "%s: invalid data offset value out of bounds.",
219
105
     function );
220
221
105
    return( -1 );
222
105
  }
223
679k
  if( ( data_size < sizeof( fwevt_template_map_t ) )
224
679k
   || ( data_offset > ( data_size - sizeof( fwevt_template_map_t ) ) ) )
225
13
  {
226
13
    libcerror_error_set(
227
13
     error,
228
13
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
229
13
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
230
13
     "%s: invalid data value too small.",
231
13
     function );
232
233
13
    return( -1 );
234
13
  }
235
679k
  wevt_map = (fwevt_template_map_t *) &( data[ data_offset ] );
236
237
#if defined( HAVE_DEBUG_OUTPUT )
238
  if( libcnotify_verbose != 0 )
239
  {
240
    libcnotify_printf(
241
     "%s: map data:\n",
242
     function );
243
    libcnotify_print_data(
244
     (uint8_t *) wevt_map,
245
     sizeof( fwevt_template_map_t ),
246
     0 );
247
  }
248
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
249
250
/* TODO implement map support */
251
252
#if defined( HAVE_DEBUG_OUTPUT )
253
  if( libcnotify_verbose != 0 )
254
  {
255
    libcnotify_printf(
256
     "%s: signature\t\t\t\t: %c%c%c%c\n",
257
     function,
258
     wevt_map->signature[ 0 ],
259
     wevt_map->signature[ 1 ],
260
     wevt_map->signature[ 2 ],
261
     wevt_map->signature[ 3 ] );
262
  }
263
#endif
264
/* TODO implement map support */
265
266
#if defined( HAVE_DEBUG_OUTPUT )
267
  if( libcnotify_verbose != 0 )
268
  {
269
    libcnotify_printf(
270
     "\n" );
271
  }
272
#endif
273
679k
  return( 1 );
274
679k
}
275