Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp8/decoder/dboolhuff.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "dboolhuff.h"
12
#include "vp8/common/common.h"
13
#include "vpx_dsp/vpx_dsp_common.h"
14
15
int vp8dx_start_decode(BOOL_DECODER *br, const unsigned char *source,
16
                       unsigned int source_sz, vpx_decrypt_cb decrypt_cb,
17
67.6k
                       void *decrypt_state) {
18
67.6k
  if (source_sz && !source) return 1;
19
20
  // To simplify calling code this fuction can be called with |source| == null
21
  // and |source_sz| == 0. This and vp8dx_bool_decoder_fill() are essentially
22
  // no-ops in this case.
23
  // Work around a ubsan warning with a ternary to avoid adding 0 to null.
24
67.6k
  br->user_buffer_end = source ? source + source_sz : source;
25
67.6k
  br->user_buffer = source;
26
67.6k
  br->value = 0;
27
67.6k
  br->count = -8;
28
67.6k
  br->range = 255;
29
67.6k
  br->decrypt_cb = decrypt_cb;
30
67.6k
  br->decrypt_state = decrypt_state;
31
32
  /* Populate the buffer */
33
67.6k
  vp8dx_bool_decoder_fill(br);
34
35
67.6k
  return 0;
36
67.6k
}
37
38
3.81M
void vp8dx_bool_decoder_fill(BOOL_DECODER *br) {
39
3.81M
  const unsigned char *bufptr = br->user_buffer;
40
3.81M
  VP8_BD_VALUE value = br->value;
41
3.81M
  int count = br->count;
42
3.81M
  int shift = VP8_BD_VALUE_SIZE - CHAR_BIT - (count + CHAR_BIT);
43
3.81M
  size_t bytes_left = br->user_buffer_end - bufptr;
44
3.81M
  size_t bits_left = bytes_left * CHAR_BIT;
45
3.81M
  int x = shift + CHAR_BIT - (int)bits_left;
46
3.81M
  int loop_end = 0;
47
3.81M
  unsigned char decrypted[sizeof(VP8_BD_VALUE) + 1];
48
49
3.81M
  if (br->decrypt_cb) {
50
0
    size_t n = VPXMIN(sizeof(decrypted), bytes_left);
51
0
    br->decrypt_cb(br->decrypt_state, bufptr, decrypted, (int)n);
52
0
    bufptr = decrypted;
53
0
  }
54
55
3.81M
  if (x >= 0) {
56
34.4k
    count += VP8_LOTS_OF_BITS;
57
34.4k
    loop_end = x;
58
34.4k
  }
59
60
3.81M
  if (x < 0 || bits_left) {
61
30.4M
    while (shift >= loop_end) {
62
26.6M
      count += CHAR_BIT;
63
26.6M
      value |= (VP8_BD_VALUE)*bufptr << shift;
64
26.6M
      ++bufptr;
65
26.6M
      ++br->user_buffer;
66
26.6M
      shift -= CHAR_BIT;
67
26.6M
    }
68
3.81M
  }
69
70
3.81M
  br->value = value;
71
3.81M
  br->count = count;
72
3.81M
}