BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file expstring.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_MISC_EXPSTRING_H
31 #define BADVPN_MISC_EXPSTRING_H
32  
33 #include <stddef.h>
34  
35 #include <misc/debug.h>
36 #include <misc/exparray.h>
37 #include <misc/bsize.h>
38 #include <misc/memref.h>
39  
40 typedef struct {
41 struct ExpArray arr;
42 size_t n;
43 } ExpString;
44  
45 static int ExpString_Init (ExpString *c);
46 static void ExpString_Free (ExpString *c);
47 static int ExpString_Append (ExpString *c, const char *str);
48 static int ExpString_AppendChar (ExpString *c, char ch);
49 static int ExpString_AppendByte (ExpString *c, uint8_t x);
50 static int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len);
51 static int ExpString_AppendBinaryMr (ExpString *c, MemRef data);
52 static int ExpString_AppendZeros (ExpString *c, size_t len);
53 static char * ExpString_Get (ExpString *c);
54 static size_t ExpString_Length (ExpString *c);
55 static MemRef ExpString_GetMr (ExpString *c);
56  
57 int ExpString_Init (ExpString *c)
58 {
59 if (!ExpArray_init(&c->arr, 1, 16)) {
60 return 0;
61 }
62  
63 c->n = 0;
64 ((char *)c->arr.v)[c->n] = '\0';
65  
66 return 1;
67 }
68  
69 void ExpString_Free (ExpString *c)
70 {
71 free(c->arr.v);
72 }
73  
74 int ExpString_Append (ExpString *c, const char *str)
75 {
76 ASSERT(str)
77  
78 size_t l = strlen(str);
79 bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(l), bsize_fromint(1)));
80  
81 if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
82 return 0;
83 }
84  
85 memcpy((char *)c->arr.v + c->n, str, l);
86 c->n += l;
87 ((char *)c->arr.v)[c->n] = '\0';
88  
89 return 1;
90 }
91  
92 int ExpString_AppendChar (ExpString *c, char ch)
93 {
94 ASSERT(ch != '\0')
95  
96 bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_fromint(2));
97  
98 if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
99 return 0;
100 }
101  
102 ((char *)c->arr.v)[c->n] = ch;
103 c->n++;
104 ((char *)c->arr.v)[c->n] = '\0';
105  
106 return 1;
107 }
108  
109 int ExpString_AppendByte (ExpString *c, uint8_t x)
110 {
111 bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_fromint(2));
112  
113 if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
114 return 0;
115 }
116  
117 ((uint8_t *)c->arr.v)[c->n] = x;
118 c->n++;
119 ((char *)c->arr.v)[c->n] = '\0';
120  
121 return 1;
122 }
123  
124 int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len)
125 {
126 bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(len), bsize_fromint(1)));
127  
128 if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
129 return 0;
130 }
131  
132 memcpy((char *)c->arr.v + c->n, data, len);
133 c->n += len;
134 ((char *)c->arr.v)[c->n] = '\0';
135  
136 return 1;
137 }
138  
139 int ExpString_AppendBinaryMr (ExpString *c, MemRef data)
140 {
141 return ExpString_AppendBinary(c, (uint8_t const *)data.ptr, data.len);
142 }
143  
144 int ExpString_AppendZeros (ExpString *c, size_t len)
145 {
146 bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(len), bsize_fromint(1)));
147  
148 if (newsize.is_overflow || !ExpArray_resize(&c->arr, newsize.value)) {
149 return 0;
150 }
151  
152 memset((char *)c->arr.v + c->n, 0, len);
153 c->n += len;
154 ((char *)c->arr.v)[c->n] = '\0';
155  
156 return 1;
157 }
158  
159 char * ExpString_Get (ExpString *c)
160 {
161 return (char *)c->arr.v;
162 }
163  
164 size_t ExpString_Length (ExpString *c)
165 {
166 return c->n;
167 }
168  
169 MemRef ExpString_GetMr (ExpString *c)
170 {
171 return MemRef_Make((char const *)c->arr.v, c->n);
172 }
173  
174 #endif