Annotation of embedaddon/strongswan/src/libstrongswan/utils/lexparser.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2001-2006 Andreas Steffen
                      3:  *
                      4:  * This program is free software; you can redistribute it and/or modify it
                      5:  * under the terms of the GNU General Public License as published by the
                      6:  * Free Software Foundation; either version 2 of the License, or (at your
                      7:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      8:  *
                      9:  * This program is distributed in the hope that it will be useful, but
                     10:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     11:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     12:  * for more details.
                     13:  */
                     14: 
                     15: #include "lexparser.h"
                     16: 
                     17: /**
                     18:  * eat whitespace
                     19:  */
                     20: bool eat_whitespace(chunk_t *src)
                     21: {
                     22:        while (src->len > 0 && (*src->ptr == ' ' || *src->ptr == '\t'))
                     23:        {
                     24:                src->ptr++;  src->len--;
                     25:        }
                     26:        return  src->len > 0 && *src->ptr != '#';
                     27: }
                     28: 
                     29: /**
                     30:  * compare string with chunk
                     31:  */
                     32: bool match(const char *pattern, const chunk_t *ch)
                     33: {
                     34:        return ch->len == strlen(pattern) && strncmp(pattern, ch->ptr, ch->len) == 0;
                     35: }
                     36: 
                     37: /**
                     38:  * extracts a token ending with the first occurrence of a given termination symbol
                     39:  */
                     40: bool extract_token(chunk_t *token, const char termination, chunk_t *src)
                     41: {
                     42:        u_char *eot = memchr(src->ptr, termination, src->len);
                     43: 
                     44:        if (termination == ' ')
                     45:        {
                     46:                u_char *eot_tab = memchr(src->ptr, '\t', src->len);
                     47: 
                     48:                /* check if a tab instead of a space terminates the token */
                     49:                eot = ( eot_tab == NULL || (eot && eot < eot_tab) ) ? eot : eot_tab;
                     50:        }
                     51: 
                     52:        /* initialize empty token */
                     53:        *token = chunk_empty;
                     54: 
                     55:        if (eot == NULL) /* termination symbol not found */
                     56:        {
                     57:                return FALSE;
                     58:        }
                     59: 
                     60:        /* extract token */
                     61:        token->ptr = src->ptr;
                     62:        token->len = (u_int)(eot - src->ptr);
                     63: 
                     64:        /* advance src pointer after termination symbol */
                     65:        src->ptr = eot + 1;
                     66:        src->len -= (token->len + 1);
                     67: 
                     68:        return TRUE;
                     69: }
                     70: 
                     71: /**
                     72:  * extracts a token ending with the first occurrence of a given null-terminated string
                     73:  */
                     74: bool extract_token_str(chunk_t *token, const char *termination, chunk_t *src)
                     75: {
                     76:        u_char *eot = memstr(src->ptr, termination, src->len);
                     77:        size_t l = strlen(termination);
                     78: 
                     79:        /* initialize empty token */
                     80:        *token = chunk_empty;
                     81: 
                     82:        if (eot == NULL) /* termination string not found */
                     83:        {
                     84:                return FALSE;
                     85:        }
                     86: 
                     87:        /* extract token */
                     88:        token->ptr = src->ptr;
                     89:        token->len = (u_int)(eot - src->ptr);
                     90: 
                     91:        /* advance src pointer after termination string */
                     92:        src->ptr = eot + l;
                     93:        src->len -= (token->len + l);
                     94: 
                     95:        return TRUE;
                     96: }
                     97: 
                     98: /**
                     99:  *  fetches a new line terminated by \n or \r\n
                    100:  */
                    101: bool fetchline(chunk_t *src, chunk_t *line)
                    102: {
                    103:        if (src->len == 0) /* end of src reached */
                    104:                return FALSE;
                    105: 
                    106:        if (extract_token(line, '\n', src))
                    107:        {
                    108:                if (line->len > 0 && *(line->ptr + line->len -1) == '\r')
                    109:                        line->len--;  /* remove optional \r */
                    110:        }
                    111:        else /*last line ends without newline */
                    112:        {
                    113:                *line = *src;
                    114:                src->ptr += src->len;
                    115:                src->len = 0;
                    116:        }
                    117:        return TRUE;
                    118: }
                    119: 
                    120: err_t extract_value(chunk_t *value, chunk_t *line)
                    121: {
                    122:        char delimiter = ' ';
                    123: 
                    124:        if (!eat_whitespace(line))
                    125:        {
                    126:                *value = chunk_empty;
                    127:                return NULL;
                    128:        }
                    129:        if (*line->ptr == '\'' || *line->ptr == '"')
                    130:        {
                    131:                delimiter = *line->ptr;
                    132:                line->ptr++;  line->len--;
                    133:        }
                    134:        if (!extract_token(value, delimiter, line))
                    135:        {
                    136:                if (delimiter == ' ')
                    137:                {
                    138:                        *value = *line;
                    139:                        line->len = 0;
                    140:                }
                    141:                else
                    142:                {
                    143:                        return "missing second delimiter";
                    144:                }
                    145:        }
                    146:        return NULL;
                    147: }
                    148: 
                    149: /**
                    150:  * extracts a parameter: value pair
                    151:  */
                    152: err_t extract_parameter_value(chunk_t *name, chunk_t *value, chunk_t *line)
                    153: {
                    154:        /* extract name */
                    155:        if (!extract_token(name,':', line))
                    156:        {
                    157:                return "missing ':'";
                    158:        }
                    159: 
                    160:        /* extract value */
                    161:        return extract_value(value, line);
                    162: }

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