@@ -346,11 +346,17 @@ static const char *header[MAX_HDR_PARSED] = {
346
346
"From" ,"Subject" ,"Date" ,
347
347
};
348
348
349
- static inline int cmp_header (const struct strbuf * line , const char * hdr )
349
+ static inline int skip_header (const struct strbuf * line , const char * hdr ,
350
+ const char * * outval )
350
351
{
351
- int len = strlen (hdr );
352
- return !strncasecmp (line -> buf , hdr , len ) && line -> len > len &&
353
- line -> buf [len ] == ':' && isspace (line -> buf [len + 1 ]);
352
+ const char * val ;
353
+ if (!skip_iprefix (line -> buf , hdr , & val ) ||
354
+ * val ++ != ':' )
355
+ return 0 ;
356
+ while (isspace (* val ))
357
+ val ++ ;
358
+ * outval = val ;
359
+ return 1 ;
354
360
}
355
361
356
362
static int is_format_patch_separator (const char * line , int len )
@@ -543,49 +549,54 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
543
549
mi -> input_error = -1 ;
544
550
}
545
551
552
+ /*
553
+ * Returns true if "line" contains a header matching "hdr", in which case "val"
554
+ * will contain the value of the header with any RFC2047 B and Q encoding
555
+ * unwrapped, and optionally normalize the meta information to utf8.
556
+ */
557
+ static int parse_header (const struct strbuf * line ,
558
+ const char * hdr ,
559
+ struct mailinfo * mi ,
560
+ struct strbuf * val )
561
+ {
562
+ const char * val_str ;
563
+
564
+ if (!skip_header (line , hdr , & val_str ))
565
+ return 0 ;
566
+ strbuf_addstr (val , val_str );
567
+ decode_header (mi , val );
568
+ return 1 ;
569
+ }
570
+
546
571
static int check_header (struct mailinfo * mi ,
547
572
const struct strbuf * line ,
548
573
struct strbuf * hdr_data [], int overwrite )
549
574
{
550
- int i , ret = 0 , len ;
575
+ int i , ret = 0 ;
551
576
struct strbuf sb = STRBUF_INIT ;
552
577
553
578
/* search for the interesting parts */
554
579
for (i = 0 ; header [i ]; i ++ ) {
555
- int len = strlen (header [i ]);
556
- if ((!hdr_data [i ] || overwrite ) && cmp_header (line , header [i ])) {
557
- /* Unwrap inline B and Q encoding, and optionally
558
- * normalize the meta information to utf8.
559
- */
560
- strbuf_add (& sb , line -> buf + len + 2 , line -> len - len - 2 );
561
- decode_header (mi , & sb );
580
+ if ((!hdr_data [i ] || overwrite ) &&
581
+ parse_header (line , header [i ], mi , & sb )) {
562
582
handle_header (& hdr_data [i ], & sb );
563
583
ret = 1 ;
564
584
goto check_header_out ;
565
585
}
566
586
}
567
587
568
588
/* Content stuff */
569
- if (cmp_header (line , "Content-Type" )) {
570
- len = strlen ("Content-Type: " );
571
- strbuf_add (& sb , line -> buf + len , line -> len - len );
572
- decode_header (mi , & sb );
589
+ if (parse_header (line , "Content-Type" , mi , & sb )) {
573
590
handle_content_type (mi , & sb );
574
591
ret = 1 ;
575
592
goto check_header_out ;
576
593
}
577
- if (cmp_header (line , "Content-Transfer-Encoding" )) {
578
- len = strlen ("Content-Transfer-Encoding: " );
579
- strbuf_add (& sb , line -> buf + len , line -> len - len );
580
- decode_header (mi , & sb );
594
+ if (parse_header (line , "Content-Transfer-Encoding" , mi , & sb )) {
581
595
handle_content_transfer_encoding (mi , & sb );
582
596
ret = 1 ;
583
597
goto check_header_out ;
584
598
}
585
- if (cmp_header (line , "Message-Id" )) {
586
- len = strlen ("Message-Id: " );
587
- strbuf_add (& sb , line -> buf + len , line -> len - len );
588
- decode_header (mi , & sb );
599
+ if (parse_header (line , "Message-Id" , mi , & sb )) {
589
600
if (mi -> add_message_id )
590
601
mi -> message_id = strbuf_detach (& sb , NULL );
591
602
ret = 1 ;
@@ -606,8 +617,9 @@ static int is_inbody_header(const struct mailinfo *mi,
606
617
const struct strbuf * line )
607
618
{
608
619
int i ;
620
+ const char * val ;
609
621
for (i = 0 ; header [i ]; i ++ )
610
- if (!mi -> s_hdr_data [i ] && cmp_header (line , header [i ]))
622
+ if (!mi -> s_hdr_data [i ] && skip_header (line , header [i ], & val ))
611
623
return 1 ;
612
624
return 0 ;
613
625
}
0 commit comments