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