Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024, 2025 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #ifndef MSETTINGS_INTERNAL
14 : #define MSETTINGS_INTERNAL
15 :
16 : #include "msettings.h"
17 :
18 : extern const char MALLOC_FAILED[];
19 :
20 :
21 : struct string {
22 : char *str;
23 : bool must_free;
24 : };
25 :
26 : struct msettings {
27 : // Must match EXACTLY the order of enum mparm
28 : bool dummy_start_bool;
29 : bool tls;
30 : bool autocommit;
31 : bool client_info;
32 : bool dummy_end_bool;
33 :
34 : // Must match EXACTLY the order of enum mparm
35 : long dummy_start_long;
36 : long port;
37 : long timezone;
38 : long replysize;
39 : long map_to_long_varchar;
40 : long connect_timeout;
41 : long reply_timeout;
42 : long dummy_end_long;
43 :
44 : // Must match EXACTLY the order of enum mparm
45 : struct string dummy_start_string;
46 : struct string sock;
47 : struct string sockdir;
48 : struct string cert;
49 : struct string clientkey;
50 : struct string clientcert;
51 : struct string host;
52 : struct string database;
53 : struct string tableschema;
54 : struct string table;
55 : struct string certhash;
56 : struct string user;
57 : struct string password;
58 : struct string language;
59 : struct string schema;
60 : struct string binary;
61 : struct string logfile;
62 : struct string client_application;
63 : struct string client_remark;
64 : struct string dummy_end_string;
65 :
66 : bool lang_is_mal;
67 : bool lang_is_sql;
68 : long user_generation;
69 : long password_generation;
70 : char *unix_sock_name_buffer;
71 : char certhash_digits_buffer[64 + 2 + 1]; // fit more than required plus trailing '\0'
72 : bool validated;
73 : const char* (*localizer)(const void *data, mparm parm);
74 : void *localizer_data;
75 : void *(*alloc)(void *state, void *old, size_t size); // NULL means regular realloc
76 : void *alloc_state;
77 : char error_message[256];
78 : };
79 :
80 :
81 : const char *format_error(msettings *mp, const char *fmt, ...)
82 : __attribute__((__format__(__printf__, 2, 3)));
83 :
84 : // wrappers around mp->allocator
85 :
86 : static inline void*
87 34216 : realloc_with_fallback(msettings_allocator alloc, void *alloc_state, void *old, size_t size)
88 : {
89 34216 : return alloc
90 18795 : ? alloc(alloc_state, old, size)
91 34216 : : size == 0
92 7630 : ? (free(old), NULL)
93 15421 : : realloc(old, size);
94 : }
95 :
96 : static inline void*
97 31217 : msettings_realloc(const msettings *mp, void *old, size_t size)
98 : {
99 31217 : return realloc_with_fallback(mp->alloc, mp->alloc_state, old, size);
100 : }
101 :
102 : static inline void*
103 17530 : msettings_alloc(const msettings *mp, size_t size)
104 : {
105 35059 : return msettings_realloc(mp, NULL, size);
106 : }
107 :
108 : static inline void*
109 : msettings_alloc_zeroed(const msettings *mp, size_t size)
110 : {
111 : char *data = msettings_realloc(mp, NULL, size);
112 : memset(data, 0, size);
113 : return data;
114 : }
115 :
116 : static inline void*
117 15189 : msettings_dealloc(const msettings *mp, void *data)
118 : {
119 15189 : if (data != NULL) {
120 13687 : void *p = msettings_realloc(mp, data, 0);
121 13687 : (void) p; /* in case we don't have asserts */
122 13687 : assert(p == NULL);
123 : }
124 15189 : return NULL;
125 : }
126 :
127 : static inline void*
128 16435 : msettings_strdup(const msettings *mp, const char *string)
129 : {
130 16435 : if (string == NULL)
131 : return NULL;
132 :
133 16435 : size_t size = strlen(string);
134 16435 : char *new_string = msettings_alloc(mp, size + 1);
135 16434 : if (new_string) {
136 16434 : memcpy(new_string, string, size);
137 16434 : new_string[size] = '\0';
138 : }
139 : return new_string;
140 : }
141 :
142 :
143 : static inline char* msettings_allocprintf(const msettings *mp, const char *fmt, ...)
144 : __attribute__((__format__(__printf__, 2, 3)));
145 :
146 : static inline char*
147 1095 : msettings_allocprintf(const msettings *mp, const char *fmt, ...)
148 : {
149 1095 : char *buffer = NULL;
150 1095 : va_list ap, ap2;
151 :
152 1095 : va_start(ap, fmt);
153 1095 : va_copy(ap2, ap);
154 :
155 1095 : int len = vsnprintf("", 0, fmt, ap2);
156 1095 : assert(len >= 0);
157 1095 : if (len < 0)
158 : goto end;
159 :
160 1095 : buffer = msettings_alloc(mp, len + 1);
161 1095 : if (buffer == NULL)
162 0 : goto end;
163 1095 : vsnprintf(buffer, len + 1, fmt, ap);
164 1095 : buffer[len] = '\0';
165 :
166 1095 : end:
167 1095 : va_end(ap2);
168 1095 : va_end(ap);
169 1095 : return buffer;
170 : }
171 :
172 : #endif
|