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|
5 Base64 encode or decode a string based on the command and the string submitted
9 Encode and decode a string
11 $encodestring = base64('encode', 'thestring')
12 $decodestring = base64('decode', 'dGhlc3RyaW5n')
14 Explicitly define encode/decode method: default, strict, urlsafe
17 $encodestring = base64('encode', 'thestring', $method)
18 $decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
20 Encode a string as if it was binary
22 $encodestring = String(Binary('thestring', '%s'))
24 Decode a Binary assuming it is an UTF-8 String
26 $decodestring = String(Binary("dGhlc3RyaW5n"), "%s")
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.
33 @return [String] The encoded/decoded value
38 raise Puppet::ParseError, "base64(): Wrong number of arguments (#{args.length}; must be >= 2)" unless args.length >= 2
40 actions = ['encode', 'decode']
42 unless actions.include?(args[0])
43 raise Puppet::ParseError, "base64(): the first argument must be one of 'encode' or 'decode'"
46 unless args[1].is_a?(String)
47 raise Puppet::ParseError, 'base64(): the second argument must be a string to base64'
50 method = ['default', 'strict', 'urlsafe']
52 chosen_method = if args.length <= 2
58 unless method.include?(chosen_method)
59 raise Puppet::ParseError, "base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'"
66 result = Base64.encode64(args[1])
68 result = Base64.strict_encode64(args[1])
70 result = Base64.urlsafe_encode64(args[1])
75 result = Base64.decode64(args[1])
77 result = Base64.strict_decode64(args[1])
79 result = Base64.urlsafe_decode64(args[1])