1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
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
SStream_Init(SStream * ss)25 void SStream_Init(SStream *ss)
26 {
27 ss->index = 0;
28 ss->buffer[0] = '\0';
29 }
30
SStream_concat0(SStream * ss,const char * s)31 void SStream_concat0(SStream *ss, const char *s)
32 {
33 #ifndef CAPSTONE_DIET
34 unsigned int len = (unsigned int) strlen(s);
35
36 memcpy(ss->buffer + ss->index, s, len);
37 ss->index += len;
38 ss->buffer[ss->index] = '\0';
39 #endif
40 }
41
SStream_concat(SStream * ss,const char * fmt,...)42 void SStream_concat(SStream *ss, const char *fmt, ...)
43 {
44 #ifndef CAPSTONE_DIET
45 va_list ap;
46 int ret;
47
48 va_start(ap, fmt);
49 ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
50 va_end(ap);
51 ss->index += ret;
52 #endif
53 }
54
55 // print number with prefix #
printInt64Bang(SStream * O,int64_t val)56 void printInt64Bang(SStream *O, int64_t val)
57 {
58 if (val >= 0) {
59 if (val > HEX_THRESHOLD)
60 SStream_concat(O, "#0x%"PRIx64, val);
61 else
62 SStream_concat(O, "#%"PRIu64, val);
63 } else {
64 if (val <- HEX_THRESHOLD) {
65 if (val == LONG_MIN)
66 SStream_concat(O, "#-0x%"PRIx64, (uint64_t)val);
67 else
68 SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val);
69 }
70 else
71 SStream_concat(O, "#-%"PRIu64, -val);
72 }
73 }
74
printUInt64Bang(SStream * O,uint64_t val)75 void printUInt64Bang(SStream *O, uint64_t val)
76 {
77 if (val > HEX_THRESHOLD)
78 SStream_concat(O, "#0x%"PRIx64, val);
79 else
80 SStream_concat(O, "#%"PRIu64, val);
81 }
82
83 // print number
printInt64(SStream * O,int64_t val)84 void printInt64(SStream *O, int64_t val)
85 {
86 if (val >= 0) {
87 if (val > HEX_THRESHOLD)
88 SStream_concat(O, "0x%"PRIx64, val);
89 else
90 SStream_concat(O, "%"PRIu64, val);
91 } else {
92 if (val <- HEX_THRESHOLD) {
93 if (val == LONG_MIN)
94 SStream_concat(O, "-0x%"PRIx64, (uint64_t)val);
95 else
96 SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val);
97 }
98 else
99 SStream_concat(O, "-%"PRIu64, -val);
100 }
101 }
102
103 // print number in decimal mode
printInt32BangDec(SStream * O,int32_t val)104 void printInt32BangDec(SStream *O, int32_t val)
105 {
106 if (val >= 0)
107 SStream_concat(O, "#%u", val);
108 else
109 if (val == INT_MIN)
110 SStream_concat(O, "#-%u", val);
111 else
112 SStream_concat(O, "#-%u", (uint32_t)-val);
113 }
114
printInt32Bang(SStream * O,int32_t val)115 void printInt32Bang(SStream *O, int32_t val)
116 {
117 if (val >= 0) {
118 if (val > HEX_THRESHOLD)
119 SStream_concat(O, "#0x%x", val);
120 else
121 SStream_concat(O, "#%u", val);
122 } else {
123 if (val <- HEX_THRESHOLD) {
124 if (val == INT_MIN)
125 SStream_concat(O, "#-0x%x", (uint32_t)val);
126 else
127 SStream_concat(O, "#-0x%x", (uint32_t)-val);
128 }
129 else
130 SStream_concat(O, "#-%u", -val);
131 }
132 }
133
printInt32(SStream * O,int32_t val)134 void printInt32(SStream *O, int32_t val)
135 {
136 if (val >= 0) {
137 if (val > HEX_THRESHOLD)
138 SStream_concat(O, "0x%x", val);
139 else
140 SStream_concat(O, "%u", val);
141 } else {
142 if (val <- HEX_THRESHOLD) {
143 if (val == INT_MIN)
144 SStream_concat(O, "-0x%x", (uint32_t)val);
145 else
146 SStream_concat(O, "-0x%x", (uint32_t)-val);
147 }
148 else
149 SStream_concat(O, "-%u", -val);
150 }
151 }
152
printUInt32Bang(SStream * O,uint32_t val)153 void printUInt32Bang(SStream *O, uint32_t val)
154 {
155 if (val > HEX_THRESHOLD)
156 SStream_concat(O, "#0x%x", val);
157 else
158 SStream_concat(O, "#%u", val);
159 }
160
printUInt32(SStream * O,uint32_t val)161 void printUInt32(SStream *O, uint32_t val)
162 {
163 if (val > HEX_THRESHOLD)
164 SStream_concat(O, "0x%x", val);
165 else
166 SStream_concat(O, "%u", val);
167 }
168
169 /*
170 int main()
171 {
172 SStream ss;
173 int64_t i;
174
175 SStream_Init(&ss);
176
177 SStream_concat(&ss, "hello ");
178 SStream_concat(&ss, "%d - 0x%x", 200, 16);
179
180 i = 123;
181 SStream_concat(&ss, " + %ld", i);
182 SStream_concat(&ss, "%s", "haaaaa");
183
184 printf("%s\n", ss.buffer);
185
186 return 0;
187 }
188 */
189