]> diplodocus.org Git - nmh/commitdiff
Replace use of ftell() in mhparse.c:get_content() by keeping track
authorDavid Levine <levinedl@acm.org>
Thu, 27 Dec 2012 15:51:09 +0000 (09:51 -0600)
committerDavid Levine <levinedl@acm.org>
Thu, 27 Dec 2012 15:51:09 +0000 (09:51 -0600)
of bytes read by m_getfld().

sbr/m_getfld.c
uip/mhparse.c

index 27e3d3bc668437766d2fa94bef2975860ca0a4b2..c8a2d8b8274fe7d5f7929f4c48f520fe8cab895a 100644 (file)
@@ -297,12 +297,12 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
 {
     register unsigned char  *bp, *cp, *ep, *sp;
     register int cnt, c, i, j, k;
+    long bytes_read = 0;
 
     setup_buffer (iob, &m);
 
     if ((c = Getc(iob)) < 0) {
-       *bufsz = 0;
-       *buf = 0;
+       *bufsz = *buf = 0;
        return FILEEOF;
     }
     if (eom (c, iob)) {
@@ -314,8 +314,7 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
            if (c >= 0)
                Ungetc(c, iob);
        }
-       *bufsz = 0;
-       *buf = 0;
+       *bufsz = *buf = 0;
        return FILEEOF;
     }
 
@@ -325,8 +324,9 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
        case FLD:
            if (c == '\n' || c == '-') {
                /* we hit the header/body separator */
-               while (c != '\n' && (c = Getc(iob)) >= 0)
-                   ;
+               while (c != '\n' && (c = Getc(iob)) >= 0) {
+                   ++bytes_read;
+               }
 
                if (c < 0 || (c = Getc(iob)) < 0 || eom (c, iob)) {
                    if (! eom_action) {
@@ -336,8 +336,7 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
                        if (c >= 0)
                            Ungetc(c, iob);
                    }
-                   *bufsz = 0;
-                   *buf = 0;
+                   *bufsz = *buf = 0;
                    return FILEEOF;
                }
                state = BODY;
@@ -354,15 +353,17 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
                /* Store current position, ungetting the last character. */
                bp = sp = (unsigned char *) m.readpos - 1;
                j = (cnt = m.end - m.readpos + 1) < i ? cnt : i;
-               while (--j >= 0 && (c = *bp++) != ':' && c != '\n')
+               while (--j >= 0 && (c = *bp++) != ':' && c != '\n') {
                    *cp++ = c;
+                   ++bytes_read;
+               }
 
                j = bp - sp;
                if ((cnt -= j) <= 0) {
                    /* Next to force refill of the buffer here. */
                    m.readpos = m.end;
                    if (Getc (iob) == EOF) {
-                       *cp = *buf = 0;
+                       *bufsz = *cp = *buf = 0;
                        advise (NULL, "eof encountered in field \"%s\"", name);
                        return FMTERR;
                    }
@@ -392,7 +393,7 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
                    /* + 1 for the newline */
                    if (*bufsz < j + 1) {
                        /* No, it can't.  Oh well, guess we'll blow up. */
-                       *cp = *buf = 0;
+                       *bufsz = *cp = *buf = 0;
                        advise (NULL, "eol encountered in field \"%s\"", name);
                        state = FMTERR;
                        goto finish;
@@ -407,24 +408,22 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
                     * blank line in this case.  Simpler parsers (e.g. mhl)
                     * get extra newlines, but that should be harmless enough,
                     * right?  This is a corrupt message anyway. */
-                   /* emulates:  fseek (iob, ftell (iob) -(-2 + cnt + 1),
-                                        SEEK_SET) */
+                   /* emulates:  fseek (iob, ftell (iob) -2), SEEK_SET) */
                    m.readpos += cnt - 1;
-                   /* Reset file stream position so caller, e.g.,
-                      get_content, can use ftell(), etc. */
-                   fseek (iob, -cnt - 1, SEEK_CUR);
+                   *bufsz = bytes_read;
                    return BODY;
                }
                if ((i -= j) <= 0) {
-                   *cp = *buf = 0;
+                   *bufsz = *cp = *buf = 0;
                    advise (NULL, "field name \"%s\" exceeds %d bytes", name, NAMESZ - 2);
                    state = LENERR;
                    goto finish;
                }
            }
 
-           while (isspace (*--cp) && cp >= name)
-               ;
+           while (isspace (*--cp) && cp >= name) {
+               --bytes_read;
+           }
            *++cp = 0;
            /* fall through */
 
@@ -574,7 +573,7 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
            /* Advance the current position to reflect the copy out. */
            m.readpos += c;
            if (*bufsz < 0) {
-               *bufsz = c;
+               *bufsz = c + bytes_read + 1;
                return (state);
            }
            cp = buf + c;
@@ -585,7 +584,7 @@ m_getfld (int state, unsigned char name[NAMESZ], unsigned char *buf,
     }
 finish:
     *cp = 0;
-    *bufsz = cp - buf;
+    *bufsz = cp - buf + bytes_read + 1;
     return (state);
 }
 
index ea5400e07ed2194fb50e84b37075e7ed9011de48..f5a5983adadc3b56d341c9efbc5ee0709f4143a2 100644 (file)
@@ -262,6 +262,7 @@ get_content (FILE *in, char *file, int toplevel)
     char *np, *vp;
     CT ct;
     HF hp;
+    long filepos;
 
     /* allocate the content structure */
     if (!(ct = (CT) calloc (1, sizeof(*ct))))
@@ -269,7 +270,7 @@ get_content (FILE *in, char *file, int toplevel)
 
     ct->c_fp = in;
     ct->c_file = add (file, NULL);
-    ct->c_begin = ftell (ct->c_fp) + 1;
+    ct->c_begin = (filepos = ftell (ct->c_fp)) + 1;
 
     /*
      * Parse the header fields for this
@@ -282,6 +283,7 @@ get_content (FILE *in, char *file, int toplevel)
        case FLDPLUS:
        case FLDEOF:
            compnum++;
+           filepos += bufsz;
 
            /* get copies of the buffers */
            np = add (name, NULL);
@@ -299,18 +301,20 @@ get_content (FILE *in, char *file, int toplevel)
 
            /* continue, if this isn't the last header field */
            if (state != FLDEOF) {
-               ct->c_begin = ftell (in) + 1;
+               ct->c_begin = filepos + 1;
                continue;
            }
            /* else fall... */
 
        case BODY:
        case BODYEOF:
-           ct->c_begin = ftell (in) - strlen (buf);
+           filepos += bufsz;
+           ct->c_begin = filepos - strlen (buf);
            break;
 
        case FILEEOF:
-           ct->c_begin = ftell (in);
+           filepos += bufsz;
+           ct->c_begin = filepos;
            break;
 
        case LENERR: