/src/libesedb/libfwnt/libfwnt_bit_stream.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Bit-stream functions | 
| 3 |  |  * | 
| 4 |  |  * Copyright (C) 2009-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 |  | #include <types.h> | 
| 25 |  |  | 
| 26 |  | #include "libfwnt_bit_stream.h" | 
| 27 |  | #include "libfwnt_libcerror.h" | 
| 28 |  |  | 
| 29 |  | /* Creates a bit stream | 
| 30 |  |  * Make sure the value bit_stream is referencing, is set to NULL | 
| 31 |  |  * Returns 1 if successful or -1 on error | 
| 32 |  |  */ | 
| 33 |  | int libfwnt_bit_stream_initialize( | 
| 34 |  |      libfwnt_bit_stream_t **bit_stream, | 
| 35 |  |      const uint8_t *byte_stream, | 
| 36 |  |      size_t byte_stream_size, | 
| 37 |  |      libcerror_error_t **error ) | 
| 38 | 2.34k | { | 
| 39 | 2.34k |   static char *function = "libfwnt_bit_stream_initialize"; | 
| 40 |  |  | 
| 41 | 2.34k |   if( bit_stream == NULL ) | 
| 42 | 0 |   { | 
| 43 | 0 |     libcerror_error_set( | 
| 44 | 0 |      error, | 
| 45 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 46 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 47 | 0 |      "%s: invalid bit stream.", | 
| 48 | 0 |      function ); | 
| 49 |  | 
 | 
| 50 | 0 |     return( -1 ); | 
| 51 | 0 |   } | 
| 52 | 2.34k |   if( *bit_stream != NULL ) | 
| 53 | 0 |   { | 
| 54 | 0 |     libcerror_error_set( | 
| 55 | 0 |      error, | 
| 56 | 0 |      LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 57 | 0 |      LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, | 
| 58 | 0 |      "%s: invalid bit stream value already set.", | 
| 59 | 0 |      function ); | 
| 60 |  | 
 | 
| 61 | 0 |     return( -1 ); | 
| 62 | 0 |   } | 
| 63 | 2.34k |   if( byte_stream == NULL ) | 
| 64 | 0 |   { | 
| 65 | 0 |     libcerror_error_set( | 
| 66 | 0 |      error, | 
| 67 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 68 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 69 | 0 |      "%s: invalid byte stream value.", | 
| 70 | 0 |      function ); | 
| 71 |  | 
 | 
| 72 | 0 |     return( -1 ); | 
| 73 | 0 |   } | 
| 74 | 2.34k |   if( byte_stream_size > (size_t) SSIZE_MAX ) | 
| 75 | 0 |   { | 
| 76 | 0 |     libcerror_error_set( | 
| 77 | 0 |      error, | 
| 78 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 79 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, | 
| 80 | 0 |      "%s: byte stream size value exceeds maximum.", | 
| 81 | 0 |      function ); | 
| 82 |  | 
 | 
| 83 | 0 |     return( -1 ); | 
| 84 | 0 |   } | 
| 85 | 2.34k |   *bit_stream = memory_allocate_structure( | 
| 86 | 2.34k |                  libfwnt_bit_stream_t ); | 
| 87 |  |  | 
| 88 | 2.34k |   if( *bit_stream == NULL ) | 
| 89 | 0 |   { | 
| 90 | 0 |     libcerror_error_set( | 
| 91 | 0 |      error, | 
| 92 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 93 | 0 |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT, | 
| 94 | 0 |      "%s: unable to create bit stream.", | 
| 95 | 0 |      function ); | 
| 96 |  | 
 | 
| 97 | 0 |     goto on_error; | 
| 98 | 0 |   } | 
| 99 | 2.34k |   if( memory_set( | 
| 100 | 2.34k |        *bit_stream, | 
| 101 | 2.34k |        0, | 
| 102 | 2.34k |        sizeof( libfwnt_bit_stream_t ) ) == NULL ) | 
| 103 | 0 |   { | 
| 104 | 0 |     libcerror_error_set( | 
| 105 | 0 |      error, | 
| 106 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 107 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 108 | 0 |      "%s: unable to clear bit stream.", | 
| 109 | 0 |      function ); | 
| 110 |  | 
 | 
| 111 | 0 |     goto on_error; | 
| 112 | 0 |   } | 
| 113 | 2.34k |   ( *bit_stream )->byte_stream      = byte_stream; | 
| 114 | 2.34k |   ( *bit_stream )->byte_stream_size = byte_stream_size; | 
| 115 |  |  | 
| 116 | 2.34k |   return( 1 ); | 
| 117 |  |  | 
| 118 | 0 | on_error: | 
| 119 | 0 |   if( *bit_stream != NULL ) | 
| 120 | 0 |   { | 
| 121 | 0 |     memory_free( | 
| 122 | 0 |      *bit_stream ); | 
| 123 |  | 
 | 
| 124 | 0 |     *bit_stream = NULL; | 
| 125 | 0 |   } | 
| 126 | 0 |   return( -1 ); | 
| 127 | 2.34k | } | 
| 128 |  |  | 
| 129 |  | /* Frees a bit stream | 
| 130 |  |  * Returns 1 if successful or -1 on error | 
| 131 |  |  */ | 
| 132 |  | int libfwnt_bit_stream_free( | 
| 133 |  |      libfwnt_bit_stream_t **bit_stream, | 
| 134 |  |      libcerror_error_t **error ) | 
| 135 | 2.34k | { | 
| 136 | 2.34k |   static char *function = "libfwnt_bit_stream_free"; | 
| 137 |  |  | 
| 138 | 2.34k |   if( bit_stream == NULL ) | 
| 139 | 0 |   { | 
| 140 | 0 |     libcerror_error_set( | 
| 141 | 0 |      error, | 
| 142 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 143 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 144 | 0 |      "%s: invalid bit stream.", | 
| 145 | 0 |      function ); | 
| 146 |  | 
 | 
| 147 | 0 |     return( -1 ); | 
| 148 | 0 |   } | 
| 149 | 2.34k |   if( *bit_stream != NULL ) | 
| 150 | 2.34k |   { | 
| 151 | 2.34k |     memory_free( | 
| 152 | 2.34k |      *bit_stream ); | 
| 153 |  |  | 
| 154 | 2.34k |     *bit_stream = NULL; | 
| 155 | 2.34k |   } | 
| 156 | 2.34k |   return( 1 ); | 
| 157 | 2.34k | } | 
| 158 |  |  | 
| 159 |  | /* Reads bits from the underlying byte stream | 
| 160 |  |  * Returns 1 on success or -1 on error | 
| 161 |  |  */ | 
| 162 |  | int libfwnt_bit_stream_read( | 
| 163 |  |      libfwnt_bit_stream_t *bit_stream, | 
| 164 |  |      uint8_t number_of_bits, | 
| 165 |  |      libcerror_error_t **error ) | 
| 166 | 9.75M | { | 
| 167 | 9.75M |   static char *function = "libfwnt_bit_stream_read"; | 
| 168 |  |  | 
| 169 | 9.75M |   if( bit_stream == NULL ) | 
| 170 | 0 |   { | 
| 171 | 0 |     libcerror_error_set( | 
| 172 | 0 |      error, | 
| 173 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 174 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 175 | 0 |      "%s: invalid bit stream.", | 
| 176 | 0 |      function ); | 
| 177 |  | 
 | 
| 178 | 0 |     return( -1 ); | 
| 179 | 0 |   } | 
| 180 | 9.75M |   if( ( number_of_bits == 0 ) | 
| 181 | 9.75M |    || ( number_of_bits > 32 ) ) | 
| 182 | 0 |   { | 
| 183 | 0 |     libcerror_error_set( | 
| 184 | 0 |      error, | 
| 185 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 186 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 187 | 0 |      "%s: number of bits value out of bounds.", | 
| 188 | 0 |      function ); | 
| 189 |  | 
 | 
| 190 | 0 |     return( -1 ); | 
| 191 | 0 |   } | 
| 192 | 19.5M |   while( bit_stream->bit_buffer_size < number_of_bits ) | 
| 193 | 9.76M |   { | 
| 194 | 9.76M |     if( ( bit_stream->byte_stream_size < 2 ) | 
| 195 | 9.76M |      || ( bit_stream->byte_stream_offset > ( bit_stream->byte_stream_size - 2 ) ) ) | 
| 196 | 2.41M |     { | 
| 197 |  |       /* If the bit stream overflows fill the bit buffer with 0 byte values | 
| 198 |  |        */ | 
| 199 | 2.41M |       bit_stream->bit_buffer     <<= 16; | 
| 200 | 2.41M |       bit_stream->bit_buffer_size += 16; | 
| 201 | 2.41M |     } | 
| 202 | 7.35M |     else | 
| 203 | 7.35M |     { | 
| 204 | 7.35M |       bit_stream->bit_buffer     <<= 8; | 
| 205 | 7.35M |       bit_stream->bit_buffer      |= bit_stream->byte_stream[ bit_stream->byte_stream_offset + 1 ]; | 
| 206 | 7.35M |       bit_stream->bit_buffer     <<= 8; | 
| 207 | 7.35M |       bit_stream->bit_buffer      |= bit_stream->byte_stream[ bit_stream->byte_stream_offset ]; | 
| 208 | 7.35M |       bit_stream->bit_buffer_size += 16; | 
| 209 |  |  | 
| 210 | 7.35M |       bit_stream->byte_stream_offset += 2; | 
| 211 | 7.35M |     } | 
| 212 | 9.76M |   } | 
| 213 | 9.75M |   return( 1 ); | 
| 214 | 9.75M | } | 
| 215 |  |  | 
| 216 |  | /* Retrieves a value from the bit stream | 
| 217 |  |  * Returns 1 on success or -1 on error | 
| 218 |  |  */ | 
| 219 |  | int libfwnt_bit_stream_get_value( | 
| 220 |  |      libfwnt_bit_stream_t *bit_stream, | 
| 221 |  |      uint8_t number_of_bits, | 
| 222 |  |      uint32_t *value_32bit, | 
| 223 |  |      libcerror_error_t **error ) | 
| 224 | 128M | { | 
| 225 | 128M |   static char *function             = "libfwnt_bit_stream_get_value"; | 
| 226 | 128M |   uint32_t safe_value_32bit         = 0; | 
| 227 | 128M |   uint8_t remaining_bit_buffer_size = 0; | 
| 228 |  |  | 
| 229 | 128M |   if( bit_stream == NULL ) | 
| 230 | 0 |   { | 
| 231 | 0 |     libcerror_error_set( | 
| 232 | 0 |      error, | 
| 233 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 234 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 235 | 0 |      "%s: invalid bit stream.", | 
| 236 | 0 |      function ); | 
| 237 |  | 
 | 
| 238 | 0 |     return( -1 ); | 
| 239 | 0 |   } | 
| 240 | 128M |   if( number_of_bits > (uint8_t) 32 ) | 
| 241 | 0 |   { | 
| 242 | 0 |     libcerror_error_set( | 
| 243 | 0 |      error, | 
| 244 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 245 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, | 
| 246 | 0 |      "%s: invalid number of bits value exceeds maximum.", | 
| 247 | 0 |      function ); | 
| 248 |  | 
 | 
| 249 | 0 |     return( -1 ); | 
| 250 | 0 |   } | 
| 251 | 128M |   if( value_32bit == NULL ) | 
| 252 | 0 |   { | 
| 253 | 0 |     libcerror_error_set( | 
| 254 | 0 |      error, | 
| 255 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 256 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 257 | 0 |      "%s: invalid 32-bit value.", | 
| 258 | 0 |      function ); | 
| 259 |  | 
 | 
| 260 | 0 |     return( -1 ); | 
| 261 | 0 |   } | 
| 262 | 128M |   if( number_of_bits == 0 ) | 
| 263 | 57.1k |   { | 
| 264 | 57.1k |     *value_32bit = 0; | 
| 265 |  |  | 
| 266 | 57.1k |     return( 1 ); | 
| 267 | 57.1k |   } | 
| 268 | 128M |   if( bit_stream->bit_buffer_size < number_of_bits ) | 
| 269 | 54.0k |   { | 
| 270 | 54.0k |     if( libfwnt_bit_stream_read( | 
| 271 | 54.0k |          bit_stream, | 
| 272 | 54.0k |          number_of_bits, | 
| 273 | 54.0k |          error ) != 1 ) | 
| 274 | 0 |     { | 
| 275 | 0 |       libcerror_error_set( | 
| 276 | 0 |        error, | 
| 277 | 0 |        LIBCERROR_ERROR_DOMAIN_IO, | 
| 278 | 0 |        LIBCERROR_IO_ERROR_READ_FAILED, | 
| 279 | 0 |        "%s: unable to read bits.", | 
| 280 | 0 |        function ); | 
| 281 |  | 
 | 
| 282 | 0 |       return( -1 ); | 
| 283 | 0 |     } | 
| 284 | 54.0k |   } | 
| 285 | 128M |   safe_value_32bit = bit_stream->bit_buffer; | 
| 286 |  |  | 
| 287 | 128M |   if( number_of_bits < 32 ) | 
| 288 | 128M |   { | 
| 289 | 128M |     safe_value_32bit >>= bit_stream->bit_buffer_size - number_of_bits; | 
| 290 | 128M |   } | 
| 291 | 128M |   bit_stream->bit_buffer_size -= number_of_bits; | 
| 292 |  |  | 
| 293 |  |   /* The behavior of "bit_buffer & ( 0xfffffffUL >> 32 )" differs for some compilers. | 
| 294 |  |    */ | 
| 295 | 128M |   if( bit_stream->bit_buffer_size == 0 ) | 
| 296 | 203k |   { | 
| 297 | 203k |     bit_stream->bit_buffer = 0; | 
| 298 | 203k |   } | 
| 299 | 127M |   else | 
| 300 | 127M |   { | 
| 301 | 127M |     remaining_bit_buffer_size = 32 - bit_stream->bit_buffer_size; | 
| 302 | 127M |     bit_stream->bit_buffer   &= 0xffffffffUL >> remaining_bit_buffer_size; | 
| 303 | 127M |   } | 
| 304 | 128M |   *value_32bit = safe_value_32bit; | 
| 305 |  |  | 
| 306 | 128M |   return( 1 ); | 
| 307 | 128M | } | 
| 308 |  |  |