leon 1.0.3 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/io.rb +17 -16
- data/lib/leon.rb +156 -0
- metadata +1 -1
data/lib/io.rb
CHANGED
@@ -19,12 +19,9 @@ module LEON
|
|
19
19
|
end
|
20
20
|
def readString()
|
21
21
|
ret = ''
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
break
|
26
|
-
end
|
27
|
-
ret += char.chr
|
22
|
+
len = @buffer.readValue(@buffer.readUInt8())
|
23
|
+
for i in 0...len
|
24
|
+
ret += @buffer.readUInt8.chr
|
28
25
|
end
|
29
26
|
return ret
|
30
27
|
end
|
@@ -104,7 +101,7 @@ module LEON
|
|
104
101
|
return ret
|
105
102
|
elsif spec.is_a?(Hash)
|
106
103
|
ret = Hash.new
|
107
|
-
spec.each do |k, v|
|
104
|
+
Hash[spec.sort].each do |k, v|
|
108
105
|
ret[k] = parseValueWithSpec(v)
|
109
106
|
end
|
110
107
|
return ret
|
@@ -276,9 +273,15 @@ module LEON
|
|
276
273
|
writeValueWithSpec(v, spec[0])
|
277
274
|
}
|
278
275
|
elsif spec.kind_of? Hash
|
279
|
-
|
280
|
-
|
281
|
-
|
276
|
+
if val.kind_of? Hash
|
277
|
+
spec.sort.each { |k, v|
|
278
|
+
writeValueWithSpec(val[k], v)
|
279
|
+
}
|
280
|
+
else
|
281
|
+
spec.sort.each { |k, v|
|
282
|
+
writeValueWithSpec(val.send(k), v)
|
283
|
+
}
|
284
|
+
end
|
282
285
|
elsif spec === Constants::BOOLEAN
|
283
286
|
writeValue(val, LEON::type_check(val), true)
|
284
287
|
else
|
@@ -409,14 +412,12 @@ module LEON
|
|
409
412
|
end
|
410
413
|
end
|
411
414
|
def writeString(str)
|
412
|
-
bytes = StringBuffer.new
|
413
415
|
len = str.length
|
414
|
-
|
415
|
-
|
416
|
+
writeValue(len, LEON::type_check(len))
|
417
|
+
for i in 0...len
|
418
|
+
writeValue(str[i].ord, Constants::UNSIGNED_CHAR, true)
|
416
419
|
end
|
417
|
-
|
418
|
-
append(bytes)
|
419
|
-
return len + 1
|
420
|
+
return len + 2
|
420
421
|
end
|
421
422
|
def writeOLI()
|
422
423
|
if @stringIndex.length === 0
|
data/lib/leon.rb
CHANGED
@@ -20,6 +20,162 @@ module LEON
|
|
20
20
|
def self.generate(payload)
|
21
21
|
Encoder.new(payload).writeSI().writeOLI().writeData().export()
|
22
22
|
end
|
23
|
+
def self.type_to_str(type)
|
24
|
+
case type
|
25
|
+
when Constants::UNSIGNED_CHAR
|
26
|
+
return "unsigned char"
|
27
|
+
when Constants::CHAR
|
28
|
+
return "char"
|
29
|
+
when Constants::UNSIGNED_SHORT
|
30
|
+
return "unsigned short"
|
31
|
+
when Constants::SHORT
|
32
|
+
return "short"
|
33
|
+
when Constants::UNSIGNED_INT
|
34
|
+
return "unsigned int"
|
35
|
+
when Constants::INT
|
36
|
+
return "int"
|
37
|
+
when Constants::FLOAT
|
38
|
+
return "float"
|
39
|
+
when Constants::DOUBLE
|
40
|
+
return "double"
|
41
|
+
when Constants::STRING
|
42
|
+
return "string"
|
43
|
+
when Constants::BOOLEAN
|
44
|
+
return "boolean"
|
45
|
+
when Constants::NULL
|
46
|
+
return "null"
|
47
|
+
when Constants::UNDEFINED
|
48
|
+
return "undefined"
|
49
|
+
when Constants::OBJECT, Constants::NATIVE_OBJECT
|
50
|
+
return "object"
|
51
|
+
when Constants::ARRAY
|
52
|
+
return "array"
|
53
|
+
when Constants::DATE
|
54
|
+
return "date"
|
55
|
+
when Constants::BUFFER
|
56
|
+
return "buffer"
|
57
|
+
when Constants::REGEXP
|
58
|
+
return "RegExp"
|
59
|
+
when Constants::NAN
|
60
|
+
return "NaN"
|
61
|
+
when Constants::INFINITY
|
62
|
+
return "infinity"
|
63
|
+
when Constants::MINUS_INFINITY
|
64
|
+
return "minus infinity"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
def self.type_gcd(arr)
|
68
|
+
type = LEON::type_check(arr[0])
|
69
|
+
if type === Constants::BOOLEAN + 1
|
70
|
+
type = Constants::BOOLEAN
|
71
|
+
end
|
72
|
+
case type
|
73
|
+
when Constants::ARRAY
|
74
|
+
return [ self.type_gcd(arr[0]) ]
|
75
|
+
when Constants::OBJECT
|
76
|
+
ret = Hash.new
|
77
|
+
arr[0].each { |k, v|
|
78
|
+
ret[k] = self.type_gcd(self.pluck(arr, k))
|
79
|
+
}
|
80
|
+
return ret
|
81
|
+
when Constants::NATIVE_OBJECT
|
82
|
+
ret = Hash.new
|
83
|
+
vars = arr[0].instance_variables.map { |v|
|
84
|
+
v.to_sym.sub("@", "")
|
85
|
+
}
|
86
|
+
vars.each { |v|
|
87
|
+
ret[v] = self.type_gcd(self.pluck(arr, v))
|
88
|
+
}
|
89
|
+
return ret
|
90
|
+
when Constants::UNSIGNED_CHAR, Constants::CHAR, Constants::UNSIGNED_SHORT, Constants::SHORT, Constants::UNSIGNED_INT, Constants::INT, Constants::FLOAT, Constants::DOUBLE
|
91
|
+
highestMagnitude = arr[0].abs
|
92
|
+
if arr[0] < 0
|
93
|
+
sign = 1
|
94
|
+
else
|
95
|
+
sign = 0
|
96
|
+
end
|
97
|
+
if type === Constants::FLOAT or type === Constants::DOUBLE
|
98
|
+
fp = 1
|
99
|
+
else
|
100
|
+
fp = 0
|
101
|
+
end
|
102
|
+
for i in 1...arr.length
|
103
|
+
type = LEON::type_check(arr[i])
|
104
|
+
if not arr[i].kind_of? Float and not arr[i].kind_of? Fixnum
|
105
|
+
raise "Expected a numerical value but got #{self.type_to_str(type)}."
|
106
|
+
end
|
107
|
+
if arr[i].abs > highestMagnitude
|
108
|
+
highestMagnitude = arr[1]
|
109
|
+
end
|
110
|
+
if arr[i].ceil != arr[i]
|
111
|
+
fp = 1
|
112
|
+
else
|
113
|
+
fp = 0
|
114
|
+
end
|
115
|
+
end
|
116
|
+
return self.type_check((fp ? Float((sign ? -highestMagnitude : highestMagnitude )) : (sign ? -highestMagnitude : highestMagnitude )))
|
117
|
+
else
|
118
|
+
for i in 1...(arr.length)
|
119
|
+
thisType = self.type_check(arr[i])
|
120
|
+
if thisType === Constants::BOOLEAN + 1
|
121
|
+
thisType = Constants::BOOLEAN
|
122
|
+
end
|
123
|
+
if type != thisType
|
124
|
+
raise "Was expecting a #{self.type_to_str(type)} but got a #{self.type_to_str(thisType)}."
|
125
|
+
end
|
126
|
+
end
|
127
|
+
return type
|
128
|
+
end
|
129
|
+
return type
|
130
|
+
end
|
131
|
+
def self.pluck(arr, prop)
|
132
|
+
ret = Array.new
|
133
|
+
if prop.kind_of? Symbol
|
134
|
+
prop = prop.to_s
|
135
|
+
end
|
136
|
+
for i in 0...(arr.length)
|
137
|
+
if arr[i].kind_of? Hash
|
138
|
+
if not arr[i].has_key? prop
|
139
|
+
if not arr[i].has_key? prop.to_sym
|
140
|
+
raise "Object #{i} in array has no such key \"#{prop}.\""
|
141
|
+
else
|
142
|
+
ret.push arr[i][prop.to_sym]
|
143
|
+
end
|
144
|
+
else
|
145
|
+
ret.push arr[i][prop]
|
146
|
+
end
|
147
|
+
elsif arr[i].kind_of? Object
|
148
|
+
begin
|
149
|
+
ret.push arr[i].send(prop)
|
150
|
+
rescue
|
151
|
+
raise "Object #{i} in array has no such property \"#{prop}.\""
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
return ret
|
156
|
+
end
|
157
|
+
def self.to_template(payload)
|
158
|
+
type = self.type_check(payload)
|
159
|
+
case type
|
160
|
+
when Constants::ARRAY
|
161
|
+
return [self.type_gcd(payload)]
|
162
|
+
when Constants::OBJECT
|
163
|
+
ret = Hash.new
|
164
|
+
payload.each { |k, v|
|
165
|
+
ret[k] = self.to_template(v)
|
166
|
+
}
|
167
|
+
return ret
|
168
|
+
when Constants::NATIVE_OBJECT
|
169
|
+
ret = Hash.new
|
170
|
+
payload.instance_variables.each { |k, v|
|
171
|
+
ret[k] = self.to_template(v)
|
172
|
+
}
|
173
|
+
when Constants::BOOLEAN + 1
|
174
|
+
return Constants::BOOLEAN
|
175
|
+
else
|
176
|
+
return type
|
177
|
+
end
|
178
|
+
end
|
23
179
|
end
|
24
180
|
class Object
|
25
181
|
def to_leon()
|