Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable libyara user code to define its own modules #1772

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ test_re_split_SOURCES = tests/test-re-split.c tests/util.c
test_re_split_LDADD = libyara.la
test_async_SOURCES = tests/test-async.c tests/util.c
test_async_LDADD = libyara.la
test_module_SOURCES = tests/test-module.c tests/util.c
test_module_LDADD = libyara.la

TESTS = $(check_PROGRAMS)
TESTS_ENVIRONMENT = TOP_SRCDIR=$(top_srcdir) TOP_BUILDDIR=$(top_builddir)
Expand All @@ -331,7 +333,8 @@ check_PROGRAMS = \
test-stack \
test-re-split \
test-async \
test-string
test-string \
test-module

EXTRA_PROGRAMS = tests/mapper
CLEANFILES = tests/mapper$(EXEEXT)
Expand Down
10 changes: 5 additions & 5 deletions libyara/include/yara/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#else

void* yr_calloc(size_t count, size_t size);
YR_API void* yr_calloc(size_t count, size_t size);

void* yr_malloc(size_t size);
YR_API void* yr_malloc(size_t size);

void* yr_realloc(void* ptr, size_t size);
YR_API void* yr_realloc(void* ptr, size_t size);

char* yr_strdup(const char* str);
YR_API char* yr_strdup(const char* str);

char* yr_strndup(const char* str, size_t n);
YR_API char* yr_strndup(const char* str, size_t n);

YR_API void yr_free(void* ptr);

Expand Down
15 changes: 15 additions & 0 deletions libyara/include/yara/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,28 @@ struct YR_MODULE
YR_EXT_FINALIZE_FUNC finalize;
};

#define _yr_module_define_named(name) \
YR_MODULE name##__module = { \
#name, \
name##__declarations, \
name##__load, \
name##__unload, \
name##__initialize, \
name##__finalize}

#define yr_module_define_named(name) _yr_module_define_named(name)

#define yr_module_define() yr_module_define_named(MODULE_NAME)

struct YR_MODULE_IMPORT
{
const char* module_name;
void* module_data;
size_t module_data_size;
};

YR_API int yr_modules_add(YR_MODULE* module);

int yr_modules_initialize(void);

int yr_modules_finalize(void);
Expand Down
42 changes: 21 additions & 21 deletions libyara/include/yara/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,85 +63,85 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define OBJECT_TYPE_DICTIONARY 6
#define OBJECT_TYPE_FLOAT 7

int yr_object_create(
YR_API int yr_object_create(
int8_t type,
const char* identifier,
YR_OBJECT* parent,
YR_OBJECT** object);

void yr_object_set_canary(YR_OBJECT* object, int canary);

int yr_object_function_create(
YR_API int yr_object_function_create(
const char* identifier,
const char* arguments_fmt,
const char* return_fmt,
YR_MODULE_FUNC func,
YR_OBJECT* parent,
YR_OBJECT** function);

int yr_object_from_external_variable(
YR_API int yr_object_from_external_variable(
YR_EXTERNAL_VARIABLE* external,
YR_OBJECT** object);

void yr_object_destroy(YR_OBJECT* object);
YR_API void yr_object_destroy(YR_OBJECT* object);

int yr_object_copy(YR_OBJECT* object, YR_OBJECT** object_copy);
YR_API int yr_object_copy(YR_OBJECT* object, YR_OBJECT** object_copy);

YR_OBJECT* yr_object_lookup_field(YR_OBJECT* object, const char* field_name);
YR_API YR_OBJECT* yr_object_lookup_field(YR_OBJECT* object, const char* field_name);

YR_OBJECT* yr_object_lookup(
YR_API YR_OBJECT* yr_object_lookup(
YR_OBJECT* root,
int flags,
const char* pattern,
...) YR_PRINTF_LIKE(3, 4);

bool yr_object_has_undefined_value(YR_OBJECT* object, const char* field, ...)
YR_API bool yr_object_has_undefined_value(YR_OBJECT* object, const char* field, ...)
YR_PRINTF_LIKE(2, 3);

double yr_object_get_float(YR_OBJECT* object, const char* field, ...)
YR_API double yr_object_get_float(YR_OBJECT* object, const char* field, ...)
YR_PRINTF_LIKE(2, 3);

int64_t yr_object_get_integer(YR_OBJECT* object, const char* field, ...)
YR_API int64_t yr_object_get_integer(YR_OBJECT* object, const char* field, ...)
YR_PRINTF_LIKE(2, 3);

SIZED_STRING* yr_object_get_string(YR_OBJECT* object, const char* field, ...)
YR_API SIZED_STRING* yr_object_get_string(YR_OBJECT* object, const char* field, ...)
YR_PRINTF_LIKE(2, 3);

int yr_object_set_integer(
YR_API int yr_object_set_integer(
int64_t value,
YR_OBJECT* object,
const char* field,
...) YR_PRINTF_LIKE(3, 4);

int yr_object_set_float(double value, YR_OBJECT* object, const char* field, ...)
YR_API int yr_object_set_float(double value, YR_OBJECT* object, const char* field, ...)
YR_PRINTF_LIKE(3, 4);

int yr_object_set_string(
YR_API int yr_object_set_string(
const char* value,
size_t len,
YR_OBJECT* object,
const char* field,
...) YR_PRINTF_LIKE(4, 5);

int yr_object_array_length(YR_OBJECT* object);
YR_API int yr_object_array_length(YR_OBJECT* object);

YR_OBJECT* yr_object_array_get_item(YR_OBJECT* object, int flags, int index);
YR_API YR_OBJECT* yr_object_array_get_item(YR_OBJECT* object, int flags, int index);

int yr_object_array_set_item(YR_OBJECT* object, YR_OBJECT* item, int index);
YR_API int yr_object_array_set_item(YR_OBJECT* object, YR_OBJECT* item, int index);

YR_OBJECT* yr_object_dict_get_item(
YR_API YR_OBJECT* yr_object_dict_get_item(
YR_OBJECT* object,
int flags,
const char* key);

int yr_object_dict_set_item(
YR_API int yr_object_dict_set_item(
YR_OBJECT* object,
YR_OBJECT* item,
const char* key);

int yr_object_structure_set_member(YR_OBJECT* object, YR_OBJECT* member);
YR_API int yr_object_structure_set_member(YR_OBJECT* object, YR_OBJECT* member);

YR_OBJECT* yr_object_get_root(YR_OBJECT* object);
YR_API YR_OBJECT* yr_object_get_root(YR_OBJECT* object);

YR_API void yr_object_print_data(
YR_OBJECT* object,
Expand Down
7 changes: 4 additions & 3 deletions libyara/include/yara/strutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdlib.h>
#include <string.h>
#include <yara/integers.h>
#include <yara/utils.h>

#if defined(_WIN32)

Expand Down Expand Up @@ -109,8 +110,8 @@ size_t strlcpy_w(char* dst, const char* w_src, size_t n);

#endif

int yr_isalnum(const uint8_t* s);
YR_API int yr_isalnum(const uint8_t* s);

void yr_vasprintf(char** strp, const char* fmt, va_list ap);
YR_API void yr_vasprintf(char** strp, const char* fmt, va_list ap);

void yr_asprintf(char** strp, const char* fmt, ...);
YR_API void yr_asprintf(char** strp, const char* fmt, ...);
20 changes: 10 additions & 10 deletions libyara/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,25 @@ int yr_heap_free(void)
return ERROR_INTERNAL_FATAL_ERROR;
}

void* yr_calloc(size_t count, size_t size)
YR_API void* yr_calloc(size_t count, size_t size)
{
return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, count * size);
}

void* yr_malloc(size_t size)
YR_API void* yr_malloc(size_t size)
{
return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);
}

void* yr_realloc(void* ptr, size_t size)
YR_API void* yr_realloc(void* ptr, size_t size)
{
if (ptr == NULL)
return (void*) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, size);

return (void*) HeapReAlloc(hHeap, HEAP_ZERO_MEMORY, ptr, size);
}

char* yr_strdup(const char* str)
YR_API char* yr_strdup(const char* str)
{
size_t len = strlen(str);
char* dup = (char*) yr_malloc(len + 1);
Expand All @@ -87,7 +87,7 @@ char* yr_strdup(const char* str)
return (char*) dup;
}

char* yr_strndup(const char* str, size_t n)
YR_API char* yr_strndup(const char* str, size_t n)
{
size_t len = strnlen(str, n);
char* dup = (char*) yr_malloc(len + 1);
Expand Down Expand Up @@ -122,27 +122,27 @@ int yr_heap_free(void)
return ERROR_SUCCESS;
}

void* yr_calloc(size_t count, size_t size)
YR_API void* yr_calloc(size_t count, size_t size)
{
return calloc(count, size);
}

void* yr_malloc(size_t size)
YR_API void* yr_malloc(size_t size)
{
return malloc(size);
}

void* yr_realloc(void* ptr, size_t size)
YR_API void* yr_realloc(void* ptr, size_t size)
{
return realloc(ptr, size);
}

char* yr_strdup(const char* str)
YR_API char* yr_strdup(const char* str)
{
return strdup(str);
}

char* yr_strndup(const char* str, size_t n)
YR_API char* yr_strndup(const char* str, size_t n)
{
return strndup(str, n);
}
Expand Down
Loading