+ When entering m_getfld()/m_unknown():
+ Check to see if the call had changed the file position. If so,
+ adjust the internal position reference accordingly. If not, restore
+ the internal file position from last_internal_pos.
+*/
+
+
+static void
+enter_getfld (m_getfld_state_t *gstate, FILE *iob) {
+ m_getfld_state_t s;
+ off_t pos;
+ off_t pos_movement;
+
+ if (! *gstate) {
+ *gstate = m_getfld_state_init(iob);
+ }
+ s = *gstate;
+ s->bytes_read = 0;
+
+ /* This is ugly and no longer necessary, but is retained just in
+ case it's needed again. The parser used to open the input file
+ multiple times, so we had to always use the FILE * that's
+ passed to m_getfld(). Now the parser inits a new
+ m_getfld_state for each file. See comment below about the
+ readpos shift code being currently unused. */
+ s->iob = iob;
+
+ if (!s->track_filepos)
+ return;
+
+ if ((pos = ftello(iob)) == -1)
+ adios("getfld's iob", "failed to get offset on entry");
+ if (pos == 0 && s->last_internal_pos == 0)
+ return;
+
+ if (s->last_internal_pos == 0) {
+ s->total_bytes_read = pos;
+ return;
+ }
+
+ pos_movement = pos - s->last_caller_pos; /* Can be < 0. */
+ if (pos_movement == 0) {
+ pos = s->last_internal_pos;
+ } else {
+ /* The current file stream position differs from the
+ last one, so caller must have called ftell/o().
+ Or, this is the first call and the file position
+ was not at 0. */
+
+ if (s->readpos + pos_movement >= s->msg_buf &&
+ s->readpos + pos_movement < s->end) {
+ /* This is currently unused. It could be used by
+ parse_mime() if it was changed to use a global
+ m_getfld_state. */
+ /* We can shift readpos and remain within the
+ bounds of msg_buf. */
+ s->readpos += pos_movement;
+ s->total_bytes_read += pos_movement;
+ pos = s->last_internal_pos;
+ } else {
+ off_t off;
+ size_t num_read;
+
+ /* This seek skips past an integral number of
+ chunks of size MSG_INPUT_SIZE. */
+ off = pos / MSG_INPUT_SIZE * MSG_INPUT_SIZE;
+ if (fseeko(iob, off, SEEK_SET) == -1)
+ adios("getfld's iob", "failed to set offset to skip: "
+ "%" PRIdMAX, (intmax_t)off);
+ num_read = fread (s->msg_buf, 1, MSG_INPUT_SIZE, iob);
+ s->readpos = s->msg_buf + pos % MSG_INPUT_SIZE;
+ s->end = s->msg_buf + num_read;
+ s->total_bytes_read = pos;
+ }
+ }
+
+ if (fseeko(iob, pos, SEEK_SET) == -1)
+ adios("getfld's iob", "failed to set offset on entry: %" PRIdMAX,
+ (intmax_t)pos);
+}
+
+static void
+leave_getfld (m_getfld_state_t s) {
+ s->total_bytes_read += s->bytes_read;
+
+ if (s->track_filepos) {
+ /* Save the internal file position that we use for the input buffer. */
+ if ((s->last_internal_pos = ftello(s->iob)) == -1)
+ adios("getfld's iob", "failed to get offset before seek");
+
+ /* Set file stream position so that callers can use ftell(). */
+ if (fseeko(s->iob, s->total_bytes_read, SEEK_SET) == -1)
+ adios("getfld's iob", "failed to set offset: %" PRIdMAX,
+ (intmax_t)s->total_bytes_read);
+
+ s->last_caller_pos = s->total_bytes_read;
+ }
+}
+
+static size_t
+read_more (m_getfld_state_t s) {
+ /* Retain at least edelimlen characters that have already been read,
+ if at least edelimlen have been read, so that we can back up to them
+ in m_Eom(). */
+ ssize_t retain = s->end - s->msg_buf < s->edelimlen ? 0 : s->edelimlen;
+ size_t num_read;
+
+ if (retain > 0) {
+ if (retain < s->end - s->readpos)
+ retain = s->end - s->readpos;
+ assert (retain <= s->readpos - s->msg_buf);
+
+ /* Move what we want to retain at end of the buffer to the beginning. */
+ memmove (s->msg_buf, s->readpos - retain, retain);
+ }
+
+ s->readpos = s->msg_buf + retain;
+ num_read = fread (s->readpos, 1, MSG_INPUT_SIZE, s->iob);
+ s->end = s->readpos + num_read;
+
+ return num_read;
+}
+
+/* Return the next character consumed from the input, fetching more of
+ * the input for the buffer if required, or EOF on end of file. */
+static int
+Getc (m_getfld_state_t s) {
+ if ((s->end - s->readpos < 1 && read_more (s) == 0) ||
+ s->readpos >= s->end)
+ return EOF;
+
+ s->bytes_read++;
+ return (unsigned char)*s->readpos++;
+}
+
+/* Return the next character that Getc() would return, which may be EOF. */
+static int
+Peek (m_getfld_state_t s)
+{
+ int c;
+
+ c = Getc(s);
+ if (c != EOF)
+ Ungetc(s);
+
+ return c;
+}
+
+/* If there's room, undo the consumption of one character from msg_buf,
+ * rewinding so it's read next, else die. */
+static void
+Ungetc(m_getfld_state_t s)
+{
+ if (s->readpos == s->msg_buf)
+ adios(NULL, "Ungetc() at start of message buffer.");
+
+ s->readpos--;
+ s->bytes_read--;
+}