77#include "utf8.h"
88#include "strbuf.h"
99
10+ #define MAX_BOUNDARIES 5
11+
1012struct mailinfo {
1113 FILE * input ;
1214 FILE * output ;
@@ -22,6 +24,8 @@ struct mailinfo {
2224 int use_inbody_headers ;
2325 const char * metainfo_charset ;
2426
27+ struct strbuf * content [MAX_BOUNDARIES ];
28+ struct strbuf * * content_top ;
2529 struct strbuf charset ;
2630 char * message_id ;
2731 enum {
@@ -34,8 +38,6 @@ struct mailinfo {
3438 struct strbuf * * s_hdr_data ;
3539};
3640
37- #define MAX_BOUNDARIES 5
38-
3941static void cleanup_space (struct strbuf * sb )
4042{
4143 size_t pos , cnt ;
@@ -188,22 +190,18 @@ static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
188190 return 1 ;
189191}
190192
191- static struct strbuf * content [MAX_BOUNDARIES ];
192-
193- static struct strbuf * * content_top = content ;
194-
195193static void handle_content_type (struct mailinfo * mi , struct strbuf * line )
196194{
197195 struct strbuf * boundary = xmalloc (sizeof (struct strbuf ));
198196 strbuf_init (boundary , line -> len );
199197
200198 if (slurp_attr (line -> buf , "boundary=" , boundary )) {
201199 strbuf_insert (boundary , 0 , "--" , 2 );
202- if (++ content_top >= & content [MAX_BOUNDARIES ]) {
200+ if (++ mi -> content_top >= & mi -> content [MAX_BOUNDARIES ]) {
203201 fprintf (stderr , "Too many boundaries to handle\n" );
204202 exit (1 );
205203 }
206- * content_top = boundary ;
204+ * ( mi -> content_top ) = boundary ;
207205 boundary = NULL ;
208206 }
209207 slurp_attr (line -> buf , "charset=" , & mi -> charset );
@@ -231,10 +229,12 @@ static void handle_content_transfer_encoding(struct mailinfo *mi,
231229 mi -> transfer_encoding = TE_DONTCARE ;
232230}
233231
234- static int is_multipart_boundary (const struct strbuf * line )
232+ static int is_multipart_boundary (struct mailinfo * mi , const struct strbuf * line )
235233{
236- return (((* content_top )-> len <= line -> len ) &&
237- !memcmp (line -> buf , (* content_top )-> buf , (* content_top )-> len ));
234+ struct strbuf * content_top = * (mi -> content_top );
235+
236+ return ((content_top -> len <= line -> len ) &&
237+ !memcmp (line -> buf , content_top -> buf , content_top -> len ));
238238}
239239
240240static void cleanup_subject (struct mailinfo * mi , struct strbuf * subject )
@@ -797,7 +797,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
797797static int find_boundary (struct mailinfo * mi , struct strbuf * line )
798798{
799799 while (!strbuf_getline (line , mi -> input , '\n' )) {
800- if (* content_top && is_multipart_boundary (line ))
800+ if (* ( mi -> content_top ) && is_multipart_boundary (mi , line ))
801801 return 1 ;
802802 }
803803 return 0 ;
@@ -809,18 +809,18 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
809809
810810 strbuf_addch (& newline , '\n' );
811811again :
812- if (line -> len >= (* content_top )-> len + 2 &&
813- !memcmp (line -> buf + (* content_top )-> len , "--" , 2 )) {
812+ if (line -> len >= (* ( mi -> content_top ) )-> len + 2 &&
813+ !memcmp (line -> buf + (* ( mi -> content_top ) )-> len , "--" , 2 )) {
814814 /* we hit an end boundary */
815815 /* pop the current boundary off the stack */
816- strbuf_release (* content_top );
817- free (* content_top );
818- * content_top = NULL ;
816+ strbuf_release (* ( mi -> content_top ) );
817+ free (* ( mi -> content_top ) );
818+ * ( mi -> content_top ) = NULL ;
819819
820820 /* technically won't happen as is_multipart_boundary()
821821 will fail first. But just in case..
822822 */
823- if (-- content_top < content ) {
823+ if (-- mi -> content_top < mi -> content ) {
824824 fprintf (stderr , "Detected mismatched boundaries, "
825825 "can't recover\n" );
826826 exit (1 );
@@ -855,14 +855,14 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
855855 struct strbuf prev = STRBUF_INIT ;
856856
857857 /* Skip up to the first boundary */
858- if (* content_top ) {
858+ if (* ( mi -> content_top ) ) {
859859 if (!find_boundary (mi , line ))
860860 goto handle_body_out ;
861861 }
862862
863863 do {
864864 /* process any boundary lines */
865- if (* content_top && is_multipart_boundary (line )) {
865+ if (* ( mi -> content_top ) && is_multipart_boundary (mi , line )) {
866866 /* flush any leftover */
867867 if (prev .len ) {
868868 handle_filter (mi , & prev );
@@ -1026,6 +1026,7 @@ static void setup_mailinfo(struct mailinfo *mi)
10261026 strbuf_init (& mi -> charset , 0 );
10271027 mi -> header_stage = 1 ;
10281028 mi -> use_inbody_headers = 1 ;
1029+ mi -> content_top = mi -> content ;
10291030 git_config (git_mailinfo_config , & mi );
10301031}
10311032
@@ -1044,6 +1045,11 @@ static void clear_mailinfo(struct mailinfo *mi)
10441045 for (i = 0 ; mi -> s_hdr_data [i ]; i ++ )
10451046 strbuf_release (mi -> s_hdr_data [i ]);
10461047 free (mi -> s_hdr_data );
1048+
1049+ while (mi -> content < mi -> content_top ) {
1050+ free (* (mi -> content_top ));
1051+ mi -> content_top -- ;
1052+ }
10471053}
10481054
10491055static const char mailinfo_usage [] =
0 commit comments