4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/cbor-diagnostic-app/b64.rb', line 4
def self.decode(_, s)
parser = B64GRAMMARParser.new ast = parser.parse(s)
if !ast
raise ArgumentError, "cbor-diagnostic: Parse Error in b64'#{s}':\n" << EDN.reason(parser, s)
end
t = s.gsub(/\s|#[^\n]*(?:\n|\z)/, '').chars.each_slice(4).map(&:join)
if last = t[-1]
last << "=" * (4 - last.size)
end
b = t.join.tr("-_", "+/")
begin
b.unpack("m0")[0]
rescue ArgumentError
raise ArgumentError, "cbor-diagnostic: invalid base64 #{b.inspect}", caller[1..-1]
end
end
|