qvalue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * Kamailio 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. * Kamailio 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 Kamailio core :: Handling of the q value
  26. * \ingroup core
  27. * Module: \ref core
  28. */
  29. #include "error.h"
  30. #include "qvalue.h"
  31. /*
  32. * Convert string representation of q parameter in qvalue_t
  33. */
  34. int str2q(qvalue_t* q, char* s, int len)
  35. {
  36. int i, digits, order;
  37. /* States and equivalent regular expressions of input */
  38. enum {
  39. ST_START, /* (SPC|TAB)* */
  40. ST_0, /* 0+ */
  41. ST_1, /* 1 */
  42. ST_0_PT, /* 0*\. */
  43. ST_1_PT, /* 1\. */
  44. ST_1_PT_0, /* 1\.0+ */
  45. ST_0_PT_N /* 0*\.[0-9]+ */
  46. } state = ST_START;
  47. if (!q || !s) {
  48. return E_INVALID_PARAMS;
  49. }
  50. digits = 1;
  51. order = 100;
  52. for(i = 0; i < len; i++) {
  53. switch(state) {
  54. case ST_START:
  55. switch(s[i]) {
  56. case ' ':
  57. case '\t':
  58. break;
  59. case '0':
  60. *q = 0;
  61. state = ST_0;
  62. break;
  63. case '1':
  64. *q = 1000;
  65. state = ST_1;
  66. break;
  67. case '.':
  68. state = ST_0_PT;
  69. break;
  70. default:
  71. return E_Q_INV_CHAR;
  72. }
  73. break;
  74. case ST_0:
  75. switch(s[i]) {
  76. case '0':
  77. break;
  78. case '.':
  79. state = ST_0_PT;
  80. break;
  81. case '1':
  82. *q = 1000;
  83. state = ST_1;
  84. break;
  85. default:
  86. if (s[i] >= '2' && s[i] <= '9') {
  87. return E_Q_TOO_BIG;
  88. } else {
  89. return E_Q_INV_CHAR;
  90. }
  91. }
  92. break;
  93. case ST_1:
  94. if (s[i] == '.') {
  95. state = ST_1_PT;
  96. break;
  97. } else {
  98. if (s[i] >= '0' && s[i] <= '9') {
  99. return E_Q_TOO_BIG;
  100. } else {
  101. return E_Q_INV_CHAR;
  102. }
  103. }
  104. break;
  105. case ST_0_PT:
  106. if (s[i] >= '0' && s[i] <= '9') {
  107. *q = (s[i] - '0') * order;
  108. order /= 10;
  109. state = ST_0_PT_N;
  110. } else {
  111. return E_Q_INV_CHAR;
  112. }
  113. break;
  114. case ST_1_PT:
  115. if (s[i] == '0') {
  116. state = ST_1_PT_0;
  117. } else {
  118. if (s[i] >= '1' && s[i] <= '9') {
  119. return E_Q_TOO_BIG;
  120. } else {
  121. return E_Q_INV_CHAR;
  122. }
  123. }
  124. break;
  125. case ST_1_PT_0:
  126. if (s[i] == '0') {
  127. break;
  128. } else {
  129. if (s[i] >= '1' && s[i] <= '9') {
  130. return E_Q_TOO_BIG;
  131. } else {
  132. return E_Q_INV_CHAR;
  133. }
  134. }
  135. break;
  136. case ST_0_PT_N:
  137. if (s[i] >= '0' && s[i] <= '9') {
  138. if (digits <= 3) {
  139. *q += (s[i] - '0') * order;
  140. order /= 10;
  141. digits++;
  142. }
  143. } else {
  144. return E_Q_INV_CHAR;
  145. }
  146. break;
  147. }
  148. }
  149. switch(state) {
  150. case ST_START:
  151. return E_Q_EMPTY;
  152. case ST_0_PT:
  153. case ST_1_PT:
  154. return E_Q_DEC_MISSING;
  155. default:
  156. return 0;
  157. }
  158. }