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