util:datablob: data_blob_pad checks its alignment assumption
[samba.git] / source3 / registry / reg_parse.c
blobb6a7f684ac546c98963cb9967e951dd3b6d7b92a
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2010
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @brief Parser for dot.reg files
22 * @file reg_parse.c
23 * @author Gregor Beck <gb@sernet.de>
24 * @date Jun 2010
28 #include "includes.h"
29 #include "system/filesys.h"
30 #include "cbuf.h"
31 #include "srprs.h"
32 #include "reg_parse_internal.h"
33 #include "reg_parse.h"
34 #include "reg_format.h"
36 #include <stdio.h>
37 #include <talloc.h>
38 #include <stdbool.h>
39 #include <string.h>
40 #include <regex.h>
41 #include <assert.h>
42 #include <stdint.h>
44 enum reg_parse_state {
45 STATE_DEFAULT,
46 STATE_KEY_OPEN,
47 STATE_VAL_HEX_CONT,
48 STATE_VAL_SZ_CONT
51 struct reg_parse {
52 struct reg_format_callback reg_format_callback;
53 cbuf* key;
54 cbuf* valname;
55 uint32_t valtype;
56 cbuf* valblob;
57 cbuf* tmp;
58 struct reg_parse_callback call;
59 int ret;
60 int linenum;
61 enum reg_parse_state state;
62 struct reg_parse_options* opt;
63 smb_iconv_t str2UTF16;
64 unsigned flags;
67 /**
68 * @defgroup action Action
69 * @{
71 static bool act_key(struct reg_parse* p, cbuf* keyname, bool del)
73 const char* name = cbuf_gets(keyname, 0);
74 cbuf_swap(p->key, keyname);
76 assert(p->state == STATE_DEFAULT || p->state == STATE_KEY_OPEN);
77 p->state = del ? STATE_DEFAULT : STATE_KEY_OPEN;
79 assert(p->call.key);
80 p->ret = p->call.key(p->call.data, &name, 1, del);
81 return p->ret >= 0;
84 static bool value_callback(struct reg_parse* p)
86 const char* name = cbuf_gets(p->valname,0);
87 const uint8_t* val = (const uint8_t*)cbuf_gets(p->valblob,0);
88 size_t len = cbuf_getpos(p->valblob);
90 assert(p->call.val);
91 p->ret = p->call.val(p->call.data, name, p->valtype, val, len);
92 return p->ret >= 0;
95 static bool act_val_hex(struct reg_parse* p, cbuf* value, bool cont)
97 cbuf_swap(p->valblob, value);
98 assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_HEX_CONT));
100 if (cont) {
101 p->state = STATE_VAL_HEX_CONT;
102 } else {
103 p->state = STATE_KEY_OPEN;
105 switch (p->valtype) {
106 case REG_EXPAND_SZ:
107 case REG_MULTI_SZ:
108 if (p->str2UTF16 != NULL) {
109 char* dst = NULL;
110 const char* src = cbuf_gets(p->valblob, 0);
111 const size_t slen = cbuf_getpos(p->valblob);
112 size_t dlen = iconvert_talloc(p,
113 p->str2UTF16,
114 src, slen,
115 &dst);
116 if (dlen != -1) {
117 cbuf_swapptr(p->valblob, &dst, dlen);
118 } else {
119 DEBUG(0, ("iconvert_talloc failed\n"));
120 return false;
122 talloc_free(dst);
124 break;
125 default:
126 break;
128 return value_callback(p);
130 return true;
133 static bool act_val_dw(struct reg_parse* p, uint32_t val)
135 assert(p->valtype == REG_DWORD);
136 assert(p->state == STATE_KEY_OPEN);
138 cbuf_clear(p->valblob);
140 if (cbuf_putdw(p->valblob, val) < 0) {
141 return false;
143 return value_callback(p);
146 static bool act_val_sz(struct reg_parse* p, cbuf* value, bool cont)
148 cbuf_swap(p->valblob, value);
150 assert(p->valtype == REG_SZ);
151 assert((p->state == STATE_KEY_OPEN) || (p->state == STATE_VAL_SZ_CONT));
153 if (cont) {
154 p->state = STATE_VAL_SZ_CONT;
155 } else {
156 char* dst = NULL;
157 size_t dlen;
158 const char* src = cbuf_gets(p->valblob, 0);
160 p->state = STATE_KEY_OPEN;
163 if (convert_string_talloc(p->valblob, CH_UNIX, CH_UTF16LE,
164 src, strlen(src)+1,
165 &dst, &dlen))
167 cbuf_swapptr(p->valblob, &dst, dlen);
168 } else {
169 DEBUG(0, ("convert_string_talloc failed: >%s<\n"
170 "use it as is\t", src));
171 return false;
173 talloc_free(dst);
175 return value_callback(p);
177 return true;
180 static bool act_val_del(struct reg_parse* p)
182 const char* name = cbuf_gets(p->valname, 0);
184 assert(p->call.val_del);
185 p->ret = p->call.val_del(p->call.data, name);
186 return p->ret >= 0;
189 static bool act_comment (struct reg_parse* p, const char* txt)
191 assert(p->call.comment);
192 p->ret = p->call.comment(p->call.data, txt);
193 return p->ret >= 0;
195 /**@}*/
198 static int nop_callback_key(void* private_data,
199 const char* key[],
200 size_t klen,
201 bool del)
203 return 0;
206 static int nop_callback_val(void* private_data,
207 const char* name,
208 uint32_t type,
209 const uint8_t* data,
210 size_t len)
212 return 0;
215 static int nop_callback_del(void* data, const char* str)
217 return 0;
220 struct reg_parse* reg_parse_new(const void* ctx,
221 struct reg_parse_callback cb,
222 const char* str_enc, unsigned flags)
224 struct reg_parse* s = talloc_zero(ctx, struct reg_parse);
225 if (s == NULL)
226 return NULL;
227 s->key = cbuf_new(s);
228 s->valname = cbuf_new(s);
229 s->valblob = cbuf_new(s);
230 s->tmp = cbuf_new(s);
231 if ( (s->tmp == NULL) || (s->valblob == NULL)
232 || (s->valname == NULL) || (s->key == NULL) )
234 goto fail;
237 s->reg_format_callback.writeline = (reg_format_callback_writeline_t)&reg_parse_line;
238 s->reg_format_callback.data = s;
240 s->valtype = 0;
241 if (cb.key == NULL) {
242 cb.key = (reg_parse_callback_key_t)&nop_callback_key;
244 if (cb.val == NULL) {
245 cb.val = (reg_parse_callback_val_t)&nop_callback_val;
247 if (cb.val_del == NULL) {
248 cb.val_del = (reg_parse_callback_val_del_t)&nop_callback_del;
250 if (cb.comment == NULL) {
251 cb.comment =
252 (reg_parse_callback_comment_t)&nop_callback_del;
255 s->call = cb;
256 s->linenum = 0;
257 s->state = STATE_DEFAULT;
258 s->flags = flags;
260 if (str_enc && !set_iconv(&s->str2UTF16, "UTF-16LE", str_enc)) {
261 DEBUG(0, ("reg_parse_new: failed to set encoding: %s\n",
262 str_enc));
263 goto fail;
266 assert(&s->reg_format_callback == (struct reg_format_callback*)s);
267 return s;
268 fail:
269 set_iconv(&s->str2UTF16, NULL, NULL);
270 talloc_free(s);
271 return NULL;
275 * @defgroup parse Parser Primitive
276 * @ingroup internal
277 * @{
281 static bool srprs_key(const char** ptr, cbuf* key, bool* del)
283 const char* pos = *ptr;
284 const char* closing_bracket_pos = NULL;
285 size_t closing_bracket_idx = 0;
287 if (!srprs_skipws(&pos) || !srprs_char(&pos, '[')) {
288 return false;
291 *del = srprs_char(&pos, '-');
293 cbuf_clear(key);
295 while (true) {
296 while (srprs_charsetinv(&pos, "]\\", key))
299 switch (*pos) {
301 case ']':
302 closing_bracket_idx = cbuf_getpos(key);
303 closing_bracket_pos = pos;
304 cbuf_putc(key, ']');
305 pos++;
306 break;
308 case '\\':
309 cbuf_putc(key, '\\');
310 /* n++; */
311 /* cbuf_puts(subkeyidx, cbuf_getpos(key), sizeof(size_t)) */
312 while (srprs_char(&pos,'\\'))
314 break;
316 case '\0':
317 if (closing_bracket_pos == NULL) {
318 return false;
321 /* remove trailing backslash (if any) */
322 if (*(closing_bracket_pos-1)=='\\') {
323 closing_bracket_idx--;
326 cbuf_setpos(key, closing_bracket_idx);
327 *ptr = closing_bracket_pos+1;
328 return true;
330 default:
331 assert(false);
336 static bool srprs_val_name(const char** ptr, cbuf* name)
338 const char* pos = *ptr;
339 const size_t spos = cbuf_getpos(name);
341 if ( !srprs_skipws(&pos) ) {
342 goto fail;
345 if ( srprs_char(&pos, '@') ) {
346 cbuf_puts(name, "", -1);
348 else if (!srprs_quoted_string(&pos, name, NULL)) {
349 goto fail;
352 if (!srprs_skipws(&pos) || !srprs_char(&pos, '=')) {
353 goto fail;
356 *ptr = pos;
357 return true;
359 fail:
360 cbuf_setpos(name, spos);
361 return false;
364 static bool srprs_val_dword(const char** ptr, uint32_t* type, uint32_t* val)
366 const char* pos = *ptr;
368 if (!srprs_str(&pos, "dword:", -1)) {
369 return false;
372 if (!srprs_hex(&pos, 8, val)) {
373 return false;
376 *type = REG_DWORD;
377 *ptr = pos;
378 return true;
381 static bool srprs_val_sz(const char** ptr, uint32_t* type, cbuf* val, bool* cont)
383 if (!srprs_quoted_string(ptr, val, cont)) {
384 return false;
387 *type = REG_SZ;
388 return true;
392 static bool srprs_nl_no_eos(const char** ptr, cbuf* str, bool eof)
394 const char* pos = *ptr;
395 const size_t spos = cbuf_getpos(str);
397 if( srprs_nl(&pos, str) && (eof || *pos != '\0')) {
398 *ptr = pos;
399 return true;
401 cbuf_setpos(str, spos);
402 return false;
406 static bool srprs_eol_cont(const char** ptr, bool* cont)
408 const char* pos = *ptr;
409 bool bs = srprs_char(&pos, '\\');
411 if (!srprs_eol(&pos, NULL)) {
412 return false;
415 *cont = bs;
416 *ptr = pos;
417 return true;
420 /* matches the empty string, for zero length lists */
421 static bool srprs_val_hex_values(const char** ptr, cbuf* val, bool* cont)
423 const char* pos = *ptr;
424 unsigned u;
426 do {
427 if (!srprs_skipws(&pos) || !srprs_hex(&pos, 2, &u) || !srprs_skipws(&pos)) {
428 break;
430 cbuf_putc(val, (char)u);
431 } while(srprs_char(&pos, ','));
433 *ptr = pos;
435 if (srprs_skipws(&pos) && srprs_eol_cont(&pos, cont)) {
436 *ptr = pos;
439 return true;
442 static bool srprs_val_hex(const char** ptr, uint32_t* ptype, cbuf* val,
443 bool* cont)
445 const char* pos = *ptr;
446 uint32_t type;
448 if (!srprs_str(&pos, "hex", -1)) {
449 return false;
452 if (srprs_char(&pos, ':')) {
453 type = REG_BINARY;
455 else if (!srprs_char(&pos, '(') ||
456 !srprs_hex(&pos, 8, &type) ||
457 !srprs_char(&pos,')') ||
458 !srprs_char(&pos, ':'))
460 return false;
463 if (!srprs_val_hex_values(&pos, val, cont)) {
464 return false;
467 *ptype = type;
468 *ptr = pos;
469 return true;
473 static bool srprs_comment(const char** ptr, cbuf* str)
475 return srprs_char(ptr, ';') && srprs_line(ptr, str);
478 /**@}*/
480 int reg_parse_set_options(struct reg_parse* parser, const char* options)
482 static const char* DEFAULT ="enc=unix,flags=0";
484 int ret = 0;
485 char *key, *val;
486 void* ctx = talloc_new(parser);
488 if (options == NULL) {
489 options = DEFAULT;
492 while (srprs_option(&options, ctx, &key, &val)) {
493 if ((strcmp(key, "enc") == 0) || (strcmp(key, "strenc") == 0)) {
494 } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
495 char* end = NULL;
496 if (val != NULL) {
497 parser->flags = strtol(val, &end, 0);
499 if ((end==NULL) || (*end != '\0')) {
500 DEBUG(0, ("Invalid flags format: %s\n",
501 val ? val : "<NULL>"));
502 ret = -1;
505 /* else if (strcmp(key, "hive") == 0) { */
506 /* if (strcmp(val, "short") == 0) { */
507 /* f->hive_fmt = REG_FMT_SHORT_HIVES; */
508 /* } else if (strcmp(val, "long") == 0) { */
509 /* f->hive_fmt = REG_FMT_LONG_HIVES; */
510 /* } else if (strcmp(val, "preserve") == 0) { */
511 /* f->hive_fmt = REG_FMT_PRESERVE_HIVES; */
512 /* } else { */
513 /* DEBUG(0, ("Invalid hive format: %s\n", val)); */
514 /* ret = -1; */
515 /* } */
516 /* } */
518 talloc_free(ctx);
519 return ret;
523 int reg_parse_line(struct reg_parse* parser, const char* line)
525 const char* pos;
526 bool del=false;
527 cbuf* tmp=cbuf_clear(parser->tmp);
528 bool cb_ok = true;
529 bool cont = true;
531 if (!line) {
532 return -4;
535 parser->linenum++;
536 pos = line;
538 switch (parser->state) {
539 case STATE_VAL_HEX_CONT:
540 if (srprs_val_hex_values(&pos, parser->valblob, &cont)) {
541 cb_ok = act_val_hex(parser, parser->valblob, cont);
543 goto done;
544 case STATE_VAL_SZ_CONT:
545 if (srprs_quoted_string(&pos, parser->valblob, &cont)) {
546 cb_ok = act_val_sz(parser, parser->valblob, cont);
548 goto done;
549 default:
550 cont = false;
553 if ( !srprs_skipws(&pos) ) {
554 return -4;
557 /* empty line ?*/
558 if ( srprs_eol(&pos, NULL) ) {
559 return 0;
562 /* key line ?*/
563 else if (srprs_key(&pos, tmp, &del)) {
564 cb_ok = act_key(parser, tmp, del);
567 /* comment line ? */
568 else if (srprs_comment(&pos, tmp)) {
569 cb_ok = act_comment(parser, cbuf_gets(tmp, 0));
572 /* head line */
573 else if ((parser->linenum == 1) && srprs_line(&pos, tmp) ) {
574 /* cb_ok = act_head(parser, cbuf_gets(tmp, 0)); */
577 /* value line ?*/
578 else if (srprs_val_name(&pos, tmp)) {
579 uint32_t dw;
580 cbuf_swap(parser->valname, tmp);
581 cbuf_clear(tmp);
583 if (parser->state != STATE_KEY_OPEN) {
584 DEBUG(0, ("value \"%s\" without a key at line: %i\n",
585 cbuf_gets(parser->valname, 0), parser->linenum));
586 return -3;
589 if (!srprs_skipws(&pos)) {
590 return -4;
593 if (srprs_char(&pos, '-')) {
594 cb_ok = act_val_del(parser);
596 else if (srprs_val_dword(&pos, &parser->valtype, &dw)) {
597 cb_ok = act_val_dw(parser, dw);
599 else if (srprs_val_sz(&pos, &parser->valtype, tmp, &cont)) {
600 cb_ok = act_val_sz(parser, tmp, cont);
602 else if (srprs_val_hex(&pos, &parser->valtype, tmp, &cont)){
603 cb_ok = act_val_hex(parser, tmp, cont);
605 else {
606 DEBUG(0, ("value \"%s\" parse error"
607 "at line: %i pos: %li : %s\n",
608 cbuf_gets(parser->valname, 0), parser->linenum,
609 (long int)(pos-line), pos));
610 return -3;
613 else {
614 DEBUG(0, ("unrecognized line %i : %s\n", parser->linenum, line));
615 return -3;
618 done:
619 if (!cb_ok)
620 return -2;
622 if (!srprs_skipws(&pos) || !srprs_eol(&pos, NULL)) {
623 DEBUG(0, ("trailing garbage at line: %i pos: %li : %s\n",
624 parser->linenum, (long int)(pos-line), pos));
625 return -1;
627 return 0;
630 /******************************************************************************/
632 * @addtogroup misc
633 * @{
635 static bool lookslike_utf16(const char* line, size_t len, bool* little_endian)
637 static const uint16_t M_LE = 0xFF80;
638 static const uint16_t M_BE = 0x80FF;
639 uint16_t mask;
640 bool le;
642 size_t l = MIN(len/2, 64);
643 const uint16_t* u = (const uint16_t*)line;
644 size_t i;
646 assert(len >= 2);
648 if ( u[0] & M_LE ) {
649 le = true;
650 mask = M_LE;
651 } else if ( u[0] & M_BE ) {
652 le = false;
653 mask = M_BE;
654 } else {
655 return false;
658 for (i=1; i<l; i++) {
659 if ( u[i] & mask ) {
660 return false;
664 *little_endian = le;
665 return true;
668 static bool lookslike_dos(const char* line, size_t len)
670 size_t i;
671 for (i=0; i<len; i++) {
672 if ( (line[i] == '\0') || (line[i] & 0x80) ) {
673 return false;
675 if ( (line[i] == '\r') && (i+1 < len) && (line[i+1] == '\n') ) {
676 return true;
679 return false;
682 static bool guess_charset(const char** ptr,
683 size_t* len,
684 const char** file_enc,
685 const char** str_enc)
687 const char* charset = NULL;
688 const char* pos = *ptr;
690 if (*len < 4) {
691 return false;
694 if (srprs_bom(&pos, &charset, NULL)) {
695 size_t declen;
696 if (pos < *ptr) {
697 return false;
699 declen = (pos - *ptr);
700 if (*len < declen) {
701 return false;
703 *len -= declen;
704 *ptr = pos;
705 if (*file_enc == NULL) {
706 *file_enc = charset;
708 else if( strcmp(*file_enc, charset) != 0 ) {
709 DEBUG(0, ("file encoding forced to %s\n",
710 *file_enc));
713 else if (*file_enc == NULL) {
714 bool le;
715 if (lookslike_utf16(*ptr, *len, &le)) {
716 *file_enc = le ? "UTF-16LE" : "UTF-16BE";
718 else if (lookslike_dos(*ptr, *len)) {
719 *file_enc = "dos";
721 else {
722 *file_enc = "unix";
726 if ((str_enc != NULL) && (*str_enc == NULL)) {
727 *str_enc = ( strncmp(*ptr, "REGEDIT4", 8) == 0)
728 ? *file_enc
729 : "UTF-16LE";
732 return true;
734 /**@}*/
736 struct reg_parse_fd_opt {
737 const char* file_enc;
738 const char* str_enc;
739 unsigned flags;
740 int fail_level;
743 static struct reg_parse_fd_opt
744 reg_parse_fd_opt(void* mem_ctx, const char* options)
746 struct reg_parse_fd_opt ret = {
747 .file_enc = NULL,
748 .str_enc = NULL,
749 .flags = 0,
752 void* ctx = talloc_new(mem_ctx);
753 char *key, *val;
755 if (options == NULL) {
756 goto done;
759 while (srprs_option(&options, ctx, &key, &val)) {
760 if (strcmp(key, "enc") == 0) {
761 ret.file_enc = talloc_steal(mem_ctx, val);
762 ret.str_enc = ret.file_enc;
763 } else if (strcmp(key, "strenc") == 0) {
764 ret.str_enc = talloc_steal(mem_ctx, val);
765 } else if (strcmp(key, "fileenc") == 0) {
766 ret.file_enc = talloc_steal(mem_ctx, val);
767 } else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
768 char* end = NULL;
769 if (val != NULL) {
770 ret.flags = strtol(val, &end, 0);
772 if ((end==NULL) || (*end != '\0')) {
773 DEBUG(0, ("Invalid format \"%s\": %s\n",
774 key, val ? val : "<NULL>"));
776 } else if ((strcmp(key, "fail") == 0) && (val != NULL)) {
777 char* end = NULL;
778 if (val != NULL) {
779 ret.fail_level = -strtol(val, &end, 0);
781 if ((end==NULL) || (*end != '\0')) {
782 DEBUG(0, ("Invalid format \"%s\": %s\n",
783 key, val ? val : "<NULL>"));
787 done:
788 talloc_free(ctx);
789 return ret;
792 static void display_iconv_error_bytes(const char *inbuf, size_t len)
794 size_t i;
795 for (i = 0; i < 4 && i < len; i++) {
796 DEBUGADD(0, ("<%02x>", (unsigned char)inbuf[i]));
798 DEBUGADD(0, ("\n"));
801 int reg_parse_fd(int fd, const struct reg_parse_callback* cb, const char* opts)
803 void* mem_ctx = talloc_stackframe();
804 cbuf* line = cbuf_new(mem_ctx);
805 smb_iconv_t cd = (smb_iconv_t)-1;
806 struct reg_parse* parser = NULL;
807 char buf_in[1024];
808 char buf_out[1025] = { 0 };
809 ssize_t nread;
810 const char* iptr;
811 char* optr;
812 size_t ilen;
813 size_t olen;
814 size_t avail_osize = sizeof(buf_out)-1;
815 size_t space_to_read = sizeof(buf_in);
816 int ret = -1;
817 bool eof = false;
818 size_t linecount = 0;
820 struct reg_parse_fd_opt opt = reg_parse_fd_opt(mem_ctx, opts);
822 if (cb == NULL) {
823 DBG_ERR("NULL callback\n");
824 ret = -1;
825 goto done;
828 nread = read(fd, buf_in, space_to_read);
829 if (nread < 0) {
830 DBG_ERR("read failed: %s\n", strerror(errno));
831 ret = -1;
832 goto done;
834 if (nread == 0) {
835 /* Empty file. */
836 eof = true;
837 goto done;
840 iptr = buf_in;
841 ilen = nread;
843 if (!guess_charset(&iptr, &ilen,
844 &opt.file_enc, &opt.str_enc))
846 DBG_ERR("reg_parse_fd: failed to guess encoding\n");
847 ret = -1;
848 goto done;
851 if (ilen == 0) {
852 /* File only contained charset info. */
853 eof = true;
854 ret = -1;
855 goto done;
858 DBG_DEBUG("reg_parse_fd: encoding file: %s str: %s\n",
859 opt.file_enc, opt.str_enc);
862 if (!set_iconv(&cd, "unix", opt.file_enc)) {
863 DBG_ERR("reg_parse_fd: failed to set file encoding %s\n",
864 opt.file_enc);
865 ret = -1;
866 goto done;
869 parser = reg_parse_new(mem_ctx, *cb, opt.str_enc, opt.flags);
870 if (parser == NULL) {
871 ret = -1;
872 goto done;
875 /* Move input data to start of buf_in. */
876 if (iptr > buf_in) {
877 memmove(buf_in, iptr, ilen);
878 iptr = buf_in;
881 optr = buf_out;
882 /* Leave last byte for null termination. */
883 olen = avail_osize;
886 * We read from buf_in (iptr), iconv converting into
887 * buf_out (optr).
890 while (!eof) {
891 const char *pos;
892 size_t nconv;
894 if (olen == 0) {
895 /* We're out of possible room. */
896 DBG_ERR("no room in output buffer\n");
897 ret = -1;
898 goto done;
900 nconv = smb_iconv(cd, &iptr, &ilen, &optr, &olen);
901 if (nconv == (size_t)-1) {
902 bool valid_err = false;
903 if (errno == EINVAL) {
904 valid_err = true;
906 if (errno == E2BIG) {
907 valid_err = true;
909 if (!valid_err) {
910 DBG_ERR("smb_iconv error in file at line %zu: ",
911 linecount);
912 display_iconv_error_bytes(iptr, ilen);
913 ret = -1;
914 goto done;
917 * For valid errors process the
918 * existing buffer then continue.
923 * We know this is safe as we have an extra
924 * enforced zero byte at the end of buf_out.
926 *optr = '\0';
927 pos = buf_out;
929 while (srprs_line(&pos, line) && srprs_nl_no_eos(&pos, line, eof)) {
930 int retval;
932 /* Process all lines we got. */
933 retval = reg_parse_line(parser, cbuf_gets(line, 0));
934 if (retval < opt.fail_level) {
935 DBG_ERR("reg_parse_line %zu fail %d\n",
936 linecount,
937 retval);
938 ret = -1;
939 goto done;
941 cbuf_clear(line);
942 linecount++;
944 if (pos > buf_out) {
946 * The output data we have
947 * processed starts at buf_out
948 * and ends at pos.
949 * The unprocessed output
950 * data starts at pos and
951 * ends at optr.
953 * <------ sizeof(buf_out) - 1------------->|0|
954 * <--------- avail_osize------------------>|0|
955 * +----------------------+-------+-----------+
956 * | | | |0|
957 * +----------------------+-------+-----------+
958 * ^ ^ ^
959 * | | |
960 * buf_out pos optr
962 size_t unprocessed_len;
964 /* Paranoia checks. */
965 if (optr < pos) {
966 ret = -1;
967 goto done;
969 unprocessed_len = optr - pos;
971 /* Paranoia checks. */
972 if (avail_osize < unprocessed_len) {
973 ret = -1;
974 goto done;
976 /* Move down any unprocessed data. */
977 memmove(buf_out, pos, unprocessed_len);
980 * After the move, reset the output length.
982 * <------ sizeof(buf_out) - 1------------->|0|
983 * <--------- avail_osize------------------>|0|
984 * +----------------------+-------+-----------+
985 * | | |0|
986 * +----------------------+-------+-----------+
987 * ^ ^
988 * | optr
989 * buf_out
991 optr = buf_out + unprocessed_len;
993 * Calculate the new output space available
994 * for iconv.
995 * We already did the paranoia check for this
996 * arithmetic above.
998 olen = avail_osize - unprocessed_len;
1002 * Move any unprocessed data to the start of
1003 * the input buffer (buf_in).
1005 if (ilen > 0 && iptr > buf_in) {
1006 memmove(buf_in, iptr, ilen);
1009 /* Is there any space to read more input ? */
1010 if (ilen >= sizeof(buf_in)) {
1011 /* No space. Nothing was converted. Error. */
1012 DBG_ERR("no space in input buffer\n");
1013 ret = -1;
1014 goto done;
1017 space_to_read = sizeof(buf_in) - ilen;
1019 /* Read the next chunk from the file. */
1020 nread = read(fd, buf_in + ilen, space_to_read);
1021 if (nread < 0) {
1022 DBG_ERR("read failed: %s\n", strerror(errno));
1023 ret = -1;
1024 goto done;
1026 if (nread == 0) {
1027 /* Empty file. */
1028 eof = true;
1029 continue;
1032 /* Paranoia check. */
1033 if (nread + ilen < ilen) {
1034 ret = -1;
1035 goto done;
1038 /* Paranoia check. */
1039 if (nread + ilen > sizeof(buf_in)) {
1040 ret = -1;
1041 goto done;
1044 iptr = buf_in;
1045 ilen = nread + ilen;
1048 ret = 0;
1050 done:
1052 set_iconv(&cd, NULL, NULL);
1053 if (parser) {
1054 set_iconv(&parser->str2UTF16, NULL, NULL);
1056 talloc_free(mem_ctx);
1057 return ret;
1060 int reg_parse_file(const char* fname, const struct reg_parse_callback* cb,
1061 const char* opt)
1063 int ret = -1;
1064 int fd;
1066 fd = open(fname, O_RDONLY);
1067 if (fd < 0) {
1068 DEBUG(0, ("reg_parse_file: open %s failed: %s\n", fname,
1069 strerror(errno)));
1070 return -1;
1073 ret = reg_parse_fd(fd, cb, opt);
1075 close(fd);
1076 return ret;
1079 /* static struct registry_key *find_regkey_by_hnd(pipes_struct *p, */
1080 /* struct policy_handle *hnd) */
1081 /* { */
1082 /* struct registry_key *regkey = NULL; */
1084 /* if(!find_policy_by_hnd(p,hnd,(void **)(void *)&regkey)) { */
1085 /* DEBUG(2,("find_regkey_index_by_hnd: Registry Key not found: ")); */
1086 /* return NULL; */
1087 /* } */
1089 /* return regkey; */
1090 /* } */