Moved #endif so that lockit () is only declared with DOT_LOCKING,
[mmh] / sbr / m_getfld.c
1 /*
2 ** m_getfld.c -- read/parse a message
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 /*
13 ** This module has a long and checkered history.
14 **
15 ** [ Here had been some history of delimiter problems in MMDF maildrops ... ]
16 **
17 ** Unfortunately the speed issue finally caught up with us since this
18 ** routine is at the very heart of MH.  To speed things up considerably, the
19 ** routine Eom() was made an auxilary function called by the macro eom().
20 ** Unless we are bursting a maildrop, the eom() macro returns FALSE saying
21 ** we aren't at the end of the message.
22 **
23 ** [ ... and here had been some more of it. ]
24 **
25 **
26 ** ------------------------
27 ** (Written by Van Jacobson for the mh6 m_getfld, January, 1986):
28 **
29 ** This routine was accounting for 60% of the cpu time used by most mh
30 ** programs.  I spent a bit of time tuning and it now accounts for <10%
31 ** of the time used.  Like any heavily tuned routine, it's a bit
32 ** complex and you want to be sure you understand everything that it's
33 ** doing before you start hacking on it.  Let me try to emphasize
34 ** that:  every line in this atrocity depends on every other line,
35 ** sometimes in subtle ways.  You should understand it all, in detail,
36 ** before trying to change any part.  If you do change it, test the
37 ** result thoroughly (I use a hand-constructed test file that exercises
38 ** all the ways a header name, header body, header continuation,
39 ** header-body separator, body line and body eom can align themselves
40 ** with respect to a buffer boundary).  "Minor" bugs in this routine
41 ** result in garbaged or lost mail.
42 **
43 ** If you hack on this and slow it down, I, my children and my
44 ** children's children will curse you.
45 **
46 ** This routine gets used on two different types of files: normal,
47 ** single msg files and "packed" unix mailboxs (when used by inc).
48 ** The biggest impact of different file types is in "eom" testing.  The
49 ** code has been carefully organized to test for eom at appropriate
50 ** times and at no other times (since the check is quite expensive).
51 ** I have tried to arrange things so that the eom check need only be
52 ** done on entry to this routine.  Since an eom can only occur after a
53 ** newline, this is easy to manage for header fields.  For the msg
54 ** body, we try to efficiently search the input buffer to see if
55 ** contains the eom delimiter.  If it does, we take up to the
56 ** delimiter, otherwise we take everything in the buffer.  (The change
57 ** to the body eom/copy processing produced the most noticeable
58 ** performance difference, particularly for "inc" and "show".)
59 **
60 ** There are three qualitatively different things this routine busts
61 ** out of a message: field names, field text and msg bodies.  Field
62 ** names are typically short (~8 char) and the loop that extracts them
63 ** might terminate on a colon, newline or max width.  I considered
64 ** using a Vax "scanc" to locate the end of the field followed by a
65 ** "bcopy" but the routine call overhead on a Vax is too large for this
66 ** to work on short names.  If Berkeley ever makes "inline" part of the
67 ** C optimiser (so things like "scanc" turn into inline instructions) a
68 ** change here would be worthwhile.
69 **
70 ** Field text is typically 60 - 100 characters so there's (barely)
71 ** a win in doing a routine call to something that does a "locc"
72 ** followed by a "bmove".  About 30% of the fields have continuations
73 ** (usually the 822 "received:" lines) and each continuation generates
74 ** another routine call.  "Inline" would be a big win here, as well.
75 **
76 ** Messages, as of this writing, seem to come in two flavors: small
77 ** (~1K) and long (>2K).  Most messages have 400 - 600 bytes of headers
78 ** so message bodies average at least a few hundred characters.
79 ** Assuming your system uses reasonably sized stdio buffers (1K or
80 ** more), this routine should be able to remove the body in large
81 ** (>500 byte) chunks.  The makes the cost of a call to "bcopy"
82 ** small but there is a premium on checking for the eom in packed
83 ** maildrops.  The eom pattern is always a simple string so we can
84 ** construct an efficient pattern matcher for it (e.g., a Vax "matchc"
85 ** instruction).  Some thought went into recognizing the start of
86 ** an eom that has been split across two buffers.
87 **
88 ** This routine wants to deal with large chunks of data so, rather
89 ** than "getc" into a local buffer, it uses stdio's buffer.  If
90 ** you try to use it on a non-buffered file, you'll get what you
91 ** deserve.  This routine "knows" that struct FILEs have a _ptr
92 ** and a _cnt to describe the current state of the buffer and
93 ** it knows that _filbuf ignores the _ptr & _cnt and simply fills
94 ** the buffer.  If stdio on your system doesn't work this way, you
95 ** may have to make small changes in this routine.
96 **
97 ** This routine also "knows" that an EOF indication on a stream is
98 ** "sticky" (i.e., you will keep getting EOF until you reposition the
99 ** stream).  If your system doesn't work this way it is broken and you
100 ** should complain to the vendor.  As a consequence of the sticky
101 ** EOF, this routine will never return any kind of EOF status when
102 ** there is data in "name" or "buf").
103 */
104
105
106 /*
107 ** static prototypes
108 */
109 static int m_Eom(int, FILE *);
110 static unsigned char *matchc(int, char *, int, char *);
111 static unsigned char *locc(int, unsigned char *, unsigned char);
112
113 #define eom(c,iob)  (ismbox && \
114         (((c) == *msg_delim && m_Eom(c,iob)) ||\
115         (eom_action && (*eom_action)(c))))
116
117 static unsigned char **pat_map;
118
119 /*
120 ** This is a disgusting hack for "inc" so it can know how many
121 ** characters were stuffed in the buffer on the last call
122 ** (see comments in uip/scansbr.c).
123 */
124 int msg_count = 0;
125
126 int ismbox = FALSE;
127
128 /*
129 ** The "full" delimiter string for a packed maildrop consists
130 ** of a newline followed by the actual delimiter.  E.g., the
131 ** full string for a Unix maildrop would be: "\n\nFrom ".
132 ** "Fdelim" points to the start of the full string and is used
133 ** in the BODY case of the main routine to search the buffer for
134 ** a possible eom.  Msg_delim points to the first character of
135 ** the actual delim. string (i.e., fdelim+1).  Edelim
136 ** points to the 2nd character of actual delimiter string.  It
137 ** is used in m_Eom because the first character of the string
138 ** has been read and matched before m_Eom is called.
139 */
140 char *msg_delim = "";
141
142 static unsigned char *fdelim;
143 static unsigned char *delimend;
144 static int fdelimlen;
145 static unsigned char *edelim;
146 static int edelimlen;
147
148 static int (*eom_action)(int) = NULL;
149
150 #ifdef _FSTDIO
151 # define _ptr _p  /* Gag   */
152 # define _cnt _r  /* Retch */
153 # define _filbuf __srget  /* Puke  */
154 # define DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
155 #endif
156
157 #ifdef SCO_5_STDIO
158 # define _ptr  __ptr
159 # define _cnt  __cnt
160 # define _base __base
161 # define _filbuf(fp)  ((fp)->__cnt = 0, __filbuf(fp))
162 # define DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
163 #endif
164
165 #ifndef DEFINED__FILBUF_TO_SOMETHING_SPECIFIC
166 extern int  _filbuf(FILE*);
167 #endif
168
169
170 int
171 m_getfld(int state, unsigned char *name, unsigned char *buf,
172         int bufsz, FILE *iob)
173 {
174         register unsigned char  *bp, *cp, *ep, *sp;
175         register int cnt, c, i, j;
176
177         if ((c = getc(iob)) < 0) {
178                 msg_count = 0;
179                 *buf = 0;
180                 return FILEEOF;
181         }
182         if (eom(c, iob)) {
183                 if (! eom_action) {
184                         /* flush null messages */
185                         while ((c = getc(iob)) >= 0 && eom(c, iob))
186                                 ;
187                         if (c >= 0)
188                                 ungetc(c, iob);
189                 }
190                 msg_count = 0;
191                 *buf = 0;
192                 return FILEEOF;
193         }
194
195         switch (state) {
196         case FLDEOF:
197         case BODYEOF:
198         case FLD:
199                 if (c == '\n' || c == '-') {
200                         /* we hit the header/body separator */
201                         while (c != '\n' && (c = getc(iob)) >= 0)
202                                 ;
203
204                         if (c < 0 || (c = getc(iob)) < 0 || eom(c, iob)) {
205                                 if (!eom_action) {
206                                         /* flush null messages */
207                                         while ((c = getc(iob)) >= 0 && eom(c, iob))
208                                                 ;
209                                         if (c >= 0)
210                                                 ungetc(c, iob);
211                                 }
212                                 msg_count = 0;
213                                 *buf = 0;
214                                 return FILEEOF;
215                         }
216                         state = BODY;
217                         goto body;
218                 }
219                 /*
220                 ** get the name of this component.  take characters up
221                 ** to a ':', a newline or NAMESZ-1 characters,
222                 ** whichever comes first.
223                 */
224                 cp = name;
225                 i = NAMESZ - 1;
226                 for (;;) {
227 #ifdef LINUX_STDIO
228                         bp = sp = (unsigned char *) iob->_IO_read_ptr - 1;
229                         j = (cnt = ((long) iob->_IO_read_end -
230                                 (long) iob->_IO_read_ptr)  + 1) < i ? cnt : i;
231 #elif defined(__DragonFly__)
232                         bp = sp = (unsigned char *) ((struct __FILE_public *)iob)->_p - 1;
233                         j = (cnt = ((struct __FILE_public *)iob)->_r+1) < i ? cnt : i;
234 #else
235                         bp = sp = (unsigned char *) iob->_ptr - 1;
236                         j = (cnt = iob->_cnt+1) < i ? cnt : i;
237 #endif
238                         while (--j >= 0 && (c = *bp++) != ':' && c != '\n')
239                                 *cp++ = c;
240
241                         j = bp - sp;
242                         if ((cnt -= j) <= 0) {
243 #ifdef LINUX_STDIO
244                                 iob->_IO_read_ptr = iob->_IO_read_end;
245                                 if (__underflow(iob) == EOF) {
246 #elif defined(__DragonFly__)
247                                 if (__srget(iob) == EOF) {
248 #else
249                                 if (_filbuf(iob) == EOF) {
250 #endif
251                                         *cp = *buf = 0;
252                                         advise(NULL, "eof encountered in field \"%s\"", name);
253                                         return FMTERR;
254                                 }
255 #ifdef LINUX_STDIO
256                                 iob->_IO_read_ptr++; /* NOT automatic in __underflow()! */
257 #endif
258                         } else {
259 #ifdef LINUX_STDIO
260                                 iob->_IO_read_ptr = bp + 1;
261 #elif defined(__DragonFly__)
262                                 ((struct __FILE_public *)iob)->_p = bp + 1;
263                                 ((struct __FILE_public *)iob)->_r = cnt - 1;
264 #else
265                                 iob->_ptr = bp + 1;
266                                 iob->_cnt = cnt - 1;
267 #endif
268                         }
269                         if (c == ':')
270                                 break;
271
272                         /*
273                         ** something went wrong.  possibilities are:
274                         **  . hit a newline (error)
275                         **  . got more than namesz chars. (error)
276                         **  . hit the end of the buffer. (loop)
277                         */
278                         if (c == '\n') {
279                                 /*
280                                 ** We hit the end of the line without
281                                 ** seeing ':' to terminate the field name.
282                                 ** This is usually (always?)  spam.  But,
283                                 ** blowing up is lame, especially when
284                                 ** scan(1)ing a folder with such messages.
285                                 ** Pretend such lines are the first of
286                                 ** the body (at least mutt also handles
287                                 ** it this way).
288                                 */
289
290                                 /*
291                                 ** See if buf can hold this line, since we
292                                 ** were assuming we had a buffer of NAMESZ,
293                                 ** not bufsz.
294                                 */
295                                 /* + 1 for the newline */
296                                 if (bufsz < j + 1) {
297                                         /*
298                                         ** No, it can't.  Oh well,
299                                         ** guess we'll blow up.
300                                         */
301                                         *cp = *buf = 0;
302                                         advise(NULL, "eol encountered in field \"%s\"", name);
303                                         state = FMTERR;
304                                         goto finish;
305                                 }
306                                 memcpy(buf, name, j - 1);
307                                 buf[j - 1] = '\n';
308                                 buf[j] = '\0';
309                                 /*
310                                 ** mhparse.c:get_content wants to find
311                                 ** the position of the body start, but
312                                 ** it thinks there's a blank line between
313                                 ** the header and the body (naturally!),
314                                 ** so seek back so that things line up
315                                 ** even though we don't have that blank
316                                 ** line in this case.  Simpler parsers
317                                 ** (e.g. mhl) get extra newlines, but
318                                 ** that should be harmless enough, right?
319                                 ** This is a corrupt message anyway.
320                                 */
321                                 fseek(iob, ftell(iob) - 2, SEEK_SET);
322                                 return BODY;
323                         }
324                         if ((i -= j) <= 0) {
325                                 *cp = *buf = 0;
326                                 advise(NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
327                                 state = LENERR;
328                                 goto finish;
329                         }
330                 }
331
332                 while (isspace(*--cp) && cp >= name)
333                         ;
334                 *++cp = 0;
335                 /* fall through */
336
337         case FLDPLUS:
338                 /*
339                 ** get (more of) the text of a field.  take
340                 ** characters up to the end of this field (newline
341                 ** followed by non-blank) or bufsz-1 characters.
342                 */
343                 cp = buf; i = bufsz-1;
344                 for (;;) {
345 #ifdef LINUX_STDIO
346                         cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
347                         bp = (unsigned char *) --iob->_IO_read_ptr;
348 #elif defined(__DragonFly__)
349                         cnt = ((struct __FILE_public *)iob)->_r++;
350                         bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
351 #else
352                         cnt = iob->_cnt++;
353                         bp = (unsigned char *) --iob->_ptr;
354 #endif
355                         c = cnt < i ? cnt : i;
356                         while ((ep = locc( c, bp, '\n' ))) {
357                                 /*
358                                 ** if we hit the end of this field,
359                                 ** return.
360                                 */
361                                 if ((j = *++ep) != ' ' && j != '\t') {
362 #ifdef LINUX_STDIO
363                                         j = ep - (unsigned char *) iob->_IO_read_ptr;
364                                         memcpy(cp, iob->_IO_read_ptr, j);
365                                         iob->_IO_read_ptr = ep;
366 #elif defined(__DragonFly__)
367                                         j = ep - (unsigned char *) ((struct __FILE_public *)iob)->_p;
368                                         memcpy(cp, ((struct __FILE_public *)iob)->_p, j);
369                                         ((struct __FILE_public *)iob)->_p = ep;
370                                         ((struct __FILE_public *)iob)->_r -= j;
371 #else
372                                         j = ep - (unsigned char *) iob->_ptr;
373                                         memcpy(cp, iob->_ptr, j);
374                                         iob->_ptr = ep;
375                                         iob->_cnt -= j;
376 #endif
377                                         cp += j;
378                                         state = FLD;
379                                         goto finish;
380                                 }
381                                 c -= ep - bp;
382                                 bp = ep;
383                         }
384                         /*
385                         ** end of input or dest buffer - copy what
386                         ** we've found.
387                         */
388 #ifdef LINUX_STDIO
389                         c += bp - (unsigned char *) iob->_IO_read_ptr;
390                         memcpy(cp, iob->_IO_read_ptr, c);
391 #elif defined(__DragonFly__)
392                         c += bp - (unsigned char *) ((struct __FILE_public *)iob)->_p;
393                         memcpy(cp, ((struct __FILE_public *)iob)->_p, c);
394 #else
395                         c += bp - (unsigned char *) iob->_ptr;
396                         memcpy(cp, iob->_ptr, c);
397 #endif
398                         i -= c;
399                         cp += c;
400                         if (i <= 0) {
401                                 /* the dest buffer is full */
402 #ifdef LINUX_STDIO
403                                 iob->_IO_read_ptr += c;
404 #elif defined(__DragonFly__)
405                                 ((struct __FILE_public *)iob)->_r -= c;
406                                 ((struct __FILE_public *)iob)->_p += c;
407 #else
408                                 iob->_cnt -= c;
409                                 iob->_ptr += c;
410 #endif
411                                 state = FLDPLUS;
412                                 break;
413                         }
414                         /*
415                         ** There's one character left in the input
416                         ** buffer.  Copy it & fill the buffer.
417                         ** If the last char was a newline and the
418                         ** next char is not whitespace, this is
419                         ** the end of the field.  Otherwise loop.
420                         */
421                         --i;
422 #ifdef LINUX_STDIO
423                         *cp++ = j = *(iob->_IO_read_ptr + c);
424                         iob->_IO_read_ptr = iob->_IO_read_end;
425                         c = __underflow(iob);
426                         iob->_IO_read_ptr++;  /* NOT automatic! */
427 #elif defined(__DragonFly__)
428                         *cp++ =j = *(((struct __FILE_public *)iob)->_p + c);
429                         c = __srget(iob);
430 #else
431                         *cp++ = j = *(iob->_ptr + c);
432                         c = _filbuf(iob);
433 #endif
434                         if (c == EOF ||
435                           ((j == '\0' || j == '\n') && c != ' ' && c != '\t')) {
436                                 if (c != EOF) {
437 #ifdef LINUX_STDIO
438                                         --iob->_IO_read_ptr;
439 #elif defined(__DragonFly__)
440                                         --((struct __FILE_public *)iob)->_p;
441                                         ++((struct __FILE_public *)iob)->_r;
442 #else
443                                         --iob->_ptr;
444                                         ++iob->_cnt;
445 #endif
446                                 }
447                                 state = FLD;
448                                 break;
449                         }
450                 }
451                 break;
452
453         case BODY:
454         body:
455                 /*
456                 ** get the message body up to bufsz characters or
457                 ** the end of the message.  Sleazy hack: if bufsz
458                 ** is negative we assume that we were called to
459                 ** copy directly into the output buffer and we
460                 ** don't add an eos.
461                 */
462                 i = (bufsz < 0) ? -bufsz : bufsz-1;
463 #ifdef LINUX_STDIO
464                 bp = (unsigned char *) --iob->_IO_read_ptr;
465                 cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
466 #elif defined(__DragonFly__)
467                 bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
468                 cnt = ++((struct __FILE_public *)iob)->_r;
469 #else
470                 bp = (unsigned char *) --iob->_ptr;
471                 cnt = ++iob->_cnt;
472 #endif
473                 c = (cnt < i ? cnt : i);
474                 if (ismbox && c > 1) {
475                         /*
476                         ** packed maildrop - only take up to the (possible)
477                         ** start of the next message.  This "matchc" should
478                         ** probably be a Boyer-Moore matcher for non-vaxen,
479                         ** particularly since we have the alignment table
480                         ** all built for the end-of-buffer test (next).
481                         ** But our vax timings indicate that the "matchc"
482                         ** instruction is 50% faster than a carefully coded
483                         ** B.M. matcher for most strings.  (So much for
484                         ** elegant algorithms vs. brute force.)  Since I
485                         ** (currently) run MH on a vax, we use the matchc
486                         ** instruction. --vj
487                         */
488                         if ((ep = matchc( fdelimlen, fdelim, c, bp )))
489                                 c = ep - bp + 1;
490                         else {
491                                 /*
492                                 ** There's no delim in the buffer but
493                                 ** there may be a partial one at the end.
494                                 ** If so, we want to leave it so the "eom"
495                                 ** check on the next call picks it up.  Use a
496                                 ** modified Boyer-Moore matcher to make this
497                                 ** check relatively cheap.  The first "if"
498                                 ** figures out what position in the pattern
499                                 ** matches the last character in the buffer.
500                                 ** The inner "while" matches the pattern
501                                 ** against the buffer, backwards starting
502                                 ** at that position.  Note that unless the
503                                 ** buffer ends with one of the characters
504                                 ** in the pattern (excluding the first
505                                 ** and last), we do only one test.
506                                 */
507                                 ep = bp + c - 1;
508                                 if ((sp = pat_map[*ep])) {
509                                         do {
510                                                 /*
511                                                 ** This if() is true unless
512                                                 ** (a) the buffer is too
513                                                 ** small to contain this
514                                                 ** delimiter prefix,
515                                                 ** or (b) it contains
516                                                 ** exactly enough chars for
517                                                 ** the delimiter prefix.
518                                                 ** For case (a) obviously we
519                                                 ** aren't going to match.
520                                                 ** For case (b), if the
521                                                 ** buffer really contained
522                                                 ** exactly a delim prefix,
523                                                 ** then the m_eom call
524                                                 ** at entry should have
525                                                 ** found it.  Thus it's
526                                                 ** not a delim and we know
527                                                 ** we won't get a match.
528                                                 */
529                                                 if (((sp - fdelim) + 2) <= c) {
530                                                         cp = sp;
531                                                         /*
532                                                         ** Unfortunately although fdelim has a preceding NUL
533                                                         ** we can't use this as a sentinel in case the buffer
534                                                         ** contains a NUL in exactly the wrong place (this
535                                                         ** would cause us to run off the front of fdelim).
536                                                         */
537                                                         while (*--ep == *--cp)
538                                                                 if (cp < fdelim)
539                                                                         break;
540                                                         if (cp < fdelim) {
541                                                                 /* we matched the entire delim prefix,
542                                                                 ** so only take the buffer up to there.
543                                                                 ** we know ep >= bp -- check above prevents underrun
544                                                                 */
545                                                                 c = (ep - bp) + 2;
546                                                                 break;
547                                                         }
548                                                 }
549                                                 /* try matching one less char of delim string */
550                                                 ep = bp + c - 1;
551                                         } while (--sp > fdelim);
552                                 }
553                         }
554                 }
555                 memcpy( buf, bp, c );
556 #ifdef LINUX_STDIO
557                 iob->_IO_read_ptr += c;
558 #elif defined(__DragonFly__)
559                 ((struct __FILE_public *)iob)->_r -= c;
560                 ((struct __FILE_public *)iob)->_p += c;
561 #else
562                 iob->_cnt -= c;
563                 iob->_ptr += c;
564 #endif
565                 if (bufsz < 0) {
566                         msg_count = c;
567                         return (state);
568                 }
569                 cp = buf + c;
570                 break;
571
572         default:
573                 adios(NULL, "m_getfld() called with bogus state of %d", state);
574         }
575 finish:
576         *cp = 0;
577         msg_count = cp - buf;
578         return (state);
579 }
580
581 static char fromline[BUFSIZ] = "";
582
583 void
584 thisisanmbox(FILE *iob)
585 {
586         register int c;
587         register long pos;
588         char text[10];
589         register char *cp;
590         register char *delimstr;
591
592         /*
593         ** Figure out what the message delimitter string is for this
594         ** maildrop.  (This used to be part of m_Eom but I didn't like
595         ** the idea of an "if" statement that could only succeed on the
596         ** first call to m_Eom getting executed on each call, i.e., at
597         ** every newline in the message).
598         **
599         ** If the first line of the maildrop is a Unix "From " line, we
600         ** say the style is MBOX and eat the rest of the line.  Otherwise
601         ** abort.
602         */
603
604         pos = ftell(iob);
605         if (fread(text, sizeof(*text), 5, iob) != 5) {
606                 adios(NULL, "Read error");
607         }
608         if (strncmp(text, "From ", 5)!=0) {
609                 adios(NULL, "No Unix style (mbox) maildrop.");
610         }
611         ismbox = TRUE;
612         delimstr = "\nFrom ";
613         cp = fromline;
614         while ((c = getc(iob)) != '\n' && cp - fromline < BUFSIZ - 1)
615                 *cp++ = c;
616         *cp = '\0';
617
618         c = strlen(delimstr);
619         fdelim = (unsigned char *) mh_xmalloc((size_t) (c + 3));
620         *fdelim++ = '\0';
621         *fdelim = '\n';
622         msg_delim = (char *)fdelim+1;
623         edelim = (unsigned char *)msg_delim+1;
624         fdelimlen = c + 1;
625         edelimlen = c - 1;
626         strcpy(msg_delim, delimstr);
627         delimend = (unsigned char *)msg_delim + edelimlen;
628         if (edelimlen <= 1)
629                 adios(NULL, "maildrop delimiter must be at least 2 bytes");
630         /*
631         ** build a Boyer-Moore end-position map for the matcher in m_getfld.
632         ** N.B. - we don't match just the first char (since it's the newline
633         ** separator) or the last char (since the matchc would have found it
634         ** if it was a real delim).
635         */
636         pat_map = (unsigned char **) calloc(256, sizeof(unsigned char *));
637
638         for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ )
639                 pat_map[(unsigned char)*cp] = (unsigned char *) cp;
640 }
641
642
643 /*
644 ** test for msg delimiter string
645 */
646
647 static int
648 m_Eom(int c, FILE *iob)
649 {
650         register long pos = 0L;
651         register int i;
652         char text[10];
653         register char *cp;
654
655         pos = ftell(iob);
656         if ((i = fread(text, sizeof *text, edelimlen, iob)) != edelimlen ||
657                         (strncmp(text, (char *)edelim, edelimlen)!=0)) {
658                 if (i == 0 && ismbox)
659                         /*
660                         ** the final newline in the (brain damaged) unix-format
661                         ** maildrop is part of the delimitter - delete it.
662                         */
663                         return 1;
664
665                 fseek(iob, (long)(pos-1), SEEK_SET);
666                 getc(iob);  /* should be OK */
667                 return 0;
668         }
669
670         if (ismbox) {
671                 cp = fromline;
672                 while ((c = getc(iob)) != '\n' && c >= 0 && cp - fromline < BUFSIZ - 1)
673                         *cp++ = c;
674                 *cp = '\0';
675         }
676
677         return 1;
678 }
679
680
681 /*
682 ** Return the Return-Path and Delivery-Date header information.
683 **
684 ** Currently, I'm assuming that the "From " line takes the following form:
685 **         "From" sender@host  date        (sendmail delivery)
686 */
687 int
688 get_returnpath(char *rp, int rplen, char *dd, int ddlen)
689 {
690         char *cp;
691
692         if (!(cp = strchr(fromline, ' '))) {
693                 return 0;
694         }
695         /* Extract the Return-Path. */
696         snprintf(rp, rplen, "%.*s\n", (int)(cp - fromline), fromline);
697         /* Advance over the whitespace. */
698         while (*cp == ' ' || *cp == '\t') {
699                 cp++;
700         }
701         /* Extract the Delivery-Date. */
702         snprintf(dd, ddlen, "%.*s\n", 24, cp);
703         *fromline = '\0';
704         return 1;
705 }
706
707
708 static unsigned char *
709 matchc(int patln, char *pat, int strln, char *str)
710 {
711         register char *es = str + strln - patln;
712         register char *sp;
713         register char *pp;
714         register char *ep = pat + patln;
715         register char pc = *pat++;
716
717         for(;;) {
718                 while (pc != *str++)
719                         if (str > es)
720                                 return 0;
721                 if (str > es+1)
722                         return 0;
723                 sp = str; pp = pat;
724                 while (pp < ep && *sp++ == *pp)
725                         pp++;
726                 if (pp >= ep)
727                         return ((unsigned char *)--str);
728         }
729 }
730
731
732 /*
733 ** Locate character "term" in the next "cnt" characters of "src".
734 ** If found, return its address, otherwise return 0.
735 */
736
737 static unsigned char *
738 locc(int cnt, unsigned char *src, unsigned char term)
739 {
740         while (*src++ != term && --cnt > 0)
741                 ;
742
743         return (cnt > 0 ? --src : (unsigned char *)0);
744 }