Module: Secure::Marshal
- Defined in:
- lib/appswarm/secure_marshal.rb,
lib/appswarm/secure_marshal.rb
Defined Under Namespace
Classes: OutOfData
Class Method Summary collapse
-
.dump(o) ⇒ Object
this function work pretty much like ruby standard marshaling.
-
.load(o) ⇒ Object
load takes either a string or an object of type Secure::Data.
Class Method Details
.dump(o) ⇒ Object
this function work pretty much like ruby standard marshaling. But it supports only some small set of types, being some basic-types like symbols, numbers, strings, hashes, arrays and structs. This is good, because you won’t be able to pass custom-objects through the network and thus causing security vulnerabilities. You can however create structs with Struct.new(…) and add functions to this struct. The encoding is PHP-serialization-alike, because it’s relatively compact but still is halfway human-readable. It may become necessary to create a shorter syntax in the future.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/appswarm/secure_marshal.rb', line 48 def self.dump(o) case o when String "s:"+dump(o.length)+o when Integer "i:"+o.to_s+":" when Float "f:"+o.to_s+":" when Array "a:"+dump(o.length)+o.map{|elem|dump(elem)}.join("") when NilClass "b:n" # nil when TrueClass "b:t" # true when FalseClass "b:f" when Hash "h:"+dump(o.to_a) when Symbol "y:"+dump(o.to_s) else if o.respond_to?(:members) and o.respond_to?(:values) n=o.class.to_s hash={} o.members.each{|m| hash[m]=o.send(m) } "o:"+dump(n)+dump(hash) else raise "Unknown Type to marshal #{o}" end end end |
.load(o) ⇒ Object
load takes either a string or an object of type Secure::Data. It tries to load an object and return it. If no parsable object is found, an OutOfData exception will be raised.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/appswarm/secure_marshal.rb', line 84 def self.load(o) unless o.is_a?(Data) return load(Data.new(o)) end orig=o.str bt=o.dup result=case o.read(2)[0..0] when "b" case o.read(1) when "n" nil when "t" true when "f" false end when "s" len=load(o) r=o.read(len) r when "i" o.til(":").to_i when "f" o.til(":").to_f when "a" len=load(o) assert{len.is_a?(Numeric)} (0...len).to_a.map{|i| load(o) } when "h" a=load(o) h={} a.each{|p| h[p[0]]=p[1] } h when "y" load(o).to_sym when "o" name=load(o) hash=load(o) klass=getClass(name) mo=klass.new hash.each{|k,v| mo.send(k+"=",v) if mo.members.member?(k) and mo.respond_to?(k+"=") } mo end #pp "LOADING: #{orig.inspect} now:#{o.str.inspect} RES:#{result.inspect}" result end |