Coverage Report

Created: 2025-07-04 07:01

/src/libfvde/libfvde/libfvde_bit_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Bit-stream functions
3
 *
4
 * Copyright (C) 2011-2024, Omar Choudary <choudary.omar@gmail.com>,
5
 *                          Joachim Metz <joachim.metz@gmail.com>
6
 *
7
 * Refer to AUTHORS for acknowledgements.
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
#include <common.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfvde_bit_stream.h"
28
#include "libfvde_libcerror.h"
29
30
/* TODO use memory alignment in bit stream */
31
32
/* Creates a bit stream
33
 * Make sure the value bit_stream is referencing, is set to NULL
34
 * Returns 1 if successful or -1 on error
35
 */
36
int libfvde_bit_stream_initialize(
37
     libfvde_bit_stream_t **bit_stream,
38
     const uint8_t *byte_stream,
39
     size_t byte_stream_size,
40
     size_t byte_stream_offset,
41
     uint8_t storage_type,
42
     libcerror_error_t **error )
43
0
{
44
0
  static char *function = "libfvde_bit_stream_initialize";
45
46
0
  if( bit_stream == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid bit stream.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( *bit_stream != NULL )
58
0
  {
59
0
    libcerror_error_set(
60
0
     error,
61
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
62
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63
0
     "%s: invalid bit stream value already set.",
64
0
     function );
65
66
0
    return( -1 );
67
0
  }
68
0
  if( byte_stream == NULL )
69
0
  {
70
0
    libcerror_error_set(
71
0
     error,
72
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
73
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
74
0
     "%s: invalid byte stream.",
75
0
     function );
76
77
0
    return( -1 );
78
0
  }
79
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
84
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
85
0
     "%s: invalid byte stream size value exceeds maximum.",
86
0
     function );
87
88
0
    return( -1 );
89
0
  }
90
0
  if( byte_stream_offset > (size_t) SSIZE_MAX )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
95
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
96
0
     "%s: invalid byte stream offset value exceeds maximum.",
97
0
     function );
98
99
0
    return( -1 );
100
0
  }
101
0
  if( ( storage_type != LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT )
102
0
   && ( storage_type != LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_FRONT_TO_BACK ) )
103
0
  {
104
0
    libcerror_error_set(
105
0
     error,
106
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
107
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
108
0
     "%s: unsupported storage type.",
109
0
     function );
110
111
0
    return( -1 );
112
0
  }
113
0
  *bit_stream = memory_allocate_structure(
114
0
                 libfvde_bit_stream_t );
115
116
0
  if( *bit_stream == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
121
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
122
0
     "%s: unable to create bit stream.",
123
0
     function );
124
125
0
    goto on_error;
126
0
  }
127
0
  if( memory_set(
128
0
       *bit_stream,
129
0
       0,
130
0
       sizeof( libfvde_bit_stream_t ) ) == NULL )
131
0
  {
132
0
    libcerror_error_set(
133
0
     error,
134
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
135
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
136
0
     "%s: unable to clear bit stream.",
137
0
     function );
138
139
0
    goto on_error;
140
0
  }
141
0
  ( *bit_stream )->byte_stream        = byte_stream;
142
0
  ( *bit_stream )->byte_stream_size   = byte_stream_size;
143
0
  ( *bit_stream )->byte_stream_offset = byte_stream_offset;
144
0
  ( *bit_stream )->storage_type       = storage_type;
145
146
0
  return( 1 );
147
148
0
on_error:
149
0
  if( *bit_stream != NULL )
150
0
  {
151
0
    memory_free(
152
0
     *bit_stream );
153
154
0
    *bit_stream = NULL;
155
0
  }
156
0
  return( -1 );
157
0
}
158
159
/* Frees a bit stream
160
 * Returns 1 if successful or -1 on error
161
 */
162
int libfvde_bit_stream_free(
163
     libfvde_bit_stream_t **bit_stream,
164
     libcerror_error_t **error )
165
0
{
166
0
  static char *function = "libfvde_bit_stream_free";
167
168
0
  if( bit_stream == NULL )
169
0
  {
170
0
    libcerror_error_set(
171
0
     error,
172
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
173
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
174
0
     "%s: invalid bit stream.",
175
0
     function );
176
177
0
    return( -1 );
178
0
  }
179
0
  if( *bit_stream != NULL )
180
0
  {
181
0
    memory_free(
182
0
     *bit_stream );
183
184
0
    *bit_stream = NULL;
185
0
  }
186
0
  return( 1 );
187
0
}
188
189
/* Retrieves a value from the bit stream
190
 * Returns 1 on success or -1 on error
191
 */
192
int libfvde_bit_stream_get_value(
193
     libfvde_bit_stream_t *bit_stream,
194
     uint8_t number_of_bits,
195
     uint32_t *value_32bit,
196
     libcerror_error_t **error )
197
0
{
198
0
  static char *function            = "libfvde_bit_stream_get_value";
199
0
  uint32_t read_value_32bit        = 0;
200
0
  uint32_t safe_value_32bit        = 0;
201
0
  uint8_t read_number_of_bits      = 0;
202
0
  uint8_t remaining_number_of_bits = 0;
203
204
0
  if( bit_stream == NULL )
205
0
  {
206
0
    libcerror_error_set(
207
0
     error,
208
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
209
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
210
0
     "%s: invalid bit stream.",
211
0
     function );
212
213
0
    return( -1 );
214
0
  }
215
0
  if( number_of_bits > (uint8_t) 32 )
216
0
  {
217
0
    libcerror_error_set(
218
0
     error,
219
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
221
0
     "%s: invalid number of bits value exceeds maximum.",
222
0
     function );
223
224
0
    return( -1 );
225
0
  }
226
0
  if( value_32bit == NULL )
227
0
  {
228
0
    libcerror_error_set(
229
0
     error,
230
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
231
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
232
0
     "%s: invalid 32-bit value.",
233
0
     function );
234
235
0
    return( -1 );
236
0
  }
237
0
  remaining_number_of_bits = number_of_bits;
238
239
0
  while( remaining_number_of_bits > 0 )
240
0
  {
241
0
    while( ( remaining_number_of_bits > bit_stream->bit_buffer_size )
242
0
        && ( bit_stream->bit_buffer_size <= 24 ) )
243
0
    {
244
0
      if( bit_stream->byte_stream_offset >= bit_stream->byte_stream_size )
245
0
      {
246
0
        libcerror_error_set(
247
0
         error,
248
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
249
0
         LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
250
0
         "%s: invalid byte stream offset value out of bounds.",
251
0
         function );
252
253
0
        return( -1 );
254
0
      }
255
0
      if( bit_stream->storage_type == LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT )
256
0
      {
257
0
        bit_stream->bit_buffer |= (uint32_t) bit_stream->byte_stream[ bit_stream->byte_stream_offset ] << bit_stream->bit_buffer_size;
258
0
      }
259
0
      else if( bit_stream->storage_type == LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_FRONT_TO_BACK )
260
0
      {
261
0
        bit_stream->bit_buffer <<= 8;
262
0
        bit_stream->bit_buffer  |= bit_stream->byte_stream[ bit_stream->byte_stream_offset ];
263
0
      }
264
0
      bit_stream->bit_buffer_size    += 8;
265
0
      bit_stream->byte_stream_offset += 1;
266
0
    }
267
0
    if( remaining_number_of_bits < bit_stream->bit_buffer_size )
268
0
    {
269
0
      read_number_of_bits = remaining_number_of_bits;
270
0
    }
271
0
    else
272
0
    {
273
0
      read_number_of_bits = bit_stream->bit_buffer_size;
274
0
    }
275
0
    read_value_32bit = bit_stream->bit_buffer;
276
277
0
    if( remaining_number_of_bits < number_of_bits )
278
0
    {
279
0
      safe_value_32bit <<= remaining_number_of_bits;
280
0
    }
281
0
    if( bit_stream->storage_type == LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_BACK_TO_FRONT )
282
0
    {
283
0
      if( read_number_of_bits < 32 )
284
0
      {
285
        /* On VS 2008 32-bit "~( 0xfffffffUL << 32 )" does not behave as expected
286
         */
287
0
        read_value_32bit &= ~( 0xffffffffUL << read_number_of_bits );
288
289
0
        bit_stream->bit_buffer >>= read_number_of_bits;
290
0
      }
291
0
      bit_stream->bit_buffer_size -= read_number_of_bits;
292
0
    }
293
0
    else if( bit_stream->storage_type == LIBFVDE_BIT_STREAM_STORAGE_TYPE_BYTE_FRONT_TO_BACK )
294
0
    {
295
0
      bit_stream->bit_buffer_size -= read_number_of_bits;
296
0
      read_value_32bit           >>= bit_stream->bit_buffer_size;
297
298
0
      if( bit_stream->bit_buffer_size > 0 )
299
0
      {
300
0
        bit_stream->bit_buffer &= 0xffffffffUL >> ( 32 - bit_stream->bit_buffer_size );
301
0
      }
302
0
    }
303
0
    if( bit_stream->bit_buffer_size == 0 )
304
0
    {
305
0
      bit_stream->bit_buffer = 0;
306
0
    }
307
0
    safe_value_32bit         |= read_value_32bit;
308
0
    remaining_number_of_bits -= read_number_of_bits;
309
0
  }
310
0
  *value_32bit = safe_value_32bit;
311
312
0
  return( 1 );
313
0
}
314