Coverage Report

Created: 2025-08-26 06:35

/src/h2o/deps/brotli/c/enc/literal_cost.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* Literal cost model to allow backward reference replacement to be efficient.
8
*/
9
10
#include "./literal_cost.h"
11
12
#include <brotli/types.h>
13
#include "./fast_log.h"
14
#include "./port.h"
15
#include "./utf8_util.h"
16
17
#if defined(__cplusplus) || defined(c_plusplus)
18
extern "C" {
19
#endif
20
21
0
static size_t UTF8Position(size_t last, size_t c, size_t clamp) {
22
0
  if (c < 128) {
23
0
    return 0;  /* Next one is the 'Byte 1' again. */
24
0
  } else if (c >= 192) {  /* Next one is the 'Byte 2' of utf-8 encoding. */
25
0
    return BROTLI_MIN(size_t, 1, clamp);
26
0
  } else {
27
    /* Let's decide over the last byte if this ends the sequence. */
28
0
    if (last < 0xe0) {
29
0
      return 0;  /* Completed two or three byte coding. */
30
0
    } else {  /* Next one is the 'Byte 3' of utf-8 encoding. */
31
0
      return BROTLI_MIN(size_t, 2, clamp);
32
0
    }
33
0
  }
34
0
}
35
36
static size_t DecideMultiByteStatsLevel(size_t pos, size_t len, size_t mask,
37
0
                                        const uint8_t *data) {
38
0
  size_t counts[3] = { 0 };
39
0
  size_t max_utf8 = 1;  /* should be 2, but 1 compresses better. */
40
0
  size_t last_c = 0;
41
0
  size_t i;
42
0
  for (i = 0; i < len; ++i) {
43
0
    size_t c = data[(pos + i) & mask];
44
0
    ++counts[UTF8Position(last_c, c, 2)];
45
0
    last_c = c;
46
0
  }
47
0
  if (counts[2] < 500) {
48
0
    max_utf8 = 1;
49
0
  }
50
0
  if (counts[1] + counts[2] < 25) {
51
0
    max_utf8 = 0;
52
0
  }
53
0
  return max_utf8;
54
0
}
55
56
static void EstimateBitCostsForLiteralsUTF8(size_t pos, size_t len, size_t mask,
57
0
                                            const uint8_t *data, float *cost) {
58
  /* max_utf8 is 0 (normal ASCII single byte modeling),
59
     1 (for 2-byte UTF-8 modeling), or 2 (for 3-byte UTF-8 modeling). */
60
0
  const size_t max_utf8 = DecideMultiByteStatsLevel(pos, len, mask, data);
61
0
  size_t histogram[3][256] = { { 0 } };
62
0
  size_t window_half = 495;
63
0
  size_t in_window = BROTLI_MIN(size_t, window_half, len);
64
0
  size_t in_window_utf8[3] = { 0 };
65
66
0
  size_t i;
67
0
  {  /* Bootstrap histograms. */
68
0
    size_t last_c = 0;
69
0
    size_t utf8_pos = 0;
70
0
    for (i = 0; i < in_window; ++i) {
71
0
      size_t c = data[(pos + i) & mask];
72
0
      ++histogram[utf8_pos][c];
73
0
      ++in_window_utf8[utf8_pos];
74
0
      utf8_pos = UTF8Position(last_c, c, max_utf8);
75
0
      last_c = c;
76
0
    }
77
0
  }
78
79
  /* Compute bit costs with sliding window. */
80
0
  for (i = 0; i < len; ++i) {
81
0
    if (i >= window_half) {
82
      /* Remove a byte in the past. */
83
0
      size_t c =
84
0
          i < window_half + 1 ? 0 : data[(pos + i - window_half - 1) & mask];
85
0
      size_t last_c =
86
0
          i < window_half + 2 ? 0 : data[(pos + i - window_half - 2) & mask];
87
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
88
0
      --histogram[utf8_pos2][data[(pos + i - window_half) & mask]];
89
0
      --in_window_utf8[utf8_pos2];
90
0
    }
91
0
    if (i + window_half < len) {
92
      /* Add a byte in the future. */
93
0
      size_t c = data[(pos + i + window_half - 1) & mask];
94
0
      size_t last_c = data[(pos + i + window_half - 2) & mask];
95
0
      size_t utf8_pos2 = UTF8Position(last_c, c, max_utf8);
96
0
      ++histogram[utf8_pos2][data[(pos + i + window_half) & mask]];
97
0
      ++in_window_utf8[utf8_pos2];
98
0
    }
99
0
    {
100
0
      size_t c = i < 1 ? 0 : data[(pos + i - 1) & mask];
101
0
      size_t last_c = i < 2 ? 0 : data[(pos + i - 2) & mask];
102
0
      size_t utf8_pos = UTF8Position(last_c, c, max_utf8);
103
0
      size_t masked_pos = (pos + i) & mask;
104
0
      size_t histo = histogram[utf8_pos][data[masked_pos]];
105
0
      double lit_cost;
106
0
      if (histo == 0) {
107
0
        histo = 1;
108
0
      }
109
0
      lit_cost = FastLog2(in_window_utf8[utf8_pos]) - FastLog2(histo);
110
0
      lit_cost += 0.02905;
111
0
      if (lit_cost < 1.0) {
112
0
        lit_cost *= 0.5;
113
0
        lit_cost += 0.5;
114
0
      }
115
      /* Make the first bytes more expensive -- seems to help, not sure why.
116
         Perhaps because the entropy source is changing its properties
117
         rapidly in the beginning of the file, perhaps because the beginning
118
         of the data is a statistical "anomaly". */
119
0
      if (i < 2000) {
120
0
        lit_cost += 0.7 - ((double)(2000 - i) / 2000.0 * 0.35);
121
0
      }
122
0
      cost[i] = (float)lit_cost;
123
0
    }
124
0
  }
125
0
}
126
127
void BrotliEstimateBitCostsForLiterals(size_t pos, size_t len, size_t mask,
128
0
                                       const uint8_t *data, float *cost) {
129
0
  if (BrotliIsMostlyUTF8(data, pos, mask, len, kMinUTF8Ratio)) {
130
0
    EstimateBitCostsForLiteralsUTF8(pos, len, mask, data, cost);
131
0
    return;
132
0
  } else {
133
0
    size_t histogram[256] = { 0 };
134
0
    size_t window_half = 2000;
135
0
    size_t in_window = BROTLI_MIN(size_t, window_half, len);
136
137
    /* Bootstrap histogram. */
138
0
    size_t i;
139
0
    for (i = 0; i < in_window; ++i) {
140
0
      ++histogram[data[(pos + i) & mask]];
141
0
    }
142
143
    /* Compute bit costs with sliding window. */
144
0
    for (i = 0; i < len; ++i) {
145
0
      size_t histo;
146
0
      if (i >= window_half) {
147
        /* Remove a byte in the past. */
148
0
        --histogram[data[(pos + i - window_half) & mask]];
149
0
        --in_window;
150
0
      }
151
0
      if (i + window_half < len) {
152
        /* Add a byte in the future. */
153
0
        ++histogram[data[(pos + i + window_half) & mask]];
154
0
        ++in_window;
155
0
      }
156
0
      histo = histogram[data[(pos + i) & mask]];
157
0
      if (histo == 0) {
158
0
        histo = 1;
159
0
      }
160
0
      {
161
0
        double lit_cost = FastLog2(in_window) - FastLog2(histo);
162
0
        lit_cost += 0.029;
163
0
        if (lit_cost < 1.0) {
164
0
          lit_cost *= 0.5;
165
0
          lit_cost += 0.5;
166
0
        }
167
0
        cost[i] = (float)lit_cost;
168
0
      }
169
0
    }
170
0
  }
171
0
}
172
173
#if defined(__cplusplus) || defined(c_plusplus)
174
}  /* extern "C" */
175
#endif