Coverage Report

Created: 2024-08-21 06:24

/src/capstonenext/SStream.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3
4
#include <stdarg.h>
5
#if defined(CAPSTONE_HAS_OSXKERNEL)
6
#include <Availability.h>
7
#include <libkern/libkern.h>
8
#include <i386/limits.h>
9
#else
10
#include <stdio.h>
11
#include <limits.h>
12
#endif
13
#include <string.h>
14
15
#include <capstone/platform.h>
16
17
#include "SStream.h"
18
#include "cs_priv.h"
19
#include "utils.h"
20
21
#ifdef _MSC_VER
22
#pragma warning(disable: 4996) // disable MSVC's warning on strcpy()
23
#endif
24
25
void SStream_Init(SStream *ss)
26
3.94M
{
27
3.94M
  assert(ss);
28
3.94M
  ss->index = 0;
29
3.94M
  ss->buffer[0] = '\0';
30
3.94M
  ss->is_closed = false;
31
3.94M
}
32
33
/**
34
 * Open the output stream. Every write attempt is accepted again.
35
 */
36
0
void SStream_Open(SStream *ss) {
37
0
  assert(ss);
38
0
  ss->is_closed = false;
39
0
}
40
41
/**
42
 * Closes the output stream. Every write attempt is ignored.
43
 */
44
0
void SStream_Close(SStream *ss) {
45
0
  assert(ss);
46
0
  ss->is_closed = true;
47
0
}
48
49
/**
50
 * Copy the string \p s to the buffer of \p ss and terminate it with a '\\0' byte.
51
 */
52
void SStream_concat0(SStream *ss, const char *s)
53
22.3M
{
54
22.3M
#ifndef CAPSTONE_DIET
55
22.3M
  SSTREAM_RETURN_IF_CLOSED(ss);
56
22.3M
  if (s[0] == '\0')
57
7.22M
    return;
58
15.1M
  unsigned int len = (unsigned int) strlen(s);
59
60
15.1M
  memcpy(ss->buffer + ss->index, s, len);
61
15.1M
  ss->index += len;
62
15.1M
  ss->buffer[ss->index] = '\0';
63
15.1M
#endif
64
15.1M
}
65
66
/**
67
 * Copy the single char \p c to the buffer of \p ss.
68
 */
69
void SStream_concat1(SStream *ss, const char c)
70
1.32M
{
71
1.32M
#ifndef CAPSTONE_DIET
72
1.32M
  SSTREAM_RETURN_IF_CLOSED(ss);
73
1.32M
  if (c == '\0')
74
21.6k
    return;
75
1.30M
  ss->buffer[ss->index] = c;
76
1.30M
  ss->index++;
77
1.30M
  ss->buffer[ss->index] = '\0';
78
1.30M
#endif
79
1.30M
}
80
81
/**
82
 * Copy all strings given to the buffer of \p ss according to formatting \p fmt.
83
 */
84
void SStream_concat(SStream *ss, const char *fmt, ...)
85
10.9M
{
86
10.9M
#ifndef CAPSTONE_DIET
87
10.9M
  SSTREAM_RETURN_IF_CLOSED(ss);
88
10.9M
  va_list ap;
89
10.9M
  int ret;
90
91
10.9M
  va_start(ap, fmt);
92
10.9M
  ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
93
10.9M
  va_end(ap);
94
10.9M
  ss->index += ret;
95
10.9M
#endif
96
10.9M
}
97
98
// print number with prefix #
99
void printInt64Bang(SStream *O, int64_t val)
100
100k
{
101
100k
  SSTREAM_RETURN_IF_CLOSED(O);
102
100k
  if (val >= 0) {
103
94.4k
    if (val > HEX_THRESHOLD)
104
63.5k
      SStream_concat(O, "#0x%"PRIx64, val);
105
30.8k
    else
106
30.8k
      SStream_concat(O, "#%"PRIu64, val);
107
94.4k
  } else {
108
5.71k
    if (val <- HEX_THRESHOLD) {
109
4.85k
      if (val == LONG_MIN)
110
25
        SStream_concat(O, "#-0x%"PRIx64, (uint64_t)val);
111
4.82k
      else
112
4.82k
        SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val);
113
4.85k
    } else
114
860
      SStream_concat(O, "#-%"PRIu64, -val);
115
5.71k
  }
116
100k
}
117
118
void printUInt64Bang(SStream *O, uint64_t val)
119
14.1k
{
120
14.1k
  SSTREAM_RETURN_IF_CLOSED(O);
121
14.1k
  if (val > HEX_THRESHOLD)
122
13.2k
    SStream_concat(O, "#0x%"PRIx64, val);
123
864
  else
124
864
    SStream_concat(O, "#%"PRIu64, val);
125
14.1k
}
126
127
// print number
128
void printInt64(SStream *O, int64_t val)
129
762k
{
130
762k
  SSTREAM_RETURN_IF_CLOSED(O);
131
762k
  if (val >= 0) {
132
690k
    if (val > HEX_THRESHOLD)
133
425k
      SStream_concat(O, "0x%"PRIx64, val);
134
264k
    else
135
264k
      SStream_concat(O, "%"PRIu64, val);
136
690k
  } else {
137
72.3k
    if (val <- HEX_THRESHOLD) {
138
68.4k
      if (val == LONG_MIN)
139
0
        SStream_concat(O, "-0x%"PRIx64, (uint64_t)val);
140
68.4k
      else
141
68.4k
        SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val);
142
68.4k
    } else
143
3.83k
      SStream_concat(O, "-%"PRIu64, -val);
144
72.3k
  }
145
762k
}
146
147
void printUInt64(SStream *O, uint64_t val)
148
140k
{
149
140k
  SSTREAM_RETURN_IF_CLOSED(O);
150
140k
  if (val > HEX_THRESHOLD)
151
102k
    SStream_concat(O, "0x%"PRIx64, val);
152
37.9k
  else
153
37.9k
    SStream_concat(O, "%"PRIu64, val);
154
140k
}
155
156
// print number in decimal mode
157
void printInt32BangDec(SStream *O, int32_t val)
158
0
{
159
0
  SSTREAM_RETURN_IF_CLOSED(O);
160
0
  if (val >= 0)
161
0
    SStream_concat(O, "#%u", val);
162
0
  else {
163
0
    if (val == INT_MIN)
164
0
      SStream_concat(O, "#-%u", val);
165
0
    else
166
0
      SStream_concat(O, "#-%u", (uint32_t)-val);
167
0
  }
168
0
}
169
170
void printInt32Bang(SStream *O, int32_t val)
171
231k
{
172
231k
  SSTREAM_RETURN_IF_CLOSED(O);
173
231k
  if (val >= 0) {
174
172k
    if (val > HEX_THRESHOLD)
175
91.9k
      SStream_concat(O, "#0x%x", val);
176
80.5k
    else
177
80.5k
      SStream_concat(O, "#%u", val);
178
172k
  } else {
179
58.6k
    if (val <- HEX_THRESHOLD) {
180
52.9k
      if (val == INT_MIN)
181
0
        SStream_concat(O, "#-0x%x", (uint32_t)val);
182
52.9k
      else
183
52.9k
        SStream_concat(O, "#-0x%x", (uint32_t)-val);
184
52.9k
    } else
185
5.78k
      SStream_concat(O, "#-%u", -val);
186
58.6k
  }
187
231k
}
188
189
void printInt32(SStream *O, int32_t val)
190
57.3k
{
191
57.3k
  SSTREAM_RETURN_IF_CLOSED(O);
192
57.3k
  if (val >= 0) {
193
42.2k
    if (val > HEX_THRESHOLD)
194
29.9k
      SStream_concat(O, "0x%x", val);
195
12.2k
    else
196
12.2k
      SStream_concat(O, "%u", val);
197
42.2k
  } else {
198
15.0k
    if (val <- HEX_THRESHOLD) {
199
14.7k
      if (val == INT_MIN)
200
78
        SStream_concat(O, "-0x%x", (uint32_t)val);
201
14.7k
      else
202
14.7k
        SStream_concat(O, "-0x%x", (uint32_t)-val);
203
14.7k
    } else
204
279
      SStream_concat(O, "-%u", -val);
205
15.0k
  }
206
57.3k
}
207
208
void printUInt32Bang(SStream *O, uint32_t val)
209
304k
{
210
304k
  SSTREAM_RETURN_IF_CLOSED(O);
211
304k
  if (val > HEX_THRESHOLD)
212
253k
    SStream_concat(O, "#0x%x", val);
213
50.7k
  else
214
50.7k
    SStream_concat(O, "#%u", val);
215
304k
}
216
217
void printUInt32(SStream *O, uint32_t val)
218
112k
{
219
112k
  SSTREAM_RETURN_IF_CLOSED(O);
220
112k
  if (val > HEX_THRESHOLD)
221
74.6k
    SStream_concat(O, "0x%x", val);
222
38.1k
  else
223
38.1k
    SStream_concat(O, "%u", val);
224
112k
}
225
226
void printFloat(SStream *O, float val)
227
0
{
228
0
  SSTREAM_RETURN_IF_CLOSED(O);
229
0
  SStream_concat(O, "%e", val);
230
0
}
231
232
void printFloatBang(SStream *O, float val)
233
1.09k
{
234
1.09k
  SSTREAM_RETURN_IF_CLOSED(O);
235
1.09k
  SStream_concat(O, "#%e", val);
236
1.09k
}