MDGSF Software Engineer

[C/C++] Encode Decode

2017-02-09

#include "dcclientcodec.h"

int32_t
iEncode8(uint8_t ucValue, char ** ppcBuf, uint16_t * pusEncodeLen)
{
    if(NULL == ppcBuf || NULL == *ppcBuf || NULL == pusEncodeLen) {
        return EXIT_FAILURE;
    }
    (*ppcBuf)[0] = ucValue;
    *ppcBuf += sizeof(uint8_t);
    *pusEncodeLen += sizeof(uint8_t);
    return EXIT_SUCCESS;
}

int32_t
iDecode8(uint8_t * pucValue, char ** ppcMsg, uint16_t * pusMsgLen)
{
    if(NULL == pucValue || NULL == ppcMsg || NULL == *ppcMsg ||
       NULL == pusMsgLen || *pusMsgLen < sizeof(uint8_t))
    {
        return EXIT_FAILURE;
    }
    pucValue[0] = (*ppcMsg)[0];
    *ppcMsg += sizeof(uint8_t);
    *pusMsgLen -= sizeof(uint8_t);
    return EXIT_SUCCESS;
}


int32_t
iEncode16(uint16_t usValue, char ** ppcBuf, uint16_t * pusEncodeLen)
{
    if(NULL == ppcBuf || NULL == *ppcBuf || NULL == pusEncodeLen) {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)&usValue;
#if IS_BIG_ENDIAN
    (*ppcBuf)[0] = pcValue[0];
    (*ppcBuf)[1] = pcValue[1];
#else
    (*ppcBuf)[1] = pcValue[0];
    (*ppcBuf)[0] = pcValue[1];
#endif

    *ppcBuf += sizeof(uint16_t);
    *pusEncodeLen += sizeof(uint16_t);
    return EXIT_SUCCESS;
}

int32_t
iDecode16(uint16_t * pusValue, char ** ppcMsg, uint16_t * pusMsgLen)
{
    if(NULL == pusValue || NULL == ppcMsg || NULL == *ppcMsg ||
       NULL == pusMsgLen || *pusMsgLen < sizeof(uint16_t))
    {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)pusValue;
#if IS_BIG_ENDIAN
    pcValue[0] = (*ppcMsg)[0];
    pcValue[1] = (*ppcMsg)[1];
#else
    pcValue[1] = (*ppcMsg)[0];
    pcValue[0] = (*ppcMsg)[1];
#endif

    *ppcMsg += sizeof(uint16_t);
    *pusMsgLen -= sizeof(uint16_t);
    return EXIT_SUCCESS;
}


int32_t
iEncode32(uint32_t uiValue, char ** ppcBuf, uint16_t * pusEncodeLen)
{
    if(NULL == ppcBuf || NULL == *ppcBuf || NULL == pusEncodeLen) {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)&uiValue;
#if IS_BIG_ENDIAN
    (*ppcBuf)[0] = pcValue[0];
    (*ppcBuf)[1] = pcValue[1];
    (*ppcBuf)[2] = pcValue[2];
    (*ppcBuf)[3] = pcValue[3];
#else
    (*ppcBuf)[3] = pcValue[0];
    (*ppcBuf)[2] = pcValue[1];
    (*ppcBuf)[1] = pcValue[2];
    (*ppcBuf)[0] = pcValue[3];
#endif

    *ppcBuf += sizeof(uint32_t);
    *pusEncodeLen += sizeof(uint32_t);
    return EXIT_SUCCESS;
}

int32_t
iDecode32(uint32_t * puiValue, char ** ppcMsg, uint16_t * pusMsgLen)
{
    if(NULL == puiValue || NULL == ppcMsg || NULL == *ppcMsg ||
       NULL == pusMsgLen || *pusMsgLen < sizeof(uint32_t))
    {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)puiValue;
#if IS_BIG_ENDIAN
    pcValue[0] = (*ppcMsg)[0];
    pcValue[1] = (*ppcMsg)[1];
    pcValue[2] = (*ppcMsg)[2];
    pcValue[3] = (*ppcMsg)[3];
#else
    pcValue[3] = (*ppcMsg)[0];
    pcValue[2] = (*ppcMsg)[1];
    pcValue[1] = (*ppcMsg)[2];
    pcValue[0] = (*ppcMsg)[3];
#endif

    *ppcMsg += sizeof(uint32_t);
    *pusMsgLen -= sizeof(uint32_t);
    return EXIT_SUCCESS;
}

int32_t
iEncode64(uint64_t ullValue, char ** ppcBuf, uint16_t * pusEncodeLen)
{
    if(NULL == ppcBuf || NULL == *ppcBuf || NULL == pusEncodeLen) {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)&ullValue;
#if IS_BIG_ENDIAN
    memcpy(*ppcBuf, pcValue, sizeof(uint64_t));
#else
    (*ppcBuf)[7] = pcValue[0];
    (*ppcBuf)[6] = pcValue[1];
    (*ppcBuf)[5] = pcValue[2];
    (*ppcBuf)[4] = pcValue[3];
    (*ppcBuf)[3] = pcValue[4];
    (*ppcBuf)[2] = pcValue[5];
    (*ppcBuf)[1] = pcValue[6];
    (*ppcBuf)[0] = pcValue[7];
#endif

    *ppcBuf += sizeof(uint64_t);
    *pusEncodeLen += sizeof(uint64_t);
    return EXIT_SUCCESS;
}

int32_t
iDecode64(uint64_t * pullValue, char ** ppcMsg, uint16_t * pusMsgLen)
{
    if(NULL == pullValue || NULL == ppcMsg || NULL == *ppcMsg ||
       NULL == pusMsgLen || *pusMsgLen < sizeof(uint64_t))
    {
        return EXIT_FAILURE;
    }

    char * pcValue = (char*)pullValue;
#if IS_BIG_ENDIAN
    memcpy(pcValue, *ppcMsg, sizeof(uint64_t));
#else
    pcValue[7] = (*ppcMsg)[0];
    pcValue[6] = (*ppcMsg)[1];
    pcValue[5] = (*ppcMsg)[2];
    pcValue[4] = (*ppcMsg)[3];
    pcValue[3] = (*ppcMsg)[4];
    pcValue[2] = (*ppcMsg)[5];
    pcValue[1] = (*ppcMsg)[6];
    pcValue[0] = (*ppcMsg)[7];
#endif

    *ppcMsg += sizeof(uint64_t);
    *pusMsgLen -= sizeof(uint64_t);
    return EXIT_SUCCESS;
}





int32_t iEncodeMsgHeader(const SDCMsgHeader * pxMsgHeader, char ** ppcBuf, uint16_t * pusEncodeLen)
{
    if(NULL == pxMsgHeader || NULL == ppcBuf || NULL == *ppcBuf || NULL == pusEncodeLen)
    {
        return EXIT_FAILURE;
    }

    if(iEncode8(pxMsgHeader->ucMsgType, ppcBuf, pusEncodeLen) != EXIT_SUCCESS)
    {
        return EXIT_FAILURE;
    }

    if(iEncode16(pxMsgHeader->usMsgLen, ppcBuf, pusEncodeLen) != EXIT_SUCCESS)
    {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

int32_t iDecodeMsgHeader(SDCMsgHeader * pxMsgHeader, char ** ppcMsg, uint16_t * pusMsgLen)
{
    if(NULL == pxMsgHeader || NULL == ppcMsg || NULL == *ppcMsg ||
       NULL == pusMsgLen || *pusMsgLen < DC_MSG_HEADER_LEN)
    {
        return EXIT_FAILURE;
    }

    if(iDecode8(&pxMsgHeader->ucMsgType, ppcMsg, pusMsgLen) != EXIT_SUCCESS)
    {
        return EXIT_FAILURE;
    }

    if(iDecode16(&pxMsgHeader->usMsgLen, ppcMsg, pusMsgLen) != EXIT_SUCCESS)
    {
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

#ifndef DCCLIENT_CODEC_H
#define DCCLIENT_CODEC_H

#include "dcclientpublic.h"

int32_t iEncode8(uint8_t ucValue, char ** ppcBuf, uint16_t * pusEncodeLen);

int32_t iDecode8(uint8_t * pucValue, char ** ppcMsg, uint16_t * pusMsgLen);


int32_t iEncode16(uint16_t usValue, char ** ppcBuf, uint16_t * pusEncodeLen);

int32_t iDecode16(uint16_t * pusValue, char ** ppcMsg, uint16_t * pusMsgLen);


int32_t iEncode32(uint32_t uiValue, char ** ppcBuf, uint16_t * pusEncodeLen);

int32_t iDecode32(uint32_t * puiValue, char ** ppcMsg, uint16_t * pusMsgLen);


int32_t iEncode64(uint64_t ullValue, char ** ppcBuf, uint16_t * pusEncodeLen);

int32_t iDecode64(uint64_t * pullValue, char ** ppcMsg, uint16_t * pusMsgLen);



/*
Example:
pcSendBuf = acSendBuf;
usEncodeLen = 0;
iRet = iEncodeMsgHeader(&xMsgHeader, &pcSendBuf, &usEncodeLen);
after Encode, usEncodeLen is the current data's length.
*/
int32_t iEncodeMsgHeader(const SDCMsgHeader * pxMsgHeader, char ** ppcBuf, uint16_t * pusEncodeLen);

/*
Example:
char * pcRecvBuf = acRecvBuf;
usRecvBufLen: is the current data's length.
iRet = iDecodeMsgHeader(&xMsgHeader, &pcRecvBuf, &usRecvBufLen);
after Decode, usEncodeLen is zero.
*/
int32_t iDecodeMsgHeader(SDCMsgHeader * pxMsgHeader, char ** ppcMsg, uint16_t * pusMsgLen);

#endif

weixingongzhonghao

Similar Posts

上一篇 [C/C++] Endian

Comments