File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / src / tools.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 00:40:41 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: www1_4, WWW1_3, HEAD
version 1.3

    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.3 2012/08/01 00:40:41 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((int) *ct))
   64: 		ct++;
   65: 
   66: 	if (!(sc = strchr(ct, ';')))
   67: 		sc = strchr(ct, '\x0');
   68: 	while (isspace((int) *(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((int) *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 io_free() after use!
  107:  */
  108: ait_val_t *
  109: www_getpair(char ** __restrict str, const char *delim)
  110: {
  111: 	char *tr;
  112: 	int cx;
  113: 	ait_val_t *s;
  114: 
  115: 	assert(str && *str && delim);
  116: 
  117: 	s = io_allocVar();
  118: 	if (!s) {
  119: 		www_SetErr(io_GetErrno(), "%s", io_GetError());
  120: 		return NULL;
  121: 	}
  122: 
  123: 	cx = strcspn(*str, delim);
  124: 	tr = *str + cx;
  125: 	if (*tr)
  126: 		*tr++ = 0;
  127: 
  128: 	AIT_SET_STR(s, *str);
  129: 
  130: 	*str = tr;
  131: 	return s;
  132: }
  133: 
  134: /*
  135:  * www_x2c() - Hex from string to digit
  136:  *
  137:  * @str = string
  138:  * return: digit
  139:  */
  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: 
  154: /*
  155:  * www_unescape() - Unescape/decode WWW query string to host string
  156:  *
  157:  * @str = string
  158:  * return: none
  159:  */
  160: inline void
  161: www_unescape(char * __restrict str)
  162: {
  163: 	register int i, j;
  164: 
  165: 	if (!str)
  166: 		return;
  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: }

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