这次项目的需求有大概20个命令,需要根据协议来实现不同payload的编码和解码函数,抽象出来每个命令有相似的代码结构,如果每行都用键盘敲打确实是低效,所以考虑用脚本先生成一个代码模板,然后逐个填空实现对应的方法,也算是一个小的尝试!
其实后面类似协议编解码实现的代码估计AI是更加合适了,程序员失业也是不得不面对的现实而已!
模板代码:
头文件//kmip_command_sample.h
#ifndef __KMIP_COMMAND_SAMPLE__
#define __KMIP_COMMAND_SAMPLE__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "kmip.h"
typedef struct sample_request_payload
{
} SampleRequestPayload;
typedef struct sample_response_payload
{
/* KMIP 2.1 */
TextString *private_unique_identifier;
TextString *public_unique_identifier;
} SampleResponsePayload;
int
kmip_encode_sample_request_payload(KMIP *ctx, const SampleRequestPayload *value);
int
kmip_decode_sample_request_payload(KMIP *ctx, SampleRequestPayload *value);
int
kmip_free_sample_request_payload(KMIP *ctx, SampleRequestPayload *value);
int
kmip_encode_sample_resopnse_payload(KMIP *ctx, const SampleResponsePayload *value);
int
kmip_decode_sample_response_payload(KMIP *ctx, SampleResponsePayload *value);
int
kmip_free_sample_response_payload(KMIP *ctx, SampleResponsePayload *value);
#endif//__KMIP_COMMAND_CREATE_PAIR__C文件//kmip_command_sample.c
#include "kmip_command_sample.h"
int
kmip_encode_sample_request_payload(KMIP *ctx, const SampleRequestPayload *value){
CHECK_ENCODE_ARGS(ctx, value);
int result = 0;
result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_REQUEST_PAYLOAD, KMIP_TYPE_STRUCTURE));
CHECK_RESULT(ctx, result);
uint8 *length_index = ctx->index;
uint8 *value_index = ctx->index += 4;
/*....code START...*/
/*....code END...*/
uint8 *curr_index = ctx->index;
ctx->index = length_index;
result = kmip_encode_length(ctx, curr_index - value_index);
CHECK_RESULT(ctx, result);
ctx->index = curr_index;
return(KMIP_OK);
}
int
kmip_decode_sample_request_payload(KMIP *ctx, SampleRequestPayload *value){
CHECK_BUFFER_FULL(ctx, 8);
int result = 0;
int32 tag_type = 0;
uint32 length = 0;
kmip_decode_int32_be(ctx, &tag_type);
CHECK_TAG_TYPE(ctx, tag_type, KMIP_TAG_REQUEST_PAYLOAD, KMIP_TYPE_STRUCTURE);
kmip_decode_length(ctx, &length);
CHECK_BUFFER_FULL(ctx, length);
/*....code START...*/
/*....code END...*/
return(KMIP_OK);
}
int
kmip_free_sample_request_payload(KMIP *ctx, SampleRequestPayload *value){
}
int
kmip_encode_sample_resopnse_payload(KMIP *ctx, const SampleResponsePayload *value){
CHECK_ENCODE_ARGS(ctx, value);
int result = 0;
result = kmip_encode_int32_be(ctx, TAG_TYPE(KMIP_TAG_RESPONSE_PAYLOAD, KMIP_TYPE_STRUCTURE));
CHECK_RESULT(ctx, result);
uint8 *length_index = ctx->index;
uint8 *value_index = ctx->index += 4;
/*....code START...*/
/*....code END...*/
uint8 *curr_index = ctx->index;
ctx->index = length_index;
result = kmip_encode_length(ctx, curr_index - value_index);
CHECK_RESULT(ctx, result);
ctx->index = curr_index;
return(KMIP_OK);
}
int
kmip_decode_sample_response_payload(KMIP *ctx, SampleResponsePayload *value){
CHECK_DECODE_ARGS(ctx, value);
CHECK_BUFFER_FULL(ctx, 8);
int result = 0;
int32 tag_type = 0;
uint32 length = 0;
kmip_decode_int32_be(ctx, &tag_type);
CHECK_TAG_TYPE(ctx, tag_type, KMIP_TAG_RESPONSE_PAYLOAD, KMIP_TYPE_STRUCTURE);
kmip_decode_length(ctx, &length);
CHECK_BUFFER_FULL(ctx, length);
/*....code START...*/
/*....code END...*/
return (KMIP_OK);
}
int
kmip_free_sample_response_payload(KMIP *ctx, SampleResponsePayload *value){
}每个命令均需要有这些实现,所以用一个脚本来生成这些命令的基础代码,SHELL脚本如下:
#!/bin/sh
DST_CMD=$1
DST_CLASSNAME=$2
MACRO=$3
#拷贝对应命令的文件
Cur_Dir=$(pwd)
cp "${Cur_Dir}/kmip_command_sample.h" "${Cur_Dir}/kmip_command_${DST_CMD}.h"
cp "${Cur_Dir}/kmip_command_sample.c" "${Cur_Dir}/kmip_command_${DST_CMD}.c"
#替换宏
sed -i "" "s/__KMIP_COMMAND_SAMPLE__/${MACRO}/g" "${Cur_Dir}/kmip_command_${DST_CMD}.h"
#替换 sample_ -> command_
sed -i "" "s/sample/${DST_CMD}/g" "${Cur_Dir}/kmip_command_${DST_CMD}.h"
sed -i "" "s/sample/${DST_CMD}/g" "${Cur_Dir}/kmip_command_${DST_CMD}.c"
#替换 Sample -> Command
sed -i "" "s/Sample/${DST_CLASSNAME}/g" "${Cur_Dir}/kmip_command_${DST_CMD}.h"
sed -i "" "s/Sample/${DST_CLASSNAME}/g" "${Cur_Dir}/kmip_command_${DST_CMD}.c"liyizhang@macdeMac-2 libkmip % ./create_command.sh "create_datakey" "CreateDataKey"
kmip_command_create_datakey.c
kmip_command_create_datakey.h
//kmip_command_create_datakey.h
#ifndef __KMIP_COMMANDCREATE_DATA_KEY__
#define __KMIP_COMMANDCREATE_DATA_KEY__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "kmip.h"
typedef struct create_datakey_request_payload
{
/* KMIP 2.1 */
} CreateDataKeyRequestPayload;
typedef struct create_datakey_response_payload
{
/* KMIP 2.1 */
TextString *unique_identifier;
} CreateDataKeyResponsePayload;
int
kmip_encode_create_datakey_request_payload(KMIP *ctx, const CreateDataKeyRequestPayload *value);
int
kmip_decode_create_datakey_request_payload(KMIP *ctx, CreateDataKeyRequestPayload *value);
int
kmip_free_create_datakey_request_payload(KMIP *ctx, CreateDataKeyRequestPayload *value);
int
kmip_encode_create_datakey_resopnse_payload(KMIP *ctx, const CreateDataKeyResponsePayload *value);
int
kmip_decode_create_datakey_response_payload(KMIP *ctx, CreateDataKeyResponsePayload *value);
int
kmip_free_create_datakey_response_payload(KMIP *ctx, CreateDataKeyResponsePayload *value);
#endif//__KMIP_COMMAND_CREATE_PAIR__看看我要实现的命令,这一个月都得做填空题了~!!!!
./create_command.sh "create_datakey" "CreateDataKey" "CREATE_DATA_KEY" ./create_command.sh "query_keylist" "QueryKeyList" "QUERY_KEY_LIST" ./create_command.sh "modify_attribute" "ModifyAttribute" "MODIFY_ATTRIBUTE" ./create_command.sh "modify_keyname" "ModifyKeyname" "MODIFY_KEY_NAME" ./create_command.sh "modify_keydesc" "ModifyKeydesc" "MODIFYKEY_DESC" ./create_command.sh "rotate_key" "RotateKey" "ROTATE_KEY" ./create_command.sh "activate_key" "ActivateKey" "ACTIVATE_KEY" ./create_command.sh "revoke_key" "RevokeKey" "REVOKE_KEY" ./create_command.sh "renew_key" "RenewKey" "RENEW_KEY" ./create_command.sh "list_key_version" "ListKeyVersion" "LIST_KEY_VERSION" ./create_command.sh "archieve_key" "ArchieveKey" "ARCHIEVE_KEY" ./create_command.sh "schedule_destroy_key" "ScheduleDestroyKey" "SCHEDULE_DESTROY_KEY" ./create_command.sh "cancel_schedule_destroy_key" "CancelScheduleDestroyKey" "CANCEL_SCHEDULE_DESTROY_KEY" ./create_command.sh "descrypt_datakey" "DescryptDataKey" "DESCRYPT_DATA_KEY" ./create_command.sh "sign_data" "SignData" "SIGN_DATA" ./create_command.sh "authority_key" "AuthorityKey" "AUTHORITY_KEY" ./create_command.sh "export_public_key" "ExportPublicKey" "EXPORT_PUBLIC_KEY" ./create_command.sh "get_key_attributes" "GetKeyAttributes" "GET_KEY_ATTRIBUTES"
以前的一个同事,闲暇之余就爱写小工具提升工作效率,完全自驱自发的行为,有同事觉得刚好有需求就分享出来,这种感觉还是很奇妙的,也是我蛮佩服的一类人!

-------------------广告线---------------
项目、合作,欢迎勾搭,邮箱:promall@qq.com
本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com
