Coverage Report

Created: 2025-06-13 07:37

/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 <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
363k
                        const size_t num_htrees) {
29
363k
  std::vector<bool> have_htree(num_htrees);
30
363k
  size_t num_found = 0;
31
3.97G
  for (const uint8_t htree : context_map) {
32
3.97G
    if (htree >= num_htrees) {
33
0
      return JXL_FAILURE("Invalid histogram index in context map.");
34
0
    }
35
3.97G
    if (!have_htree[htree]) {
36
683k
      have_htree[htree] = true;
37
683k
      ++num_found;
38
683k
    }
39
3.97G
  }
40
363k
  if (num_found != num_htrees) {
41
4.66k
    return JXL_FAILURE("Incomplete context map.");
42
4.66k
  }
43
358k
  return true;
44
363k
}
45
46
}  // namespace
47
48
Status DecodeContextMap(JxlMemoryManager* memory_manager,
49
                        std::vector<uint8_t>* context_map, size_t* num_htrees,
50
383k
                        BitReader* input) {
51
383k
  bool is_simple = static_cast<bool>(input->ReadFixedBits<1>());
52
383k
  if (is_simple) {
53
199k
    int bits_per_entry = input->ReadFixedBits<2>();
54
199k
    if (bits_per_entry != 0) {
55
130M
      for (uint8_t& entry : *context_map) {
56
130M
        entry = input->ReadBits(bits_per_entry);
57
130M
      }
58
143k
    } else {
59
143k
      std::fill(context_map->begin(), context_map->end(), 0);
60
143k
    }
61
199k
  } else {
62
183k
    bool use_mtf = static_cast<bool>(input->ReadFixedBits<1>());
63
183k
    ANSCode code;
64
183k
    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
183k
    JXL_RETURN_IF_ERROR(
70
183k
        DecodeHistograms(memory_manager, input, 1, &code, &sink_ctx_map,
71
183k
                         /*disallow_lz77=*/context_map->size() <= 2));
72
335k
    JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader,
73
335k
                         ANSSymbolReader::Create(&code, input));
74
335k
    size_t i = 0;
75
335k
    uint32_t maxsym = 0;
76
3.81G
    while (i < context_map->size()) {
77
3.81G
      uint32_t sym = reader.ReadHybridUintInlined</*uses_lz77=*/true>(
78
3.81G
          0, input, sink_ctx_map);
79
3.81G
      maxsym = sym > maxsym ? sym : maxsym;
80
3.81G
      (*context_map)[i] = sym;
81
3.81G
      i++;
82
3.81G
    }
83
335k
    if (maxsym >= kMaxClusters) {
84
4.13k
      return JXL_FAILURE("Invalid cluster ID");
85
4.13k
    }
86
163k
    if (!reader.CheckANSFinalState()) {
87
0
      return JXL_FAILURE("Invalid context map");
88
0
    }
89
163k
    if (use_mtf) {
90
56.4k
      InverseMoveToFrontTransform(context_map->data(), context_map->size());
91
56.4k
    }
92
163k
  }
93
363k
  *num_htrees = *std::max_element(context_map->begin(), context_map->end()) + 1;
94
363k
  return VerifyContextMap(*context_map, *num_htrees);
95
383k
}
96
97
}  // namespace jxl