qvalue.c 3.3 KB

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