You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

content_type_parser.rl 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. %%{
  2. machine content_type_parser;
  3. action Type_Start {
  4. qstart = NULL;
  5. qend = NULL;
  6. ct->type.begin = p;
  7. }
  8. action Type_End {
  9. if (qstart) {
  10. ct->type.begin = qstart;
  11. }
  12. if (qend && qend >= qstart) {
  13. ct->type.len = qend - qstart;
  14. }
  15. else if (p >= ct->type.begin) {
  16. ct->type.len = p - ct->type.begin;
  17. }
  18. qstart = NULL;
  19. qend = NULL;
  20. }
  21. action Subtype_Start {
  22. qstart = NULL;
  23. qend = NULL;
  24. ct->subtype.begin = p;
  25. }
  26. action Subtype_End {
  27. if (qstart) {
  28. ct->subtype.begin = qstart;
  29. }
  30. if (qend && qend >= qstart) {
  31. ct->subtype.len = qend - qstart;
  32. }
  33. else if (p >= ct->subtype.begin) {
  34. ct->subtype.len = p - ct->subtype.begin;
  35. }
  36. qstart = NULL;
  37. qend = NULL;
  38. }
  39. action Param_Name_Start {
  40. printf("name start: %s\n", p);
  41. qstart = NULL;
  42. qend = NULL;
  43. pname_start = p;
  44. pname_end = NULL;
  45. }
  46. action Param_Name_End {
  47. printf("name end: %s\n", p);
  48. if (qstart) {
  49. pname_start = qstart;
  50. }
  51. if (qstart && qend && qend >= qstart) {
  52. pname_end = qend;
  53. }
  54. else if (p >= pname_start) {
  55. pname_end = p;
  56. }
  57. if (qstart && qend) {
  58. qstart = NULL;
  59. qend = NULL;
  60. }
  61. }
  62. action Param_Value_Start {
  63. printf("value start: %s\n", p);
  64. if (qend) {
  65. qstart = NULL;
  66. qend = NULL;
  67. }
  68. if (pname_end && !pvalue_start) {
  69. pvalue_start = p;
  70. pvalue_end = NULL;
  71. }
  72. }
  73. action Param_Value_End {
  74. printf("value end: %s\n", p);
  75. if (pname_end && pname_start) {
  76. if (qstart) {
  77. pvalue_start = qstart;
  78. if (!qend) {
  79. pvalue_end = NULL;
  80. }
  81. }
  82. if (qend && qend >= qstart) {
  83. if (qstart) {
  84. pvalue_end = qend;
  85. }
  86. else {
  87. pvalue_end = NULL;
  88. }
  89. }
  90. else if (!qstart && p >= pvalue_start) {
  91. pvalue_end = p;
  92. }
  93. if (pname_start && pvalue_start && pvalue_end && pvalue_end > pvalue_start
  94. && pname_end > pname_start) {
  95. rspamd_content_type_add_param (pool, ct, pname_start, pname_end,
  96. pvalue_start, pvalue_end);
  97. pname_start = NULL;
  98. pname_end = NULL;
  99. pvalue_start = NULL;
  100. pvalue_end = NULL;
  101. qend = NULL;
  102. qstart = NULL;
  103. }
  104. }
  105. }
  106. action Quoted_Str_Start {
  107. qstart = p;
  108. qend = NULL;
  109. }
  110. action Quoted_Str_End {
  111. if (qstart) {
  112. qend = p;
  113. }
  114. }
  115. include content_type "content_type.rl";
  116. main := content_type;
  117. }%%
  118. #include "smtp_parsers.h"
  119. #include "content_type.h"
  120. %% write data;
  121. gboolean
  122. rspamd_content_type_parser (const char *data, size_t len, struct rspamd_content_type *ct, rspamd_mempool_t *pool)
  123. {
  124. const char *p = data, *pe = data + len, *eof, *qstart = NULL, *qend = NULL,
  125. *pname_start = NULL, *pname_end = NULL, *pvalue_start = NULL, *pvalue_end = NULL;
  126. int cs, *stack = NULL;
  127. gsize top = 0;
  128. struct _ragel_st_storage {
  129. int *data;
  130. gsize size;
  131. } st_storage;
  132. memset (&st_storage, 0, sizeof (st_storage));
  133. memset (ct, 0, sizeof (*ct));
  134. eof = pe;
  135. %% write init;
  136. %% write exec;
  137. if (st_storage.data) {
  138. free (st_storage.data);
  139. }
  140. return ct->type.len > 0;
  141. }