Coverage Report

Created: 2025-06-24 07:14

/src/libmodi/libmodi/libmodi_udif_block_table_entry.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Universal Disk Image Format (UDIF) block table entry functions
3
 *
4
 * Copyright (C) 2012-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 "libmodi_debug.h"
28
#include "libmodi_libcerror.h"
29
#include "libmodi_libcnotify.h"
30
#include "libmodi_udif_block_table_entry.h"
31
32
#include "modi_udif_block_table.h"
33
34
/* Creates a block table entry entry
35
 * Make sure the value block_table_entry is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libmodi_udif_block_table_entry_initialize(
39
     libmodi_udif_block_table_entry_t **block_table_entry,
40
     libcerror_error_t **error )
41
4.64k
{
42
4.64k
  static char *function = "libmodi_udif_block_table_entry_initialize";
43
44
4.64k
  if( block_table_entry == 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 block table entry.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
4.64k
  if( *block_table_entry != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid block table entry value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
4.64k
  *block_table_entry = memory_allocate_structure(
67
4.64k
                        libmodi_udif_block_table_entry_t );
68
69
4.64k
  if( *block_table_entry == 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 block table entry.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
4.64k
  if( memory_set(
81
4.64k
       *block_table_entry,
82
4.64k
       0,
83
4.64k
       sizeof( libmodi_udif_block_table_entry_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 block table entry.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
4.64k
  return( 1 );
95
96
0
on_error:
97
0
  if( *block_table_entry != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *block_table_entry );
101
102
0
    *block_table_entry = NULL;
103
0
  }
104
0
  return( -1 );
105
4.64k
}
106
107
/* Frees a block table entry
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libmodi_udif_block_table_entry_free(
111
     libmodi_udif_block_table_entry_t **block_table_entry,
112
     libcerror_error_t **error )
113
4.64k
{
114
4.64k
  static char *function = "libmodi_udif_block_table_entry_free";
115
116
4.64k
  if( block_table_entry == 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 block table entry.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
4.64k
  if( *block_table_entry != NULL )
128
4.64k
  {
129
4.64k
    memory_free(
130
4.64k
     *block_table_entry );
131
132
4.64k
    *block_table_entry = NULL;
133
4.64k
  }
134
4.64k
  return( 1 );
135
4.64k
}
136
137
/* Reads a block table entry
138
 * Returns 1 if successful or -1 on error
139
 */
140
int libmodi_udif_block_table_entry_read_data(
141
     libmodi_udif_block_table_entry_t *block_table_entry,
142
     const uint8_t *data,
143
     size_t data_size,
144
     libcerror_error_t **error )
145
4.64k
{
146
4.64k
  static char *function = "libmodi_udif_block_table_entry_read_data";
147
148
#if defined( HAVE_DEBUG_OUTPUT )
149
  uint32_t value_32bit  = 0;
150
#endif
151
152
4.64k
  if( block_table_entry == NULL )
153
0
  {
154
0
    libcerror_error_set(
155
0
     error,
156
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
157
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
158
0
     "%s: invalid block table entry.",
159
0
     function );
160
161
0
    return( -1 );
162
0
  }
163
4.64k
  if( data == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
168
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
169
0
     "%s: invalid data.",
170
0
     function );
171
172
0
    return( -1 );
173
0
  }
174
4.64k
  if( ( data_size < sizeof( modi_udif_block_table_entry_t ) )
175
4.64k
   || ( data_size > (size_t) SSIZE_MAX ) )
176
0
  {
177
0
    libcerror_error_set(
178
0
     error,
179
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
180
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
181
0
     "%s: invalid data size value out of bounds.",
182
0
     function );
183
184
0
    return( -1 );
185
0
  }
186
#if defined( HAVE_DEBUG_OUTPUT )
187
  if( libcnotify_verbose != 0 )
188
  {
189
    libcnotify_printf(
190
     "%s: block table entry data:\n",
191
     function );
192
    libcnotify_print_data(
193
     data,
194
     data_size,
195
     0 );
196
  }
197
#endif
198
4.64k
  byte_stream_copy_to_uint32_big_endian(
199
4.64k
   ( (modi_udif_block_table_entry_t *) data )->type,
200
4.64k
   block_table_entry->type );
201
202
4.64k
  byte_stream_copy_to_uint64_big_endian(
203
4.64k
   ( (modi_udif_block_table_entry_t *) data )->start_sector,
204
4.64k
   block_table_entry->start_sector );
205
206
4.64k
  byte_stream_copy_to_uint64_big_endian(
207
4.64k
   ( (modi_udif_block_table_entry_t *) data )->number_of_sectors,
208
4.64k
   block_table_entry->number_of_sectors );
209
210
4.64k
  byte_stream_copy_to_uint64_big_endian(
211
4.64k
   ( (modi_udif_block_table_entry_t *) data )->data_offset,
212
4.64k
   block_table_entry->data_offset );
213
214
4.64k
  byte_stream_copy_to_uint64_big_endian(
215
4.64k
   ( (modi_udif_block_table_entry_t *) data )->data_size,
216
4.64k
   block_table_entry->data_size );
217
218
#if defined( HAVE_DEBUG_OUTPUT )
219
  if( libcnotify_verbose != 0 )
220
  {
221
    libcnotify_printf(
222
     "%s: type\t\t\t\t: 0x%08" PRIx32 " (%s)\n",
223
     function,
224
     block_table_entry->type,
225
     libmodi_debug_get_udif_block_table_entry_type(
226
      block_table_entry->type ) );
227
228
    byte_stream_copy_to_uint32_big_endian(
229
     ( (modi_udif_block_table_entry_t *) data )->unknown1,
230
     value_32bit );
231
    libcnotify_printf(
232
     "%s: unknown1\t\t\t: 0x%08" PRIx32 "\n",
233
     function,
234
     value_32bit );
235
236
    libcnotify_printf(
237
     "%s: start sector\t\t\t: %" PRIu64 "\n",
238
     function,
239
     block_table_entry->start_sector );
240
241
    libcnotify_printf(
242
     "%s: number of sectors\t\t: %" PRIu64 "\n",
243
     function,
244
     block_table_entry->number_of_sectors );
245
246
    libcnotify_printf(
247
     "%s: data offset\t\t\t: %" PRIu64 "\n",
248
     function,
249
     block_table_entry->data_offset );
250
251
    libcnotify_printf(
252
     "%s: data size\t\t\t: %" PRIu64 "\n",
253
     function,
254
     block_table_entry->data_size );
255
256
    libcnotify_printf(
257
     "\n" );
258
  }
259
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
260
261
4.64k
  return( 1 );
262
4.64k
}
263