Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / base64.rb
1 #  Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
2 module Puppet::Parser::Functions
3   newfunction(:base64, :type => :rvalue, :doc => <<-'DOC') do |args|
4     Base64 encode or decode a string based on the command and the string submitted
5
6     Usage:
7
8       $encodestring = base64('encode', 'thestring')
9       $decodestring = base64('decode', 'dGhlc3RyaW5n')
10
11       # explicitly define encode/decode method: default, strict, urlsafe
12       $method = 'default'
13       $encodestring = base64('encode', 'thestring', $method)
14       $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
15
16     Note: Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
17     See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
18     function for reading a file with binary (non UTF-8) content.
19
20        # encode a string as if it was binary
21        $encodestring = String(Binary('thestring', '%s'))
22        # decode a Binary assuming it is an UTF-8 String
23        $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
24
25     DOC
26
27     require 'base64'
28
29     raise Puppet::ParseError, "base64(): Wrong number of arguments (#{args.length}; must be >= 2)" unless args.length >= 2
30
31     actions = ['encode', 'decode']
32
33     unless actions.include?(args[0])
34       raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'"
35     end
36
37     unless args[1].is_a?(String)
38       raise Puppet::ParseError, 'base64(): the second argument must be a string to base64'
39     end
40
41     method = ['default', 'strict', 'urlsafe']
42
43     chosen_method = if args.length <= 2
44                       'default'
45                     else
46                       args[2]
47                     end
48
49     unless method.include?(chosen_method)
50       raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'"
51     end
52
53     case args[0]
54     when 'encode'
55       case chosen_method
56       when 'default'
57         result = Base64.encode64(args[1])
58       when 'strict'
59         result = Base64.strict_encode64(args[1])
60       when 'urlsafe'
61         result = Base64.urlsafe_encode64(args[1])
62       end
63     when 'decode'
64       case chosen_method
65       when 'default'
66         result = Base64.decode64(args[1])
67       when 'strict'
68         result = Base64.strict_decode64(args[1])
69       when 'urlsafe'
70         result = Base64.urlsafe_decode64(args[1])
71       end
72     end
73
74     return result
75   end
76 end