@matheusbsbs / matheus-bagliano / commits / 3b6121f69b
Fork 0

git-mailinfo: use strbuf's instead of fixed buffers

Signed-off-by: Lukas Sandström <lukass@etek.chalmers.se> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lukas Sandström committed Jul 13, 2008 at 20:30 UTC 3b6121f69b2a27c3bbff5cc3056c30b15394441b
1 file changed +352 -405
builtin-mailinfo.c
+352 -405
@@ -5,14 +5,15 @@
5 #include "cache.h"
6 #include "builtin.h"
7 #include "utf8.h"
8 +#include "strbuf.h"
9
10 static FILE *cmitmsg, *patchfile, *fin, *fout;
11
12 static int keep_subject;
13 static const char *metainfo_charset;
13 -static char line[1000];
14 -static char name[1000];
15 -static char email[1000];
14 +static struct strbuf line = STRBUF_INIT;
15 +static struct strbuf name = STRBUF_INIT;
16 +static struct strbuf email = STRBUF_INIT;
17
18 static enum {
19 TE_DONTCARE, TE_QP, TE_BASE64,
@@ -21,74 +22,79 @@ static enum {
22 TYPE_TEXT, TYPE_OTHER,
23 } message_type;
24
24 -static char charset[256];
25 +static struct strbuf charset = STRBUF_INIT;
26 static int patch_lines;
26 -static char **p_hdr_data, **s_hdr_data;
27 +static struct strbuf **p_hdr_data, **s_hdr_data;
28
29 #define MAX_HDR_PARSED 10
30 #define MAX_BOUNDARIES 5
31
31 -static char *sanity_check(char *name, char *email)
32 +static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
33 {
33 - int len = strlen(name);
34 - if (len < 3 || len > 60)
35 - return email;
36 - if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
37 - return email;
38 - return name;
34 + struct strbuf *src = name;
35 + if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
36 + strchr(name->buf, '<') || strchr(name->buf, '>'))
37 + src = email;
38 + else if (name == out)
39 + return;
40 + strbuf_reset(out);
41 + strbuf_addbuf(out, src);
42 }
43
41 -static int bogus_from(char *line)
44 +static void parse_bogus_from(const struct strbuf *line)
45 {
46 /* John Doe <johndoe> */
44 - char *bra, *ket, *dst, *cp;
47
48 + char *bra, *ket;
49 /* This is fallback, so do not bother if we already have an
50 * e-mail address.
51 */
49 - if (*email)
50 - return 0;
52 + if (email.len)
53 + return;
54
52 - bra = strchr(line, '<');
55 + bra = strchr(line->buf, '<');
56 if (!bra)
54 - return 0;
57 + return;
58 ket = strchr(bra, '>');
59 if (!ket)
57 - return 0;
60 + return;
61
59 - for (dst = email, cp = bra+1; cp < ket; )
60 - *dst++ = *cp++;
61 - *dst = 0;
62 - for (cp = line; isspace(*cp); cp++)
63 - ;
64 - for (bra--; isspace(*bra); bra--)
65 - *bra = 0;
66 - cp = sanity_check(cp, email);
67 - strcpy(name, cp);
68 - return 1;
62 + strbuf_reset(&email);
63 + strbuf_add(&email, bra + 1, ket - bra - 1);
64 +
65 + strbuf_reset(&name);
66 + strbuf_add(&name, line->buf, bra - line->buf);
67 + strbuf_trim(&name);
68 + get_sane_name(&name, &name, &email);
69 }
70
71 -static int handle_from(char *in_line)
71 +static void handle_from(const struct strbuf *from)
72 {
73 - char line[1000];
73 char *at;
75 - char *dst;
74 + size_t el;
75 + struct strbuf f;
76
77 - strcpy(line, in_line);
78 - at = strchr(line, '@');
79 - if (!at)
80 - return bogus_from(line);
77 + strbuf_init(&f, from->len);
78 + strbuf_addbuf(&f, from);
79 +
80 + at = strchr(f.buf, '@');
81 + if (!at) {
82 + parse_bogus_from(from);
83 + return;
84 + }
85
86 /*
87 * If we already have one email, don't take any confusing lines
88 */
85 - if (*email && strchr(at+1, '@'))
86 - return 0;
89 + if (email.len && strchr(at + 1, '@')) {
90 + strbuf_release(&f);
91 + return;
92 + }
93
94 /* Pick up the string around '@', possibly delimited with <>
89 - * pair; that is the email part. White them out while copying.
95 + * pair; that is the email part.
96 */
91 - while (at > line) {
97 + while (at > f.buf) {
98 char c = at[-1];
99 if (isspace(c))
100 break;
@@ -98,56 +104,35 @@ static int handle_from(char *in_line)
104 }
105 at--;
106 }
101 - dst = email;
102 - for (;;) {
103 - unsigned char c = *at;
104 - if (!c || c == '>' || isspace(c)) {
105 - if (c == '>')
106 - *at = ' ';
107 - break;
108 - }
109 - *at++ = ' ';
110 - *dst++ = c;
111 - }
112 - *dst++ = 0;
107 + el = strcspn(at, " \n\t\r\v\f>");
108 + strbuf_reset(&email);
109 + strbuf_add(&email, at, el);
110 + strbuf_remove(&f, at - f.buf, el + 1);
111
112 /* The remainder is name. It could be "John Doe <john.doe@xz>"
115 - * or "john.doe@xz (John Doe)", but we have whited out the
113 + * or "john.doe@xz (John Doe)", but we have removed the
114 * email part, so trim from both ends, possibly removing
115 * the () pair at the end.
116 */
119 - at = line + strlen(line);
120 - while (at > line) {
121 - unsigned char c = *--at;
122 - if (!isspace(c)) {
123 - at[(c == ')') ? 0 : 1] = 0;
124 - break;
125 - }
126 - }
117 + strbuf_trim(&f);
118 + if (f.buf[0] == '(')
119 + strbuf_remove(&name, 0, 1);
120 + if (f.len && f.buf[f.len - 1] == ')')
121 + strbuf_setlen(&f, f.len - 1);
122
128 - at = line;
129 - for (;;) {
130 - unsigned char c = *at;
131 - if (!c || !isspace(c)) {
132 - if (c == '(')
133 - at++;
134 - break;
135 - }
136 - at++;
137 - }
138 - at = sanity_check(at, email);
139 - strcpy(name, at);
140 - return 1;
123 + get_sane_name(&name, &f, &email);
124 + strbuf_release(&f);
125 }
126
143 -static int handle_header(char *line, char *data, int ofs)
127 +static void handle_header(struct strbuf **out, const struct strbuf *line)
128 {
145 - if (!line || !data)
146 - return 1;
129 + if (!*out) {
130 + *out = xmalloc(sizeof(struct strbuf));
131 + strbuf_init(*out, line->len);
132 + } else
133 + strbuf_reset(*out);
134
148 - strcpy(data, line+ofs);
149 -
150 - return 0;
135 + strbuf_addbuf(*out, line);
136 }
137
138 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
@@ -156,13 +141,13 @@ static int handle_header(char *line, char *data, int ofs)
141 * case insensitively.
142 */
143
159 -static int slurp_attr(const char *line, const char *name, char *attr)
144 +static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
145 {
146 const char *ends, *ap = strcasestr(line, name);
147 size_t sz;
148
149 if (!ap) {
165 - *attr = 0;
150 + strbuf_setlen(attr, 0);
151 return 0;
152 }
153 ap += strlen(name);
@@ -173,180 +158,172 @@ static int slurp_attr(const char *line, const char *name, char *attr)
158 else
159 ends = "; \t";
160 sz = strcspn(ap, ends);
176 - memcpy(attr, ap, sz);
177 - attr[sz] = 0;
161 + strbuf_add(attr, ap, sz);
162 return 1;
163 }
164
181 -struct content_type {
182 - char *boundary;
183 - int boundary_len;
184 -};
185 -
186 -static struct content_type content[MAX_BOUNDARIES];
165 +static struct strbuf *content[MAX_BOUNDARIES];
166
188 -static struct content_type *content_top = content;
167 +static struct strbuf **content_top = content;
168
190 -static int handle_content_type(char *line)
169 +static void handle_content_type(struct strbuf *line)
170 {
192 - char boundary[256];
171 + struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
172 + strbuf_init(boundary, line->len);
173
194 - if (strcasestr(line, "text/") == NULL)
174 + if (!strcasestr(line->buf, "text/"))
175 message_type = TYPE_OTHER;
196 - if (slurp_attr(line, "boundary=", boundary + 2)) {
197 - memcpy(boundary, "--", 2);
176 + if (slurp_attr(line->buf, "boundary=", boundary)) {
177 + strbuf_insert(boundary, 0, "--", 2);
178 if (content_top++ >= &content[MAX_BOUNDARIES]) {
179 fprintf(stderr, "Too many boundaries to handle\n");
180 exit(1);
181 }
202 - content_top->boundary_len = strlen(boundary);
203 - content_top->boundary = xmalloc(content_top->boundary_len+1);
204 - strcpy(content_top->boundary, boundary);
182 + *content_top = boundary;
183 + boundary = NULL;
184 }
206 - if (slurp_attr(line, "charset=", charset)) {
207 - int i, c;
208 - for (i = 0; (c = charset[i]) != 0; i++)
209 - charset[i] = tolower(c);
185 + if (slurp_attr(line->buf, "charset=", &charset))
186 + strbuf_tolower(&charset);
187 +
188 + if (boundary) {
189 + strbuf_release(boundary);
190 + free(boundary);
191 }
211 - return 0;
192 }
193
214 -static int handle_content_transfer_encoding(char *line)
194 +static void handle_content_transfer_encoding(const struct strbuf *line)
195 {
216 - if (strcasestr(line, "base64"))
196 + if (strcasestr(line->buf, "base64"))
197 transfer_encoding = TE_BASE64;
218 - else if (strcasestr(line, "quoted-printable"))
198 + else if (strcasestr(line->buf, "quoted-printable"))
199 transfer_encoding = TE_QP;
200 else
201 transfer_encoding = TE_DONTCARE;
222 - return 0;
202 }
203
225 -static int is_multipart_boundary(const char *line)
204 +static int is_multipart_boundary(const struct strbuf *line)
205 {
227 - return (!memcmp(line, content_top->boundary, content_top->boundary_len));
206 + return !strbuf_cmp(line, *content_top);
207 }
208
230 -static int eatspace(char *line)
209 +static void cleanup_subject(struct strbuf *subject)
210 {
232 - int len = strlen(line);
233 - while (len > 0 && isspace(line[len-1]))
234 - line[--len] = 0;
235 - return len;
236 -}
237 -
238 -static char *cleanup_subject(char *subject)
239 -{
240 - for (;;) {
241 - char *p;
242 - int len, remove;
243 - switch (*subject) {
211 + char *pos;
212 + size_t remove;
213 + while (subject->len) {
214 + switch (*subject->buf) {
215 case 'r': case 'R':
245 - if (!memcmp("e:", subject+1, 2)) {
246 - subject += 3;
216 + if (subject->len <= 3)
217 + break;
218 + if (!memcmp(subject->buf + 1, "e:", 2)) {
219 + strbuf_remove(subject, 0, 3);
220 continue;
221 }
222 break;
223 case ' ': case '\t': case ':':
251 - subject++;
224 + strbuf_remove(subject, 0, 1);
225 continue;
253 -
226 case '[':
255 - p = strchr(subject, ']');
256 - if (!p) {
257 - subject++;
258 - continue;
259 - }
260 - len = strlen(p);
261 - remove = p - subject;
262 - if (remove <= len *2) {
263 - subject = p+1;
264 - continue;
265 - }
227 + if ((pos = strchr(subject->buf, ']'))) {
228 + remove = pos - subject->buf + 1;
229 + /* Don't remove too much. */
230 + if (remove <= (subject->len - remove + 1) * 2) {
231 + strbuf_remove(subject, 0, remove);
232 + continue;
233 + }
234 + } else
235 + strbuf_remove(subject, 0, 1);
236 break;
237 }
268 - eatspace(subject);
269 - return subject;
238 + strbuf_trim(subject);
239 + return;
240 }
241 }
242
273 -static void cleanup_space(char *buf)
243 +static void cleanup_space(struct strbuf *sb)
244 {
275 - unsigned char c;
276 - while ((c = *buf) != 0) {
277 - buf++;
278 - if (isspace(c)) {
279 - buf[-1] = ' ';
280 - c = *buf;
281 - while (isspace(c)) {
282 - int len = strlen(buf);
283 - memmove(buf, buf+1, len);
284 - c = *buf;
285 - }
245 + size_t pos, cnt;
246 + for (pos = 0; pos < sb->len; pos++) {
247 + if (isspace(sb->buf[pos])) {
248 + sb->buf[pos] = ' ';
249 + for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
250 + strbuf_remove(sb, pos + 1, cnt);
251 }
252 }
253 }
254
290 -static void decode_header(char *it, unsigned itsize);
255 +static void decode_header(struct strbuf *line);
256 static const char *header[MAX_HDR_PARSED] = {
257 "From","Subject","Date",
258 };
259
295 -static int check_header(char *line, unsigned linesize, char **hdr_data, int overwrite)
260 +static inline int cmp_header(const struct strbuf *line, const char *hdr)
261 {
297 - int i;
262 + int len = strlen(hdr);
263 + return !strncasecmp(line->buf, hdr, len) && line->len > len &&
264 + line->buf[len] == ':' && isspace(line->buf[len + 1]);
265 +}
266
267 +static int check_header(const struct strbuf *line,
268 + struct strbuf *hdr_data[], int overwrite)
269 +{
270 + int i, ret = 0, len;
271 + struct strbuf sb = STRBUF_INIT;
272 /* search for the interesting parts */
273 for (i = 0; header[i]; i++) {
274 int len = strlen(header[i]);
302 - if ((!hdr_data[i] || overwrite) &&
303 - !strncasecmp(line, header[i], len) &&
304 - line[len] == ':' && isspace(line[len + 1])) {
275 + if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
276 /* Unwrap inline B and Q encoding, and optionally
277 * normalize the meta information to utf8.
278 */
308 - decode_header(line + len + 2, linesize - len - 2);
309 - hdr_data[i] = xmalloc(1000 * sizeof(char));
310 - if (! handle_header(line, hdr_data[i], len + 2)) {
311 - return 1;
312 - }
279 + strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
280 + decode_header(&sb);
281 + handle_header(&hdr_data[i], &sb);
282 + ret = 1;
283 + goto check_header_out;
284 }
285 }
286
287 /* Content stuff */
317 - if (!strncasecmp(line, "Content-Type", 12) &&
318 - line[12] == ':' && isspace(line[12 + 1])) {
319 - decode_header(line + 12 + 2, linesize - 12 - 2);
320 - if (! handle_content_type(line)) {
321 - return 1;
322 - }
323 - }
324 - if (!strncasecmp(line, "Content-Transfer-Encoding", 25) &&
325 - line[25] == ':' && isspace(line[25 + 1])) {
326 - decode_header(line + 25 + 2, linesize - 25 - 2);
327 - if (! handle_content_transfer_encoding(line)) {
328 - return 1;
329 - }
288 + if (cmp_header(line, "Content-Type")) {
289 + len = strlen("Content-Type: ");
290 + strbuf_add(&sb, line->buf + len, line->len - len);
291 + decode_header(&sb);
292 + strbuf_insert(&sb, 0, "Content-Type: ", len);
293 + handle_content_type(&sb);
294 + ret = 1;
295 + goto check_header_out;
296 + }
297 + if (cmp_header(line, "Content-Transfer-Encoding")) {
298 + len = strlen("Content-Transfer-Encoding: ");
299 + strbuf_add(&sb, line->buf + len, line->len - len);
300 + decode_header(&sb);
301 + handle_content_transfer_encoding(&sb);
302 + ret = 1;
303 + goto check_header_out;
304 }
305
306 /* for inbody stuff */
333 - if (!memcmp(">From", line, 5) && isspace(line[5]))
334 - return 1;
335 - if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
307 + if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) {
308 + ret = 1; /* Should this return 0? */
309 + goto check_header_out;
310 + }
311 + if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) {
312 for (i = 0; header[i]; i++) {
337 - if (!memcmp("Subject: ", header[i], 9)) {
338 - if (! handle_header(line, hdr_data[i], 0)) {
339 - return 1;
340 - }
313 + if (!memcmp("Subject", header[i], 7)) {
314 + handle_header(&hdr_data[i], line);
315 + ret = 1;
316 + goto check_header_out;
317 }
318 }
319 }
320
345 - /* no match */
346 - return 0;
321 +check_header_out:
322 + strbuf_release(&sb);
323 + return ret;
324 }
325
349 -static int is_rfc2822_header(char *line)
326 +static int is_rfc2822_header(const struct strbuf *line)
327 {
328 /*
329 * The section that defines the loosest possible
@@ -357,15 +334,15 @@ static int is_rfc2822_header(char *line)
334 * ftext = %d33-57 / %59-126
335 */
336 int ch;
360 - char *cp = line;
337 + char *cp = line->buf;
338
339 /* Count mbox From headers as headers */
363 - if (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6))
340 + if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From "))
341 return 1;
342
343 while ((ch = *cp++)) {
344 if (ch == ':')
368 - return cp != line;
345 + return 1;
346 if ((33 <= ch && ch <= 57) ||
347 (59 <= ch && ch <= 126))
348 continue;
@@ -374,34 +351,20 @@ static int is_rfc2822_header(char *line)
351 return 0;
352 }
353
377 -/*
378 - * sz is size of 'line' buffer in bytes. Must be reasonably
379 - * long enough to hold one physical real-world e-mail line.
380 - */
381 -static int read_one_header_line(char *line, int sz, FILE *in)
354 +static int read_one_header_line(struct strbuf *line, FILE *in)
355 {
383 - int len;
384 -
385 - /*
386 - * We will read at most (sz-1) bytes and then potentially
387 - * re-add NUL after it. Accessing line[sz] after this is safe
388 - * and we can allow len to grow up to and including sz.
389 - */
390 - sz--;
391 -
356 /* Get the first part of the line. */
393 - if (!fgets(line, sz, in))
357 + if (strbuf_getline(line, in, '\n'))
358 return 0;
359
360 /*
361 * Is it an empty line or not a valid rfc2822 header?
362 * If so, stop here, and return false ("not a header")
363 */
400 - len = eatspace(line);
401 - if (!len || !is_rfc2822_header(line)) {
364 + strbuf_rtrim(line);
365 + if (!line->len || !is_rfc2822_header(line)) {
366 /* Re-add the newline */
403 - line[len] = '\n';
404 - line[len + 1] = '\0';
367 + strbuf_addch(line, '\n');
368 return 0;
369 }
370
@@ -410,65 +373,53 @@ static int read_one_header_line(char *line, int sz, FILE *in)
373 * Yuck, 2822 header "folding"
374 */
375 for (;;) {
413 - int peek, addlen;
414 - static char continuation[1000];
376 + int peek;
377 + struct strbuf continuation = STRBUF_INIT;
378
379 peek = fgetc(in); ungetc(peek, in);
380 if (peek != ' ' && peek != '\t')
381 break;
419 - if (!fgets(continuation, sizeof(continuation), in))
382 + if (strbuf_getline(&continuation, in, '\n'))
383 break;
421 - addlen = eatspace(continuation);
422 - if (len < sz - 1) {
423 - if (addlen >= sz - len)
424 - addlen = sz - len - 1;
425 - memcpy(line + len, continuation, addlen);
426 - line[len] = '\n';
427 - len += addlen;
428 - }
384 + continuation.buf[0] = '\n';
385 + strbuf_rtrim(&continuation);
386 + strbuf_addbuf(line, &continuation);
387 }
430 - line[len] = 0;
388
389 return 1;
390 }
391
435 -static int decode_q_segment(char *in, char *ot, unsigned otsize, char *ep, int rfc2047)
392 +static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
393 {
437 - char *otbegin = ot;
438 - char *otend = ot + otsize;
394 + const char *in = q_seg->buf;
395 int c;
440 - while ((c = *in++) != 0 && (in <= ep)) {
441 - if (ot == otend) {
442 - *--ot = '\0';
443 - return -1;
444 - }
396 + struct strbuf *out = xmalloc(sizeof(struct strbuf));
397 + strbuf_init(out, q_seg->len);
398 +
399 + while ((c = *in++) != 0) {
400 if (c == '=') {
401 int d = *in++;
402 if (d == '\n' || !d)
403 break; /* drop trailing newline */
449 - *ot++ = ((hexval(d) << 4) | hexval(*in++));
404 + strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
405 continue;
406 }
407 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
408 c = 0x20;
454 - *ot++ = c;
409 + strbuf_addch(out, c);
410 }
456 - *ot = 0;
457 - return (ot - otbegin);
411 + return out;
412 }
413
460 -static int decode_b_segment(char *in, char *ot, unsigned otsize, char *ep)
414 +static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
415 {
416 /* Decode in..ep, possibly in-place to ot */
417 int c, pos = 0, acc = 0;
464 - char *otbegin = ot;
465 - char *otend = ot + otsize;
418 + const char *in = b_seg->buf;
419 + struct strbuf *out = xmalloc(sizeof(struct strbuf));
420 + strbuf_init(out, b_seg->len);
421
467 - while ((c = *in++) != 0 && (in <= ep)) {
468 - if (ot == otend) {
469 - *--ot = '\0';
470 - return -1;
471 - }
422 + while ((c = *in++) != 0) {
423 if (c == '+')
424 c = 62;
425 else if (c == '/')
@@ -493,21 +444,20 @@ static int decode_b_segment(char *in, char *ot, unsigned otsize, char *ep)
444 acc = (c << 2);
445 break;
446 case 1:
496 - *ot++ = (acc | (c >> 4));
447 + strbuf_addch(out, (acc | (c >> 4)));
448 acc = (c & 15) << 4;
449 break;
450 case 2:
500 - *ot++ = (acc | (c >> 2));
451 + strbuf_addch(out, (acc | (c >> 2)));
452 acc = (c & 3) << 6;
453 break;
454 case 3:
504 - *ot++ = (acc | c);
455 + strbuf_addch(out, (acc | c));
456 acc = pos = 0;
457 break;
458 }
459 }
509 - *ot = 0;
510 - return (ot - otbegin);
460 + return out;
461 }
462
463 /*
@@ -521,16 +471,16 @@ static int decode_b_segment(char *in, char *ot, unsigned otsize, char *ep)
471 * Otherwise, we default to assuming it is Latin1 for historical
472 * reasons.
473 */
524 -static const char *guess_charset(const char *line, const char *target_charset)
474 +static const char *guess_charset(const struct strbuf *line, const char *target_charset)
475 {
476 if (is_encoding_utf8(target_charset)) {
527 - if (is_utf8(line))
477 + if (is_utf8(line->buf))
478 return NULL;
479 }
480 return "latin1";
481 }
482
533 -static void convert_to_utf8(char *line, unsigned linesize, const char *charset)
483 +static void convert_to_utf8(struct strbuf *line, const char *charset)
484 {
485 char *out;
486
@@ -542,112 +492,118 @@ static void convert_to_utf8(char *line, unsigned linesize, const char *charset)
492
493 if (!strcmp(metainfo_charset, charset))
494 return;
545 - out = reencode_string(line, metainfo_charset, charset);
495 + out = reencode_string(line->buf, metainfo_charset, charset);
496 if (!out)
497 die("cannot convert from %s to %s\n",
498 charset, metainfo_charset);
549 - strlcpy(line, out, linesize);
550 - free(out);
499 + strbuf_attach(line, out, strlen(out), strlen(out));
500 }
501
553 -static int decode_header_bq(char *it, unsigned itsize)
502 +static int decode_header_bq(struct strbuf *it)
503 {
555 - char *in, *out, *ep, *cp, *sp;
556 - char outbuf[1000];
504 + char *in, *ep, *cp;
505 + struct strbuf outbuf = STRBUF_INIT, *dec;
506 + struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
507 int rfc2047 = 0;
508
559 - in = it;
560 - out = outbuf;
561 - while ((ep = strstr(in, "=?")) != NULL) {
562 - int sz, encoding;
563 - char charset_q[256], piecebuf[256];
509 + in = it->buf;
510 + while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
511 + int encoding;
512 + strbuf_reset(&charset_q);
513 + strbuf_reset(&piecebuf);
514 rfc2047 = 1;
515
516 if (in != ep) {
567 - sz = ep - in;
568 - memcpy(out, in, sz);
569 - out += sz;
570 - in += sz;
517 + strbuf_add(&outbuf, in, ep - in);
518 + in = ep;
519 }
520 /* E.g.
521 * ep : "=?iso-2022-jp?B?GyR...?= foo"
522 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
523 */
524 ep += 2;
577 - cp = strchr(ep, '?');
578 - if (!cp)
579 - return rfc2047; /* no munging */
580 - for (sp = ep; sp < cp; sp++)
581 - charset_q[sp - ep] = tolower(*sp);
582 - charset_q[cp - ep] = 0;
525 +
526 + if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
527 + goto decode_header_bq_out;
528 +
529 + if (cp + 3 - it->buf > it->len)
530 + goto decode_header_bq_out;
531 + strbuf_add(&charset_q, ep, cp - ep);
532 + strbuf_tolower(&charset_q);
533 +
534 encoding = cp[1];
535 if (!encoding || cp[2] != '?')
585 - return rfc2047; /* no munging */
536 + goto decode_header_bq_out;
537 ep = strstr(cp + 3, "?=");
538 if (!ep)
588 - return rfc2047; /* no munging */
539 + goto decode_header_bq_out;
540 + strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
541 switch (tolower(encoding)) {
542 default:
591 - return rfc2047; /* no munging */
543 + goto decode_header_bq_out;
544 case 'b':
593 - sz = decode_b_segment(cp + 3, piecebuf, sizeof(piecebuf), ep);
545 + dec = decode_b_segment(&piecebuf);
546 break;
547 case 'q':
596 - sz = decode_q_segment(cp + 3, piecebuf, sizeof(piecebuf), ep, 1);
548 + dec = decode_q_segment(&piecebuf, 1);
549 break;
550 }
599 - if (sz < 0)
600 - return rfc2047;
551 if (metainfo_charset)
602 - convert_to_utf8(piecebuf, sizeof(piecebuf), charset_q);
552 + convert_to_utf8(dec, charset_q.buf);
553
604 - sz = strlen(piecebuf);
605 - if (outbuf + sizeof(outbuf) <= out + sz)
606 - return rfc2047; /* no munging */
607 - strcpy(out, piecebuf);
608 - out += sz;
554 + strbuf_addbuf(&outbuf, dec);
555 + strbuf_release(dec);
556 + free(dec);
557 in = ep + 2;
558 }
611 - strcpy(out, in);
612 - strlcpy(it, outbuf, itsize);
559 + strbuf_addstr(&outbuf, in);
560 + strbuf_reset(it);
561 + strbuf_addbuf(it, &outbuf);
562 +decode_header_bq_out:
563 + strbuf_release(&outbuf);
564 + strbuf_release(&charset_q);
565 + strbuf_release(&piecebuf);
566 return rfc2047;
567 }
568
616 -static void decode_header(char *it, unsigned itsize)
569 +static void decode_header(struct strbuf *it)
570 {
618 -
619 - if (decode_header_bq(it, itsize))
571 + if (decode_header_bq(it))
572 return;
573 /* otherwise "it" is a straight copy of the input.
574 * This can be binary guck but there is no charset specified.
575 */
576 if (metainfo_charset)
625 - convert_to_utf8(it, itsize, "");
577 + convert_to_utf8(it, "");
578 }
579
628 -static int decode_transfer_encoding(char *line, unsigned linesize, int inputlen)
580 +static void decode_transfer_encoding(struct strbuf *line)
581 {
630 - char *ep;
582 + struct strbuf *ret;
583
584 switch (transfer_encoding) {
585 case TE_QP:
634 - ep = line + inputlen;
635 - return decode_q_segment(line, line, linesize, ep, 0);
586 + ret = decode_q_segment(line, 0);
587 + break;
588 case TE_BASE64:
637 - ep = line + inputlen;
638 - return decode_b_segment(line, line, linesize, ep);
589 + ret = decode_b_segment(line);
590 + break;
591 case TE_DONTCARE:
592 default:
641 - return inputlen;
593 + return;
594 }
595 + strbuf_reset(line);
596 + strbuf_addbuf(line, ret);
597 + strbuf_release(ret);
598 + free(ret);
599 }
600
645 -static int handle_filter(char *line, unsigned linesize, int linelen);
601 +static void handle_filter(struct strbuf *line);
602
603 static int find_boundary(void)
604 {
649 - while(fgets(line, sizeof(line), fin) != NULL) {
650 - if (is_multipart_boundary(line))
605 + while (!strbuf_getline(&line, fin, '\n')) {
606 + if (is_multipart_boundary(&line))
607 return 1;
608 }
609 return 0;
@@ -655,12 +611,17 @@ static int find_boundary(void)
611
612 static int handle_boundary(void)
613 {
658 - char newline[]="\n";
614 + struct strbuf newline = STRBUF_INIT;
615 +
616 + strbuf_addch(&newline, '\n');
617 again:
660 - if (!memcmp(line+content_top->boundary_len, "--", 2)) {
618 + if (line.len >= (*content_top)->len + 2 &&
619 + !memcmp(line.buf + (*content_top)->len, "--", 2)) {
620 /* we hit an end boundary */
621 /* pop the current boundary off the stack */
663 - free(content_top->boundary);
622 + strbuf_release(*content_top);
623 + free(*content_top);
624 + *content_top = NULL;
625
626 /* technically won't happen as is_multipart_boundary()
627 will fail first. But just in case..
@@ -670,7 +631,8 @@ again:
631 "can't recover\n");
632 exit(1);
633 }
673 - handle_filter(newline, sizeof(newline), strlen(newline));
634 + handle_filter(&newline);
635 + strbuf_release(&newline);
636
637 /* skip to the next boundary */
638 if (!find_boundary())
@@ -680,39 +642,44 @@ again:
642
643 /* set some defaults */
644 transfer_encoding = TE_DONTCARE;
683 - charset[0] = 0;
645 + strbuf_reset(&charset);
646 message_type = TYPE_TEXT;
647
648 /* slurp in this section's info */
687 - while (read_one_header_line(line, sizeof(line), fin))
688 - check_header(line, sizeof(line), p_hdr_data, 0);
649 + while (read_one_header_line(&line, fin))
650 + check_header(&line, p_hdr_data, 0);
651
652 + strbuf_release(&newline);
653 /* eat the blank line after section info */
691 - return (fgets(line, sizeof(line), fin) != NULL);
654 + return (strbuf_getline(&line, fin, '\n') == 0);
655 }
656
694 -static inline int patchbreak(const char *line)
657 +static inline int patchbreak(const struct strbuf *line)
658 {
659 + size_t i;
660 +
661 /* Beginning of a "diff -" header? */
697 - if (!memcmp("diff -", line, 6))
662 + if (!prefixcmp(line->buf, "diff -"))
663 return 1;
664
665 /* CVS "Index: " line? */
701 - if (!memcmp("Index: ", line, 7))
666 + if (!prefixcmp(line->buf, "Index: "))
667 return 1;
668
669 /*
670 * "--- <filename>" starts patches without headers
671 * "---<sp>*" is a manual separator
672 */
708 - if (!memcmp("---", line, 3)) {
709 - line += 3;
673 + if (line->len < 4)
674 + return 0;
675 +
676 + if (!prefixcmp(line->buf, "---")) {
677 /* space followed by a filename? */
711 - if (line[0] == ' ' && !isspace(line[1]))
678 + if (line->buf[3] == ' ' && !isspace(line->buf[4]))
679 return 1;
680 /* Just whitespace? */
714 - for (;;) {
715 - unsigned char c = *line++;
681 + for (i = 3; i < line->len; i++) {
682 + unsigned char c = line->buf[i];
683 if (c == '\n')
684 return 1;
685 if (!isspace(c))
@@ -723,32 +690,24 @@ static inline int patchbreak(const char *line)
690 return 0;
691 }
692
726 -
727 -static int handle_commit_msg(char *line, unsigned linesize)
693 +static int handle_commit_msg(struct strbuf *line)
694 {
695 static int still_looking = 1;
730 - char *endline = line + linesize;
696
697 if (!cmitmsg)
698 return 0;
699
700 if (still_looking) {
736 - char *cp = line;
737 - if (isspace(*line)) {
738 - for (cp = line + 1; *cp; cp++) {
739 - if (!isspace(*cp))
740 - break;
741 - }
742 - if (!*cp)
743 - return 0;
744 - }
745 - if ((still_looking = check_header(cp, endline - cp, s_hdr_data, 0)) != 0)
701 + strbuf_ltrim(line);
702 + if (!line->len)
703 + return 0;
704 + if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
705 return 0;
706 }
707
708 /* normalize the log message to UTF-8. */
709 if (metainfo_charset)
751 - convert_to_utf8(line, endline - line, charset);
710 + convert_to_utf8(line, charset.buf);
711
712 if (patchbreak(line)) {
713 fclose(cmitmsg);
@@ -756,142 +715,132 @@ static int handle_commit_msg(char *line, unsigned linesize)
715 return 1;
716 }
717
759 - fputs(line, cmitmsg);
718 + fputs(line->buf, cmitmsg);
719 return 0;
720 }
721
763 -static int handle_patch(char *line, int len)
722 +static void handle_patch(const struct strbuf *line)
723 {
765 - fwrite(line, 1, len, patchfile);
724 + fwrite(line->buf, 1, line->len, patchfile);
725 patch_lines++;
767 - return 0;
726 }
727
770 -static int handle_filter(char *line, unsigned linesize, int linelen)
728 +static void handle_filter(struct strbuf *line)
729 {
730 static int filter = 0;
731
774 - /* filter tells us which part we left off on
775 - * a non-zero return indicates we hit a filter point
776 - */
732 + /* filter tells us which part we left off on */
733 switch (filter) {
734 case 0:
779 - if (!handle_commit_msg(line, linesize))
735 + if (!handle_commit_msg(line))
736 break;
737 filter++;
738 case 1:
783 - if (!handle_patch(line, linelen))
784 - break;
785 - filter++;
786 - default:
787 - return 1;
739 + handle_patch(line);
740 + break;
741 }
789 -
790 - return 0;
742 }
743
744 static void handle_body(void)
745 {
795 - int rc = 0;
796 - static char newline[2000];
797 - static char *np = newline;
798 - int len = strlen(line);
746 + int len = 0;
747 + struct strbuf prev = STRBUF_INIT;
748
749 /* Skip up to the first boundary */
801 - if (content_top->boundary) {
750 + if (*content_top) {
751 if (!find_boundary())
803 - return;
752 + goto handle_body_out;
753 }
754
755 do {
756 + strbuf_setlen(&line, line.len + len);
757 +
758 /* process any boundary lines */
808 - if (content_top->boundary && is_multipart_boundary(line)) {
759 + if (*content_top && is_multipart_boundary(&line)) {
760 /* flush any leftover */
810 - if (np != newline)
811 - handle_filter(newline, sizeof(newline),
812 - np - newline);
761 + if (line.len)
762 + handle_filter(&line);
763 +
764 if (!handle_boundary())
814 - return;
815 - len = strlen(line);
765 + goto handle_body_out;
766 }
767
768 /* Unwrap transfer encoding */
819 - len = decode_transfer_encoding(line, sizeof(line), len);
820 - if (len < 0) {
821 - error("Malformed input line");
822 - return;
823 - }
769 + decode_transfer_encoding(&line);
770
771 switch (transfer_encoding) {
772 case TE_BASE64:
773 case TE_QP:
774 {
829 - char *op = line;
775 + struct strbuf **lines, **it, *sb;
776 +
777 + /* Prepend any previous partial lines */
778 + strbuf_insert(&line, 0, prev.buf, prev.len);
779 + strbuf_reset(&prev);
780
781 /* binary data most likely doesn't have newlines */
782 if (message_type != TYPE_TEXT) {
833 - rc = handle_filter(line, sizeof(line), len);
783 + handle_filter(&line);
784 break;
785 }
836 -
786 /*
787 * This is a decoded line that may contain
788 * multiple new lines. Pass only one chunk
789 * at a time to handle_filter()
790 */
842 - do {
843 - while (op < line + len && *op != '\n')
844 - *np++ = *op++;
845 - *np = *op;
846 - if (*np != 0) {
847 - /* should be sitting on a new line */
848 - *(++np) = 0;
849 - op++;
850 - rc = handle_filter(newline, sizeof(newline), np - newline);
851 - np = newline;
852 - }
853 - } while (op < line + len);
791 + lines = strbuf_split(&line, '\n');
792 + for (it = lines; (sb = *it); it++) {
793 + if (*(it + 1) == NULL) /* The last line */
794 + if (sb->buf[sb->len - 1] != '\n') {
795 + /* Partial line, save it for later. */
796 + strbuf_addbuf(&prev, sb);
797 + break;
798 + }
799 + handle_filter(sb);
800 + }
801 /*
855 - * The partial chunk is saved in newline and will be
802 + * The partial chunk is saved in "prev" and will be
803 * appended by the next iteration of read_line_with_nul().
804 */
805 + strbuf_list_free(lines);
806 break;
807 }
808 default:
861 - rc = handle_filter(line, sizeof(line), len);
809 + handle_filter(&line);
810 }
863 - if (rc)
864 - /* nothing left to filter */
865 - break;
866 - } while ((len = read_line_with_nul(line, sizeof(line), fin)));
811
868 - return;
812 + strbuf_reset(&line);
813 + if (strbuf_avail(&line) < 100)
814 + strbuf_grow(&line, 100);
815 + } while ((len = read_line_with_nul(line.buf, strbuf_avail(&line), fin)));
816 +
817 +handle_body_out:
818 + strbuf_release(&prev);
819 }
820
871 -static void output_header_lines(FILE *fout, const char *hdr, char *data)
821 +static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
822 {
823 + const char *sp = data->buf;
824 while (1) {
874 - char *ep = strchr(data, '\n');
825 + char *ep = strchr(sp, '\n');
826 int len;
827 if (!ep)
877 - len = strlen(data);
828 + len = strlen(sp);
829 else
879 - len = ep - data;
880 - fprintf(fout, "%s: %.*s\n", hdr, len, data);
830 + len = ep - sp;
831 + fprintf(fout, "%s: %.*s\n", hdr, len, sp);
832 if (!ep)
833 break;
883 - data = ep + 1;
834 + sp = ep + 1;
835 }
836 }
837
838 static void handle_info(void)
839 {
889 - char *sub;
890 - char *hdr;
840 + struct strbuf *hdr;
841 int i;
842
843 for (i = 0; header[i]; i++) {
894 -
844 /* only print inbody headers if we output a patch file */
845 if (patch_lines && s_hdr_data[i])
846 hdr = s_hdr_data[i];
@@ -901,20 +850,18 @@ static void handle_info(void)
850 continue;
851
852 if (!memcmp(header[i], "Subject", 7)) {
904 - if (keep_subject)
905 - sub = hdr;
906 - else {
907 - sub = cleanup_subject(hdr);
908 - cleanup_space(sub);
853 + if (!keep_subject) {
854 + cleanup_subject(hdr);
855 + cleanup_space(hdr);
856 }
910 - output_header_lines(fout, "Subject", sub);
857 + output_header_lines(fout, "Subject", hdr);
858 } else if (!memcmp(header[i], "From", 4)) {
859 handle_from(hdr);
913 - fprintf(fout, "Author: %s\n", name);
914 - fprintf(fout, "Email: %s\n", email);
860 + fprintf(fout, "Author: %s\n", name.buf);
861 + fprintf(fout, "Email: %s\n", email.buf);
862 } else {
863 cleanup_space(hdr);
917 - fprintf(fout, "%s: %s\n", header[i], hdr);
864 + fprintf(fout, "%s: %s\n", header[i], hdr->buf);
865 }
866 }
867 fprintf(fout, "\n");
@@ -941,8 +888,8 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
888 return -1;
889 }
890
944 - p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
945 - s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(char *));
891 + p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data));
892 + s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
893
894 do {
895 peek = fgetc(in);
@@ -950,8 +897,8 @@ static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
897 ungetc(peek, in);
898
899 /* process the email header */
953 - while (read_one_header_line(line, sizeof(line), fin))
954 - check_header(line, sizeof(line), p_hdr_data, 1);
900 + while (read_one_header_line(&line, fin))
901 + check_header(&line, p_hdr_data, 1);
902
903 handle_body();
904 handle_info();