Update stdlib and concat to 6.1.0 both
[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     @summary
5       Base64 encode or decode a string based on the command and the string submitted
6
7     @example Example usage
8
9       Encode and decode a string
10
11         $encodestring = base64('encode', 'thestring')
12         $decodestring = base64('decode', 'dGhlc3RyaW5n')
13
14       Explicitly define encode/decode method: default, strict, urlsafe
15
16         $method = 'default'
17         $encodestring = base64('encode', 'thestring', $method)
18         $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
19
20       Encode a string as if it was binary
21
22        $encodestring = String(Binary('thestring', '%s'))
23
24       Decode a Binary assuming it is an UTF-8 String
25
26        $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
27
28     > **Note:*
29         Since Puppet 4.8.0, the Binary data type can be used to produce base 64 encoded strings.
30         See the `new()` function for the Binary and String types for documentation. Also see `binary_file()`
31         function for reading a file with binary (non UTF-8) content.
32
33     @return [String] The encoded/decoded value
34     DOC
35
36     require 'base64'
37
38     raise Puppet::ParseError, "base64(): Wrong number of arguments (#{args.length}; must be >= 2)" unless args.length >= 2
39
40     actions = ['encode', 'decode']
41
42     unless actions.include?(args[0])
43       raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'"
44     end
45
46     unless args[1].is_a?(String)
47       raise Puppet::ParseError, 'base64(): the second argument must be a string to base64'
48     end
49
50     method = ['default', 'strict', 'urlsafe']
51
52     chosen_method = if args.length <= 2
53                       'default'
54                     else
55                       args[2]
56                     end
57
58     unless method.include?(chosen_method)
59       raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'"
60     end
61
62     case args[0]
63     when 'encode'
64       case chosen_method
65       when 'default'
66         result = Base64.encode64(args[1])
67       when 'strict'
68         result = Base64.strict_encode64(args[1])
69       when 'urlsafe'
70         result = Base64.urlsafe_encode64(args[1])
71       end
72     when 'decode'
73       case chosen_method
74       when 'default'
75         result = Base64.decode64(args[1])
76       when 'strict'
77         result = Base64.strict_decode64(args[1])
78       when 'urlsafe'
79         result = Base64.urlsafe_decode64(args[1])
80       end
81     end
82
83     return result
84   end
85 end