s3-smbd Remove manual override of DEBUGELVEL during exit
[Samba/bjacke.git] / source3 / registry / reg_parse_internal.c
blob47346623020d1e7e36a11756a9819b6e472705b9
1 /* * Samba Unix/Linux SMB client library
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 /**
17 * @file reg_parse_internal.h
18 * @author Gregor Beck <gb@sernet.de>
19 * @date Sep 2010
20 * @brief
23 #include "reg_parse_internal.h"
24 #include "cbuf.h"
25 #include "srprs.h"
26 #include "registry.h"
28 size_t iconvert_talloc(const void* ctx,
29 smb_iconv_t cd,
30 const char* src, size_t srclen,
31 char** pdst)
33 size_t dstlen, ret;
34 size_t obytes, ibytes;
35 char *optr, *dst, *tmp;
36 const char* iptr;
38 if (cd == NULL || cd == ((smb_iconv_t)-1)) {
39 return -1;
42 dst = *pdst;
44 if (dst == NULL) {
45 /* dstlen = 2*srclen + 2; */
46 dstlen = srclen;
47 dst = (char *)talloc_size(ctx, dstlen);
48 if (dst == NULL) {
49 DEBUG(0,("iconver_talloc no mem\n"));
50 return -1;
52 } else {
53 dstlen = talloc_get_size(dst);
55 convert:
56 iptr = src;
57 ibytes = srclen;
58 optr = dst;
59 obytes = dstlen-2;
61 ret = smb_iconv(cd, &iptr, &ibytes, &optr, &obytes);
63 if(ret == -1) {
64 const char *reason="unknown error";
65 switch(errno) {
66 case EINVAL:
67 reason="Incomplete multibyte sequence";
68 break;
69 case E2BIG:
70 dstlen = 2*dstlen + 2;
71 tmp = talloc_realloc(ctx, dst, char, dstlen);
72 if (tmp == NULL) {
73 reason="talloc_realloc failed";
74 break;
76 dst = tmp;
77 goto convert;
78 case EILSEQ:
79 reason="Illegal multibyte sequence";
80 break;
82 DEBUG(0,("Conversion error: %s(%.80s) %li\n", reason, iptr,
83 (long int)(iptr-src)));
84 talloc_free(dst);
85 return -1;
88 dstlen = (dstlen-2) - obytes;
90 SSVAL(dst, dstlen, 0);
92 *pdst = dst;
93 return dstlen;
96 #ifndef HKEY_CURRENT_CONFIG
97 #define HKEY_CURRENT_CONFIG 0x80000005
98 #endif
99 #ifndef HKEY_DYN_DATA
100 #define HKEY_DYN_DATA 0x80000006
101 #endif
102 #ifndef HKEY_PERFORMANCE_TEXT
103 #define HKEY_PERFORMANCE_TEXT 0x80000050
104 #endif
105 #ifndef HKEY_PERFORMANCE_NLSTEXT
106 #define HKEY_PERFORMANCE_NLSTEXT 0x80000060
107 #endif
109 #define HIVE_INFO_ENTRY(SHORT,LONG) \
110 static const struct hive_info HIVE_INFO_##SHORT = { \
111 .handle = LONG, \
112 .short_name = #SHORT, \
113 .short_name_len = sizeof(#SHORT)-1, \
114 .long_name = #LONG, \
115 .long_name_len = sizeof(#LONG)-1, \
118 HIVE_INFO_ENTRY(HKLM, HKEY_LOCAL_MACHINE);
119 HIVE_INFO_ENTRY(HKCU, HKEY_CURRENT_USER);
120 HIVE_INFO_ENTRY(HKCR, HKEY_CLASSES_ROOT);
121 HIVE_INFO_ENTRY(HKU , HKEY_USERS);
122 HIVE_INFO_ENTRY(HKCC, HKEY_CURRENT_CONFIG);
123 HIVE_INFO_ENTRY(HKDD, HKEY_DYN_DATA);
124 HIVE_INFO_ENTRY(HKPD, HKEY_PERFORMANCE_DATA);
125 HIVE_INFO_ENTRY(HKPT, HKEY_PERFORMANCE_TEXT);
126 HIVE_INFO_ENTRY(HKPN, HKEY_PERFORMANCE_NLSTEXT);
127 #undef HIVE_INFO_ENTRY
129 static const struct hive_info* HIVE_INFO[] = {
130 &HIVE_INFO_HKLM, &HIVE_INFO_HKCU, &HIVE_INFO_HKCR, &HIVE_INFO_HKU,
131 &HIVE_INFO_HKCC, &HIVE_INFO_HKDD, &HIVE_INFO_HKPD, &HIVE_INFO_HKPT,
132 &HIVE_INFO_HKPN, NULL
135 const struct hive_info* hive_info(const char* name, int nlen)
137 const struct hive_info** info;
138 char buf[32];
139 int s;
141 if (nlen >= sizeof(buf)) {
142 return NULL;
144 for (s=0; s<nlen; s++) {
145 buf[s] = toupper(name[s]);
147 buf[s] = '\0';
149 if ((s < 3) || (strncmp(buf, "HK", 2) != 0)) {
150 return NULL;
153 if (s <= 4) {
154 for(info = HIVE_INFO; *info; info++) {
155 if (strcmp(buf+2, (*info)->short_name+2) == 0) {
156 return *info;
159 return NULL;
162 if ((s < 10) || (strncmp(buf, "HKEY_", 5)) != 0) {
163 return NULL;
166 for(info = HIVE_INFO; *info; info++) {
167 if (strcmp(buf+5, (*info)->long_name+5) == 0) {
168 return *info;
171 return NULL;
174 const char* get_charset(const char* c)
176 if (strcmp(c, "dos") == 0) {
177 return lp_dos_charset();
178 } else if (strcmp(c, "unix") == 0) {
179 return lp_unix_charset();
180 } else {
181 return c;
185 bool set_iconv(smb_iconv_t* t, const char* to, const char* from)
187 smb_iconv_t cd = (smb_iconv_t)-1;
189 if (to && from) {
190 to = get_charset(to);
191 from = get_charset(from);
192 cd = smb_iconv_open(to, from);
193 if (cd == ((smb_iconv_t)-1)) {
194 return false;
197 if ((*t != (smb_iconv_t)NULL) && (*t != (smb_iconv_t)-1)) {
198 smb_iconv_close(*t);
200 *t = cd;
201 return true;
205 * Parse option string
206 * @param[in,out] ptr parse position
207 * @param[in] mem_ctx talloc context
208 * @param[out] name ptr 2 value
209 * @param[out] value ptr 2 value
210 * @return true on success
212 bool srprs_option(const char** ptr, const void* mem_ctx, char** name, char** value)
214 const char* pos = *ptr;
215 void* ctx = talloc_new(mem_ctx);
217 cbuf* key = cbuf_new(ctx);
218 cbuf* val = NULL;
220 while(srprs_charsetinv(&pos, ",= \t\n\r", key))
222 if (pos == *ptr) {
223 talloc_free(ctx);
224 return false;
227 if (name != NULL) {
228 *name = talloc_steal(mem_ctx, cbuf_gets(key, 0));
231 if (*pos == '=') {
232 val = cbuf_new(ctx);
233 pos++;
234 if (!srprs_quoted_string(ptr, val, NULL)) {
235 while(srprs_charsetinv(&pos, ", \t\n\r", val))
238 if (value != NULL) {
239 *value = talloc_steal(mem_ctx, cbuf_gets(val, 0));
241 } else {
242 if (value != NULL) {
243 *value = NULL;
247 while(srprs_char(&pos, ','))
250 *ptr = pos;
251 return true;
254 #define CH_INVALID ((charset_t)-1)
255 static const struct {
256 const char* const name;
257 charset_t ctype;
258 int len;
259 char seq[4];
260 } BOM[] = {
261 {"UTF-8", CH_UTF8, 3, {0xEF, 0xBB, 0xBF}},
262 {"UTF-32LE", CH_INVALID, 4, {0xFF, 0xFE, 0x00, 0x00}},
263 {"UTF-16LE", CH_UTF16LE, 2, {0xFF, 0xFE}},
264 {"UTF-16BE", CH_UTF16BE, 2, {0xFE, 0xFF}},
265 {"UTF-32BE", CH_INVALID, 4, {0x00, 0x00, 0xFE, 0xFF}},
266 {NULL, CH_INVALID, 0}
269 bool srprs_bom(const char** ptr, const char** name, charset_t* ctype)
271 int i;
272 for (i=0; BOM[i].name; i++) {
273 if (memcmp(*ptr, BOM[i].seq, BOM[i].len) == 0) {
274 break;
278 if (BOM[i].name != NULL) {
279 DEBUG(0, ("Found Byte Order Mark for : %s\n", BOM[i].name));
281 if (name != NULL) {
282 *name = BOM[i].name;
285 if (ctype != NULL) {
286 *ctype = BOM[i].ctype;
289 *ptr += BOM[i].len;
291 return true;
293 return false;
296 int write_bom(FILE* file, const char* charset, charset_t ctype)
298 int i;
299 if ( charset == NULL ) {
300 for (i=0; BOM[i].name; i++) {
301 if (BOM[i].ctype == ctype) {
302 return fwrite(BOM[i].seq, 1, BOM[i].len, file);
305 DEBUG(0, ("No Byte Order Mark for charset_t: %u\n", (unsigned)ctype));
306 } else {
307 for (i=0; BOM[i].name; i++) {
308 if (StrCaseCmp(BOM[i].name, charset) == 0) {
309 return fwrite(BOM[i].seq, 1, BOM[i].len, file);
312 DEBUG(0, ("No Byte Order Mark for charset_t: %s\n", charset));
314 return 0;
318 int cbuf_puts_case(cbuf* s, const char* str, size_t len, enum fmt_case fmt)
320 size_t pos = cbuf_getpos(s);
321 int ret = cbuf_puts(s, str, len);
322 char* ptr = cbuf_gets(s,pos);
324 if (ret <= 0) {
325 return ret;
328 switch (fmt) {
329 case FMT_CASE_PRESERVE:
330 break;
331 case FMT_CASE_UPPER:
332 while(*ptr != '\0') {
333 *ptr = toupper(*ptr);
334 ptr++;
336 break;
337 case FMT_CASE_TITLE:
338 *ptr = toupper(*ptr);
339 ptr++;
340 case FMT_CASE_LOWER:
341 while(*ptr != '\0') {
342 *ptr = tolower(*ptr);
343 ptr++;
346 return ret;