tsdp_parser_message.rl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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_machine_message.rl
  21. * @brief Ragel file.
  22. */
  23. #include "tinysdp/parsers/tsdp_parser_message.h"
  24. #include "tinysdp/headers/tsdp_header_A.h"
  25. #include "tinysdp/headers/tsdp_header_B.h"
  26. #include "tinysdp/headers/tsdp_header_C.h"
  27. #include "tinysdp/headers/tsdp_header_Dummy.h"
  28. #include "tinysdp/headers/tsdp_header_E.h"
  29. #include "tinysdp/headers/tsdp_header_I.h"
  30. #include "tinysdp/headers/tsdp_header_K.h"
  31. #include "tinysdp/headers/tsdp_header_M.h"
  32. #include "tinysdp/headers/tsdp_header_O.h"
  33. #include "tinysdp/headers/tsdp_header_P.h"
  34. #include "tinysdp/headers/tsdp_header_R.h"
  35. #include "tinysdp/headers/tsdp_header_S.h"
  36. #include "tinysdp/headers/tsdp_header_T.h"
  37. #include "tinysdp/headers/tsdp_header_U.h"
  38. #include "tinysdp/headers/tsdp_header_V.h"
  39. #include "tinysdp/headers/tsdp_header_Z.h"
  40. #include "tsk_debug.h"
  41. %%{
  42. machine tsdp_machine_message;
  43. ###########################################
  44. # Includes
  45. ###########################################
  46. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  47. action tag{
  48. tag_start = p;
  49. }
  50. ###########################################
  51. # Actions
  52. ###########################################
  53. action parse_header_A{
  54. if(hdr_M){
  55. if(!hdr_M->Attributes){
  56. hdr_M->Attributes = tsk_list_create();
  57. }
  58. if((header = (tsdp_header_t*)tsdp_header_A_parse(tag_start, (p - tag_start)))){
  59. tsk_list_push_back_data(hdr_M->Attributes, (void**)&header);
  60. }
  61. }
  62. else if((header = (tsdp_header_t*)tsdp_header_A_parse(tag_start, (p - tag_start)))){
  63. tsdp_message_add_header(sdp_msg, header);
  64. tsk_object_unref(header);
  65. }
  66. }
  67. action parse_header_B{
  68. if(hdr_M){
  69. if(!hdr_M->Bandwidths){
  70. hdr_M->Bandwidths = tsk_list_create();
  71. }
  72. if((header = (tsdp_header_t*)tsdp_header_B_parse(tag_start, (p - tag_start)))){
  73. tsk_list_push_back_data(hdr_M->Bandwidths, (void**)&header);
  74. }
  75. }
  76. else if((header = (tsdp_header_t*)tsdp_header_B_parse(tag_start, (p - tag_start)))){
  77. tsdp_message_add_header(sdp_msg, header);
  78. tsk_object_unref(header);
  79. }
  80. }
  81. action parse_header_C{
  82. if(hdr_M && !hdr_M->C){
  83. hdr_M->C = tsdp_header_C_parse(tag_start, (p - tag_start));
  84. }
  85. else if((header = (tsdp_header_t*)tsdp_header_C_parse(tag_start, (p - tag_start)))){
  86. tsdp_message_add_header(sdp_msg, header);
  87. tsk_object_unref(header);
  88. }
  89. }
  90. action parse_header_Dummy{
  91. if((header = (tsdp_header_t*)tsdp_header_Dummy_parse(tag_start, (p - tag_start)))){
  92. tsdp_message_add_header(sdp_msg, header);
  93. tsk_object_unref(header);
  94. }
  95. }
  96. action parse_header_E{
  97. if((header = (tsdp_header_t*)tsdp_header_E_parse(tag_start, (p - tag_start)))){
  98. tsdp_message_add_header(sdp_msg, header);
  99. tsk_object_unref(header);
  100. }
  101. }
  102. action parse_header_I{
  103. if(hdr_M && !hdr_M->I){
  104. hdr_M->I = tsdp_header_I_parse(tag_start, (p - tag_start));
  105. }
  106. else if((header = (tsdp_header_t*)tsdp_header_I_parse(tag_start, (p - tag_start)))){
  107. tsdp_message_add_header(sdp_msg, header);
  108. tsk_object_unref(header);
  109. }
  110. }
  111. action parse_header_K{
  112. if(hdr_M && !hdr_M->K){
  113. hdr_M->K = tsdp_header_K_parse(tag_start, (p - tag_start));
  114. }
  115. else if((header = (tsdp_header_t*)tsdp_header_K_parse(tag_start, (p - tag_start)))){
  116. tsdp_message_add_header(sdp_msg, header);
  117. tsk_object_unref(header);
  118. }
  119. }
  120. action parse_header_M{
  121. if((hdr_M = tsdp_header_M_parse(tag_start, (p - tag_start)))){
  122. tsdp_message_add_header(sdp_msg, TSDP_HEADER(hdr_M));
  123. hdr_M = tsk_object_unref(hdr_M);
  124. }
  125. }
  126. action parse_header_O{
  127. if((header = (tsdp_header_t*)tsdp_header_O_parse(tag_start, (p - tag_start)))){
  128. tsdp_message_add_header(sdp_msg, header);
  129. tsk_object_unref(header);
  130. }
  131. }
  132. action parse_header_P{
  133. if((header = (tsdp_header_t*)tsdp_header_P_parse(tag_start, (p - tag_start)))){
  134. tsdp_message_add_header(sdp_msg, header);
  135. tsk_object_unref(header);
  136. }
  137. }
  138. action parse_header_R{
  139. if((header = (tsdp_header_t*)tsdp_header_R_parse(tag_start, (p - tag_start)))){
  140. if(hdr_T){
  141. if(!hdr_T->repeat_fields){
  142. hdr_T->repeat_fields = tsk_list_create();
  143. }
  144. tsk_list_push_back_data(hdr_T->repeat_fields, (void**)&header);
  145. }
  146. else{
  147. tsdp_message_add_header(sdp_msg, header);
  148. tsk_object_unref(header);
  149. }
  150. }
  151. }
  152. action parse_header_S{
  153. if((header = (tsdp_header_t*)tsdp_header_S_parse(tag_start, (p - tag_start)))){
  154. tsdp_message_add_header(sdp_msg, header);
  155. tsk_object_unref(header);
  156. }
  157. }
  158. action parse_header_T{
  159. if((hdr_T = tsdp_header_T_parse(tag_start, (p - tag_start)))){
  160. tsdp_message_add_header(sdp_msg, TSDP_HEADER(hdr_T));
  161. hdr_T = tsk_object_unref(hdr_T);
  162. }
  163. }
  164. action parse_header_U{
  165. if((header = (tsdp_header_t*)tsdp_header_U_parse(tag_start, (p - tag_start)))){
  166. tsdp_message_add_header(sdp_msg, header);
  167. tsk_object_unref(header);
  168. }
  169. }
  170. action parse_header_V{
  171. if((header = (tsdp_header_t*)tsdp_header_V_parse(tag_start, (p - tag_start)))){
  172. tsdp_message_add_header(sdp_msg, header);
  173. tsk_object_unref(header);
  174. }
  175. }
  176. action parse_header_Z{
  177. if((header = (tsdp_header_t*)tsdp_header_Z_parse(tag_start, (p - tag_start)))){
  178. tsdp_message_add_header(sdp_msg, header);
  179. tsk_object_unref(header);
  180. }
  181. }
  182. ###########################################
  183. # Headers
  184. ###########################################
  185. A = "a"i SP* "=" SP*<: any* %parse_header_A :>CRLF;
  186. B = "b"i SP* "=" SP*<: any* %parse_header_B :>CRLF;
  187. C = "c"i SP* "=" SP*<: any* %parse_header_C :>CRLF;
  188. E = "e"i SP* "=" SP*<: any* %parse_header_E :>CRLF;
  189. I = "i"i SP* "=" SP*<: any* %parse_header_I :>CRLF;
  190. K = "k"i SP* "=" SP*<: any* %parse_header_K :>CRLF;
  191. M = "m"i SP* "=" SP*<: any* %parse_header_M :>CRLF;
  192. O = "o"i SP* "=" SP*<: any* %parse_header_O :>CRLF;
  193. P = "p"i SP* "=" SP*<: any* %parse_header_P :>CRLF;
  194. R = "r"i SP* "=" SP*<: any* %parse_header_R :>CRLF;
  195. S = "s"i SP* "=" SP*<: any* %parse_header_S :>CRLF;
  196. T = "t"i SP* "=" SP*<: any* %parse_header_T :>CRLF;
  197. U = "u"i SP* "=" SP*<: any* %parse_header_U :>CRLF;
  198. V = "v"i SP* "=" SP*<: any* %parse_header_V :>CRLF;
  199. Z = "z"i SP* "=" SP*<: any* %parse_header_Z :>CRLF;
  200. Dummy = alpha SP* "=" SP*<: any* %parse_header_Dummy :>CRLF;
  201. Header = (A | B | C | E | I | K | M | O | P | R | S | T | U | V | Z)>tag >1 | (Dummy>tag) >0;
  202. ###########################################
  203. # Message
  204. ###########################################
  205. SDP_message = Header*;
  206. ###########################################
  207. # Entry Point
  208. ###########################################
  209. main := SDP_message;
  210. }%%
  211. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  212. /* Ragel data */
  213. %% write data;
  214. TSK_RAGEL_DISABLE_WARNINGS_END()
  215. tsdp_message_t* tsdp_message_parse(const void *input, tsk_size_t size)
  216. {
  217. tsdp_message_t* sdp_msg = tsk_null;
  218. const char* tag_start = tsk_null;
  219. tsdp_header_t *header = tsk_null;
  220. tsdp_header_T_t *hdr_T = tsk_null;
  221. tsdp_header_M_t *hdr_M = tsk_null;
  222. /* Ragel variables */
  223. int cs = 0;
  224. const char* p = input;
  225. const char* pe = p + size;
  226. const char* eof = tsk_null;
  227. (void)(eof);
  228. if(!input || !size){
  229. TSK_DEBUG_ERROR("Null or empty buffer.");
  230. goto bail;
  231. }
  232. if(!(sdp_msg = tsdp_message_create())){
  233. goto bail;
  234. }
  235. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  236. /* Ragel init */
  237. %% write init;
  238. /* Ragel execute */
  239. %% write exec;
  240. TSK_RAGEL_DISABLE_WARNINGS_END()
  241. /* Check result */
  242. if( cs < %%{ write first_final; }%% )
  243. {
  244. TSK_DEBUG_ERROR("Failed to parse SDP message.");
  245. TSK_OBJECT_SAFE_FREE(sdp_msg);
  246. goto bail;
  247. }
  248. bail:
  249. return sdp_msg;
  250. }