tsdp_parser_header_O.rl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2010-2015 Mamadou Diop.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO 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 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO 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 DOUBANGO.
  18. *
  19. */
  20. /**@file tsdp_header_O.c
  21. * @brief SDP "o=" header (Origin).
  22. */
  23. #include "tinysdp/headers/tsdp_header_O.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include "tsk_string.h"
  27. #include <string.h>
  28. /***********************************
  29. * Ragel state machine.
  30. */
  31. %%{
  32. machine tsdp_machine_parser_header_O;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_username{
  39. TSK_PARSER_SET_STRING(hdr_O->username);
  40. }
  41. action parse_sess_id{
  42. TSK_PARSER_SET_UINT(hdr_O->sess_id);
  43. }
  44. action parse_sess_version{
  45. TSK_PARSER_SET_UINT(hdr_O->sess_version);
  46. }
  47. action parse_nettype{
  48. TSK_PARSER_SET_STRING(hdr_O->nettype);
  49. }
  50. action parse_addrtype{
  51. TSK_PARSER_SET_STRING(hdr_O->addrtype);
  52. }
  53. action parse_addr{
  54. TSK_PARSER_SET_STRING(hdr_O->addr);
  55. }
  56. username = any*>tag %parse_username;
  57. sess_id = DIGIT+>tag %parse_sess_id;
  58. sess_version = DIGIT+>tag %parse_sess_version;
  59. nettype = any*>tag %parse_nettype;
  60. addrtype = any*>tag %parse_addrtype;
  61. addr = any*>tag %parse_addr;
  62. O = 'o' SP* "=" SP*<: username :>SP sess_id :>SP sess_version :>SP nettype :>SP addrtype :>SP addr;
  63. # Entry point
  64. main := O :>CRLF?;
  65. }%%
  66. tsdp_header_O_t* tsdp_header_O_create(const char* username, uint32_t sess_id, uint32_t sess_version, const char* nettype, const char* addrtype, const char* addr)
  67. {
  68. return tsk_object_new(TSDP_HEADER_O_VA_ARGS(username, sess_id, sess_version, nettype, addrtype, addr));
  69. }
  70. tsdp_header_O_t* tsdp_header_O_create_null()
  71. {
  72. return tsdp_header_O_create(tsk_null, 0, 0, tsk_null, tsk_null, tsk_null);
  73. }
  74. tsdp_header_O_t* tsdp_header_O_create_default(const char* username, const char* nettype, const char* addrtype, const char* addr)
  75. {
  76. return tsdp_header_O_create(username, TSDP_HEADER_O_SESS_ID_DEFAULT, TSDP_HEADER_O_SESS_VERSION_DEFAULT, nettype, addrtype, addr);
  77. }
  78. int tsdp_header_O_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  79. {
  80. if(header){
  81. const tsdp_header_O_t *O = (const tsdp_header_O_t *)header;
  82. // o=alice 2890844526 2890844526 IN IP4 host.atlanta.example.com
  83. return tsk_buffer_append_2(output, "%s %u %u %s %s %s",
  84. O->username,
  85. O->sess_id,
  86. O->sess_version,
  87. O->nettype,
  88. O->addrtype,
  89. O->addr
  90. );
  91. return 0;
  92. }
  93. return -1;
  94. }
  95. tsdp_header_t* tsdp_header_O_clone(const tsdp_header_t* header)
  96. {
  97. if(header){
  98. const tsdp_header_O_t *O = (const tsdp_header_O_t *)header;
  99. return (tsdp_header_t*)tsdp_header_O_create(O->username, O->sess_id, O->sess_version, O->nettype, O->addrtype, O->addr);
  100. }
  101. return tsk_null;
  102. }
  103. tsdp_header_O_t *tsdp_header_O_parse(const char *data, tsk_size_t size)
  104. {
  105. int cs = 0;
  106. const char *p = data;
  107. const char *pe = p + size;
  108. const char *eof = pe;
  109. tsdp_header_O_t *hdr_O = tsdp_header_O_create_null();
  110. const char *tag_start = tsk_null;
  111. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  112. %%write data;
  113. (void)(tsdp_machine_parser_header_O_first_final);
  114. (void)(tsdp_machine_parser_header_O_error);
  115. (void)(tsdp_machine_parser_header_O_en_main);
  116. %%write init;
  117. %%write exec;
  118. TSK_RAGEL_DISABLE_WARNINGS_END()
  119. if( cs < %%{ write first_final; }%% ){
  120. TSK_DEBUG_ERROR("Failed to parse \"o=\" header.");
  121. TSK_OBJECT_SAFE_FREE(hdr_O);
  122. }
  123. return hdr_O;
  124. }
  125. //========================================================
  126. // O header object definition
  127. //
  128. static tsk_object_t* tsdp_header_O_ctor(tsk_object_t *self, va_list * app)
  129. {
  130. tsdp_header_O_t *O = self;
  131. if(O){
  132. TSDP_HEADER(O)->type = tsdp_htype_O;
  133. TSDP_HEADER(O)->tostring = tsdp_header_O_tostring;
  134. TSDP_HEADER(O)->clone = tsdp_header_O_clone;
  135. TSDP_HEADER(O)->rank = TSDP_HTYPE_O_RANK;
  136. O->username = tsk_strdup(va_arg(*app, const char*));
  137. O->sess_id = va_arg(*app, uint32_t);
  138. O->sess_version = va_arg(*app, uint32_t);
  139. O->nettype = tsk_strdup(va_arg(*app, const char*));
  140. O->addrtype = tsk_strdup(va_arg(*app, const char*));
  141. O->addr = tsk_strdup(va_arg(*app, const char*));
  142. }
  143. else{
  144. TSK_DEBUG_ERROR("Failed to create new O header.");
  145. }
  146. return self;
  147. }
  148. static tsk_object_t* tsdp_header_O_dtor(tsk_object_t *self)
  149. {
  150. tsdp_header_O_t *O = self;
  151. if(O){
  152. TSK_FREE(O->username);
  153. TSK_FREE(O->nettype);
  154. TSK_FREE(O->addrtype);
  155. TSK_FREE(O->addr);
  156. }
  157. else{
  158. TSK_DEBUG_ERROR("Null O header.");
  159. }
  160. return self;
  161. }
  162. static int tsdp_header_O_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  163. {
  164. if(obj1 && obj2){
  165. return tsdp_header_rank_cmp(obj1, obj2);
  166. }
  167. else{
  168. return -1;
  169. }
  170. }
  171. static const tsk_object_def_t tsdp_header_O_def_s =
  172. {
  173. sizeof(tsdp_header_O_t),
  174. tsdp_header_O_ctor,
  175. tsdp_header_O_dtor,
  176. tsdp_header_O_cmp
  177. };
  178. const tsk_object_def_t *tsdp_header_O_def_t = &tsdp_header_O_def_s;