/src/php-src/main/php_ini_builder.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Max Kellermann <max.kellermann@ionos.com> | |
12 | | +----------------------------------------------------------------------+ |
13 | | */ |
14 | | |
15 | | #include "php_ini_builder.h" |
16 | | |
17 | | #include <ctype.h> |
18 | | #include <string.h> |
19 | | |
20 | | PHPAPI void php_ini_builder_prepend(struct php_ini_builder *b, const char *src, size_t length) |
21 | 0 | { |
22 | 0 | php_ini_builder_realloc(b, length); |
23 | 0 | if (b->length > 0) |
24 | 0 | memmove(b->value + length, b->value, b->length); |
25 | 0 | memcpy(b->value, src, length); |
26 | 0 | b->length += length; |
27 | 0 | } |
28 | | |
29 | | PHPAPI void php_ini_builder_unquoted(struct php_ini_builder *b, const char *name, size_t name_length, const char *value, size_t value_length) |
30 | 0 | { |
31 | 0 | php_ini_builder_realloc(b, name_length + 1 + value_length + 1); |
32 | |
|
33 | 0 | memcpy(b->value + b->length, name, name_length); |
34 | 0 | b->length += name_length; |
35 | |
|
36 | 0 | b->value[b->length++] = '='; |
37 | |
|
38 | 0 | memcpy(b->value + b->length, value, value_length); |
39 | 0 | b->length += value_length; |
40 | |
|
41 | 0 | b->value[b->length++] = '\n'; |
42 | 0 | } |
43 | | |
44 | | PHPAPI void php_ini_builder_quoted(struct php_ini_builder *b, const char *name, size_t name_length, const char *value, size_t value_length) |
45 | 0 | { |
46 | 0 | php_ini_builder_realloc(b, name_length + 2 + value_length + 2); |
47 | |
|
48 | 0 | memcpy(b->value + b->length, name, name_length); |
49 | 0 | b->length += name_length; |
50 | |
|
51 | 0 | b->value[b->length++] = '='; |
52 | 0 | b->value[b->length++] = '"'; |
53 | |
|
54 | 0 | memcpy(b->value + b->length, value, value_length); |
55 | 0 | b->length += value_length; |
56 | |
|
57 | 0 | b->value[b->length++] = '"'; |
58 | 0 | b->value[b->length++] = '\n'; |
59 | 0 | } |
60 | | |
61 | | PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg) |
62 | 0 | { |
63 | 0 | const size_t len = strlen(arg); |
64 | 0 | const char *val = strchr(arg, '='); |
65 | |
|
66 | 0 | if (val != NULL) { |
67 | 0 | val++; |
68 | 0 | if (!isalnum((unsigned char)*val) && *val != '"' && *val != '\'' && *val != '\0') { |
69 | 0 | php_ini_builder_quoted(b, arg, val - arg - 1, val, arg + len - val); |
70 | 0 | } else { |
71 | 0 | php_ini_builder_realloc(b, len + strlen("\n")); |
72 | 0 | memcpy(b->value + b->length, arg, len); |
73 | 0 | b->length += len; |
74 | 0 | b->value[b->length++] = '\n'; |
75 | 0 | } |
76 | 0 | } else { |
77 | 0 | php_ini_builder_unquoted(b, arg, len, "1", 1); |
78 | 0 | } |
79 | 0 | } |
80 | | |