Rizin
unix-like reverse engineering framework and cli tools
SStream.c
Go to the documentation of this file.
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 
26 {
27  ss->index = 0;
28  ss->buffer[0] = '\0';
29 }
30 
31 void SStream_concat0(SStream *ss, const char *s)
32 {
33 #ifndef CAPSTONE_DIET
34  if (!ss || !s) {
35  return;
36  }
37  unsigned int len = (unsigned int) strlen(s);
38 
39  memcpy(ss->buffer + ss->index, s, len);
40  ss->index += len;
41  ss->buffer[ss->index] = '\0';
42 #endif
43 }
44 
45 void SStream_concat(SStream *ss, const char *fmt, ...)
46 {
47 #ifndef CAPSTONE_DIET
48  va_list ap;
49  int ret;
50 
51  va_start(ap, fmt);
52  ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
53  va_end(ap);
54  ss->index += ret;
55 #endif
56 }
57 
58 // print number with prefix #
60 {
61  if (val >= 0) {
62  if (val > HEX_THRESHOLD)
63  SStream_concat(O, "#0x%"PRIx64, val);
64  else
65  SStream_concat(O, "#%"PRIu64, val);
66  } else {
67  if (val <- HEX_THRESHOLD) {
68  if (val == LONG_MIN)
69  SStream_concat(O, "#-0x%"PRIx64, (uint64_t)val);
70  else
71  SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val);
72  }
73  else
74  SStream_concat(O, "#-%"PRIu64, -val);
75  }
76 }
77 
79 {
80  if (val > HEX_THRESHOLD)
81  SStream_concat(O, "#0x%"PRIx64, val);
82  else
83  SStream_concat(O, "#%"PRIu64, val);
84 }
85 
86 // print number
88 {
89  if (val >= 0) {
90  if (val > HEX_THRESHOLD)
91  SStream_concat(O, "0x%"PRIx64, val);
92  else
93  SStream_concat(O, "%"PRIu64, val);
94  } else {
95  if (val <- HEX_THRESHOLD) {
96  if (val == LONG_MIN)
98  else
99  SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val);
100  }
101  else
102  SStream_concat(O, "-%"PRIu64, -val);
103  }
104 }
105 
106 // print number in decimal mode
108 {
109  if (val >= 0)
110  SStream_concat(O, "#%u", val);
111  else
112  if (val == INT_MIN)
113  SStream_concat(O, "#-%u", val);
114  else
115  SStream_concat(O, "#-%u", (uint32_t)-val);
116 }
117 
119 {
120  if (val >= 0) {
121  if (val > HEX_THRESHOLD)
122  SStream_concat(O, "#0x%x", val);
123  else
124  SStream_concat(O, "#%u", val);
125  } else {
126  if (val <- HEX_THRESHOLD) {
127  if (val == INT_MIN)
128  SStream_concat(O, "#-0x%x", (uint32_t)val);
129  else
130  SStream_concat(O, "#-0x%x", (uint32_t)-val);
131  }
132  else
133  SStream_concat(O, "#-%u", -val);
134  }
135 }
136 
138 {
139  if (val >= 0) {
140  if (val > HEX_THRESHOLD)
141  SStream_concat(O, "0x%x", val);
142  else
143  SStream_concat(O, "%u", val);
144  } else {
145  if (val <- HEX_THRESHOLD) {
146  if (val == INT_MIN)
147  SStream_concat(O, "-0x%x", (uint32_t)val);
148  else
149  SStream_concat(O, "-0x%x", (uint32_t)-val);
150  }
151  else
152  SStream_concat(O, "-%u", -val);
153  }
154 }
155 
157 {
158  if (val > HEX_THRESHOLD)
159  SStream_concat(O, "#0x%x", val);
160  else
161  SStream_concat(O, "#%u", val);
162 }
163 
165 {
166  if (val > HEX_THRESHOLD)
167  SStream_concat(O, "0x%x", val);
168  else
169  SStream_concat(O, "%u", val);
170 }
171 
172 /*
173  int main()
174  {
175  SStream ss;
176  int64_t i;
177 
178  SStream_Init(&ss);
179 
180  SStream_concat(&ss, "hello ");
181  SStream_concat(&ss, "%d - 0x%x", 200, 16);
182 
183  i = 123;
184  SStream_concat(&ss, " + %ld", i);
185  SStream_concat(&ss, "%s", "haaaaa");
186 
187  printf("%s\n", ss.buffer);
188 
189  return 0;
190  }
191  */
size_t len
Definition: 6502dis.c:15
void SStream_Init(SStream *ss)
Definition: SStream.c:25
void printUInt32Bang(SStream *O, uint32_t val)
Definition: SStream.c:156
void printInt64(SStream *O, int64_t val)
Definition: SStream.c:87
void printUInt64Bang(SStream *O, uint64_t val)
Definition: SStream.c:78
void SStream_concat(SStream *ss, const char *fmt,...)
Definition: SStream.c:45
void printInt32Bang(SStream *O, int32_t val)
Definition: SStream.c:118
void printInt32BangDec(SStream *O, int32_t val)
Definition: SStream.c:107
void SStream_concat0(SStream *ss, const char *s)
Definition: SStream.c:31
void printInt64Bang(SStream *O, int64_t val)
Definition: SStream.c:59
void printUInt32(SStream *O, uint32_t val)
Definition: SStream.c:164
void printInt32(SStream *O, int32_t val)
Definition: SStream.c:137
ut16 val
Definition: armass64_const.h:6
cs_vsnprintf_t cs_vsnprintf
Definition: cs.c:352
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define PRIu64
Definition: macros.h:18
#define O
Definition: rcond.c:14
static RzSocket * s
Definition: rtr.c:28
static int
Definition: sfsocketcall.h:114
long int64_t
Definition: sftypes.h:32
int int32_t
Definition: sftypes.h:33
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
Definition: SStream.h:9
int index
Definition: SStream.h:11
char buffer[512]
Definition: SStream.h:10
#define HEX_THRESHOLD
Definition: utils.h:16
#define PRIx64
Definition: sysdefs.h:94