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