Coverage Report

Created: 2025-09-08 07:52

/src/libjxl/lib/jxl/dec_context_map.cc
Line
Count
Source (jump to first uncovered line)
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 <cstdint>
12
#include <vector>
13
14
#include "lib/jxl/base/status.h"
15
#include "lib/jxl/dec_ans.h"
16
#include "lib/jxl/inverse_mtf-inl.h"
17
18
namespace jxl {
19
20
namespace {
21
22
// Context map uses uint8_t.
23
constexpr size_t kMaxClusters = 256;
24
25
Status VerifyContextMap(const std::vector<uint8_t>& context_map,
26
22.7k
                        const size_t num_htrees) {
27
22.7k
  std::vector<bool> have_htree(num_htrees);
28
22.7k
  size_t num_found = 0;
29
44.0M
  for (const uint8_t htree : context_map) {
30
44.0M
    if (htree >= num_htrees) {
31
0
      return JXL_FAILURE("Invalid histogram index in context map.");
32
0
    }
33
44.0M
    if (!have_htree[htree]) {
34
33.0k
      have_htree[htree] = true;
35
33.0k
      ++num_found;
36
33.0k
    }
37
44.0M
  }
38
22.7k
  if (num_found != num_htrees) {
39
5
    return JXL_FAILURE("Incomplete context map.");
40
5
  }
41
22.7k
  return true;
42
22.7k
}
43
44
}  // namespace
45
46
Status DecodeContextMap(JxlMemoryManager* memory_manager,
47
                        std::vector<uint8_t>* context_map, size_t* num_htrees,
48
22.8k
                        BitReader* input) {
49
22.8k
  bool is_simple = static_cast<bool>(input->ReadFixedBits<1>());
50
22.8k
  if (is_simple) {
51
21.2k
    int bits_per_entry = input->ReadFixedBits<2>();
52
21.2k
    if (bits_per_entry != 0) {
53
93.5k
      for (uint8_t& entry : *context_map) {
54
93.5k
        entry = input->ReadBits(bits_per_entry);
55
93.5k
      }
56
17.3k
    } else {
57
17.3k
      std::fill(context_map->begin(), context_map->end(), 0);
58
17.3k
    }
59
21.2k
  } else {
60
1.68k
    bool use_mtf = static_cast<bool>(input->ReadFixedBits<1>());
61
1.68k
    ANSCode code;
62
1.68k
    std::vector<uint8_t> sink_ctx_map;
63
    // Usage of LZ77 is disallowed if decoding only two symbols. This doesn't
64
    // make sense in non-malicious bitstreams, and could cause a stack overflow
65
    // in malicious bitstreams by making every context map require its own
66
    // context map.
67
1.68k
    JXL_RETURN_IF_ERROR(
68
1.68k
        DecodeHistograms(memory_manager, input, 1, &code, &sink_ctx_map,
69
1.68k
                         /*disallow_lz77=*/context_map->size() <= 2));
70
3.09k
    JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader,
71
3.09k
                         ANSSymbolReader::Create(&code, input));
72
3.09k
    size_t i = 0;
73
3.09k
    uint32_t maxsym = 0;
74
416k
    while (i < context_map->size()) {
75
414k
      uint32_t sym = reader.ReadHybridUintInlined</*uses_lz77=*/true>(
76
414k
          0, input, sink_ctx_map);
77
414k
      maxsym = sym > maxsym ? sym : maxsym;
78
414k
      (*context_map)[i] = sym;
79
414k
      i++;
80
414k
    }
81
3.09k
    if (maxsym >= kMaxClusters) {
82
0
      return JXL_FAILURE("Invalid cluster ID");
83
0
    }
84
1.54k
    if (!reader.CheckANSFinalState()) {
85
0
      return JXL_FAILURE("Invalid context map");
86
0
    }
87
1.54k
    if (use_mtf) {
88
853
      InverseMoveToFrontTransform(context_map->data(), context_map->size());
89
853
    }
90
1.54k
  }
91
22.7k
  *num_htrees = *std::max_element(context_map->begin(), context_map->end()) + 1;
92
22.7k
  return VerifyContextMap(*context_map, *num_htrees);
93
22.8k
}
94
95
}  // namespace jxl