OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
fsw_lib.c
Go to the documentation of this file.
1
6/*-
7 * Copyright (c) 2020 Vladislav Yaroshchuk <yaroshchuk2000@gmail.com>
8 * Copyright (c) 2006 Christoph Pfisterer
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met:
13 *
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the
20 * distribution.
21 *
22 * * Neither the name of Christoph Pfisterer nor the names of the
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include "fsw_core.h"
40
41/* Include generated string encoding specific functions */
42#include "fsw_strfunc.h"
43
44
49fsw_status_t fsw_alloc_zero(int len, void **ptr_out)
50{
51 fsw_status_t status;
52
53 status = fsw_alloc(len, ptr_out);
54 if (status)
55 return status;
56 fsw_memzero(*ptr_out, len);
57 return FSW_SUCCESS;
58}
59
64fsw_status_t fsw_memdup(void **dest_out, void *src, int len)
65{
66 fsw_status_t status;
67
68 status = fsw_alloc(len, dest_out);
69 if (status)
70 return status;
71 fsw_memcpy(*dest_out, src, len);
72 return FSW_SUCCESS;
73}
74
79int fsw_strlen(struct fsw_string *s)
80{
81 if (s == NULL || s->type == FSW_STRING_TYPE_EMPTY)
82 return 0;
83 return s->len;
84}
85
89int fsw_strsize(struct fsw_string *s)
90{
91 if (s == NULL || s->type == FSW_STRING_TYPE_EMPTY)
92 return 0;
93
94 return s->size;
95}
96
100void *fsw_strdata(struct fsw_string *s)
101{
102 if (s == NULL || s->type == FSW_STRING_TYPE_EMPTY)
103 return NULL;
104
105 return (void *) s->data;
106}
107
114int fsw_streq(struct fsw_string *s1, struct fsw_string *s2)
115{
116 struct fsw_string temp_s;
117
118 // handle empty strings
119 if (s1->type == FSW_STRING_TYPE_EMPTY) {
121 temp_s.size = temp_s.len = 0;
122 temp_s.data = NULL;
123 return fsw_streq(&temp_s, s2);
124 }
125 if (s2->type == FSW_STRING_TYPE_EMPTY) {
127 temp_s.size = temp_s.len = 0;
128 temp_s.data = NULL;
129 return fsw_streq(s1, &temp_s);
130 }
131
132 // check length (count of chars)
133 if (s1->len != s2->len)
134 return 0;
135 if (s1->len == 0) // both strings are empty
136 return 1;
137
138 if (s1->type == s2->type) {
139 // same type, do a dumb memory compare
140 if (s1->size != s2->size)
141 return 0;
142 return fsw_memeq(s1->data, s2->data, s1->size);
143 }
144
145 // dispatch to type-specific functions
146 #define STREQ_DISPATCH(type1, type2) \
147 if (s1->type == FSW_STRING_TYPE_##type1 && s2->type == FSW_STRING_TYPE_##type2) \
148 return fsw_streq_##type1##_##type2(s1->data, s2->data, s1->len); \
149 if (s2->type == FSW_STRING_TYPE_##type1 && s1->type == FSW_STRING_TYPE_##type2) \
150 return fsw_streq_##type1##_##type2(s2->data, s1->data, s1->len);
151 STREQ_DISPATCH(ISO88591, UTF8);
152 STREQ_DISPATCH(ISO88591, UTF16);
153 STREQ_DISPATCH(ISO88591, UTF16_SWAPPED);
154 STREQ_DISPATCH(UTF8, UTF16);
155 STREQ_DISPATCH(UTF8, UTF16_SWAPPED);
156 STREQ_DISPATCH(UTF16, UTF16_SWAPPED);
157
158 // final fallback
159 return 0;
160}
161
169int fsw_streq_cstr(struct fsw_string *s1, const char *s2)
170{
171 struct fsw_string temp_s;
172 int i;
173
174 for (i = 0; s2[i]; i++)
175 ;
176
178 temp_s.size = temp_s.len = i;
179 temp_s.data = (char *)s2;
180
181 return fsw_streq(s1, &temp_s);
182}
183
191{
192 fsw_status_t status;
193
194 if (src->type == FSW_STRING_TYPE_EMPTY || src->len == 0) {
195 dest->type = type;
196 dest->size = dest->len = 0;
197 dest->data = NULL;
198 return FSW_SUCCESS;
199 }
200
201 if (src->type == type) {
202 dest->type = type;
203 dest->len = src->len;
204 dest->size = src->size;
205 status = fsw_alloc(dest->size, &dest->data);
206 if (status)
207 return status;
208
209 fsw_memcpy(dest->data, src->data, dest->size);
210 return FSW_SUCCESS;
211 }
212
213 // dispatch to type-specific functions
214 #define STRCOERCE_DISPATCH(type1, type2) \
215 if (src->type == FSW_STRING_TYPE_##type1 && type == FSW_STRING_TYPE_##type2) \
216 return fsw_strcoerce_##type1##_##type2(src->data, src->len, dest);
217 STRCOERCE_DISPATCH(UTF8, ISO88591);
218 STRCOERCE_DISPATCH(UTF16, ISO88591);
219 STRCOERCE_DISPATCH(UTF16_SWAPPED, ISO88591);
220 STRCOERCE_DISPATCH(ISO88591, UTF8);
221 STRCOERCE_DISPATCH(UTF16, UTF8);
222 STRCOERCE_DISPATCH(UTF16_SWAPPED, UTF8);
223 STRCOERCE_DISPATCH(ISO88591, UTF16);
224 STRCOERCE_DISPATCH(UTF8, UTF16);
225 STRCOERCE_DISPATCH(UTF16_SWAPPED, UTF16);
226
227 return FSW_UNSUPPORTED;
228}
229
246void fsw_strsplit(struct fsw_string *element, struct fsw_string *buffer, char separator)
247{
248 int i, maxlen;
249
250 if (buffer->type == FSW_STRING_TYPE_EMPTY || buffer->len == 0) {
251 element->type = FSW_STRING_TYPE_EMPTY;
252 return;
253 }
254
255 maxlen = buffer->len;
256 *element = *buffer;
257
258 if (buffer->type == FSW_STRING_TYPE_ISO88591) {
259 fsw_u8 *p;
260
261 p = (fsw_u8 *)element->data;
262 for (i = 0; i < maxlen; i++, p++) {
263 if (*p == separator) {
264 buffer->data = p + 1;
265 buffer->len -= i + 1;
266 break;
267 }
268 }
269 element->len = i;
270 if (i == maxlen) {
271 buffer->data = p;
272 buffer->len -= i;
273 }
274
275 element->size = element->len;
276 buffer->size = buffer->len;
277
278 } else if (buffer->type == FSW_STRING_TYPE_UTF16) {
279 fsw_u16 *p;
280
281 p = (fsw_u16 *)element->data;
282 for (i = 0; i < maxlen; i++, p++) {
283 if (*p == separator) {
284 buffer->data = p + 1;
285 buffer->len -= i + 1;
286 break;
287 }
288 }
289 element->len = i;
290 if (i == maxlen) {
291 buffer->data = p;
292 buffer->len -= i;
293 }
294
295 element->size = element->len * sizeof(fsw_u16);
296 buffer->size = buffer->len * sizeof(fsw_u16);
297
298 } else {
299 // fallback
300 buffer->type = FSW_STRING_TYPE_EMPTY;
301 }
302
303 // TODO: support UTF8 and UTF16_SWAPPED
304}
305
310void fsw_strfree(struct fsw_string *s)
311{
312 if (s->type != FSW_STRING_TYPE_EMPTY && s->data)
313 fsw_free(s->data);
315}
316
317// EOF
cache_type_t type
int fsw_status_t
Definition fsw_core.h:153
@ FSW_UNSUPPORTED
Definition fsw_core.h:162
@ FSW_SUCCESS
Definition fsw_core.h:159
@ FSW_STRING_TYPE_ISO88591
Definition fsw_core.h:186
@ FSW_STRING_TYPE_EMPTY
Definition fsw_core.h:185
@ FSW_STRING_TYPE_UTF16
Definition fsw_core.h:188
#define fsw_memcpy(dest, src, size)
#define fsw_memzero(dest, size)
UINT16 fsw_u16
#define fsw_free(ptr)
#define fsw_alloc(size, ptrptr)
UINT8 fsw_u8
#define fsw_memeq(p1, p2, size)
#define STRCOERCE_DISPATCH(type1, type2)
void fsw_strfree(struct fsw_string *s)
Definition fsw_lib.c:310
int fsw_streq(struct fsw_string *s1, struct fsw_string *s2)
Definition fsw_lib.c:114
int fsw_strsize(struct fsw_string *s)
Definition fsw_lib.c:89
int fsw_streq_cstr(struct fsw_string *s1, const char *s2)
Definition fsw_lib.c:169
int fsw_strlen(struct fsw_string *s)
Definition fsw_lib.c:79
fsw_status_t fsw_strdup_coerce(struct fsw_string *dest, int type, struct fsw_string *src)
Definition fsw_lib.c:190
#define STREQ_DISPATCH(type1, type2)
fsw_status_t fsw_memdup(void **dest_out, void *src, int len)
Definition fsw_lib.c:64
void * fsw_strdata(struct fsw_string *s)
Definition fsw_lib.c:100
void fsw_strsplit(struct fsw_string *element, struct fsw_string *buffer, char separator)
Definition fsw_lib.c:246
fsw_status_t fsw_alloc_zero(int len, void **ptr_out)
Definition fsw_lib.c:49
void * data
Data pointer (may be NULL if type is EMPTY or len is zero)
Definition fsw_core.h:177
int len
Length in characters.
Definition fsw_core.h:175
int size
Total data size in bytes.
Definition fsw_core.h:176
int type
Encoding of the string - empty, ISO-8859-1, UTF8, UTF16.
Definition fsw_core.h:174