Coverage Report

Created: 2023-09-25 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.42M
{
27
3.42M
  assert(ss);
28
3.42M
  ss->index = 0;
29
3.42M
  ss->buffer[0] = '\0';
30
3.42M
  ss->is_closed = false;
31
3.42M
}
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
19.0M
{
54
19.0M
#ifndef CAPSTONE_DIET
55
19.0M
  SSTREAM_RETURN_IF_CLOSED(ss);
56
19.0M
  if (s[0] == '\0')
57
4.89M
    return;
58
14.1M
  unsigned int len = (unsigned int) strlen(s);
59
60
14.1M
  memcpy(ss->buffer + ss->index, s, len);
61
14.1M
  ss->index += len;
62
14.1M
  ss->buffer[ss->index] = '\0';
63
14.1M
#endif
64
14.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
869k
{
71
869k
#ifndef CAPSTONE_DIET
72
869k
  SSTREAM_RETURN_IF_CLOSED(ss);
73
869k
  if (c == '\0')
74
0
    return;
75
869k
  ss->buffer[ss->index] = c;
76
869k
  ss->index++;
77
869k
  ss->buffer[ss->index] = '\0';
78
869k
#endif
79
869k
}
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
8.64M
{
86
8.64M
#ifndef CAPSTONE_DIET
87
8.64M
  SSTREAM_RETURN_IF_CLOSED(ss);
88
8.64M
  va_list ap;
89
8.64M
  int ret;
90
91
8.64M
  va_start(ap, fmt);
92
8.64M
  ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
93
8.64M
  va_end(ap);
94
8.64M
  ss->index += ret;
95
8.64M
#endif
96
8.64M
}
97
98
// print number with prefix #
99
void printInt64Bang(SStream *O, int64_t val)
100
89.2k
{
101
89.2k
  SSTREAM_RETURN_IF_CLOSED(O);
102
89.2k
  if (val >= 0) {
103
66.8k
    if (val > HEX_THRESHOLD)
104
57.3k
      SStream_concat(O, "#0x%"PRIx64, val);
105
9.55k
    else
106
9.55k
      SStream_concat(O, "#%"PRIu64, val);
107
66.8k
  } else {
108
22.3k
    if (val <- HEX_THRESHOLD) {
109
20.6k
      if (val == LONG_MIN)
110
67
        SStream_concat(O, "#-0x%"PRIx64, (uint64_t)val);
111
20.5k
      else
112
20.5k
        SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val);
113
20.6k
    } else
114
1.74k
      SStream_concat(O, "#-%"PRIu64, -val);
115
22.3k
  }
116
89.2k
}
117
118
void printUInt64Bang(SStream *O, uint64_t val)
119
72.6k
{
120
72.6k
  SSTREAM_RETURN_IF_CLOSED(O);
121
72.6k
  if (val > HEX_THRESHOLD)
122
49.0k
    SStream_concat(O, "#0x%"PRIx64, val);
123
23.5k
  else
124
23.5k
    SStream_concat(O, "#%"PRIu64, val);
125
72.6k
}
126
127
// print number
128
void printInt64(SStream *O, int64_t val)
129
678k
{
130
678k
  SSTREAM_RETURN_IF_CLOSED(O);
131
678k
  if (val >= 0) {
132
611k
    if (val > HEX_THRESHOLD)
133
355k
      SStream_concat(O, "0x%"PRIx64, val);
134
256k
    else
135
256k
      SStream_concat(O, "%"PRIu64, val);
136
611k
  } else {
137
66.4k
    if (val <- HEX_THRESHOLD) {
138
62.5k
      if (val == LONG_MIN)
139
0
        SStream_concat(O, "-0x%"PRIx64, (uint64_t)val);
140
62.5k
      else
141
62.5k
        SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val);
142
62.5k
    } else
143
3.88k
      SStream_concat(O, "-%"PRIu64, -val);
144
66.4k
  }
145
678k
}
146
147
void printUInt64(SStream *O, uint64_t val)
148
60.6k
{
149
60.6k
  SSTREAM_RETURN_IF_CLOSED(O);
150
60.6k
  if (val > HEX_THRESHOLD)
151
60.6k
    SStream_concat(O, "0x%"PRIx64, val);
152
7
  else
153
7
    SStream_concat(O, "%"PRIu64, val);
154
60.6k
}
155
156
// print number in decimal mode
157
void printInt32BangDec(SStream *O, int32_t val)
158
14.9k
{
159
14.9k
  SSTREAM_RETURN_IF_CLOSED(O);
160
14.9k
  if (val >= 0)
161
14.9k
    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
14.9k
}
169
170
void printInt32Bang(SStream *O, int32_t val)
171
114k
{
172
114k
  SSTREAM_RETURN_IF_CLOSED(O);
173
114k
  if (val >= 0) {
174
90.9k
    if (val > HEX_THRESHOLD)
175
64.9k
      SStream_concat(O, "#0x%x", val);
176
26.0k
    else
177
26.0k
      SStream_concat(O, "#%u", val);
178
90.9k
  } else {
179
23.7k
    if (val <- HEX_THRESHOLD) {
180
21.5k
      if (val == INT_MIN)
181
0
        SStream_concat(O, "#-0x%x", (uint32_t)val);
182
21.5k
      else
183
21.5k
        SStream_concat(O, "#-0x%x", (uint32_t)-val);
184
21.5k
    } else
185
2.15k
      SStream_concat(O, "#-%u", -val);
186
23.7k
  }
187
114k
}
188
189
void printInt32(SStream *O, int32_t val)
190
91.6k
{
191
91.6k
  SSTREAM_RETURN_IF_CLOSED(O);
192
91.6k
  if (val >= 0) {
193
79.6k
    if (val > HEX_THRESHOLD)
194
27.0k
      SStream_concat(O, "0x%x", val);
195
52.5k
    else
196
52.5k
      SStream_concat(O, "%u", val);
197
79.6k
  } else {
198
12.0k
    if (val <- HEX_THRESHOLD) {
199
11.7k
      if (val == INT_MIN)
200
38
        SStream_concat(O, "-0x%x", (uint32_t)val);
201
11.6k
      else
202
11.6k
        SStream_concat(O, "-0x%x", (uint32_t)-val);
203
11.7k
    } else
204
363
      SStream_concat(O, "-%u", -val);
205
12.0k
  }
206
91.6k
}
207
208
void printUInt32Bang(SStream *O, uint32_t val)
209
237k
{
210
237k
  SSTREAM_RETURN_IF_CLOSED(O);
211
237k
  if (val > HEX_THRESHOLD)
212
194k
    SStream_concat(O, "#0x%x", val);
213
43.0k
  else
214
43.0k
    SStream_concat(O, "#%u", val);
215
237k
}
216
217
void printUInt32(SStream *O, uint32_t val)
218
78.2k
{
219
78.2k
  SSTREAM_RETURN_IF_CLOSED(O);
220
78.2k
  if (val > HEX_THRESHOLD)
221
64.0k
    SStream_concat(O, "0x%x", val);
222
14.2k
  else
223
14.2k
    SStream_concat(O, "%u", val);
224
78.2k
}
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
608
{
234
608
  SSTREAM_RETURN_IF_CLOSED(O);
235
608
  SStream_concat(O, "#%e", val);
236
608
}