BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file bstring.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #ifndef BADVPN_BSTRING_H |
||
31 | #define BADVPN_BSTRING_H |
||
32 | |||
33 | #include <misc/debug.h> |
||
34 | |||
35 | #include <stdlib.h> |
||
36 | #include <string.h> |
||
37 | |||
38 | #define BSTRING_TYPE_STATIC 5 |
||
39 | #define BSTRING_TYPE_DYNAMIC 7 |
||
40 | #define BSTRING_TYPE_EXTERNAL 11 |
||
41 | |||
42 | #define BSTRING_STATIC_SIZE 23 |
||
43 | #define BSTRING_STATIC_MAX (BSTRING_STATIC_SIZE - 1) |
||
44 | |||
45 | typedef struct { |
||
46 | union { |
||
47 | struct { |
||
48 | char type; |
||
49 | char static_string[BSTRING_STATIC_SIZE]; |
||
50 | } us; |
||
51 | struct { |
||
52 | char type; |
||
53 | char *dynamic_string; |
||
54 | } ud; |
||
55 | struct { |
||
56 | char type; |
||
57 | const char *external_string; |
||
58 | } ue; |
||
59 | } u; |
||
60 | } BString; |
||
61 | |||
62 | static void BString__assert (BString *o) |
||
63 | { |
||
64 | switch (o->u.us.type) { |
||
65 | case BSTRING_TYPE_STATIC: |
||
66 | case BSTRING_TYPE_DYNAMIC: |
||
67 | case BSTRING_TYPE_EXTERNAL: |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | ASSERT(0); |
||
72 | } |
||
73 | |||
74 | static int BString_Init (BString *o, const char *str) |
||
75 | { |
||
76 | if (strlen(str) <= BSTRING_STATIC_MAX) { |
||
77 | strcpy(o->u.us.static_string, str); |
||
78 | o->u.us.type = BSTRING_TYPE_STATIC; |
||
79 | } else { |
||
80 | if (!(o->u.ud.dynamic_string = malloc(strlen(str) + 1))) { |
||
81 | return 0; |
||
82 | } |
||
83 | strcpy(o->u.ud.dynamic_string, str); |
||
84 | o->u.ud.type = BSTRING_TYPE_DYNAMIC; |
||
85 | } |
||
86 | |||
87 | BString__assert(o); |
||
88 | return 1; |
||
89 | } |
||
90 | |||
91 | static void BString_InitStatic (BString *o, const char *str) |
||
92 | { |
||
93 | ASSERT(strlen(str) <= BSTRING_STATIC_MAX) |
||
94 | |||
95 | strcpy(o->u.us.static_string, str); |
||
96 | o->u.us.type = BSTRING_TYPE_STATIC; |
||
97 | |||
98 | BString__assert(o); |
||
99 | } |
||
100 | |||
101 | static void BString_InitExternal (BString *o, const char *str) |
||
102 | { |
||
103 | o->u.ue.external_string = str; |
||
104 | o->u.ue.type = BSTRING_TYPE_EXTERNAL; |
||
105 | |||
106 | BString__assert(o); |
||
107 | } |
||
108 | |||
109 | static void BString_InitAllocated (BString *o, char *str) |
||
110 | { |
||
111 | o->u.ud.dynamic_string = str; |
||
112 | o->u.ud.type = BSTRING_TYPE_DYNAMIC; |
||
113 | |||
114 | BString__assert(o); |
||
115 | } |
||
116 | |||
117 | static void BString_Free (BString *o) |
||
118 | { |
||
119 | BString__assert(o); |
||
120 | |||
121 | if (o->u.ud.type == BSTRING_TYPE_DYNAMIC) { |
||
122 | free(o->u.ud.dynamic_string); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | static const char * BString_Get (BString *o) |
||
127 | { |
||
128 | BString__assert(o); |
||
129 | |||
130 | switch (o->u.us.type) { |
||
131 | case BSTRING_TYPE_STATIC: return o->u.us.static_string; |
||
132 | case BSTRING_TYPE_DYNAMIC: return o->u.ud.dynamic_string; |
||
133 | case BSTRING_TYPE_EXTERNAL: return o->u.ue.external_string; |
||
134 | } |
||
135 | |||
136 | ASSERT(0); |
||
137 | return NULL; |
||
138 | } |
||
139 | |||
140 | #endif |