void yr_object_destroy( YR_OBJECT* object) { YR_STRUCTURE_MEMBER* member; YR_STRUCTURE_MEMBER* next_member; YR_ARRAY_ITEMS* array_items; RE* re; int i; char* str; switch(object->type) { case OBJECT_TYPE_STRUCTURE: member = ((YR_OBJECT_STRUCTURE*) object)->members; while (member != NULL) { next_member = member->next; yr_object_destroy(member->object); yr_free(member); member = next_member; } break; case OBJECT_TYPE_STRING: str = ((YR_OBJECT_STRING*) object)->value; if (str != NULL) yr_free(str); break; case OBJECT_TYPE_REGEXP: re = ((YR_OBJECT_REGEXP*) object)->value; if (re != NULL) yr_re_destroy(re); break; case OBJECT_TYPE_ARRAY: array_items = ((YR_OBJECT_ARRAY*) object)->items; for (i = 0; i < array_items->count; i++) if (array_items->objects[i] != NULL) yr_object_destroy(array_items->objects[i]); yr_free(array_items); break; case OBJECT_TYPE_FUNCTION: yr_object_destroy(((YR_OBJECT_FUNCTION*) object)->return_obj); break; } yr_free((void*) object->identifier); yr_free(object); }
int yr_object_function_create( const char* identifier, const char* arguments_fmt, const char* return_fmt, YR_MODULE_FUNC code, YR_OBJECT* parent, YR_OBJECT** function) { YR_OBJECT* return_obj; YR_OBJECT* f; int8_t return_type; switch (*return_fmt) { case 'i': return_type = OBJECT_TYPE_INTEGER; break; case 's': return_type = OBJECT_TYPE_STRING; break; default: return ERROR_INVALID_FORMAT; } FAIL_ON_ERROR(yr_object_create( OBJECT_TYPE_FUNCTION, identifier, parent, &f)); FAIL_ON_ERROR_WITH_CLEANUP( yr_object_create(return_type, "result", f, &return_obj), yr_object_destroy(f)); ((YR_OBJECT_FUNCTION* )f)->arguments_fmt = arguments_fmt; ((YR_OBJECT_FUNCTION* )f)->return_obj = return_obj; ((YR_OBJECT_FUNCTION* )f)->code = code; if (function != NULL) *function = f; return ERROR_SUCCESS; }
int yr_modules_unload_all( YR_SCAN_CONTEXT* context) { int i; for (i = 0; i < sizeof(yr_modules_table) / sizeof(YR_MODULE); i++) { YR_OBJECT* module_structure = (YR_OBJECT*) yr_hash_table_remove( context->objects_table, yr_modules_table[i].name, NULL); if (module_structure != NULL) { yr_modules_table[i].unload(module_structure); yr_object_destroy(module_structure); } } return ERROR_SUCCESS; }
int yr_modules_load( const char* module_name, YR_SCAN_CONTEXT* context) { int i, result; YR_MODULE_IMPORT mi; YR_OBJECT* module_structure = (YR_OBJECT*) yr_hash_table_lookup( context->objects_table, module_name, NULL); // if module_structure != NULL, the module was already // loaded, return successfully without doing nothing. if (module_structure != NULL) return ERROR_SUCCESS; // not loaded yet FAIL_ON_ERROR(yr_object_create( OBJECT_TYPE_STRUCTURE, module_name, NULL, &module_structure)); mi.module_name = module_name; mi.module_data = NULL; mi.module_data_size = 0; result = context->callback( CALLBACK_MSG_IMPORT_MODULE, &mi, context->user_data); if (result == CALLBACK_ERROR) { yr_object_destroy(module_structure); return ERROR_CALLBACK_ERROR; } FAIL_ON_ERROR_WITH_CLEANUP( yr_modules_do_declarations(module_name, module_structure), yr_object_destroy(module_structure)); FAIL_ON_ERROR_WITH_CLEANUP( yr_hash_table_add( context->objects_table, module_name, NULL, module_structure), yr_object_destroy(module_structure)); for (i = 0; i < sizeof(yr_modules_table) / sizeof(YR_MODULE); i++) { if (strcmp(yr_modules_table[i].name, module_name) == 0) { result = yr_modules_table[i].load( context, module_structure, mi.module_data, mi.module_data_size); if (result != ERROR_SUCCESS) return result; } } result = context->callback( CALLBACK_MSG_MODULE_IMPORTED, module_structure, context->user_data); return ERROR_SUCCESS; }