tOptimizing sgetbound with memmem. - rohrpost - A commandline mail client to change the world as we see it.
 (HTM) git clone git://r-36.net/rohrpost
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit e87694cbf0a912cd2a75046038ecaa7cb02fa55f
 (DIR) parent 7e18aa00e7e99d563b9a032b444e08090987f7a2
 (HTM) Author: Christoph Lohmann <20h@r-36.net>
       Date:   Fri, 28 Feb 2014 19:55:43 +0100
       
       Optimizing sgetbound with memmem.
       
       Diffstat:
         config.mk                           |       2 +-
         mime.c                              |     114 +++++++++----------------------
       
       2 files changed, 34 insertions(+), 82 deletions(-)
       ---
 (DIR) diff --git a/config.mk b/config.mk
       t@@ -14,7 +14,7 @@ INCS = -I. -I/usr/include
        LIBS = -L/usr/lib -lc -lssl -lcrypto -lz -ldl
        
        # flags
       -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_XOPEN_SOURCE=600 -D_SVID_SOURCE
       +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_XOPEN_SOURCE=600 -D_SVID_SOURCE -D_GNU_SOURCE
        CFLAGS = -g -std=gnu99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
        LDFLAGS = -g ${LIBS}
        #LDFLAGS = -s ${LIBS}
 (DIR) diff --git a/mime.c b/mime.c
       t@@ -734,7 +734,7 @@ char *
        mime_sgetbound(char *bound, char **p, char *max, int *len, int *choice)
        {
                char *ret, *op;
       -        int slen, foundit, isnl, isend, beginning;
       +        int slen, isenl, isend, sublen;
        
                ret = NULL;
        
       t@@ -742,94 +742,46 @@ mime_sgetbound(char *bound, char **p, char *max, int *len, int *choice)
                //printf("p = '%s'\n", *p);
                slen = strlen(bound);
                *choice = 0;
       -        foundit = 0;
       -        isnl = 0;
       -        beginning = 0;
       +        isenl = 0;
                isend = 0;
       -        for (op = *p; op < max; op++) {
       -                /*
       -                 * Handling invalid nested boundaries.
       -                 *
       -                 * It's sad that you have to handle history; the disgrace
       -                 * of all software development.
       -                 */
       -                if (op == *p && op+slen < max
       -                                && !strncmp(op, bound, slen)) {
       -                        //printf("boundary: non-standard.\n");
       -                        if (!strncmp(op+slen, "--\r\n", 4)) {
       -                                foundit = 1;
       -                                isend = 1;
       -                                break;
       -                        }
       -                        if (!strncmp(op+slen, "--\n", 3)) {
       -                                isend = 1;
       -                                foundit = 1;
       -                                isnl = 1;
       -                                break;
       -                        }
       -                        if (!strncmp(op+slen, "\r\n", 2)) {
       -                                foundit = 1;
       -                                break;
       -                        }
       -                        if (!strncmp(op+slen, "\n", 1)) {
       -                                foundit = 1;
       -                                isnl = 1;
       -                                break;
       -                        }
       -                }
       +        sublen = 0;
        
       -                /*
       -                 * Ahh, standard compliance!
       -                 */
       -                if (!strncmp(op, "\r\n", 2) && op+2+slen < max
       -                                && !strncmp(op+2, bound, slen)) {
       -                        //printf("boundary: standard '\\r\\n'.\n");
       -                        if (!strncmp(op+2+slen, "--\r\n", 4)) {
       -                                isend = 1;
       -                                foundit = 1;
       -                                beginning = 1;
       -                                break;
       -                        }
       -                        if (!strncmp(op+2+slen, "\r\n", 2)) {
       -                                foundit = 1;
       -                                beginning = 1;
       -                                break;
       -                        }
       +        for (;;) {
       +                op = memmem(*p, (max-(*p)), bound, slen);
       +                if (op == NULL)
       +                        return ret;
       +
       +                if (!strncmp(op+slen, "--", 2)) {
       +                        isend = 1;
       +                        if (op[slen+2] == '\n')
       +                                isenl = 1;
       +                } else if (op[slen] == '\n') {
       +                        isenl = 1;
                        }
       +                //printf("isenl = %d, isend = %d\n", isenl, isend);
        
       -                if (!strncmp(op, "\n", 1) && op+1+slen < max
       -                                && !strncmp(op+1, bound, slen)) {
       -                        //printf("boundary: standard '\\n'.\n");
       -                        if (!strncmp(op+1+slen, "--\n", 3)) {
       -                                isend = 1;
       -                                foundit = 1;
       -                                isnl = 1;
       -                                beginning = 1;
       -                                break;
       -                        }
       -                        if (!strncmp(op+1+slen, "\n", 1)) {
       -                                foundit = 1;
       -                                isnl = 1;
       -                                beginning = 1;
       -                                break;
       -                        }
       -                }
       +                if (op == *p)
       +                        break;
       +
       +                if (op > (*p + 1) && op[-2] == '\r' && op[-1] == '\n')
       +                        sublen = 2;
       +                if (op > *p && op[-2] != '\r' && op[-1] == '\n')
       +                        sublen = 1;
       +                //printf("sublen = %d\n", sublen);
       +                break;
       +        }
       +
       +        if (isend) {
       +                *choice = 1;
       +                slen += 2;
                }
       -        if (foundit) {
       -                //printf("foundit = %d, shortending = %d, beginning = %d\n",
       -                //                foundit, shortending, beginning);
       -                if (isend) {
       -                        *choice = 1;
       -                        slen += 2;
       -                }
        
       -                *len = op - *p;
       -                ret = memdupz(*p, *len);
       +        *len = op - *p - sublen;
       +        ret = memdupz(*p, *len);
        
       -                *p = op + (1 + beginning) * (2 - isnl) + slen;
       +        *p = op + slen + (isend * 2) + (2 - isenl);
        
       -                //printf("p = '%s'\n", *p);
       -        }
       +        //printf("p = '%s'\n", *p);
        
                return ret;
        }