Replace mh_xmalloc() with mh_xcalloc()
[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 FLD:
191                 if (c == '\n' || c == '-') {
192                         /* we hit the header/body separator */
193                         while (c != '\n' && (c = getc(iob)) >= 0)
194                                 ;
195
196                         if (c < 0 || (c = getc(iob)) < 0 || eom(c, iob)) {
197                                 if (!eom_action) {
198                                         /* flush null messages */
199                                         while ((c = getc(iob)) >= 0 && eom(c, iob))
200                                                 ;
201                                         if (c >= 0)
202                                                 ungetc(c, iob);
203                                 }
204                                 msg_count = 0;
205                                 *buf = 0;
206                                 return FILEEOF;
207                         }
208                         state = BODY;
209                         goto body;
210                 }
211                 /*
212                 ** get the name of this component.  take characters up
213                 ** to a ':', a newline or NAMESZ-1 characters,
214                 ** whichever comes first.
215                 */
216                 cp = name;
217                 i = NAMESZ - 1;
218                 for (;;) {
219 #ifdef LINUX_STDIO
220                         bp = sp = (unsigned char *) iob->_IO_read_ptr - 1;
221                         j = (cnt = ((long) iob->_IO_read_end -
222                                 (long) iob->_IO_read_ptr)  + 1) < i ? cnt : i;
223 #elif defined(__DragonFly__)
224                         bp = sp = (unsigned char *) ((struct __FILE_public *)iob)->_p - 1;
225                         j = (cnt = ((struct __FILE_public *)iob)->_r+1) < i ? cnt : i;
226 #else
227                         bp = sp = (unsigned char *) iob->_ptr - 1;
228                         j = (cnt = iob->_cnt+1) < i ? cnt : i;
229 #endif
230                         while (--j >= 0 && (c = *bp++) != ':' && c != '\n')
231                                 *cp++ = c;
232
233                         j = bp - sp;
234                         if ((cnt -= j) <= 0) {
235 #ifdef LINUX_STDIO
236                                 iob->_IO_read_ptr = iob->_IO_read_end;
237                                 if (__underflow(iob) == EOF) {
238 #elif defined(__DragonFly__)
239                                 if (__srget(iob) == EOF) {
240 #else
241                                 if (_filbuf(iob) == EOF) {
242 #endif
243                                         *cp = *buf = 0;
244                                         advise(NULL, "eof encountered in field \"%s\"", name);
245                                         return FMTERR;
246                                 }
247 #ifdef LINUX_STDIO
248                                 iob->_IO_read_ptr++; /* NOT automatic in __underflow()! */
249 #endif
250                         } else {
251 #ifdef LINUX_STDIO
252                                 iob->_IO_read_ptr = bp + 1;
253 #elif defined(__DragonFly__)
254                                 ((struct __FILE_public *)iob)->_p = bp + 1;
255                                 ((struct __FILE_public *)iob)->_r = cnt - 1;
256 #else
257                                 iob->_ptr = bp + 1;
258                                 iob->_cnt = cnt - 1;
259 #endif
260                         }
261                         if (c == ':')
262                                 break;
263
264                         /*
265                         ** something went wrong.  possibilities are:
266                         **  . hit a newline (error)
267                         **  . got more than namesz chars. (error)
268                         **  . hit the end of the buffer. (loop)
269                         */
270                         if (c == '\n') {
271                                 /*
272                                 ** We hit the end of the line without
273                                 ** seeing ':' to terminate the field name.
274                                 ** This is usually (always?)  spam.  But,
275                                 ** blowing up is lame, especially when
276                                 ** scan(1)ing a folder with such messages.
277                                 ** Pretend such lines are the first of
278                                 ** the body (at least mutt also handles
279                                 ** it this way).
280                                 */
281
282                                 /*
283                                 ** See if buf can hold this line, since we
284                                 ** were assuming we had a buffer of NAMESZ,
285                                 ** not bufsz.
286                                 */
287                                 /* + 1 for the newline */
288                                 if (bufsz < j + 1) {
289                                         /*
290                                         ** No, it can't.  Oh well,
291                                         ** guess we'll blow up.
292                                         */
293                                         *cp = *buf = 0;
294                                         advise(NULL, "eol encountered in field \"%s\"", name);
295                                         state = FMTERR;
296                                         goto finish;
297                                 }
298                                 memcpy(buf, name, j - 1);
299                                 buf[j - 1] = '\n';
300                                 buf[j] = '\0';
301                                 /*
302                                 ** mhparse.c:get_content wants to find
303                                 ** the position of the body start, but
304                                 ** it thinks there's a blank line between
305                                 ** the header and the body (naturally!),
306                                 ** so seek back so that things line up
307                                 ** even though we don't have that blank
308                                 ** line in this case.  Simpler parsers
309                                 ** (e.g. mhl) get extra newlines, but
310                                 ** that should be harmless enough, right?
311                                 ** This is a corrupt message anyway.
312                                 */
313                                 fseek(iob, ftell(iob) - 2, SEEK_SET);
314                                 return BODY;
315                         }
316                         if ((i -= j) <= 0) {
317                                 *cp = *buf = 0;
318                                 advise(NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
319                                 state = LENERR;
320                                 goto finish;
321                         }
322                 }
323
324                 while (isspace(*--cp) && cp >= name)
325                         ;
326                 *++cp = 0;
327                 /* fall through */
328
329         case FLDPLUS:
330                 /*
331                 ** get (more of) the text of a field.  take
332                 ** characters up to the end of this field (newline
333                 ** followed by non-blank) or bufsz-1 characters.
334                 */
335                 cp = buf; i = bufsz-1;
336                 for (;;) {
337 #ifdef LINUX_STDIO
338                         cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
339                         bp = (unsigned char *) --iob->_IO_read_ptr;
340 #elif defined(__DragonFly__)
341                         cnt = ((struct __FILE_public *)iob)->_r++;
342                         bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
343 #else
344                         cnt = iob->_cnt++;
345                         bp = (unsigned char *) --iob->_ptr;
346 #endif
347                         c = cnt < i ? cnt : i;
348                         while ((ep = locc( c, bp, '\n' ))) {
349                                 /*
350                                 ** if we hit the end of this field,
351                                 ** return.
352                                 */
353                                 if ((j = *++ep) != ' ' && j != '\t') {
354 #ifdef LINUX_STDIO
355                                         j = ep - (unsigned char *) iob->_IO_read_ptr;
356                                         memcpy(cp, iob->_IO_read_ptr, j);
357                                         iob->_IO_read_ptr = ep;
358 #elif defined(__DragonFly__)
359                                         j = ep - (unsigned char *) ((struct __FILE_public *)iob)->_p;
360                                         memcpy(cp, ((struct __FILE_public *)iob)->_p, j);
361                                         ((struct __FILE_public *)iob)->_p = ep;
362                                         ((struct __FILE_public *)iob)->_r -= j;
363 #else
364                                         j = ep - (unsigned char *) iob->_ptr;
365                                         memcpy(cp, iob->_ptr, j);
366                                         iob->_ptr = ep;
367                                         iob->_cnt -= j;
368 #endif
369                                         cp += j;
370                                         state = FLD;
371                                         goto finish;
372                                 }
373                                 c -= ep - bp;
374                                 bp = ep;
375                         }
376                         /*
377                         ** end of input or dest buffer - copy what
378                         ** we've found.
379                         */
380 #ifdef LINUX_STDIO
381                         c += bp - (unsigned char *) iob->_IO_read_ptr;
382                         memcpy(cp, iob->_IO_read_ptr, c);
383 #elif defined(__DragonFly__)
384                         c += bp - (unsigned char *) ((struct __FILE_public *)iob)->_p;
385                         memcpy(cp, ((struct __FILE_public *)iob)->_p, c);
386 #else
387                         c += bp - (unsigned char *) iob->_ptr;
388                         memcpy(cp, iob->_ptr, c);
389 #endif
390                         i -= c;
391                         cp += c;
392                         if (i <= 0) {
393                                 /* the dest buffer is full */
394 #ifdef LINUX_STDIO
395                                 iob->_IO_read_ptr += c;
396 #elif defined(__DragonFly__)
397                                 ((struct __FILE_public *)iob)->_r -= c;
398                                 ((struct __FILE_public *)iob)->_p += c;
399 #else
400                                 iob->_cnt -= c;
401                                 iob->_ptr += c;
402 #endif
403                                 state = FLDPLUS;
404                                 break;
405                         }
406                         /*
407                         ** There's one character left in the input
408                         ** buffer.  Copy it & fill the buffer.
409                         ** If the last char was a newline and the
410                         ** next char is not whitespace, this is
411                         ** the end of the field.  Otherwise loop.
412                         */
413                         --i;
414 #ifdef LINUX_STDIO
415                         *cp++ = j = *(iob->_IO_read_ptr + c);
416                         iob->_IO_read_ptr = iob->_IO_read_end;
417                         c = __underflow(iob);
418                         iob->_IO_read_ptr++;  /* NOT automatic! */
419 #elif defined(__DragonFly__)
420                         *cp++ =j = *(((struct __FILE_public *)iob)->_p + c);
421                         c = __srget(iob);
422 #else
423                         *cp++ = j = *(iob->_ptr + c);
424                         c = _filbuf(iob);
425 #endif
426                         if (c == EOF || ((j == '\0' || j == '\n')
427                                         && c != ' ' && c != '\t')) {
428                                 if (c != EOF) {
429 #ifdef LINUX_STDIO
430                                         --iob->_IO_read_ptr;
431 #elif defined(__DragonFly__)
432                                         --((struct __FILE_public *)iob)->_p;
433                                         ++((struct __FILE_public *)iob)->_r;
434 #else
435                                         --iob->_ptr;
436                                         ++iob->_cnt;
437 #endif
438                                 }
439                                 state = FLD;
440                                 break;
441                         }
442                 }
443                 break;
444
445         case BODY:
446         body:
447                 /*
448                 ** get the message body up to bufsz characters or
449                 ** the end of the message.  Sleazy hack: if bufsz
450                 ** is negative we assume that we were called to
451                 ** copy directly into the output buffer and we
452                 ** don't add an eos.
453                 */
454                 i = (bufsz < 0) ? -bufsz : bufsz-1;
455 #ifdef LINUX_STDIO
456                 bp = (unsigned char *) --iob->_IO_read_ptr;
457                 cnt = (long) iob->_IO_read_end - (long) iob->_IO_read_ptr;
458 #elif defined(__DragonFly__)
459                 bp = (unsigned char *) --((struct __FILE_public *)iob)->_p;
460                 cnt = ++((struct __FILE_public *)iob)->_r;
461 #else
462                 bp = (unsigned char *) --iob->_ptr;
463                 cnt = ++iob->_cnt;
464 #endif
465                 c = (cnt < i ? cnt : i);
466                 if (ismbox && c > 1) {
467                         /*
468                         ** packed maildrop - only take up to the (possible)
469                         ** start of the next message.  This "matchc" should
470                         ** probably be a Boyer-Moore matcher for non-vaxen,
471                         ** particularly since we have the alignment table
472                         ** all built for the end-of-buffer test (next).
473                         ** But our vax timings indicate that the "matchc"
474                         ** instruction is 50% faster than a carefully coded
475                         ** B.M. matcher for most strings.  (So much for
476                         ** elegant algorithms vs. brute force.)  Since I
477                         ** (currently) run MH on a vax, we use the matchc
478                         ** instruction. --vj
479                         */
480                         if ((ep = matchc( fdelimlen, fdelim, c, bp )))
481                                 c = ep - bp + 1;
482                         else {
483                                 /*
484                                 ** There's no delim in the buffer but
485                                 ** there may be a partial one at the end.
486                                 ** If so, we want to leave it so the "eom"
487                                 ** check on the next call picks it up.  Use a
488                                 ** modified Boyer-Moore matcher to make this
489                                 ** check relatively cheap.  The first "if"
490                                 ** figures out what position in the pattern
491                                 ** matches the last character in the buffer.
492                                 ** The inner "while" matches the pattern
493                                 ** against the buffer, backwards starting
494                                 ** at that position.  Note that unless the
495                                 ** buffer ends with one of the characters
496                                 ** in the pattern (excluding the first
497                                 ** and last), we do only one test.
498                                 */
499                                 ep = bp + c - 1;
500                                 if ((sp = pat_map[*ep])) {
501                                         do {
502                                                 /*
503                                                 ** This if() is true unless
504                                                 ** (a) the buffer is too
505                                                 ** small to contain this
506                                                 ** delimiter prefix,
507                                                 ** or (b) it contains
508                                                 ** exactly enough chars for
509                                                 ** the delimiter prefix.
510                                                 ** For case (a) obviously we
511                                                 ** aren't going to match.
512                                                 ** For case (b), if the
513                                                 ** buffer really contained
514                                                 ** exactly a delim prefix,
515                                                 ** then the m_eom call
516                                                 ** at entry should have
517                                                 ** found it.  Thus it's
518                                                 ** not a delim and we know
519                                                 ** we won't get a match.
520                                                 */
521                                                 if (((sp - fdelim) + 2) <= c) {
522                                                         cp = sp;
523                                                         /*
524                                                         ** Unfortunately although fdelim has a preceding NUL
525                                                         ** we can't use this as a sentinel in case the buffer
526                                                         ** contains a NUL in exactly the wrong place (this
527                                                         ** would cause us to run off the front of fdelim).
528                                                         */
529                                                         while (*--ep == *--cp)
530                                                                 if (cp < fdelim)
531                                                                         break;
532                                                         if (cp < fdelim) {
533                                                                 /* we matched the entire delim prefix,
534                                                                 ** so only take the buffer up to there.
535                                                                 ** we know ep >= bp -- check above prevents underrun
536                                                                 */
537                                                                 c = (ep - bp) + 2;
538                                                                 break;
539                                                         }
540                                                 }
541                                                 /* try matching one less char of delim string */
542                                                 ep = bp + c - 1;
543                                         } while (--sp > fdelim);
544                                 }
545                         }
546                 }
547                 memcpy( buf, bp, c );
548 #ifdef LINUX_STDIO
549                 iob->_IO_read_ptr += c;
550 #elif defined(__DragonFly__)
551                 ((struct __FILE_public *)iob)->_r -= c;
552                 ((struct __FILE_public *)iob)->_p += c;
553 #else
554                 iob->_cnt -= c;
555                 iob->_ptr += c;
556 #endif
557                 if (bufsz < 0) {
558                         msg_count = c;
559                         return (state);
560                 }
561                 cp = buf + c;
562                 break;
563
564         default:
565                 adios(EX_SOFTWARE, NULL, "m_getfld() called with bogus state of %d", state);
566         }
567 finish:
568         *cp = 0;
569         msg_count = cp - buf;
570         return (state);
571 }
572
573
574 void
575 thisisanmbox(FILE *iob)
576 {
577         int c;
578         char text[10];
579         char *cp;
580         char *delimstr;
581
582         c = getc(iob); 
583         if (feof(iob)) {
584                 return;
585         }
586         ungetc(c, iob);
587
588         /*
589         ** Figure out what the message delimitter string is for this
590         ** maildrop.  (This used to be part of m_Eom but I didn't like
591         ** the idea of an "if" statement that could only succeed on the
592         ** first call to m_Eom getting executed on each call, i.e., at
593         ** every newline in the message).
594         **
595         ** If the first line of the maildrop is a Unix "From " line, we
596         ** say the style is MBOX and eat the rest of the line.  Otherwise
597         ** abort.
598         */
599
600         if (fread(text, sizeof(*text), 5, iob) != 5) {
601                 adios(EX_IOERR, NULL, "Read error");
602         }
603         if (strncmp(text, "From ", 5)!=0) {
604                 adios(EX_USAGE, NULL, "No Unix style (mbox) maildrop.");
605         }
606         ismbox = TRUE;
607         delimstr = "\nFrom ";
608         while ((c = getc(iob)) != '\n' && c >= 0) {
609                 continue;
610         }
611         c = strlen(delimstr);
612         fdelim = (unsigned char *) mh_xcalloc((size_t) (c + 3), sizeof(char));
613         *fdelim++ = '\0';
614         *fdelim = '\n';
615         msg_delim = (char *)fdelim+1;
616         edelim = (unsigned char *)msg_delim+1;
617         fdelimlen = c + 1;
618         edelimlen = c - 1;
619         strcpy(msg_delim, delimstr);
620         delimend = (unsigned char *)msg_delim + edelimlen;
621         if (edelimlen <= 1)
622                 adios(EX_DATAERR, NULL, "maildrop delimiter must be at least 2 bytes");
623         /*
624         ** build a Boyer-Moore end-position map for the matcher in m_getfld.
625         ** N.B. - we don't match just the first char (since it's the newline
626         ** separator) or the last char (since the matchc would have found it
627         ** if it was a real delim).
628         */
629         pat_map = (unsigned char **) mh_xcalloc(256, sizeof(unsigned char *));
630
631         for (cp = (char *) fdelim + 1; cp < (char *) delimend; cp++ )
632                 pat_map[(unsigned char)*cp] = (unsigned char *) cp;
633 }
634
635
636 /*
637 ** test for msg delimiter string
638 */
639
640 static int
641 m_Eom(int c, FILE *iob)
642 {
643         long pos = 0L;
644         int i;
645         char text[10];
646
647         pos = ftell(iob);
648         if ((i = fread(text, sizeof *text, edelimlen, iob)) != edelimlen ||
649                         (strncmp(text, (char *)edelim, edelimlen)!=0)) {
650                 if (i == 0 && ismbox)
651                         /*
652                         ** the final newline in the (brain damaged) unix-format
653                         ** maildrop is part of the delimitter - delete it.
654                         */
655                         return 1;
656
657                 fseek(iob, (long)(pos-1), SEEK_SET);
658                 getc(iob);  /* should be OK */
659                 return 0;
660         }
661
662         if (ismbox) {
663                 while ((c = getc(iob)) != '\n' && c >= 0) {
664                         continue;
665                 }
666         }
667
668         return 1;
669 }
670
671
672 static unsigned char *
673 matchc(int patln, char *pat, int strln, char *str)
674 {
675         char *es = str + strln - patln;
676         char *sp;
677         char *pp;
678         char *ep = pat + patln;
679         char pc = *pat++;
680
681         for(;;) {
682                 while (pc != *str++)
683                         if (str > es)
684                                 return 0;
685                 if (str > es+1)
686                         return 0;
687                 sp = str; pp = pat;
688                 while (pp < ep && *sp++ == *pp)
689                         pp++;
690                 if (pp >= ep)
691                         return ((unsigned char *)--str);
692         }
693 }
694
695
696 /*
697 ** Locate character "term" in the next "cnt" characters of "src".
698 ** If found, return its address, otherwise return 0.
699 */
700
701 static unsigned char *
702 locc(int cnt, unsigned char *src, unsigned char term)
703 {
704         while (*src++ != term && --cnt > 0)
705                 ;
706
707         return (cnt > 0 ? --src : (unsigned char *)0);
708 }