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 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 4 | #include <stdarg.h> |
reverser | 160e198 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 5 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 6 | #include <libkern/libkern.h> |
| 7 | #else |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 8 | #include <stdio.h> |
reverser | 160e198 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 9 | #endif |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 10 | #include <string.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 11 | |
tandasat | 45e5eab | 2016-05-11 21:48:32 -0700 | [diff] [blame] | 12 | #include <capstone/platform.h> |
| 13 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | #include "SStream.h" |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 15 | #include "cs_priv.h" |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 16 | #include "utils.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | |
Nguyen Anh Quynh | 476d5ad | 2014-06-17 15:09:59 +0800 | [diff] [blame] | 18 | #ifdef _MSC_VER |
| 19 | #pragma warning(disable: 4996) // disable MSVC's warning on strcpy() |
| 20 | #endif |
| 21 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 22 | void SStream_Init(SStream *ss) |
| 23 | { |
| 24 | ss->index = 0; |
| 25 | ss->buffer[0] = '\0'; |
| 26 | } |
| 27 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 28 | void SStream_concat0(SStream *ss, char *s) |
| 29 | { |
| 30 | #ifndef CAPSTONE_DIET |
flyingsymbols | d91f964 | 2014-10-22 03:21:43 -0400 | [diff] [blame] | 31 | unsigned int len = (unsigned int) strlen(s); |
Nguyen Anh Quynh | ea3c089 | 2014-10-02 10:17:55 +0800 | [diff] [blame] | 32 | |
| 33 | memcpy(ss->buffer + ss->index, s, len); |
| 34 | ss->index += len; |
| 35 | ss->buffer[ss->index] = '\0'; |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 36 | #endif |
| 37 | } |
| 38 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 39 | void SStream_concat(SStream *ss, const char *fmt, ...) |
| 40 | { |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 41 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 42 | va_list ap; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 43 | int ret; |
| 44 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 45 | va_start(ap, fmt); |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 46 | 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] | 47 | va_end(ap); |
| 48 | ss->index += ret; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 49 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 52 | // print number with prefix # |
| 53 | void 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 Quynh | 206f5ff | 2017-12-15 10:17:56 +0800 | [diff] [blame] | 62 | SStream_concat(O, "#-0x%"PRIx64, (uint64_t)-val); |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 63 | else |
| 64 | SStream_concat(O, "#-%"PRIu64, -val); |
| 65 | } |
| 66 | } |
| 67 | |
Nguyen Anh Quynh | 03a1836 | 2014-12-12 22:06:06 +0800 | [diff] [blame] | 68 | void 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 Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 76 | // print number |
| 77 | void 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 Quynh | 206f5ff | 2017-12-15 10:17:56 +0800 | [diff] [blame] | 86 | SStream_concat(O, "-0x%"PRIx64, (uint64_t)-val); |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 87 | else |
| 88 | SStream_concat(O, "-%"PRIu64, -val); |
| 89 | } |
| 90 | } |
| 91 | |
Nguyen Anh Quynh | c109e8e | 2014-11-08 13:58:50 +0800 | [diff] [blame] | 92 | // print number in decimal mode |
| 93 | void printInt32BangDec(SStream *O, int32_t val) |
| 94 | { |
| 95 | if (val >= 0) |
| 96 | SStream_concat(O, "#%u", val); |
| 97 | else |
Nguyen Anh Quynh | 206f5ff | 2017-12-15 10:17:56 +0800 | [diff] [blame] | 98 | SStream_concat(O, "#-%u", (uint32_t)-val); |
Nguyen Anh Quynh | c109e8e | 2014-11-08 13:58:50 +0800 | [diff] [blame] | 99 | } |
| 100 | |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 101 | void 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 Quynh | 206f5ff | 2017-12-15 10:17:56 +0800 | [diff] [blame] | 110 | SStream_concat(O, "#-0x%x", (uint32_t)-val); |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 111 | else |
| 112 | SStream_concat(O, "#-%u", -val); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void 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 Quynh | 206f5ff | 2017-12-15 10:17:56 +0800 | [diff] [blame] | 125 | SStream_concat(O, "-0x%x", (uint32_t)-val); |
Nguyen Anh Quynh | 46a74e5 | 2014-08-25 16:47:12 +0800 | [diff] [blame] | 126 | else |
| 127 | SStream_concat(O, "-%u", -val); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void 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 | |
| 139 | void 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 147 | /* |
| 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 | */ |