/src/kamailio/src/core/qvalue.h
Line | Count | Source |
1 | | /* |
2 | | * Handling of the q value |
3 | | * |
4 | | * Copyright (C) 2004 FhG FOKUS |
5 | | * |
6 | | * This file is part of Kamailio, a free SIP server. |
7 | | * |
8 | | * SPDX-License-Identifier: GPL-2.0-or-later |
9 | | * |
10 | | * Kamailio is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU General Public License as published by |
12 | | * the Free Software Foundation; either version 2 of the License, or |
13 | | * (at your option) any later version |
14 | | * |
15 | | * Kamailio is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License |
21 | | * along with this program; if not, write to the Free Software |
22 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | | * |
24 | | */ |
25 | | /*! |
26 | | * \file |
27 | | * \brief Kamailio core :: Handling of the Q value |
28 | | * \author janakj |
29 | | * \ingroup core |
30 | | * Module: \ref core |
31 | | */ |
32 | | |
33 | | #ifndef _QVALUE_H |
34 | | #define _QVALUE_H 1 |
35 | | |
36 | | #include <string.h> |
37 | | |
38 | | /* |
39 | | * The q value expresses the priority of a URI within a set of URIs |
40 | | * (Contact header field in the same SIP message or dset array in |
41 | | * ser. The higher is the q value of a URI the higher is the priority |
42 | | * of the URI. |
43 | | * |
44 | | * The q value is usually expressed as a floating point number with |
45 | | * limited number of decimal digits, for example 0.346. RFC3261 allows |
46 | | * 0-3 decimal digits. |
47 | | * |
48 | | * To speed things up we represent the q value as integer number, it |
49 | | * is then easier to handle/print the value. To convert float into |
50 | | * integer we multiply the q value by 1000, i.e. |
51 | | * (float)0.567 == (int)567. In the opposite direction, values |
52 | | * higher or equal to 1000 are converted to 1.0 and values below or |
53 | | * equal to 0 are converted to 0. |
54 | | * |
55 | | * Value Q_UNSPECIFIED (which is in fact -1) has a special meaning, it |
56 | | * means that the q value is not known and the parameter should not be |
57 | | * printed when printing Contacts, implementations will then use |
58 | | * implementation specific pre-defined values. |
59 | | */ |
60 | | |
61 | | typedef int qvalue_t; |
62 | | |
63 | | /* |
64 | | * Use this if the value of q is not specified |
65 | | */ |
66 | 0 | #define Q_UNSPECIFIED ((qvalue_t)-1) |
67 | | |
68 | | |
69 | 0 | #define MAX_Q ((qvalue_t)1000) |
70 | 0 | #define MIN_Q ((qvalue_t)0) |
71 | | |
72 | 0 | #define MAX_Q_STR "1" |
73 | 0 | #define MAX_Q_STR_LEN (sizeof(MAX_Q_STR) - 1) |
74 | | |
75 | 0 | #define MIN_Q_STR "0" |
76 | 0 | #define MIN_Q_STR_LEN (sizeof(MIN_Q_STR) - 1) |
77 | | |
78 | 0 | #define Q_PREFIX "0." |
79 | 0 | #define Q_PREFIX_LEN (sizeof(Q_PREFIX) - 1) |
80 | | |
81 | | |
82 | | /* |
83 | | * Calculate the length of printed q |
84 | | */ |
85 | | static inline size_t len_q(qvalue_t q) |
86 | 0 | { |
87 | 0 | if(q == Q_UNSPECIFIED) { |
88 | 0 | return 0; |
89 | 0 | } else if(q >= MAX_Q) { |
90 | 0 | return MAX_Q_STR_LEN; |
91 | 0 | } else if(q <= MIN_Q) { |
92 | 0 | return MIN_Q_STR_LEN; |
93 | 0 | } else if(q % 100 == 0) { |
94 | 0 | return Q_PREFIX_LEN + 1; |
95 | 0 | } else if(q % 10 == 0) { |
96 | 0 | return Q_PREFIX_LEN + 2; |
97 | 0 | } else { |
98 | 0 | return Q_PREFIX_LEN + 3; |
99 | 0 | } |
100 | 0 | } Unexecuted instantiation: fmsg.c:len_q Unexecuted instantiation: main.c:len_q Unexecuted instantiation: receive.c:len_q Unexecuted instantiation: select_core.c:len_q Unexecuted instantiation: action.c:len_q Unexecuted instantiation: cfg.tab.c:len_q Unexecuted instantiation: dset.c:len_q Unexecuted instantiation: kemi.c:len_q |
101 | | |
102 | | |
103 | | /* |
104 | | * Convert qvalue_t to double |
105 | | */ |
106 | | static inline double q2double(qvalue_t q) |
107 | 0 | { |
108 | 0 | if(q == Q_UNSPECIFIED) { |
109 | 0 | return -1; |
110 | 0 | } else { |
111 | 0 | return (double)((double)q / (double)1000); |
112 | 0 | } |
113 | 0 | } Unexecuted instantiation: fmsg.c:q2double Unexecuted instantiation: main.c:q2double Unexecuted instantiation: receive.c:q2double Unexecuted instantiation: select_core.c:q2double Unexecuted instantiation: action.c:q2double Unexecuted instantiation: cfg.tab.c:q2double Unexecuted instantiation: dset.c:q2double Unexecuted instantiation: kemi.c:q2double |
114 | | |
115 | | |
116 | | /* |
117 | | * Convert double to qvalue_t |
118 | | */ |
119 | | static inline qvalue_t double2q(double q) |
120 | 0 | { |
121 | 0 | if(q == -1) { |
122 | 0 | return Q_UNSPECIFIED; |
123 | 0 | } else { |
124 | 0 | return q * 1000; |
125 | 0 | } |
126 | 0 | } Unexecuted instantiation: fmsg.c:double2q Unexecuted instantiation: main.c:double2q Unexecuted instantiation: receive.c:double2q Unexecuted instantiation: select_core.c:double2q Unexecuted instantiation: action.c:double2q Unexecuted instantiation: cfg.tab.c:double2q Unexecuted instantiation: dset.c:double2q Unexecuted instantiation: kemi.c:double2q |
127 | | |
128 | | |
129 | | /* |
130 | | * Convert q value to string |
131 | | */ |
132 | | static inline char *q2str(qvalue_t q, unsigned int *len) |
133 | 0 | { |
134 | 0 | static char buf[sizeof("0.123")]; |
135 | 0 | char *p; |
136 | |
|
137 | 0 | p = buf; |
138 | 0 | if(q == Q_UNSPECIFIED) { |
139 | | /* Do nothing */ |
140 | 0 | } else if(q >= MAX_Q) { |
141 | 0 | memcpy(p, MAX_Q_STR, MAX_Q_STR_LEN); |
142 | 0 | p += MAX_Q_STR_LEN; |
143 | 0 | } else if(q <= MIN_Q) { |
144 | 0 | memcpy(p, MIN_Q_STR, MIN_Q_STR_LEN); |
145 | 0 | p += MIN_Q_STR_LEN; |
146 | 0 | } else { |
147 | 0 | memcpy(p, Q_PREFIX, Q_PREFIX_LEN); |
148 | 0 | p += Q_PREFIX_LEN; |
149 | |
|
150 | 0 | *p++ = q / 100 + '0'; |
151 | 0 | q %= 100; |
152 | 0 | if(!q) |
153 | 0 | goto end; |
154 | | |
155 | 0 | *p++ = q / 10 + '0'; |
156 | 0 | q %= 10; |
157 | 0 | if(!q) |
158 | 0 | goto end; |
159 | | |
160 | 0 | *p++ = q + '0'; |
161 | 0 | } |
162 | 0 | end: |
163 | 0 | *p = '\0'; |
164 | 0 | if(len) { |
165 | 0 | *len = p - buf; |
166 | 0 | } |
167 | 0 | return buf; |
168 | 0 | } Unexecuted instantiation: fmsg.c:q2str Unexecuted instantiation: main.c:q2str Unexecuted instantiation: receive.c:q2str Unexecuted instantiation: select_core.c:q2str Unexecuted instantiation: action.c:q2str Unexecuted instantiation: cfg.tab.c:q2str Unexecuted instantiation: dset.c:q2str Unexecuted instantiation: kemi.c:q2str |
169 | | |
170 | | |
171 | | /* |
172 | | * Convert string representation of q parameter in qvalue_t |
173 | | */ |
174 | | int str2q(qvalue_t *q, char *s, int len); |
175 | | |
176 | | |
177 | | #endif /* _QVALUE_H */ |