Coverage Report

Created: 2026-09-14 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/dec/huffman_decode.h
Line
Count
Source
1
// Copyright (c) Google LLC 2019
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file or at
5
// https://opensource.org/licenses/MIT.
6
7
// Library to decode the Huffman code lengths from the bit-stream and build a
8
// decoding table from them.
9
10
#ifndef BRUNSLI_DEC_HUFFMAN_DECODE_H_
11
#define BRUNSLI_DEC_HUFFMAN_DECODE_H_
12
13
#include <memory>
14
#include <vector>
15
16
#include "./huffman_table.h"
17
18
namespace brunsli {
19
20
struct BrunsliBitReader;
21
22
template <typename T>
23
struct Arena {
24
  size_t capacity = 0;
25
  // TODO(eustas): use "char" storage to ensure new[] does not initialize...
26
  std::unique_ptr<T[]> storage;
27
28
6.97k
  void reserve(size_t limit) {
29
6.97k
    if (capacity < limit) {
30
6.82k
      capacity = limit;
31
6.82k
      storage.reset(new T[capacity]);
32
6.82k
    }
33
6.97k
  }
34
35
462
  T* data() { return storage.get(); }
36
37
5.30k
  void reset() {
38
5.30k
    capacity = 0;
39
5.30k
    storage.reset();
40
5.30k
  }
41
};
42
43
struct HuffmanDecodingData {
44
  // Decodes the Huffman code lengths from the bit-stream and fills in the
45
  // pre-allocated table with the corresponding 2-level Huffman decoding table.
46
  // |arena| is used as an intermediate output for BuildHuffmanTable.
47
  // Returns false if the Huffman code lengths can not de decoded.
48
  bool ReadFromBitStream(size_t alphabet_size, BrunsliBitReader* br,
49
                         Arena<HuffmanCode>* arena = nullptr);
50
51
  uint16_t ReadSymbol(BrunsliBitReader* br) const;
52
53
  std::vector<HuffmanCode> table_;
54
};
55
56
}  // namespace brunsli
57
58
#endif  // BRUNSLI_DEC_HUFFMAN_DECODE_H_