tmsrp_parser_header_Byte-Range.rl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2009-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 tmsrp_header_Byte_Range.c
  21. * @brief MSRP 'Byte-Range' header.
  22. */
  23. #include "tinymsrp/headers/tmsrp_header_Byte-Range.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 tmsrp_machine_parser_header_Byte_Range;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_start{
  39. TSK_PARSER_SET_INTEGER_EX(hdr_Byte_Range->start, int64_t, atoi64);
  40. }
  41. action parse_end{
  42. if(tag_start && *tag_start == '*'){
  43. hdr_Byte_Range->end = -1;
  44. }
  45. else{
  46. TSK_PARSER_SET_INTEGER_EX(hdr_Byte_Range->end, int64_t, atoi64);
  47. }
  48. }
  49. action parse_total{
  50. if(tag_start && *tag_start == '*'){
  51. hdr_Byte_Range->total = -1;
  52. }
  53. else{
  54. TSK_PARSER_SET_INTEGER_EX(hdr_Byte_Range->total, int64_t, atoi64);
  55. }
  56. }
  57. #// Byte-Range = "Byte-Range:" SP range-start "-" range-end "/" total
  58. #//range-start = 1*DIGIT
  59. #//range-end = 1*DIGIT / "*"
  60. #//total = 1*DIGIT / "*"
  61. Byte_Range = "Byte-Range:"i SP DIGIT+>tag %parse_start "-" (DIGIT+ | "*")>tag %parse_end "/" (DIGIT+ | "*")>tag %parse_total;
  62. # Entry point
  63. main := Byte_Range :>CRLF?;
  64. }%%
  65. tmsrp_header_Byte_Range_t* tmsrp_header_Byte_Range_create(int64_t start, int64_t end, int64_t total)
  66. {
  67. return tsk_object_new(TMSRP_HEADER_BYTE_RANGE_VA_ARGS(start, end, total));
  68. }
  69. tmsrp_header_Byte_Range_t* tmsrp_header_Byte_Range_create_null()
  70. {
  71. return tmsrp_header_Byte_Range_create(1, -1, -1);
  72. }
  73. int tmsrp_header_Byte_Range_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  74. {
  75. if(header){
  76. const tmsrp_header_Byte_Range_t *Byte_Range = (const tmsrp_header_Byte_Range_t *)header;
  77. tsk_istr_t start, end, total;
  78. if(Byte_Range->start>=0){
  79. tsk_itoa(Byte_Range->start, &start);
  80. }
  81. if(Byte_Range->end>=0){
  82. tsk_itoa(Byte_Range->end, &end);
  83. }
  84. if(Byte_Range->total>=0){
  85. tsk_itoa(Byte_Range->total, &total);
  86. }
  87. return tsk_buffer_append_2(output, "%s-%s/%s",
  88. Byte_Range->start>=0 ? start : "*",
  89. Byte_Range->end>=0 ? end : "*",
  90. Byte_Range->total>=0 ? total : "*"
  91. );
  92. }
  93. return -1;
  94. }
  95. tmsrp_header_Byte_Range_t *tmsrp_header_Byte_Range_parse(const char *data, tsk_size_t size)
  96. {
  97. int cs = 0;
  98. const char *p = data;
  99. const char *pe = p + size;
  100. const char *eof = pe;
  101. tmsrp_header_Byte_Range_t *hdr_Byte_Range = tmsrp_header_Byte_Range_create_null();
  102. const char *tag_start = tsk_null;
  103. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  104. %%write data;
  105. (void)(eof);
  106. (void)(tmsrp_machine_parser_header_Byte_Range_first_final);
  107. (void)(tmsrp_machine_parser_header_Byte_Range_error);
  108. (void)(tmsrp_machine_parser_header_Byte_Range_en_main);
  109. %%write init;
  110. %%write exec;
  111. TSK_RAGEL_DISABLE_WARNINGS_END()
  112. if( cs < %%{ write first_final; }%% ){
  113. TSK_DEBUG_ERROR("Failed to parse 'Byte-Range' header.");
  114. TSK_OBJECT_SAFE_FREE(hdr_Byte_Range);
  115. }
  116. return hdr_Byte_Range;
  117. }
  118. //========================================================
  119. // Byte_Range header object definition
  120. //
  121. static tsk_object_t* tmsrp_header_Byte_Range_ctor(tsk_object_t *self, va_list * app)
  122. {
  123. tmsrp_header_Byte_Range_t *Byte_Range = self;
  124. if(Byte_Range){
  125. TMSRP_HEADER(Byte_Range)->type = tmsrp_htype_Byte_Range;
  126. TMSRP_HEADER(Byte_Range)->tostring = tmsrp_header_Byte_Range_tostring;
  127. Byte_Range->start = va_arg(*app, int64_t);
  128. Byte_Range->end = va_arg(*app, int64_t);
  129. Byte_Range->total = va_arg(*app, int64_t);
  130. }
  131. else{
  132. TSK_DEBUG_ERROR("Failed to create new Byte-Range header.");
  133. }
  134. return self;
  135. }
  136. static tsk_object_t* tmsrp_header_Byte_Range_dtor(tsk_object_t *self)
  137. {
  138. tmsrp_header_Byte_Range_t *Byte_Range = self;
  139. if(Byte_Range){
  140. }
  141. else{
  142. TSK_DEBUG_ERROR("Null Byte-Range header.");
  143. }
  144. return self;
  145. }
  146. static const tsk_object_def_t tmsrp_header_Byte_Range_def_s =
  147. {
  148. sizeof(tmsrp_header_Byte_Range_t),
  149. tmsrp_header_Byte_Range_ctor,
  150. tmsrp_header_Byte_Range_dtor,
  151. tsk_null
  152. };
  153. const tsk_object_def_t *tmsrp_header_Byte_Range_def_t = &tmsrp_header_Byte_Range_def_s;