Annotation of libwww/Library/src/HTParse.c, revision 2.26

1.1       timbl       1: /*             Parse HyperText Document Address                HTParse.c
                      2: **             ================================
2.26    ! frystyk     3: **
        !             4: ** history:
        !             5: **     May 12 94       TAB added as legal char in HTCleanTelnetString
        !             6: **
1.1       timbl       7: */
2.26    ! frystyk     8: #if 0
1.1       timbl       9: #include "HTUtils.h"
2.26    ! frystyk    10: #endif
1.1       timbl      11: #include "HTParse.h"
                     12: #include "tcp.h"
                     13: 
2.6       timbl      14: #define HEX_ESCAPE '%'
                     15: 
1.1       timbl      16: struct struct_parts {
2.20      timbl      17:        char * access;          /* Now known as "scheme" */
1.1       timbl      18:        char * host;
                     19:        char * absolute;
                     20:        char * relative;
                     21: /*     char * search;          no - treated as part of path */
                     22:        char * anchor;
                     23: };
                     24: 
                     25: 
                     26: /*     Strip white space off a string
                     27: **     ------------------------------
                     28: **
                     29: ** On exit,
                     30: **     Return value points to first non-white character, or to 0 if none.
                     31: **     All trailing white space is OVERWRITTEN with zero.
                     32: */
                     33: 
2.13      luotonen   34: PUBLIC char * HTStrip ARGS1(char *, s)
1.1       timbl      35: {
                     36: #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
                     37:     char * p=s;
2.13      luotonen   38:     if (!s) return NULL;       /* Doesn't dump core if NULL */
                     39:     for(p=s;*p;p++);           /* Find end of string */
1.1       timbl      40:     for(p--;p>=s;p--) {
                     41:        if(SPACE(*p)) *p=0;     /* Zap trailing blanks */
                     42:        else break;
                     43:     }
                     44:     while(SPACE(*s))s++;       /* Strip leading blanks */
                     45:     return s;
                     46: }
                     47: 
                     48: 
                     49: /*     Scan a filename for its consituents
                     50: **     -----------------------------------
                     51: **
                     52: ** On entry,
                     53: **     name    points to a document name which may be incomplete.
                     54: ** On exit,
                     55: **      absolute or relative may be nonzero (but not both).
                     56: **     host, anchor and access may be nonzero if they were specified.
                     57: **     Any which are nonzero point to zero terminated strings.
                     58: */
                     59: #ifdef __STDC__
                     60: PRIVATE void scan(char * name, struct struct_parts *parts)
                     61: #else
                     62: PRIVATE void scan(name, parts)
                     63:     char * name;
                     64:     struct struct_parts *parts;
                     65: #endif
                     66: {
                     67:     char * after_access;
                     68:     char * p;
                     69:     int length = strlen(name);
                     70:     
                     71:     parts->access = 0;
                     72:     parts->host = 0;
                     73:     parts->absolute = 0;
                     74:     parts->relative = 0;
                     75:     parts->anchor = 0;
                     76:     
                     77:     after_access = name;
                     78:     for(p=name; *p; p++) {
                     79:        if (*p==':') {
                     80:                *p = 0;
2.20      timbl      81:                parts->access = after_access; /* Scheme has been specified */
1.1       timbl      82:                after_access = p+1;
2.22      luotonen   83:                if (0==strcasecomp("URL", parts->access)) {
2.20      timbl      84:                    parts->access = NULL;  /* Ignore IETF's URL: pre-prefix */
                     85:                } else break;
1.1       timbl      86:        }
2.20      timbl      87:        if (*p=='/') break;             /* Access has not been specified */
1.1       timbl      88:        if (*p=='#') break;
                     89:     }
                     90:     
                     91:     for(p=name+length-1; p>=name; p--) {
                     92:        if (*p =='#') {
                     93:            parts->anchor=p+1;
                     94:            *p=0;                               /* terminate the rest */
                     95:        }
                     96:     }
                     97:     p = after_access;
                     98:     if (*p=='/'){
                     99:        if (p[1]=='/') {
                    100:            parts->host = p+2;          /* host has been specified      */
                    101:            *p=0;                       /* Terminate access             */
                    102:            p=strchr(parts->host,'/');  /* look for end of host name if any */
                    103:            if(p) {
                    104:                *p=0;                   /* Terminate host */
                    105:                parts->absolute = p+1;          /* Root has been found */
                    106:            }
                    107:        } else {
                    108:            parts->absolute = p+1;              /* Root found but no host */
                    109:        }           
                    110:     } else {
                    111:         parts->relative = (*after_access) ? after_access : 0;  /* zero for "" */
                    112:     }
                    113: 
2.16      timbl     114: #ifdef OLD_CODE
1.1       timbl     115:     /* Access specified but no host: the anchor was not really one
2.16      timbl     116:        e.g. news:j462#36487@foo.bar -- JFG 10/jul/92, from bug report */
                    117:     /* This kludge doesn't work for example when coming across
                    118:        file:/usr/local/www/fred#123
                    119:        which loses its anchor. Correct approach in news is to
                    120:        escape weird characters not allowed in URL.  TBL 21/dec/93
                    121:     */
1.1       timbl     122:     if (parts->access && ! parts->host && parts->anchor) {
                    123:       *(parts->anchor - 1) = '#';  /* Restore the '#' in the address */
                    124:       parts->anchor = 0;
                    125:     }
2.16      timbl     126: #endif
1.1       timbl     127: 
                    128: #ifdef NOT_DEFINED     /* search is just treated as part of path */
                    129:     {
                    130:         char *p = relative ? relative : absolute;
                    131:        if (p) {
                    132:            char * q = strchr(p, '?');  /* Any search string? */
                    133:            if (q) {
                    134:                *q = 0;                 /* If so, chop that off. */
                    135:                parts->search = q+1;
                    136:            }
                    137:        }
                    138:     }
                    139: #endif
                    140: } /*scan */    
                    141: 
                    142: 
                    143: /*     Parse a Name relative to another name
                    144: **     -------------------------------------
                    145: **
                    146: **     This returns those parts of a name which are given (and requested)
                    147: **     substituting bits from the related name where necessary.
                    148: **
                    149: ** On entry,
                    150: **     aName           A filename given
                    151: **      relatedName     A name relative to which aName is to be parsed
                    152: **      wanted          A mask for the bits which are wanted.
                    153: **
                    154: ** On exit,
                    155: **     returns         A pointer to a malloc'd string which MUST BE FREED
                    156: */
                    157: #ifdef __STDC__
                    158: char * HTParse(const char * aName, const char * relatedName, int wanted)
                    159: #else
                    160: char * HTParse(aName, relatedName, wanted)
                    161:     char * aName;
                    162:     char * relatedName;
                    163:     int wanted;
                    164: #endif
                    165: 
                    166: {
                    167:     char * result = 0;
                    168:     char * return_value = 0;
                    169:     int len;
                    170:     char * name = 0;
                    171:     char * rel = 0;
                    172:     char * p;
2.12      timbl     173:     char * access;
1.1       timbl     174:     struct struct_parts given, related;
                    175:     
                    176:     /* Make working copies of input strings to cut up:
                    177:     */
                    178:     len = strlen(aName)+strlen(relatedName)+10;
                    179:     result=(char *)malloc(len);                /* Lots of space: more than enough */
                    180:     if (result == NULL) outofmem(__FILE__, "HTParse");
                    181:     
                    182:     StrAllocCopy(name, aName);
                    183:     StrAllocCopy(rel, relatedName);
                    184:     
                    185:     scan(name, &given);
                    186:     scan(rel,  &related); 
                    187:     result[0]=0;               /* Clear string  */
2.12      timbl     188:     access = given.access ? given.access : related.access;
1.1       timbl     189:     if (wanted & PARSE_ACCESS)
2.12      timbl     190:         if (access) {
                    191:            strcat(result, access);
1.1       timbl     192:            if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
                    193:        }
                    194:        
                    195:     if (given.access && related.access)        /* If different, inherit nothing. */
                    196:         if (strcmp(given.access, related.access)!=0) {
                    197:            related.host=0;
                    198:            related.absolute=0;
                    199:            related.relative=0;
                    200:            related.anchor=0;
                    201:        }
                    202:        
                    203:     if (wanted & PARSE_HOST)
                    204:         if(given.host || related.host) {
2.12      timbl     205:            char * tail = result + strlen(result);
1.1       timbl     206:            if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
                    207:            strcat(result, given.host ? given.host : related.host);
2.12      timbl     208: #define CLEAN_URLS
                    209: #ifdef CLEAN_URLS
                    210:            /* Ignore default port numbers, and trailing dots on FQDNs
                    211:               which will only cause identical adreesses to look different */
                    212:            {
                    213:                char * p;
                    214:                p = strchr(tail, ':');
                    215:                if (p && access) {              /* Port specified */
                    216:                    if (  (   strcmp(access, "http") == 0
                    217:                           && strcmp(p, ":80") == 0 )
                    218:                        ||
                    219:                          (   strcmp(access, "gopher") == 0
                    220:                           && strcmp(p, ":70") == 0 )
                    221:                        )
                    222:                    *p = (char)0;       /* It is the default: ignore it */
                    223:                }
                    224:                if (!p) p = tail + strlen(tail); /* After hostname */
2.21      frystyk   225:                if (*p) {                                 /* Henrik 17/04-94 */
                    226:                    p--;                                /* End of hostname */
                    227:                    if (*p == '.') *p = (char)0; /* chop final . */
                    228:                }
2.12      timbl     229:            }
                    230: #endif
1.1       timbl     231:        }
                    232:        
                    233:     if (given.host && related.host)  /* If different hosts, inherit no path. */
                    234:         if (strcmp(given.host, related.host)!=0) {
                    235:            related.absolute=0;
                    236:            related.relative=0;
                    237:            related.anchor=0;
                    238:        }
                    239:        
                    240:     if (wanted & PARSE_PATH) {
                    241:         if(given.absolute) {                           /* All is given */
                    242:            if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
                    243:            strcat(result, given.absolute);
                    244:        } else if(related.absolute) {   /* Adopt path not name */
                    245:            strcat(result, "/");
                    246:            strcat(result, related.absolute);
                    247:            if (given.relative) {
                    248:                p = strchr(result, '?');        /* Search part? */
                    249:                if (!p) p=result+strlen(result)-1;
                    250:                for (; *p!='/'; p--);   /* last / */
                    251:                p[1]=0;                                 /* Remove filename */
                    252:                strcat(result, given.relative);         /* Add given one */
                    253:                HTSimplify (result);
                    254:            }
                    255:        } else if(given.relative) {
                    256:            strcat(result, given.relative);             /* what we've got */
                    257:        } else if(related.relative) {
                    258:            strcat(result, related.relative);
                    259:        } else {  /* No inheritance */
                    260:            strcat(result, "/");
                    261:        }
                    262:     }
                    263:                
                    264:     if (wanted & PARSE_ANCHOR)
                    265:         if(given.anchor || related.anchor) {
                    266:            if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
                    267:            strcat(result, given.anchor ? given.anchor : related.anchor);
                    268:        }
                    269:     free(rel);
                    270:     free(name);
                    271:     
                    272:     StrAllocCopy(return_value, result);
                    273:     free(result);
                    274:     return return_value;               /* exactly the right length */
                    275: }
                    276: 
2.11      timbl     277: 
2.21      frystyk   278: #if 0  /* NOT USED FOR THE MOMENT */
2.15      luotonen  279: /*
                    280: **     As strcpy() but guaranteed to work correctly
                    281: **     with overlapping parameters.    AL 7 Feb 1994
                    282: */
                    283: PRIVATE void ari_strcpy ARGS2(char *, to,
                    284:                              char *, from)
                    285: {
                    286:     char * tmp;
                    287: 
                    288:     if (!to || !from) return;
                    289: 
                    290:     tmp = (char*)malloc(strlen(from)+1);
                    291:     if (!tmp) outofmem(__FILE__, "my_strcpy");
                    292: 
                    293:     strcpy(tmp, from);
                    294:     strcpy(to, tmp);
                    295:     free(tmp);
                    296: }
2.21      frystyk   297: #endif
                    298: 
2.20      timbl     299: 
                    300: /*             Simplify a URI
                    301: //             --------------
                    302: // A URI is allowed to contain the seqeunce xxx/../ which may be
1.1       timbl     303: // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
2.20      timbl     304: // Simplification helps us recognize duplicate URIs. 
1.1       timbl     305: //
                    306: //     Thus,   /etc/junk/../fred       becomes /etc/fred
                    307: //             /etc/junk/./fred        becomes /etc/junk/fred
2.11      timbl     308: //
                    309: //      but we should NOT change
                    310: //             http://fred.xxx.edu/../..
                    311: //
                    312: //     or      ../../albert.html
2.26    ! frystyk   313: //
        !           314: // In the same manner, the following prefixed are preserved:
        !           315: //
        !           316: //     ./<etc>
        !           317: //     //<etc>
        !           318: //
        !           319: // In order to avoid empty URLs the following URLs become:
        !           320: //
        !           321: //             /fred/..                becomes /fred/..
        !           322: //             /fred/././..            becomes /fred/..
        !           323: //
1.1       timbl     324: */
2.14      luotonen  325: PUBLIC void HTSimplify ARGS1(char *, filename)
1.1       timbl     326: {
2.19      frystyk   327:     int tokcnt = 0;
                    328:     char *strptr;
                    329:     char *urlptr;
                    330:     if (!filename || !*filename)                         /* Just to be sure! */
                    331:        return;
                    332: 
                    333:     /* Skip prefix, starting ./ and starting ///<etc> */
                    334:     urlptr = strstr(filename, "://");                         /* Find prefix */
                    335:     urlptr = urlptr ? urlptr+3 : filename;
                    336:     if (*urlptr == '.' && *(urlptr+1) == '/')            /* Starting ./<etc> */
                    337:        urlptr += 2;
                    338:     else if (*urlptr == '/') {                  /* Some URLs start //<file> */
                    339:        while (*++urlptr == '/');
                    340:     }
                    341:     if (!*urlptr)
                    342:        return;
                    343: 
                    344:     /* Now we have the string we want to work with */
                    345:     strptr = urlptr;
                    346:     while (*strptr++) {                        /* Count number of delimiters */
                    347:        if (*strptr == '/')
                    348:            tokcnt++;
                    349:     }
                    350:     {
                    351:        BOOL slashtail = NO;
                    352:        char *empty = "";
                    353:        char *url = NULL;
                    354:        char **tokptr;
                    355:        char **tokstart;
2.26    ! frystyk   356:        char *first;        /* Points to the first `real' segment in the URL */
2.19      frystyk   357:        StrAllocCopy(url, urlptr);
2.26    ! frystyk   358:        first = url;
        !           359:        while (*first && (*first == '/' ||  *first == '.'))
        !           360:            first++;
2.19      frystyk   361: 
                    362:        /* Does the URL end with a slash? */
                    363:        if(*(filename+strlen(filename)-1) == '/')
                    364:            slashtail = YES;
                    365:        
                    366:        /* I allocate cnt+2 as I don't know if the url is terminated by '/' */
                    367:        if ((tokstart = (char **) calloc(tokcnt+2, sizeof(char *))) == NULL)
                    368:            outofmem(__FILE__, "HTSimplify");
                    369: 
                    370:        /* Read the tokens forwards */
                    371:        tokptr = tokstart;
                    372:        *tokptr++ = strtok(url, "/");
                    373:        while ((strptr = strtok(NULL, "/")) != NULL)
                    374:            *tokptr++ = strptr;
                    375:        
                    376:        /* Scan backwards for '.' and '..' */
                    377:        tokptr--;
                    378:        while(tokptr >= tokstart) {
                    379:            if (!strcmp(*tokptr, ".")) {
                    380:                *tokptr = empty;
                    381:            } else if (!strcmp(*tokptr, "..")) {
                    382:                char **pptr = tokptr-1;
                    383:                while (pptr >= tokstart) {
2.26    ! frystyk   384:                    if (**pptr && strcmp(*pptr, "..") && strcmp(*pptr, ".") &&
        !           385:                        strcmp(*pptr, first)) {
2.19      frystyk   386:                        *pptr = empty;
                    387:                        *tokptr = empty;
                    388:                        break;
                    389:                    }
                    390:                    pptr--;
                    391:                }
                    392:            }
                    393:            tokptr--;
                    394:        }
                    395: 
                    396:        /* Write the rest out forwards */
                    397:        *urlptr = '\0';
                    398:        strcat(urlptr, *++tokptr);
                    399:        while (*++tokptr) {
                    400:            if (**tokptr) {
                    401:                strcat(urlptr, "/");
                    402:                strcat(urlptr, *tokptr);
                    403:            }
                    404:        }
                    405:        if (slashtail == YES)
                    406:            strcat(urlptr, "/");
                    407:        free(url);
                    408:        free(tokstart);
                    409:     }
                    410:     if (TRACE)
2.21      frystyk   411:        fprintf(stderr, "HTSimplify.. `%s\'\n", filename);
2.19      frystyk   412: }
                    413: #ifdef OLD_CODE
2.17      frystyk   414:     char * p = filename;
1.1       timbl     415:     char * q;
2.17      frystyk   416:     
                    417:     if (p) {
                    418:        while (*p && (*p == '/' || *p == '.'))     /* Pass starting / or .'s */
                    419:            p++;
                    420:        while(*p) {
                    421:            if (*p=='/') {
1.1       timbl     422:            if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
2.11      timbl     423:                for (q=p-1; (q>=filename) && (*q!='/'); q--); /* prev slash */
                    424:                if (q[0]=='/' && 0!=strncmp(q, "/../", 4)
                    425:                        &&!(q-1>filename && q[-1]=='/')) {
2.15      luotonen  426:                    ari_strcpy(q, p+3);         /* Remove  /xxx/..      */
1.1       timbl     427:                    if (!*filename) strcpy(filename, "/");
                    428:                    p = q-1;            /* Start again with prev slash  */
2.11      timbl     429:                } else {                        /*   xxx/.. leave it!   */
2.9       timbl     430: #ifdef BUG_CODE
2.15      luotonen  431:                    ari_strcpy(filename, p[3] ? p+4 : p+3); /* rm  xxx/../ */
1.1       timbl     432:                    p = filename;               /* Start again */
2.9       timbl     433: #endif
1.1       timbl     434:                }
                    435:            } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
2.15      luotonen  436:                ari_strcpy(p, p+2);             /* Remove a slash and a dot */
2.13      luotonen  437:            } else if (p[-1] != ':') {
                    438:                while (p[1] == '/') {
2.15      luotonen  439:                    ari_strcpy(p, p+1); /* Remove multiple slashes */
2.13      luotonen  440:                }
1.1       timbl     441:            }
2.17      frystyk   442:            }
                    443:            p++;
                    444:        }  /* end while (*p) */
                    445:     } /* end if (p) */
1.1       timbl     446: }
2.19      frystyk   447: #endif /* OLD_CODE */
1.1       timbl     448: 
                    449: 
                    450: /*             Make Relative Name
                    451: **             ------------------
                    452: **
                    453: ** This function creates and returns a string which gives an expression of
                    454: ** one address as related to another. Where there is no relation, an absolute
                    455: ** address is retured.
                    456: **
                    457: **  On entry,
                    458: **     Both names must be absolute, fully qualified names of nodes
                    459: **     (no anchor bits)
                    460: **
                    461: **  On exit,
                    462: **     The return result points to a newly allocated name which, if
                    463: **     parsed by HTParse relative to relatedName, will yield aName.
                    464: **     The caller is responsible for freeing the resulting name later.
                    465: **
                    466: */
                    467: #ifdef __STDC__
                    468: char * HTRelative(const char * aName, const char *relatedName)
                    469: #else
                    470: char * HTRelative(aName, relatedName)
                    471:    char * aName;
                    472:    char * relatedName;
                    473: #endif
                    474: {
                    475:     char * result = 0;
                    476:     CONST char *p = aName;
                    477:     CONST char *q = relatedName;
                    478:     CONST char * after_access = 0;
                    479:     CONST char * path = 0;
                    480:     CONST char * last_slash = 0;
                    481:     int slashes = 0;
                    482:     
                    483:     for(;*p; p++, q++) {       /* Find extent of match */
                    484:        if (*p!=*q) break;
                    485:        if (*p==':') after_access = p+1;
                    486:        if (*p=='/') {
                    487:            last_slash = p;
                    488:            slashes++;
                    489:            if (slashes==3) path=p;
                    490:        }
                    491:     }
                    492:     
                    493:     /* q, p point to the first non-matching character or zero */
                    494:     
                    495:     if (!after_access) {                       /* Different access */
                    496:         StrAllocCopy(result, aName);
                    497:     } else if (slashes<3){                     /* Different nodes */
                    498:        StrAllocCopy(result, after_access);
2.21      frystyk   499: #if 0
1.1       timbl     500:     } else if (slashes==3){                    /* Same node, different path */
                    501:         StrAllocCopy(result, path);
2.21      frystyk   502: #endif
1.1       timbl     503:     } else {                                   /* Some path in common */
                    504:         int levels= 0;
                    505:         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
                    506:        result = (char *)malloc(3*levels + strlen(last_slash) + 1);
                    507:       if (result == NULL) outofmem(__FILE__, "HTRelative");
                    508:        result[0]=0;
                    509:        for(;levels; levels--)strcat(result, "../");
                    510:        strcat(result, last_slash+1);
                    511:     }
2.21      frystyk   512:     if (TRACE) fprintf(stderr,
                    513:                      "HTRelative.. `%s' expressed relative to `%s' is `%s'\n",
                    514:                       aName, relatedName, result);
1.1       timbl     515:     return result;
                    516: }
2.1       timbl     517: 
                    518: 
2.6       timbl     519: /*             Escape undesirable characters using %           HTEscape()
                    520: **             -------------------------------------
                    521: **
                    522: **     This function takes a pointer to a string in which
                    523: **     some characters may be unacceptable unescaped.
                    524: **     It returns a string which has these characters
                    525: **     represented by a '%' character followed by two hex digits.
                    526: **
2.20      timbl     527: **     In the tradition of being conservative in what you do and liberal
                    528: **     in what you accept, we encode some characters which in fact are
                    529: **     allowed in URLs unencoded -- so DON'T use the table below for
                    530: **     parsing! 
                    531: **
2.6       timbl     532: **     Unlike HTUnEscape(), this routine returns a malloced string.
2.20      timbl     533: **
2.6       timbl     534: */
                    535: 
2.20      timbl     536: /* Not BOTH static AND const at the same time in gcc :-(, Henrik 18/03-94 
                    537: **  code gen error in gcc when making random access to
                    538: **  static const table(!!)  */
2.19      frystyk   539: /* PRIVATE CONST unsigned char isAcceptable[96] = */
                    540: PRIVATE unsigned char isAcceptable[96] =
2.6       timbl     541: 
2.20      timbl     542: /* Overencodes */
2.6       timbl     543: /*     Bit 0           xalpha          -- see HTFile.h
                    544: **     Bit 1           xpalpha         -- as xalpha but with plus.
2.20      timbl     545: **     Bit 2 ...       path            -- as xpalpha but with /
2.6       timbl     546: */
                    547:     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
                    548:     {    0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,      /* 2x   !"#$%&'()*+,-./  */
                    549:          7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,      /* 3x  0123456789:;<=>?  */
                    550:         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 4x  @ABCDEFGHIJKLMNO  */
                    551:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,       /* 5X  PQRSTUVWXYZ[\]^_  */
                    552:         0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,       /* 6x  `abcdefghijklmno  */
                    553:         7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };     /* 7X  pqrstuvwxyz{\}~  DEL */
                    554: 
                    555: PRIVATE char *hex = "0123456789ABCDEF";
                    556: 
2.8       timbl     557: PUBLIC char * HTEscape ARGS2 (CONST char *, str,
2.6       timbl     558:        unsigned char, mask)
                    559: {
                    560: #define ACCEPTABLE(a)  ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
                    561:     CONST char * p;
                    562:     char * q;
                    563:     char * result;
                    564:     int unacceptable = 0;
                    565:     for(p=str; *p; p++)
                    566:         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
                    567:                unacceptable++;
                    568:     result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
                    569:     if (result == NULL) outofmem(__FILE__, "HTEscape");
                    570:     for(q=result, p=str; *p; p++) {
                    571:        unsigned char a = TOASCII(*p);
                    572:        if (!ACCEPTABLE(a)) {
                    573:            *q++ = HEX_ESCAPE;  /* Means hex commming */
                    574:            *q++ = hex[a >> 4];
                    575:            *q++ = hex[a & 15];
                    576:        }
                    577:        else *q++ = *p;
                    578:     }
                    579:     *q++ = 0;                  /* Terminate */
                    580:     return result;
                    581: }
                    582: 
                    583: 
2.1       timbl     584: /*             Decode %xx escaped characters                   HTUnEscape()
                    585: **             -----------------------------
                    586: **
                    587: **     This function takes a pointer to a string in which some
                    588: **     characters may have been encoded in %xy form, where xy is
                    589: **     the acsii hex code for character 16x+y.
                    590: **     The string is converted in place, as it will never grow.
                    591: */
                    592: 
                    593: PRIVATE char from_hex ARGS1(char, c)
                    594: {
2.6       timbl     595:     return  c >= '0' && c <= '9' ?  c - '0' 
                    596:            : c >= 'A' && c <= 'F'? c - 'A' + 10
                    597:            : c - 'a' + 10;     /* accept small letters just in case */
2.1       timbl     598: }
                    599: 
                    600: PUBLIC char * HTUnEscape ARGS1( char *, str)
                    601: {
                    602:     char * p = str;
                    603:     char * q = str;
2.25      frystyk   604: 
                    605:     if (!str) {                                              /* Just for safety ;-) */
                    606:        if (TRACE)
                    607:            fprintf(stderr, "HTUnEscape.. Called with NULL argument.\n");
                    608:        return "";
                    609:     }
2.1       timbl     610:     while(*p) {
2.6       timbl     611:         if (*p == HEX_ESCAPE) {
2.1       timbl     612:            p++;
                    613:            if (*p) *q = from_hex(*p++) * 16;
                    614:            if (*p) *q = FROMASCII(*q + from_hex(*p++));
                    615:            q++;
                    616:        } else {
                    617:            *q++ = *p++; 
                    618:        }
                    619:     }
                    620:     
                    621:     *q++ = 0;
                    622:     return str;
                    623:     
                    624: } /* HTUnEscape */
                    625: 
                    626: 
2.24      luotonen  627: /*                                                     HTCleanTelnetString()
                    628:  *     Make sure that the given string doesn't contain characters that
                    629:  *     could cause security holes, such as newlines in ftp, gopher,
                    630:  *     news or telnet URLs; more specifically: allows everything between
2.26    ! frystyk   631:  *     ASCII 20-7E, and also A0-FE, inclusive. Also TAB ('\t') allowed!
2.24      luotonen  632:  *
                    633:  * On entry,
                    634:  *     str     the string that is *modified* if necessary.  The
                    635:  *             string will be truncated at the first illegal
                    636:  *             character that is encountered.
                    637:  * On exit,
                    638:  *     returns YES, if the string was modified.
                    639:  *             NO, otherwise.
                    640:  */
                    641: PUBLIC BOOL HTCleanTelnetString ARGS1(char *, str)
                    642: {
                    643:     char * cur = str;
                    644: 
                    645:     if (!str) return NO;
                    646: 
                    647:     while (*cur) {
                    648:        int a = TOASCII(*cur);
2.26    ! frystyk   649:        if (a != 0x9 && (a < 0x20 || (a > 0x7E && a < 0xA0) ||  a > 0xFE)) {
2.24      luotonen  650:            CTRACE(stderr, "Illegal..... character in URL: \"%s\"\n",str);
                    651:            *cur = 0;
                    652:            CTRACE(stderr, "Truncated... \"%s\"\n",str);
                    653:            return YES;
                    654:        }
                    655:        cur++;
                    656:     }
                    657:     return NO;
                    658: }
                    659: 

Webmaster