parse_expires.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Expires header field body parser
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2003-04-26 ZSW (jiri)
  30. */
  31. /*! \file
  32. * \brief Parser :: Expires header field body parser.
  33. *
  34. *
  35. * \ingroup parser
  36. */
  37. #include "parse_expires.h"
  38. #include <stdio.h> /* printf */
  39. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  40. #include "../dprint.h"
  41. #include "../trim.h" /* trim_leading */
  42. #include <string.h> /* memset */
  43. #include "../ut.h"
  44. static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
  45. {
  46. int i;
  47. str tmp;
  48. tmp.s = _s;
  49. tmp.len = _l;
  50. trim(&tmp);
  51. if (tmp.len == 0) {
  52. LOG(L_ERR, "expires_parser(): Empty body\n");
  53. _e->valid = 0;
  54. return -1;
  55. }
  56. _e->text.s = tmp.s;
  57. _e->text.len = tmp.len;
  58. /* more then 32bit/maxuint cant be valid */
  59. if (tmp.len > 10) {
  60. _e->valid = 0;
  61. return 0;
  62. }
  63. for(i = 0; i < tmp.len; i++) {
  64. if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
  65. _e->val *= 10;
  66. _e->val += tmp.s[i] - '0';
  67. } else {
  68. switch(tmp.s[i]) {
  69. case ' ':
  70. case '\t':
  71. case '\r':
  72. case '\n':
  73. _e->text.len = i;
  74. _e->valid = 1;
  75. return 0;
  76. default:
  77. /* Exit normally here, we want to be backwards compatible with
  78. * RFC2543 entities that can put absolute time here
  79. */
  80. /*
  81. LOG(L_ERR, "expires_parser(): Invalid character\n");
  82. return -2;
  83. */
  84. _e->valid = 0;
  85. return 0;
  86. }
  87. }
  88. }
  89. _e->valid = 1;
  90. return 0;
  91. }
  92. /*! \brief
  93. * Parse expires header field body
  94. */
  95. int parse_expires(struct hdr_field* _h)
  96. {
  97. exp_body_t* e;
  98. if (_h->parsed) {
  99. return 0; /* Already parsed */
  100. }
  101. e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
  102. if (e == 0) {
  103. LOG(L_ERR, "parse_expires(): No memory left\n");
  104. return -1;
  105. }
  106. memset(e, 0, sizeof(exp_body_t));
  107. if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
  108. LOG(L_ERR, "parse_expires(): Error while parsing\n");
  109. pkg_free(e);
  110. return -2;
  111. }
  112. _h->parsed = (void*)e;
  113. return 0;
  114. }
  115. /*! \brief
  116. * Free all memory associated with exp_body_t
  117. */
  118. void free_expires(exp_body_t** _e)
  119. {
  120. pkg_free(*_e);
  121. *_e = 0;
  122. }
  123. /*! \brief
  124. * Print exp_body_t content, for debugging only
  125. */
  126. void print_expires(exp_body_t* _e)
  127. {
  128. printf("===Expires===\n");
  129. printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
  130. printf("val : %d\n", _e->val);
  131. printf("===/Expires===\n");
  132. }