Coverage Report

Created: 2025-06-24 07:14

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