Coverage Report

Created: 2025-06-22 08:04

/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
82.1k
                        const size_t num_htrees) {
27
82.1k
  std::vector<bool> have_htree(num_htrees);
28
82.1k
  size_t num_found = 0;
29
73.3M
  for (const uint8_t htree : context_map) {
30
73.3M
    if (htree >= num_htrees) {
31
0
      return JXL_FAILURE("Invalid histogram index in context map.");
32
0
    }
33
73.3M
    if (!have_htree[htree]) {
34
131k
      have_htree[htree] = true;
35
131k
      ++num_found;
36
131k
    }
37
73.3M
  }
38
82.1k
  if (num_found != num_htrees) {
39
1.17k
    return JXL_FAILURE("Incomplete context map.");
40
1.17k
  }
41
80.9k
  return true;
42
82.1k
}
43
44
}  // namespace
45
46
Status DecodeContextMap(JxlMemoryManager* memory_manager,
47
                        std::vector<uint8_t>* context_map, size_t* num_htrees,
48
85.9k
                        BitReader* input) {
49
85.9k
  bool is_simple = static_cast<bool>(input->ReadFixedBits<1>());
50
85.9k
  if (is_simple) {
51
66.7k
    int bits_per_entry = input->ReadFixedBits<2>();
52
66.7k
    if (bits_per_entry != 0) {
53
558k
      for (uint8_t& entry : *context_map) {
54
558k
        entry = input->ReadBits(bits_per_entry);
55
558k
      }
56
45.7k
    } else {
57
45.7k
      std::fill(context_map->begin(), context_map->end(), 0);
58
45.7k
    }
59
66.7k
  } else {
60
19.2k
    bool use_mtf = static_cast<bool>(input->ReadFixedBits<1>());
61
19.2k
    ANSCode code;
62
19.2k
    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
19.2k
    JXL_RETURN_IF_ERROR(
68
19.2k
        DecodeHistograms(memory_manager, input, 1, &code, &sink_ctx_map,
69
19.2k
                         /*disallow_lz77=*/context_map->size() <= 2));
70
32.6k
    JXL_ASSIGN_OR_RETURN(ANSSymbolReader reader,
71
32.6k
                         ANSSymbolReader::Create(&code, input));
72
32.6k
    size_t i = 0;
73
32.6k
    uint32_t maxsym = 0;
74
7.07M
    while (i < context_map->size()) {
75
7.05M
      uint32_t sym = reader.ReadHybridUintInlined</*uses_lz77=*/true>(
76
7.05M
          0, input, sink_ctx_map);
77
7.05M
      maxsym = sym > maxsym ? sym : maxsym;
78
7.05M
      (*context_map)[i] = sym;
79
7.05M
      i++;
80
7.05M
    }
81
32.6k
    if (maxsym >= kMaxClusters) {
82
841
      return JXL_FAILURE("Invalid cluster ID");
83
841
    }
84
15.4k
    if (!reader.CheckANSFinalState()) {
85
0
      return JXL_FAILURE("Invalid context map");
86
0
    }
87
15.4k
    if (use_mtf) {
88
5.08k
      InverseMoveToFrontTransform(context_map->data(), context_map->size());
89
5.08k
    }
90
15.4k
  }
91
82.2k
  *num_htrees = *std::max_element(context_map->begin(), context_map->end()) + 1;
92
82.2k
  return VerifyContextMap(*context_map, *num_htrees);
93
85.9k
}
94
95
}  // namespace jxl