/src/php-src/main/php_ini_builder.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Max Kellermann <max.kellermann@ionos.com> | |
14 | | +----------------------------------------------------------------------+ |
15 | | */ |
16 | | |
17 | | #include "php_ini_builder.h" |
18 | | |
19 | | #include <ctype.h> |
20 | | #include <string.h> |
21 | | |
22 | | PHPAPI void php_ini_builder_prepend(struct php_ini_builder *b, const char *src, size_t length) |
23 | 0 | { |
24 | 0 | php_ini_builder_realloc(b, length); |
25 | 0 | if (b->length > 0) |
26 | 0 | memmove(b->value + length, b->value, b->length); |
27 | 0 | memcpy(b->value, src, length); |
28 | 0 | b->length += length; |
29 | 0 | } |
30 | | |
31 | | 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) |
32 | 0 | { |
33 | 0 | php_ini_builder_realloc(b, name_length + 1 + value_length + 1); |
34 | |
|
35 | 0 | memcpy(b->value + b->length, name, name_length); |
36 | 0 | b->length += name_length; |
37 | |
|
38 | 0 | b->value[b->length++] = '='; |
39 | |
|
40 | 0 | memcpy(b->value + b->length, value, value_length); |
41 | 0 | b->length += value_length; |
42 | |
|
43 | 0 | b->value[b->length++] = '\n'; |
44 | 0 | } |
45 | | |
46 | | 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) |
47 | 0 | { |
48 | 0 | php_ini_builder_realloc(b, name_length + 2 + value_length + 2); |
49 | |
|
50 | 0 | memcpy(b->value + b->length, name, name_length); |
51 | 0 | b->length += name_length; |
52 | |
|
53 | 0 | b->value[b->length++] = '='; |
54 | 0 | b->value[b->length++] = '"'; |
55 | |
|
56 | 0 | memcpy(b->value + b->length, value, value_length); |
57 | 0 | b->length += value_length; |
58 | |
|
59 | 0 | b->value[b->length++] = '"'; |
60 | 0 | b->value[b->length++] = '\n'; |
61 | 0 | } |
62 | | |
63 | | PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg) |
64 | 0 | { |
65 | 0 | const size_t len = strlen(arg); |
66 | 0 | const char *val = strchr(arg, '='); |
67 | |
|
68 | 0 | if (val != NULL) { |
69 | 0 | val++; |
70 | 0 | if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') { |
71 | 0 | php_ini_builder_quoted(b, arg, val - arg - 1, val, arg + len - val); |
72 | 0 | } else { |
73 | 0 | php_ini_builder_realloc(b, len + strlen("\n")); |
74 | 0 | memcpy(b->value + b->length, arg, len); |
75 | 0 | b->length += len; |
76 | 0 | b->value[b->length++] = '\n'; |
77 | 0 | } |
78 | 0 | } else { |
79 | 0 | php_ini_builder_unquoted(b, arg, len, "1", 1); |
80 | 0 | } |
81 | 0 | } |
82 | | |