Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
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" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 11 | |
Nguyen Anh Quynh | 476d5ad | 2014-06-17 15:09:59 +0800 | [diff] [blame] | 12 | #ifdef _MSC_VER |
| 13 | #pragma warning(disable: 4996) // disable MSVC's warning on strcpy() |
| 14 | #endif |
| 15 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 16 | void SStream_Init(SStream *ss) |
| 17 | { |
| 18 | ss->index = 0; |
| 19 | ss->buffer[0] = '\0'; |
| 20 | } |
| 21 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame] | 22 | void SStream_concat0(SStream *ss, char *s) |
| 23 | { |
| 24 | #ifndef CAPSTONE_DIET |
| 25 | strcpy(ss->buffer + ss->index, s); |
| 26 | ss->index += strlen(s); |
| 27 | #endif |
| 28 | } |
| 29 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 30 | void SStream_concat(SStream *ss, const char *fmt, ...) |
| 31 | { |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 32 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 33 | va_list ap; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 34 | int ret; |
| 35 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 36 | va_start(ap, fmt); |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 37 | 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] | 38 | va_end(ap); |
| 39 | ss->index += ret; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 40 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | int main() |
| 45 | { |
| 46 | SStream ss; |
| 47 | int64_t i; |
| 48 | |
| 49 | SStream_Init(&ss); |
| 50 | |
| 51 | SStream_concat(&ss, "hello "); |
| 52 | SStream_concat(&ss, "%d - 0x%x", 200, 16); |
| 53 | |
| 54 | i = 123; |
| 55 | SStream_concat(&ss, " + %ld", i); |
| 56 | SStream_concat(&ss, "%s", "haaaaa"); |
| 57 | |
| 58 | printf("%s\n", ss.buffer); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | */ |