NetParse.h
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:3k
源码类别:
P2P编程
开发平台:
Visual C++
- #ifndef _NETPARSE_H__
- #define _NETPARSE_H__
- typedef struct stream
- {
- stream() : size(0), p(NULL), end(NULL), data(NULL) {};
- ~stream(){ if(data){ free(data); data=NULL; } }
- unsigned char *p; //pointer of current read position
- unsigned char *end;
- unsigned char *data;
- unsigned int size;
- } STREAM, *PSTREAM;
- #define s_mark_end(s) (s)->end = (s)->p;
- #define s_check(s) ((s)->p <= (s)->end)
- #define s_check_rem(s,n) ((s)->p + n <= (s)->end)
- #define s_check_end(s) ((s)->p == (s)->end)
- #if defined(L_ENDIAN) && !defined(NEED_ALIGN)
- #define in_uint16_le(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
- #define in_uint32_le(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
- #define out_uint16_le(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
- #define out_uint32_le(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
- #else
- #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; }
- #define in_uint32_le(s,v) { in_uint16_le(s,v)
- v += *((s)->p++) << 16; v += *((s)->p++) << 24; }
- #define out_uint16_le(s,v) { *((s)->p++) = (v) & 0xff; *((s)->p++) = ((v) >> 8) & 0xff; }
- #define out_uint32_le(s,v) { out_uint16_le( s, (uint16)((v)&0xffff) ); out_uint16_le(s, ((v) >> 16) & 0xffff); }
- #endif
- #if defined(B_ENDIAN) && !defined(NEED_ALIGN)
- #define in_uint16_be(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; }
- #define in_uint32_be(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; }
- #define out_uint16_be(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; }
- #define out_uint32_be(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; }
- #define B_ENDIAN_PREFERRED
- #define in_uint16(s,v) in_uint16_be(s,v)
- #define in_uint32(s,v) in_uint32_be(s,v)
- #define out_uint16(s,v) out_uint16_be(s,v)
- #define out_uint32(s,v) out_uint32_be(s,v)
- #else
- #define next_be(s,v) v = ((v) << 8) + *((s)->p++);
- #define in_uint16_be(s,v) { v = *((s)->p++); next_be(s,v); }
- #define in_uint32_be(s,v) { in_uint16_be(s,v); next_be(s,v); next_be(s,v); }
- #define out_uint16_be(s,v) { *((s)->p++) = ((v) >> 8) & 0xff; *((s)->p++) = (v) & 0xff; }
- #define out_uint32_be(s,v) { out_uint16_be(s, ((v) >> 16) & 0xffff); out_uint16_be(s, (v) & 0xffff); }
- #endif
- #ifndef B_ENDIAN_PREFERRED
- #define in_uint16(s,v) in_uint16_le(s,v)
- #define in_uint32(s,v) in_uint32_le(s,v)
- #define out_uint16(s,v) out_uint16_le(s,v)
- #define out_uint32(s,v) out_uint32_le(s,v)
- #endif
- #define in_uint8(s,v) v = *((s)->p++);
- #define in_uint8p(s,v,n) { v = (s)->p; (s)->p += n; }
- #define in_uint8a(s,v,n) { memcpy(v,(s)->p,n); (s)->p += n; }
- #define in_uint8s(s,n) (s)->p += n;
- #define out_uint8(s,v) *((s)->p++) = v;
- #define out_uint8p(s,v,n) { memcpy((s)->p,v,n); (s)->p += n; }
- #define out_uint8a(s,v,n) out_uint8p(s,v,n);
- #define out_uint8s(s,n) { memset((s)->p,0,n); (s)->p += n; }
- #endif