summaryrefslogtreecommitdiffstats
path: root/maildir/maildirinfo2.c
blob: fcae14c2d1a13c789db9fb5fe64baa21fce9aaee (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
/*
** Copyright 2018 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<errno.h>
#include	<ctype.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#include	"maildirinfo.h"
#include	<courier-unicode.h>


/* Count the size of the converted filename or foldername string */
static void count_utf8(const char *str, size_t n, void *arg)
{
	*(size_t *)arg += n;
}

/* Save the converted filename or foldername string */
static void save_utf8(const char *str, size_t n, void *arg)
{
	char **p=(char **)arg;

	memcpy( (*p), str, n);
	*p += n;
}

/*
** Split a string at periods, convert each split range between charsets.
*/
static int foldername_filename_convert(const char *src_chset,
				       const char *dst_chset,
				       const char *foldername,
				       void (*closure)(const char *, size_t,
						       void *),
				       void *arg)
{
	unicode_convert_handle_t h;
	char *cbufptr;
	size_t cbufsize;
	size_t l;
	int conversion_error;

	while (*foldername)
	{
		if (*foldername == '.')
		{
			(*closure)(foldername, 1, arg);
			++foldername;
			continue;
		}

		h=unicode_convert_tocbuf_init(src_chset,
					      dst_chset,
					      &cbufptr,
					      &cbufsize,
					      0);
		if (!h)
		{
			errno=EINVAL;
			return -1;
		}

		for (l=0; foldername[l] && foldername[l] != '.'; ++l)
			;

		unicode_convert(h, foldername, l);

		if (unicode_convert_deinit(h, &conversion_error) == 0)
		{
			closure(cbufptr, cbufsize, arg);
			free(cbufptr);
			if (conversion_error)
			{
				errno=EILSEQ;
				return -1;
			}
		}
		else
			return -1;
		foldername += l;
	}
	return 0;
}

/* Convert either a foldername or a filename to the other */

static char *foldername_filename_convert_tobuf(const char *src_chset,
					       const char *dst_chset,
					       const char *foldername)
{
	char *utf8, *p;
	size_t l=1;

	if (foldername_filename_convert(src_chset,
					dst_chset,
					foldername, count_utf8, &l))
		return NULL;

	utf8=malloc(l+1);

	if (!utf8)
		return NULL;

	p=utf8;
	if (foldername_filename_convert(src_chset,
					dst_chset,
					foldername, save_utf8, &p))
	{
		free(utf8);
		return NULL;
	}

	*p=0;

	return utf8;
}

char *imap_foldername_to_filename(int utf8_format, const char *foldername)
{
	if (utf8_format)
		return strdup(foldername);

	return foldername_filename_convert_tobuf
		(unicode_x_imap_modutf7,
		 "utf-8",
		 foldername);
}

char *imap_filename_to_foldername(int utf8_format, const char *filename)
{
	if (utf8_format)
		return strdup(filename);

	return foldername_filename_convert_tobuf
		("utf-8",
		 unicode_x_imap_modutf7,
		 filename);
}