Coverage Report

Created: 2024-02-25 07:20

/src/libphdi/libphdi/libphdi_disk_parameters.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Disk parameters functions
3
 *
4
 * Copyright (C) 2015-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 <memory.h>
24
#include <types.h>
25
26
#include "libphdi_disk_parameters.h"
27
#include "libphdi_libcerror.h"
28
#include "libphdi_libuna.h"
29
30
/* Creates disk parameters
31
 * Make sure the value disk_parameters is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libphdi_disk_parameters_initialize(
35
     libphdi_disk_parameters_t **disk_parameters,
36
     libcerror_error_t **error )
37
82
{
38
82
  static char *function = "libphdi_disk_parameters_initialize";
39
40
82
  if( disk_parameters == NULL )
41
0
  {
42
0
    libcerror_error_set(
43
0
     error,
44
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
45
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
46
0
     "%s: invalid disk parameters.",
47
0
     function );
48
49
0
    return( -1 );
50
0
  }
51
82
  if( *disk_parameters != NULL )
52
0
  {
53
0
    libcerror_error_set(
54
0
     error,
55
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
56
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
57
0
     "%s: invalid disk parameters value already set.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
82
  *disk_parameters = memory_allocate_structure(
63
82
                      libphdi_disk_parameters_t );
64
65
82
  if( *disk_parameters == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
70
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
71
0
     "%s: unable to create disk parameters.",
72
0
     function );
73
74
0
    goto on_error;
75
0
  }
76
82
  if( memory_set(
77
82
       *disk_parameters,
78
82
       0,
79
82
       sizeof( libphdi_disk_parameters_t ) ) == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
85
0
     "%s: unable to clear disk parameters.",
86
0
     function );
87
88
0
    memory_free(
89
0
     *disk_parameters );
90
91
0
    *disk_parameters = NULL;
92
93
0
    return( -1 );
94
0
  }
95
82
  return( 1 );
96
97
0
on_error:
98
0
  if( *disk_parameters != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *disk_parameters );
102
103
0
    *disk_parameters = NULL;
104
0
  }
105
0
  return( -1 );
106
82
}
107
108
/* Frees a disk parameters
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libphdi_disk_parameters_free(
112
     libphdi_disk_parameters_t **disk_parameters,
113
     libcerror_error_t **error )
114
82
{
115
82
  static char *function = "libphdi_disk_parameters_free";
116
117
82
  if( disk_parameters == 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 disk parameters.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
82
  if( *disk_parameters != NULL )
129
82
  {
130
82
    if( ( *disk_parameters )->name != NULL )
131
0
    {
132
0
      memory_free(
133
0
       ( *disk_parameters )->name );
134
0
    }
135
82
    memory_free(
136
82
     *disk_parameters );
137
138
82
    *disk_parameters = NULL;
139
82
  }
140
82
  return( 1 );
141
82
}
142
143
/* Retrieves the media size
144
 * Returns 1 if successful or -1 on error
145
 */
146
int libphdi_disk_parameters_get_media_size(
147
     libphdi_disk_parameters_t *disk_parameters,
148
     size64_t *media_size,
149
     libcerror_error_t **error )
150
0
{
151
0
  static char *function = "libphdi_disk_parameters_get_media_size";
152
153
0
  if( disk_parameters == NULL )
154
0
  {
155
0
    libcerror_error_set(
156
0
     error,
157
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
158
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
159
0
     "%s: invalid file name values.",
160
0
     function );
161
162
0
    return( -1 );
163
0
  }
164
0
  if( media_size == NULL )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
169
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
170
0
     "%s: invalid media size.",
171
0
     function );
172
173
0
    return( -1 );
174
0
  }
175
0
  *media_size = disk_parameters->media_size;
176
177
0
  return( 1 );
178
0
}
179
180
/* Retrieves the size of the UTF-8 encoded name
181
 * The returned size includes the end of string character
182
 * Returns 1 if successful, 0 if not available or -1 on error
183
 */
184
int libphdi_disk_parameters_get_utf8_name_size(
185
     libphdi_disk_parameters_t *disk_parameters,
186
     size_t *utf8_string_size,
187
     libcerror_error_t **error )
188
0
{
189
0
  static char *function = "libphdi_disk_parameters_get_utf8_name_size";
190
191
0
  if( disk_parameters == NULL )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
196
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
197
0
     "%s: invalid file name values.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
0
  if( ( disk_parameters->name == NULL )
203
0
   || ( disk_parameters->name_size == 0 ) )
204
0
  {
205
0
    return( 0 );
206
0
  }
207
0
  if( libuna_utf8_string_size_from_utf8_stream(
208
0
       disk_parameters->name,
209
0
       disk_parameters->name_size,
210
0
       utf8_string_size,
211
0
       error ) != 1 )
212
0
  {
213
0
    libcerror_error_set(
214
0
     error,
215
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
216
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
217
0
     "%s: unable to retrieve UTF-8 string size.",
218
0
     function );
219
220
0
    return( -1 );
221
0
  }
222
0
  return( 1 );
223
0
}
224
225
/* Retrieves the UTF-8 encoded name
226
 * The size should include the end of string character
227
 * Returns 1 if successful, 0 if not available or -1 on error
228
 */
229
int libphdi_disk_parameters_get_utf8_name(
230
     libphdi_disk_parameters_t *disk_parameters,
231
     uint8_t *utf8_string,
232
     size_t utf8_string_size,
233
     libcerror_error_t **error )
234
0
{
235
0
  static char *function = "libphdi_disk_parameters_get_utf8_name";
236
237
0
  if( disk_parameters == NULL )
238
0
  {
239
0
    libcerror_error_set(
240
0
     error,
241
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
242
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
243
0
     "%s: invalid file name values.",
244
0
     function );
245
246
0
    return( -1 );
247
0
  }
248
0
  if( ( disk_parameters->name == NULL )
249
0
   || ( disk_parameters->name_size == 0 ) )
250
0
  {
251
0
    return( 0 );
252
0
  }
253
0
  if( libuna_utf8_string_copy_from_utf8_stream(
254
0
       utf8_string,
255
0
       utf8_string_size,
256
0
       disk_parameters->name,
257
0
       disk_parameters->name_size,
258
0
       error ) != 1 )
259
0
  {
260
0
    libcerror_error_set(
261
0
     error,
262
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
263
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
264
0
     "%s: unable to retrieve UTF-8 string.",
265
0
     function );
266
267
0
    return( -1 );
268
0
  }
269
0
  return( 1 );
270
0
}
271
272
/* Retrieves the size of the UTF-16 encoded name
273
 * The returned size includes the end of string character
274
 * Returns 1 if successful, 0 if not available or -1 on error
275
 */
276
int libphdi_disk_parameters_get_utf16_name_size(
277
     libphdi_disk_parameters_t *disk_parameters,
278
     size_t *utf16_string_size,
279
     libcerror_error_t **error )
280
0
{
281
0
  static char *function = "libphdi_disk_parameters_get_utf16_name_size";
282
283
0
  if( disk_parameters == NULL )
284
0
  {
285
0
    libcerror_error_set(
286
0
     error,
287
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
288
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
289
0
     "%s: invalid file name values.",
290
0
     function );
291
292
0
    return( -1 );
293
0
  }
294
0
  if( ( disk_parameters->name == NULL )
295
0
   || ( disk_parameters->name_size == 0 ) )
296
0
  {
297
0
    return( 0 );
298
0
  }
299
0
  if( libuna_utf16_string_size_from_utf8_stream(
300
0
       disk_parameters->name,
301
0
       disk_parameters->name_size,
302
0
       utf16_string_size,
303
0
       error ) != 1 )
304
0
  {
305
0
    libcerror_error_set(
306
0
     error,
307
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
308
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
309
0
     "%s: unable to retrieve UTF-16 string size.",
310
0
     function );
311
312
0
    return( -1 );
313
0
  }
314
0
  return( 1 );
315
0
}
316
317
/* Retrieves the UTF-16 encoded name
318
 * The size should include the end of string character
319
 * Returns 1 if successful, 0 if not available or -1 on error
320
 */
321
int libphdi_disk_parameters_get_utf16_name(
322
     libphdi_disk_parameters_t *disk_parameters,
323
     uint16_t *utf16_string,
324
     size_t utf16_string_size,
325
     libcerror_error_t **error )
326
0
{
327
0
  static char *function = "libphdi_disk_parameters_get_utf16_name";
328
329
0
  if( disk_parameters == NULL )
330
0
  {
331
0
    libcerror_error_set(
332
0
     error,
333
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
334
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
335
0
     "%s: invalid file name values.",
336
0
     function );
337
338
0
    return( -1 );
339
0
  }
340
0
  if( ( disk_parameters->name == NULL )
341
0
   || ( disk_parameters->name_size == 0 ) )
342
0
  {
343
0
    return( 0 );
344
0
  }
345
0
  if( libuna_utf16_string_copy_from_utf8_stream(
346
0
       utf16_string,
347
0
       utf16_string_size,
348
0
       disk_parameters->name,
349
0
       disk_parameters->name_size,
350
0
       error ) != 1 )
351
0
  {
352
0
    libcerror_error_set(
353
0
     error,
354
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
355
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
356
0
     "%s: unable to retrieve UTF-16 string.",
357
0
     function );
358
359
0
    return( -1 );
360
0
  }
361
0
  return( 1 );
362
0
}
363
364
/* Sets the name
365
 * Returns 1 if successful or -1 on error
366
 */
367
int libphdi_disk_parameters_set_name(
368
     libphdi_disk_parameters_t *disk_parameters,
369
     const uint8_t *name,
370
     size_t name_size,
371
     libcerror_error_t **error )
372
0
{
373
0
  static char *function = "libphdi_disk_parameters_set_name";
374
375
0
  if( disk_parameters == NULL )
376
0
  {
377
0
    libcerror_error_set(
378
0
     error,
379
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
380
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
381
0
     "%s: invalid file name values.",
382
0
     function );
383
384
0
    return( -1 );
385
0
  }
386
0
  if( disk_parameters->name != NULL )
387
0
  {
388
0
    libcerror_error_set(
389
0
     error,
390
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
391
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
392
0
     "%s: invalid disk parameters - name value already set.",
393
0
     function );
394
395
0
    return( -1 );
396
0
  }
397
0
  if( name == NULL )
398
0
  {
399
0
    libcerror_error_set(
400
0
     error,
401
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
402
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
403
0
     "%s: invalid name.",
404
0
     function );
405
406
0
    return( -1 );
407
0
  }
408
0
  if( ( name_size == 0 )
409
0
   || ( name_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
410
0
  {
411
0
    libcerror_error_set(
412
0
     error,
413
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
414
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
415
0
     "%s: invalid name size value out of bounds.",
416
0
     function );
417
418
0
    return( -1 );
419
0
  }
420
0
  disk_parameters->name = (uint8_t *) memory_allocate(
421
0
                                       sizeof( uint8_t ) * name_size );
422
423
0
  if( disk_parameters->name == NULL )
424
0
  {
425
0
    libcerror_error_set(
426
0
     error,
427
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
428
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
429
0
     "%s: unable to create name.",
430
0
     function );
431
432
0
    goto on_error;
433
0
  }
434
0
  disk_parameters->name_size = name_size;
435
436
0
  if( memory_copy(
437
0
       disk_parameters->name,
438
0
       name,
439
0
       name_size ) == NULL )
440
0
  {
441
0
    libcerror_error_set(
442
0
     error,
443
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
444
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
445
0
     "%s: unable to copy name.",
446
0
     function );
447
448
0
    goto on_error;
449
0
  }
450
0
  return( 1 );
451
452
0
on_error:
453
0
  if( disk_parameters->name != NULL )
454
0
  {
455
0
    memory_free(
456
0
     disk_parameters->name );
457
458
0
    disk_parameters->name = NULL;
459
0
  }
460
0
  disk_parameters->name_size = 0;
461
462
0
  return( -1 );
463
0
}
464