下面是项目中摘抄的的一部分代码:
#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);
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;
}