mirror of https://github.com/x64dbg/zydis
				
				
				
			introduced custom instruction formatters
This commit is contained in:
		
							parent
							
								
									b93ddf848b
								
							
						
					
					
						commit
						7e26fa1683
					
				| 
						 | 
					@ -75,8 +75,6 @@ static void VXBaseSymbolResolver_Destruct(VXBaseSymbolResolverContext *ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef void(*VXBaseInstructionFormatter_DestructionCallback)(
 | 
					typedef void(*VXBaseInstructionFormatter_DestructionCallback)(
 | 
				
			||||||
    VXBaseInstructionFormatterContext *ctx);
 | 
					    VXBaseInstructionFormatterContext *ctx);
 | 
				
			||||||
typedef void(*VXBaseInstructionFormatter_InternalFormatInstructionCallback)(
 | 
					 | 
				
			||||||
    VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct _VXBaseInstructionFormatter
 | 
					typedef struct _VXBaseInstructionFormatter
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -263,6 +261,27 @@ static void VXIntelInstructionFormatter_FormatOperand(VXBaseInstructionFormatter
 | 
				
			||||||
static void VXIntelInstructionFormatter_InternalFormatInstruction(
 | 
					static void VXIntelInstructionFormatter_InternalFormatInstruction(
 | 
				
			||||||
    VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
 | 
					    VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* VXCustomInstructionFormatter ---------------------------------------------------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct _VXCustomInstructionFormatter
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter super;
 | 
				
			||||||
 | 
					} VXCustomInstructionFormatter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief   Contructor.
 | 
				
			||||||
 | 
					 * @param   ctx             The context.
 | 
				
			||||||
 | 
					 * @param   formatInsnCb    The callback formatting the instruction.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static void VXCustomInstructionFormatter_Construct(VXBaseInstructionFormatterContext *ctx,
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief   Destructor.
 | 
				
			||||||
 | 
					 * @param   ctx The context.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static void VXCustomInstructionFormatter_Destruct(VXBaseInstructionFormatterContext *ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Implementation ============================================================================== */
 | 
					/* Implementation ============================================================================== */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* VXBaseSymbolResolver ------------------------------------------------------------------------ */
 | 
					/* VXBaseSymbolResolver ------------------------------------------------------------------------ */
 | 
				
			||||||
| 
						 | 
					@ -1069,6 +1088,49 @@ static void VXIntelInstructionFormatter_InternalFormatInstruction(
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* VXCustomInstructionFormatter ---------------------------------------------------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void VXCustomInstructionFormatter_Construct(VXBaseInstructionFormatterContext *ctx,
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_Construct(ctx, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    VXCustomInstructionFormatter *thiz = VXCustomInstructionFormatter_thiz(ctx);
 | 
				
			||||||
 | 
					    thiz->super.internalFormat = formatInsnCb;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void VXCustomInstructionFormatter_Destruct(VXBaseInstructionFormatterContext *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_Destruct(ctx);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					VX_EXPORT VXBaseInstructionFormatterContext* VXCustomInstructionFormatter_Create(
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    VXCustomInstructionFormatter *thiz = malloc(sizeof(VXCustomInstructionFormatter));
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatterContext *ctx = malloc(sizeof(VXBaseInstructionFormatterContext));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!thiz || !ctx)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (thiz)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            free(thiz);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (ctx)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            free(ctx);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ctx->d.type = TYPE_CUSTOMINSTRUCTIONFORMATTER;
 | 
				
			||||||
 | 
					    ctx->d.ptr  = thiz;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    VXCustomInstructionFormatter_Construct(ctx, formatInsnCb);
 | 
				
			||||||
 | 
					    return ctx;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* --------------------------------------------------------------------------------------------- */
 | 
					/* --------------------------------------------------------------------------------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ============================================================================================= */
 | 
					/* ============================================================================================= */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -97,6 +97,9 @@ typedef struct _VXBaseInstructionFormatterContext
 | 
				
			||||||
    VXContextDescriptor d;
 | 
					    VXContextDescriptor d;
 | 
				
			||||||
} VXBaseInstructionFormatterContext;
 | 
					} VXBaseInstructionFormatterContext;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef void(*VXBaseInstructionFormatter_InternalFormatInstructionCallback)(
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatterContext *ctx, const VXInstructionInfo *info);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief   Formats a decoded instruction.
 | 
					 * @brief   Formats a decoded instruction.
 | 
				
			||||||
 * @param   ctx     The instruction formatter context.
 | 
					 * @param   ctx     The instruction formatter context.
 | 
				
			||||||
| 
						 | 
					@ -134,7 +137,7 @@ VX_EXPORT void VXBaseInstructionFormatter_SetSymbolResolver(
 | 
				
			||||||
VX_EXPORT void VXBaseInstructionFormatter_Release(
 | 
					VX_EXPORT void VXBaseInstructionFormatter_Release(
 | 
				
			||||||
    VXBaseInstructionFormatterContext *ctx);
 | 
					    VXBaseInstructionFormatterContext *ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* VXIntelInstructionFormatter ================================================================ */
 | 
					/* VXIntelInstructionFormatter ================================================================= */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief   Creates an Intel-syntax instruction formatter.
 | 
					 * @brief   Creates an Intel-syntax instruction formatter.
 | 
				
			||||||
| 
						 | 
					@ -152,6 +155,16 @@ VX_EXPORT VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_Create(
 | 
				
			||||||
VX_EXPORT VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CreateEx(
 | 
					VX_EXPORT VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CreateEx(
 | 
				
			||||||
    VXBaseSymbolResolverContext *resolver);
 | 
					    VXBaseSymbolResolverContext *resolver);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* VXCustomInstructionFormatter ================================================================ */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief   Creats a custom instruction formatter.
 | 
				
			||||||
 | 
					 * @param   formatInsnCb    The callback formatting the instruction.
 | 
				
			||||||
 | 
					 * @return  @c NULL if it fails, else a custom instruction formatter context.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					VX_EXPORT VXBaseInstructionFormatterContext* VXCustomInstructionFormatter_Create(
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatter_InternalFormatInstructionCallback formatInsnCb);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ============================================================================================= */
 | 
					/* ============================================================================================= */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef __cplusplus
 | 
					#ifdef __cplusplus
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -51,6 +51,7 @@ typedef enum _VXTypeId
 | 
				
			||||||
    TYPE_CUSTOMSYMBOLRESOLVER,
 | 
					    TYPE_CUSTOMSYMBOLRESOLVER,
 | 
				
			||||||
    TYPE_BASEINSTRUCTIONFORMATTER,
 | 
					    TYPE_BASEINSTRUCTIONFORMATTER,
 | 
				
			||||||
    TYPE_INTELINSTRUCTIONFORMATTER,
 | 
					    TYPE_INTELINSTRUCTIONFORMATTER,
 | 
				
			||||||
 | 
					    TYPE_CUSTOMINSTRUCTIONFORMATTER,
 | 
				
			||||||
} VXTypeId;
 | 
					} VXTypeId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Context conversion helpers ================================================================== */
 | 
					/* Context conversion helpers ================================================================== */
 | 
				
			||||||
| 
						 | 
					@ -149,7 +150,8 @@ VX_INLINE struct _VXBaseInstructionFormatter* VXBaseInstructionFormatter_thiz(
 | 
				
			||||||
    VXBaseInstructionFormatterContext *ctx)
 | 
					    VXBaseInstructionFormatterContext *ctx)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER 
 | 
					    assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER 
 | 
				
			||||||
        || ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER);
 | 
					        || ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER
 | 
				
			||||||
 | 
					        || ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
 | 
				
			||||||
    return (struct _VXBaseInstructionFormatter*)ctx->d.ptr;
 | 
					    return (struct _VXBaseInstructionFormatter*)ctx->d.ptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -157,7 +159,8 @@ VX_INLINE const struct _VXBaseInstructionFormatter* VXBaseInstructionFormatter_c
 | 
				
			||||||
    const VXBaseInstructionFormatterContext *ctx)
 | 
					    const VXBaseInstructionFormatterContext *ctx)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER
 | 
					    assert(ctx->d.type == TYPE_BASEINSTRUCTIONFORMATTER
 | 
				
			||||||
        || ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER);
 | 
					        || ctx->d.type == TYPE_INTELINSTRUCTIONFORMATTER
 | 
				
			||||||
 | 
					        || ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
 | 
				
			||||||
    return (const struct _VXBaseInstructionFormatter*)ctx->d.ptr;
 | 
					    return (const struct _VXBaseInstructionFormatter*)ctx->d.ptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -175,6 +178,20 @@ VX_INLINE const struct _VXIntelInstructionFormatter* VXIntelInstructionFormatter
 | 
				
			||||||
    return (const struct _VXIntelInstructionFormatter*)ctx->d.ptr;
 | 
					    return (const struct _VXIntelInstructionFormatter*)ctx->d.ptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					VX_INLINE struct _VXCustomInstructionFormatter* VXCustomInstructionFormatter_thiz(
 | 
				
			||||||
 | 
					    VXBaseInstructionFormatterContext *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    assert(ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
 | 
				
			||||||
 | 
					    return (struct _VXCustomInstructionFormatter*)ctx->d.ptr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					VX_INLINE const struct _VXCustomInstructionFormatter* VXCustomInstructionFormatter_cthiz(
 | 
				
			||||||
 | 
					    const VXBaseInstructionFormatterContext *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    assert(ctx->d.type == TYPE_CUSTOMINSTRUCTIONFORMATTER);
 | 
				
			||||||
 | 
					    return (struct _VXCustomInstructionFormatter*)ctx->d.ptr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ============================================================================================= */
 | 
					/* ============================================================================================= */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* _VDE_VXINTERNALHELPERS_H_ */
 | 
					#endif /* _VDE_VXINTERNALHELPERS_H_ */
 | 
				
			||||||
		Loading…
	
		Reference in New Issue