make vsnprintf() user-defined function pointer, which is passed in via the same CS_OPT_MEM option like malloc/calloc etc

This commit is contained in:
Nguyen Anh Quynh 2014-01-15 20:44:03 +08:00
parent a9ffb440f8
commit edeeb04a1a
5 changed files with 14 additions and 4 deletions

View File

@ -6,6 +6,7 @@
#include <stdio.h>
#include "SStream.h"
#include "cs_priv.h"
void SStream_Init(SStream *ss)
{
@ -18,7 +19,7 @@ void SStream_concat(SStream *ss, const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
int ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap);
va_end(ap);
ss->index += ret;
}

View File

@ -17,10 +17,11 @@
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
#include <stdarg.h> /* for va_*() */
#include <stdio.h> /* for vsnprintf() */
#include <stdlib.h> /* for exit() */
#include <string.h> /* for memset() */
#include "../../cs_priv.h"
#include "X86DisassemblerDecoder.h"
#include "X86GenDisassemblerTables.inc"
@ -282,7 +283,7 @@ static void dbgprintf(struct InternalInstruction* insn,
return;
va_start(ap, format);
(void)vsnprintf(buffer, sizeof(buffer), format, ap);
(void)cs_vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
insn->dlog(insn->dlogArg, buffer);

5
cs.c
View File

@ -47,11 +47,13 @@ cs_malloc_t cs_mem_malloc = malloc;
cs_calloc_t cs_mem_calloc = calloc;
cs_realloc_t cs_mem_realloc = realloc;
cs_free_t cs_mem_free = free;
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
#else
cs_malloc_t cs_mem_malloc = NULL;
cs_calloc_t cs_mem_calloc = NULL;
cs_realloc_t cs_mem_realloc = NULL;
cs_free_t cs_mem_free = NULL;
cs_vsnprintf_t cs_vsnprintf = NULL;
#endif
unsigned int cs_version(int *major, int *minor)
@ -112,7 +114,7 @@ const char *cs_strerror(cs_err code)
cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
{
if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free)
if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
// Error: before cs_open(), dynamic memory management must be initialized
// with cs_option(CS_OPT_MEM)
return CS_ERR_MEMSETUP;
@ -241,6 +243,7 @@ cs_err cs_option(csh ud, cs_opt_type type, size_t value)
cs_mem_calloc = mem->calloc;
cs_mem_realloc = mem->realloc;
cs_mem_free = mem->free;
cs_vsnprintf = mem->vsnprintf;
return CS_ERR_OK;
}

View File

@ -64,5 +64,6 @@ extern cs_malloc_t cs_mem_malloc;
extern cs_calloc_t cs_mem_calloc;
extern cs_realloc_t cs_mem_realloc;
extern cs_free_t cs_mem_free;
extern cs_vsnprintf_t cs_vsnprintf;
#endif

View File

@ -9,6 +9,7 @@ extern "C" {
#endif
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
@ -52,6 +53,8 @@ typedef void* (*cs_malloc_t)(size_t size);
typedef void* (*cs_calloc_t)(size_t nmemb, size_t size);
typedef void* (*cs_realloc_t)(void *ptr, size_t size);
typedef void (*cs_free_t)(void *ptr);
typedef int (*cs_vsnprintf_t)(char * restrict str, size_t size, const char * restrict format, va_list ap);
// User-defined memory malloc/calloc/realloc/free functions
// These functions will be used internally to dynamically manage memory.
@ -61,6 +64,7 @@ typedef struct cs_opt_mem {
cs_calloc_t calloc;
cs_realloc_t realloc;
cs_free_t free;
cs_vsnprintf_t vsnprintf;
} cs_opt_mem;
// Runtime option for the disassembled engine