Skip to content

Commit

Permalink
make vsnprintf() user-defined function pointer, which is passed in vi…
Browse files Browse the repository at this point in the history
…a the same CS_OPT_MEM option like malloc/calloc etc
  • Loading branch information
aquynh committed Jan 15, 2014
1 parent a9ffb44 commit edeeb04
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion SStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdio.h>

#include "SStream.h"
#include "cs_priv.h"

void SStream_Init(SStream *ss)
{
Expand All @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions arch/X86/X86DisassemblerDecoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
/* By Nguyen Anh Quynh <[email protected]>, 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"
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions cs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions include/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit edeeb04

Please sign in to comment.