You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by Marc Slemko <ma...@hyperreal.org> on 1997/07/12 22:20:37 UTC

cvs commit: apache/src mod_mime_magic.c

marc        97/07/12 13:20:36

  Modified:    src       mod_mime_magic.c
  Log:
  General mod_mime_magic cleanup.
  
  The fixes include:
  
  	- change DEBUG define to MIME_MAGIC_DEBUG to avoid namespace
  	  collisions.
  	- change fprintf(stderr, ...) into log_printf
  	- remove '\n's from log_printf calls
  	- couple of possible buffer overflow cleanups
  	- minor spelling cleanup
  
  Reviewed by:	Ian Kluft, Jim Jagielski
  
  Revision  Changes    Path
  1.3       +124 -168  apache/src/mod_mime_magic.c
  
  Index: mod_mime_magic.c
  ===================================================================
  RCS file: /export/home/cvs/apache/src/mod_mime_magic.c,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -C3 -r1.2 -r1.3
  *** mod_mime_magic.c	1997/07/08 23:11:41	1.2
  --- mod_mime_magic.c	1997/07/12 20:20:35	1.3
  ***************
  *** 53,59 ****
    /*
     * mod_mime_magic: MIME type lookup via file magic numbers
     * Copyright (c) 1996-1997 Cisco Systems, Inc.
  -  * $Revision: 1.2 $
     * 
     * This software was submitted by Cisco Systems to the Apache Group in July
     * 1997.  Future revisions and derivatives of this source code must
  --- 53,58 ----
  ***************
  *** 87,93 ****
     * 4. This notice may not be removed or altered.
     * -------------------------------------------------------------------------
     * 
  !  * For complicance with Mr Darwin's terms: this has been very significantly
     * modified from the free "file" command.
     * - all-in-one file for compilation convenience when moving from one
     *   version of Apache to the next.
  --- 86,92 ----
     * 4. This notice may not be removed or altered.
     * -------------------------------------------------------------------------
     * 
  !  * For compliance with Mr Darwin's terms: this has been very significantly
     * modified from the free "file" command.
     * - all-in-one file for compilation convenience when moving from one
     *   version of Apache to the next.
  ***************
  *** 121,127 ****
    
    #include <sys/wait.h>
    #include <sys/stat.h>
  - #include <sys/sysmacros.h>
    #include <time.h>
    #include <utime.h>
    #include <stdarg.h>
  --- 120,125 ----
  ***************
  *** 138,144 ****
     */
    
    #define MODNAME        "mod_mime_magic"
  ! #define DEBUG        0
    
    #ifndef MAGIC
    #define MAGIC "conf/magic"
  --- 136,142 ----
     */
    
    #define MODNAME        "mod_mime_magic"
  ! #define MIME_MAGIC_DEBUG        0
    
    #ifndef MAGIC
    #define MAGIC "conf/magic"
  ***************
  *** 250,256 ****
    
    static int getvalue(server_rec *, struct magic *, char **);
    static int hextoint(int);
  ! static char *getstr(char *, char *, int, int *);
    static int parse(server_rec *, pool * p, char *, int);
    
    static int match(request_rec *, unsigned char *, int);
  --- 248,254 ----
    
    static int getvalue(server_rec *, struct magic *, char **);
    static int hextoint(int);
  ! static char *getstr(server_rec *, char *, char *, int, int *);
    static int parse(server_rec *, pool * p, char *, int);
    
    static int match(request_rec *, unsigned char *, int);
  ***************
  *** 262,268 ****
    static int mconvert(request_rec *, union VALUETYPE *, struct magic *);
    
    static int 
  ! uncompress(server_rec *, int, const unsigned char *,
           unsigned char **, int);
    static long from_oct(int, char *);
    static int fsmagic(request_rec * r, const char *fn, struct stat * sb);
  --- 260,266 ----
    static int mconvert(request_rec *, union VALUETYPE *, struct magic *);
    
    static int 
  ! uncompress(request_rec *, int, const unsigned char *,
           unsigned char **, int);
    static long from_oct(int, char *);
    static int fsmagic(request_rec * r, const char *fn, struct stat * sb);
  ***************
  *** 342,348 ****
        },
        {
            "/*", L_C
  !     },            /* must preced "The", "the", etc. */
        {
            "#include", L_C
        },
  --- 340,346 ----
        },
        {
            "/*", L_C
  !     },            /* must precede "The", "the", etc. */
        {
            "#include", L_C
        },
  ***************
  *** 577,585 ****
            palloc(r->pool, sizeof(magic_req_rec));
    
        if (!req_dat) {
  !         fprintf(stderr, MODNAME ": memory allocation "
  !             "failure in magic_set_config()\n");
  !         fflush(stderr);
            return NULL;
        }
        req_dat->head = req_dat->tail = (magic_rsl *) NULL;
  --- 575,582 ----
            palloc(r->pool, sizeof(magic_req_rec));
    
        if (!req_dat) {
  ! 	log_printf(r->server, "%s: memory allocation failure in "
  ! 	    "magic_set_config()", MODNAME);
            return NULL;
        }
        req_dat->head = req_dat->tail = (magic_rsl *) NULL;
  ***************
  *** 598,606 ****
    
        /* make sure we have a list to put it in */
        if (!req_dat) {
  !         fprintf(stderr, MODNAME ": request config "
  !             "should not be NULL\n");
  !         fflush(stderr);
            if (!(req_dat = magic_set_config(r))) {
                /* failure */
                return -1;
  --- 595,602 ----
    
        /* make sure we have a list to put it in */
        if (!req_dat) {
  ! 	log_printf(r->server, "%s: request config should not be NULL",
  ! 	    MODNAME);
            if (!(req_dat = magic_set_config(r))) {
                /* failure */
                return -1;
  ***************
  *** 610,617 ****
        /* allocate the list entry */
        if (!(rsl = (magic_rsl *) palloc(r->pool, sizeof(magic_rsl)))) {
            log_printf(r->server, MODNAME ": "
  !                "memory allocation failure in magic_rsl_add()\n");
  !         fflush(stderr);
            /* failure */
            return -1;
        }
  --- 606,612 ----
        /* allocate the list entry */
        if (!(rsl = (magic_rsl *) palloc(r->pool, sizeof(magic_rsl)))) {
            log_printf(r->server, MODNAME ": "
  !                "memory allocation failure in magic_rsl_add()");
            /* failure */
            return -1;
        }
  ***************
  *** 646,657 ****
    {
        va_list ap;
    
  -     /* make sure this is long enough since most OS's have no vsnprintf() */
        char buf[MAXMIMESTRING];
    
        /* assemble the string into the buffer */
        va_start(ap, str);
  !     vsprintf(buf, str, ap);
        va_end(ap);
    
        /* add the buffer to the list */
  --- 641,651 ----
    {
        va_list ap;
    
        char buf[MAXMIMESTRING];
    
        /* assemble the string into the buffer */
        va_start(ap, str);
  !     ap_vsnprintf(buf, sizeof(buf), str, ap);
        va_end(ap);
    
        /* add the buffer to the list */
  ***************
  *** 686,692 ****
        /* allocate the result string */
        if (!(result = (char *) palloc(r->pool, len + 1))) {
            log_printf(r->server, MODNAME ": "
  !                "memory allocation failure in rsl_strdup()\n");
            return NULL;
        }
    
  --- 680,686 ----
        /* allocate the result string */
        if (!(result = (char *) palloc(r->pool, len + 1))) {
            log_printf(r->server, MODNAME ": "
  !                "memory allocation failure in rsl_strdup()");
            return NULL;
        }
    
  ***************
  *** 716,726 ****
    
        /* clean up and return */
        result[res_pos] = 0;
  ! #if DEBUG
  !     fprintf(stderr, MODNAME ": rsl_strdup() %d chars: %s\n",
            res_pos - 1, result);
  -     fflush(stderr);
  -     fflush(stderr);
    #endif
        return result;
    }
  --- 710,718 ----
    
        /* clean up and return */
        result[res_pos] = 0;
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(r->server, MODNAME ": rsl_strdup() %d chars: %s",
            res_pos - 1, result);
    #endif
        return result;
    }
  ***************
  *** 793,799 ****
                        /* should not be possible */
                        /* abandon malfunctioning module */
                        log_printf(r->server,
  !                            "%s: bad state %d (ws)\n",
                               MODNAME);
                        return DECLINED;
                    }
  --- 785,791 ----
                        /* should not be possible */
                        /* abandon malfunctioning module */
                        log_printf(r->server,
  !                            "%s: bad state %d (ws)",
                               MODNAME);
                        return DECLINED;
                    }
  ***************
  *** 838,844 ****
                        /* should not be possible */
                        /* abandon malfunctioning module */
                        log_printf(r->server,
  !                            "%s: bad state %d (ns)\n",
                               MODNAME);
                        return DECLINED;
                    }
  --- 830,836 ----
                        /* should not be possible */
                        /* abandon malfunctioning module */
                        log_printf(r->server,
  !                            "%s: bad state %d (ns)",
                               MODNAME);
                        return DECLINED;
                    }
  ***************
  *** 868,874 ****
        /* detect memory allocation errors */
        if (!r->content_type ||
            (state == rsl_encoding && !r->content_encoding)) {
  !         return 500;
        }
    
        /* success! */
  --- 860,866 ----
        /* detect memory allocation errors */
        if (!r->content_type ||
            (state == rsl_encoding && !r->content_encoding)) {
  !         return HTTP_INTERNAL_SERVER_ERROR;
        }
    
        /* success! */
  ***************
  *** 877,883 ****
    
    /*
     * magic_process - process input file r        Apache API request record
  !  * (formerly caled "process" in file command, prefix added for clarity) Opens
     * the file and reads a fixed-size buffer to begin processing the contents.
     */
    static void
  --- 869,875 ----
    
    /*
     * magic_process - process input file r        Apache API request record
  !  * (formerly called "process" in file command, prefix added for clarity) Opens
     * the file and reads a fixed-size buffer to begin processing the contents.
     */
    static void
  ***************
  *** 900,909 ****
        if ((fd = open(r->filename, O_RDONLY)) < 0) {
            /* We can't open it, but we were able to stat it. */
            /*
  !          * if (sb.st_mode & 0002) magic_rsl_puts(r,"writeable, "); if
             * (sb.st_mode & 0111) magic_rsl_puts(r,"executable, ");
             */
  !         log_printf(r->server, "can't read `%s' (%s).\n",
                   r->filename, strerror(errno));
            return;
        }
  --- 892,901 ----
        if ((fd = open(r->filename, O_RDONLY)) < 0) {
            /* We can't open it, but we were able to stat it. */
            /*
  !          * if (sb.st_mode & 0002) magic_rsl_puts(r,"writable, "); if
             * (sb.st_mode & 0111) magic_rsl_puts(r,"executable, ");
             */
  !         log_printf(r->server, "can't read `%s' (%s).",
                   r->filename, strerror(errno));
            return;
        }
  ***************
  *** 912,918 ****
         * try looking at the first HOWMANY bytes
         */
        if ((nbytes = read(fd, (char *) buf, HOWMANY)) == -1) {
  !         log_printf(r->server, "read failed (%s).\n", strerror(errno));
            /* NOTREACHED */
        }
    
  --- 904,910 ----
         * try looking at the first HOWMANY bytes
         */
        if ((nbytes = read(fd, (char *) buf, HOWMANY)) == -1) {
  !         log_printf(r->server, "read failed (%s).", strerror(errno));
            /* NOTREACHED */
        }
    
  ***************
  *** 975,982 ****
        char line[BUFSIZ + 1];
        int errs = 0;
        int lineno;
  ! #if DEBUG
  !     int rule;
        struct magic *m, *prevm;
    #endif
        char *fname;
  --- 967,974 ----
        char line[BUFSIZ + 1];
        int errs = 0;
        int lineno;
  ! #if MIME_MAGIC_DEBUG
  !     int rule = 0;
        struct magic *m, *prevm;
    #endif
        char *fname;
  ***************
  *** 990,1000 ****
        fname = server_root_relative(p, conf->magicfile);
        f = pfopen(p, fname, "r");
        if (f == NULL) {
  !         (void) fprintf(stderr, "%s: can't read magic file %s\n",
  !                    MODNAME, fname);
  !         fflush(stderr);
  !         perror(MODNAME);
  !         fflush(stderr);
            return -1;
        }
    
  --- 982,989 ----
        fname = server_root_relative(p, conf->magicfile);
        f = pfopen(p, fname, "r");
        if (f == NULL) {
  ! 	log_printf(s, "%s: can't read magic file %s: %s",
  !             MODNAME, fname, strerror(errno));
            return -1;
        }
    
  ***************
  *** 1025,1031 ****
            if (line[ws_offset] == '#')
                continue;
    
  ! #if DEBUG
            /* if we get here, we're going to use it so count it */
            rule++;
    #endif
  --- 1014,1020 ----
            if (line[ws_offset] == '#')
                continue;
    
  ! #if MIME_MAGIC_DEBUG
            /* if we get here, we're going to use it so count it */
            rule++;
    #endif
  ***************
  *** 1037,1066 ****
    
        (void) pfclose(p, f);
    
  ! #if DEBUG
  !     fprintf(stderr,
  !         "%s: apprentice conf=%x file=%s m=%s m->next=%s last=%s\n",
            MODNAME, conf,
            conf->magicfile ? conf->magicfile : "NULL",
            conf->magic ? "set" : "NULL",
            (conf->magic && conf->magic->next) ? "set" : "NULL",
            conf->last ? "set" : "NULL");
  !     fprintf(stderr, "%s: apprentice read %d lines, %d rules, %d errors\n",
            MODNAME, lineno, rule, errs);
  -     fflush(stderr);
    #endif
    
  ! #if DEBUG
        prevm = 0;
  !     fprintf(stderr, "%s: apprentice test\n", MODNAME);
        for (m = conf->magic; m; m = m->next) {
            if (isprint((((unsigned long) m) >> 24) & 255) &&
                isprint((((unsigned long) m) >> 16) & 255) &&
                isprint((((unsigned long) m) >> 8) & 255) &&
                isprint(((unsigned long) m) & 255)) {
  !             fprintf(stderr, "%s: apprentice: "
                    "POINTER CLOBBERED! "
  !                 "m=\"%c%c%c%c\" line=%d\n", MODNAME,
                    (((unsigned long) m) >> 24) & 255,
                    (((unsigned long) m) >> 16) & 255,
                    (((unsigned long) m) >> 8) & 255,
  --- 1026,1054 ----
    
        (void) pfclose(p, f);
    
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(s,
  !         "%s: apprentice conf=%x file=%s m=%s m->next=%s last=%s",
            MODNAME, conf,
            conf->magicfile ? conf->magicfile : "NULL",
            conf->magic ? "set" : "NULL",
            (conf->magic && conf->magic->next) ? "set" : "NULL",
            conf->last ? "set" : "NULL");
  !     log_printf(s, "%s: apprentice read %d lines, %d rules, %d errors",
            MODNAME, lineno, rule, errs);
    #endif
    
  ! #if MIME_MAGIC_DEBUG
        prevm = 0;
  !     log_printf(s, "%s: apprentice test", MODNAME);
        for (m = conf->magic; m; m = m->next) {
            if (isprint((((unsigned long) m) >> 24) & 255) &&
                isprint((((unsigned long) m) >> 16) & 255) &&
                isprint((((unsigned long) m) >> 8) & 255) &&
                isprint(((unsigned long) m) & 255)) {
  !             log_printf(s, "%s: apprentice: "
                    "POINTER CLOBBERED! "
  !                 "m=\"%c%c%c%c\" line=%d", MODNAME,
                    (((unsigned long) m) >> 24) & 255,
                    (((unsigned long) m) >> 16) & 255,
                    (((unsigned long) m) >> 8) & 255,
  ***************
  *** 1070,1076 ****
            }
            prevm = m;
        }
  -     fflush(stderr);
    #endif
    
        return (errs ? -1 : 0);
  --- 1058,1063 ----
  ***************
  *** 1108,1117 ****
            case STRING:
                break;
            default:
  !             fprintf(stderr, "%s: can't happen: m->type=%d\n",
                    MODNAME, m->type);
  -             fflush(stderr);
  -             fflush(stderr);
                return -1;
            }
        return v;
  --- 1095,1102 ----
            case STRING:
                break;
            default:
  !             log_printf(s, "%s: can't happen: m->type=%d",
                    MODNAME, m->type);
                return -1;
            }
        return v;
  ***************
  *** 1131,1138 ****
    
        /* allocate magic structure entry */
        if ((m = (struct magic *) pcalloc(p, sizeof(struct magic))) == NULL) {
  !         (void) fprintf(stderr, "%s: Out of memory.\n", MODNAME);
  !         fflush(stderr);
            return -1;
        }
    
  --- 1116,1122 ----
    
        /* allocate magic structure entry */
        if ((m = (struct magic *) pcalloc(p, sizeof(struct magic))) == NULL) {
  !         (void) log_printf(serv, "%s: Out of memory.", MODNAME);
            return -1;
        }
    
  ***************
  *** 1164,1171 ****
        /* get offset, then skip over it */
        m->offset = (int) strtol(l, &t, 0);
        if (l == t) {
  !         fprintf(stderr, "%s: offset %s invalid", MODNAME, l);
  !         fflush(stderr);
        }
        l = t;
    
  --- 1148,1154 ----
        /* get offset, then skip over it */
        m->offset = (int) strtol(l, &t, 0);
        if (l == t) {
  !         log_printf(serv, "%s: offset %s invalid", MODNAME, l);
        }
        l = t;
    
  ***************
  *** 1187,1194 ****
                    m->in.type = BYTE;
                    break;
                default:
  !                 fprintf(stderr, "%s: indirect offset type %c invalid", MODNAME, *l);
  !                 fflush(stderr);
                    break;
                }
                l++;
  --- 1170,1176 ----
                    m->in.type = BYTE;
                    break;
                default:
  !                 log_printf(serv, "%s: indirect offset type %c invalid", MODNAME, *l);
                    break;
                }
                l++;
  ***************
  *** 1204,1212 ****
            else
                t = l;
            if (*t++ != ')') {
  !             fprintf(stderr, "%s: missing ')' in indirect offset",
                    MODNAME);
  -             fflush(stderr);
            }
            l = t;
        }
  --- 1186,1193 ----
            else
                t = l;
            if (*t++ != ')') {
  !             log_printf(serv, "%s: missing ')' in indirect offset",
                    MODNAME);
            }
            l = t;
        }
  ***************
  *** 1279,1286 ****
            l += NLEDATE;
        }
        else {
  !         fprintf(stderr, "%s: type %s invalid", MODNAME, l);
  !         fflush(stderr);
            return -1;
        }
        /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
  --- 1260,1266 ----
            l += NLEDATE;
        }
        else {
  !         log_printf(serv, "%s: type %s invalid", MODNAME, l);
            return -1;
        }
        /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
  ***************
  *** 1342,1353 ****
        while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
             /* NULLBODY */ ;
    
  ! #if DEBUG
  !     fprintf(stderr, "%s: parse line=%d m=%x next=%x cont=%d desc=%s\n",
            MODNAME, lineno, m, m->next, m->cont_level,
            m->desc ? m->desc : "NULL");
  !     fflush(stderr);
  ! #endif                /* DEBUG */
    
        return 0;
    }
  --- 1322,1332 ----
        while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
             /* NULLBODY */ ;
    
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(serv, "%s: parse line=%d m=%x next=%x cont=%d desc=%s",
            MODNAME, lineno, m, m->next, m->cont_level,
            m->desc ? m->desc : "NULL");
  ! #endif                /* MIME_MAGIC_DEBUG */
    
        return 0;
    }
  ***************
  *** 1363,1369 ****
        int slen;
    
        if (m->type == STRING) {
  !         *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
            m->vallen = slen;
        }
        else if (m->reln != 'x')
  --- 1342,1348 ----
        int slen;
    
        if (m->type == STRING) {
  !         *p = getstr(s, *p, m->value.s, sizeof(m->value.s), &slen);
            m->vallen = slen;
        }
        else if (m->reln != 'x')
  ***************
  *** 1377,1386 ****
     * *slen. Return updated scan pointer as function result.
     */
    static char *
  ! getstr(s, p, plen, slen)
  !     register char *s;
  !     register char *p;
  !     int plen, *slen;
    {
        char *origs = s, *origp = p;
        char *pmax = p + plen - 1;
  --- 1356,1363 ----
     * *slen. Return updated scan pointer as function result.
     */
    static char *
  ! getstr(server_rec *serv, register char *s, register char *p, 
  !     int plen, int *slen)
    {
        char *origs = s, *origp = p;
        char *pmax = p + plen - 1;
  ***************
  *** 1391,1398 ****
            if (isspace((unsigned char) c))
                break;
            if (p >= pmax) {
  !             fprintf(stderr, "String too long: %s\n", origs);
  !             fflush(stderr);
                break;
            }
            if (c == '\\') {
  --- 1368,1374 ----
            if (isspace((unsigned char) c))
                break;
            if (p >= pmax) {
  !             log_printf(serv, "String too long: %s", origs);
                break;
            }
            if (c == '\\') {
  ***************
  *** 1517,1525 ****
        ret = stat(fn, sb);    /* don't merge into if; see "ret =" above */
    
        if (ret) {
  -         log_printf(r->server,
  -         /* No \n, caller will provide. */
  -                "can't stat `%s' (%s).", fn, strerror(errno));
            return 1;
        }
    
  --- 1493,1498 ----
  ***************
  *** 1593,1602 ****
                                 * anyway */
                    }
                    else {
  !                     strcpy(buf2, fn);    /* take directory part */
  !                     buf2[tmp - fn + 1] = '\0';
  !                     strcat(buf2, buf);    /* plus (relative)
  !                                  * symlink */
                        tmp = buf2;
                    }
                    if (stat(tmp, &tstatbuf) < 0) {
  --- 1566,1574 ----
                                 * anyway */
                    }
                    else {
  ! 		    /* directory part plus (relative) symlink */
  ! 		    ap_snprintf(buf2, sizeof(buf2), "%s%s",
  ! 			fn, buf);
                        tmp = buf2;
                    }
                    if (stat(tmp, &tstatbuf) < 0) {
  ***************
  *** 1626,1632 ****
        case S_IFREG:
            break;
        default:
  !         log_printf(r->server, "%s: invalid mode 0%o.\n", MODNAME,
                   sb->st_mode);
            /* NOTREACHED */
        }
  --- 1598,1604 ----
        case S_IFREG:
            break;
        default:
  !         log_printf(r->server, "%s: invalid mode 0%o.", MODNAME,
                   sb->st_mode);
            /* NOTREACHED */
        }
  ***************
  *** 1684,1691 ****
    static int
    match(request_rec * r, unsigned char *s, int nbytes)
    {
  ! #if DEBUG
  !     int rule_counter;
    #endif
        int cont_level = 0;
        int need_separator = 0;
  --- 1656,1663 ----
    static int
    match(request_rec * r, unsigned char *s, int nbytes)
    {
  ! #if MIME_MAGIC_DEBUG
  !     int rule_counter = 0;
    #endif
        int cont_level = 0;
        int need_separator = 0;
  ***************
  *** 1694,1734 ****
            get_module_config(r->server->module_config, &mime_magic_module);
        struct magic *m;
    
  ! #if DEBUG
  !     fprintf(stderr,
  !         "%s: match conf=%x file=%s m=%s m->next=%s last=%s\n",
            MODNAME, conf,
            conf->magicfile ? conf->magicfile : "NULL",
            conf->magic ? "set" : "NULL",
            (conf->magic && conf->magic->next) ? "set" : "NULL",
            conf->last ? "set" : "NULL");
  -     fflush(stderr);
    #endif
    
  ! #if DEBUG
        for (m = conf->magic; m; m = m->next) {
            if (isprint((((unsigned long) m) >> 24) & 255) &&
                isprint((((unsigned long) m) >> 16) & 255) &&
                isprint((((unsigned long) m) >> 8) & 255) &&
                isprint(((unsigned long) m) & 255)) {
  !             fprintf(stderr, "%s: match: POINTER CLOBBERED! "
  !                 "m=\"%c%c%c%c\"\n", MODNAME,
                    (((unsigned long) m) >> 24) & 255,
                    (((unsigned long) m) >> 16) & 255,
                    (((unsigned long) m) >> 8) & 255,
                    ((unsigned long) m) & 255);
  -             fflush(stderr);
                break;
            }
        }
    #endif
    
        for (m = conf->magic; m; m = m->next) {
  ! #if DEBUG
            rule_counter++;
  !         fprintf(stderr, "%s: line=%d desc=%s\n", MODNAME,
                m->lineno, m->desc);
  -         fflush(stderr);
    #endif
    
            /* check if main entry matches */
  --- 1666,1703 ----
            get_module_config(r->server->module_config, &mime_magic_module);
        struct magic *m;
    
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(r->server,
  !         "%s: match conf=%x file=%s m=%s m->next=%s last=%s",
            MODNAME, conf,
            conf->magicfile ? conf->magicfile : "NULL",
            conf->magic ? "set" : "NULL",
            (conf->magic && conf->magic->next) ? "set" : "NULL",
            conf->last ? "set" : "NULL");
    #endif
    
  ! #if MIME_MAGIC_DEBUG
        for (m = conf->magic; m; m = m->next) {
            if (isprint((((unsigned long) m) >> 24) & 255) &&
                isprint((((unsigned long) m) >> 16) & 255) &&
                isprint((((unsigned long) m) >> 8) & 255) &&
                isprint(((unsigned long) m) & 255)) {
  !             log_printf(r->server, "%s: match: POINTER CLOBBERED! "
  !                 "m=\"%c%c%c%c\"", MODNAME,
                    (((unsigned long) m) >> 24) & 255,
                    (((unsigned long) m) >> 16) & 255,
                    (((unsigned long) m) >> 8) & 255,
                    ((unsigned long) m) & 255);
                break;
            }
        }
    #endif
    
        for (m = conf->magic; m; m = m->next) {
  ! #if MIME_MAGIC_DEBUG
            rule_counter++;
  !         log_printf(r->server, "%s: line=%d desc=%s", MODNAME,
                m->lineno, m->desc);
    #endif
    
            /* check if main entry matches */
  ***************
  *** 1745,1759 ****
    
                m_cont = m->next;
                while (m_cont && (m_cont->cont_level != 0)) {
  ! #if DEBUG
                    rule_counter++;
  !                 fprintf(stderr,
                        "%s: line=%d mc=%x mc->next=%x "
  !                     "cont=%d desc=%s\n",
                        MODNAME, m_cont->lineno, m_cont,
                        m_cont->next, m_cont->cont_level,
                          m_cont->desc ? m_cont->desc : "NULL");
  -                 fflush(stderr);
    #endif
                    /*
                     * this trick allows us to keep *m in sync
  --- 1714,1727 ----
    
                m_cont = m->next;
                while (m_cont && (m_cont->cont_level != 0)) {
  ! #if MIME_MAGIC_DEBUG
                    rule_counter++;
  !                 log_printf(r->server,
                        "%s: line=%d mc=%x mc->next=%x "
  !                     "cont=%d desc=%s",
                        MODNAME, m_cont->lineno, m_cont,
                        m_cont->next, m_cont->cont_level,
                          m_cont->desc ? m_cont->desc : "NULL");
    #endif
                    /*
                     * this trick allows us to keep *m in sync
  ***************
  *** 1767,1777 ****
    
            /* if we get here, the main entry rule was a match */
            /* this will be the last run through the loop */
  ! #if DEBUG
  !         fprintf(stderr, "%s: rule matched, line=%d type=%d %s\n",
                MODNAME, m->lineno, m->type,
                (m->type == STRING) ? m->value.s : "");
  -         fflush(stderr);
    #endif
    
            /* print the match */
  --- 1735,1744 ----
    
            /* if we get here, the main entry rule was a match */
            /* this will be the last run through the loop */
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%s: rule matched, line=%d type=%d %s",
                MODNAME, m->lineno, m->type,
                (m->type == STRING) ? m->value.s : "");
    #endif
    
            /* print the match */
  ***************
  *** 1791,1802 ****
             */
            m = m->next;
            while (m && (m->cont_level != 0)) {
  ! #if DEBUG
  !             fprintf(stderr,
  !                 "%s: match line=%d cont=%d type=%d %s\n",
                    MODNAME, m->lineno, m->cont_level, m->type,
                    (m->type == STRING) ? m->value.s : "");
  -             fflush(stderr);
    #endif
                if (cont_level >= m->cont_level) {
                    if (cont_level > m->cont_level) {
  --- 1758,1768 ----
             */
            m = m->next;
            while (m && (m->cont_level != 0)) {
  ! #if MIME_MAGIC_DEBUG
  !             log_printf(r->server,
  !                 "%s: match line=%d cont=%d type=%d %s",
                    MODNAME, m->lineno, m->cont_level, m->type,
                    (m->type == STRING) ? m->value.s : "");
    #endif
                if (cont_level >= m->cont_level) {
                    if (cont_level > m->cont_level) {
  ***************
  *** 1837,1852 ****
                /* move to next continuation record */
                m = m->next;
            }
  ! #if DEBUG
  !         fprintf(stderr, "%s: matched after %d rules\n",
                MODNAME, rule_counter);
  -         fflush(stderr);
    #endif
            return 1;    /* all through */
        }
  ! #if DEBUG
  !     fprintf(stderr, "%s: failed after %d rules\n", MODNAME, rule_counter);
  !     fflush(stderr);
    #endif
        return 0;        /* no match at all */
    }
  --- 1803,1816 ----
                /* move to next continuation record */
                m = m->next;
            }
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%s: matched after %d rules",
                MODNAME, rule_counter);
    #endif
            return 1;    /* all through */
        }
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(r->server, "%s: failed after %d rules", MODNAME, rule_counter);
    #endif
        return 0;        /* no match at all */
    }
  ***************
  *** 1893,1899 ****
            (void) magic_rsl_printf(r, m->desc, pp);
            return;
        default:
  !         log_printf(r->server, "%s: invalid m->type (%d) in mprint().\n",
                   MODNAME, m->type);
            return;
        }
  --- 1857,1863 ----
            (void) magic_rsl_printf(r, m->desc, pp);
            return;
        default:
  !         log_printf(r->server, "%s: invalid m->type (%d) in mprint().",
                   MODNAME, m->type);
            return;
        }
  ***************
  *** 1939,1945 ****
                ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
            return 1;
        default:
  !         log_printf(r->server, "%s: invalid type %d in mconvert().\n",
                   MODNAME, m->type);
            return 0;
        }
  --- 1903,1909 ----
                ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
            return 1;
        default:
  !         log_printf(r->server, "%s: invalid type %d in mconvert().",
                   MODNAME, m->type);
            return 0;
        }
  ***************
  *** 1993,2000 ****
        int matched;
    
        if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
  !         fprintf(stderr, "BOINK");
  !         fflush(stderr);
            return 1;
        }
    
  --- 1957,1963 ----
        int matched;
    
        if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
  !         log_printf(r->server, "BOINK");
            return 1;
        }
    
  ***************
  *** 2038,2044 ****
            }
            break;
        default:
  !         log_printf(r->server, "%s: invalid type %d in mcheck().\n",
                   MODNAME, m->type);
            return 0;    /* NOTREACHED */
        }
  --- 2001,2007 ----
            }
            break;
        default:
  !         log_printf(r->server, "%s: invalid type %d in mcheck().",
                   MODNAME, m->type);
            return 0;    /* NOTREACHED */
        }
  ***************
  *** 2047,2070 ****
    
        switch (m->reln) {
        case 'x':
  ! #if DEBUG
  !         (void) fprintf(stderr, "%lu == *any* = 1\n", v);
    #endif
            matched = 1;
            break;
    
        case '!':
            matched = v != l;
  ! #if DEBUG
  !         (void) fprintf(stderr, "%lu != %lu = %d\n",
                       v, l, matched);
    #endif
            break;
    
        case '=':
            matched = v == l;
  ! #if DEBUG
  !         (void) fprintf(stderr, "%lu == %lu = %d\n",
                       v, l, matched);
    #endif
            break;
  --- 2010,2033 ----
    
        switch (m->reln) {
        case 'x':
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%lu == *any* = 1", v);
    #endif
            matched = 1;
            break;
    
        case '!':
            matched = v != l;
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%lu != %lu = %d",
                       v, l, matched);
    #endif
            break;
    
        case '=':
            matched = v == l;
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%lu == %lu = %d",
                       v, l, matched);
    #endif
            break;
  ***************
  *** 2072,2086 ****
        case '>':
            if (m->flag & UNSIGNED) {
                matched = v > l;
  ! #if DEBUG
  !             (void) fprintf(stderr, "%lu > %lu = %d\n",
                           v, l, matched);
    #endif
            }
            else {
                matched = (long) v > (long) l;
  ! #if DEBUG
  !             (void) fprintf(stderr, "%ld > %ld = %d\n",
                           v, l, matched);
    #endif
            }
  --- 2035,2049 ----
        case '>':
            if (m->flag & UNSIGNED) {
                matched = v > l;
  ! #if MIME_MAGIC_DEBUG
  !             log_printf(r->server, "%lu > %lu = %d",
                           v, l, matched);
    #endif
            }
            else {
                matched = (long) v > (long) l;
  ! #if MIME_MAGIC_DEBUG
  !             log_printf(r->server, "%ld > %ld = %d",
                           v, l, matched);
    #endif
            }
  ***************
  *** 2089,2136 ****
        case '<':
            if (m->flag & UNSIGNED) {
                matched = v < l;
  !             if (DEBUG)
  !                 (void) fprintf(stderr, "%lu < %lu = %d\n",
  !                            v, l, matched);
            }
            else {
                matched = (long) v < (long) l;
  ! #if DEBUG
  !             (void) fprintf(stderr, "%ld < %ld = %d\n",
  !                        v, l, matched);
    #endif
            }
            break;
    
        case '&':
            matched = (v & l) == l;
  ! #if DEBUG
  !         (void) fprintf(stderr, "((%lx & %lx) == %lx) = %d\n",
                       v, l, l, matched);
    #endif
            break;
    
        case '^':
            matched = (v & l) != l;
  ! #if DEBUG
  !         (void) fprintf(stderr, "((%lx & %lx) != %lx) = %d\n",
                       v, l, l, matched);
    #endif
            break;
    
        default:
            matched = 0;
  !         log_printf(r->server, "%s: mcheck: can't happen: invalid relation %d.\n", MODNAME, m->reln);
            break;        /* NOTREACHED */
        }
  - #if DEBUG
  -     fflush(stderr);
  - #endif
    
        return matched;
    }
    
  ! /* an optimisation over plain strcmp() */
    #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
    
    static int
  --- 2052,2095 ----
        case '<':
            if (m->flag & UNSIGNED) {
                matched = v < l;
  ! #if MIME_MAGIC_DEBUG
  !                 log_printf(r->server, "%lu < %lu = %d", v, l, matched);
  ! #endif
            }
            else {
                matched = (long) v < (long) l;
  ! #if MIME_MAGIC_DEBUG
  !             log_printf(r->server, "%ld < %ld = %d", v, l, matched);
    #endif
            }
            break;
    
        case '&':
            matched = (v & l) == l;
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "((%lx & %lx) == %lx) = %d",
                       v, l, l, matched);
    #endif
            break;
    
        case '^':
            matched = (v & l) != l;
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "((%lx & %lx) != %lx) = %d",
                       v, l, l, matched);
    #endif
            break;
    
        default:
            matched = 0;
  !         log_printf(r->server, "%s: mcheck: can't happen: invalid relation %d.", MODNAME, m->reln);
            break;        /* NOTREACHED */
        }
    
        return matched;
    }
    
  ! /* an optimization over plain strcmp() */
    #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
    
    static int
  ***************
  *** 2267,2275 ****
        if (i == ncompr)
            return 0;
    
  !     if ((newsize = uncompress(r->server, i, buf, &newbuf, nbytes)) != 0) {
            tryit(r, newbuf, newsize);
  -         free(newbuf);
    
            /* set encoding type in the request record */
            r->content_encoding = pstrdup(r->pool, compr[i].encoding);
  --- 2226,2233 ----
        if (i == ncompr)
            return 0;
    
  !     if ((newsize = uncompress(r, i, buf, &newbuf, nbytes)) != 0) {
            tryit(r, newbuf, newsize);
    
            /* set encoding type in the request record */
            r->content_encoding = pstrdup(r->pool, compr[i].encoding);
  ***************
  *** 2279,2291 ****
    
    
    static int
  ! uncompress(server_rec * s, int method, const unsigned char *old,
           unsigned char **newch, int n)
    {
        int fdin[2], fdout[2];
    
        if (pipe(fdin) == -1 || pipe(fdout) == -1) {
  !         log_printf(s, "%s: cannot create pipe (%s).\n",
                   MODNAME, strerror(errno));
            return -1;
        }
  --- 2237,2249 ----
    
    
    static int
  ! uncompress(request_rec *r, int method, const unsigned char *old,
           unsigned char **newch, int n)
    {
        int fdin[2], fdout[2];
    
        if (pipe(fdin) == -1 || pipe(fdout) == -1) {
  !         log_printf(r->server, "%s: cannot create pipe (%s).",
                   MODNAME, strerror(errno));
            return -1;
        }
  ***************
  *** 2304,2314 ****
                (void) close(2);
    
            execvp(compr[method].argv[0], compr[method].argv);
  !         log_printf(s, "%s: could not execute `%s' (%s).\n", MODNAME,
                   compr[method].argv[0], strerror(errno));
            return -1;
        case -1:
  !         log_printf(s, "%s: could not fork (%s).\n", MODNAME,
                   strerror(errno));
            return -1;
    
  --- 2262,2272 ----
                (void) close(2);
    
            execvp(compr[method].argv[0], compr[method].argv);
  !         log_printf(r->server, "%s: could not execute `%s' (%s).", MODNAME,
                   compr[method].argv[0], strerror(errno));
            return -1;
        case -1:
  !         log_printf(r->server, "%s: could not fork (%s).", MODNAME,
                   strerror(errno));
            return -1;
    
  ***************
  *** 2316,2334 ****
            (void) close(fdin[0]);
            (void) close(fdout[1]);
            if (write(fdin[1], old, n) != n) {
  !             log_printf(s, "%s: write failed (%s).\n", MODNAME,
                       strerror(errno));
                return -1;
            }
            (void) close(fdin[1]);
  !         if ((*newch = (unsigned char *) malloc(n)) == NULL) {
  !             log_printf(s, "%s: out of memory in uncompress()\n",
                       MODNAME);
                return -1;
            }
            if ((n = read(fdout[0], *newch, n)) <= 0) {
  !             free(*newch);
  !             log_printf(s, "%s: read failed (%s).\n", MODNAME,
                       strerror(errno));
                return -1;
            }
  --- 2274,2291 ----
            (void) close(fdin[0]);
            (void) close(fdout[1]);
            if (write(fdin[1], old, n) != n) {
  !             log_printf(r->server, "%s: write failed (%s).", MODNAME,
                       strerror(errno));
                return -1;
            }
            (void) close(fdin[1]);
  !         if ((*newch = (unsigned char *) palloc(r->pool, n)) == NULL) {
  !             log_printf(r->server, "%s: out of memory in uncompress()",
                       MODNAME);
                return -1;
            }
            if ((n = read(fdout[0], *newch, n)) <= 0) {
  !             log_printf(r->server, "%s: read failed (%s).", MODNAME,
                       strerror(errno));
                return -1;
            }
  ***************
  *** 2429,2435 ****
     * Check for file-revision suffix
     *
     * This is for an obscure document control system used on an intranet.
  !  * The web respresentation of each file's revision has an @1, @2, etc
     * appended with the revision number.  This needs to be stripped off to
     * find the file suffix, which can be recognized by sending the name back
     * through a sub-request.  The base file name (without the @num suffix)
  --- 2386,2392 ----
     * Check for file-revision suffix
     *
     * This is for an obscure document control system used on an intranet.
  !  * The web representation of each file's revision has an @1, @2, etc
     * appended with the revision number.  This needs to be stripped off to
     * find the file suffix, which can be recognized by sending the name back
     * through a sub-request.  The base file name (without the @num suffix)
  ***************
  *** 2442,2451 ****
        char *sub_filename;
        request_rec *sub;
    
  ! #if DEBUG
  !     fprintf(stderr, "%s: revision_suffix checking%s\n", MODNAME,
            r->filename);
  ! #endif                /* DEBUG */
    
        /* check for recognized revision suffix */
        suffix_pos = strlen(r->filename) - 1;
  --- 2399,2408 ----
        char *sub_filename;
        request_rec *sub;
    
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(r->server, "%s: revision_suffix checking%s", MODNAME,
            r->filename);
  ! #endif                /* MIME_MAGIC_DEBUG */
    
        /* check for recognized revision suffix */
        suffix_pos = strlen(r->filename) - 1;
  ***************
  *** 2461,2478 ****
        /* perform sub-request for the file name without the suffix */
        result = 0;
        sub_filename = pstrndup(r->pool, r->filename, suffix_pos);
  ! #if DEBUG
  !     fprintf(stderr, "%s: subrequest lookup for %s\n", MODNAME, sub_filename);
  ! #endif                /* DEBUG */
        sub = sub_req_lookup_file(sub_filename, r);
    
        /* extract content type/encoding/language from sub-request */
        if (sub->content_type) {
            r->content_type = pstrdup(r->pool, sub->content_type);
  ! #if DEBUG
  !         fprintf(stderr, "%s: subrequest %s got %s\n", MODNAME,
                sub_filename, r->content_type);
  ! #endif                /* DEBUG */
            if (sub->content_encoding)
                r->content_encoding =
                    pstrdup(r->pool, sub->content_encoding);
  --- 2418,2435 ----
        /* perform sub-request for the file name without the suffix */
        result = 0;
        sub_filename = pstrndup(r->pool, r->filename, suffix_pos);
  ! #if MIME_MAGIC_DEBUG
  !     log_printf(r->server, "%s: subrequest lookup for %s", MODNAME, sub_filename);
  ! #endif                /* MIME_MAGIC_DEBUG */
        sub = sub_req_lookup_file(sub_filename, r);
    
        /* extract content type/encoding/language from sub-request */
        if (sub->content_type) {
            r->content_type = pstrdup(r->pool, sub->content_type);
  ! #if MIME_MAGIC_DEBUG
  !         log_printf(r->server, "%s: subrequest %s got %s", MODNAME,
                sub_filename, r->content_type);
  ! #endif                /* MIME_MAGIC_DEBUG */
            if (sub->content_encoding)
                r->content_encoding =
                    pstrdup(r->pool, sub->content_encoding);
  ***************
  *** 2498,2523 ****
        int result;
        magic_server_config_rec *conf = (magic_server_config_rec *)
            get_module_config(s->module_config, &mime_magic_module);
  ! #if DEBUG
        struct magic *m, *prevm;
  ! #endif                /* DEBUG */
    
        /* on the first time through we read the magic file */
        if (conf->magicfile && !conf->magic) {
            result = apprentice(s, p);
            if (result == -1)
                return;
  ! #if DEBUG
            prevm = 0;
  !         fprintf(stderr, "%s: magic_init 1 test\n", MODNAME);
            for (m = conf->magic; m; m = m->next) {
                if (isprint((((unsigned long) m) >> 24) & 255) &&
                    isprint((((unsigned long) m) >> 16) & 255) &&
                    isprint((((unsigned long) m) >> 8) & 255) &&
                    isprint(((unsigned long) m) & 255)) {
  !                 fprintf(stderr, "%s: magic_init 1: "
                        "POINTER CLOBBERED! "
  !                     "m=\"%c%c%c%c\" line=%d\n", MODNAME,
                        (((unsigned long) m) >> 24) & 255,
                        (((unsigned long) m) >> 16) & 255,
                        (((unsigned long) m) >> 8) & 255,
  --- 2455,2480 ----
        int result;
        magic_server_config_rec *conf = (magic_server_config_rec *)
            get_module_config(s->module_config, &mime_magic_module);
  ! #if MIME_MAGIC_DEBUG
        struct magic *m, *prevm;
  ! #endif                /* MIME_MAGIC_DEBUG */
    
        /* on the first time through we read the magic file */
        if (conf->magicfile && !conf->magic) {
            result = apprentice(s, p);
            if (result == -1)
                return;
  ! #if MIME_MAGIC_DEBUG
            prevm = 0;
  !         log_printf(s, "%s: magic_init 1 test", MODNAME);
            for (m = conf->magic; m; m = m->next) {
                if (isprint((((unsigned long) m) >> 24) & 255) &&
                    isprint((((unsigned long) m) >> 16) & 255) &&
                    isprint((((unsigned long) m) >> 8) & 255) &&
                    isprint(((unsigned long) m) & 255)) {
  !                 log_printf(s, "%s: magic_init 1: "
                        "POINTER CLOBBERED! "
  !                     "m=\"%c%c%c%c\" line=%d", MODNAME,
                        (((unsigned long) m) >> 24) & 255,
                        (((unsigned long) m) >> 16) & 255,
                        (((unsigned long) m) >> 8) & 255,
  ***************
  *** 2527,2533 ****
                }
                prevm = m;
            }
  -         fflush(stderr);
    #endif
        }
    
  --- 2484,2489 ----
  ***************
  *** 2580,2586 ****
    module mime_magic_module = {
        STANDARD_MODULE_STUFF,
        magic_init,      /* initializer */
  !     NULL,            /* dir config creater */
        NULL,            /* dir merger --- default is to override */
        create_magic_server_config,    /* server config */
        merge_magic_server_config,    /* merge server config */
  --- 2536,2542 ----
    module mime_magic_module = {
        STANDARD_MODULE_STUFF,
        magic_init,      /* initializer */
  !     NULL,            /* dir config creator */
        NULL,            /* dir merger --- default is to override */
        create_magic_server_config,    /* server config */
        merge_magic_server_config,    /* merge server config */