File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / src / tools.c
Revision 1.2.6.1: download - view: text, annotated - select for diffs - revision graph
Tue Jul 31 11:56:16 2012 UTC (12 years ago) by misho
Branches: www1_3
Diff to: branchpoint 1.2: preferred, unified
remove header

    1: /*************************************************************************
    2: * (C) 2012 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: tools.c,v 1.2.6.1 2012/07/31 11:56:16 misho Exp $
    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: 
   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:  */
   56: int
   57: www_cmp(const char *ct, const char *s)
   58: {
   59: 	char *sc;
   60: 
   61: 	assert(ct && s);
   62: 
   63: 	while (isspace(*ct))
   64: 		ct++;
   65: 
   66: 	if (!(sc = strchr(ct, ';')))
   67: 		sc = strchr(ct, '\x0');
   68: 	while (isspace(*(sc - 1)))
   69: 		sc--;
   70: 
   71: 	if (strlen(s) != sc - ct)
   72: 		return -1;
   73: 	return strncasecmp(ct, s, sc - ct);
   74: }
   75: 
   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:  */
   83: int
   84: www_cmptype(const char *ct, const char *type)
   85: {
   86: 	char *sl;
   87: 
   88: 	assert(ct && type);
   89: 
   90: 	while (isspace(*ct))
   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: 
  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 free() after use!
  107:  */
  108: char *
  109: www_getpair(char ** __restrict str, const char *delim)
  110: {
  111: 	char *tr, *s = NULL;
  112: 	int cx;
  113: 
  114: 	assert(str && *str && delim);
  115: 
  116: 	cx = strcspn(*str, delim);
  117: 	tr = *str + cx;
  118: 
  119: 	s = malloc(cx + 1);
  120: 	if (!s) {
  121: 		LOGERR;
  122: 		return NULL;
  123: 	} else
  124: 		strlcpy(s, *str, cx + 1);
  125: 
  126: 	*str = tr;
  127: 	if (**str)
  128: 		(*str)++;
  129: 
  130: 	return s;
  131: }
  132: 
  133: /*
  134:  * www_x2c() - Hex from string to digit
  135:  *
  136:  * @str = string
  137:  * return: digit
  138:  */
  139: inline char
  140: www_x2c(const char *str)
  141: {
  142: 	register char digit;
  143: 
  144: 	assert(str);
  145: 
  146: 	digit = (str[0] >= 'A' ? ((str[0] & 0xdf) - 'A') + 10 : (str[0] - '0'));
  147: 	digit *= 16;
  148: 	digit += (str[1] >= 'A' ? ((str[1] & 0xdf) - 'A') + 10 : (str[1] - '0'));
  149: 
  150: 	return digit;
  151: }
  152: 
  153: /*
  154:  * www_unescape() - Unescape/decode WWW query string to host string
  155:  *
  156:  * @str = string
  157:  * return: none
  158:  */
  159: inline void
  160: www_unescape(char * __restrict str)
  161: {
  162: 	register int i, j;
  163: 
  164: 	assert(str);
  165: 
  166: 	for (i = j = 0; str[j]; i++, j++) {
  167: 		str[i] = str[j];
  168: 
  169: 		if (str[j] == '+')
  170: 			str[i] = ' ';
  171: 		else if (str[j] == '%') {
  172: 			str[i] = www_x2c(&str[j + 1]);
  173: 			j += 2;
  174: 		}
  175: 	}
  176: 
  177: 	str[i] = 0;
  178: }

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