1 #ifndef STRUCT_INCLUDED 2 #define STRUCT_INCLUDED 3 /* 4 * struct.h 5 * 6 * Created on: 2011. 5. 2. 7 * Author: wonseok choi (svperbeast@gmail.com) 8 * 9 * Interpret strings as packed binary data 10 * 11 * Table 1. Byte order 12 * ---------------------------------- 13 * Character | Byte order 14 * ----------+----------------------- 15 * = | native 16 * ----------+----------------------- 17 * < | little-endian 18 * ----------+----------------------- 19 * > | big-endian 20 * ----------+----------------------- 21 * ! | network (= big-endian) 22 * ---------------------------------- 23 * 24 * Table 2. Format characters 25 * ------------------------------------------- 26 * Format | C/C++ Type | Standard size 27 * -------+--------------------+-------------- 28 * b | char | 1 29 * -------+--------------------+-------------- 30 * B | unsigned char | 1 31 * -------+--------------------+-------------- 32 * h | short | 2 33 * -------+--------------------+-------------- 34 * H | unsigned short | 2 35 * -------+--------------------+-------------- 36 * i | int | 4 37 * -------+--------------------+-------------- 38 * I | unsigned int | 4 39 * -------+--------------------+-------------- 40 * l | long | 4 41 * -------+--------------------+-------------- 42 * L | unsigned long | 4 43 * -------+--------------------+-------------- 44 * q | long long | 8 45 * -------+--------------------+-------------- 46 * Q | unsigned long long | 8 47 * -------+--------------------+-------------- 48 * f | float | 4 49 * -------+--------------------+-------------- 50 * d | double | 8 51 * -------+--------------------+-------------- 52 * s | char[] | 53 * -------+--------------------+-------------- 54 * p | char[] | 55 * -------+--------------------+-------------- 56 * x | pad bytes | 57 * ------------------------------------------- 58 * 59 * A format character may be preceded by an integral repeat count. 60 * For example, the format string '4h' means exactly the same as 'hhhh'. 61 * 62 * For the 's' format character, the count is interpreted as the size of the 63 * string, not a repeat count like for the other format characters. 64 * For example, '10s' means a single 10-byte string. 65 * 66 * Example 1. pack/unpack int type value. 67 * 68 * char buf[BUFSIZ] = {0, }; 69 * int val = 0x12345678; 70 * int oval; 71 * 72 * struct_pack(buf, "i", val); 73 * struct_unpack(buf, "i", &oval); 74 * 75 * Example 2. pack/unpack a string. 76 * 77 * char buf[BUFSIZ] = {0, }; 78 * char str[32] = {'\0', }; 79 * char fmt[32] = {'\0', }; 80 * char ostr[32] = {'\0', }; 81 * 82 * strcpy(str, "test"); 83 * sprintf(fmt, "%ds", strlen(str)); 84 * 85 * struct_pack(buf, fmt, str); 86 * struct_unpack(buf, fmt, ostr); 87 * 88 */ 89 90 #ifdef __cplusplus 91 extern "C" 92 { 93 #endif 94 95 /* simple size macros 96 * the standard sizes of signed/unsigned are the same. 97 */ 98 #define STRUCT_BSIZE 1 99 #define STRUCT_HSIZE 2 100 #define STRUCT_ISIZE 4 101 #define STRUCT_QSIZE 8 102 #define STRUCT_FSIZE 4 103 #define STRUCT_DSIZE 8 104 105 /** 106 * @brief pack data 107 * @return the number of bytes encoded on success, -1 on failure. 108 */ 109 extern int struct_pack(void* buf, const char* fmt, ...); 110 111 /** 112 * @brief pack data with offset 113 * @return the number of bytes encoded on success, -1 on failure. 114 */ 115 extern int struct_pack_into(int offset, void* buf, const char* fmt, ...); 116 117 /** 118 * @brief unpack data 119 * @return the number of bytes decoded on success, -1 on failure. 120 */ 121 extern int struct_unpack(const void* buf, const char* fmt, ...); 122 123 /** 124 * @brief unpack data with offset 125 * @return the number of bytes decoded on success, -1 on failure. 126 */ 127 extern int struct_unpack_from(int offset, const void* buf, const char* fmt, 128 ...); 129 130 /** 131 * @brief calculate the size of a format string 132 * @return the number of bytes needed by the format string on success, 133 * -1 on failure. 134 * 135 * make sure that the return value is > 0, before using it. 136 */ 137 extern int struct_calcsize(const char* fmt); 138 139 #ifdef __cplusplus 140 } 141 #endif 142 143 #endif /* !STRUCT_INCLUDED */ 144