Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
Nguyen Anh Quynh | bfcaba5 | 2015-03-04 17:45:23 +0800 | [diff] [blame] | 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <stdarg.h> |
| 6 | #include <stdio.h> |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 7 | #include <string.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 8 | |
| 9 | #include "SStream.h" |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 10 | #include "cs_priv.h" |
Yegor Derevenets | ced9d24 | 2014-09-21 17:27:11 +0200 | [diff] [blame] | 11 | #include "inttypes.h" |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 12 | #include "utils.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 13 | |
Nguyen Anh Quynh | 476d5ad | 2014-06-17 15:09:59 +0800 | [diff] [blame] | 14 | #ifdef _MSC_VER |
| 15 | #pragma warning(disable: 4996) // disable MSVC's warning on strcpy() |
| 16 | #endif |
| 17 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 18 | void SStream_Init(SStream *ss) |
| 19 | { |
| 20 | ss->index = 0; |
| 21 | ss->buffer[0] = '\0'; |
| 22 | } |
| 23 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 24 | void SStream_concat0(SStream *ss, char *s) |
| 25 | { |
| 26 | #ifndef CAPSTONE_DIET |
flyingsymbols | d91f964 | 2014-10-22 03:21:43 -0400 | [diff] [blame] | 27 | unsigned int len = (unsigned int) strlen(s); |
Nguyen Anh Quynh | ea3c089 | 2014-10-02 10:17:55 +0800 | [diff] [blame] | 28 | |
| 29 | memcpy(ss->buffer + ss->index, s, len); |
| 30 | ss->index += len; |
| 31 | ss->buffer[ss->index] = '\0'; |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 32 | #endif |
| 33 | } |
| 34 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 35 | void SStream_concat(SStream *ss, const char *fmt, ...) |
| 36 | { |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 37 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 38 | va_list ap; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 39 | int ret; |
| 40 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 41 | va_start(ap, fmt); |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 42 | ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 43 | va_end(ap); |
| 44 | ss->index += ret; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 45 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 48 | // print number with prefix # |
| 49 | void 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 Quynh | 03a1836 | 2014-12-12 22:06:06 +0800 | [diff] [blame] | 64 | void 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 Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 72 | // print number |
| 73 | void 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 Quynh | c109e8e | 2014-11-08 13:58:50 +0800 | [diff] [blame] | 88 | // print number in decimal mode |
| 89 | void 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 Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 97 | void 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 | |
| 112 | void 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 | |
| 127 | void 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 | |
| 135 | void 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 143 | /* |
| 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 | */ |