@@ -4160,7 +4160,7 @@ PHP_FUNCTION(mb_send_mail)
4160
4160
zend_string * extra_cmd = NULL ;
4161
4161
HashTable * headers_ht = NULL ;
4162
4162
zend_string * str_headers = NULL ;
4163
- size_t n , i ;
4163
+ size_t i ;
4164
4164
char * to_r = NULL ;
4165
4165
char * force_extra_parameters = INI_STR ("mail.force_extra_parameters" );
4166
4166
struct {
@@ -4175,15 +4175,12 @@ PHP_FUNCTION(mb_send_mail)
4175
4175
const mbfl_encoding * tran_cs , /* transfer text charset */
4176
4176
* head_enc , /* header transfer encoding */
4177
4177
* body_enc ; /* body transfer encoding */
4178
- mbfl_memory_device device ; /* automatic allocateable buffer for additional header */
4179
4178
const mbfl_language * lang ;
4180
4179
int err = 0 ;
4181
4180
HashTable ht_headers ;
4182
4181
zval * s ;
4183
- extern void mbfl_memory_device_unput (mbfl_memory_device * device );
4184
4182
4185
4183
/* initialize */
4186
- mbfl_memory_device_init (& device , 0 , 0 );
4187
4184
mbfl_string_init (& orig_str );
4188
4185
mbfl_string_init (& conv_str );
4189
4186
@@ -4343,47 +4340,59 @@ PHP_FUNCTION(mb_send_mail)
4343
4340
#define PHP_MBSTR_MAIL_MIME_HEADER2 "Content-Type: text/plain"
4344
4341
#define PHP_MBSTR_MAIL_MIME_HEADER3 "; charset="
4345
4342
#define PHP_MBSTR_MAIL_MIME_HEADER4 "Content-Transfer-Encoding: "
4343
+
4344
+ smart_str str = {0 };
4345
+ bool empty = true;
4346
+
4346
4347
if (str_headers != NULL ) {
4347
- p = ZSTR_VAL (str_headers );
4348
- n = ZSTR_LEN (str_headers );
4349
- mbfl_memory_device_strncat (& device , p , n );
4350
- if (n > 0 && p [n - 1 ] != '\n' ) {
4351
- mbfl_memory_device_strncat (& device , line_sep , line_sep_len );
4348
+ /* Strip trailing CRLF from `str_headers`; we will add CRLF back if necessary */
4349
+ size_t len = ZSTR_LEN (str_headers );
4350
+ if (ZSTR_VAL (str_headers )[len - 1 ] == '\n' ) {
4351
+ len -- ;
4352
+ }
4353
+ if (ZSTR_VAL (str_headers )[len - 1 ] == '\r' ) {
4354
+ len -- ;
4352
4355
}
4356
+ smart_str_appendl (& str , ZSTR_VAL (str_headers ), len );
4357
+ empty = false;
4353
4358
zend_string_release_ex (str_headers , 0 );
4354
4359
}
4355
4360
4356
4361
if (!zend_hash_str_exists (& ht_headers , "mime-version" , sizeof ("mime-version" ) - 1 )) {
4357
- mbfl_memory_device_strncat (& device , PHP_MBSTR_MAIL_MIME_HEADER1 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER1 ) - 1 );
4358
- mbfl_memory_device_strncat (& device , line_sep , line_sep_len );
4362
+ if (!empty ) {
4363
+ smart_str_appendl (& str , line_sep , line_sep_len );
4364
+ }
4365
+ smart_str_appendl (& str , PHP_MBSTR_MAIL_MIME_HEADER1 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER1 ) - 1 );
4366
+ empty = false;
4359
4367
}
4360
4368
4361
4369
if (!suppressed_hdrs .cnt_type ) {
4362
- mbfl_memory_device_strncat (& device , PHP_MBSTR_MAIL_MIME_HEADER2 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER2 ) - 1 );
4370
+ if (!empty ) {
4371
+ smart_str_appendl (& str , line_sep , line_sep_len );
4372
+ }
4373
+ smart_str_appendl (& str , PHP_MBSTR_MAIL_MIME_HEADER2 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER2 ) - 1 );
4363
4374
4364
4375
p = (char * )mbfl_encoding_preferred_mime_name (tran_cs );
4365
4376
if (p != NULL ) {
4366
- mbfl_memory_device_strncat ( & device , PHP_MBSTR_MAIL_MIME_HEADER3 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER3 ) - 1 );
4367
- mbfl_memory_device_strcat ( & device , p );
4377
+ smart_str_appendl ( & str , PHP_MBSTR_MAIL_MIME_HEADER3 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER3 ) - 1 );
4378
+ smart_str_appends ( & str , p );
4368
4379
}
4369
- mbfl_memory_device_strncat ( & device , line_sep , line_sep_len ) ;
4380
+ empty = false ;
4370
4381
}
4382
+
4371
4383
if (!suppressed_hdrs .cnt_trans_enc ) {
4372
- mbfl_memory_device_strncat (& device , PHP_MBSTR_MAIL_MIME_HEADER4 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER4 ) - 1 );
4384
+ if (!empty ) {
4385
+ smart_str_appendl (& str , line_sep , line_sep_len );
4386
+ }
4387
+ smart_str_appendl (& str , PHP_MBSTR_MAIL_MIME_HEADER4 , sizeof (PHP_MBSTR_MAIL_MIME_HEADER4 ) - 1 );
4373
4388
p = (char * )mbfl_encoding_preferred_mime_name (body_enc );
4374
4389
if (p == NULL ) {
4375
4390
p = "7bit" ;
4376
4391
}
4377
- mbfl_memory_device_strcat (& device , p );
4378
- mbfl_memory_device_strncat (& device , line_sep , line_sep_len );
4392
+ smart_str_appends (& str , p );
4379
4393
}
4380
4394
4381
- if (!PG (mail_mixed_lf_and_crlf )) {
4382
- mbfl_memory_device_unput (& device );
4383
- }
4384
- mbfl_memory_device_unput (& device );
4385
- mbfl_memory_device_output ('\0' , & device );
4386
- str_headers = zend_string_init ((char * )device .buffer , strlen ((char * )device .buffer ), 0 );
4395
+ str_headers = smart_str_extract (& str );
4387
4396
4388
4397
if (force_extra_parameters ) {
4389
4398
extra_cmd = php_escape_shell_cmd (force_extra_parameters );
@@ -4404,7 +4413,6 @@ PHP_FUNCTION(mb_send_mail)
4404
4413
efree ((void * )subject_buf );
4405
4414
}
4406
4415
zend_string_free (conv );
4407
- mbfl_memory_device_clear (& device );
4408
4416
zend_hash_destroy (& ht_headers );
4409
4417
if (str_headers ) {
4410
4418
zend_string_release_ex (str_headers , 0 );
0 commit comments