parse_expires.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include "parse_expires.h"
  30. #include <stdio.h> /* printf */
  31. #include "../mem/mem.h" /* pkg_malloc, pkg_free */
  32. #include "../dprint.h"
  33. #include "../trim.h" /* trim_leading */
  34. #include <string.h> /* memset */
  35. static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
  36. {
  37. int i;
  38. str tmp;
  39. tmp.s = _s;
  40. tmp.len = _l;
  41. trim_leading(&tmp);
  42. if (tmp.len == 0) {
  43. LOG(L_ERR, "expires_parser(): Empty body\n");
  44. return -1;
  45. }
  46. _e->text.s = tmp.s;
  47. for(i = 0; i < tmp.len; i++) {
  48. if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
  49. _e->val *= 10;
  50. _e->val += tmp.s[i] - '0';
  51. } else {
  52. switch(tmp.s[i]) {
  53. case ' ':
  54. case '\t':
  55. case '\r':
  56. case '\n':
  57. _e->text.len = i;
  58. return 0;
  59. default:
  60. LOG(L_ERR, "expires_parser(): Invalid character\n");
  61. return -2;
  62. }
  63. }
  64. }
  65. _e->text.len = _l;
  66. return 0;
  67. }
  68. /*
  69. * Parse expires header field body
  70. */
  71. int parse_expires(struct hdr_field* _h)
  72. {
  73. exp_body_t* e;
  74. if (_h->parsed) {
  75. return 0; /* Already parsed */
  76. }
  77. e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
  78. if (e == 0) {
  79. LOG(L_ERR, "parse_expires(): No memory left\n");
  80. return -1;
  81. }
  82. memset(e, 0, sizeof(exp_body_t));
  83. if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
  84. LOG(L_ERR, "parse_expires(): Error while parsing\n");
  85. pkg_free(e);
  86. return -2;
  87. }
  88. _h->parsed = (void*)e;
  89. return 0;
  90. }
  91. /*
  92. * Free all memory associated with exp_body_t
  93. */
  94. void free_expires(exp_body_t** _e)
  95. {
  96. pkg_free(*_e);
  97. *_e = 0;
  98. }
  99. /*
  100. * Print exp_body_t content, for debugging only
  101. */
  102. void print_expires(exp_body_t* _e)
  103. {
  104. printf("===Expires===\n");
  105. printf("text: \'%.*s\'\n", _e->text.len, _e->text.s);
  106. printf("val : %d\n", _e->val);
  107. printf("===/Expires===\n");
  108. }