blob: a332f89beb6237554781764557af9ad8c65118c8 [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
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08004#include <stdarg.h>
reverser160e1982015-04-09 18:28:19 +01005#if defined(CAPSTONE_HAS_OSXKERNEL)
6#include <libkern/libkern.h>
7#else
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08008#include <stdio.h>
reverser160e1982015-04-09 18:28:19 +01009#endif
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +080010#include <string.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011
tandasat45e5eab2016-05-11 21:48:32 -070012#include <capstone/platform.h>
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include "SStream.h"
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080015#include "cs_priv.h"
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080016#include "utils.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017
Nguyen Anh Quynh476d5ad2014-06-17 15:09:59 +080018#ifdef _MSC_VER
19#pragma warning(disable: 4996) // disable MSVC's warning on strcpy()
20#endif
21
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022void SStream_Init(SStream *ss)
23{
24 ss->index = 0;
25 ss->buffer[0] = '\0';
26}
27
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +080028void SStream_concat0(SStream *ss, char *s)
29{
30#ifndef CAPSTONE_DIET
flyingsymbolsd91f9642014-10-22 03:21:43 -040031 unsigned int len = (unsigned int) strlen(s);
Nguyen Anh Quynhea3c0892014-10-02 10:17:55 +080032
33 memcpy(ss->buffer + ss->index, s, len);
34 ss->index += len;
35 ss->buffer[ss->index] = '\0';
Nguyen Anh Quynhb76233c2014-06-04 18:31:02 +080036#endif
37}
38
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080039void SStream_concat(SStream *ss, const char *fmt, ...)
40{
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080041#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080042 va_list ap;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +080043 int ret;
44
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080045 va_start(ap, fmt);
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +010046 ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080047 va_end(ap);
48 ss->index += ret;
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +080049#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050}
51
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080052// print number with prefix #
53void printInt64Bang(SStream *O, int64_t val)
54{
55 if (val >= 0) {
56 if (val > HEX_THRESHOLD)
57 SStream_concat(O, "#0x%"PRIx64, val);
58 else
59 SStream_concat(O, "#%"PRIu64, val);
60 } else {
61 if (val <- HEX_THRESHOLD)
Nguyen Anh Quynh206f5ff2017-12-15 10:17:56 +080062 SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val);
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080063 else
64 SStream_concat(O, "#-%"PRIu64, -val);
65 }
66}
67
Nguyen Anh Quynh03a18362014-12-12 22:06:06 +080068void printUInt64Bang(SStream *O, uint64_t val)
69{
70 if (val > HEX_THRESHOLD)
71 SStream_concat(O, "#0x%"PRIx64, val);
72 else
73 SStream_concat(O, "#%"PRIu64, val);
74}
75
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080076// print number
77void printInt64(SStream *O, int64_t val)
78{
79 if (val >= 0) {
80 if (val > HEX_THRESHOLD)
81 SStream_concat(O, "0x%"PRIx64, val);
82 else
83 SStream_concat(O, "%"PRIu64, val);
84 } else {
85 if (val <- HEX_THRESHOLD)
Nguyen Anh Quynh206f5ff2017-12-15 10:17:56 +080086 SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val);
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +080087 else
88 SStream_concat(O, "-%"PRIu64, -val);
89 }
90}
91
Nguyen Anh Quynhc109e8e2014-11-08 13:58:50 +080092// print number in decimal mode
93void printInt32BangDec(SStream *O, int32_t val)
94{
95 if (val >= 0)
96 SStream_concat(O, "#%u", val);
97 else
Nguyen Anh Quynh206f5ff2017-12-15 10:17:56 +080098 SStream_concat(O, "#-%u", (uint32_t)-val);
Nguyen Anh Quynhc109e8e2014-11-08 13:58:50 +080099}
100
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +0800101void printInt32Bang(SStream *O, int32_t val)
102{
103 if (val >= 0) {
104 if (val > HEX_THRESHOLD)
105 SStream_concat(O, "#0x%x", val);
106 else
107 SStream_concat(O, "#%u", val);
108 } else {
109 if (val <- HEX_THRESHOLD)
Nguyen Anh Quynh206f5ff2017-12-15 10:17:56 +0800110 SStream_concat(O, "#-0x%x", (uint32_t)-val);
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +0800111 else
112 SStream_concat(O, "#-%u", -val);
113 }
114}
115
116void printInt32(SStream *O, int32_t val)
117{
118 if (val >= 0) {
119 if (val > HEX_THRESHOLD)
120 SStream_concat(O, "0x%x", val);
121 else
122 SStream_concat(O, "%u", val);
123 } else {
124 if (val <- HEX_THRESHOLD)
Nguyen Anh Quynh206f5ff2017-12-15 10:17:56 +0800125 SStream_concat(O, "-0x%x", (uint32_t)-val);
Nguyen Anh Quynh46a74e52014-08-25 16:47:12 +0800126 else
127 SStream_concat(O, "-%u", -val);
128 }
129}
130
131void printUInt32Bang(SStream *O, uint32_t val)
132{
133 if (val > HEX_THRESHOLD)
134 SStream_concat(O, "#0x%x", val);
135 else
136 SStream_concat(O, "#%u", val);
137}
138
139void printUInt32(SStream *O, uint32_t val)
140{
141 if (val > HEX_THRESHOLD)
142 SStream_concat(O, "0x%x", val);
143 else
144 SStream_concat(O, "%u", val);
145}
146
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147/*
148 int main()
149 {
150 SStream ss;
151 int64_t i;
152
153 SStream_Init(&ss);
154
155 SStream_concat(&ss, "hello ");
156 SStream_concat(&ss, "%d - 0x%x", 200, 16);
157
158 i = 123;
159 SStream_concat(&ss, " + %ld", i);
160 SStream_concat(&ss, "%s", "haaaaa");
161
162 printf("%s\n", ss.buffer);
163
164 return 0;
165 }
166 */