Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/dec_context_map.cc
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#include "lib/jxl/dec_context_map.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstddef>
12
#include <cstdint>
13
#include <vector>
14
15
#include "lib/jxl/base/status.h"
16
#include "lib/jxl/dec_ans.h"
17
#include "lib/jxl/dec_bit_reader.h"
18
#include "lib/jxl/inverse_mtf-inl.h"
19
20
namespace jxl {
21
22
namespace {
23
24
// Context map uses uint8_t.
25
constexpr size_t kMaxClusters = 256;
26
27
Status VerifyContextMap(const std::vector<uint8_t>& context_map,
28
72.6k
                        const size_t num_htrees) {
29
72.6k
  std::vector<bool> have_htree(num_htrees);
30
72.6k
  size_t num_found = 0;
31
30.5M
  for (const uint8_t htree : context_map) {
32
30.5M
    if (htree >= num_htrees) {
33
0
      return JXL_FAILURE("Invalid histogram index in context map.");
34
0
    }
35
30.5M
    if (!have_htree[htree]) {
36
165k
      have_htree[htree] = true;
37
165k
      ++num_found;
38
165k
    }
39
30.5M
  }
40
72.6k
  if (num_found != num_htrees) {
41
1.22k
    return JXL_FAILURE("Incomplete context map.");
42
1.22k
  }
43
71.4k
  return true;
44
72.6k
}
45
46
}  // namespace
47
48
Status DecodeContextMap(JxlMemoryManager* memory_manager,
49
                        std::vector<uint8_t>* context_map, size_t* num_htrees,
50
76.8k
                        BitReader* input) {
51
76.8k
  bool is_simple = static_cast<bool>(input->ReadFixedBits<1>());
52
76.8k
  if (is_simple) {
53
52.5k
    int bits_per_entry = input->ReadFixedBits<2>();
54
52.5k
    if (bits_per_entry != 0) {
55
648k
      for (uint8_t& entry : *context_map) {
56
648k
        entry = input->ReadBits(bits_per_entry);
57
648k
      }
58
34.9k
    } else {
59
34.9k
      std::fill(context_map->begin(), context_map->end(), 0);
60
34.9k
    }
61
52.5k
  } else {
62
24.2k
    bool use_mtf = static_cast<bool>(input->ReadFixedBits<1>());
63
24.2k
    ANSCode code;
64
24.2k
    std::vector<uint8_t> sink_ctx_map;
65
    // Usage of LZ77 is disallowed if decoding only two symbols. This doesn't
66
    // make sense in non-malicious bitstreams, and could cause a stack overflow
67
    // in malicious bitstreams by making every context map require its own
68
    // context map.
69
24.2k
    JXL_RETURN_IF_ERROR(
70
24.2k
        DecodeHistograms(memory_manager, input, 1, &code, &sink_ctx_map,
71
24.2k
                         /*disallow_lz77=*/context_map->size() <= 2));
72
42.6k
    JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader,
73
42.6k
                         ANSSymbolReader::Create(&code, input));
74
42.6k
    size_t i = 0;
75
42.6k
    uint32_t maxsym = 0;
76
17.3M
    while (i < context_map->size()) {
77
17.2M
      uint32_t sym = reader.ReadHybridUintInlined</*uses_lz77=*/true>(
78
17.2M
          0, input, sink_ctx_map);
79
17.2M
      maxsym = sym > maxsym ? sym : maxsym;
80
17.2M
      (*context_map)[i] = sym;
81
17.2M
      i++;
82
17.2M
    }
83
42.6k
    if (maxsym >= kMaxClusters) {
84
1.24k
      return JXL_FAILURE("Invalid cluster ID");
85
1.24k
    }
86
20.0k
    if (!reader.CheckANSFinalState()) {
87
0
      return JXL_FAILURE("Invalid context map");
88
0
    }
89
20.0k
    if (use_mtf) {
90
6.00k
      InverseMoveToFrontTransform(context_map->data(), context_map->size());
91
6.00k
    }
92
20.0k
  }
93
72.6k
  *num_htrees = *std::max_element(context_map->begin(), context_map->end()) + 1;
94
72.6k
  return VerifyContextMap(*context_map, *num_htrees);
95
76.8k
}
96
97
}  // namespace jxl