tsdp_parser_header_K.rl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_K.c
  21. * @brief SDP "k=" header (Encryption Key).
  22. */
  23. #include "tinysdp/headers/tsdp_header_K.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_K;
  33. # Includes
  34. include tsdp_machine_utils "./ragel/tsdp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_value{
  39. TSK_PARSER_SET_STRING(hdr_K->value);
  40. }
  41. K = 'k' SP* "=" SP*<: any*>tag %parse_value;
  42. # Entry point
  43. main := K :>CRLF?;
  44. }%%
  45. tsdp_header_K_t* tsdp_header_K_create(const char* value)
  46. {
  47. return tsk_object_new(TSDP_HEADER_K_VA_ARGS(value));
  48. }
  49. tsdp_header_K_t* tsdp_header_K_create_null()
  50. {
  51. return tsdp_header_K_create(tsk_null);
  52. }
  53. int tsdp_header_K_tostring(const tsdp_header_t* header, tsk_buffer_t* output)
  54. {
  55. if(header){
  56. const tsdp_header_K_t *K = (const tsdp_header_K_t *)header;
  57. if(K->value){
  58. tsk_buffer_append(output, K->value, tsk_strlen(K->value));
  59. }
  60. return 0;
  61. }
  62. return -1;
  63. }
  64. tsdp_header_t* tsdp_header_K_clone(const tsdp_header_t* header)
  65. {
  66. if(header){
  67. const tsdp_header_K_t *K = (const tsdp_header_K_t *)header;
  68. return (tsdp_header_t*)tsdp_header_K_create(K->value);
  69. }
  70. return tsk_null;
  71. }
  72. tsdp_header_K_t *tsdp_header_K_parse(const char *data, tsk_size_t size)
  73. {
  74. int cs = 0;
  75. const char *p = data;
  76. const char *pe = p + size;
  77. const char *eof = pe;
  78. tsdp_header_K_t *hdr_K = tsdp_header_K_create_null();
  79. const char *tag_start = tsk_null;
  80. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  81. %%write data;
  82. (void)(tsdp_machine_parser_header_K_first_final);
  83. (void)(tsdp_machine_parser_header_K_error);
  84. (void)(tsdp_machine_parser_header_K_en_main);
  85. %%write init;
  86. %%write exec;
  87. TSK_RAGEL_DISABLE_WARNINGS_END()
  88. if( cs < %%{ write first_final; }%% ){
  89. TSK_DEBUG_ERROR("Failed to parse \"k=\" header.");
  90. TSK_OBJECT_SAFE_FREE(hdr_K);
  91. }
  92. return hdr_K;
  93. }
  94. //========================================================
  95. // K header object definition
  96. //
  97. static tsk_object_t* tsdp_header_K_ctor(tsk_object_t *self, va_list * app)
  98. {
  99. tsdp_header_K_t *K = self;
  100. if(K){
  101. TSDP_HEADER(K)->type = tsdp_htype_K;
  102. TSDP_HEADER(K)->tostring = tsdp_header_K_tostring;
  103. TSDP_HEADER(K)->clone = tsdp_header_K_clone;
  104. TSDP_HEADER(K)->rank = TSDP_HTYPE_P_RANK;
  105. K->value = tsk_strdup(va_arg(*app, const char*));
  106. }
  107. else{
  108. TSK_DEBUG_ERROR("Failed to create new K header.");
  109. }
  110. return self;
  111. }
  112. static tsk_object_t* tsdp_header_K_dtor(tsk_object_t *self)
  113. {
  114. tsdp_header_K_t *K = self;
  115. if(K){
  116. TSK_FREE(K->value);
  117. }
  118. else{
  119. TSK_DEBUG_ERROR("Null K header.");
  120. }
  121. return self;
  122. }
  123. static int tsdp_header_K_cmp(const tsk_object_t *obj1, const tsk_object_t *obj2)
  124. {
  125. if(obj1 && obj2){
  126. return tsdp_header_rank_cmp(obj1, obj2);
  127. }
  128. else{
  129. return -1;
  130. }
  131. }
  132. static const tsk_object_def_t tsdp_header_K_def_s =
  133. {
  134. sizeof(tsdp_header_K_t),
  135. tsdp_header_K_ctor,
  136. tsdp_header_K_dtor,
  137. tsdp_header_K_cmp
  138. };
  139. const tsk_object_def_t *tsdp_header_K_def_t = &tsdp_header_K_def_s;