2 * Unix SMB/CIFS implementation.
4 * Registry helper routines
6 * Copyright (C) Gregor Beck 2010
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @file reg_parse_internal.h
23 * @author Gregor Beck <gb@sernet.de>
28 #include "reg_parse_internal.h"
33 size_t iconvert_talloc(const void* ctx
,
35 const char* src
, size_t srclen
,
39 size_t obytes
, ibytes
;
40 char *optr
, *dst
, *tmp
;
43 if (cd
== NULL
|| cd
== ((smb_iconv_t
)-1)) {
51 * Allocate an extra two bytes for the
55 dst
= (char *)talloc_size(ctx
, dstlen
);
57 DEBUG(0,("iconver_talloc no mem\n"));
61 dstlen
= talloc_get_size(dst
);
69 ret
= smb_iconv(cd
, &iptr
, &ibytes
, &optr
, &obytes
);
72 const char *reason
="unknown error";
75 reason
="Incomplete multibyte sequence";
78 dstlen
= 2*dstlen
+ 2;
79 tmp
= talloc_realloc(ctx
, dst
, char, dstlen
);
81 reason
="talloc_realloc failed";
87 reason
="Illegal multibyte sequence";
90 DEBUG(0,("Conversion error: %s(%.80s) %li\n", reason
, iptr
,
91 (long int)(iptr
-src
)));
96 dstlen
= (dstlen
-2) - obytes
;
98 SSVAL(dst
, dstlen
, 0);
104 #ifndef HKEY_CURRENT_CONFIG
105 #define HKEY_CURRENT_CONFIG 0x80000005
107 #ifndef HKEY_DYN_DATA
108 #define HKEY_DYN_DATA 0x80000006
110 #ifndef HKEY_PERFORMANCE_TEXT
111 #define HKEY_PERFORMANCE_TEXT 0x80000050
113 #ifndef HKEY_PERFORMANCE_NLSTEXT
114 #define HKEY_PERFORMANCE_NLSTEXT 0x80000060
117 #define HIVE_INFO_ENTRY(SHORT,LONG) \
118 static const struct hive_info HIVE_INFO_##SHORT = { \
120 .short_name = #SHORT, \
121 .short_name_len = sizeof(#SHORT)-1, \
122 .long_name = #LONG, \
123 .long_name_len = sizeof(#LONG)-1, \
126 HIVE_INFO_ENTRY(HKLM
, HKEY_LOCAL_MACHINE
);
127 HIVE_INFO_ENTRY(HKCU
, HKEY_CURRENT_USER
);
128 HIVE_INFO_ENTRY(HKCR
, HKEY_CLASSES_ROOT
);
129 HIVE_INFO_ENTRY(HKU
, HKEY_USERS
);
130 HIVE_INFO_ENTRY(HKCC
, HKEY_CURRENT_CONFIG
);
131 HIVE_INFO_ENTRY(HKDD
, HKEY_DYN_DATA
);
132 HIVE_INFO_ENTRY(HKPD
, HKEY_PERFORMANCE_DATA
);
133 HIVE_INFO_ENTRY(HKPT
, HKEY_PERFORMANCE_TEXT
);
134 HIVE_INFO_ENTRY(HKPN
, HKEY_PERFORMANCE_NLSTEXT
);
135 #undef HIVE_INFO_ENTRY
137 static const struct hive_info
* HIVE_INFO
[] = {
138 &HIVE_INFO_HKLM
, &HIVE_INFO_HKCU
, &HIVE_INFO_HKCR
, &HIVE_INFO_HKU
,
139 &HIVE_INFO_HKCC
, &HIVE_INFO_HKDD
, &HIVE_INFO_HKPD
, &HIVE_INFO_HKPT
,
140 &HIVE_INFO_HKPN
, NULL
143 const struct hive_info
* hive_info(const char* name
, int nlen
)
145 const struct hive_info
** info
;
149 if (nlen
>= sizeof(buf
)) {
152 for (s
=0; s
<nlen
; s
++) {
153 buf
[s
] = toupper(name
[s
]);
157 if ((s
< 3) || (strncmp(buf
, "HK", 2) != 0)) {
162 for(info
= HIVE_INFO
; *info
; info
++) {
163 if (strcmp(buf
+2, (*info
)->short_name
+2) == 0) {
170 if ((s
< 10) || (strncmp(buf
, "HKEY_", 5)) != 0) {
174 for(info
= HIVE_INFO
; *info
; info
++) {
175 if (strcmp(buf
+5, (*info
)->long_name
+5) == 0) {
182 const char* get_charset(const char* c
)
184 if (strcmp(c
, "dos") == 0) {
185 return lp_dos_charset();
186 } else if (strcmp(c
, "unix") == 0) {
187 return lp_unix_charset();
193 bool set_iconv(smb_iconv_t
* t
, const char* to
, const char* from
)
195 smb_iconv_t cd
= (smb_iconv_t
)-1;
198 to
= get_charset(to
);
199 from
= get_charset(from
);
200 cd
= smb_iconv_open(to
, from
);
201 if (cd
== ((smb_iconv_t
)-1)) {
205 if ((*t
!= (smb_iconv_t
)NULL
) && (*t
!= (smb_iconv_t
)-1)) {
213 * Parse option string
214 * @param[in,out] ptr parse position
215 * @param[in] mem_ctx talloc context
216 * @param[out] name ptr 2 value
217 * @param[out] value ptr 2 value
218 * @return true on success
220 bool srprs_option(const char** ptr
, const void* mem_ctx
, char** name
, char** value
)
222 const char* pos
= *ptr
;
223 void* ctx
= talloc_new(mem_ctx
);
225 cbuf
* key
= cbuf_new(ctx
);
228 while(srprs_charsetinv(&pos
, ",= \t\n\r", key
))
236 *name
= talloc_steal(mem_ctx
, cbuf_gets(key
, 0));
242 if (!srprs_quoted_string(ptr
, val
, NULL
)) {
243 while(srprs_charsetinv(&pos
, ", \t\n\r", val
))
247 *value
= talloc_steal(mem_ctx
, cbuf_gets(val
, 0));
255 while(srprs_char(&pos
, ','))
262 #define CH_INVALID ((charset_t)-1)
263 static const struct {
264 const char* const name
;
269 {"UTF-8", CH_UTF8
, 3, {0xEF, 0xBB, 0xBF}},
270 {"UTF-32LE", CH_INVALID
, 4, {0xFF, 0xFE, 0x00, 0x00}},
271 {"UTF-16LE", CH_UTF16LE
, 2, {0xFF, 0xFE}},
272 {"UTF-16BE", CH_UTF16BE
, 2, {0xFE, 0xFF}},
273 {"UTF-32BE", CH_INVALID
, 4, {0x00, 0x00, 0xFE, 0xFF}},
274 {NULL
, CH_INVALID
, 0}
277 bool srprs_bom(const char** ptr
, const char** name
, charset_t
* ctype
)
280 for (i
=0; BOM
[i
].name
; i
++) {
281 if (memcmp(*ptr
, BOM
[i
].seq
, BOM
[i
].len
) == 0) {
286 if (BOM
[i
].name
!= NULL
) {
287 DEBUG(0, ("Found Byte Order Mark for : %s\n", BOM
[i
].name
));
294 *ctype
= BOM
[i
].ctype
;
304 int write_bom(FILE* file
, const char* charset
, charset_t ctype
)
307 if ( charset
== NULL
) {
308 for (i
=0; BOM
[i
].name
; i
++) {
309 if (BOM
[i
].ctype
== ctype
) {
310 return fwrite(BOM
[i
].seq
, 1, BOM
[i
].len
, file
);
313 DEBUG(0, ("No Byte Order Mark for charset_t: %u\n", (unsigned)ctype
));
315 for (i
=0; BOM
[i
].name
; i
++) {
316 if (strcasecmp_m(BOM
[i
].name
, charset
) == 0) {
317 return fwrite(BOM
[i
].seq
, 1, BOM
[i
].len
, file
);
320 DEBUG(0, ("No Byte Order Mark for charset_t: %s\n", charset
));
326 int cbuf_puts_case(cbuf
* s
, const char* str
, size_t len
, enum fmt_case fmt
)
328 size_t pos
= cbuf_getpos(s
);
329 int ret
= cbuf_puts(s
, str
, len
);
330 char* ptr
= cbuf_gets(s
,pos
);
337 case FMT_CASE_PRESERVE
:
340 while(*ptr
!= '\0') {
341 *ptr
= toupper(*ptr
);
346 *ptr
= toupper(*ptr
);
349 while(*ptr
!= '\0') {
350 *ptr
= tolower(*ptr
);