Coverage Report

Created: 2023-06-07 06:53

/src/libfsntfs/libfsntfs/libfsntfs_logged_utility_stream_values.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Logged utility stream attribute ($LOGGED_UTILITY_STREAM) values functions
3
 *
4
 * Copyright (C) 2010-2023, 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 "libfsntfs_definitions.h"
28
#include "libfsntfs_libcerror.h"
29
#include "libfsntfs_libcnotify.h"
30
#include "libfsntfs_logged_utility_stream_values.h"
31
#include "libfsntfs_mft_attribute.h"
32
33
#include "fsntfs_logged_utility_stream.h"
34
35
/* Creates logged utility stream values
36
 * Make sure the value logged_utility_stream_values is referencing, is set to NULL
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libfsntfs_logged_utility_stream_values_initialize(
40
     libfsntfs_logged_utility_stream_values_t **logged_utility_stream_values,
41
     libcerror_error_t **error )
42
0
{
43
0
  static char *function = "libfsntfs_logged_utility_stream_values_initialize";
44
45
0
  if( logged_utility_stream_values == 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 logged utility stream values.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
0
  if( *logged_utility_stream_values != 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 logged utility stream values value already set.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  *logged_utility_stream_values = memory_allocate_structure(
68
0
                                   libfsntfs_logged_utility_stream_values_t );
69
70
0
  if( *logged_utility_stream_values == 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 logged utility stream values.",
77
0
     function );
78
79
0
    goto on_error;
80
0
  }
81
0
  if( memory_set(
82
0
       *logged_utility_stream_values,
83
0
       0,
84
0
       sizeof( libfsntfs_logged_utility_stream_values_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 logged utility stream values.",
91
0
     function );
92
93
0
    goto on_error;
94
0
  }
95
0
  return( 1 );
96
97
0
on_error:
98
0
  if( *logged_utility_stream_values != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *logged_utility_stream_values );
102
103
0
    *logged_utility_stream_values = NULL;
104
0
  }
105
0
  return( -1 );
106
0
}
107
108
/* Frees logged utility stream values
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libfsntfs_logged_utility_stream_values_free(
112
     libfsntfs_logged_utility_stream_values_t **logged_utility_stream_values,
113
     libcerror_error_t **error )
114
0
{
115
0
  static char *function = "libfsntfs_logged_utility_stream_values_free";
116
117
0
  if( logged_utility_stream_values == 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 logged utility stream values.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
0
  if( *logged_utility_stream_values != NULL )
129
0
  {
130
0
    memory_free(
131
0
     *logged_utility_stream_values );
132
133
0
    *logged_utility_stream_values = NULL;
134
0
  }
135
0
  return( 1 );
136
0
}
137
138
/* Reads the logged utility stream values
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsntfs_logged_utility_stream_values_read_data(
142
     libfsntfs_logged_utility_stream_values_t *logged_utility_stream_values,
143
     const uint8_t *data,
144
     size_t data_size,
145
     libcerror_error_t **error )
146
0
{
147
0
  static char *function = "libfsntfs_logged_utility_stream_values_read_data";
148
149
0
  if( logged_utility_stream_values == 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 logged utility stream values.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
0
  if( data == NULL )
161
0
  {
162
0
    libcerror_error_set(
163
0
     error,
164
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
165
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
166
0
     "%s: invalid data.",
167
0
     function );
168
169
0
    return( -1 );
170
0
  }
171
0
  if( data_size > (size_t) SSIZE_MAX )
172
0
  {
173
0
    libcerror_error_set(
174
0
     error,
175
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
176
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
177
0
     "%s: invalid data size value out of bounds.",
178
0
     function );
179
180
0
    return( -1 );
181
0
  }
182
#if defined( HAVE_DEBUG_OUTPUT )
183
  if( libcnotify_verbose != 0 )
184
  {
185
    libcnotify_printf(
186
     "%s: logged utility stream data:\n",
187
     function );
188
    libcnotify_print_data(
189
     data,
190
     data_size,
191
     0 );
192
  }
193
#endif
194
0
  if( data_size == 0 )
195
0
  {
196
0
    libcerror_error_set(
197
0
     error,
198
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
199
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
200
0
     "%s: unsupported logged utility stream data size: %" PRIzd "\n",
201
0
     function,
202
0
     data_size );
203
204
0
    return( -1 );
205
0
  }
206
0
  return( 1 );
207
0
}
208
209
/* Reads the logged utility stream values from an MFT attribute
210
 * Returns 1 if successful or -1 on error
211
 */
212
int libfsntfs_logged_utility_stream_values_read_from_mft_attribute(
213
     libfsntfs_logged_utility_stream_values_t *logged_utility_stream_values,
214
     libfsntfs_mft_attribute_t *mft_attribute,
215
     libcerror_error_t **error )
216
0
{
217
0
  uint8_t *data           = NULL;
218
0
  static char *function   = "libfsntfs_logged_utility_stream_values_read_from_mft_attribute";
219
0
  size_t data_size        = 0;
220
0
  uint32_t attribute_type = 0;
221
0
  int result              = 0;
222
223
0
  if( logged_utility_stream_values == NULL )
224
0
  {
225
0
    libcerror_error_set(
226
0
     error,
227
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
228
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
229
0
     "%s: invalid logged utility stream values.",
230
0
     function );
231
232
0
    return( -1 );
233
0
  }
234
0
  if( libfsntfs_mft_attribute_get_type(
235
0
       mft_attribute,
236
0
       &attribute_type,
237
0
       error ) != 1 )
238
0
  {
239
0
    libcerror_error_set(
240
0
     error,
241
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
242
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
243
0
     "%s: unable to retrieve type from attribute.",
244
0
     function );
245
246
0
    return( -1 );
247
0
  }
248
0
  if( attribute_type != LIBFSNTFS_ATTRIBUTE_TYPE_LOGGED_UTILITY_STREAM )
249
0
  {
250
0
    libcerror_error_set(
251
0
     error,
252
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
253
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
254
0
     "%s: unsupported attribute type.",
255
0
     function );
256
257
0
    return( -1 );
258
0
  }
259
0
  result = libfsntfs_mft_attribute_data_is_resident(
260
0
            mft_attribute,
261
0
            error );
262
263
0
  if( result == -1 )
264
0
  {
265
0
    libcerror_error_set(
266
0
     error,
267
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
268
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
269
0
     "%s: unable to determine if attribute data is resident.",
270
0
     function );
271
272
0
    return( -1 );
273
0
  }
274
0
  else if( result == 0 )
275
0
  {
276
0
    libcerror_error_set(
277
0
     error,
278
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
279
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
280
0
     "%s: unsupported non-resident attribute.",
281
0
     function );
282
283
0
    return( 1 );
284
0
  }
285
0
  if( libfsntfs_mft_attribute_get_resident_data(
286
0
       mft_attribute,
287
0
       &data,
288
0
       &data_size,
289
0
       error ) != 1 )
290
0
  {
291
0
    libcerror_error_set(
292
0
     error,
293
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
294
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
295
0
     "%s: unable to retrieve resident data from attribute.",
296
0
     function );
297
298
0
    return( -1 );
299
0
  }
300
0
  if( libfsntfs_logged_utility_stream_values_read_data(
301
0
       logged_utility_stream_values,
302
0
       data,
303
0
       data_size,
304
0
       error ) != 1 )
305
0
  {
306
0
    libcerror_error_set(
307
0
     error,
308
0
     LIBCERROR_ERROR_DOMAIN_IO,
309
0
     LIBCERROR_IO_ERROR_READ_FAILED,
310
0
     "%s: unable to read logged utility stream values.",
311
0
     function );
312
313
0
    return( -1 );
314
0
  }
315
0
  return( 1 );
316
0
}
317