Coverage Report

Created: 2025-06-24 07:14

/src/libewf/libewf/libewf_media_values.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Media values functions
3
 *
4
 * Copyright (C) 2006-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
25
#include "libewf_definitions.h"
26
#include "libewf_libcerror.h"
27
#include "libewf_libcnotify.h"
28
#include "libewf_media_values.h"
29
30
/* Creates media values
31
 * Make sure the value media_values is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libewf_media_values_initialize(
35
     libewf_media_values_t **media_values,
36
     libcerror_error_t **error )
37
3.21k
{
38
3.21k
  static char *function = "libewf_media_values_initialize";
39
40
3.21k
  if( media_values == 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 media values.",
47
0
     function );
48
49
0
    return( -1 );
50
0
  }
51
3.21k
  if( *media_values != 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 media values value already set.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
3.21k
  *media_values = memory_allocate_structure(
63
3.21k
                   libewf_media_values_t );
64
65
3.21k
  if( *media_values == 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 media values.",
72
0
     function );
73
74
0
    goto on_error;
75
0
  }
76
3.21k
  if( memory_set(
77
3.21k
       *media_values,
78
3.21k
       0,
79
3.21k
       sizeof( libewf_media_values_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 media values.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
3.21k
  ( *media_values )->chunk_size        = LIBEWF_MINIMUM_CHUNK_SIZE;
91
3.21k
  ( *media_values )->sectors_per_chunk = 64;
92
3.21k
  ( *media_values )->bytes_per_sector  = 512;
93
3.21k
  ( *media_values )->media_flags       = 0x01;
94
95
3.21k
  return( 1 );
96
97
0
on_error:
98
0
  if( *media_values != NULL )
99
0
  {
100
0
    memory_free(
101
0
     *media_values );
102
103
0
    *media_values = NULL;
104
0
  }
105
0
  return( -1 );
106
3.21k
}
107
108
/* Frees media values
109
 * Returns 1 if successful or -1 on error
110
 */
111
int libewf_media_values_free(
112
     libewf_media_values_t **media_values,
113
     libcerror_error_t **error )
114
3.21k
{
115
3.21k
        static char *function = "libewf_media_values_free";
116
117
3.21k
  if( media_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 media values.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
3.21k
  if( *media_values != NULL )
129
3.21k
  {
130
3.21k
    memory_free(
131
3.21k
     *media_values );
132
133
3.21k
    *media_values = NULL;
134
3.21k
  }
135
3.21k
  return( 1 );
136
3.21k
}
137
138
/* Clones the media values
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libewf_media_values_clear(
142
     libewf_media_values_t *media_values,
143
     libcerror_error_t **error )
144
1.17k
{
145
1.17k
        static char *function = "libewf_media_values_clear";
146
147
1.17k
  if( media_values == NULL )
148
0
  {
149
0
    libcerror_error_set(
150
0
     error,
151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
153
0
     "%s: invalid media values.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
1.17k
  if( memory_set(
159
1.17k
       media_values,
160
1.17k
       0,
161
1.17k
       sizeof( libewf_media_values_t ) ) == NULL )
162
0
  {
163
0
    libcerror_error_set(
164
0
     error,
165
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
166
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
167
0
     "%s: unable to clear media values.",
168
0
     function );
169
170
0
    return( -1 );
171
0
  }
172
1.17k
  media_values->chunk_size        = LIBEWF_MINIMUM_CHUNK_SIZE;
173
1.17k
  media_values->sectors_per_chunk = 64;
174
1.17k
  media_values->bytes_per_sector  = 512;
175
1.17k
  media_values->media_flags       = 0x01;
176
177
1.17k
  return( 1 );
178
1.17k
}
179
180
/* Clones the media values
181
 * Returns 1 if successful or -1 on error
182
 */
183
int libewf_media_values_clone(
184
     libewf_media_values_t **destination_media_values,
185
     libewf_media_values_t *source_media_values,
186
     libcerror_error_t **error )
187
0
{
188
0
  static char *function = "libewf_media_values_clone";
189
190
0
  if( destination_media_values == 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 destination media values.",
197
0
     function );
198
199
0
    return( -1 );
200
0
  }
201
0
  if( *destination_media_values != NULL )
202
0
  {
203
0
    libcerror_error_set(
204
0
     error,
205
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
206
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
207
0
     "%s: invalid destination media values already set.",
208
0
     function );
209
210
0
    return( -1 );
211
0
  }
212
0
  if( source_media_values == NULL )
213
0
  {
214
0
    *destination_media_values = NULL;
215
216
0
    return( 1 );
217
0
  }
218
0
  *destination_media_values = memory_allocate_structure(
219
0
                         libewf_media_values_t );
220
221
0
  if( *destination_media_values == NULL )
222
0
  {
223
0
    libcerror_error_set(
224
0
     error,
225
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
226
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
227
0
     "%s: unable to create destination media values.",
228
0
     function );
229
230
0
    goto on_error;
231
0
  }
232
0
  if( memory_copy(
233
0
       *destination_media_values,
234
0
       source_media_values,
235
0
       sizeof( libewf_media_values_t ) ) == NULL )
236
0
  {
237
0
    libcerror_error_set(
238
0
     error,
239
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
240
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
241
0
     "%s: unable to copy source to destination media values.",
242
0
     function );
243
244
0
    goto on_error;
245
0
  }
246
0
  return( 1 );
247
248
0
on_error:
249
0
  if( *destination_media_values != NULL )
250
0
  {
251
0
    memory_free(
252
0
     *destination_media_values );
253
254
0
    *destination_media_values = NULL;
255
0
  }
256
0
  return( -1 );
257
0
}
258
259
/* Calculate the chunk size
260
 * Returns 1 if successful or -1 on error
261
 */
262
int libewf_media_values_calculate_chunk_size(
263
     libewf_media_values_t *media_values,
264
     libcerror_error_t **error )
265
809
{
266
809
        static char *function    = "libewf_media_values_calculate_chunk_size";
267
809
  size64_t bytes_per_chunk = 0;
268
269
809
  if( media_values == NULL )
270
0
  {
271
0
    libcerror_error_set(
272
0
     error,
273
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
274
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
275
0
     "%s: invalid media values.",
276
0
     function );
277
278
0
    return( -1 );
279
0
  }  
280
809
  if( ( media_values->sectors_per_chunk == 0 )
281
809
   || ( media_values->sectors_per_chunk > (uint32_t) INT32_MAX ) )
282
2
  {
283
2
    libcerror_error_set(
284
2
     error,
285
2
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
286
2
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
287
2
     "%s: invalid sectors per chunk value out of bounds.",
288
2
     function );
289
290
2
    return( -1 );
291
2
  }
292
807
  if( ( media_values->bytes_per_sector == 0 )
293
807
   || ( media_values->bytes_per_sector > (uint32_t) INT32_MAX ) )
294
3
  {
295
3
    libcerror_error_set(
296
3
     error,
297
3
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
298
3
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
299
3
     "%s: invalid bytes per sector value out of bounds.",
300
3
     function );
301
302
3
    return( -1 );
303
3
  }
304
804
  bytes_per_chunk = (size64_t) media_values->sectors_per_chunk * media_values->bytes_per_sector;
305
306
804
  if( bytes_per_chunk > (size64_t) INT32_MAX )
307
3
  {
308
#if defined( HAVE_VERBOSE_OUTPUT )
309
    if( libcnotify_verbose != 0 )
310
    {
311
      libcnotify_printf(
312
       "%s: chunk size value exceeds maximum defaulting to: %d.\n",
313
       function,
314
       LIBEWF_MINIMUM_CHUNK_SIZE );
315
    }
316
#endif
317
3
    bytes_per_chunk = (size64_t) LIBEWF_MINIMUM_CHUNK_SIZE;
318
3
  }
319
804
  media_values->chunk_size = (uint32_t) bytes_per_chunk;
320
321
#if defined( HAVE_DEBUG_OUTPUT )
322
  if( libcnotify_verbose != 0 )
323
  {
324
    libcnotify_printf(
325
     "%s: sectors per chunk\t\t: %" PRIu32 "\n",
326
     function,
327
     media_values->sectors_per_chunk );
328
329
    libcnotify_printf(
330
     "%s: bytes per sector\t\t: %" PRIu32 "\n",
331
     function,
332
     media_values->bytes_per_sector );
333
334
    libcnotify_printf(
335
     "%s: chunk size\t\t\t: %" PRIu32 "\n",
336
     function,
337
     media_values->chunk_size );
338
339
    libcnotify_printf(
340
     "\n" );
341
  }
342
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
343
344
804
  return( 1 );
345
807
}
346