Coverage Report

Created: 2024-07-27 06:12

/src/lzma-fuzz/sdk/C/Bra.c
Line
Count
Source (jump to first uncovered line)
1
/* Bra.c -- Converters for RISC code
2
2017-04-04 : Igor Pavlov : Public domain */
3
4
#include "Precomp.h"
5
6
#include "CpuArch.h"
7
#include "Bra.h"
8
9
SizeT ARM_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
10
9
{
11
9
  Byte *p;
12
9
  const Byte *lim;
13
9
  size &= ~(size_t)3;
14
9
  ip += 4;
15
9
  p = data;
16
9
  lim = data + size;
17
18
9
  if (encoding)
19
20
0
  for (;;)
21
0
  {
22
0
    for (;;)
23
0
    {
24
0
      if (p >= lim)
25
0
        return p - data;
26
0
      p += 4;
27
0
      if (p[-1] == 0xEB)
28
0
        break;
29
0
    }
30
0
    {
31
0
      UInt32 v = GetUi32(p - 4);
32
0
      v <<= 2;
33
0
        v += ip + (UInt32)(p - data);
34
0
      v >>= 2;
35
0
      v &= 0x00FFFFFF;
36
0
      v |= 0xEB000000;
37
0
      SetUi32(p - 4, v);
38
0
    }
39
0
  }
40
41
9
  for (;;)
42
35
  {
43
35
    for (;;)
44
153
    {
45
153
      if (p >= lim)
46
9
        return p - data;
47
144
      p += 4;
48
144
      if (p[-1] == 0xEB)
49
26
        break;
50
144
    }
51
26
    {
52
26
      UInt32 v = GetUi32(p - 4);
53
26
      v <<= 2;
54
26
        v -= ip + (UInt32)(p - data);
55
26
      v >>= 2;
56
26
      v &= 0x00FFFFFF;
57
26
      v |= 0xEB000000;
58
26
      SetUi32(p - 4, v);
59
26
    }
60
26
  }
61
9
}
62
63
64
SizeT ARMT_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
65
5
{
66
5
  Byte *p;
67
5
  const Byte *lim;
68
5
  size &= ~(size_t)1;
69
5
  p = data;
70
5
  lim = data + size - 4;
71
72
5
  if (encoding)
73
  
74
0
  for (;;)
75
0
  {
76
0
    UInt32 b1;
77
0
    for (;;)
78
0
    {
79
0
      UInt32 b3;
80
0
      if (p > lim)
81
0
        return p - data;
82
0
      b1 = p[1];
83
0
      b3 = p[3];
84
0
      p += 2;
85
0
      b1 ^= 8;
86
0
      if ((b3 & b1) >= 0xF8)
87
0
        break;
88
0
    }
89
0
    {
90
0
      UInt32 v =
91
0
             ((UInt32)b1 << 19)
92
0
          + (((UInt32)p[1] & 0x7) << 8)
93
0
          + (((UInt32)p[-2] << 11))
94
0
          + (p[0]);
95
96
0
      p += 2;
97
0
      {
98
0
        UInt32 cur = (ip + (UInt32)(p - data)) >> 1;
99
0
          v += cur;
100
0
      }
101
102
0
      p[-4] = (Byte)(v >> 11);
103
0
      p[-3] = (Byte)(0xF0 | ((v >> 19) & 0x7));
104
0
      p[-2] = (Byte)v;
105
0
      p[-1] = (Byte)(0xF8 | (v >> 8));
106
0
    }
107
0
  }
108
  
109
5
  for (;;)
110
15
  {
111
15
    UInt32 b1;
112
15
    for (;;)
113
156
    {
114
156
      UInt32 b3;
115
156
      if (p > lim)
116
5
        return p - data;
117
151
      b1 = p[1];
118
151
      b3 = p[3];
119
151
      p += 2;
120
151
      b1 ^= 8;
121
151
      if ((b3 & b1) >= 0xF8)
122
10
        break;
123
151
    }
124
10
    {
125
10
      UInt32 v =
126
10
             ((UInt32)b1 << 19)
127
10
          + (((UInt32)p[1] & 0x7) << 8)
128
10
          + (((UInt32)p[-2] << 11))
129
10
          + (p[0]);
130
131
10
      p += 2;
132
10
      {
133
10
        UInt32 cur = (ip + (UInt32)(p - data)) >> 1;
134
10
          v -= cur;
135
10
      }
136
137
      /*
138
      SetUi16(p - 4, (UInt16)(((v >> 11) & 0x7FF) | 0xF000));
139
      SetUi16(p - 2, (UInt16)(v | 0xF800));
140
      */
141
      
142
10
      p[-4] = (Byte)(v >> 11);
143
10
      p[-3] = (Byte)(0xF0 | ((v >> 19) & 0x7));
144
10
      p[-2] = (Byte)v;
145
10
      p[-1] = (Byte)(0xF8 | (v >> 8));
146
10
    }
147
10
  }
148
5
}
149
150
151
SizeT PPC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
152
9
{
153
9
  Byte *p;
154
9
  const Byte *lim;
155
9
  size &= ~(size_t)3;
156
9
  ip -= 4;
157
9
  p = data;
158
9
  lim = data + size;
159
160
9
  for (;;)
161
40
  {
162
40
    for (;;)
163
153
    {
164
153
      if (p >= lim)
165
9
        return p - data;
166
144
      p += 4;
167
      /* if ((v & 0xFC000003) == 0x48000001) */
168
144
      if ((p[-4] & 0xFC) == 0x48 && (p[-1] & 3) == 1)
169
31
        break;
170
144
    }
171
31
    {
172
31
      UInt32 v = GetBe32(p - 4);
173
31
      if (encoding)
174
0
        v += ip + (UInt32)(p - data);
175
31
      else
176
31
        v -= ip + (UInt32)(p - data);
177
31
      v &= 0x03FFFFFF;
178
31
      v |= 0x48000000;
179
31
      SetBe32(p - 4, v);
180
31
    }
181
31
  }
182
9
}
183
184
185
SizeT SPARC_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
186
8
{
187
8
  Byte *p;
188
8
  const Byte *lim;
189
8
  size &= ~(size_t)3;
190
8
  ip -= 4;
191
8
  p = data;
192
8
  lim = data + size;
193
194
8
  for (;;)
195
8
  {
196
8
    for (;;)
197
136
    {
198
136
      if (p >= lim)
199
8
        return p - data;
200
      /*
201
      v = GetBe32(p);
202
      p += 4;
203
      m = v + ((UInt32)5 << 29);
204
      m ^= (UInt32)7 << 29;
205
      m += (UInt32)1 << 22;
206
      if ((m & ((UInt32)0x1FF << 23)) == 0)
207
        break;
208
      */
209
128
      p += 4;
210
128
      if ((p[-4] == 0x40 && (p[-3] & 0xC0) == 0) ||
211
128
          (p[-4] == 0x7F && (p[-3] >= 0xC0)))
212
0
        break;
213
128
    }
214
0
    {
215
0
      UInt32 v = GetBe32(p - 4);
216
0
      v <<= 2;
217
0
      if (encoding)
218
0
        v += ip + (UInt32)(p - data);
219
0
      else
220
0
        v -= ip + (UInt32)(p - data);
221
      
222
0
      v &= 0x01FFFFFF;
223
0
      v -= (UInt32)1 << 24;
224
0
      v ^= 0xFF000000;
225
0
      v >>= 2;
226
0
      v |= 0x40000000;
227
0
      SetBe32(p - 4, v);
228
0
    }
229
0
  }
230
8
}