-
Notifications
You must be signed in to change notification settings - Fork 2
/
charset_utils.c
58 lines (48 loc) · 1.14 KB
/
charset_utils.c
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
/*
FTP file system
Copyright (C) 2007 Robson Braga Araujo <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include "config.h"
#include <string.h>
#include <limits.h>
#include <iconv.h>
#include <errno.h>
#include <stdlib.h>
#include "ftpfs.h"
int convert_charsets(const char* from, const char* to, char** str) {
iconv_t cd;
char* s = *str;
if (!s || !*s)
return 0;
if (to && from && (cd = iconv_open(to, from)) != (iconv_t)-1) {
ICONV_CONST char* ib;
char* buf;
char* ob;
size_t ibl, obl;
ibl = strlen(s) + 1;
ib = s;
obl = MB_LEN_MAX * ibl;
buf = (char*)malloc(obl * sizeof(char));
ob = buf;
do {
if (iconv(cd, &ib, &ibl, &ob, &obl) == (size_t)-1) {
DEBUG(2, "iconv return error %d\n", errno);
if (obl) {
*ob++ = *ib++;
ibl--;
obl--;
}
}
} while (ibl && obl);
*ob = 0;
DEBUG(2, "iconv return %s\n", buf);
iconv_close (cd);
free(*str);
*str = buf;
} else {
DEBUG(2, "iconv_open return error %d\n", errno);
}
return 0;
}