Class: CBOR_DIAG::App_b64

Inherits:
Object
  • Object
show all
Defined in:
lib/cbor-diagnostic-app/b64.rb

Class Method Summary collapse

Class Method Details

.decode(_, s) ⇒ Object



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 #  B? XXX
  ast = parser.parse(s)
  if !ast
    raise ArgumentError, "cbor-diagnostic: Parse Error in b64'#{s}':\n" << EDN.reason(parser, s)
  end
  # lazy -- not using parse tree...:
  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