C语言回调函数

下面是项目中摘抄的的一部分代码:

//1 直接使用
#define CICVM_OCTSTR_GEN(name, type)\
static inline int32_t cicvm_octstr_parse_##name(int32_t (*func)(void *, type *), void *ctx, type *arg)\
{\
    uint32_t len;\
\
    if (oer_parse_length(ctx, &len)) {\
        return -1;\
    }\
\
    return (*func)(ctx, arg);\
}\
\
static int32_t cicvm_octstr_build_##name(int32_t (*func)(void *, type *), void *ctx, type *arg)\
{\
    buf_ctx_t sctx;\
    uint32_t len = sizeof(type);\
    uint8_t buf[len + 1];\
\
    buf_init(&sctx, buf, len + 1, BUF_ENDIAN_BIG);\
    if ((*func)(&sctx, arg)) {\
        return -1;\
    }\
\
    return oer_build_octet_string(ctx, 0, sctx.offset, buf) < 0 ? -1 : 0;\
}

CICVM_OCTSTR_GEN(ecp384, scms_ecp_t);
CICVM_OCTSTR_GEN(ecp256, dcb_ecp_t);

//2, typedef使用
typedef int32_t (*func_call_t)(buf_ctx_t *ctx, void *arg);

static int32_t cicvm_parse_seqof(func_call_t call, buf_ctx_t *ctx, void *arg, uint32_t *total)
{
    uint32_t idx;

    if (oer_parse_sequence_of(ctx, total)) {
        return -1;
    }

    for (idx = 0; idx < *total; idx++) {
        if (call(ctx, arg)) {
            return -2;
        }
    }

    return 0;
}

static int32_t cicvm_build_seqof(func_call_t call, buf_ctx_t *ctx, void *arg, uint32_t total)
{
    uint32_t idx;

    if (oer_build_sequence_of(ctx, total)) {
        return -1;
    }

    for (idx = 0; idx < total; idx++) {
        if (call(ctx, arg)) {
            return -2;
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值