summaryrefslogtreecommitdiff
path: root/source3/registry/reg_format.c
blob: 946380d676a6c190f11e6d1852318c2c3d7f3370 (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
 * Samba Unix/Linux SMB client library
 *
 * Copyright (C) Gregor Beck 2010
 *
 * 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/>.
 */

/**
 * @brief  Format dot.reg files
 * @file   reg_format.c
 * @author Gregor Beck <gb@sernet.de>
 * @date   Sep 2010
 */

#include "reg_format.h"
#include "reg_parse.h"
#include "reg_parse_internal.h"
#include "cbuf.h"
#include "srprs.h"
#include "registry.h"
#include "registry/reg_objects.h"
#include <assert.h>

static void cstr_unescape(char* val)
{
	all_string_sub(val, "\\r", "\r", 0);
	all_string_sub(val, "\\n", "\n", 0);
	all_string_sub(val, "\\t", "\t", 0);
	all_string_sub(val, "\\\\", "\\", 0);
}

/******************************************************************************/

/**
 * Print value assign to stream.
 *
 * @param[out] ost outstream
 * @param[in]  name string
 *
 * @return numner of bytes written, -1 on error
 * @see srprs_val_name
 */
static int cbuf_print_value_assign(cbuf* ost, const char* name) {
	int ret, n;
	if (*name == '\0') {
		ret = cbuf_putc(ost, '@');
	} else {
		ret = cbuf_print_quoted_string(ost, name);
	}

	if (ret<0) {
		return ret;
	}

	n = cbuf_putc(ost, '=');
	if (n < 0) {
		return n;
	}
	ret += n;

	return ret;
}

enum fmt_hive {
	FMT_HIVE_PRESERVE=0,
	FMT_HIVE_SHORT,
	FMT_HIVE_LONG
};


struct fmt_key {
	enum fmt_hive hive_fmt;
	enum fmt_case hive_case;
	enum fmt_case key_case;
	const char*   sep;
};


static int
cbuf_print_hive(cbuf* ost, const char* hive, int len, const struct fmt_key* fmt)
{
	if (fmt->hive_fmt != FMT_HIVE_PRESERVE) {
		const struct hive_info* hinfo = hive_info(hive, len);
		if (hinfo == NULL) {
			DEBUG(0, ("Unknown hive %*s", len, hive));
		} else {
			switch(fmt->hive_fmt) {
			case FMT_HIVE_SHORT:
				hive = hinfo->short_name;
				len  = hinfo->short_name_len;
				break;
			case FMT_HIVE_LONG:
				hive = hinfo->long_name;
				len  = hinfo->long_name_len;
				break;
			default:
				DEBUG(0, ("Unsupported hive format %d",
					  (int)fmt->hive_fmt));
				return -1;
			}
		}
	}

	return cbuf_puts_case(ost, hive, len, fmt->hive_case);
}

static int
cbuf_print_keyname(cbuf* ost, const char* key[], int n, const struct fmt_key* fmt)
{
	int r, ret=0;
	size_t pos = cbuf_getpos(ost);
	bool hive = true;

	for (; n>0; key++, n--) {
		const char* start = *key;
		while(*start != '\0') {
			const char* end = start;
			while(*end != '\\' && *end != '\0') {
				end++;
			}

			if (hive) {
				r = cbuf_print_hive(ost, start, end-start, fmt);
				if (r < 0) {
					goto fail;
				}

				ret += r;
				hive = false;
			} else {
				r = cbuf_puts(ost, fmt->sep, -1);
				if (r < 0) {
					goto fail;
				}
				ret += r;

				r = cbuf_puts_case(ost, start, end-start, fmt->key_case);
				if (r < 0) {
					goto fail;
				}
				ret += r;
			}

			while(*end == '\\') {
				end++;
			}
			start = end;
		}
	}
	return ret;
fail:
	cbuf_setpos(ost, pos);
	return r;
}
/**@}*/

/**
 * @defgroup reg_format Format dot.reg file.
 * @{
 */

struct reg_format
{
	struct reg_parse_callback reg_parse_callback;
	struct reg_format_callback call;
	unsigned flags;
	smb_iconv_t fromUTF16;
	const char* sep;
};

int reg_format_value_delete(struct reg_format* f, const char* name)
{
	int ret;
	cbuf* line = cbuf_new(f);

	ret = cbuf_print_value_assign(line, name);
	if (ret < 0) {
		goto done;
	}

	ret = cbuf_putc(line, '-');
	if (ret < 0 ) {
		goto done;
	}

	ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
done:
	talloc_free(line);
	return ret;
}

/* Todo: write hex if str contains CR or LF */
static int
reg_format_value_sz(struct reg_format* f, const char* name, const char* str)
{
	int ret;
	cbuf* line = cbuf_new(f);

	ret = cbuf_print_value_assign(line, name);
	if (ret < 0) {
		goto done;
	}

	ret = cbuf_print_quoted_string(line, str);
	if (ret < 0) {
		goto done;
	}

	ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));

done:
	talloc_free(line);
	return ret;
}

static int reg_format_value_dw(struct reg_format* f, const char* name, uint32_t dw)
{
	int ret;
	cbuf* line = cbuf_new(f);

	ret = cbuf_print_value_assign(line, name);
	if (ret < 0) {
		goto done;
	}

	ret = cbuf_printf(line, "dword:%08x", dw);
	if (ret < 0) {
		goto done;
	}

	ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
done:
	talloc_free(line);
	return ret;
}

static int reg_format_value_hex(struct reg_format* f, const char* name, uint32_t type,
				const void* data, size_t len)
{
	int n;
	int cpl=0;
	int ret=0;
	const unsigned char* ptr;

	cbuf* line = cbuf_new(f);

	n = cbuf_print_value_assign(line, name);
	if (n < 0) {
		ret = n;
		goto done;
	}

	cpl += n;

	if (type==REG_BINARY && !(f->flags & REG_FMT_HEX_BIN)) {
		n=cbuf_puts(line, "hex:", -1);
	} else {
		n=cbuf_printf(line, "hex(%x):", type);
	}
	if (n < 0) {
		ret = n;
		goto done;
	}

	cpl += n;

	for (ptr=data; len>1; len--,ptr++) {
		n = cbuf_printf(line, "%02x,", (unsigned)(*ptr));
		if (n < 0) {
			return n;
		}
		cpl += n;

		if ( cpl > 76 ) {
			n = cbuf_putc(line, '\\');
			if (n< 0) {
				return n;
			}

			n = f->call.writeline(f->call.data, cbuf_gets(line,0));
			if (n < 0) {
				ret = n;
				goto done;
			}
			ret += n;

			cbuf_clear(line);
			cpl = cbuf_puts(line, "  ", -1);
			if (cpl < 0) {
				ret = cpl;
				goto done;
			}
		}
	}

	if ( len > 0 ) {
		n = cbuf_printf(line, "%02x", (unsigned)(*ptr));
		if (n < 0) {
			ret = n;
			goto done;
		}
		cpl += n;
	}

	n = f->call.writeline(f->call.data, cbuf_gets(line,0));
	if (n < 0) {
		ret = n;
		goto done;
	}
	ret += n;
done:
	talloc_free(line);
	return ret;
}

int reg_format_value(struct reg_format* f, const char* name, uint32_t type,
		     const uint8_t* data, size_t len)
{
	int ret = 0;
	void* mem_ctx = talloc_new(f);

	switch (type) {
	case REG_SZ:
		if (!(f->flags & REG_FMT_HEX_SZ)) {
			char* str = NULL;
			size_t dlen;
			if (pull_ucs2_talloc(mem_ctx, &str, (const smb_ucs2_t*)data, &dlen)) {
				ret = reg_format_value_sz(f, name, str);
				goto done;
			} else {
				DEBUG(0, ("reg_format_value %s: "
					  "pull_ucs2_talloc failed"
					  ", try to write hex\n", name));
			}
		}
		break;

	case REG_DWORD:
		if (!(f->flags & REG_FMT_HEX_SZ) && (len == sizeof(uint32_t))) {
			uint32_t dw = IVAL(data,0);
			ret = reg_format_value_dw(f, name, dw);
			goto done;
		}
		break;

	case REG_MULTI_SZ:
	case REG_EXPAND_SZ:
		if (f->fromUTF16 && (f->fromUTF16 != ((smb_iconv_t)-1))) {
			char* str = NULL;
			size_t dlen = iconvert_talloc(mem_ctx, f->fromUTF16,
						      (const char*)data, len, &str);
			if (dlen != -1) {
				ret = reg_format_value_hex(f, name, type, str, dlen);
				goto done;
			} else {
				DEBUG(0, ("reg_format_value %s: "
					  "iconvert_talloc failed"
					  ", try to write hex\n", name));
			}
		}
		break;
	default:
		break;
	}

	ret = reg_format_value_hex(f, name, type, data, len);
done:
	talloc_free(mem_ctx);
	return ret;
}


int reg_format_comment(struct reg_format* f, const char* txt)
{
	int ret;
	cbuf* line = cbuf_new(f);

	ret = cbuf_putc(line,';');
	if (ret<0) {
		goto done;
	}

	ret = cbuf_puts(line, txt, -1);
	if (ret < 0) {
		goto done;
	}

	ret = f->call.writeline(f->call.data, cbuf_gets(line, 0));
done:
	talloc_free(line);
	return ret;
}


/******************************************************************************/



struct reg_format* reg_format_new(const void* talloc_ctx,
				  struct reg_format_callback cb,
				  const char* str_enc, unsigned flags,
				  const char* sep)
{
	static const struct reg_parse_callback reg_parse_callback_default = {
		.key = (reg_parse_callback_key_t)&reg_format_key,
		.val = (reg_parse_callback_val_t)&reg_format_value,
		.val_del = (reg_parse_callback_val_del_t)&reg_format_value_delete,
		.comment = (reg_parse_callback_comment_t)&reg_format_comment,
	};

	struct reg_format* f = talloc_zero(talloc_ctx, struct reg_format);
	if (f == NULL) {
		return NULL;
	}

	f->reg_parse_callback = reg_parse_callback_default;
	f->reg_parse_callback.data = f;

	f->call = cb;
	f->flags = flags;
	f->sep   = sep;

	if (str_enc && !set_iconv(&f->fromUTF16, str_enc, "UTF-16LE")) {
		DEBUG(0, ("reg_format_new: failed to set encoding: %s\n",
			  str_enc));
		goto fail;
	}

	assert(&f->reg_parse_callback == (struct reg_parse_callback*)f);
	return f;
fail:
	talloc_free(f);
	return NULL;
}

int reg_format_set_options(struct reg_format* fmt, const char* options)
{
	static const char* DEFAULT ="enc=unix,flags=0,sep=\\";

	int ret = 0;
	char *key, *val;
	void* ctx = talloc_new(fmt);

	if (options == NULL) {
		options = DEFAULT;
	}

	while (srprs_option(&options, ctx, &key, &val)) {
		if ((strcmp(key, "enc") == 0) || (strcmp(key, "strenc") == 0)) {
			if (!set_iconv(&fmt->fromUTF16, val, "UTF-16LE")) {
				DEBUG(0, ("Failed to set encoding: %s\n", val));
				ret = -1;
			}
		} else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
			char* end = NULL;
			if (val != NULL) {
				fmt->flags = strtol(val, &end, 0);
			}
			if ((end==NULL) || (*end != '\0')) {
				DEBUG(0, ("Invalid flags format: %s\n",
					  val ? val : "<NULL>"));
				ret = -1;
			}
		} else if ((strcmp(key, "sep") == 0) && (val != NULL)) {
			cstr_unescape(val);
			fmt->sep = talloc_steal(fmt, val);
		}

		/* else if (strcmp(key, "hive") == 0) { */
		/* 	if (strcmp(val, "short") == 0) { */
		/* 		f->hive_fmt = REG_FMT_SHORT_HIVES; */
		/* 	} else if (strcmp(val, "long") == 0) { */
		/* 		f->hive_fmt = REG_FMT_LONG_HIVES; */
		/* 	} else if (strcmp(val, "preserve") == 0) { */
		/* 		f->hive_fmt = REG_FMT_PRESERVE_HIVES; */
		/* 	} else { */
		/* 		DEBUG(0, ("Invalid hive format: %s\n", val)); */
		/* 		ret = -1; */
		/* 	} */
		/* } */
	}
	talloc_free(ctx);
	return ret;
}

int reg_format_key(struct reg_format* f, const char* key[], size_t n, bool del)
{
	int ret, r;
	cbuf* line = cbuf_new(f);
	struct fmt_key key_fmt = {
		.key_case  = (f->flags >>  4) & 0x0F,
		.hive_case = (f->flags >>  8) & 0x0F,
		.hive_fmt  = (f->flags >> 12) & 0x0F,
		.sep       = f->sep,
	};

	ret = cbuf_putc(line, '[');
	if (ret < 0) {
		goto done;
	}

	if (del) {
		ret = cbuf_putc(line, '-');
		if (ret < 0) {
			goto done;
		}
	}

	ret = cbuf_print_keyname(line, key, n, &key_fmt);
	if (ret < 0) {
		goto done;
	}

	ret = cbuf_putc(line, ']');
	if (ret < 0) {
		goto done;
	}

	ret = f->call.writeline(f->call.data, "");
	if (ret < 0) {
		goto done;
	}

	r = f->call.writeline(f->call.data, cbuf_gets(line, 0));
	if (r < 0) {
		ret = r;
		goto done;
	}
	ret += r;

done:
	talloc_free(line);
	return ret;
}


int reg_format_registry_key(struct reg_format* f, struct registry_key* key,
			    bool del)
{
	return reg_format_key(f, (const char**)&key->key->name, 1, del);
}

int reg_format_registry_value(struct reg_format* f, const char* name,
			      struct registry_value* val)
{
	return reg_format_value(f, name, val->type,
				val->data.data, val->data.length);
}

int reg_format_regval_blob(struct reg_format* f, const char* name,
			   struct regval_blob* val)
{

	return reg_format_value(f,
				name ? name : regval_name(val),
				regval_type(val),
				regval_data_p(val),
				regval_size(val));
}

/**@}*/


struct reg_format_file
{
	FILE* file;
	const char* encoding;
	smb_iconv_t fromUnix;
	char* nl;
	size_t nl_len;
};


static int reg_format_file_close(struct reg_format* fmt)
{
	struct reg_format_file* fmt_ctx
		= (struct reg_format_file*) fmt->call.data;
	int ret = 0;
	FILE* file = fmt_ctx->file;

	if (fmt_ctx->encoding) {
		char buf[32];
		snprintf(buf, sizeof(buf), "coding: %s", fmt_ctx->encoding);
		reg_format_comment(fmt, "Local Variables:");
		reg_format_comment(fmt, buf);
		reg_format_comment(fmt, "End:");
	}

	if (file != NULL) {
		ret = fclose(file);
	}

	return ret;
}

static int reg_format_file_writeline(void* ptr, const char* line)
{
	size_t size;
	char* dst=NULL;
	struct reg_format_file* fmt_ctx = (struct reg_format_file*)ptr;
	int ret, r;

	size = iconvert_talloc(ptr, fmt_ctx->fromUnix, line, strlen(line), &dst);
	if (size == -1 ) {
		DEBUG(0, ("reg_format_file_writeline: iconvert_talloc failed >%s<\n",  line));
		return -1;
	}

	ret = fwrite(dst, 1, size, fmt_ctx->file);
	if (ret < 0) {
		goto done;
	}

	r = fwrite(fmt_ctx->nl, 1, fmt_ctx->nl_len, fmt_ctx->file);
	ret = (r < 0) ? r : ret + r;

done:
	talloc_free(dst);
	return ret;
}

struct reg_format_file_opt {
	const char* head;
	const char* nl;
	const char* enc;
	bool bom;
	const char* str_enc;
	unsigned flags;
	const char* sep;
};

struct reg_format_file_opt reg_format_file_opt(void* mem_ctx, const char* opt)
{
	static const struct reg_format_file_opt REG4 = {
		.head = "REGEDIT4",
		.nl   = "\r\n",
		.enc  = "dos",
		.str_enc = "dos",
		.bom  = false,
		.flags = (FMT_HIVE_LONG << 12),
		.sep   = "\\",
	};

	static const struct reg_format_file_opt REG5 = {
		.head = "Windows Registry Editor Version 5.00",
		.nl   = "\r\n",
		.enc  = "UTF-16LE",
		.str_enc = "UTF-16LE",
		.bom  = true,
		.flags = (FMT_HIVE_LONG << 12),
		.sep   = "\\",
	};

	struct reg_format_file_opt ret = {
		.head = REG5.head,
		.nl   = "\n",
		.enc  = "unix",
		.bom  = false,
		.str_enc = "UTF-16LE",
		.flags = 0,
		.sep   = "\\",
	};

	void* tmp_ctx = talloc_new(mem_ctx);

	char *key, *val;

	if (opt == NULL) {
		goto done;
	}

	while(srprs_option(&opt, tmp_ctx, &key, &val)) {
		if (strcmp(key, "enc") == 0) {
			ret.enc     = talloc_steal(mem_ctx, val);
			ret.str_enc = ret.enc;
		} else if (strcmp(key, "strenc") == 0) {
			ret.str_enc = talloc_steal(mem_ctx, val);
		} else if (strcmp(key, "fileenc") == 0) {
			ret.enc = talloc_steal(mem_ctx, val);
		} else if ((strcmp(key, "flags") == 0) && (val != NULL)) {
			char* end = NULL;
			if (val != NULL) {
				ret.flags = strtol(val, &end, 0);
			}
			if ((end==NULL) || (*end != '\0')) {
				DEBUG(0, ("Invalid flags format: %s\n",
					  val ? val : "<NULL>"));
			}
		} else if ((strcmp(key, "sep") == 0) && (val != NULL)) {
			cstr_unescape(val);
			ret.sep = talloc_steal(mem_ctx, val);
		} else if (strcmp(key, "head") == 0) {
			cstr_unescape(val);
			ret.head = talloc_steal(mem_ctx, val);
		} else if (strcmp(key, "nl") == 0) {
			cstr_unescape(val);
			ret.nl = talloc_steal(mem_ctx, val);
		} else if (strcmp(key, "bom") == 0) {
			if (val == NULL) {
				ret.bom = true;
			} else {
				ret.bom = atoi(val);
			}
		} else if (strcmp(key, "regedit4") == 0) {
			ret = REG4;
		} else if (strcmp(key, "regedit5") == 0) {
			ret = REG5;
		}
	}
done:
	talloc_free(tmp_ctx);
	return ret;
}


struct reg_format* reg_format_file(const void* talloc_ctx,
				   const char* filename,
				   const char* options)
{
	struct reg_format_file* fmt_ctx;
	struct reg_format* fmt;
	int ret;
	struct reg_format_file_opt opt;

	struct reg_format_callback reg_format_cb = {
		.writeline = &reg_format_file_writeline
	};

	fmt_ctx = talloc_zero(talloc_ctx, struct reg_format_file);
	if (fmt_ctx == NULL) {
		errno = ENOMEM;
		return NULL;
	}

	opt = reg_format_file_opt(fmt_ctx, options);

	reg_format_cb.data = fmt_ctx;

	fmt = reg_format_new(talloc_ctx, reg_format_cb,
			     opt.str_enc, opt.flags, opt.sep);
	if (fmt == NULL) {
		errno = ENOMEM;
		talloc_free(fmt_ctx);
		return NULL;
	}

	talloc_steal(fmt, fmt_ctx);

	if (!set_iconv(&fmt->fromUTF16, opt.str_enc, "UTF-16LE")) { /* HACK */
		DEBUG(0, ("reg_format_file: failed to set string encoding %s",
			      opt.str_enc));
		goto fail;
	}

	if (!set_iconv(&fmt_ctx->fromUnix, opt.enc, "unix")) {
		DEBUG(0, ("reg_format_file: failed to set file encoding %s",
			  opt.enc));
		goto fail;
	}
	fmt_ctx->encoding = talloc_strdup(fmt_ctx, get_charset(opt.enc));

	fmt_ctx->file = fopen(filename, "w");
	if (fmt_ctx->file == NULL) {
		DEBUG(0, ("reg_format_file: fopen failed: %s\n", strerror(errno)));
		goto fail;
	}

	if (setvbuf(fmt_ctx->file, NULL, _IOFBF, 64000) < 0) {
		DEBUG(0, ("reg_format_file: setvbuf failed: %s\n", strerror(errno)));
	}

	talloc_set_destructor(fmt, reg_format_file_close);

	fmt_ctx->nl_len = iconvert_talloc(fmt, fmt_ctx->fromUnix, opt.nl, strlen(opt.nl), &fmt_ctx->nl);
	if (fmt_ctx->nl_len == -1) {
		DEBUG(0, ("iconvert_talloc failed\n"));
		goto fail;
	}

	if (opt.bom) {
		ret = write_bom(fmt_ctx->file, opt.enc, -1);
		if (ret < 0) {
			goto fail;
		}
	}

	ret = fmt->call.writeline(fmt->call.data, opt.head);
	if (ret < 0) {
		goto fail;
	}

	return fmt;
fail:
	talloc_free(fmt);
	return NULL;
}