Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Handling of the q value |
3 | | * |
4 | | * Copyright (C) 2004 FhG FOKUS |
5 | | * |
6 | | * This file is part of opensips, a free SIP server. |
7 | | * |
8 | | * opensips is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 2 of the License, or |
11 | | * (at your option) any later version |
12 | | * |
13 | | * opensips is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | /*! |
24 | | * \file |
25 | | * \brief Functions related to the SIP q value |
26 | | */ |
27 | | |
28 | | |
29 | | #include "error.h" |
30 | | #include "qvalue.h" |
31 | | |
32 | | |
33 | | /* |
34 | | * Convert string representation of q parameter in qvalue_t |
35 | | */ |
36 | | int str2q(qvalue_t* q, char* s, int len) |
37 | 0 | { |
38 | 0 | int i, digits, order; |
39 | | |
40 | | /* States and equivalent regular expressions of input */ |
41 | 0 | enum { |
42 | 0 | ST_START, /* (SPC|TAB)* */ |
43 | 0 | ST_0, /* 0+ */ |
44 | 0 | ST_1, /* 1 */ |
45 | 0 | ST_0_PT, /* 0*\. */ |
46 | 0 | ST_1_PT, /* 1\. */ |
47 | 0 | ST_1_PT_0, /* 1\.0+ */ |
48 | 0 | ST_0_PT_N /* 0*\.[0-9]+ */ |
49 | 0 | } state = ST_START; |
50 | |
|
51 | 0 | if (!q || !s) { |
52 | 0 | return E_INVALID_PARAMS; |
53 | 0 | } |
54 | | |
55 | 0 | digits = 1; |
56 | 0 | order = 100; |
57 | 0 | for(i = 0; i < len; i++) { |
58 | 0 | switch(state) { |
59 | 0 | case ST_START: |
60 | 0 | switch(s[i]) { |
61 | 0 | case ' ': |
62 | 0 | case '\t': |
63 | 0 | break; |
64 | | |
65 | 0 | case '0': |
66 | 0 | *q = 0; |
67 | 0 | state = ST_0; |
68 | 0 | break; |
69 | | |
70 | 0 | case '1': |
71 | 0 | *q = 1000; |
72 | 0 | state = ST_1; |
73 | 0 | break; |
74 | | |
75 | 0 | case '.': |
76 | 0 | *q = 0; |
77 | 0 | state = ST_0_PT; |
78 | 0 | break; |
79 | | |
80 | 0 | default: |
81 | 0 | return E_Q_INV_CHAR; |
82 | 0 | } |
83 | 0 | break; |
84 | | |
85 | 0 | case ST_0: |
86 | 0 | switch(s[i]) { |
87 | 0 | case '0': |
88 | 0 | break; |
89 | | |
90 | 0 | case '.': |
91 | 0 | state = ST_0_PT; |
92 | 0 | break; |
93 | | |
94 | 0 | case '1': |
95 | 0 | *q = 1000; |
96 | 0 | state = ST_1; |
97 | 0 | break; |
98 | | |
99 | 0 | default: |
100 | 0 | if (s[i] >= '2' && s[i] <= '9') { |
101 | 0 | return E_Q_TOO_BIG; |
102 | 0 | } else { |
103 | 0 | return E_Q_INV_CHAR; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | break; |
107 | | |
108 | 0 | case ST_1: |
109 | 0 | if (s[i] == '.') { |
110 | 0 | state = ST_1_PT; |
111 | 0 | break; |
112 | 0 | } else { |
113 | 0 | if (s[i] >= '0' && s[i] <= '9') { |
114 | 0 | return E_Q_TOO_BIG; |
115 | 0 | } else { |
116 | 0 | return E_Q_INV_CHAR; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | break; |
120 | | |
121 | 0 | case ST_0_PT: |
122 | 0 | if (s[i] >= '0' && s[i] <= '9') { |
123 | 0 | *q = (s[i] - '0') * order; |
124 | 0 | order /= 10; |
125 | 0 | state = ST_0_PT_N; |
126 | 0 | } else { |
127 | 0 | return E_Q_INV_CHAR; |
128 | 0 | } |
129 | 0 | break; |
130 | | |
131 | 0 | case ST_1_PT: |
132 | 0 | if (s[i] == '0') { |
133 | 0 | state = ST_1_PT_0; |
134 | 0 | } else { |
135 | 0 | if (s[i] >= '1' && s[i] <= '9') { |
136 | 0 | return E_Q_TOO_BIG; |
137 | 0 | } else { |
138 | 0 | return E_Q_INV_CHAR; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | break; |
142 | | |
143 | 0 | case ST_1_PT_0: |
144 | 0 | if (s[i] == '0') { |
145 | 0 | break; |
146 | 0 | } else { |
147 | 0 | if (s[i] >= '1' && s[i] <= '9') { |
148 | 0 | return E_Q_TOO_BIG; |
149 | 0 | } else { |
150 | 0 | return E_Q_INV_CHAR; |
151 | 0 | } |
152 | 0 | } |
153 | 0 | break; |
154 | | |
155 | 0 | case ST_0_PT_N: |
156 | 0 | if (s[i] >= '0' && s[i] <= '9') { |
157 | 0 | if (digits <= 3) { |
158 | 0 | *q += (s[i] - '0') * order; |
159 | 0 | order /= 10; |
160 | 0 | digits++; |
161 | 0 | } |
162 | 0 | } else { |
163 | 0 | return E_Q_INV_CHAR; |
164 | 0 | } |
165 | 0 | break; |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | 0 | if (state == ST_START) |
170 | 0 | return E_Q_EMPTY; |
171 | | |
172 | 0 | return 0; |
173 | 0 | } |