/src/libscca/libfwnt/libfwnt_huffman_tree.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Huffman tree 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_huffman_tree.h" | 
| 28 |  | #include "libfwnt_libcerror.h" | 
| 29 |  | #include "libfwnt_libcnotify.h" | 
| 30 |  |  | 
| 31 |  | /* Creates a Huffman tree | 
| 32 |  |  * Make sure the value huffman_tree is referencing, is set to NULL | 
| 33 |  |  * Returns 1 if successful or -1 on error | 
| 34 |  |  */ | 
| 35 |  | int libfwnt_huffman_tree_initialize( | 
| 36 |  |      libfwnt_huffman_tree_t **huffman_tree, | 
| 37 |  |      int number_of_symbols, | 
| 38 |  |      uint8_t maximum_code_size, | 
| 39 |  |      libcerror_error_t **error ) | 
| 40 | 22.6k | { | 
| 41 | 22.6k |   static char *function = "libfwnt_huffman_tree_initialize"; | 
| 42 | 22.6k |   size_t array_size     = 0; | 
| 43 |  |  | 
| 44 | 22.6k |   if( huffman_tree == 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 Huffman tree.", | 
| 51 | 0 |      function ); | 
| 52 |  | 
 | 
| 53 | 0 |     return( -1 ); | 
| 54 | 0 |   } | 
| 55 | 22.6k |   if( *huffman_tree != 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 Huffman tree value already set.", | 
| 62 | 0 |      function ); | 
| 63 |  | 
 | 
| 64 | 0 |     return( -1 ); | 
| 65 | 0 |   } | 
| 66 | 22.6k |   if( ( number_of_symbols < 0 ) | 
| 67 | 22.6k |    || ( number_of_symbols > 1024 ) ) | 
| 68 | 0 |   { | 
| 69 | 0 |     libcerror_error_set( | 
| 70 | 0 |      error, | 
| 71 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 72 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 73 | 0 |      "%s: invalid number of symbols value out of bounds.", | 
| 74 | 0 |      function ); | 
| 75 |  | 
 | 
| 76 | 0 |     return( -1 ); | 
| 77 | 0 |   } | 
| 78 | 22.6k |   if( maximum_code_size > 32 ) | 
| 79 | 0 |   { | 
| 80 | 0 |     libcerror_error_set( | 
| 81 | 0 |      error, | 
| 82 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 83 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 84 | 0 |      "%s: invalid maximum code size value out of bounds.", | 
| 85 | 0 |      function ); | 
| 86 |  | 
 | 
| 87 | 0 |     return( -1 ); | 
| 88 | 0 |   } | 
| 89 | 22.6k |   *huffman_tree = memory_allocate_structure( | 
| 90 | 22.6k |                    libfwnt_huffman_tree_t ); | 
| 91 |  |  | 
| 92 | 22.6k |   if( *huffman_tree == NULL ) | 
| 93 | 0 |   { | 
| 94 | 0 |     libcerror_error_set( | 
| 95 | 0 |      error, | 
| 96 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 97 | 0 |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT, | 
| 98 | 0 |      "%s: unable to create Huffman tree.", | 
| 99 | 0 |      function ); | 
| 100 |  | 
 | 
| 101 | 0 |     goto on_error; | 
| 102 | 0 |   } | 
| 103 | 22.6k |   if( memory_set( | 
| 104 | 22.6k |        *huffman_tree, | 
| 105 | 22.6k |        0, | 
| 106 | 22.6k |        sizeof( libfwnt_huffman_tree_t ) ) == NULL ) | 
| 107 | 0 |   { | 
| 108 | 0 |     libcerror_error_set( | 
| 109 | 0 |      error, | 
| 110 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 111 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 112 | 0 |      "%s: unable to clear Huffman tree.", | 
| 113 | 0 |      function ); | 
| 114 |  | 
 | 
| 115 | 0 |     memory_free( | 
| 116 | 0 |      *huffman_tree ); | 
| 117 |  | 
 | 
| 118 | 0 |     *huffman_tree = NULL; | 
| 119 |  | 
 | 
| 120 | 0 |     return( -1 ); | 
| 121 | 0 |   } | 
| 122 | 22.6k |   array_size = sizeof( int ) * number_of_symbols; | 
| 123 |  |  | 
| 124 | 22.6k |   ( *huffman_tree )->symbols = (int *) memory_allocate( | 
| 125 | 22.6k |                                         array_size ); | 
| 126 |  |  | 
| 127 | 22.6k |   if( ( *huffman_tree )->symbols == NULL ) | 
| 128 | 0 |   { | 
| 129 | 0 |     libcerror_error_set( | 
| 130 | 0 |      error, | 
| 131 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 132 | 0 |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT, | 
| 133 | 0 |      "%s: unable to create symbols.", | 
| 134 | 0 |      function ); | 
| 135 |  | 
 | 
| 136 | 0 |     goto on_error; | 
| 137 | 0 |   } | 
| 138 | 22.6k |   if( memory_set( | 
| 139 | 22.6k |        ( *huffman_tree )->symbols, | 
| 140 | 22.6k |        0, | 
| 141 | 22.6k |        array_size ) == NULL ) | 
| 142 | 0 |   { | 
| 143 | 0 |     libcerror_error_set( | 
| 144 | 0 |      error, | 
| 145 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 146 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 147 | 0 |      "%s: unable to clear symbols.", | 
| 148 | 0 |      function ); | 
| 149 |  | 
 | 
| 150 | 0 |     goto on_error; | 
| 151 | 0 |   } | 
| 152 | 22.6k |   array_size = sizeof( int ) * ( maximum_code_size + 1 ); | 
| 153 |  |  | 
| 154 | 22.6k |   ( *huffman_tree )->code_size_counts = (int *) memory_allocate( | 
| 155 | 22.6k |                                                  array_size ); | 
| 156 |  |  | 
| 157 | 22.6k |   if( ( *huffman_tree )->code_size_counts == NULL ) | 
| 158 | 0 |   { | 
| 159 | 0 |     libcerror_error_set( | 
| 160 | 0 |      error, | 
| 161 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 162 | 0 |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT, | 
| 163 | 0 |      "%s: unable to create code size counts.", | 
| 164 | 0 |      function ); | 
| 165 |  | 
 | 
| 166 | 0 |     goto on_error; | 
| 167 | 0 |   } | 
| 168 | 22.6k |   if( memory_set( | 
| 169 | 22.6k |        ( *huffman_tree )->code_size_counts, | 
| 170 | 22.6k |        0, | 
| 171 | 22.6k |        array_size ) == NULL ) | 
| 172 | 0 |   { | 
| 173 | 0 |     libcerror_error_set( | 
| 174 | 0 |      error, | 
| 175 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 176 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 177 | 0 |      "%s: unable to clear code size counts.", | 
| 178 | 0 |      function ); | 
| 179 |  | 
 | 
| 180 | 0 |     goto on_error; | 
| 181 | 0 |   } | 
| 182 | 22.6k |   ( *huffman_tree )->maximum_code_size = maximum_code_size; | 
| 183 |  |  | 
| 184 | 22.6k |   return( 1 ); | 
| 185 |  |  | 
| 186 | 0 | on_error: | 
| 187 | 0 |   if( *huffman_tree != NULL ) | 
| 188 | 0 |   { | 
| 189 | 0 |     if( ( *huffman_tree )->code_size_counts != NULL ) | 
| 190 | 0 |     { | 
| 191 | 0 |       memory_free( | 
| 192 | 0 |        ( *huffman_tree )->code_size_counts ); | 
| 193 | 0 |     } | 
| 194 | 0 |     if( ( *huffman_tree )->symbols != NULL ) | 
| 195 | 0 |     { | 
| 196 | 0 |       memory_free( | 
| 197 | 0 |        ( *huffman_tree )->symbols ); | 
| 198 | 0 |     } | 
| 199 | 0 |     memory_free( | 
| 200 | 0 |      *huffman_tree ); | 
| 201 |  | 
 | 
| 202 | 0 |     *huffman_tree = NULL; | 
| 203 | 0 |   } | 
| 204 | 0 |   return( -1 ); | 
| 205 | 22.6k | } | 
| 206 |  |  | 
| 207 |  | /* Frees a Huffman tree | 
| 208 |  |  * Returns 1 if successful or -1 on error | 
| 209 |  |  */ | 
| 210 |  | int libfwnt_huffman_tree_free( | 
| 211 |  |      libfwnt_huffman_tree_t **huffman_tree, | 
| 212 |  |      libcerror_error_t **error ) | 
| 213 | 22.6k | { | 
| 214 | 22.6k |   static char *function = "libfwnt_huffman_tree_free"; | 
| 215 |  |  | 
| 216 | 22.6k |   if( huffman_tree == NULL ) | 
| 217 | 0 |   { | 
| 218 | 0 |     libcerror_error_set( | 
| 219 | 0 |      error, | 
| 220 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 221 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 222 | 0 |      "%s: invalid Huffman tree.", | 
| 223 | 0 |      function ); | 
| 224 |  | 
 | 
| 225 | 0 |     return( -1 ); | 
| 226 | 0 |   } | 
| 227 | 22.6k |   if( *huffman_tree != NULL ) | 
| 228 | 22.6k |   { | 
| 229 | 22.6k |     if( ( *huffman_tree )->code_size_counts != NULL ) | 
| 230 | 22.6k |     { | 
| 231 | 22.6k |       memory_free( | 
| 232 | 22.6k |        ( *huffman_tree )->code_size_counts ); | 
| 233 | 22.6k |     } | 
| 234 | 22.6k |     if( ( *huffman_tree )->symbols != NULL ) | 
| 235 | 22.6k |     { | 
| 236 | 22.6k |       memory_free( | 
| 237 | 22.6k |        ( *huffman_tree )->symbols ); | 
| 238 | 22.6k |     } | 
| 239 | 22.6k |     memory_free( | 
| 240 | 22.6k |      *huffman_tree ); | 
| 241 |  |  | 
| 242 | 22.6k |     *huffman_tree = NULL; | 
| 243 | 22.6k |   } | 
| 244 | 22.6k |   return( 1 ); | 
| 245 | 22.6k | } | 
| 246 |  |  | 
| 247 |  | /* Builds the Huffman tree | 
| 248 |  |  * Returns 1 on success, 0 if the tree is empty or -1 on error | 
| 249 |  |  */ | 
| 250 |  | int libfwnt_huffman_tree_build( | 
| 251 |  |      libfwnt_huffman_tree_t *huffman_tree, | 
| 252 |  |      const uint8_t *code_sizes_array, | 
| 253 |  |      int number_of_code_sizes, | 
| 254 |  |      libcerror_error_t **error ) | 
| 255 | 22.4k | { | 
| 256 | 22.4k |   int *symbol_offsets   = NULL; | 
| 257 | 22.4k |   static char *function = "libfwnt_huffman_tree_build"; | 
| 258 | 22.4k |   size_t array_size     = 0; | 
| 259 | 22.4k |   uint8_t bit_index     = 0; | 
| 260 | 22.4k |   uint8_t code_size     = 0; | 
| 261 | 22.4k |   int code_offset       = 0; | 
| 262 | 22.4k |   int left_value        = 0; | 
| 263 | 22.4k |   int symbol            = 0; | 
| 264 |  |  | 
| 265 | 22.4k |   if( huffman_tree == NULL ) | 
| 266 | 0 |   { | 
| 267 | 0 |     libcerror_error_set( | 
| 268 | 0 |      error, | 
| 269 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 270 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 271 | 0 |      "%s: invalid Huffman tree.", | 
| 272 | 0 |      function ); | 
| 273 |  | 
 | 
| 274 | 0 |     return( -1 ); | 
| 275 | 0 |   } | 
| 276 | 22.4k |   if( code_sizes_array == NULL ) | 
| 277 | 0 |   { | 
| 278 | 0 |     libcerror_error_set( | 
| 279 | 0 |      error, | 
| 280 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 281 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 282 | 0 |      "%s: invalid code sizes array.", | 
| 283 | 0 |      function ); | 
| 284 |  | 
 | 
| 285 | 0 |     return( -1 ); | 
| 286 | 0 |   } | 
| 287 | 22.4k |   if( number_of_code_sizes < 0 ) | 
| 288 | 0 |   { | 
| 289 | 0 |     libcerror_error_set( | 
| 290 | 0 |      error, | 
| 291 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 292 | 0 |      LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 293 | 0 |      "%s: invalid number of code sizes value out of bounds.", | 
| 294 | 0 |      function ); | 
| 295 |  | 
 | 
| 296 | 0 |     return( -1 ); | 
| 297 | 0 |   } | 
| 298 |  |   /* Determine the code size frequencies | 
| 299 |  |    */ | 
| 300 | 22.4k |   array_size = sizeof( int ) * ( huffman_tree->maximum_code_size + 1 ); | 
| 301 |  |  | 
| 302 | 22.4k |   if( memory_set( | 
| 303 | 22.4k |        huffman_tree->code_size_counts, | 
| 304 | 22.4k |        0, | 
| 305 | 22.4k |        array_size ) == NULL ) | 
| 306 | 0 |   { | 
| 307 | 0 |     libcerror_error_set( | 
| 308 | 0 |      error, | 
| 309 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 310 | 0 |      LIBCERROR_MEMORY_ERROR_SET_FAILED, | 
| 311 | 0 |      "%s: unable to clear code size counts.", | 
| 312 | 0 |      function ); | 
| 313 |  | 
 | 
| 314 | 0 |     goto on_error; | 
| 315 | 0 |   } | 
| 316 | 22.4k |   for( symbol = 0; | 
| 317 | 5.52M |        symbol < number_of_code_sizes; | 
| 318 | 5.50M |        symbol++ ) | 
| 319 | 5.50M |   { | 
| 320 | 5.50M |     code_size = code_sizes_array[ symbol ]; | 
| 321 |  |  | 
| 322 | 5.50M |     if( code_size > huffman_tree->maximum_code_size ) | 
| 323 | 0 |     { | 
| 324 | 0 |       libcerror_error_set( | 
| 325 | 0 |        error, | 
| 326 | 0 |        LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 327 | 0 |        LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 328 | 0 |        "%s: invalid symbol: %d code size: %" PRIu8 " value out of bounds.", | 
| 329 | 0 |        function, | 
| 330 | 0 |        symbol, | 
| 331 | 0 |        code_size ); | 
| 332 |  | 
 | 
| 333 | 0 |       goto on_error; | 
| 334 | 0 |     } | 
| 335 | 5.50M |     huffman_tree->code_size_counts[ code_size ] += 1; | 
| 336 | 5.50M |   } | 
| 337 |  |   /* The tree has no codes | 
| 338 |  |    */ | 
| 339 | 22.4k |   if( huffman_tree->code_size_counts[ 0 ] == number_of_code_sizes ) | 
| 340 | 179 |   { | 
| 341 | 179 |     return( 0 ); | 
| 342 | 179 |   } | 
| 343 |  |   /* Check if the set of code sizes is incomplete or over-subscribed | 
| 344 |  |    */ | 
| 345 | 22.2k |   left_value = 1; | 
| 346 |  |  | 
| 347 | 22.2k |   for( bit_index = 1; | 
| 348 | 361k |        bit_index <= huffman_tree->maximum_code_size; | 
| 349 | 339k |        bit_index++ ) | 
| 350 | 339k |   { | 
| 351 | 339k |     left_value <<= 1; | 
| 352 | 339k |     left_value  -= huffman_tree->code_size_counts[ bit_index ]; | 
| 353 |  |  | 
| 354 | 339k |     if( left_value < 0 ) | 
| 355 | 100 |     { | 
| 356 | 100 |       libcerror_error_set( | 
| 357 | 100 |        error, | 
| 358 | 100 |        LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 359 | 100 |        LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 360 | 100 |        "%s: code sizes are over-subscribed.", | 
| 361 | 100 |        function ); | 
| 362 |  |  | 
| 363 | 100 |       goto on_error; | 
| 364 | 100 |     } | 
| 365 | 339k |   } | 
| 366 |  | /* TODO | 
| 367 |  |   if( left_value > 0 ) | 
| 368 |  |   { | 
| 369 |  |     libcerror_error_set( | 
| 370 |  |      error, | 
| 371 |  |      LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 372 |  |      LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 373 |  |      "%s: code sizes are incomplete.", | 
| 374 |  |      function ); | 
| 375 |  |  | 
| 376 |  |     goto on_error; | 
| 377 |  |   } | 
| 378 |  | */ | 
| 379 | 22.1k |   symbol_offsets = (int *) memory_allocate( | 
| 380 | 22.1k |                             array_size ); | 
| 381 |  |  | 
| 382 | 22.1k |   if( symbol_offsets == NULL ) | 
| 383 | 0 |   { | 
| 384 | 0 |     libcerror_error_set( | 
| 385 | 0 |      error, | 
| 386 | 0 |      LIBCERROR_ERROR_DOMAIN_MEMORY, | 
| 387 | 0 |      LIBCERROR_MEMORY_ERROR_INSUFFICIENT, | 
| 388 | 0 |      "%s: unable to create symbol offsets.", | 
| 389 | 0 |      function ); | 
| 390 |  | 
 | 
| 391 | 0 |     goto on_error; | 
| 392 | 0 |   } | 
| 393 |  |   /* Calculate the offsets to sort the symbols per code size | 
| 394 |  |    */ | 
| 395 | 22.1k |   symbol_offsets[ 0 ] = 0; | 
| 396 | 22.1k |   symbol_offsets[ 1 ] = 0; | 
| 397 |  |  | 
| 398 | 22.1k |   for( bit_index = 1; | 
| 399 | 338k |        bit_index < huffman_tree->maximum_code_size; | 
| 400 | 316k |        bit_index++ ) | 
| 401 | 316k |   { | 
| 402 | 316k |     symbol_offsets[ bit_index + 1 ] = symbol_offsets[ bit_index ] + huffman_tree->code_size_counts[ bit_index ]; | 
| 403 | 316k |   } | 
| 404 |  |   /* Fill the symbols sorted by code size | 
| 405 |  |    */ | 
| 406 | 22.1k |   for( symbol = 0; | 
| 407 | 5.47M |        symbol < number_of_code_sizes; | 
| 408 | 5.45M |        symbol++ ) | 
| 409 | 5.45M |   { | 
| 410 | 5.45M |     code_size = code_sizes_array[ symbol ]; | 
| 411 |  |  | 
| 412 | 5.45M |     if( code_size == 0 ) | 
| 413 | 4.20M |     { | 
| 414 | 4.20M |       continue; | 
| 415 | 4.20M |     } | 
| 416 | 1.25M |     code_offset = symbol_offsets[ code_size ]; | 
| 417 |  |  | 
| 418 | 1.25M |     if( ( code_offset < 0 ) | 
| 419 | 1.25M |      || ( code_offset > number_of_code_sizes ) ) | 
| 420 | 0 |     { | 
| 421 | 0 |       libcerror_error_set( | 
| 422 | 0 |        error, | 
| 423 | 0 |        LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 424 | 0 |        LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 425 | 0 |        "%s: invalid symbol: %d code offset: %d value out of bounds.", | 
| 426 | 0 |        function, | 
| 427 | 0 |        symbol, | 
| 428 | 0 |        code_offset ); | 
| 429 |  | 
 | 
| 430 | 0 |       goto on_error; | 
| 431 | 0 |     } | 
| 432 | 1.25M |     symbol_offsets[ code_size ] += 1; | 
| 433 |  |  | 
| 434 | 1.25M |     huffman_tree->symbols[ code_offset ] = symbol; | 
| 435 | 1.25M |   } | 
| 436 | 22.1k |   memory_free( | 
| 437 | 22.1k |    symbol_offsets ); | 
| 438 |  |  | 
| 439 | 22.1k |   return( 1 ); | 
| 440 |  |  | 
| 441 | 100 | on_error: | 
| 442 | 100 |   if( symbol_offsets != NULL ) | 
| 443 | 0 |   { | 
| 444 | 0 |     memory_free( | 
| 445 | 0 |      symbol_offsets ); | 
| 446 | 0 |   } | 
| 447 | 100 |   return( -1 ); | 
| 448 | 22.1k | } | 
| 449 |  |  | 
| 450 |  | /* Retrieves a symbol based on the Huffman code read from the bit-stream | 
| 451 |  |  * Returns 1 on success or -1 on error | 
| 452 |  |  */ | 
| 453 |  | int libfwnt_huffman_tree_get_symbol_from_bit_stream( | 
| 454 |  |      libfwnt_huffman_tree_t *huffman_tree, | 
| 455 |  |      libfwnt_bit_stream_t *bit_stream, | 
| 456 |  |      uint32_t *symbol, | 
| 457 |  |      libcerror_error_t **error ) | 
| 458 | 22.1M | { | 
| 459 | 22.1M |   static char *function  = "libfwnt_huffman_tree_get_symbol_from_bit_stream"; | 
| 460 | 22.1M |   uint32_t safe_symbol   = 0; | 
| 461 | 22.1M |   uint32_t value_32bit   = 0; | 
| 462 | 22.1M |   uint8_t bit_index      = 0; | 
| 463 | 22.1M |   uint8_t number_of_bits = 0; | 
| 464 | 22.1M |   int code_size_count    = 0; | 
| 465 | 22.1M |   int first_huffman_code = 0; | 
| 466 | 22.1M |   int first_index        = 0; | 
| 467 | 22.1M |   int huffman_code       = 0; | 
| 468 | 22.1M |   int result             = 0; | 
| 469 |  |  | 
| 470 | 22.1M |   if( huffman_tree == NULL ) | 
| 471 | 0 |   { | 
| 472 | 0 |     libcerror_error_set( | 
| 473 | 0 |      error, | 
| 474 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 475 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 476 | 0 |      "%s: invalid Huffman tree.", | 
| 477 | 0 |      function ); | 
| 478 |  | 
 | 
| 479 | 0 |     return( -1 ); | 
| 480 | 0 |   } | 
| 481 | 22.1M |   if( bit_stream == NULL ) | 
| 482 | 0 |   { | 
| 483 | 0 |     libcerror_error_set( | 
| 484 | 0 |      error, | 
| 485 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 486 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 487 | 0 |      "%s: invalid bit stream.", | 
| 488 | 0 |      function ); | 
| 489 |  | 
 | 
| 490 | 0 |     return( -1 ); | 
| 491 | 0 |   } | 
| 492 | 22.1M |   if( symbol == NULL ) | 
| 493 | 0 |   { | 
| 494 | 0 |     libcerror_error_set( | 
| 495 | 0 |      error, | 
| 496 | 0 |      LIBCERROR_ERROR_DOMAIN_ARGUMENTS, | 
| 497 | 0 |      LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, | 
| 498 | 0 |      "%s: invalid symbol.", | 
| 499 | 0 |      function ); | 
| 500 |  | 
 | 
| 501 | 0 |     return( -1 ); | 
| 502 | 0 |   } | 
| 503 |  |   /* Try to fill the bit buffer with the maximum number of bits | 
| 504 |  |    */ | 
| 505 | 23.2M |   while( bit_stream->bit_buffer_size < huffman_tree->maximum_code_size ) | 
| 506 | 1.14M |   { | 
| 507 | 1.14M |     result = libfwnt_bit_stream_read( | 
| 508 | 1.14M |               bit_stream, | 
| 509 | 1.14M |               huffman_tree->maximum_code_size, | 
| 510 | 1.14M |               error ); | 
| 511 |  |  | 
| 512 | 1.14M |     if( result == -1 ) | 
| 513 | 0 |     { | 
| 514 | 0 |       libcerror_error_set( | 
| 515 | 0 |        error, | 
| 516 | 0 |        LIBCERROR_ERROR_DOMAIN_IO, | 
| 517 | 0 |        LIBCERROR_IO_ERROR_READ_FAILED, | 
| 518 | 0 |        "%s: unable to read bits.", | 
| 519 | 0 |        function ); | 
| 520 |  | 
 | 
| 521 | 0 |       return( -1 ); | 
| 522 | 0 |     } | 
| 523 | 1.14M |     else if( result == 0 ) | 
| 524 | 0 |     { | 
| 525 | 0 |       break; | 
| 526 | 0 |     } | 
| 527 | 1.14M |   } | 
| 528 | 22.1M |   if( huffman_tree->maximum_code_size < bit_stream->bit_buffer_size ) | 
| 529 | 21.7M |   { | 
| 530 | 21.7M |     number_of_bits = huffman_tree->maximum_code_size; | 
| 531 | 21.7M |   } | 
| 532 | 396k |   else | 
| 533 | 396k |   { | 
| 534 | 396k |     number_of_bits = bit_stream->bit_buffer_size; | 
| 535 | 396k |   } | 
| 536 | 22.1M |   for( bit_index = 1; | 
| 537 | 121M |        bit_index <= number_of_bits; | 
| 538 | 99.4M |        bit_index++ ) | 
| 539 | 121M |   { | 
| 540 |  | /* TODO get maximum_code_size of bits into local bit buffer */ | 
| 541 | 121M |     if( libfwnt_bit_stream_get_value( | 
| 542 | 121M |          bit_stream, | 
| 543 | 121M |          1, | 
| 544 | 121M |          &value_32bit, | 
| 545 | 121M |          error ) != 1 ) | 
| 546 | 0 |     { | 
| 547 | 0 |       libcerror_error_set( | 
| 548 | 0 |        error, | 
| 549 | 0 |        LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 550 | 0 |        LIBCERROR_RUNTIME_ERROR_GET_FAILED, | 
| 551 | 0 |        "%s: unable to retrieve value from bit stream.", | 
| 552 | 0 |        function ); | 
| 553 |  | 
 | 
| 554 | 0 |       return( -1 ); | 
| 555 | 0 |     } | 
| 556 | 121M |     huffman_code <<= 1; | 
| 557 | 121M |     huffman_code  |= (int) value_32bit; | 
| 558 |  |  | 
| 559 | 121M |     code_size_count = huffman_tree->code_size_counts[ bit_index ]; | 
| 560 |  |  | 
| 561 | 121M |     if( ( huffman_code - code_size_count ) < first_huffman_code ) | 
| 562 | 21.7M |     { | 
| 563 | 21.7M |       safe_symbol = huffman_tree->symbols[ first_index + ( huffman_code - first_huffman_code ) ]; | 
| 564 |  |  | 
| 565 | 21.7M |       result = 1; | 
| 566 |  |  | 
| 567 | 21.7M |       break; | 
| 568 | 21.7M |     } | 
| 569 | 99.4M |     first_huffman_code  += code_size_count; | 
| 570 | 99.4M |     first_huffman_code <<= 1; | 
| 571 | 99.4M |     first_index         += code_size_count; | 
| 572 | 99.4M |   } | 
| 573 | 22.1M |   if( result != 1 ) | 
| 574 | 132 |   { | 
| 575 | 132 |     libcerror_error_set( | 
| 576 | 132 |      error, | 
| 577 | 132 |      LIBCERROR_ERROR_DOMAIN_RUNTIME, | 
| 578 | 132 |      LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS, | 
| 579 | 132 |      "%s: invalid Huffman code: 0x%08" PRIx32 ".", | 
| 580 | 132 |      function, | 
| 581 | 132 |      huffman_code ); | 
| 582 |  |  | 
| 583 | 132 |     return( -1 ); | 
| 584 | 132 |   } | 
| 585 | 22.1M |   *symbol = safe_symbol; | 
| 586 |  |  | 
| 587 | 22.1M |   return( 1 ); | 
| 588 | 22.1M | } | 
| 589 |  |  |