• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3 
4 #if !defined(_MSC_VER) || !defined(_KERNEL_MODE)
5 #include <stdint.h>
6 #endif
7 #include <stdarg.h>
8 #if defined(CAPSTONE_HAS_OSXKERNEL)
9 #include <libkern/libkern.h>
10 #else
11 #include <stdio.h>
12 #endif
13 #include <string.h>
14 
15 #include <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,char * s)31 void SStream_concat0(SStream *ss, 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 			SStream_concat(O, "#-0x%"PRIx64, -val);
66 		else
67 			SStream_concat(O, "#-%"PRIu64, -val);
68 	}
69 }
70 
printUInt64Bang(SStream * O,uint64_t val)71 void printUInt64Bang(SStream *O, uint64_t val)
72 {
73 	if (val > HEX_THRESHOLD)
74 		SStream_concat(O, "#0x%"PRIx64, val);
75 	else
76 		SStream_concat(O, "#%"PRIu64, val);
77 }
78 
79 // print number
printInt64(SStream * O,int64_t val)80 void printInt64(SStream *O, int64_t val)
81 {
82 	if (val >= 0) {
83 		if (val > HEX_THRESHOLD)
84 			SStream_concat(O, "0x%"PRIx64, val);
85 		else
86 			SStream_concat(O, "%"PRIu64, val);
87 	} else {
88 		if (val <- HEX_THRESHOLD)
89 			SStream_concat(O, "-0x%"PRIx64, -val);
90 		else
91 			SStream_concat(O, "-%"PRIu64, -val);
92 	}
93 }
94 
95 // print number in decimal mode
printInt32BangDec(SStream * O,int32_t val)96 void printInt32BangDec(SStream *O, int32_t val)
97 {
98 	if (val >= 0)
99 		SStream_concat(O, "#%u", val);
100 	else
101 		SStream_concat(O, "#-%u", -val);
102 }
103 
printInt32Bang(SStream * O,int32_t val)104 void printInt32Bang(SStream *O, int32_t val)
105 {
106 	if (val >= 0) {
107 		if (val > HEX_THRESHOLD)
108 			SStream_concat(O, "#0x%x", val);
109 		else
110 			SStream_concat(O, "#%u", val);
111 	} else {
112 		if (val <- HEX_THRESHOLD)
113 			SStream_concat(O, "#-0x%x", -val);
114 		else
115 			SStream_concat(O, "#-%u", -val);
116 	}
117 }
118 
printInt32(SStream * O,int32_t val)119 void printInt32(SStream *O, int32_t val)
120 {
121 	if (val >= 0) {
122 		if (val > HEX_THRESHOLD)
123 			SStream_concat(O, "0x%x", val);
124 		else
125 			SStream_concat(O, "%u", val);
126 	} else {
127 		if (val <- HEX_THRESHOLD)
128 			SStream_concat(O, "-0x%x", -val);
129 		else
130 			SStream_concat(O, "-%u", -val);
131 	}
132 }
133 
printUInt32Bang(SStream * O,uint32_t val)134 void printUInt32Bang(SStream *O, uint32_t val)
135 {
136 	if (val > HEX_THRESHOLD)
137 		SStream_concat(O, "#0x%x", val);
138 	else
139 		SStream_concat(O, "#%u", val);
140 }
141 
printUInt32(SStream * O,uint32_t val)142 void printUInt32(SStream *O, uint32_t val)
143 {
144 	if (val > HEX_THRESHOLD)
145 		SStream_concat(O, "0x%x", val);
146 	else
147 		SStream_concat(O, "%u", val);
148 }
149 
150 /*
151    int main()
152    {
153    SStream ss;
154    int64_t i;
155 
156    SStream_Init(&ss);
157 
158    SStream_concat(&ss, "hello ");
159    SStream_concat(&ss, "%d - 0x%x", 200, 16);
160 
161    i = 123;
162    SStream_concat(&ss, " + %ld", i);
163    SStream_concat(&ss, "%s", "haaaaa");
164 
165    printf("%s\n", ss.buffer);
166 
167    return 0;
168    }
169  */
170