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/>.
17 * @file reg_parse_internal.h
18 * @author Gregor Beck <gb@sernet.de>
23 #include "reg_parse_internal.h"
28 size_t iconvert_talloc(const void* ctx,
30 const char* src, size_t srclen,
34 size_t obytes, ibytes;
35 char *optr, *dst, *tmp;
38 if (cd == NULL || cd == ((smb_iconv_t)-1)) {
45 /* dstlen = 2*srclen + 2; */
47 dst = (char *)talloc_size(ctx, dstlen);
49 DEBUG(0,("iconver_talloc no mem\n"));
53 dstlen = talloc_get_size(dst);
61 ret = smb_iconv(cd, &iptr, &ibytes, &optr, &obytes);
64 const char *reason="unknown error";
67 reason="Incomplete multibyte sequence";
70 dstlen = 2*dstlen + 2;
71 tmp = talloc_realloc(ctx, dst, char, dstlen);
73 reason="talloc_realloc failed";
79 reason="Illegal multibyte sequence";
82 DEBUG(0,("Conversion error: %s(%.80s) %li\n", reason, iptr,
83 (long int)(iptr-src)));
88 dstlen = (dstlen-2) - obytes;
90 SSVAL(dst, dstlen, 0);
96 #ifndef HKEY_CURRENT_CONFIG
97 #define HKEY_CURRENT_CONFIG 0x80000005
100 #define HKEY_DYN_DATA 0x80000006
102 #ifndef HKEY_PERFORMANCE_TEXT
103 #define HKEY_PERFORMANCE_TEXT 0x80000050
105 #ifndef HKEY_PERFORMANCE_NLSTEXT
106 #define HKEY_PERFORMANCE_NLSTEXT 0x80000060
109 #define HIVE_INFO_ENTRY(SHORT,LONG) \
110 static const struct hive_info HIVE_INFO_##SHORT = { \
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;
141 if (nlen >= sizeof(buf)) {
144 for (s=0; s<nlen; s++) {
145 buf[s] = toupper(name[s]);
149 if ((s < 3) || (strncmp(buf, "HK", 2) != 0)) {
154 for(info = HIVE_INFO; *info; info++) {
155 if (strcmp(buf+2, (*info)->short_name+2) == 0) {
162 if ((s < 10) || (strncmp(buf, "HKEY_", 5)) != 0) {
166 for(info = HIVE_INFO; *info; info++) {
167 if (strcmp(buf+5, (*info)->long_name+5) == 0) {
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();
185 bool set_iconv(smb_iconv_t* t, const char* to, const char* from)
187 smb_iconv_t cd = (smb_iconv_t)-1;
190 to = get_charset(to);
191 from = get_charset(from);
192 cd = smb_iconv_open(to, from);
193 if (cd == ((smb_iconv_t)-1)) {
197 if ((*t != (smb_iconv_t)NULL) && (*t != (smb_iconv_t)-1)) {
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);
220 while(srprs_charsetinv(&pos, ",= \t\n\r", key))
228 *name = talloc_steal(mem_ctx, cbuf_gets(key, 0));
234 if (!srprs_quoted_string(ptr, val, NULL)) {
235 while(srprs_charsetinv(&pos, ", \t\n\r", val))
239 *value = talloc_steal(mem_ctx, cbuf_gets(val, 0));
247 while(srprs_char(&pos, ','))
254 #define CH_INVALID ((charset_t)-1)
255 static const struct {
256 const char* const name;
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)
272 for (i=0; BOM[i].name; i++) {
273 if (memcmp(*ptr, BOM[i].seq, BOM[i].len) == 0) {
278 if (BOM[i].name != NULL) {
279 DEBUG(0, ("Found Byte Order Mark for : %s\n", BOM[i].name));
286 *ctype = BOM[i].ctype;
296 int write_bom(FILE* file, const char* charset, charset_t ctype)
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));
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));
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);
329 case FMT_CASE_PRESERVE:
332 while(*ptr != '\0') {
333 *ptr = toupper(*ptr);
338 *ptr = toupper(*ptr);
341 while(*ptr != '\0') {
342 *ptr = tolower(*ptr);