Coverage Report

Created: 2026-08-31 07:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfsext/libfsext/libfsext_bitmap.c
Line
Count
Source
1
/*
2
 * Bitmap functions
3
 *
4
 * Copyright (C) 2010-2026, 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
151
#if defined( HAVE_DEBUG_OUTPUT )
152
  int allocated_element_index       = 0;
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
#if defined( HAVE_DEBUG_OUTPUT )
250
      allocated_element_index++;
251
#endif
252
0
    }
253
0
  }
254
0
  if( in_allocated_range != 0 )
255
0
  {
256
#if defined( HAVE_DEBUG_OUTPUT )
257
    if( libcnotify_verbose != 0 )
258
    {
259
      if( first_allocated_element_index == ( allocated_element_index - 1 ) )
260
      {
261
        libcnotify_printf(
262
         "%s: allocated element\t\t\t\t: %d\n",
263
         function,
264
         first_allocated_element_index );
265
      }
266
      else
267
      {
268
        libcnotify_printf(
269
         "%s: allocated element\t\t\t\t: %d - %d\n",
270
         function,
271
         first_allocated_element_index,
272
         allocated_element_index - 1 );
273
      }
274
    }
275
#endif
276
0
  }
277
#if defined( HAVE_DEBUG_OUTPUT )
278
  if( libcnotify_verbose != 0 )
279
  {
280
    libcnotify_printf(
281
     "\n" );
282
  }
283
#endif
284
0
  return( 1 );
285
0
}
286
287
/* Reads the bitmap from a Basic File IO (bfio) handle
288
 * Returns 1 if successful or -1 on error
289
 */
290
int libfsext_bitmap_read_file_io_handle(
291
     libfsext_bitmap_t *bitmap,
292
     libfsext_io_handle_t *io_handle,
293
     libbfio_handle_t *file_io_handle,
294
     off64_t file_offset,
295
     libcerror_error_t **error )
296
0
{
297
0
  uint8_t *data         = NULL;
298
0
  static char *function = "libfsext_bitmap_read_file_io_handle";
299
0
  ssize_t read_count    = 0;
300
301
0
  if( io_handle == NULL )
302
0
  {
303
0
    libcerror_error_set(
304
0
     error,
305
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
306
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
307
0
     "%s: invalid IO handle.",
308
0
     function );
309
310
0
    return( -1 );
311
0
  }
312
0
  if( ( io_handle->block_size == 0 )
313
0
   || ( io_handle->block_size > (uint32_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
314
0
  {
315
0
    libcerror_error_set(
316
0
     error,
317
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
318
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
319
0
     "%s: invalid IO handle - block size value out of bounds.",
320
0
     function );
321
322
0
    return( -1 );
323
0
  }
324
0
  data = (uint8_t *) memory_allocate(
325
0
                      sizeof( uint8_t ) * io_handle->block_size );
326
327
0
  if( data == NULL )
328
0
  {
329
0
    libcerror_error_set(
330
0
     error,
331
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
332
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
333
0
     "%s: unable to create data.",
334
0
     function );
335
336
0
    goto on_error;
337
0
  }
338
#if defined( HAVE_DEBUG_OUTPUT )
339
  if( libcnotify_verbose != 0 )
340
  {
341
    libcnotify_printf(
342
     "%s: reading bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ")\n",
343
     function,
344
     file_offset,
345
     file_offset );
346
  }
347
#endif
348
0
  read_count = libbfio_handle_read_buffer_at_offset(
349
0
                file_io_handle,
350
0
                data,
351
0
                (size_t) io_handle->block_size,
352
0
                file_offset,
353
0
                error );
354
355
0
  if( read_count != (ssize_t) io_handle->block_size )
356
0
  {
357
0
    libcerror_error_set(
358
0
     error,
359
0
     LIBCERROR_ERROR_DOMAIN_IO,
360
0
     LIBCERROR_IO_ERROR_READ_FAILED,
361
0
     "%s: unable to read bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ").",
362
0
     function,
363
0
     file_offset,
364
0
     file_offset );
365
366
0
    goto on_error;
367
0
  }
368
0
  if( libfsext_bitmap_read_data(
369
0
       bitmap,
370
0
       data,
371
0
       (size_t) io_handle->block_size,
372
0
       error ) != 1 )
373
0
  {
374
0
    libcerror_error_set(
375
0
     error,
376
0
     LIBCERROR_ERROR_DOMAIN_IO,
377
0
     LIBCERROR_IO_ERROR_READ_FAILED,
378
0
     "%s: unable to read bitmap at offset: %" PRIi64 " (0x%08" PRIx64 ").",
379
0
     function,
380
0
     file_offset,
381
0
     file_offset );
382
383
0
    goto on_error;
384
0
  }
385
0
  memory_free(
386
0
   data );
387
388
0
  return( 1 );
389
390
0
on_error:
391
0
  if( data != NULL )
392
0
  {
393
0
    memory_free(
394
0
     data );
395
0
  }
396
0
  return( -1 );
397
0
}
398