BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file memref.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_MEMREF_H |
||
31 | #define BADVPN_MEMREF_H |
||
32 | |||
33 | #include <stddef.h> |
||
34 | #include <string.h> |
||
35 | #include <limits.h> |
||
36 | |||
37 | #include <misc/debug.h> |
||
38 | #include <misc/balloc.h> |
||
39 | #include <misc/strdup.h> |
||
40 | |||
41 | typedef struct { |
||
42 | char const *ptr; |
||
43 | size_t len; |
||
44 | } MemRef; |
||
45 | |||
46 | static MemRef MemRef_Make (char const *ptr, size_t len); |
||
47 | static MemRef MemRef_MakeCstr (char const *ptr); |
||
48 | static char MemRef_At (MemRef o, size_t pos); |
||
49 | static void MemRef_AssertRange (MemRef o, size_t offset, size_t length); |
||
50 | static MemRef MemRef_SubFrom (MemRef o, size_t offset); |
||
51 | static MemRef MemRef_SubTo (MemRef o, size_t length); |
||
52 | static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length); |
||
53 | static char * MemRef_StrDup (MemRef o); |
||
54 | static void MemRef_CopyOut (MemRef o, char *out); |
||
55 | static int MemRef_Equal (MemRef o, MemRef other); |
||
56 | static int MemRef_FindChar (MemRef o, char ch, size_t *out_index); |
||
57 | |||
58 | #define MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \ |
||
59 | { \ |
||
60 | for (size_t char_rel_pos_var = 0; char_rel_pos_var < MemRef__Loop_length; char_rel_pos_var++) { \ |
||
61 | char char_var = MemRef__Loop_o.ptr[MemRef__Loop_offset + char_rel_pos_var]; \ |
||
62 | { body } \ |
||
63 | } \ |
||
64 | } |
||
65 | |||
66 | #define MEMREF_LOOP_CHARS_RANGE(o, offset, length, char_rel_pos_var, char_var, body) \ |
||
67 | { \ |
||
68 | MemRef MemRef__Loop_o = (o); \ |
||
69 | size_t MemRef__Loop_offset = (offset); \ |
||
70 | size_t MemRef__Loop_length = (length); \ |
||
71 | MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \ |
||
72 | } |
||
73 | |||
74 | #define MEMREF_LOOP_CHARS(o, char_rel_pos_var, char_var, body) \ |
||
75 | { \ |
||
76 | MemRef MemRef__Loop_o = (o); \ |
||
77 | size_t MemRef__Loop_offset = 0; \ |
||
78 | size_t MemRef__Loop_length = MemRef__Loop_o.len; \ |
||
79 | MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \ |
||
80 | } |
||
81 | |||
82 | // |
||
83 | |||
84 | static MemRef MemRef_Make (char const *ptr, size_t len) |
||
85 | { |
||
86 | MemRef res; |
||
87 | res.ptr = ptr; |
||
88 | res.len = len; |
||
89 | return res; |
||
90 | } |
||
91 | |||
92 | static MemRef MemRef_MakeCstr (char const *ptr) |
||
93 | { |
||
94 | ASSERT(ptr) |
||
95 | |||
96 | return MemRef_Make(ptr, strlen(ptr)); |
||
97 | } |
||
98 | |||
99 | static char MemRef_At (MemRef o, size_t pos) |
||
100 | { |
||
101 | ASSERT(o.ptr) |
||
102 | ASSERT(pos < o.len) |
||
103 | |||
104 | return o.ptr[pos]; |
||
105 | } |
||
106 | |||
107 | static void MemRef_AssertRange (MemRef o, size_t offset, size_t length) |
||
108 | { |
||
109 | ASSERT(offset <= o.len) |
||
110 | ASSERT(length <= o.len - offset) |
||
111 | } |
||
112 | |||
113 | static MemRef MemRef_SubFrom (MemRef o, size_t offset) |
||
114 | { |
||
115 | ASSERT(o.ptr) |
||
116 | ASSERT(offset <= o.len) |
||
117 | |||
118 | return MemRef_Make(o.ptr + offset, o.len - offset); |
||
119 | } |
||
120 | |||
121 | static MemRef MemRef_SubTo (MemRef o, size_t length) |
||
122 | { |
||
123 | ASSERT(o.ptr) |
||
124 | ASSERT(length <= o.len) |
||
125 | |||
126 | return MemRef_Make(o.ptr, length); |
||
127 | } |
||
128 | |||
129 | static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length) |
||
130 | { |
||
131 | ASSERT(o.ptr) |
||
132 | MemRef_AssertRange(o, offset, length); |
||
133 | |||
134 | return MemRef_Make(o.ptr + offset, length); |
||
135 | } |
||
136 | |||
137 | static char * MemRef_StrDup (MemRef o) |
||
138 | { |
||
139 | ASSERT(o.ptr) |
||
140 | |||
141 | return b_strdup_bin(o.ptr, o.len); |
||
142 | } |
||
143 | |||
144 | static void MemRef_CopyOut (MemRef o, char *out) |
||
145 | { |
||
146 | ASSERT(o.ptr) |
||
147 | ASSERT(out) |
||
148 | |||
149 | memcpy(out, o.ptr, o.len); |
||
150 | } |
||
151 | |||
152 | static int MemRef_Equal (MemRef o, MemRef other) |
||
153 | { |
||
154 | ASSERT(o.ptr) |
||
155 | ASSERT(other.ptr) |
||
156 | |||
157 | return (o.len == other.len) && !memcmp(o.ptr, other.ptr, o.len); |
||
158 | } |
||
159 | |||
160 | static int MemRef_FindChar (MemRef o, char ch, size_t *out_index) |
||
161 | { |
||
162 | ASSERT(o.ptr) |
||
163 | |||
164 | for (size_t i = 0; i < o.len; i++) { |
||
165 | if (o.ptr[i] == ch) { |
||
166 | if (out_index) { |
||
167 | *out_index = i; |
||
168 | } |
||
169 | return 1; |
||
170 | } |
||
171 | } |
||
172 | return 0; |
||
173 | } |
||
174 | |||
175 | #endif |