Skip to content

Commit 689978a

Browse files
committed
Code cleanup in mbfilter_utf7.c
1 parent ebe6500 commit 689978a

File tree

1 file changed

+58
-103
lines changed

1 file changed

+58
-103
lines changed

ext/mbstring/libmbfl/filters/mbfilter_utf7.c

Lines changed: 58 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -79,55 +79,52 @@ const struct mbfl_convert_vtbl vtbl_wchar_utf7 = {
7979

8080
#define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
8181

82-
/*
83-
* UTF-7 => wchar
84-
*/
82+
static unsigned int decode_base64_char(unsigned char c)
83+
{
84+
if (c >= 'A' && c <= 'Z') {
85+
return c - 65;
86+
} else if (c >= 'a' && c <= 'z') {
87+
return c - 71;
88+
} else if (c >= '0' && c <= '9') {
89+
return c + 4;
90+
} else if (c == '+') {
91+
return 62;
92+
} else if (c == '/') {
93+
return 63;
94+
}
95+
return -1;
96+
}
97+
8598
int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
8699
{
87-
int s, n;
88-
89-
n = -1;
90-
if (filter->status != 0) { /* Modified Base64 */
91-
if (c >= 0x41 && c <= 0x5a) { /* A - Z */
92-
n = c - 65;
93-
} else if (c >= 0x61 && c <= 0x7a) { /* a - z */
94-
n = c - 71;
95-
} else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
96-
n = c + 4;
97-
} else if (c == 0x2b) { /* '+' */
98-
n = 62;
99-
} else if (c == 0x2f) { /* '/' */
100-
n = 63;
101-
}
102-
if (n < 0 || n > 63) {
103-
if (c == 0x2d) {
104-
if (filter->status == 1) { /* "+-" -> "+" */
105-
CK((*filter->output_function)(0x2b, filter->data));
100+
int s, n = -1;
101+
102+
if (filter->status) { /* Modified Base64 */
103+
n = decode_base64_char(c);
104+
if (n < 0) {
105+
if (c == '-') {
106+
if (filter->status == 1) { /* "+-" -> "+" */
107+
CK((*filter->output_function)('+', filter->data));
106108
}
107-
} else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
109+
} else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
108110
CK((*filter->output_function)(c, filter->data));
109-
} else { /* illegal character */
110-
s = c & MBFL_WCSGROUP_MASK;
111-
s |= MBFL_WCSGROUP_THROUGH;
112-
CK((*filter->output_function)(s, filter->data));
111+
} else { /* illegal character */
112+
CK((*filter->output_function)(c | MBFL_WCSGROUP_THROUGH, filter->data));
113113
}
114-
filter->cache = 0;
115-
filter->status = 0;
114+
filter->cache = filter->status = 0;
116115
return c;
117116
}
118117
}
119118

120119
switch (filter->status) {
121120
/* directly encoded characters */
122121
case 0:
123-
if (c == 0x2b) { /* '+' shift character */
122+
if (c == '+') { /* '+' shift character */
124123
filter->status = 1;
125-
} else if (c >= 0 && c < 0x80) { /* ASCII */
124+
} else if (c >= 0 && c < 0x80) { /* ASCII */
126125
CK((*filter->output_function)(c, filter->data));
127-
} else { /* illegal character */
128-
s = c & MBFL_WCSGROUP_MASK;
129-
s |= MBFL_WCSGROUP_THROUGH;
130-
CK((*filter->output_function)(s, filter->data));
126+
} else { /* illegal character */
127+
CK((*filter->output_function)(c | MBFL_WCSGROUP_THROUGH, filter->data));
131128
}
132129
break;
133130

@@ -154,10 +151,8 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
154151
filter->cache = n;
155152
if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
156153
CK((*filter->output_function)(s, filter->data));
157-
} else { /* illegal character */
158-
s &= MBFL_WCSGROUP_MASK;
159-
s |= MBFL_WCSGROUP_THROUGH;
160-
CK((*filter->output_function)(s, filter->data));
154+
} else { /* illegal character */
155+
CK((*filter->output_function)(s | MBFL_WCSGROUP_THROUGH, filter->data));
161156
}
162157
} else {
163158
filter->cache = n;
@@ -186,10 +181,8 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
186181
filter->cache = n;
187182
if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
188183
CK((*filter->output_function)(s, filter->data));
189-
} else { /* illegal character */
190-
s &= MBFL_WCSGROUP_MASK;
191-
s |= MBFL_WCSGROUP_THROUGH;
192-
CK((*filter->output_function)(s, filter->data));
184+
} else { /* illegal character */
185+
CK((*filter->output_function)(s | MBFL_WCSGROUP_THROUGH, filter->data));
193186
}
194187
} else {
195188
filter->cache = n;
@@ -232,57 +225,22 @@ int mbfl_filt_conv_utf7_wchar(int c, mbfl_convert_filter *filter)
232225
return c;
233226
}
234227

235-
/*
236-
* wchar => UTF-7
237-
*/
238228
int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
239229
{
240-
int s, n;
230+
int s;
241231

242-
n = 0;
243-
if (c >= 0 && c < 0x80) { /* ASCII */
244-
if (c >= 0x41 && c <= 0x5a) { /* A - Z */
245-
n = 1;
246-
} else if (c >= 0x61 && c <= 0x7a) { /* a - z */
232+
int n = 0;
233+
if (c >= 0 && c < 0x80) { /* ASCII */
234+
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '\0' || c == '/' || c == '-') {
247235
n = 1;
248-
} else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
249-
n = 1;
250-
} else if (c == '\0') { /* '\0' */
251-
n = 1;
252-
} else if (c == 0x2f) { /* '/' */
253-
n = 1;
254-
} else if (c == 0x2d) { /* '-' */
255-
n = 1;
256-
} else if (c == 0x20) { /* SPACE */
257-
n = 2;
258-
} else if (c == 0x09) { /* HTAB */
259-
n = 2;
260-
} else if (c == 0x0d) { /* CR */
261-
n = 2;
262-
} else if (c == 0x0a) { /* LF */
263-
n = 2;
264-
} else if (c == 0x27) { /* "'" */
265-
n = 2;
266-
} else if (c == 0x28) { /* '(' */
267-
n = 2;
268-
} else if (c == 0x29) { /* ')' */
269-
n = 2;
270-
} else if (c == 0x2c) { /* ',' */
271-
n = 2;
272-
} else if (c == 0x2e) { /* '.' */
273-
n = 2;
274-
} else if (c == 0x3a) { /* ':' */
275-
n = 2;
276-
} else if (c == 0x3f) { /* '?' */
236+
} else if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\'' || c == '(' || c == ')' || c == ',' || c == '.' || c == ':' || c == '?') {
277237
n = 2;
278238
}
279239
} else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
280240
;
281241
} else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
282-
s = ((c >> 10) - 0x40) | 0xd800;
283-
CK((*filter->filter_function)(s, filter));
284-
s = (c & 0x3ff) | 0xdc00;
285-
CK((*filter->filter_function)(s, filter));
242+
CK((*filter->filter_function)(((c >> 10) - 0x40) | 0xd800, filter));
243+
CK((*filter->filter_function)((c & 0x3ff) | 0xdc00, filter));
286244
return c;
287245
} else {
288246
CK(mbfl_filt_conv_illegal_output(c, filter));
@@ -291,11 +249,11 @@ int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
291249

292250
switch (filter->status) {
293251
case 0:
294-
if (n != 0) { /* directly encode characters */
252+
if (n != 0) { /* directly encode characters */
295253
CK((*filter->output_function)(c, filter->data));
296-
} else { /* Modified Base64 */
297-
CK((*filter->output_function)(0x2b, filter->data)); /* '+' */
298-
filter->status++;
254+
} else { /* Modified Base64 */
255+
CK((*filter->output_function)('+', filter->data));
256+
filter->status = 1;
299257
filter->cache = c;
300258
}
301259
break;
@@ -308,12 +266,12 @@ int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
308266
if (n != 0) {
309267
CK((*filter->output_function)(mbfl_base64_table[(s << 2) & 0x3c], filter->data));
310268
if (n == 1) {
311-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
269+
CK((*filter->output_function)('-', filter->data));
312270
}
313271
CK((*filter->output_function)(c, filter->data));
314272
filter->status = 0;
315273
} else {
316-
filter->status++;
274+
filter->status = 2;
317275
filter->cache = ((s & 0xf) << 16) | c;
318276
}
319277
break;
@@ -326,12 +284,12 @@ int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
326284
if (n != 0) {
327285
CK((*filter->output_function)(mbfl_base64_table[(s << 4) & 0x30], filter->data));
328286
if (n == 1) {
329-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
287+
CK((*filter->output_function)('-', filter->data));
330288
}
331289
CK((*filter->output_function)(c, filter->data));
332290
filter->status = 0;
333291
} else {
334-
filter->status++;
292+
filter->status = 3;
335293
filter->cache = ((s & 0x3) << 16) | c;
336294
}
337295
break;
@@ -343,7 +301,7 @@ int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
343301
CK((*filter->output_function)(mbfl_base64_table[s & 0x3f], filter->data));
344302
if (n != 0) {
345303
if (n == 1) {
346-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
304+
CK((*filter->output_function)('-', filter->data));
347305
}
348306
CK((*filter->output_function)(c, filter->data));
349307
filter->status = 0;
@@ -364,38 +322,35 @@ int mbfl_filt_conv_wchar_utf7(int c, mbfl_convert_filter *filter)
364322

365323
int mbfl_filt_conv_wchar_utf7_flush(mbfl_convert_filter *filter)
366324
{
367-
int status, cache;
325+
int status = filter->status;
326+
int cache = filter->cache;
368327

369-
status = filter->status;
370-
cache = filter->cache;
371-
filter->status = 0;
372-
filter->cache = 0;
373328
/* flush fragments */
374329
switch (status) {
375330
case 1:
376331
CK((*filter->output_function)(mbfl_base64_table[(cache >> 10) & 0x3f], filter->data));
377332
CK((*filter->output_function)(mbfl_base64_table[(cache >> 4) & 0x3f], filter->data));
378333
CK((*filter->output_function)(mbfl_base64_table[(cache << 2) & 0x3c], filter->data));
379-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
334+
CK((*filter->output_function)('-', filter->data));
380335
break;
381336

382337
case 2:
383338
CK((*filter->output_function)(mbfl_base64_table[(cache >> 14) & 0x3f], filter->data));
384339
CK((*filter->output_function)(mbfl_base64_table[(cache >> 8) & 0x3f], filter->data));
385340
CK((*filter->output_function)(mbfl_base64_table[(cache >> 2) & 0x3f], filter->data));
386341
CK((*filter->output_function)(mbfl_base64_table[(cache << 4) & 0x30], filter->data));
387-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
342+
CK((*filter->output_function)('-', filter->data));
388343
break;
389344

390345
case 3:
391346
CK((*filter->output_function)(mbfl_base64_table[(cache >> 12) & 0x3f], filter->data));
392347
CK((*filter->output_function)(mbfl_base64_table[(cache >> 6) & 0x3f], filter->data));
393348
CK((*filter->output_function)(mbfl_base64_table[cache & 0x3f], filter->data));
394-
CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
349+
CK((*filter->output_function)('-', filter->data));
395350
break;
396351
}
397352

398-
if (filter->flush_function != NULL) {
353+
if (filter->flush_function) {
399354
(*filter->flush_function)(filter->data);
400355
}
401356

0 commit comments

Comments
 (0)