Debian Codegraph @

crt/aws-c-common/source/string.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/string.h>
6
7 #ifdef _WIN32
8 # include <windows.h>
9
10 struct aws_wstring *aws_string_convert_to_wstring(
11 struct aws_allocator *allocator,
12 const struct aws_string *to_convert) {
13 AWS_PRECONDITION(to_convert);
14
15 struct aws_byte_cursor convert_cur = aws_byte_cursor_from_string(to_convert);
16 return aws_string_convert_to_wchar_from_byte_cursor(allocator, &convert_cur);
17 }
18
19 struct aws_wstring *aws_string_convert_to_wchar_from_byte_cursor(
20 struct aws_allocator *allocator,
21 const struct aws_byte_cursor *to_convert) {
22 AWS_PRECONDITION(to_convert);
23
24 /* if a length is passed for the to_convert string, converted size does not include the null terminator,
25 * which is a good thing. */
26 int converted_size = MultiByteToWideChar(CP_UTF8, 0, (const char *)to_convert->ptr, (int)to_convert->len, NULL, 0);
27
28 if (!converted_size) {
29 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
30 return NULL;
31 }
32
33 size_t str_len_size = 0;
34 size_t malloc_size = 0;
35
36 /* double the size because the return value above is # of characters, not bytes size. */
37 if (aws_mul_size_checked(sizeof(wchar_t), converted_size, &str_len_size)) {
38 return NULL;
39 }
40
41 /* UTF-16, the NULL terminator is two bytes. */
42 if (aws_add_size_checked(sizeof(struct aws_wstring) + 2, str_len_size, &malloc_size)) {
43 return NULL;
44 }
45
46 struct aws_wstring *str = aws_mem_acquire(allocator, malloc_size);
47 if (!str) {
48 return NULL;
49 }
50
51 /* Fields are declared const, so we need to copy them in like this */
52 *(struct aws_allocator **)(&str->allocator) = allocator;
53 *(size_t *)(&str->len) = (size_t)converted_size;
54
55 int converted_res = MultiByteToWideChar(
56 CP_UTF8, 0, (const char *)to_convert->ptr, (int)to_convert->len, (wchar_t *)str->bytes, converted_size);
57 /* windows had its chance to do its thing, no take backsies. */
58 AWS_FATAL_ASSERT(converted_res > 0);
59
60 *(wchar_t *)&str->bytes[converted_size] = 0;
61 return str;
62 }
63
64 struct aws_wstring *aws_wstring_new_from_cursor(
65 struct aws_allocator *allocator,
66 const struct aws_byte_cursor *w_str_cur) {
67 AWS_PRECONDITION(allocator && aws_byte_cursor_is_valid(w_str_cur));
68 return aws_wstring_new_from_array(allocator, (wchar_t *)w_str_cur->ptr, w_str_cur->len / sizeof(wchar_t));
69 }
70
71 struct aws_wstring *aws_wstring_new_from_array(struct aws_allocator *allocator, const wchar_t *w_str, size_t len) {
72 AWS_PRECONDITION(allocator);
73 AWS_PRECONDITION(AWS_MEM_IS_READABLE(w_str, len));
74
75 size_t str_byte_len = 0;
76 size_t malloc_size = 0;
77
78 /* double the size because the return value above is # of characters, not bytes size. */
79 if (aws_mul_size_checked(sizeof(wchar_t), len, &str_byte_len)) {
80 return NULL;
81 }
82
83 /* UTF-16, the NULL terminator is two bytes. */
84 if (aws_add_size_checked(sizeof(struct aws_wstring) + 2, str_byte_len, &malloc_size)) {
85 return NULL;
86 }
87
88 struct aws_wstring *str = aws_mem_acquire(allocator, malloc_size);
89
90 /* Fields are declared const, so we need to copy them in like this */
91 *(struct aws_allocator **)(&str->allocator) = allocator;
92 *(size_t *)(&str->len) = len;
93 if (len > 0) {
94 memcpy((void *)str->bytes, w_str, str_byte_len);
95 }
96 /* in case this is a utf-16 string in the array, allow that here. */
97 *(wchar_t *)&str->bytes[len] = 0;
98 AWS_RETURN_WITH_POSTCONDITION(str, aws_wstring_is_valid(str));
99 }
100
101 bool aws_wstring_is_valid(const struct aws_wstring *str) {
102 return str && AWS_MEM_IS_READABLE(&str->bytes[0], str->len + 1) && str->bytes[str->len] == 0;
103 }
104
105 void aws_wstring_destroy(struct aws_wstring *str) {
106 AWS_PRECONDITION(!str || aws_wstring_is_valid(str));
107 if (str && str->allocator) {
108 aws_mem_release(str->allocator, str);
109 }
110 }
111
112 static struct aws_string *s_convert_from_wchar(
113 struct aws_allocator *allocator,
114 const wchar_t *to_convert,
115 int len_chars) {
116 AWS_FATAL_PRECONDITION(to_convert);
117
118 int bytes_size = WideCharToMultiByte(CP_UTF8, 0, to_convert, len_chars, NULL, 0, NULL, NULL);
119
120 if (!bytes_size) {
121 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
122 return NULL;
123 }
124
125 size_t malloc_size = 0;
126
127 /* bytes_size already contains the space for the null terminator */
128 if (aws_add_size_checked(sizeof(struct aws_string), bytes_size, &malloc_size)) {
129 return NULL;
130 }
131
132 struct aws_string *str = aws_mem_acquire(allocator, malloc_size);
133 if (!str) {
134 return NULL;
135 }
136
137 /* Fields are declared const, so we need to copy them in like this */
138 *(struct aws_allocator **)(&str->allocator) = allocator;
139 *(size_t *)(&str->len) = (size_t)bytes_size - 1;
140
141 int converted_res =
142 WideCharToMultiByte(CP_UTF8, 0, to_convert, len_chars, (char *)str->bytes, bytes_size, NULL, NULL);
143 /* windows had its chance to do its thing, no take backsies. */
144 AWS_FATAL_ASSERT(converted_res > 0);
145
146 *(uint8_t *)&str->bytes[str->len] = 0;
147 return str;
148 }
149
150 struct aws_string *aws_string_convert_from_wchar_str(
151 struct aws_allocator *allocator,
152 const struct aws_wstring *to_convert) {
153 AWS_FATAL_PRECONDITION(to_convert);
154
155 return s_convert_from_wchar(allocator, aws_wstring_c_str(to_convert), (int)aws_wstring_num_chars(to_convert));
156 }
157 struct aws_string *aws_string_convert_from_wchar_c_str(struct aws_allocator *allocator, const wchar_t *to_convert) {
158 return s_convert_from_wchar(allocator, to_convert, -1);
159 }
160
161 const wchar_t *aws_wstring_c_str(const struct aws_wstring *str) {
162 AWS_PRECONDITION(str);
163 return str->bytes;
164 }
165
166 size_t aws_wstring_num_chars(const struct aws_wstring *str) {
167 AWS_PRECONDITION(str);
168
169 if (str->len == 0) {
170 return 0;
171 }
172
173 return str->len;
174 }
175
176 size_t aws_wstring_size_bytes(const struct aws_wstring *str) {
177 AWS_PRECONDITION(str);
178
179 return aws_wstring_num_chars(str) * sizeof(wchar_t);
180 }
181
182 #endif /* _WIN32 */
183
184 struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, const char *c_str) {
185 AWS_PRECONDITION(allocator && c_str);
186 return aws_string_new_from_array(allocator, (const uint8_t *)c_str, strlen(c_str));
187 }
188
189 struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) {
190 AWS_PRECONDITION(allocator);
191 AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len));
192
193 struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len + 1]));
194 if (!str) {
195 return NULL;
196 }
197
198 /* Fields are declared const, so we need to copy them in like this */
199 *(struct aws_allocator **)(&str->allocator) = allocator;
200 *(size_t *)(&str->len) = len;
201 if (len > 0) {
202 memcpy((void *)str->bytes, bytes, len);
203 }
204 *(uint8_t *)&str->bytes[len] = 0;
205 AWS_RETURN_WITH_POSTCONDITION(str, aws_string_is_valid(str));
206 }
207
208 struct aws_string *aws_string_new_from_string(struct aws_allocator *allocator, const struct aws_string *str) {
209 AWS_PRECONDITION(allocator && aws_string_is_valid(str));
210 return aws_string_new_from_array(allocator, str->bytes, str->len);
211 }
212
213 struct aws_string *aws_string_new_from_cursor(struct aws_allocator *allocator, const struct aws_byte_cursor *cursor) {
214 AWS_PRECONDITION(allocator && aws_byte_cursor_is_valid(cursor));
215 return aws_string_new_from_array(allocator, cursor->ptr, cursor->len);
216 }
217
218 struct aws_string *aws_string_new_from_buf(struct aws_allocator *allocator, const struct aws_byte_buf *buf) {
219 AWS_PRECONDITION(allocator && aws_byte_buf_is_valid(buf));
220 return aws_string_new_from_array(allocator, buf->buffer, buf->len);
221 }
222
223 void aws_string_destroy(struct aws_string *str) {
224 AWS_PRECONDITION(!str || aws_string_is_valid(str));
225 if (str && str->allocator) {
226 aws_mem_release(str->allocator, str);
227 }
228 }
229
230 void aws_string_destroy_secure(struct aws_string *str) {
231 AWS_PRECONDITION(!str || aws_string_is_valid(str));
232 if (str) {
233 aws_secure_zero((void *)aws_string_bytes(str), str->len);
234 if (str->allocator) {
235 aws_mem_release(str->allocator, str);
236 }
237 }
238 }
239
240 int aws_string_compare(const struct aws_string *a, const struct aws_string *b) {
241 AWS_PRECONDITION(!a || aws_string_is_valid(a));
242 AWS_PRECONDITION(!b || aws_string_is_valid(b));
243 if (a == b) {
244 return 0; /* strings identical */
245 }
246 if (a == NULL) {
247 return -1;
248 }
249 if (b == NULL) {
250 return 1;
251 }
252
253 size_t len_a = a->len;
254 size_t len_b = b->len;
255 size_t min_len = len_a < len_b ? len_a : len_b;
256
257 int ret = memcmp(aws_string_bytes(a), aws_string_bytes(b), min_len);
258 AWS_POSTCONDITION(aws_string_is_valid(a));
259 AWS_POSTCONDITION(aws_string_is_valid(b));
260 if (ret) {
261 return ret; /* overlapping characters differ */
262 }
263 if (len_a == len_b) {
264 return 0; /* strings identical */
265 }
266 if (len_a > len_b) {
267 return 1; /* string b is first n characters of string a */
268 }
269 return -1; /* string a is first n characters of string b */
270 }
271
272 int aws_array_list_comparator_string(const void *a, const void *b) {
273 if (a == b) {
274 return 0; /* strings identical */
275 }
276 if (a == NULL) {
277 return -1;
278 }
279 if (b == NULL) {
280 return 1;
281 }
282 const struct aws_string *str_a = *(const struct aws_string **)a;
283 const struct aws_string *str_b = *(const struct aws_string **)b;
284 return aws_string_compare(str_a, str_b);
285 }
286
287 /**
288 * Returns true if bytes of string are the same, false otherwise.
289 */
290 bool aws_string_eq(const struct aws_string *a, const struct aws_string *b) {
291 AWS_PRECONDITION(!a || aws_string_is_valid(a));
292 AWS_PRECONDITION(!b || aws_string_is_valid(b));
293 if (a == b) {
294 return true;
295 }
296 if (a == NULL || b == NULL) {
297 return false;
298 }
299 return aws_array_eq(a->bytes, a->len, b->bytes, b->len);
300 }
301
302 /**
303 * Returns true if bytes of string are equivalent, using a case-insensitive comparison.
304 */
305 bool aws_string_eq_ignore_case(const struct aws_string *a, const struct aws_string *b) {
306 AWS_PRECONDITION(!a || aws_string_is_valid(a));
307 AWS_PRECONDITION(!b || aws_string_is_valid(b));
308 if (a == b) {
309 return true;
310 }
311 if (a == NULL || b == NULL) {
312 return false;
313 }
314 return aws_array_eq_ignore_case(a->bytes, a->len, b->bytes, b->len);
315 }
316
317 /**
318 * Returns true if bytes of string and cursor are the same, false otherwise.
319 */
320 bool aws_string_eq_byte_cursor(const struct aws_string *str, const struct aws_byte_cursor *cur) {
321 AWS_PRECONDITION(!str || aws_string_is_valid(str));
322 AWS_PRECONDITION(!cur || aws_byte_cursor_is_valid(cur));
323 if (str == NULL && cur == NULL) {
324 return true;
325 }
326 if (str == NULL || cur == NULL) {
327 return false;
328 }
329 return aws_array_eq(str->bytes, str->len, cur->ptr, cur->len);
330 }
331
332 /**
333 * Returns true if bytes of string and cursor are equivalent, using a case-insensitive comparison.
334 */
335
336 bool aws_string_eq_byte_cursor_ignore_case(const struct aws_string *str, const struct aws_byte_cursor *cur) {
337 AWS_PRECONDITION(!str || aws_string_is_valid(str));
338 AWS_PRECONDITION(!cur || aws_byte_cursor_is_valid(cur));
339 if (str == NULL && cur == NULL) {
340 return true;
341 }
342 if (str == NULL || cur == NULL) {
343 return false;
344 }
345 return aws_array_eq_ignore_case(str->bytes, str->len, cur->ptr, cur->len);
346 }
347
348 /**
349 * Returns true if bytes of string and buffer are the same, false otherwise.
350 */
351 bool aws_string_eq_byte_buf(const struct aws_string *str, const struct aws_byte_buf *buf) {
352 AWS_PRECONDITION(!str || aws_string_is_valid(str));
353 AWS_PRECONDITION(!buf || aws_byte_buf_is_valid(buf));
354 if (str == NULL && buf == NULL) {
355 return true;
356 }
357 if (str == NULL || buf == NULL) {
358 return false;
359 }
360 return aws_array_eq(str->bytes, str->len, buf->buffer, buf->len);
361 }
362
363 /**
364 * Returns true if bytes of string and buffer are equivalent, using a case-insensitive comparison.
365 */
366
367 bool aws_string_eq_byte_buf_ignore_case(const struct aws_string *str, const struct aws_byte_buf *buf) {
368 AWS_PRECONDITION(!str || aws_string_is_valid(str));
369 AWS_PRECONDITION(!buf || aws_byte_buf_is_valid(buf));
370 if (str == NULL && buf == NULL) {
371 return true;
372 }
373 if (str == NULL || buf == NULL) {
374 return false;
375 }
376 return aws_array_eq_ignore_case(str->bytes, str->len, buf->buffer, buf->len);
377 }
378
379 bool aws_string_eq_c_str(const struct aws_string *str, const char *c_str) {
380 AWS_PRECONDITION(!str || aws_string_is_valid(str));
381 if (str == NULL && c_str == NULL) {
382 return true;
383 }
384 if (str == NULL || c_str == NULL) {
385 return false;
386 }
387 return aws_array_eq_c_str(str->bytes, str->len, c_str);
388 }
389
390 /**
391 * Returns true if bytes of strings are equivalent, using a case-insensitive comparison.
392 */
393 bool aws_string_eq_c_str_ignore_case(const struct aws_string *str, const char *c_str) {
394 AWS_PRECONDITION(!str || aws_string_is_valid(str));
395 if (str == NULL && c_str == NULL) {
396 return true;
397 }
398 if (str == NULL || c_str == NULL) {
399 return false;
400 }
401 return aws_array_eq_c_str_ignore_case(str->bytes, str->len, c_str);
402 }
403
404 bool aws_byte_buf_write_from_whole_string(
405 struct aws_byte_buf *AWS_RESTRICT buf,
406 const struct aws_string *AWS_RESTRICT src) {
407 AWS_PRECONDITION(!buf || aws_byte_buf_is_valid(buf));
408 AWS_PRECONDITION(!src || aws_string_is_valid(src));
409 if (buf == NULL || src == NULL) {
410 return false;
411 }
412 return aws_byte_buf_write(buf, aws_string_bytes(src), src->len);
413 }
414
415 /**
416 * Creates an aws_byte_cursor from an existing string.
417 */
418 struct aws_byte_cursor aws_byte_cursor_from_string(const struct aws_string *src) {
419 if (!src) {
420 struct aws_byte_cursor cursor;
421 AWS_ZERO_STRUCT(cursor);
422 return cursor;
423 }
424
425 return aws_byte_cursor_from_array(aws_string_bytes(src), src->len);
426 }
427
428 struct aws_string *aws_string_clone_or_reuse(struct aws_allocator *allocator, const struct aws_string *str) {
429 AWS_PRECONDITION(allocator);
430 AWS_PRECONDITION(aws_string_is_valid(str));
431
432 if (str->allocator == NULL) {
433 /* Since the string cannot be deallocated, we assume that it will remain valid for the lifetime of the
434 * application */
435 AWS_POSTCONDITION(aws_string_is_valid(str));
436 return (struct aws_string *)str;
437 }
438
439 AWS_POSTCONDITION(aws_string_is_valid(str));
440 return aws_string_new_from_string(allocator, str);
441 }
442
443 int aws_secure_strlen(const char *str, size_t max_read_len, size_t *str_len) {
444 AWS_ERROR_PRECONDITION(str && str_len, AWS_ERROR_INVALID_ARGUMENT);
445
446 /* why not strnlen? It doesn't work everywhere as it wasn't standardized til C11, and is considered
447 * a GNU extension. This should be faster anyways. This should work for ascii and utf8.
448 * Any other character sets in use deserve what they get. */
449 char *null_char_ptr = memchr(str, '\0', max_read_len);
450
451 if (null_char_ptr) {
452 *str_len = null_char_ptr - str;
453 return AWS_OP_SUCCESS;
454 }
455
456 return aws_raise_error(AWS_ERROR_C_STRING_BUFFER_NOT_NULL_TERMINATED);
457 }