]> diplodocus.org Git - nmh/blob - sbr/m_getfld.c
854965aaf2df5d28adffde9476d5f4d1dbffe7d8
[nmh] / sbr / m_getfld.c
1
2 /*
3 * m_getfld.c -- read/parse a message
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/mts.h>
12 #include <h/utils.h>
13
14 /* This module has a long and checkered history. First, it didn't burst
15 maildrops correctly because it considered two CTRL-A:s in a row to be
16 an inter-message delimiter. It really is four CTRL-A:s followed by a
17 newline. Unfortunately, MMDF will convert this delimiter *inside* a
18 message to a CTRL-B followed by three CTRL-A:s and a newline. This
19 caused the old version of m_getfld() to declare eom prematurely. The
20 fix was a lot slower than
21
22 c == '\001' && peekc (iob) == '\001'
23
24 but it worked, and to increase generality, MBOX style maildrops could
25 be parsed as well. Unfortunately the speed issue finally caught up with
26 us since this routine is at the very heart of MH.
27
28 To speed things up considerably, the routine Eom() was made an auxilary
29 function called by the macro eom(). Unless we are bursting a maildrop,
30 the eom() macro returns FALSE saying we aren't at the end of the
31 message.
32
33 The next thing to do is to read the mts.conf file and initialize
34 delimiter[] and delimlen accordingly...
35
36 After mhl was made a built-in in msh, m_getfld() worked just fine
37 (using m_unknown() at startup). Until one day: a message which was
38 the result of a bursting was shown. Then, since the burst boundaries
39 aren't CTRL-A:s, m_getfld() would blinding plunge on past the boundary.
40 Very sad. The solution: introduce m_eomsbr(). This hook gets called
41 after the end of each line (since testing for eom involves an fseek()).
42 This worked fine, until one day: a message with no body portion arrived.
43 Then the
44
45 while (eom (c = Getc (iob), iob))
46 continue;
47
48 loop caused m_getfld() to return FMTERR. So, that logic was changed to
49 check for (*eom_action) and act accordingly.
50
51 This worked fine, until one day: someone didn't use four CTRL:A's as
52 their delimiters. So, the bullet got bit and we read mts.h and
53 continue to struggle on. It's not that bad though, since the only time
54 the code gets executed is when inc (or msh) calls it, and both of these
55 have already called mts_init().
56
57 ------------------------
58 (Written by Van Jacobson for the mh6 m_getfld, January, 1986):
59
60 This routine was accounting for 60% of the cpu time used by most mh
61 programs. I spent a bit of time tuning and it now accounts for <10%
62 of the time used. Like any heavily tuned routine, it's a bit
63 complex and you want to be sure you understand everything that it's
64 doing before you start hacking on it. Let me try to emphasize
65 that: every line in this atrocity depends on every other line,
66 sometimes in subtle ways. You should understand it all, in detail,
67 before trying to change any part. If you do change it, test the
68 result thoroughly (I use a hand-constructed test file that exercises
69 all the ways a header name, header body, header continuation,
70 header-body separator, body line and body eom can align themselves
71 with respect to a buffer boundary). "Minor" bugs in this routine
72 result in garbaged or lost mail.
73
74 If you hack on this and slow it down, I, my children and my
75 children's children will curse you.
76
77 This routine gets used on three different types of files: normal,
78 single msg files, "packed" unix or mmdf mailboxs (when used by inc)
79 and packed, directoried bulletin board files (when used by msh).
80 The biggest impact of different file types is in "eom" testing. The
81 code has been carefully organized to test for eom at appropriate
82 times and at no other times (since the check is quite expensive).
83 I have tried to arrange things so that the eom check need only be
84 done on entry to this routine. Since an eom can only occur after a
85 newline, this is easy to manage for header fields. For the msg
86 body, we try to efficiently search the input buffer to see if
87 contains the eom delimiter. If it does, we take up to the
88 delimiter, otherwise we take everything in the buffer. (The change
89 to the body eom/copy processing produced the most noticeable
90 performance difference, particularly for "inc" and "show".)
91
92 There are three qualitatively different things this routine busts
93 out of a message: field names, field text and msg bodies. Field
94 names are typically short (~8 char) and the loop that extracts them
95 might terminate on a colon, newline or max width. I considered
96 using a Vax "scanc" to locate the end of the field followed by a
97 "bcopy" but the routine call overhead on a Vax is too large for this
98 to work on short names. If Berkeley ever makes "inline" part of the
99 C optimiser (so things like "scanc" turn into inline instructions) a
100 change here would be worthwhile.
101
102 Field text is typically 60 - 100 characters so there's (barely)
103 a win in doing a routine call to something that does a "locc"
104 followed by a "bmove". About 30% of the fields have continuations
105 (usually the 822 "received:" lines) and each continuation generates
106 another routine call. "Inline" would be a big win here, as well.
107
108 Messages, as of this writing, seem to come in two flavors: small
109 (~1K) and long (>2K). Most messages have 400 - 600 bytes of headers
110 so message bodies average at least a few hundred characters.
111 Assuming your system uses reasonably sized stdio buffers (1K or
112 more), this routine should be able to remove the body in large
113 (>500 byte) chunks. The makes the cost of a call to "bcopy"
114 small but there is a premium on checking for the eom in packed
115 maildrops. The eom pattern is always a simple string so we can
116 construct an efficient pattern matcher for it (e.g., a Vax "matchc"
117 instruction). Some thought went into recognizing the start of
118 an eom that has been split across two buffers.
119
120 This routine wants to deal with large chunks of data so, rather
121 than "getc" into a local buffer, it uses stdio's buffer. If
122 you try to use it on a non-buffered file, you'll get what you
123 deserve. This routine "knows" that struct FILEs have a _ptr
124 and a _cnt to describe the current state of the buffer and
125 it knows that _filbuf ignores the _ptr & _cnt and simply fills
126 the buffer. If stdio on your system doesn't work this way, you
127 may have to make small changes in this routine.
128
129 This routine also "knows" that an EOF indication on a stream is
130 "sticky" (i.e., you will keep getting EOF until you reposition the
131 stream). If your system doesn't work this way it is broken and you
132 should complain to the vendor. As a consequence of the sticky
133 EOF, this routine will never return any kind of EOF status when
134 there is data in "name" or "buf").
135 */
136
137 /*
138 Purpose
139 =======
140 Reads an Internet message (RFC 5322), or one or more messages stored in a
141 maildrop in mbox (RFC 4155) or MMDF format, from a file stream. Each call
142 to m_getfld() reads one header field, or a portion of the body, in sequence.
143
144 Inputs
145 ======
146 state: message parse state
147 bufsz: maximum number of characters to load into buf
148 iob: input file stream
149
150 Outputs
151 =======
152 name: header field name (array of size NAMESZ=999)
153 buf: either a header field body or message body
154 bufsz: number of characters loaded into buf
155 (return value): message parse state on return from function
156
157 Functions
158 =========
159 void m_unknown(FILE *iob): Determines the message delimiter string for the
160 maildrop. Called by inc, scan, and msh when reading from a maildrop file.
161
162 void m_eomsbr (int (*action)(int)): Sets the hook to check for end of
163 message in a maildrop. Called only by msh.
164
165 Those functions save state in the State variables listed below.
166
167 State variables
168 ===============
169 m_getfld() retains state internally between calls in some state variables.
170 These are used for detecting the end of each message when reading maildrops:
171 static unsigned char **pat_map
172 static unsigned char *fdelim
173 static unsigned char *delimend
174 static int fdelimlen
175 static unsigned char *edelim
176 static int edelimlen
177 char * msg_delim
178 int msg_style
179 int (*eom_action)(int)
180
181 Restrictions
182 ============
183 m_getfld() is restricted to operate on one file stream at a time
184 because of the retained state (see "State variables" above). And the
185 first call to m_getfld() on that file stream requires that the read
186 pointer be at the beginning of the file (ftell() of 0).
187
188 Current usage
189 =============
190 The first call to m_getfld() on a file stream is with a state of FLD.
191 Subsequent calls provide the state returned by the previous call.
192 Therefore, given the Restrictions above, the state variable could be
193 removed from the signature and just retained internally.
194 */
195
196 /*
197 * static prototypes
198 */
199 static int m_Eom (int, FILE *);
200 static unsigned char *matchc(int, char *, int, char *);
201
202 #define eom(c,iob) (msg_style != MS_DEFAULT && \
203 (((c) == *msg_delim && m_Eom(c,iob)) ||\
204 (eom_action && (*eom_action)(c))))
205
206 static unsigned char **pat_map;
207
208 static int msg_style = MS_DEFAULT;
209
210 /*
211 * The "full" delimiter string for a packed maildrop consists
212 * of a newline followed by the actual delimiter. E.g., the
213 * full string for a Unix maildrop would be: "\n\nFrom ".
214 * "Fdelim" points to the start of the full string and is used
215 * in the BODY case of the main routine to search the buffer for
216 * a possible eom. Msg_delim points to the first character of
217 * the actual delim. string (i.e., fdelim+1). Edelim
218 * points to the 2nd character of actual delimiter string. It
219 * is used in m_Eom because the first character of the string
220 * has been read and matched before m_Eom is called.
221 */
222 static char *msg_delim = "";
223 static unsigned char *fdelim;
224 static unsigned char *delimend;
225 static int fdelimlen;
226 static unsigned char *edelim;
227 static int edelimlen;
228
229 static int (*eom_action)(int) = NULL;
230
231 /* This replaces the old approach, with its direct access to stdio
232 * internals. It uses one fread() to load a buffer that we manage.
233 *
234 * MSG_INPUT_SIZE is the size of the buffer.
235 * MAX_DELIMITER_SIZE is the maximum size of the delimiter used to
236 * separate messages in a maildrop, such as mbox "\nFrom ".
237 *
238 * test/inc/lots_of_headers.txt depends on the particular value of
239 * MSG_INPUT_SIZE. */
240 #define MSG_INPUT_SIZE 8192
241 #define MAX_DELIMITER_SIZE 32
242 static struct m_getfld_buffer {
243 unsigned char msg_buf[2 * MSG_INPUT_SIZE + MAX_DELIMITER_SIZE];
244 unsigned char *readpos;
245 unsigned char *end; /* One past the last character read in. */
246 /* The following support tracking of the read position in the
247 input file stream so that callers can interleave m_getfld()
248 calls with ftell() and fseek(). ytes_read replaces the old
249 m_getfld() msg_count global. last_file_pos is stored when
250 leaving m_getfld()/m_unkown(), then checked on the next entry.
251 last_internal_pos is used to remember the position used
252 internally by m_getfld() (read_more(), actually). */
253 off_t bytes_read;
254 off_t total_bytes_read;
255 off_t last_file_pos;
256 off_t last_internal_pos;
257 } m;
258
259 static void
260 enter_getfld (FILE *iob, struct m_getfld_buffer *m) {
261 off_t pos = ftello (iob);
262
263 /* Rely on Restriction that the first call to m_getfld (), etc.,
264 is with the read position for the file stream set to 0. */
265 if (pos == 0) {
266 /* A new file stream, so reset the buffer state. */
267 m->readpos = m->end = m->msg_buf;
268 m->total_bytes_read = 0;
269 m->last_file_pos = m->last_internal_pos = ftello (iob);
270 } else {
271 /* If the current file stream position differs from the last one,
272 then caller must have called ftell(), so adjust. */
273 if (pos != m->last_file_pos) {
274 size_t num_read;
275
276 /* Opportunity for optimization here: don't reread if the
277 new position had already been read into the buffer,
278 just move m->readpos to it. */
279 fseeko (iob, 0, SEEK_SET);
280 do {
281 num_read = fread (m->msg_buf, 1, MSG_INPUT_SIZE, iob);
282 pos -= num_read;
283 } while (pos > 0);
284 pos += num_read;
285 /* assert (ftello (iob) == pos); */
286 m->readpos = m->msg_buf + pos;
287 m->end = m->msg_buf + num_read;
288 m->last_internal_pos = m->last_file_pos = pos;
289 m->total_bytes_read = pos;
290 } else {
291 /* Restore the file position that we use for the input buffer. */
292 pos = m->last_internal_pos;
293 }
294
295 fseeko (iob, pos, SEEK_SET);
296 }
297
298 m->bytes_read = 0;
299 }
300
301 static void
302 leave_getfld (struct m_getfld_buffer *m, FILE *iob) {
303 /* Save the internal file position that we use for the input buffer. */
304 m->last_internal_pos = ftello (iob);
305
306 /* Set file stream position so that callers can use ftell (). */
307 m->total_bytes_read += m->bytes_read;
308 fseeko (iob, m->total_bytes_read, SEEK_SET);
309 m->last_file_pos = ftello (iob);
310 }
311
312 static size_t
313 read_more (struct m_getfld_buffer *m, FILE *iob) {
314 /* Retain at least edelimlen characters that have already been
315 read so that we can back up to them in m_Eom(). */
316 ssize_t retain = edelimlen;
317 size_t num_read;
318
319 if (retain < m->end - m->readpos) retain = m->end - m->readpos;
320 /* assert (retain <= m->readpos - m->msg_buf <= sizeof msg_buf); */
321
322 /* Move what we want to retain at end of the buffer to the beginning. */
323 memmove (m->msg_buf, m->readpos - retain, retain);
324
325 m->readpos = m->msg_buf + retain;
326 num_read = fread (m->readpos, 1, MSG_INPUT_SIZE, iob);
327 m->end = m->readpos + num_read;
328
329 return num_read;
330 }
331
332 static int
333 Getc (FILE *iob) {
334 if (m.end - m.readpos < 1) {
335 if (read_more (&m, iob) == 0) {
336 /* Pretend that we read a character. That's what stdio does. */
337 ++m.readpos;
338 return EOF;
339 }
340 }
341
342 ++m.bytes_read;
343 return m.readpos < m.end ? *m.readpos++ : EOF;
344 }
345
346 static int
347 Peek (FILE *iob) {
348 int next_char = Getc (iob);
349 --m.readpos;
350 --m.bytes_read;
351
352 return next_char;
353 }
354
355 static int
356 Ungetc (int c, FILE *iob) {
357 NMH_UNUSED (iob);
358
359 if (m.readpos == m.msg_buf) {
360 return EOF;
361 } else {
362 --m.bytes_read;
363 return *--m.readpos = c;
364 }
365 }
366
367
368 int
369 m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
370 int *bufsz, FILE *iob)
371 {
372 register unsigned char *bp, *cp, *ep, *sp;
373 register int cnt, c, i, j;
374
375 enter_getfld (iob, &m);
376
377 if ((c = Getc(iob)) < 0) {
378 *bufsz = *buf = 0;
379 leave_getfld (&m, iob);
380 return FILEEOF;
381 }
382 if (eom (c, iob)) {
383 if (! eom_action) {
384 /* flush null messages */
385 while ((c = Getc(iob)) >= 0 && eom (c, iob))
386 ;
387
388 if (c >= 0)
389 Ungetc(c, iob);
390 }
391 *bufsz = *buf = 0;
392 leave_getfld (&m, iob);
393 return FILEEOF;
394 }
395
396 switch (state) {
397 case FLDEOF:
398 case BODYEOF:
399 case FLD:
400 if (c == '\n' || c == '-') {
401 /* we hit the header/body separator */
402 while (c != '\n' && (c = Getc(iob)) >= 0) continue;
403
404 if (c < 0 || (c = Getc(iob)) < 0 || eom (c, iob)) {
405 if (! eom_action) {
406 /* flush null messages */
407 while ((c = Getc(iob)) >= 0 && eom (c, iob))
408 ;
409 if (c >= 0)
410 Ungetc(c, iob);
411 }
412 *bufsz = *buf = 0;
413 leave_getfld (&m, iob);
414 return FILEEOF;
415 }
416 state = BODY;
417 goto body;
418 }
419 /*
420 * get the name of this component. take characters up
421 * to a ':', a newline or NAMESZ-1 characters, whichever
422 * comes first.
423 */
424 cp = name;
425 i = NAMESZ - 1;
426 for (;;) {
427 /* Get the field name. The first time through the
428 loop, this copies out the first character, which
429 was loaded into c prior to loop entry. Initialize
430 j to 1 to account for that. */
431 for (j = 1;
432 c != ':' && c != '\n' && c != EOF && j <= i;
433 ++j, c = Getc (iob)) {
434 *cp++ = c;
435 }
436
437 /* Skip next character, which is either the space
438 after the ':' or the first folded whitespace. */
439 {
440 int next_char;
441 if (c == EOF || (next_char = Peek (iob)) == EOF) {
442 *bufsz = *cp = *buf = 0;
443 advise (NULL, "eof encountered in field \"%s\"", name);
444 leave_getfld (&m, iob);
445 return FMTERR;
446 }
447 }
448 if (c == ':')
449 break;
450
451 /*
452 * something went wrong. possibilities are:
453 * . hit a newline (error)
454 * . got more than namesz chars. (error)
455 * . hit the end of the buffer. (loop)
456 */
457 if (c == '\n') {
458 /* We hit the end of the line without seeing ':' to
459 * terminate the field name. This is usually (always?)
460 * spam. But, blowing up is lame, especially when
461 * scan(1)ing a folder with such messages. Pretend such
462 * lines are the first of the body (at least mutt also
463 * handles it this way). */
464
465 /* See if buf can hold this line, since we were assuming
466 * we had a buffer of NAMESZ, not bufsz. */
467 /* + 1 for the newline */
468
469 if (*bufsz < j + 1) {
470 /* No, it can't. Oh well, guess we'll blow up. */
471 *bufsz = *cp = *buf = 0;
472 advise (NULL, "eol encountered in field \"%s\"", name);
473 state = FMTERR;
474 goto finish;
475 }
476 memcpy (buf, name, j - 1);
477 buf[j - 1] = '\n';
478 buf[j] = '\0';
479 /* The last character read was '\n'. m.bytes_read (and j)
480 include that, but it was not put into the name array
481 in the for loop above. So subtract 1. */
482 *bufsz = --m.bytes_read; /* == j - 1 */
483 leave_getfld (&m, iob);
484 return BODY;
485 }
486 if ((i -= j) <= 0) {
487 *bufsz = *cp = *buf = 0;
488 advise (NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
489 state = LENERR;
490 goto finish;
491 }
492 }
493
494 /* Trim any trailing spaces from the end of name. */
495 while (isspace (*--cp) && cp >= name) continue;
496 *++cp = 0;
497 /* c is ':' here. And readpos points to the first
498 character of the field body. */
499 /* fall through */
500
501 case FLDPLUS:
502 /*
503 * get (more of) the text of a field. Take
504 * characters up to the end of this field (newline
505 * followed by non-blank) or bufsz-1 characters.
506 */
507 cp = buf;
508 i = *bufsz-1;
509 j = 0;
510 for (;;) {
511 while (c != '\n' && c != EOF && j++ < i) {
512 *cp++ = c = Getc (iob);
513 }
514
515 if (c != EOF) c = Peek (iob);
516 if (i < j) {
517 /* the dest buffer is full */
518 *bufsz = m.bytes_read;
519 state = FLDPLUS;
520 goto finish;
521 } else if (c != ' ' && c != '\t') {
522 /* The next character is not folded whitespace, so
523 prepare to move on to the next field. It's OK
524 if c is EOF, it will be handled on the next
525 call to m_getfld (). */
526 *bufsz = m.bytes_read;
527 state = FLD;
528 goto finish;
529 } else {
530 /* Folded header field, continues on the next line. */
531 continue;
532 }
533 }
534 *bufsz = m.bytes_read;
535 break;
536
537 case BODY:
538 body:
539 /*
540 * get the message body up to bufsz characters or the
541 * end of the message.
542 */
543 i = *bufsz-1;
544 /* Back up and store the current position and update cnt. */
545 bp = --m.readpos;
546 cnt = m.end - m.readpos;
547 c = cnt < i ? cnt : i;
548 if (msg_style != MS_DEFAULT && c > 1) {
549 /*
550 * packed maildrop - only take up to the (possible)
551 * start of the next message. This "matchc" should
552 * probably be a Boyer-Moore matcher for non-vaxen,
553 * particularly since we have the alignment table
554 * all built for the end-of-buffer test (next).
555 * But our vax timings indicate that the "matchc"
556 * instruction is 50% faster than a carefully coded
557 * B.M. matcher for most strings. (So much for elegant
558 * algorithms vs. brute force.) Since I (currently)
559 * run MH on a vax, we use the matchc instruction. --vj
560 */
561 if ((ep = matchc( fdelimlen, fdelim, c, bp )))
562 c = ep - bp + 1;
563 else {
564 /*
565 * There's no delim in the buffer but there may be
566 * a partial one at the end. If so, we want to leave
567 * it so the "eom" check on the next call picks it up.
568 * Use a modified Boyer-Moore matcher to make this
569 * check relatively cheap. The first "if" figures
570 * out what position in the pattern matches the last
571 * character in the buffer. The inner "while" matches
572 * the pattern against the buffer, backwards starting
573 * at that position. Note that unless the buffer
574 * ends with one of the characters in the pattern
575 * (excluding the first and last), we do only one test.
576 */
577 ep = bp + c - 1;
578 if ((sp = pat_map[*ep])) {
579 do {
580 /* This if() is true unless (a) the buffer is too
581 * small to contain this delimiter prefix, or
582 * (b) it contains exactly enough chars for the
583 * delimiter prefix.
584 * For case (a) obviously we aren't going to match.
585 * For case (b), if the buffer really contained exactly
586 * a delim prefix, then the m_eom call at entry
587 * should have found it. Thus it's not a delim
588 * and we know we won't get a match.
589 */
590 if (((sp - fdelim) + 2) <= c) {
591 cp = sp;
592 /* Unfortunately although fdelim has a preceding NUL
593 * we can't use this as a sentinel in case the buffer
594 * contains a NUL in exactly the wrong place (this
595 * would cause us to run off the front of fdelim).
596 */
597 while (*--ep == *--cp)
598 if (cp < fdelim)
599 break;
600 if (cp < fdelim) {
601 /* we matched the entire delim prefix,
602 * so only take the buffer up to there.
603 * we know ep >= bp -- check above prevents underrun
604 */
605 c = (ep - bp) + 2;
606 break;
607 }
608 }
609 /* try matching one less char of delim string */
610 ep = bp + c - 1;
611 } while (--sp > fdelim);
612 }
613 }
614 }
615 memcpy( buf, bp, c );
616 /* Advance the current position to reflect the copy out.
617 c is less than or equal to the number of bytes remaining
618 in the read buffer, so will not overrun it. */
619 m.readpos += c;
620 cp = buf + c;
621 /* Less 1 from c because the first character was read by Getc(),
622 and therefore already accounted for in m.bytes_read. */
623 m.bytes_read += c - 1;
624 *bufsz = m.bytes_read;
625 break;
626
627 default:
628 adios (NULL, "m_getfld() called with bogus state of %d", state);
629 }
630 finish:
631 *cp = 0;
632
633 leave_getfld (&m, iob);
634 return (state);
635 }
636
637
638 void
639 m_unknown(FILE *iob)
640 {
641 register int c;
642 char text[MAX_DELIMITER_SIZE];
643 register char *cp;
644 register char *delimstr;
645
646 enter_getfld (iob, &m);
647
648 /*
649 * Figure out what the message delimitter string is for this
650 * maildrop. (This used to be part of m_Eom but I didn't like
651 * the idea of an "if" statement that could only succeed on the
652 * first call to m_Eom getting executed on each call, i.e., at
653 * every newline in the message).
654 *
655 * If the first line of the maildrop is a Unix "From " line, we
656 * say the style is MBOX and eat the rest of the line. Otherwise
657 * we say the style is MMDF and look for the delimiter string
658 * specified when nmh was built (or from the mts.conf file).
659 */
660
661 msg_style = MS_UNKNOWN;
662
663 for (c = 0, cp = text; c < 5; ++c, ++cp) {
664 if ((*cp = Getc (iob)) == EOF) {
665 break;
666 }
667 }
668
669 if (c == 5 && strncmp (text, "From ", 5) == 0) {
670 msg_style = MS_MBOX;
671 delimstr = "\nFrom ";
672 while ((c = Getc (iob)) != '\n' && c >= 0) continue;
673 /* m_unknown is only called on maildrop files, and they are only
674 read using m_getfld (). The caller musn't try to read from
675 the stream directly because the file position indicator was
676 not advanced based on bytes_read, but instead on whatever
677 was read into the message buffer. */
678 } else {
679 /* not a Unix style maildrop */
680 m.readpos -= m.bytes_read;
681 if (mmdlm2 == NULL || *mmdlm2 == 0)
682 mmdlm2 = "\001\001\001\001\n";
683 delimstr = mmdlm2;
684 msg_style = MS_MMDF;
685 }
686 c = strlen (delimstr);
687 fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
688 *fdelim++ = '\0';
689 *fdelim = '\n';
690 msg_delim = (char *)fdelim+1;
691 edelim = (unsigned char *)msg_delim+1;
692 fdelimlen = c + 1;
693 edelimlen = c - 1; /* == strlen (delimstr) */
694 strcpy (msg_delim, delimstr);
695 delimend = (unsigned char *)msg_delim + edelimlen;
696 if (edelimlen <= 1)
697 adios (NULL, "maildrop delimiter must be at least 2 bytes");
698 /*
699 * build a Boyer-Moore end-position map for the matcher in m_getfld.
700 * N.B. - we don't match just the first char (since it's the newline
701 * separator) or the last char (since the matchc would have found it
702 * if it was a real delim).
703 */
704 pat_map = (unsigned char **) calloc (256, sizeof(unsigned char *));
705
706 for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ )
707 pat_map[(unsigned char)*cp] = (unsigned char *) cp;
708
709 if (msg_style == MS_MMDF) {
710 /* flush extra msg hdrs */
711 while ((c = Getc(iob)) >= 0 && eom (c, iob))
712 ;
713 if (c >= 0)
714 Ungetc(c, iob);
715 }
716
717 leave_getfld (&m, iob);
718 }
719
720
721 void
722 m_eomsbr (int (*action)(int))
723 {
724 if ((eom_action = action)) {
725 msg_style = MS_MSH;
726 *msg_delim = 0;
727 fdelimlen = 1;
728 delimend = fdelim;
729 } else {
730 msg_style = MS_MMDF;
731 msg_delim = (char *)fdelim + 1;
732 fdelimlen = strlen((char *)fdelim);
733 delimend = (unsigned char *)(msg_delim + edelimlen);
734 }
735 }
736
737
738 /*
739 * test for msg delimiter string
740 */
741
742 static int
743 m_Eom (int c, FILE *iob)
744 {
745 register int i;
746 char text[MAX_DELIMITER_SIZE];
747 char *cp;
748
749 for (i = 0, cp = text; i < edelimlen; ++i, ++cp) {
750 if ((*cp = Getc (iob)) == EOF) {
751 break;
752 }
753 }
754
755 if (i != edelimlen || strncmp (text, (char *)edelim, edelimlen)) {
756 if (i == 0 && msg_style == MS_MBOX)
757 /* the final newline in the (brain damaged) unix-format
758 * maildrop is part of the delimitter - delete it.
759 */
760 return 1;
761
762 /* Did not find delimiter, so restore the read position.
763 Note that on input, a character had already been read
764 with Getc(). It will be unget by m_getfld () on return. */
765 m.readpos -= m.bytes_read - 1;
766 return 0;
767 }
768
769 if (msg_style == MS_MBOX) {
770 while ((c = Getc (iob)) != '\n')
771 if (c < 0)
772 break;
773 }
774
775 return 1;
776 }
777
778
779 static unsigned char *
780 matchc(int patln, char *pat, int strln, char *str)
781 {
782 register char *es = str + strln - patln;
783 register char *sp;
784 register char *pp;
785 register char *ep = pat + patln;
786 register char pc = *pat++;
787
788 for(;;) {
789 while (pc != *str++)
790 if (str > es)
791 return 0;
792 if (str > es+1)
793 return 0;
794 sp = str; pp = pat;
795 while (pp < ep && *sp++ == *pp)
796 pp++;
797 if (pp >= ep)
798 return ((unsigned char *)--str);
799 }
800 }