@@ -231,7 +231,7 @@ static int is_rfc822_special(char ch)
231231 }
232232}
233233
234- static int has_rfc822_specials (const char * s , int len )
234+ static int needs_rfc822_quoting (const char * s , int len )
235235{
236236 int i ;
237237 for (i = 0 ; i < len ; i ++ )
@@ -329,25 +329,29 @@ static int is_rfc2047_special(char ch, enum rfc2047_type type)
329329 return !(isalnum (ch ) || ch == '!' || ch == '*' || ch == '+' || ch == '-' || ch == '/' );
330330}
331331
332- static void add_rfc2047 ( struct strbuf * sb , const char * line , int len ,
333- const char * encoding , enum rfc2047_type type )
332+ static int needs_rfc2047_encoding ( const char * line , int len ,
333+ enum rfc2047_type type )
334334{
335- static const int max_length = 78 ; /* per rfc2822 */
336- static const int max_encoded_length = 76 ; /* per rfc2047 */
337335 int i ;
338- int line_len = last_line_length (sb );
339336
340337 for (i = 0 ; i < len ; i ++ ) {
341338 int ch = line [i ];
342339 if (non_ascii (ch ) || ch == '\n' )
343- goto needquote ;
340+ return 1 ;
344341 if ((i + 1 < len ) && (ch == '=' && line [i + 1 ] == '?' ))
345- goto needquote ;
342+ return 1 ;
346343 }
347- strbuf_add_wrapped_bytes (sb , line , len , - line_len , 1 , max_length );
348- return ;
349344
350- needquote :
345+ return 0 ;
346+ }
347+
348+ static void add_rfc2047 (struct strbuf * sb , const char * line , int len ,
349+ const char * encoding , enum rfc2047_type type )
350+ {
351+ static const int max_encoded_length = 76 ; /* per rfc2047 */
352+ int i ;
353+ int line_len = last_line_length (sb );
354+
351355 strbuf_grow (sb , len * 3 + strlen (encoding ) + 100 );
352356 strbuf_addf (sb , "=?%s?q?" , encoding );
353357 line_len += strlen (encoding ) + 5 ; /* 5 for =??q? */
@@ -383,6 +387,7 @@ void pp_user_info(const struct pretty_print_context *pp,
383387 const char * what , struct strbuf * sb ,
384388 const char * line , const char * encoding )
385389{
390+ int max_length = 78 ; /* per rfc2822 */
386391 char * date ;
387392 int namelen ;
388393 unsigned long time ;
@@ -406,17 +411,21 @@ void pp_user_info(const struct pretty_print_context *pp,
406411 name_tail -- ;
407412 display_name_length = name_tail - line ;
408413 strbuf_addstr (sb , "From: " );
409- if (! has_rfc822_specials (line , display_name_length )) {
414+ if (needs_rfc2047_encoding (line , display_name_length , RFC2047_ADDRESS )) {
410415 add_rfc2047 (sb , line , display_name_length ,
411416 encoding , RFC2047_ADDRESS );
412- } else {
417+ max_length = 76 ; /* per rfc2047 */
418+ } else if (needs_rfc822_quoting (line , display_name_length )) {
413419 struct strbuf quoted = STRBUF_INIT ;
414420 add_rfc822_quoted (& quoted , line , display_name_length );
415- add_rfc2047 (sb , quoted .buf , quoted .len ,
416- encoding , RFC2047_ADDRESS );
421+ strbuf_add_wrapped_bytes (sb , quoted .buf , quoted .len ,
422+ -6 , 1 , max_length );
417423 strbuf_release (& quoted );
424+ } else {
425+ strbuf_add_wrapped_bytes (sb , line , display_name_length ,
426+ -6 , 1 , max_length );
418427 }
419- if (namelen - display_name_length + last_line_length (sb ) > 78 ) {
428+ if (namelen - display_name_length + last_line_length (sb ) > max_length ) {
420429 strbuf_addch (sb , '\n' );
421430 if (!isspace (name_tail [0 ]))
422431 strbuf_addch (sb , ' ' );
@@ -1336,6 +1345,7 @@ void pp_title_line(const struct pretty_print_context *pp,
13361345 const char * encoding ,
13371346 int need_8bit_cte )
13381347{
1348+ static const int max_length = 78 ; /* per rfc2047 */
13391349 struct strbuf title ;
13401350
13411351 strbuf_init (& title , 80 );
@@ -1345,7 +1355,12 @@ void pp_title_line(const struct pretty_print_context *pp,
13451355 strbuf_grow (sb , title .len + 1024 );
13461356 if (pp -> subject ) {
13471357 strbuf_addstr (sb , pp -> subject );
1348- add_rfc2047 (sb , title .buf , title .len , encoding , RFC2047_SUBJECT );
1358+ if (needs_rfc2047_encoding (title .buf , title .len , RFC2047_SUBJECT ))
1359+ add_rfc2047 (sb , title .buf , title .len ,
1360+ encoding , RFC2047_SUBJECT );
1361+ else
1362+ strbuf_add_wrapped_bytes (sb , title .buf , title .len ,
1363+ - last_line_length (sb ), 1 , max_length );
13491364 } else {
13501365 strbuf_addbuf (sb , & title );
13511366 }
0 commit comments