Coverage Report

Created: 2024-02-25 07:19

/src/libfsext/libfsext/libfsext_bitmap.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Bitmap functions
3
 *
4
 * Copyright (C) 2010-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 "libfsext_bitmap.h"
28
#include "libfsext_io_handle.h"
29
#include "libfsext_libbfio.h"
30
#include "libfsext_libcerror.h"
31
#include "libfsext_libcnotify.h"
32
33
/* Creates a bitmap
34
 * Make sure the value bitmap is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfsext_bitmap_initialize(
38
     libfsext_bitmap_t **bitmap,
39
     libcerror_error_t **error )
40
0
{
41
0
  static char *function = "libfsext_bitmap_initialize";
42
43
0
  if( bitmap == NULL )
44
0
  {
45
0
    libcerror_error_set(
46
0
     error,
47
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
48
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
49
0
     "%s: invalid bitmap.",
50
0
     function );
51
52
0
    return( -1 );
53
0
  }
54
0
  if( *bitmap != NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
59
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
60
0
     "%s: invalid bitmap value already set.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
0
  *bitmap = memory_allocate_structure(
66
0
             libfsext_bitmap_t );
67
68
0
  if( *bitmap == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
73
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
74
0
     "%s: unable to create bitmap.",
75
0
     function );
76
77
0
    goto on_error;
78
0
  }
79
0
  if( memory_set(
80
0
       *bitmap,
81
0
       0,
82
0
       sizeof( libfsext_bitmap_t ) ) == NULL )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
87
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
88
0
     "%s: unable to clear bitmap.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
0
  return( 1 );
94
95
0
on_error:
96
0
  if( *bitmap != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *bitmap );
100
101
0
    *bitmap = NULL;
102
0
  }
103
0
  return( -1 );
104
0
}
105
106
/* Frees a bitmap
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libfsext_bitmap_free(
110
     libfsext_bitmap_t **bitmap,
111
     libcerror_error_t **error )
112
0
{
113
0
  static char *function = "libfsext_bitmap_free";
114
115
0
  if( bitmap == NULL )
116
0
  {
117
0
    libcerror_error_set(
118
0
     error,
119
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
120
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
121
0
     "%s: invalid bitmap.",
122
0
     function );
123
124
0
    return( -1 );
125
0
  }
126
0
  if( *bitmap != NULL )
127
0
  {
128
0
    memory_free(
129
0
     *bitmap );
130
131
0
    *bitmap = NULL;
132
0
  }
133
0
  return( 1 );
134
0
}
135
136
/* Reads the bitmap data
137
 * Returns 1 if successful or -1 on error
138
 */
139
int libfsext_bitmap_read_data(
140
     libfsext_bitmap_t *bitmap,
141
     const uint8_t *data,
142
     size_t data_size,
143
     libcerror_error_t **error )
144
0
{
145
0
  static char *function             = "libfsext_bitmap_read_data";
146
0
  size_t data_offset                = 0;
147
0
  uint8_t bit_index                 = 0;
148
0
  uint8_t byte_value                = 0;
149
0
  uint8_t in_allocated_range        = 0;
150
0
  int allocated_element_index       = 0;
151
152
#if defined( HAVE_DEBUG_OUTPUT )
153
  int first_allocated_element_index = 0;
154
#endif
155
156
0
  if( bitmap == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid bitmap.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
0
  if( data == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid data.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
0
  if( ( data_size == 0 )
179
0
   || ( data_size > (size_t) SSIZE_MAX ) )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
184
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
185
0
     "%s: invalid data size value out of bounds.",
186
0
     function );
187
188
0
    return( -1 );
189
0
  }
190
#if defined( HAVE_DEBUG_OUTPUT )
191
  if( libcnotify_verbose != 0 )
192
  {
193
    libcnotify_printf(
194
     "%s: bitmap data:\n",
195
     function );
196
    libcnotify_print_data(
197
     data,
198
     data_size,
199
     LIBCNOTIFY_PRINT_DATA_FLAG_GROUP_DATA );
200
  }
201
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
202
203
0
  while( data_offset < data_size )
204
0
  {
205
0
    byte_value = data[ data_offset ];
206
207
0
    data_offset++;
208
209
0
    for( bit_index = 0;
210
0
         bit_index < 8;
211
0
         bit_index++ )
212
0
    {
213
0
      if( in_allocated_range == 0 )
214
0
      {
215
0
        if( ( byte_value & 0x01 ) != 0 )
216
0
        {
217
0
          in_allocated_range            = 1;
218
#if defined( HAVE_DEBUG_OUTPUT )
219
          first_allocated_element_index = allocated_element_index;
220
#endif
221
0
        }
222
0
      }
223
0
      else if( ( byte_value & 0x01 ) == 0 )
224
0
      {
225
#if defined( HAVE_DEBUG_OUTPUT )
226
        if( libcnotify_verbose != 0 )
227
        {
228
          if( first_allocated_element_index == ( allocated_element_index - 1 ) )
229
          {
230
            libcnotify_printf(
231
             "%s: allocated element\t\t\t\t: %d\n",
232
             function,
233
             first_allocated_element_index );
234
          }
235
          else
236
          {
237
            libcnotify_printf(
238
             "%s: allocated element\t\t\t\t: %d - %d\n",
239
             function,
240
             first_allocated_element_index,
241
             allocated_element_index - 1 );
242
          }
243
        }
244
#endif
245
0
        in_allocated_range = 0;
246
0
      }
247
0
      byte_value >>= 1;
248
249
0
      allocated_element_index++;
250
0
    }
251
0
  }
252
0
  if( in_allocated_range != 0 )
253
0
  {
254
#if defined( HAVE_DEBUG_OUTPUT )
255
    if( libcnotify_verbose != 0 )
256
    {
257
      if( first_allocated_element_index == ( allocated_element_index - 1 ) )
258
      {
259
        libcnotify_printf(
260
         "%s: allocated element\t\t\t\t: %d\n",
261
         function,
262
         first_allocated_element_index );
263
      }
264
      else
265
      {
266
        libcnotify_printf(
267
         "%s: allocated element\t\t\t\t: %d - %d\n",
268
         function,
269
         first_allocated_element_index,
270
         allocated_element_index - 1 );
271
      }
272
    }
273
#endif
274
0
  }
275
#if defined( HAVE_DEBUG_OUTPUT )
276
  if( libcnotify_verbose != 0 )
277
  {
278
    libcnotify_printf(
279
     "\n" );
280
  }
281
#endif
282
0
  return( 1 );
283
0
}
284
285
/* Reads the bitmap from a Basic File IO (bfio) handle
286
 * Returns 1 if successful or -1 on error
287
 */
288
int libfsext_bitmap_read_file_io_handle(
289
     libfsext_bitmap_t *bitmap,
290
     libfsext_io_handle_t *io_handle,
291
     libbfio_handle_t *file_io_handle,
292
     off64_t file_offset,
293
     libcerror_error_t **error )
294
0
{
295
0
  uint8_t *data         = NULL;
296
0
  static char *function = "libfsext_bitmap_read_file_io_handle";
297
0
  ssize_t read_count    = 0;
298
299
0
  if( io_handle == NULL )
300
0
  {
301
0
    libcerror_error_set(
302
0
     error,
303
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
304
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
305
0
     "%s: invalid IO handle.",
306
0
     function );
307
308
0
    return( -1 );
309
0
  }
310
0
  if( ( io_handle->block_size == 0 )
311
0
   || ( io_handle->block_size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
316
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
317
0
     "%s: invalid IO handle - block size value out of bounds.",
318
0
     function );
319
320
0
    return( -1 );
321
0
  }
322
0
  data = (uint8_t *) memory_allocate(
323
0
                      sizeof( uint8_t ) * io_handle->block_size );
324
325
0
  if( data == NULL )
326
0
  {
327
0
    libcerror_error_set(
328
0
     error,
329
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
330
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
331
0
     "%s: unable to create data.",
332
0
     function );
333
334
0
    goto on_error;
335
0
  }
336
#if defined( HAVE_DEBUG_OUTPUT )
337
  if( libcnotify_verbose != 0 )
338
  {
339
    libcnotify_printf(
340
     "%s: reading bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
341
     function,
342
     file_offset,
343
     file_offset );
344
  }
345
#endif
346
0
  read_count = libbfio_handle_read_buffer_at_offset(
347
0
                file_io_handle,
348
0
                data,
349
0
                (size_t) io_handle->block_size,
350
0
                file_offset,
351
0
                error );
352
353
0
  if( read_count != (ssize_t) io_handle->block_size )
354
0
  {
355
0
    libcerror_error_set(
356
0
     error,
357
0
     LIBCERROR_ERROR_DOMAIN_IO,
358
0
     LIBCERROR_IO_ERROR_READ_FAILED,
359
0
     "%s: unable to read bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ").",
360
0
     function,
361
0
     file_offset,
362
0
     file_offset );
363
364
0
    goto on_error;
365
0
  }
366
0
  if( libfsext_bitmap_read_data(
367
0
       bitmap,
368
0
       data,
369
0
       (size_t) io_handle->block_size,
370
0
       error ) != 1 )
371
0
  {
372
0
    libcerror_error_set(
373
0
     error,
374
0
     LIBCERROR_ERROR_DOMAIN_IO,
375
0
     LIBCERROR_IO_ERROR_READ_FAILED,
376
0
     "%s: unable to read bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ").",
377
0
     function,
378
0
     file_offset,
379
0
     file_offset );
380
381
0
    goto on_error;
382
0
  }
383
0
  memory_free(
384
0
   data );
385
386
0
  return( 1 );
387
388
0
on_error:
389
0
  if( data != NULL )
390
0
  {
391
0
    memory_free(
392
0
     data );
393
0
  }
394
0
  return( -1 );
395
0
}
396