parse_date.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2007 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * ser is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*! \file
  21. * \brief Parser :: Date header
  22. *
  23. * \ingroup parser
  24. */
  25. #include <string.h>
  26. #include "parse_date.h"
  27. #include "parse_def.h"
  28. #include "parser_f.h" /* eat_space_end and so on */
  29. #include "../mem/mem.h"
  30. /*
  31. * Parse Date header field
  32. */
  33. #define READ(val) \
  34. (*(val + 0) + (*(val + 1) << 8) + (*(val + 2) << 16) + (*(val + 3) << 24))
  35. inline static int char2int (char *p, int *t)
  36. {
  37. if (*p < '0' || *p > '9' || *(p + 1) < '0' || *(p + 1) > '9')
  38. return -1;
  39. *t = (*p - '0')*10 + *(p + 1) - '0';
  40. return 0;
  41. }
  42. /*! \brief
  43. * Converts a RFC 1123 formatted date string to stuct tm
  44. */
  45. static int rfc1123totm (char *stime, struct tm *ttm ) {
  46. char *ptime = stime;
  47. unsigned int uval;
  48. int ires;
  49. uval = READ(ptime);
  50. ptime+=4;
  51. switch (uval) {
  52. /* Sun, */
  53. case 0x2c6e7553: ttm->tm_wday = 0; break;
  54. /* Mon, */
  55. case 0x2c6e6f4d: ttm->tm_wday = 1; break;
  56. /* Tue, */
  57. case 0x2c657554: ttm->tm_wday = 2; break;
  58. /* Wed, */
  59. case 0x2c646557: ttm->tm_wday = 3; break;
  60. /* Thu, */
  61. case 0x2c756854: ttm->tm_wday = 4; break;
  62. /* Fri, */
  63. case 0x2c697246: ttm->tm_wday = 5; break;
  64. /* Sat, */
  65. case 0x2c746153: ttm->tm_wday = 6; break;
  66. default: return -2;
  67. }
  68. if (*(ptime++)!=' ') return -3;
  69. if (char2int(ptime,&ttm->tm_mday) || ttm->tm_mday > 31) return -4;
  70. ptime+=2;
  71. if (*(ptime++) != ' ') return -5;
  72. uval = READ(ptime);
  73. ptime+=4;
  74. switch (uval) {
  75. /* Jan, */
  76. case 0x206e614a: ttm->tm_mon = 0; break;
  77. /* Feb, */
  78. case 0x20626546: ttm->tm_mon = 1; break;
  79. /* Mar, */
  80. case 0x2072614d: ttm->tm_mon = 2; break;
  81. /* Apr, */
  82. case 0x20727041: ttm->tm_mon = 3; break;
  83. /* May, */
  84. case 0x2079614d: ttm->tm_mon = 4; break;
  85. /* Jun, */
  86. case 0x206e754a: ttm->tm_mon = 5; break;
  87. /* Jul, */
  88. case 0x206c754a: ttm->tm_mon = 6; break;
  89. /* Aug, */
  90. case 0x20677541: ttm->tm_mon = 7; break;
  91. /* Sep, */
  92. case 0x20706553: ttm->tm_mon = 8; break;
  93. /* Oct, */
  94. case 0x2074634f: ttm->tm_mon = 9; break;
  95. /* Nov, */
  96. case 0x20766f4e: ttm->tm_mon = 10; break;
  97. /* Dec, */
  98. case 0x20636544: ttm->tm_mon = 11; break;
  99. default: return -6;
  100. }
  101. if (char2int(ptime,&ires)) return -7;
  102. ptime+=2;
  103. if (char2int(ptime,&ttm->tm_year)) return -8;
  104. ptime+=2;
  105. ttm->tm_year+=(ires-19)*100;
  106. if (*(ptime++) != ' ') return -9;
  107. if (char2int(ptime,&ttm->tm_hour) || ttm->tm_hour > 23) return -10;
  108. ptime+=2;
  109. if (*(ptime++) != ':') return -11;
  110. if (char2int(ptime,&ttm->tm_min) || ttm->tm_min > 59) return -12;
  111. ptime+=2;
  112. if (*(ptime++) != ':') return -13;
  113. if (char2int(ptime,&ttm->tm_sec) || ttm->tm_sec > 59) return -14;
  114. ptime+=2;
  115. /* " GMT" */
  116. uval = READ(ptime);
  117. if ((uval | 0x20202020) != 0x746d6720) return -15;
  118. return 0;
  119. }
  120. void parse_date(char *buffer, char *end, struct date_body *db)
  121. {
  122. db->error=PARSE_ERROR;
  123. /* check whether enough characters are available */
  124. if (end - buffer < RFC1123DATELENGTH)
  125. goto error;
  126. if (rfc1123totm(buffer,&db->date))
  127. goto error;
  128. db->error=PARSE_OK;
  129. return ;
  130. error:
  131. LOG(L_ERR,"ERROR: parse_date: parse error\n");
  132. return ;
  133. }
  134. int parse_date_header(struct sip_msg *msg)
  135. {
  136. struct date_body* date_b;
  137. if ( !msg->date && (parse_headers(msg,HDR_DATE_F,0)==-1 || !msg->date) ) {
  138. LOG(L_ERR,"ERROR:parse_date_header: bad msg or missing DATE header\n");
  139. goto error;
  140. }
  141. /* maybe the header is already parsed! */
  142. if (msg->date->parsed)
  143. return 0;
  144. date_b=pkg_malloc(sizeof(*date_b));
  145. if (date_b==0){
  146. LOG(L_ERR, "ERROR:parse_date_header: out of memory\n");
  147. goto error;
  148. }
  149. memset(date_b, 0, sizeof(*date_b));
  150. parse_date(msg->date->body.s,
  151. msg->date->body.s + msg->date->body.len+1,
  152. date_b);
  153. if (date_b->error==PARSE_ERROR){
  154. free_date(date_b);
  155. goto error;
  156. }
  157. msg->date->parsed=(void*)date_b;
  158. return 0;
  159. error:
  160. return -1;
  161. }
  162. void free_date(struct date_body *db)
  163. {
  164. pkg_free(db);
  165. }