huffman_fuzzer.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2023 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include "src/dec/vp8li_dec.h"
  19. #include "src/utils/bit_reader_utils.h"
  20. #include "src/utils/huffman_utils.h"
  21. #include "src/utils/utils.h"
  22. #include "src/webp/format_constants.h"
  23. int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
  24. // Number of bits to initialize data.
  25. static const int kColorCacheBitsBits = 4;
  26. // 'num_htree_groups' is contained in the RG channel, hence 16 bits.
  27. static const int kNumHtreeGroupsBits = 16;
  28. if (size * sizeof(*data) < kColorCacheBitsBits + kNumHtreeGroupsBits) {
  29. return 0;
  30. }
  31. // A non-NULL mapping brings minor changes that are tested by the normal
  32. // fuzzer.
  33. int* const mapping = NULL;
  34. HuffmanTables huffman_tables;
  35. memset(&huffman_tables, 0, sizeof(huffman_tables));
  36. HTreeGroup* htree_groups = NULL;
  37. VP8LDecoder* dec = VP8LNew();
  38. if (dec == NULL) goto Error;
  39. VP8LBitReader* const br = &dec->br_;
  40. VP8LInitBitReader(br, data, size);
  41. const int color_cache_bits = VP8LReadBits(br, kColorCacheBitsBits);
  42. if (color_cache_bits < 1 || color_cache_bits > MAX_CACHE_BITS) goto Error;
  43. const int num_htree_groups = VP8LReadBits(br, kNumHtreeGroupsBits);
  44. // 'num_htree_groups' cannot be 0 as it is built from a non-empty image.
  45. if (num_htree_groups == 0) goto Error;
  46. // This variable is only useful when mapping is not NULL.
  47. const int num_htree_groups_max = num_htree_groups;
  48. (void)ReadHuffmanCodesHelper(color_cache_bits, num_htree_groups,
  49. num_htree_groups_max, mapping, dec,
  50. &huffman_tables, &htree_groups);
  51. Error:
  52. WebPSafeFree(mapping);
  53. VP8LHtreeGroupsFree(htree_groups);
  54. VP8LHuffmanTablesDeallocate(&huffman_tables);
  55. VP8LDelete(dec);
  56. return 0;
  57. }