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
php_ini_builder_prepend(struct php_ini_builder * b,const char * src,size_t length)22 PHPAPI void php_ini_builder_prepend(struct php_ini_builder *b, const char *src, size_t length)
23 {
24 php_ini_builder_realloc(b, length);
25 if (b->length > 0)
26 memmove(b->value + length, b->value, b->length);
27 memcpy(b->value, src, length);
28 b->length += length;
29 }
30
php_ini_builder_unquoted(struct php_ini_builder * b,const char * name,size_t name_length,const char * value,size_t value_length)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 {
33 php_ini_builder_realloc(b, name_length + 1 + value_length + 1);
34
35 memcpy(b->value + b->length, name, name_length);
36 b->length += name_length;
37
38 b->value[b->length++] = '=';
39
40 memcpy(b->value + b->length, value, value_length);
41 b->length += value_length;
42
43 b->value[b->length++] = '\n';
44 }
45
php_ini_builder_quoted(struct php_ini_builder * b,const char * name,size_t name_length,const char * value,size_t value_length)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 {
48 php_ini_builder_realloc(b, name_length + 2 + value_length + 2);
49
50 memcpy(b->value + b->length, name, name_length);
51 b->length += name_length;
52
53 b->value[b->length++] = '=';
54 b->value[b->length++] = '"';
55
56 memcpy(b->value + b->length, value, value_length);
57 b->length += value_length;
58
59 b->value[b->length++] = '"';
60 b->value[b->length++] = '\n';
61 }
62
php_ini_builder_define(struct php_ini_builder * b,const char * arg)63 PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg)
64 {
65 const size_t len = strlen(arg);
66 const char *val = strchr(arg, '=');
67
68 if (val != NULL) {
69 val++;
70 if (!isalnum(*val) && *val != '"' && *val != '\'' && *val != '\0') {
71 php_ini_builder_quoted(b, arg, val - arg - 1, val, arg + len - val);
72 } else {
73 php_ini_builder_realloc(b, len + strlen("\n"));
74 memcpy(b->value + b->length, arg, len);
75 b->length += len;
76 b->value[b->length++] = '\n';
77 }
78 } else {
79 php_ini_builder_unquoted(b, arg, len, "1", 1);
80 }
81 }
82
83