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