二进制数据流处理函数

static inline uint64_t pletoh64(const void* p)
{
    return (uint64_t) * ((const uint8_t*)(p) + 7) << 56 | (uint64_t) * ((const uint8_t*)(p) + 6) << 48 | (uint64_t) * ((const uint8_t*)(p) + 5) << 40 | (uint64_t) * ((const uint8_t*)(p) + 4) << 32 | (uint64_t) * ((const uint8_t*)(p) + 3) << 24 | (uint64_t) * ((const uint8_t*)(p) + 2) << 16 | (uint64_t) * ((const uint8_t*)(p) + 1) << 8 | (uint64_t) * ((const uint8_t*)(p) + 0) << 0;
}

static inline uint32_t pletoh32(const void* p)
{
    return (uint32_t) * ((const uint8_t*)(p) + 3) << 24 | (uint32_t) * ((const uint8_t*)(p) + 2) << 16 | (uint32_t) * ((const uint8_t*)(p) + 1) << 8 | (uint32_t) * ((const uint8_t*)(p) + 0) << 0;
}

static inline uint32_t pletoh24(const void* p)
{
    return (uint32_t) * ((const uint8_t*)(p) + 2) << 16 | (uint32_t) * ((const uint8_t*)(p) + 1) << 8 | (uint32_t) * ((const uint8_t*)(p) + 0) << 0;
}

static inline uint16_t pletoh16(const void* p)
{
    return (uint16_t) * ((const uint8_t*)(p) + 1) << 8 | (uint16_t) * ((const uint8_t*)(p) + 0) << 0;
}

static inline uint64_t pntoh64(const void* p)
{
    return (uint64_t) * ((const uint8_t*)(p) + 0) << 56 | (uint64_t) * ((const uint8_t*)(p) + 1) << 48 | (uint64_t) * ((const uint8_t*)(p) + 2) << 40 | (uint64_t) * ((const uint8_t*)(p) + 3) << 32 | (uint64_t) * ((const uint8_t*)(p) + 4) << 24 | (uint64_t) * ((const uint8_t*)(p) + 5) << 16 | (uint64_t) * ((const uint8_t*)(p) + 6) << 8 | (uint64_t) * ((const uint8_t*)(p) + 7) << 0;
}

static inline uint32_t pntoh32(const void* p)
{
    return (uint32_t) * ((const uint8_t*)(p) + 0) << 24 | (uint32_t) * ((const uint8_t*)(p) + 1) << 16 | (uint32_t) * ((const uint8_t*)(p) + 2) << 8 | (uint32_t) * ((const uint8_t*)(p) + 3) << 0;
}

static inline uint32_t pntoh24(const void* p)
{
    return (uint32_t) * ((const uint8_t*)(p) + 0) << 16 | (uint32_t) * ((const uint8_t*)(p) + 1) << 8 | (uint32_t) * ((const uint8_t*)(p) + 2) << 0;
}

static inline uint16_t pntoh16(const void* p)
{
    return (uint16_t) * ((const uint8_t*)(p) + 0) << 8 | (uint16_t) * ((const uint8_t*)(p) + 1) << 0;
}

static inline uint8_t tvb_get_guint8(const void* tvb, const int offset)
{
    return ((uint8_t*)tvb)[0];
}




static uint16_t get_host16(const uint8_t *input, uint32_t offset)
{
    return pntoh16(input + offset);
}

static uint16_t get_host24(const uint8_t *input, uint32_t offset)
{
    return pntoh24(input + offset);
}

static uint16_t __attribute__((unused)) get_host32(const uint8_t *input, uint32_t offset)
{
    return pntoh32(input + offset);
}

static uint16_t get_host64(const uint8_t *input, uint32_t offset)
{
    return pntoh64(input + offset);
}



static uint32_t strsize(const uint8_t *input, uint32_t input_len, uint32_t offset)
{
    uint32_t i;

    if (offset >= input_len)
    {
        return 0;
    }

    for (i = offset; i < input_len; i++)
    {
        if (input[i] == '\0')
        {
            break;
        }
    }
    if (input[i] != '\0')
    {
        return 0;
    }

    return i - offset ;
}
using namespace std;





int main(){
    string filepath = "二进制文件";
    int len=84;
    int m_fd=open(filepath.c_str(), O_RDONLY);
    uint8_t* m_data;
    if (m_fd <= 0) {
        cout<<"open error";
    }
    if (len > 0) {
        m_data = (uint8_t*)malloc(len);
        if (m_data == nullptr) {
            printf("failed malloc data\n");
            
            //m_data 这个空间是new的
        }
        if (read(m_fd, m_data, len) != len) {
            //这个read是从文件弄来数据赋给m_data吗
            printf("failed read data\n");
            
        }
        
    }
    uint32_t offset = 0;
    char type = m_data[offset];//第一个字符如果是00

    

    if (type != '\0')
        offset += 1;
    uint32_t length = get_host32(m_data, offset);
    offset+=4;
    uint16_t major=get_host16(m_data, offset);
    uint16_t minor=get_host16(m_data, offset+2);
    if(major==3&&minor==0)
    {
        cout<< "Startup message"<<endl;
        offset+=4;
        int length=0;
        cout<<m_data[offset];
        
        while(1){
            length=strsize(m_data,84,offset);
            
            
            cout<<"len="<<length<<endl;
            for(int i=0;i<length;++i){
                
                cout<<(char)m_data[i+offset];
            }
            cout<<endl;
            if(strsize(m_data,84,offset)==0)break;
            cout<<length<<endl;
            offset+=length;
        }

    }
    

    
    
    

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值