Skip to content

Commit 8a73a68

Browse files
committed
Use fast encoding conversion filters in mb_send_mail
1 parent 3ed5264 commit 8a73a68

File tree

3 files changed

+11
-95
lines changed

3 files changed

+11
-95
lines changed

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -230,66 +230,6 @@ const mbfl_encoding *mbfl_encoding_detector_judge(mbfl_encoding_detector *identd
230230
return enc;
231231
}
232232

233-
/*
234-
* encoding converter
235-
*/
236-
mbfl_string *
237-
mbfl_convert_encoding(
238-
mbfl_string *string,
239-
mbfl_string *result,
240-
const mbfl_encoding *toenc)
241-
{
242-
size_t n;
243-
unsigned char *p;
244-
mbfl_memory_device device;
245-
mbfl_convert_filter *filter1 = NULL;
246-
mbfl_convert_filter *filter2 = NULL;
247-
248-
/* initialize */
249-
if (mbfl_convert_filter_get_vtbl(string->encoding, toenc) != NULL) {
250-
filter1 = mbfl_convert_filter_new(string->encoding, toenc, mbfl_memory_device_output, 0, &device);
251-
} else {
252-
filter2 = mbfl_convert_filter_new(&mbfl_encoding_wchar, toenc, mbfl_memory_device_output, 0, &device);
253-
if (filter2 != NULL) {
254-
filter1 = mbfl_convert_filter_new(string->encoding, &mbfl_encoding_wchar, (int (*)(int, void*))filter2->filter_function, NULL, filter2);
255-
if (filter1 == NULL) {
256-
mbfl_convert_filter_delete(filter2);
257-
}
258-
}
259-
}
260-
if (filter1 == NULL) {
261-
return NULL;
262-
}
263-
264-
if (filter2 != NULL) {
265-
filter2->illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
266-
filter2->illegal_substchar = '?';
267-
}
268-
269-
mbfl_memory_device_init(&device, string->len, (string->len >> 2) + 8);
270-
271-
/* feed data */
272-
n = string->len;
273-
p = string->val;
274-
if (p != NULL) {
275-
while (n > 0) {
276-
if ((*filter1->filter_function)(*p++, filter1) < 0) {
277-
break;
278-
}
279-
n--;
280-
}
281-
}
282-
283-
mbfl_convert_filter_flush(filter1);
284-
mbfl_convert_filter_delete(filter1);
285-
if (filter2 != NULL) {
286-
mbfl_convert_filter_flush(filter2);
287-
mbfl_convert_filter_delete(filter2);
288-
}
289-
290-
return mbfl_memory_device_result(&device, result);
291-
}
292-
293233
/*
294234
* strcut
295235
*/

ext/mbstring/libmbfl/mbfl/mbfilter.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,6 @@ MBFLAPI extern void mbfl_encoding_detector_delete(mbfl_encoding_detector *identd
147147
MBFLAPI extern int mbfl_encoding_detector_feed(mbfl_encoding_detector *identd, mbfl_string *string);
148148
MBFLAPI extern const mbfl_encoding *mbfl_encoding_detector_judge(mbfl_encoding_detector *identd);
149149

150-
151-
/*
152-
* encoding converter
153-
*/
154-
MBFLAPI extern mbfl_string *
155-
mbfl_convert_encoding(mbfl_string *string, mbfl_string *result, const mbfl_encoding *toenc);
156-
157-
158150
/* Lengths -1 through -16 are reserved for error return values */
159151
static inline int mbfl_is_error(size_t len) {
160152
return len >= (size_t) -16;

ext/mbstring/mbstring.c

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,7 +4168,7 @@ PHP_FUNCTION(mb_send_mail)
41684168
int cnt_trans_enc:1;
41694169
} suppressed_hdrs = { 0, 0 };
41704170

4171-
char *message_buf = NULL, *subject_buf = NULL, *p;
4171+
char *subject_buf = NULL, *p;
41724172
mbfl_string orig_str, conv_str;
41734173
mbfl_string *pstr; /* pointer to mbfl string for return value */
41744174
enum mbfl_no_encoding;
@@ -4326,27 +4326,17 @@ PHP_FUNCTION(mb_send_mail)
43264326
}
43274327

43284328
/* message body */
4329-
orig_str.val = (unsigned char *)message;
4330-
orig_str.len = message_len;
4331-
orig_str.encoding = MBSTRG(current_internal_encoding);
4329+
const mbfl_encoding *msg_enc = MBSTRG(current_internal_encoding);
43324330

4333-
if (orig_str.encoding->no_encoding == mbfl_no_encoding_invalid || orig_str.encoding->no_encoding == mbfl_no_encoding_pass) {
4334-
orig_str.encoding = mb_guess_encoding((unsigned char*)message, message_len, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
4331+
if (msg_enc == &mbfl_encoding_pass) {
4332+
msg_enc = mb_guess_encoding((unsigned char*)message, message_len, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
43354333
}
43364334

4337-
pstr = NULL;
4338-
{
4339-
mbfl_string tmpstr;
4340-
4341-
if (mbfl_convert_encoding(&orig_str, &tmpstr, tran_cs) != NULL) {
4342-
tmpstr.encoding = &mbfl_encoding_8bit;
4343-
pstr = mbfl_convert_encoding(&tmpstr, &conv_str, body_enc);
4344-
efree(tmpstr.val);
4345-
}
4346-
}
4347-
if (pstr != NULL) {
4348-
message_buf = message = (char *)pstr->val;
4349-
}
4335+
unsigned int num_errors = 0;
4336+
zend_string *tmpstr = mb_fast_convert((unsigned char*)message, message_len, msg_enc, tran_cs, '?', MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR, &num_errors);
4337+
zend_string *conv = mb_fast_convert((unsigned char*)ZSTR_VAL(tmpstr), ZSTR_LEN(tmpstr), &mbfl_encoding_8bit, body_enc, '?', MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR, &num_errors);
4338+
zend_string_free(tmpstr);
4339+
message = ZSTR_VAL(conv);
43504340

43514341
/* other headers */
43524342
#define PHP_MBSTR_MAIL_MIME_HEADER1 "MIME-Version: 1.0"
@@ -4401,11 +4391,7 @@ PHP_FUNCTION(mb_send_mail)
44014391
extra_cmd = php_escape_shell_cmd(ZSTR_VAL(extra_cmd));
44024392
}
44034393

4404-
if (!err && php_mail(to_r, subject, message, ZSTR_VAL(str_headers), extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
4405-
RETVAL_TRUE;
4406-
} else {
4407-
RETVAL_FALSE;
4408-
}
4394+
RETVAL_BOOL(!err && php_mail(to_r, subject, message, ZSTR_VAL(str_headers), extra_cmd ? ZSTR_VAL(extra_cmd) : NULL));
44094395

44104396
if (extra_cmd) {
44114397
zend_string_release_ex(extra_cmd, 0);
@@ -4417,9 +4403,7 @@ PHP_FUNCTION(mb_send_mail)
44174403
if (subject_buf) {
44184404
efree((void *)subject_buf);
44194405
}
4420-
if (message_buf) {
4421-
efree((void *)message_buf);
4422-
}
4406+
zend_string_free(conv);
44234407
mbfl_memory_device_clear(&device);
44244408
zend_hash_destroy(&ht_headers);
44254409
if (str_headers) {

0 commit comments

Comments
 (0)