Sha256: a062a4cde313c30afaa3058bb0c43eee4d98db8da3d81d2872f49a436c1d742f

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

# encoding: utf-8
require 'base64'

module Dragonfly
  module Serializer
    
    # Exceptions
    class BadString < RuntimeError; end
    class MaliciousString < RuntimeError; end
    
    extend self # So we can do Serializer.b64_encode, etc.
    
    def b64_encode(string)
      Base64.encode64(string).tr("\n=",'')
    end
    
    def b64_decode(string)
      padding_length = string.length % 4
      Base64.decode64(string + '=' * padding_length)
    end
    
    def marshal_encode(object)
      b64_encode(Marshal.dump(object))
    end
    
    def marshal_decode(string)
      marshal_string = b64_decode(string)
      raise MaliciousString, "potentially malicious marshal string #{marshal_string.inspect}" if marshal_string[/@[a-z_]/i]
      Marshal.load(marshal_string)
    rescue TypeError, ArgumentError => e
      raise BadString, "couldn't decode #{string} - got #{e}"
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dragonfly-0.8.6 lib/dragonfly/serializer.rb