This repository was archived by the owner on Oct 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstruct.h
More file actions
126 lines (121 loc) · 4.04 KB
/
struct.h
File metadata and controls
126 lines (121 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* struct.h
*
* Created on: 2011. 5. 2.
* Author: Wonseok
*
* stolen from https://github.com/svperbeast/struct
*
* modify:
* - pack/unpack Null-terminated string
* - pack/unpack blob data
* - pack/unpack *struct timeb*
*
* Interpret strings as packed binary data
*
*
* Table 1. Byte order
* ----------------------------------
* Character | Byte order
* ----------+-----------------------
* = | native
* ----------+-----------------------
* < | little-endian
* ----------+-----------------------
* > | big-endian
* ----------+-----------------------
* ! | network (= big-endian)
* ----------------------------------
*
* Table 2. Format characters
* -------------------------------------------
* Format | C/C++ Type | Standard size
* -------+--------------------+--------------
* b | char | 1
* -------+--------------------+--------------
* B | unsigned char | 1
* -------+--------------------+--------------
* h | short | 2
* -------+--------------------+--------------
* H | unsigned short | 2
* -------+--------------------+--------------
* i | int | 4
* -------+--------------------+--------------
* I | unsigned int | 4
* -------+--------------------+--------------
* l | long | 4
* -------+--------------------+--------------
* L | unsigned long | 4
* -------+--------------------+--------------
* q | long long | 8
* -------+--------------------+--------------
* Q | unsigned long long | 8
* -------+--------------------+--------------
* f | float | 4
* -------+--------------------+--------------
* d | double | 8
* -------+--------------------+--------------
* s | char * | Null-terminated string
* -------+--------------------+--------------
* o | char * | blob data
* -------+--------------------+---------------
* t | struct timeb | sizeof(struct timeb) == 14
* --------------------------------------------
*
* Note. blob data and null-terminated string will add extern null character.
*
* +-----------------+--------+--------+...+--------+--------+...+--------+
* | 4 byte length n | byte 0 | byte 1 |...|byte n-1| 0 |...| 0 |
* +-----------------+--------+--------+...+--------+--------+...+--------+
* |<-----------n bytes data----->|<------r bytes------>|
* |<-----------n+r (where (n+r) mod 4 = 0)>----------->|
*
* A format character may be preceded by an integral repeat count.
* For example, the format string '4h' means exactly the same as 'hhhh'.
*
* For the 's' format character:
* [string length 4 byte][string content][0 ~ 3 zero]
*
* Example 1. pack/unpack int type value.
*
* char buf[BUFSIZ] = {0, };
* int val = 0x12345678;
* int oval;
*
* struct_pack(buf, "i", val);
* struct_unpack(buf, "i", &oval);
*
* Example 2. pack/unpack a string.
*
* char buf[BUFSIZ] = {0, );
* char *ostr, *str;
*
* struct_pack(buf, "!2s", "test", "packet");
*
* now, buf maybe like this:
*
* OFFSET HEX BYTES ASCII COMMENT
* ------ ----------- ------ --------
* 00 00 00 00 04 .... length of first string
* 04 74 65 73 74 test first string data
* 08 00 00 00 06 .... length of second string
* 12 70 61 63 6b pack second string data
* 16 65 74 00 00 et.. .. and 2 zero-byte to fill
*
* struct_unpack(buf, "!2s", &ostr, &str);
*
*/
#ifndef _STRUCT_H_
#define _STRUCT_H_
#ifdef __cplusplus
extern "C" {
#endif
extern int struct_pack(unsigned char *buf, const char *fmt, ...);
extern int struct_pack_into(int offset, unsigned char *buf, const char *fmt, ...);
extern int struct_unpack(unsigned char *buf, const char *fmt, ...);
extern int struct_unpack_from(int offset, unsigned char *buf, const char *fmt, ...);
extern int struct_calcsize(const char *fmt);
#ifdef __cplusplus
}
#endif
#endif /* _STRUCT_H_ */