parse_expires.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * $Id$
  3. *
  4. * Expires header field body parser
  5. *
  6. * Copyright (C) 2001-2003 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. * 2003-04-26 ZSW (jiri)
  32. */
  33. /*! \file
  34. * \brief Parser :: Expires header field body parser.
  35. *
  36. *
  37. * \ingroup parser
  38. */
  39. #include "parse_expires.h"
  40. #include <stdio.h> /* printf */
  41. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  42. #include "../dprint.h"
  43. #include "../trim.h" /* trim_leading */
  44. #include <string.h> /* memset */
  45. #include "../ut.h"
  46. static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
  47. {
  48. int i;
  49. str tmp;
  50. tmp.s = _s;
  51. tmp.len = _l;
  52. trim(&tmp);
  53. if (tmp.len == 0) {
  54. LOG(L_ERR, "expires_parser(): Empty body\n");
  55. _e->valid = 0;
  56. return -1;
  57. }
  58. _e->text.s = tmp.s;
  59. _e->text.len = tmp.len;
  60. /* more then 32bit/maxuint cant be valid */
  61. if (tmp.len > 10) {
  62. _e->valid = 0;
  63. return 0;
  64. }
  65. for(i = 0; i < tmp.len; i++) {
  66. if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
  67. _e->val *= 10;
  68. _e->val += tmp.s[i] - '0';
  69. } else {
  70. switch(tmp.s[i]) {
  71. case ' ':
  72. case '\t':
  73. case '\r':
  74. case '\n':
  75. _e->text.len = i;
  76. _e->valid = 1;
  77. return 0;
  78. default:
  79. /* Exit normally here, we want to be backwards compatible with
  80. * RFC2543 entities that can put absolute time here
  81. */
  82. /*
  83. LOG(L_ERR, "expires_parser(): Invalid character\n");
  84. return -2;
  85. */
  86. _e->valid = 0;
  87. return 0;
  88. }
  89. }
  90. }
  91. _e->valid = 1;
  92. return 0;
  93. }
  94. /*! \brief
  95. * Parse expires header field body
  96. */
  97. int parse_expires(struct hdr_field* _h)
  98. {
  99. exp_body_t* e;
  100. if (_h->parsed) {
  101. return 0; /* Already parsed */
  102. }
  103. e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
  104. if (e == 0) {
  105. LOG(L_ERR, "parse_expires(): No memory left\n");
  106. return -1;
  107. }
  108. memset(e, 0, sizeof(exp_body_t));
  109. if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
  110. LOG(L_ERR, "parse_expires(): Error while parsing\n");
  111. pkg_free(e);
  112. return -2;
  113. }
  114. _h->parsed = (void*)e;
  115. return 0;
  116. }
  117. /*! \brief
  118. * Free all memory associated with exp_body_t
  119. */
  120. void free_expires(exp_body_t** _e)
  121. {
  122. pkg_free(*_e);
  123. *_e = 0;
  124. }
  125. /*! \brief
  126. * Print exp_body_t content, for debugging only
  127. */
  128. void print_expires(exp_body_t* _e)
  129. {
  130. printf("===Expires===\n");
  131. printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
  132. printf("val : %d\n", _e->val);
  133. printf("===/Expires===\n");
  134. }