📄 util.h
字号:
/* * This piece of code is totally free. If any pitfalls found, * please feel free to contact me at jetmotor@21cn.com * THANKS A LOT! */#ifndef UTIL_H_#define UTIL_H_#include <stdio.h>#include <string.h>#include "types.h"#define PACK_UINT8(buf, n) (*(buf)++ = (n) & 0xff)#define PACK_UINT16(buf, n) (*(buf)++ = ((n) >> 8) & 0xff, *(buf)++ = (n) & 0xff)#define PACK_UINT24(buf, n) (*(buf)++ = ((n) >> 16) & 0xff, *(buf)++ = ((n) >> 8) & 0xff, *(buf)++ = (n) & 0xff)#define PACK_UINT32(buf, n) (*(buf)++ = ((n) >> 24) & 0xff, *(buf)++ = ((n) >> 16) & 0xff, *(buf)++ = ((n) >> 8) & 0xff, *(buf)++ = (n) & 0xff)#define PACK_BYTES(buf, p, n) (memcpy((buf), (p), (n)), (buf) += (n))#define UNPACK_UINT8(buf) (*(buf)++)#define UNPACK_UINT16(buf) ((buf) += 2, ((buf)[-2] << 8) | (buf)[-1])#define UNPACK_UINT24(buf) ((buf) += 3, ((buf)[-3] << 16) | ((buf)[-2] << 8) | (buf)[-1])#define UNPACK_UINT32(buf) ((buf) += 4, ((buf)[-4] << 24) | ((buf)[-3] << 16) | ((buf)[-2] << 8) | (buf)[-1])#define UNPACK_BYTES(buf, p, n) (memcpy((p), (buf), (n)), (buf) += (n))#define ISDEC(c) ((c) >= '0' && (c) <= '9')#define ISHEX(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))#define HEX2DEC(c) (((c) >= 'A' && (c)<= 'F') ? (c) - 'A' + 10 : (((c) >= 'a' && (c)<= 'f') ? (c) - 'a' + 10 : ((c) - '0')))#define HEX2BYTE(c1, c2) ((HEX2DEC(c1) << 4) | HEX2DEC(c2))typedef struct{ UINT8* buf; UINT16 len; UINT16 cur; UINT8 offset; /* [0..7], MSB = 0 */} BUFFER;#define init_buffer(buf) (memset(buf, 0, sizeof(BUFFER))static inline UINT8 eat_bits(BUFFER* buf, UINT8 bits /* range:[1..8] */){ UINT8 c; UINT8 c1 = buf->buf[buf->cur]; UINT8 c2 = buf->buf[buf->cur + 1]; if (8 - buf->offset >= bits) { c = c1 >> (8 - buf->offset - bits); buf->offset += bits; } else { c = (c1 << (bits - (8 - buf->offset))) | (c2 >> (8 - (bits - (8 - buf->offset)))); ++buf->cur; buf->offset = (bits - (8 - buf->offset)); } return c & ~(((UINT8)0xff) << bits);}static inline void pack_bits(BUFFER* to, BUFFER* from, UINT8 bits /* range:[1..255] */){ UINT8 n = 8 - (to)->offset; UINT8 c = eat_bits((from), n); (to)->buf[(to)->cur] &= ((UINT8)0xff) << n; (to)->buf[(to)->cur] |= c; if ((bits) < n) { (to)->offset += bits; } else { ++(to)->cur; (to)->offset = 0; for (n = (bits) - n; n >= 8; n -= 8) { c = eat_bits((from), 8); (to)->buf[(to)->cur++] = c; } if (n) {\ c = eat_bits((from), n); (to)->buf[(to)->cur] &= ((UINT8)0xff) >> n; (to)->buf[(to)->cur] |= c << (8 - n); } (to)->offset = n; }}/* * the below function can only be used in little endian system */static inline void pack_num(BUFFER* to, UINT32 n, UINT8 bits /* range:[1..32] */){ UINT8 buf[4]; BUFFER from; buf[0] = (n >> 24) & 0xff; buf[1] = (n >> 16) & 0xff; buf[2] = (n >> 8) & 0xff; buf[3] = n & 0xff; from.buf = buf; /* from.len = 4; */ from.cur = 3 - ((bits - 1) >> 3); from.offset = (~(bits - 1)) & 0x07; pack_bits(to, &from, bits);}/* * the below function can only be used in little endian system */static inline UINT32 unpack_num(BUFFER* from, UINT32* n, UINT8 bits /* range:[1..32] */){ if (bits > 24) { *n &= 0x00ffffff; *n |= eat_bits(from, ((bits - 1) & 0x07) + 1) << 24; bits = 24; } if (bits > 16) { *n &= 0xff00ffff; *n |= eat_bits(from, ((bits - 1) & 0x07) + 1) << 16; bits = 16; } if (bits > 8) { *n &= 0xffff00ff; *n |= eat_bits(from, ((bits - 1) & 0x07) + 1) << 8; bits = 8; } *n &= 0xffffff00; *n |= eat_bits(from, ((bits - 1) & 0x07) + 1); return *n;}static inline void pack_num_x(BUFFER* buf, UINT32* n, UINT8 bits /* range:[1..32] */){ pack_num(buf, *n, bits);}static inline void unpack_num_x(BUFFER* buf, UINT32* n, UINT8 bits /* range:[1..32] */){ unpack_num(buf, n, bits);}int str2hex(const char* s, char* buf);void fdumphex(FILE* f, const unsigned char* buf, size_t len);static inline void dumphex(const unsigned char* buf, size_t len){ fdumphex(stdout, buf, len);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -