Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmliblzma/liblzma/simple/x86.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       x86.c
6
/// \brief      Filter for x86 binaries (BCJ filter)
7
///
8
//  Authors:    Igor Pavlov
9
//              Lasse Collin
10
//
11
///////////////////////////////////////////////////////////////////////////////
12
13
#include "simple_private.h"
14
15
16
89.3k
#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
17
18
19
typedef struct {
20
  uint32_t prev_mask;
21
  uint32_t prev_pos;
22
} lzma_simple_x86;
23
24
25
static size_t
26
x86_code(void *simple_ptr, uint32_t now_pos, bool is_encoder,
27
    uint8_t *buffer, size_t size)
28
4.18k
{
29
4.18k
  static const uint32_t MASK_TO_BIT_NUMBER[5] = { 0, 1, 2, 2, 3 };
30
31
4.18k
  lzma_simple_x86 *simple = simple_ptr;
32
4.18k
  uint32_t prev_mask = simple->prev_mask;
33
4.18k
  uint32_t prev_pos = simple->prev_pos;
34
35
4.18k
  if (size < 5)
36
3.01k
    return 0;
37
38
1.17k
  if (now_pos - prev_pos > 5)
39
448
    prev_pos = now_pos - 5;
40
41
1.17k
  const size_t limit = size - 5;
42
1.17k
  size_t buffer_pos = 0;
43
44
15.2M
  while (buffer_pos <= limit) {
45
15.2M
    uint8_t b = buffer[buffer_pos];
46
15.2M
    if (b != 0xE8 && b != 0xE9) {
47
15.2M
      ++buffer_pos;
48
15.2M
      continue;
49
15.2M
    }
50
51
30.1k
    const uint32_t offset = now_pos + (uint32_t)(buffer_pos)
52
30.1k
        - prev_pos;
53
30.1k
    prev_pos = now_pos + (uint32_t)(buffer_pos);
54
55
30.1k
    if (offset > 5) {
56
9.76k
      prev_mask = 0;
57
20.3k
    } else {
58
43.7k
      for (uint32_t i = 0; i < offset; ++i) {
59
23.4k
        prev_mask &= 0x77;
60
23.4k
        prev_mask <<= 1;
61
23.4k
      }
62
20.3k
    }
63
64
30.1k
    b = buffer[buffer_pos + 4];
65
66
30.1k
    if (Test86MSByte(b) && (prev_mask >> 1) <= 4
67
3.47k
      && (prev_mask >> 1) != 3) {
68
69
2.96k
      uint32_t src = ((uint32_t)(b) << 24)
70
2.96k
        | ((uint32_t)(buffer[buffer_pos + 3]) << 16)
71
2.96k
        | ((uint32_t)(buffer[buffer_pos + 2]) << 8)
72
2.96k
        | (buffer[buffer_pos + 1]);
73
74
2.96k
      uint32_t dest;
75
3.90k
      while (true) {
76
3.90k
        if (is_encoder)
77
0
          dest = src + (now_pos + (uint32_t)(
78
0
              buffer_pos) + 5);
79
3.90k
        else
80
3.90k
          dest = src - (now_pos + (uint32_t)(
81
3.90k
              buffer_pos) + 5);
82
83
3.90k
        if (prev_mask == 0)
84
1.90k
          break;
85
86
1.99k
        const uint32_t i = MASK_TO_BIT_NUMBER[
87
1.99k
            prev_mask >> 1];
88
89
1.99k
        b = (uint8_t)(dest >> (24 - i * 8));
90
91
1.99k
        if (!Test86MSByte(b))
92
1.06k
          break;
93
94
934
        src = dest ^ ((1ull << (32 - i * 8)) - 1);
95
934
      }
96
97
2.96k
      buffer[buffer_pos + 4]
98
2.96k
          = (uint8_t)(~(((dest >> 24) & 1) - 1));
99
2.96k
      buffer[buffer_pos + 3] = (uint8_t)(dest >> 16);
100
2.96k
      buffer[buffer_pos + 2] = (uint8_t)(dest >> 8);
101
2.96k
      buffer[buffer_pos + 1] = (uint8_t)(dest);
102
2.96k
      buffer_pos += 5;
103
2.96k
      prev_mask = 0;
104
105
27.1k
    } else {
106
27.1k
      ++buffer_pos;
107
27.1k
      prev_mask |= 1;
108
27.1k
      if (Test86MSByte(b))
109
2.35k
        prev_mask |= 0x10;
110
27.1k
    }
111
30.1k
  }
112
113
1.17k
  simple->prev_mask = prev_mask;
114
1.17k
  simple->prev_pos = prev_pos;
115
116
1.17k
  return buffer_pos;
117
4.18k
}
118
119
120
static lzma_ret
121
x86_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
122
    const lzma_filter_info *filters, bool is_encoder)
123
2.61k
{
124
2.61k
  const lzma_ret ret = lzma_simple_coder_init(next, allocator, filters,
125
2.61k
      &x86_code, sizeof(lzma_simple_x86), 5, 1, is_encoder);
126
127
2.61k
  if (ret == LZMA_OK) {
128
2.60k
    lzma_simple_coder *coder = next->coder;
129
2.60k
    lzma_simple_x86 *simple = coder->simple;
130
2.60k
    simple->prev_mask = 0;
131
2.60k
    simple->prev_pos = (uint32_t)(-5);
132
2.60k
  }
133
134
2.61k
  return ret;
135
2.61k
}
136
137
138
#ifdef HAVE_ENCODER_X86
139
extern lzma_ret
140
lzma_simple_x86_encoder_init(lzma_next_coder *next,
141
    const lzma_allocator *allocator,
142
    const lzma_filter_info *filters)
143
0
{
144
0
  return x86_coder_init(next, allocator, filters, true);
145
0
}
146
#endif
147
148
149
#ifdef HAVE_DECODER_X86
150
extern lzma_ret
151
lzma_simple_x86_decoder_init(lzma_next_coder *next,
152
    const lzma_allocator *allocator,
153
    const lzma_filter_info *filters)
154
2.61k
{
155
  return x86_coder_init(next, allocator, filters, false);
156
2.61k
}
157
#endif