qvalue.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * $Id$
  3. *
  4. * Handling of the q value
  5. *
  6. * Copyright (C) 2004 FhG FOKUS
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser 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. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. *
  29. * History
  30. * ------
  31. * 2004-04-25 created (janakj)
  32. */
  33. /*!
  34. * \file
  35. * \brief SIP-router core :: Handling of the q value
  36. * \ingroup core
  37. * Module: \ref core
  38. */
  39. #include "error.h"
  40. #include "qvalue.h"
  41. /*
  42. * Convert string representation of q parameter in qvalue_t
  43. */
  44. int str2q(qvalue_t* q, char* s, int len)
  45. {
  46. int i, digits, order;
  47. /* States and equivalent regular expressions of input */
  48. enum {
  49. ST_START, /* (SPC|TAB)* */
  50. ST_0, /* 0+ */
  51. ST_1, /* 1 */
  52. ST_0_PT, /* 0*\. */
  53. ST_1_PT, /* 1\. */
  54. ST_1_PT_0, /* 1\.0+ */
  55. ST_0_PT_N /* 0*\.[0-9]+ */
  56. } state = ST_START;
  57. if (!q || !s) {
  58. return E_INVALID_PARAMS;
  59. }
  60. digits = 1;
  61. order = 100;
  62. for(i = 0; i < len; i++) {
  63. switch(state) {
  64. case ST_START:
  65. switch(s[i]) {
  66. case ' ':
  67. case '\t':
  68. break;
  69. case '0':
  70. *q = 0;
  71. state = ST_0;
  72. break;
  73. case '1':
  74. *q = 1000;
  75. state = ST_1;
  76. break;
  77. case '.':
  78. state = ST_0_PT;
  79. break;
  80. default:
  81. return E_Q_INV_CHAR;
  82. }
  83. break;
  84. case ST_0:
  85. switch(s[i]) {
  86. case '0':
  87. break;
  88. case '.':
  89. state = ST_0_PT;
  90. break;
  91. case '1':
  92. *q = 1000;
  93. state = ST_1;
  94. break;
  95. default:
  96. if (s[i] >= '2' && s[i] <= '9') {
  97. return E_Q_TOO_BIG;
  98. } else {
  99. return E_Q_INV_CHAR;
  100. }
  101. }
  102. break;
  103. case ST_1:
  104. if (s[i] == '.') {
  105. state = ST_1_PT;
  106. break;
  107. } else {
  108. if (s[i] >= '0' && s[i] <= '9') {
  109. return E_Q_TOO_BIG;
  110. } else {
  111. return E_Q_INV_CHAR;
  112. }
  113. }
  114. break;
  115. case ST_0_PT:
  116. if (s[i] >= '0' && s[i] <= '9') {
  117. *q = (s[i] - '0') * order;
  118. order /= 10;
  119. state = ST_0_PT_N;
  120. } else {
  121. return E_Q_INV_CHAR;
  122. }
  123. break;
  124. case ST_1_PT:
  125. if (s[i] == '0') {
  126. state = ST_1_PT_0;
  127. } else {
  128. if (s[i] >= '1' && s[i] <= '9') {
  129. return E_Q_TOO_BIG;
  130. } else {
  131. return E_Q_INV_CHAR;
  132. }
  133. }
  134. break;
  135. case ST_1_PT_0:
  136. if (s[i] == '0') {
  137. break;
  138. } else {
  139. if (s[i] >= '1' && s[i] <= '9') {
  140. return E_Q_TOO_BIG;
  141. } else {
  142. return E_Q_INV_CHAR;
  143. }
  144. }
  145. break;
  146. case ST_0_PT_N:
  147. if (s[i] >= '0' && s[i] <= '9') {
  148. if (digits <= 3) {
  149. *q += (s[i] - '0') * order;
  150. order /= 10;
  151. digits++;
  152. }
  153. } else {
  154. return E_Q_INV_CHAR;
  155. }
  156. break;
  157. }
  158. }
  159. switch(state) {
  160. case ST_START:
  161. return E_Q_EMPTY;
  162. case ST_0_PT:
  163. case ST_1_PT:
  164. return E_Q_DEC_MISSING;
  165. default:
  166. return 0;
  167. }
  168. }