parse_expires.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Expires header field body parser
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License 
00026  * along with this program; if not, write to the Free Software 
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  * History:
00030  * --------
00031  * 2003-04-26 ZSW (jiri)
00032  */
00033 
00043 #include "parse_expires.h"
00044 #include <stdio.h>          /* printf */
00045 #include "../mem/mem.h"     /* pkg_malloc, pkg_free */
00046 #include "../dprint.h"
00047 #include "../trim.h"        /* trim_leading */
00048 #include <string.h>         /* memset */
00049 #include "../ut.h"
00050 
00051 
00052 static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
00053 {
00054         int i;
00055         str tmp;
00056         
00057         tmp.s = _s;
00058         tmp.len = _l;
00059 
00060         trim(&tmp);
00061 
00062         if (tmp.len == 0) {
00063                 LOG(L_ERR, "expires_parser(): Empty body\n");
00064                 _e->valid = 0;
00065                 return -1;
00066         }
00067 
00068         _e->text.s = tmp.s;
00069         _e->text.len = tmp.len;
00070 
00071         /* more then 32bit/maxuint cant be valid */
00072         if (tmp.len > 10) {
00073                 _e->valid = 0;
00074                 return 0;
00075         }
00076 
00077         for(i = 0; i < tmp.len; i++) {
00078                 if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
00079                         _e->val *= 10;
00080                         _e->val += tmp.s[i] - '0';
00081                 } else {
00082                         switch(tmp.s[i]) {
00083                         case ' ':
00084                         case '\t':
00085                         case '\r':
00086                         case '\n':
00087                                 _e->text.len = i;
00088                                 _e->valid = 1;
00089                                 return 0;
00090 
00091                         default:
00092                                      /* Exit normally here, we want to be backwards compatible with
00093                                       * RFC2543 entities that can put absolute time here
00094                                       */
00095                                      /*
00096                                 LOG(L_ERR, "expires_parser(): Invalid character\n");
00097                                 return -2;
00098                                      */
00099                                 _e->valid = 0;
00100                                 return 0;
00101                         }
00102                 }
00103         }
00104 
00105         _e->valid = 1;
00106         return 0;
00107 }
00108 
00109 
00113 int parse_expires(struct hdr_field* _h)
00114 {
00115         exp_body_t* e;
00116 
00117         if (_h->parsed) {
00118                 return 0;  /* Already parsed */
00119         }
00120 
00121         e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
00122         if (e == 0) {
00123                 LOG(L_ERR, "parse_expires(): No memory left\n");
00124                 return -1;
00125         }
00126         
00127         memset(e, 0, sizeof(exp_body_t));
00128 
00129         if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
00130                 LOG(L_ERR, "parse_expires(): Error while parsing\n");
00131                 pkg_free(e);
00132                 return -2;
00133         }
00134         
00135         _h->parsed = (void*)e;
00136         return 0;
00137 }
00138 
00139 
00143 void free_expires(exp_body_t** _e)
00144 {
00145         pkg_free(*_e);
00146         *_e = 0;
00147 }
00148 
00149 
00153 void print_expires(exp_body_t* _e)
00154 {
00155         printf("===Expires===\n");
00156         printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00157         printf("val : %d\n", _e->val);
00158         printf("===/Expires===\n");
00159 }