Coverage Report

Created: 2025-07-04 07:01

/src/liblnk/liblnk/liblnk_known_folder_location.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Known folder location functions
3
 *
4
 * Copyright (C) 2009-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 "liblnk_data_block.h"
28
#include "liblnk_debug.h"
29
#include "liblnk_known_folder_location.h"
30
#include "liblnk_libcerror.h"
31
#include "liblnk_libcnotify.h"
32
#include "liblnk_libfguid.h"
33
#include "liblnk_libfwsi.h"
34
35
#include "lnk_data_blocks.h"
36
37
/* Creates a known folder location
38
 * Make sure the value known_folder_location is referencing, is set to NULL
39
 * Returns 1 if successful or -1 on error
40
 */
41
int liblnk_known_folder_location_initialize(
42
     liblnk_known_folder_location_t **known_folder_location,
43
     libcerror_error_t **error )
44
25
{
45
25
  static char *function = "liblnk_known_folder_location_initialize";
46
47
25
  if( known_folder_location == NULL )
48
0
  {
49
0
    libcerror_error_set(
50
0
     error,
51
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
52
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
53
0
     "%s: invalid known folder location.",
54
0
     function );
55
56
0
    return( -1 );
57
0
  }
58
25
  if( *known_folder_location != NULL )
59
1
  {
60
1
    libcerror_error_set(
61
1
     error,
62
1
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
63
1
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
64
1
     "%s: invalid known folder location value already set.",
65
1
     function );
66
67
1
    return( -1 );
68
1
  }
69
24
  *known_folder_location = memory_allocate_structure(
70
24
                            liblnk_known_folder_location_t );
71
72
24
  if( *known_folder_location == NULL )
73
0
  {
74
0
    libcerror_error_set(
75
0
     error,
76
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
77
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
78
0
     "%s: unable to create known folder location.",
79
0
     function );
80
81
0
    goto on_error;
82
0
  }
83
24
  if( memory_set(
84
24
       *known_folder_location,
85
24
       0,
86
24
       sizeof( liblnk_known_folder_location_t ) ) == NULL )
87
0
  {
88
0
    libcerror_error_set(
89
0
     error,
90
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
91
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
92
0
     "%s: unable to clear known folder location.",
93
0
     function );
94
95
0
    goto on_error;
96
0
  }
97
24
  return( 1 );
98
99
0
on_error:
100
0
  if( *known_folder_location != NULL )
101
0
  {
102
0
    memory_free(
103
0
     *known_folder_location );
104
105
0
    *known_folder_location = NULL;
106
0
  }
107
0
  return( -1 );
108
24
}
109
110
/* Frees q known folder location
111
 * Returns 1 if successful or -1 on error
112
 */
113
int liblnk_known_folder_location_free(
114
     liblnk_known_folder_location_t **known_folder_location,
115
     libcerror_error_t **error )
116
24
{
117
24
  static char *function = "liblnk_known_folder_location_free";
118
119
24
  if( known_folder_location == 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 known folder location.",
126
0
     function );
127
128
0
    return( -1 );
129
0
  }
130
24
  if( *known_folder_location != NULL )
131
24
  {
132
24
    memory_free(
133
24
     *known_folder_location );
134
135
24
    *known_folder_location = NULL;
136
24
  }
137
24
  return( 1 );
138
24
}
139
140
/* Reads the known folder location data block
141
 * Returns 1 if successful or -1 on error
142
 */
143
int liblnk_known_folder_location_read_data_block(
144
     liblnk_known_folder_location_t *known_folder_location,
145
     const liblnk_data_block_t *data_block,
146
     libcerror_error_t **error )
147
24
{
148
24
  liblnk_internal_data_block_t *internal_data_block = NULL;
149
24
  static char *function                             = "liblnk_known_folder_location_read_data_block";
150
151
24
  if( known_folder_location == NULL )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
156
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
157
0
     "%s: invalid known folder location.",
158
0
     function );
159
160
0
    return( -1 );
161
0
  }
162
24
  if( data_block == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
167
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
168
0
     "%s: invalid data block.",
169
0
     function );
170
171
0
    return( -1 );
172
0
  }
173
24
  internal_data_block = (liblnk_internal_data_block_t *) data_block;
174
175
24
  if( liblnk_known_folder_location_read_data(
176
24
       known_folder_location,
177
24
       internal_data_block->data,
178
24
       internal_data_block->data_size,
179
24
       error ) != 1 )
180
8
  {
181
8
    libcerror_error_set(
182
8
     error,
183
8
     LIBCERROR_ERROR_DOMAIN_IO,
184
8
     LIBCERROR_IO_ERROR_READ_FAILED,
185
8
     "%s: unable to read known folder location.",
186
8
     function );
187
188
8
    return( -1 );
189
8
  }
190
16
  return( 1 );
191
24
}
192
193
/* Reads the known folder location
194
 * Returns 1 if successful or -1 on error
195
 */
196
int liblnk_known_folder_location_read_data(
197
     liblnk_known_folder_location_t *known_folder_location,
198
     const uint8_t *data,
199
     size_t data_size,
200
     libcerror_error_t **error )
201
24
{
202
24
  static char *function = "liblnk_known_folder_location_read_data";
203
204
24
  if( known_folder_location == NULL )
205
0
  {
206
0
    libcerror_error_set(
207
0
     error,
208
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
209
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
210
0
     "%s: invalid known folder location.",
211
0
     function );
212
213
0
    return( -1 );
214
0
  }
215
24
  if( data == NULL )
216
0
  {
217
0
    libcerror_error_set(
218
0
     error,
219
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
221
0
     "%s: invalid data.",
222
0
     function );
223
224
0
    return( -1 );
225
0
  }
226
24
  if( data_size < sizeof( lnk_data_block_known_folder_location_t ) )
227
8
  {
228
8
    libcerror_error_set(
229
8
     error,
230
8
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
231
8
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
232
8
     "%s: invalid data size value too small.",
233
8
     function );
234
235
8
    return( -1 );
236
8
  }
237
16
  if( data_size > (size_t) SSIZE_MAX )
238
0
  {
239
0
    libcerror_error_set(
240
0
     error,
241
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
242
0
     LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
243
0
     "%s: invalid data size value exceeds maximum.",
244
0
     function );
245
246
0
    return( -1 );
247
0
  }
248
16
  if( memory_copy(
249
16
       known_folder_location->folder_identifier,
250
16
       ( (lnk_data_block_known_folder_location_t *) data )->folder_identifier,
251
16
       16 ) == NULL )
252
0
  {
253
0
    libcerror_error_set(
254
0
     error,
255
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
256
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
257
0
     "%s: unable to copy folder identifier.",
258
0
     function );
259
260
0
    return( -1 );
261
0
  }
262
16
  byte_stream_copy_to_uint32_little_endian(
263
16
   ( (lnk_data_block_known_folder_location_t *) data )->first_child_segment_offset,
264
16
   known_folder_location->first_child_segment_offset );
265
266
#if defined( HAVE_DEBUG_OUTPUT )
267
  if( libcnotify_verbose != 0 )
268
  {
269
    if( liblnk_debug_print_guid_value(
270
         function,
271
         "known folder identifier\t\t\t",
272
         known_folder_location->folder_identifier,
273
         16,
274
         LIBFGUID_ENDIAN_LITTLE,
275
         LIBFGUID_STRING_FORMAT_FLAG_USE_LOWER_CASE,
276
         error ) != 1 )
277
    {
278
      libcerror_error_set(
279
       error,
280
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
281
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
282
       "%s: unable to print GUID value.",
283
       function );
284
285
      return( -1 );
286
    }
287
    libcnotify_printf(
288
     "%s: known folder name\t\t\t: %s\n",
289
     function,
290
     libfwsi_known_folder_identifier_get_name(
291
      known_folder_location->folder_identifier ) );
292
293
    libcnotify_printf(
294
     "%s: first child segment offset\t\t: %" PRIu32 "\n",
295
     function,
296
     known_folder_location->first_child_segment_offset );
297
298
    libcnotify_printf(
299
     "\n" );
300
  }
301
#endif
302
#if defined( HAVE_DEBUG_OUTPUT )
303
  if( libcnotify_verbose != 0 )
304
  {
305
    if( data_size > sizeof( lnk_data_block_known_folder_location_t ) )
306
    {
307
      libcnotify_printf(
308
       "%s: trailing data:\n",
309
       function );
310
      libcnotify_print_data(
311
       &( data[ sizeof( lnk_data_block_known_folder_location_t ) ] ),
312
       data_size - sizeof( lnk_data_block_known_folder_location_t ),
313
       0 );
314
    }
315
  }
316
#endif
317
16
  return( 1 );
318
16
}
319