-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoding.c
165 lines (147 loc) · 3.61 KB
/
encoding.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
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
/*
* Charset encoding processing.
*
* Copyright (c) 2011,2014,2017 ABC <abc at openwall.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#include <string.h>
#include <stdlib.h>
#include <iconv.h>
#include <errno.h>
#include "buffer.h"
#include "encoding.h"
#define UTF8_CHARSET "UTF-8"
#define DEFAULT_CHARSET "latin1"
#define UNKNOWN_CHARSET "latin1"
#define MAX_CHARSET_LEN 70
static const char *charset_whitelist[] = {
"us-ascii$",
"iso-8859-",
"utf-7$",
"koi8-r$",
"koi8-u$",
"windows-",
"cp",
"gb2312$",
"gbk$",
"gb18030$",
"big5$",
"iso-2022-jp$",
"utf-8$", /* redundant in enc_to_utf8(), may be needed elsewhere */
NULL
};
static inline int simple_tolower(char ch)
{
if (ch >= 'A' && ch <= 'Z')
return ch + ('a' - 'A');
return ch;
}
static int match_charset(const char *charset, const char *mask)
{
for (; *mask; mask++, charset++) {
if (*mask == '$')
return !*charset;
if (*mask != simple_tolower(*charset))
return 0;
}
/* allow up to 8 digits */
unsigned int i;
for (i = 0; *charset && i < 8; i++, charset++) {
if (*charset < '0' || *charset > '9')
return 0;
}
return !*charset;
}
int enc_allowed_charset(const char *charset)
{
const char **p;
for (p = charset_whitelist; *p; p++)
if (match_charset(charset, *p))
return 1;
return 0;
}
/* convert text from `enc' buffer to `dst' by `charset' */
int enc_to_utf8(struct buffer *dst, struct buffer *enc, const char *charset)
{
char *iptr = enc->start;
size_t inlen = enc->ptr - enc->start;
char charset_buf[MAX_CHARSET_LEN];
size_t i;
const char *p;
if (!charset)
charset = UNKNOWN_CHARSET;
/* sanitize charset string */
p = charset;
i = 0;
while (i < sizeof(charset_buf) - 1 &&
((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') ||
(*p == '-')))
charset_buf[i++] = *p++;
if (!*p || *p == '?') {
charset_buf[i] = '\0';
charset = charset_buf;
} else {
charset = UNKNOWN_CHARSET;
}
if (!strcasecmp(UTF8_CHARSET, charset) /* no recoding needed */ ||
!enc_allowed_charset(charset)) {
buffer_append(dst, iptr, inlen);
} else {
iconv_t cd = iconv_open(UTF8_CHARSET, charset);
char out[ENC_ICONV_BUF_SIZE];
if (cd == (iconv_t)(-1))
cd = iconv_open(UTF8_CHARSET, UNKNOWN_CHARSET);
if (cd == (iconv_t)(-1))
return -1;
do {
char *optr = out;
size_t outlen = sizeof(out);
size_t e = iconv(cd, &iptr, &inlen, &optr, &outlen);
buffer_append(dst, out, optr - out);
/* if output buffer is full (errno == E2BIG) we
* will just continue processing (it will be
* resumed on next iteration, because iconv()
* also updates iptr and inlen), otherwise
* report conversion error with REPLACEMENT
* CHARACTER (U+FFFD), which looks like <?>. */
if (e == (size_t)-1 && errno != E2BIG) {
buffer_appenduc(dst, 0xFFFD);
iptr++;
inlen--;
}
} while ((ssize_t)inlen > 0);
iconv_close(cd);
}
enc->ptr = enc->start;
return dst->error;
}
/* remove trailing partial utf8 character from string by reducing its len */
/* return how many bytes are removed, *lenp is modified to reflect new length */
int enc_utf8_remove_partial(char *ptr, int *lenp)
{
int len;
for (len = *lenp; len; ) {
int s_size = 1; /* sequence size */
unsigned char ch = *ptr;
if (ch >= 0xf3)
/* illegal multi-byte sequence */;
else if (ch >= 0xf0)
s_size = 4;
else if (ch >= 0xe0)
s_size = 3;
else if (ch >= 0xc0)
s_size = 2;
if (len < s_size)
break;
len -= s_size;
ptr += s_size;
}
*lenp -= len;
return len;
}