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;
}
}
}
05-16
2933
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-22
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)