Annotation of libaitwww/src/tools.c, revision 1.3.2.2

1.1       misho       1: /*************************************************************************
                      2: * (C) 2012 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.3.2.2 ! misho       6: * $Id: tools.c,v 1.3.2.1 2012/08/06 11:08:08 misho Exp $
1.1       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: 
                     48: 
1.3       misho      49: /*
                     50:  * www_cmp() - Compare two string
                     51:  *
                     52:  * @ct = content text from www
                     53:  * @s = string
                     54:  * return: 0 are equal or !0 are different
                     55:  */
1.1       misho      56: int
                     57: www_cmp(const char *ct, const char *s)
                     58: {
                     59:        char *sc;
                     60: 
                     61:        assert(ct && s);
                     62: 
1.3       misho      63:        while (isspace((int) *ct))
1.1       misho      64:                ct++;
                     65: 
                     66:        if (!(sc = strchr(ct, ';')))
                     67:                sc = strchr(ct, '\x0');
1.3       misho      68:        while (isspace((int) *(sc - 1)))
1.1       misho      69:                sc--;
                     70: 
                     71:        if (strlen(s) != sc - ct)
                     72:                return -1;
                     73:        return strncasecmp(ct, s, sc - ct);
                     74: }
                     75: 
1.3       misho      76: /*
                     77:  * www_cmptype() - Compare context type
                     78:  *
                     79:  * @ct = content text from www
                     80:  * @type = content type
                     81:  * return: 0 are equal or !0 are different
                     82:  */
1.1       misho      83: int
                     84: www_cmptype(const char *ct, const char *type)
                     85: {
                     86:        char *sl;
                     87: 
                     88:        assert(ct && type);
                     89: 
1.3       misho      90:        while (isspace((int) *ct))
1.1       misho      91:                ct++;
                     92: 
                     93:        if (!(sl = strchr(ct, '/')))
                     94:                return -1;
                     95: 
                     96:        if (strlen(type) != sl - ct)
                     97:                return 1;
                     98:        return strncasecmp(ct, type, sl - ct);
                     99: }
                    100: 
1.3       misho     101: /*
                    102:  * www_getpair() - Get AV pair from WWW query string
                    103:  *
                    104:  * @str = query string
                    105:  * @delim = delimiter
                    106:  * return: NULL error or AV pair, must be io_free() after use!
                    107:  */
                    108: ait_val_t *
1.1       misho     109: www_getpair(char ** __restrict str, const char *delim)
                    110: {
1.3       misho     111:        char *tr;
1.1       misho     112:        int cx;
1.3       misho     113:        ait_val_t *s;
1.1       misho     114: 
                    115:        assert(str && *str && delim);
                    116: 
1.3       misho     117:        s = io_allocVar();
                    118:        if (!s) {
                    119:                www_SetErr(io_GetErrno(), "%s", io_GetError());
                    120:                return NULL;
                    121:        }
                    122: 
1.1       misho     123:        cx = strcspn(*str, delim);
                    124:        tr = *str + cx;
1.3       misho     125:        if (*tr)
                    126:                *tr++ = 0;
1.1       misho     127: 
1.3       misho     128:        AIT_SET_STR(s, *str);
1.1       misho     129: 
                    130:        *str = tr;
                    131:        return s;
                    132: }
                    133: 
1.3       misho     134: /*
                    135:  * www_x2c() - Hex from string to digit
                    136:  *
                    137:  * @str = string
                    138:  * return: digit
                    139:  */
1.1       misho     140: inline char
                    141: www_x2c(const char *str)
                    142: {
                    143:        register char digit;
                    144: 
                    145:        assert(str);
                    146: 
                    147:        digit = (str[0] >= 'A' ? ((str[0] & 0xdf) - 'A') + 10 : (str[0] - '0'));
                    148:        digit *= 16;
                    149:        digit += (str[1] >= 'A' ? ((str[1] & 0xdf) - 'A') + 10 : (str[1] - '0'));
                    150: 
                    151:        return digit;
                    152: }
                    153: 
1.3       misho     154: /*
                    155:  * www_unescape() - Unescape/decode WWW query string to host string
                    156:  *
                    157:  * @str = string
                    158:  * return: none
                    159:  */
1.1       misho     160: inline void
                    161: www_unescape(char * __restrict str)
                    162: {
                    163:        register int i, j;
                    164: 
1.3       misho     165:        if (!str)
                    166:                return;
1.1       misho     167: 
                    168:        for (i = j = 0; str[j]; i++, j++) {
                    169:                str[i] = str[j];
                    170: 
                    171:                if (str[j] == '+')
                    172:                        str[i] = ' ';
                    173:                else if (str[j] == '%') {
                    174:                        str[i] = www_x2c(&str[j + 1]);
                    175:                        j += 2;
                    176:                }
                    177:        }
                    178: 
                    179:        str[i] = 0;
                    180: }
1.3.2.1   misho     181: 
                    182: /*
                    183:  * www_undot() - Undotted and clean WWW query filename
                    184:  *
                    185:  * @fname = query filename
1.3.2.2 ! misho     186:  * @fnlen = filename length
        !           187:  * return: -1 error, 0 not valid filename or >0 validated filename length
1.3.2.1   misho     188:  */
1.3.2.2 ! misho     189: int
        !           190: www_undot(const char * __restrict fname, int fnlen)
1.3.2.1   misho     191: {
                    192:        char *s, *s2;
                    193:        int l;
                    194: 
1.3.2.2 ! misho     195:        if (!fname || !fnlen)
        !           196:                return -1;
1.3.2.1   misho     197: 
                    198:        /* collapse / sequences */
                    199:        if ((s = strstr(fname, "//"))) {
                    200:                s2 = s + 1;
                    201:                for (s2 = ++s; *s2 == '/'; s2++);
                    202:                memmove(s, s2, strlen(s2) + 1);
                    203:        }
                    204: 
                    205:        /* escaped ./ and /./ sequences */
                    206:        while (!strncmp(fname, "./", 2))
                    207:                memmove((void*) fname, fname + 2, strlen(fname + 1));
                    208:        while ((s = strstr(fname, "/./")))
                    209:                memmove(s, s + 2, strlen(s + 1));
                    210: 
                    211:        /* alternate between removing leading ../ and removing xxx/../ */
                    212:        while (42) {
                    213:                while (!strncmp(fname, "../", 3))
                    214:                        memmove((void*) fname, fname + 3, strlen(fname + 2));
                    215:                if (!(s = strstr(fname, "/../")))
                    216:                        break;
                    217:                for (s2 = s - 1; s2 >= fname && *s2 != '/'; --s2);
                    218:                memmove(s2 + 1, s + 4, strlen(s + 3));
                    219:        }
                    220: 
                    221:        /* elide any /.. at the end */
                    222:        while ((l = strlen(fname)) > 3 && 
                    223:                        !strcmp((s = (char*) fname + l - 3), "/..")) {
                    224:                for (s2 = s - 1; s2 >= fname && *s2 != '/'; --s2);
                    225:                if (s2 < fname)
                    226:                        break;
                    227:                *s2 = 0;
                    228:        }
1.3.2.2 ! misho     229: 
        !           230:        /* if filename is empry add current dir */
        !           231:        if (!*fname)
        !           232:                strlcpy((char*) fname, "./", fnlen);
        !           233: 
        !           234:        /* check for valid filename */
        !           235:        if (*fname == '/' || (fname[0] == '.' && fname[1] == '.' && 
        !           236:                                (!fname[2] || fname[2] == '/')))
        !           237:                return 0;
        !           238: 
        !           239:        return strlen(fname);
1.3.2.1   misho     240: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>