File: content_transfer_encoding_parser.rl

package info (click to toggle)
ruby-mail 2.8.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,704 kB
  • sloc: ruby: 73,709; makefile: 3
file content (71 lines) | stat: -rw-r--r-- 1,695 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
require 'mail/utilities'
require 'mail/parser_tools'

begin
  original_verbose, $VERBOSE = $VERBOSE, nil

%%{
  machine content_transfer_encoding;
  alphtype int;

  action encoding_s { encoding_s = p }
  action encoding_e { content_transfer_encoding.encoding = chars(data, encoding_s, p-1).downcase }

  # No-op actions
  action comment_e { }
  action comment_s { }
  action phrase_e { }
  action phrase_s { }
  action qstr_e { }
  action qstr_s { }
  action param_attr_e { }
  action param_attr_s { }
  action param_val_e { }
  action param_val_s { }
  action main_type_e { }
  action main_type_s { }
  action sub_type_e { }
  action sub_type_s { }

  include rfc2045_content_transfer_encoding "rfc2045_content_transfer_encoding.rl";
  main := content_transfer_encoding;
}%%

module Mail::Parsers
  module ContentTransferEncodingParser
    extend Mail::ParserTools

    ContentTransferEncodingStruct = Struct.new(:encoding, :error)

    %%write data noprefix;

    def self.parse(data)
      data = data.dup.force_encoding(Encoding::ASCII_8BIT) if data.respond_to?(:force_encoding)

      content_transfer_encoding = ContentTransferEncodingStruct.new('')
      return content_transfer_encoding if Mail::Utilities.blank?(data)

      # Parser state
      encoding_s = nil

      # 5.1 Variables Used by Ragel
      p = 0
      eof = pe = data.length
      stack = []

      %%write init;
      %%write exec;

      if p != eof || cs < %%{ write first_final; }%%
        raise Mail::Field::IncompleteParseError.new(Mail::ContentTransferEncodingElement, data, p)
      end

      content_transfer_encoding
    end
  end
end

ensure
  $VERBOSE = original_verbose
end