blob: 7bab4beb4fa7543c8595a34355bd530d0a419ca8 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
4#include <stdint.h>
5#include <stdarg.h>
6#include <stdio.h>
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +08007#include <string.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08008
9#include "SStream.h"
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080010#include "cs_priv.h"
Yegor Derevenetsced9d242014-09-21 17:27:11 +020011#include "inttypes.h"
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080012#include "utils.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080013
Nguyen Anh Quynh476d5ad2014-06-17 15:09:59 +080014#ifdef _MSC_VER
15#pragma warning(disable: 4996) // disable MSVC's warning on strcpy()
16#endif
17
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018void SStream_Init(SStream *ss)
19{
20 ss->index = 0;
21 ss->buffer[0] = '\0';
22}
23
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +080024void SStream_concat0(SStream *ss, char *s)
25{
26#ifndef CAPSTONE_DIET
flyingsymbolsd91f9642014-10-22 03:21:43 -040027 unsigned int len = (unsigned int) strlen(s);
Nguyen Anh Quynhea3c0892014-10-02 10:17:55 +080028
29 memcpy(ss->buffer + ss->index, s, len);
30 ss->index += len;
31 ss->buffer[ss->index] = '\0';
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +080032#endif
33}
34
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035void SStream_concat(SStream *ss, const char *fmt, ...)
36{
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080037#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038 va_list ap;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +080039 int ret;
40
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080041 va_start(ap, fmt);
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +010042 ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043 va_end(ap);
44 ss->index += ret;
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080045#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080046}
47
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080048// print number with prefix #
49void printInt64Bang(SStream *O, int64_t val)
50{
51 if (val >= 0) {
52 if (val > HEX_THRESHOLD)
53 SStream_concat(O, "#0x%"PRIx64, val);
54 else
55 SStream_concat(O, "#%"PRIu64, val);
56 } else {
57 if (val <- HEX_THRESHOLD)
58 SStream_concat(O, "#-0x%"PRIx64, -val);
59 else
60 SStream_concat(O, "#-%"PRIu64, -val);
61 }
62}
63
Nguyen Anh Quynh03a18362014-12-12 22:06:06 +080064void printUInt64Bang(SStream *O, uint64_t val)
65{
66 if (val > HEX_THRESHOLD)
67 SStream_concat(O, "#0x%"PRIx64, val);
68 else
69 SStream_concat(O, "#%"PRIu64, val);
70}
71
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080072// print number
73void printInt64(SStream *O, int64_t val)
74{
75 if (val >= 0) {
76 if (val > HEX_THRESHOLD)
77 SStream_concat(O, "0x%"PRIx64, val);
78 else
79 SStream_concat(O, "%"PRIu64, val);
80 } else {
81 if (val <- HEX_THRESHOLD)
82 SStream_concat(O, "-0x%"PRIx64, -val);
83 else
84 SStream_concat(O, "-%"PRIu64, -val);
85 }
86}
87
Nguyen Anh Quynhc109e8e2014-11-08 13:58:50 +080088// print number in decimal mode
89void printInt32BangDec(SStream *O, int32_t val)
90{
91 if (val >= 0)
92 SStream_concat(O, "#%u", val);
93 else
94 SStream_concat(O, "#-%u", -val);
95}
96
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080097void printInt32Bang(SStream *O, int32_t val)
98{
99 if (val >= 0) {
100 if (val > HEX_THRESHOLD)
101 SStream_concat(O, "#0x%x", val);
102 else
103 SStream_concat(O, "#%u", val);
104 } else {
105 if (val <- HEX_THRESHOLD)
106 SStream_concat(O, "#-0x%x", -val);
107 else
108 SStream_concat(O, "#-%u", -val);
109 }
110}
111
112void printInt32(SStream *O, int32_t val)
113{
114 if (val >= 0) {
115 if (val > HEX_THRESHOLD)
116 SStream_concat(O, "0x%x", val);
117 else
118 SStream_concat(O, "%u", val);
119 } else {
120 if (val <- HEX_THRESHOLD)
121 SStream_concat(O, "-0x%x", -val);
122 else
123 SStream_concat(O, "-%u", -val);
124 }
125}
126
127void printUInt32Bang(SStream *O, uint32_t val)
128{
129 if (val > HEX_THRESHOLD)
130 SStream_concat(O, "#0x%x", val);
131 else
132 SStream_concat(O, "#%u", val);
133}
134
135void printUInt32(SStream *O, uint32_t val)
136{
137 if (val > HEX_THRESHOLD)
138 SStream_concat(O, "0x%x", val);
139 else
140 SStream_concat(O, "%u", val);
141}
142
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800143/*
144 int main()
145 {
146 SStream ss;
147 int64_t i;
148
149 SStream_Init(&ss);
150
151 SStream_concat(&ss, "hello ");
152 SStream_concat(&ss, "%d - 0x%x", 200, 16);
153
154 i = 123;
155 SStream_concat(&ss, " + %ld", i);
156 SStream_concat(&ss, "%s", "haaaaa");
157
158 printf("%s\n", ss.buffer);
159
160 return 0;
161 }
162 */