File: base64.rb

package info (click to toggle)
ruby-mail 2.7.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,732 kB
  • sloc: ruby: 71,628; makefile: 3
file content (38 lines) | stat: -rw-r--r-- 828 bytes parent folder | download | duplicates (3)
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
# encoding: utf-8
# frozen_string_literal: true
require 'mail/encodings/7bit'

module Mail
  module Encodings
    # Base64 encoding handles binary content at the cost of 4 output bytes
    # per input byte.
    class Base64 < SevenBit
      NAME = 'base64'
      PRIORITY = 3
      Encodings.register(NAME, self)

      def self.can_encode?(enc)
        true
      end

      def self.decode(str)
        RubyVer.decode_base64(str)
      end

      def self.encode(str)
        ::Mail::Utilities.binary_unsafe_to_crlf(RubyVer.encode_base64(str))
      end

      # 3 bytes in -> 4 bytes out
      def self.cost(str)
        4.0 / 3
      end

      # Ruby Base64 inserts newlines automatically, so it doesn't exceed
      # SMTP line length limits.
      def self.compatible_input?(str)
        true
      end
    end
  end
end