parse_date.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (c) 2007 iptelorg GmbH
  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. * ser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*! \file
  23. * \brief Parser :: Date header
  24. *
  25. * \ingroup parser
  26. */
  27. #include <string.h>
  28. #include "parse_date.h"
  29. #include "parse_def.h"
  30. #include "parser_f.h" /* eat_space_end and so on */
  31. #include "../mem/mem.h"
  32. /*
  33. * Parse Date header field
  34. */
  35. #define READ(val) \
  36. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
  37. inline static int char2int (char *p, int *t)
  38. {
  39. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9')
  40. return -1;
  41. *t = (*p - '0')*10 + *(p + 1) - '0';
  42. return 0;
  43. }
  44. /*! \brief
  45. * Converts a RFC 1123 formatted date string to stuct tm
  46. */
  47. static int rfc1123totm (char *stime, struct tm *ttm ) {
  48. char *ptime = stime;
  49. unsigned int uval;
  50. int ires;
  51. uval = READ(ptime);
  52. ptime+=4;
  53. switch (uval) {
  54. /* Sun, */
  55. case 0x2c6e7553: ttm->tm_wday = 0; break;
  56. /* Mon, */
  57. case 0x2c6e6f4d: ttm->tm_wday = 1; break;
  58. /* Tue, */
  59. case 0x2c657554: ttm->tm_wday = 2; break;
  60. /* Wed, */
  61. case 0x2c646557: ttm->tm_wday = 3; break;
  62. /* Thu, */
  63. case 0x2c756854: ttm->tm_wday = 4; break;
  64. /* Fri, */
  65. case 0x2c697246: ttm->tm_wday = 5; break;
  66. /* Sat, */
  67. case 0x2c746153: ttm->tm_wday = 6; break;
  68. default: return -2;
  69. }
  70. if (*(ptime++)!=' ') return -3;
  71. if (char2int(ptime,&ttm->tm_mday) || ttm->tm_mday > 31) return -4;
  72. ptime+=2;
  73. if (*(ptime++) != ' ') return -5;
  74. uval = READ(ptime);
  75. ptime+=4;
  76. switch (uval) {
  77. /* Jan, */
  78. case 0x206e614a: ttm->tm_mon = 0; break;
  79. /* Feb, */
  80. case 0x20626546: ttm->tm_mon = 1; break;
  81. /* Mar, */
  82. case 0x2072614d: ttm->tm_mon = 2; break;
  83. /* Apr, */
  84. case 0x20727041: ttm->tm_mon = 3; break;
  85. /* May, */
  86. case 0x2079614d: ttm->tm_mon = 4; break;
  87. /* Jun, */
  88. case 0x206e754a: ttm->tm_mon = 5; break;
  89. /* Jul, */
  90. case 0x206c754a: ttm->tm_mon = 6; break;
  91. /* Aug, */
  92. case 0x20677541: ttm->tm_mon = 7; break;
  93. /* Sep, */
  94. case 0x20706553: ttm->tm_mon = 8; break;
  95. /* Oct, */
  96. case 0x2074634f: ttm->tm_mon = 9; break;
  97. /* Nov, */
  98. case 0x20766f4e: ttm->tm_mon = 10; break;
  99. /* Dec, */
  100. case 0x20636544: ttm->tm_mon = 11; break;
  101. default: return -6;
  102. }
  103. if (char2int(ptime,&ires)) return -7;
  104. ptime+=2;
  105. if (char2int(ptime,&ttm->tm_year)) return -8;
  106. ptime+=2;
  107. ttm->tm_year+=(ires-19)*100;
  108. if (*(ptime++) != ' ') return -9;
  109. if (char2int(ptime,&ttm->tm_hour) || ttm->tm_hour > 23) return -10;
  110. ptime+=2;
  111. if (*(ptime++) != ':') return -11;
  112. if (char2int(ptime,&ttm->tm_min) || ttm->tm_min > 59) return -12;
  113. ptime+=2;
  114. if (*(ptime++) != ':') return -13;
  115. if (char2int(ptime,&ttm->tm_sec) || ttm->tm_sec > 59) return -14;
  116. ptime+=2;
  117. /* " GMT" */
  118. uval = READ(ptime);
  119. if ((uval | 0x20202020) != 0x746d6720) return -15;
  120. return 0;
  121. }
  122. void parse_date(char *buffer, char *end, struct date_body *db)
  123. {
  124. db->error=PARSE_ERROR;
  125. /* check whether enough characters are available */
  126. if (end - buffer < RFC1123DATELENGTH)
  127. goto error;
  128. if (rfc1123totm(buffer,&db->date))
  129. goto error;
  130. db->error=PARSE_OK;
  131. return ;
  132. error:
  133. LOG(L_ERR,"ERROR: parse_date: parse error\n");
  134. return ;
  135. }
  136. int parse_date_header(struct sip_msg *msg)
  137. {
  138. struct date_body* date_b;
  139. if ( !msg->date && (parse_headers(msg,HDR_DATE_F,0)==-1 || !msg->date) ) {
  140. LOG(L_ERR,"ERROR:parse_date_header: bad msg or missing DATE header\n");
  141. goto error;
  142. }
  143. /* maybe the header is already parsed! */
  144. if (msg->date->parsed)
  145. return 0;
  146. date_b=pkg_malloc(sizeof(*date_b));
  147. if (date_b==0){
  148. LOG(L_ERR, "ERROR:parse_date_header: out of memory\n");
  149. goto error;
  150. }
  151. memset(date_b, 0, sizeof(*date_b));
  152. parse_date(msg->date->body.s,
  153. msg->date->body.s + msg->date->body.len+1,
  154. date_b);
  155. if (date_b->error==PARSE_ERROR){
  156. free_date(date_b);
  157. goto error;
  158. }
  159. msg->date->parsed=(void*)date_b;
  160. return 0;
  161. error:
  162. return -1;
  163. }
  164. void free_date(struct date_body *db)
  165. {
  166. pkg_free(db);
  167. }