2014-04-29 03:21:04 +00:00
|
|
|
/* Capstone Disassembly Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2014-06-04 10:31:02 +00:00
|
|
|
#include <string.h>
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
#include "SStream.h"
|
2014-01-15 12:44:03 +00:00
|
|
|
#include "cs_priv.h"
|
2014-09-21 15:27:11 +00:00
|
|
|
#include "inttypes.h"
|
2014-08-25 08:47:12 +00:00
|
|
|
#include "utils.h"
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-06-17 07:09:59 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable: 4996) // disable MSVC's warning on strcpy()
|
|
|
|
#endif
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
void SStream_Init(SStream *ss)
|
|
|
|
{
|
|
|
|
ss->index = 0;
|
|
|
|
ss->buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:31:02 +00:00
|
|
|
void SStream_concat0(SStream *ss, char *s)
|
|
|
|
{
|
|
|
|
#ifndef CAPSTONE_DIET
|
2014-10-22 07:21:43 +00:00
|
|
|
unsigned int len = (unsigned int) strlen(s);
|
2014-10-02 02:17:55 +00:00
|
|
|
|
|
|
|
memcpy(ss->buffer + ss->index, s, len);
|
|
|
|
ss->index += len;
|
|
|
|
ss->buffer[ss->index] = '\0';
|
2014-06-04 10:31:02 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
void SStream_concat(SStream *ss, const char *fmt, ...)
|
|
|
|
{
|
2014-02-22 15:26:27 +00:00
|
|
|
#ifndef CAPSTONE_DIET
|
2013-11-27 04:11:31 +00:00
|
|
|
va_list ap;
|
2014-05-12 05:41:49 +00:00
|
|
|
int ret;
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
va_start(ap, fmt);
|
2014-05-08 22:44:49 +00:00
|
|
|
ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
|
2013-11-27 04:11:31 +00:00
|
|
|
va_end(ap);
|
|
|
|
ss->index += ret;
|
2014-02-22 15:26:27 +00:00
|
|
|
#endif
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-25 08:47:12 +00:00
|
|
|
// print number with prefix #
|
|
|
|
void printInt64Bang(SStream *O, int64_t val)
|
|
|
|
{
|
|
|
|
if (val >= 0) {
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "#0x%"PRIx64, val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "#%"PRIu64, val);
|
|
|
|
} else {
|
|
|
|
if (val <- HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "#-0x%"PRIx64, -val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "#-%"PRIu64, -val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print number
|
|
|
|
void printInt64(SStream *O, int64_t val)
|
|
|
|
{
|
|
|
|
if (val >= 0) {
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "0x%"PRIx64, val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "%"PRIu64, val);
|
|
|
|
} else {
|
|
|
|
if (val <- HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "-0x%"PRIx64, -val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "-%"PRIu64, -val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printInt32Bang(SStream *O, int32_t val)
|
|
|
|
{
|
|
|
|
if (val >= 0) {
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "#0x%x", val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "#%u", val);
|
|
|
|
} else {
|
|
|
|
if (val <- HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "#-0x%x", -val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "#-%u", -val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printInt32(SStream *O, int32_t val)
|
|
|
|
{
|
|
|
|
if (val >= 0) {
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "0x%x", val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "%u", val);
|
|
|
|
} else {
|
|
|
|
if (val <- HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "-0x%x", -val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "-%u", -val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printUInt32Bang(SStream *O, uint32_t val)
|
|
|
|
{
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "#0x%x", val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "#%u", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void printUInt32(SStream *O, uint32_t val)
|
|
|
|
{
|
|
|
|
if (val > HEX_THRESHOLD)
|
|
|
|
SStream_concat(O, "0x%x", val);
|
|
|
|
else
|
|
|
|
SStream_concat(O, "%u", val);
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
/*
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
SStream ss;
|
|
|
|
int64_t i;
|
|
|
|
|
|
|
|
SStream_Init(&ss);
|
|
|
|
|
|
|
|
SStream_concat(&ss, "hello ");
|
|
|
|
SStream_concat(&ss, "%d - 0x%x", 200, 16);
|
|
|
|
|
|
|
|
i = 123;
|
|
|
|
SStream_concat(&ss, " + %ld", i);
|
|
|
|
SStream_concat(&ss, "%s", "haaaaa");
|
|
|
|
|
|
|
|
printf("%s\n", ss.buffer);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|