rcstorable 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/ext/rcstorable.c +25 -7
- data/spec/rcstorable_spec.rb +21 -3
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/ext/rcstorable.c
CHANGED
@@ -38,15 +38,26 @@ enum hash_key_types
|
|
38
38
|
|
39
39
|
// Used globally. Raptors. I know.
|
40
40
|
unsigned char *serialized;
|
41
|
+
unsigned char *serialized_end;
|
42
|
+
|
43
|
+
void
|
44
|
+
check_pointer(unsigned char *ptr)
|
45
|
+
{
|
46
|
+
extern unsigned char *serialized_end;
|
47
|
+
if (ptr > serialized_end) {
|
48
|
+
rb_raise(rb_eRangeError, "malformed data");
|
49
|
+
}
|
50
|
+
}
|
41
51
|
|
42
52
|
VALUE
|
43
53
|
thaw(VALUE self, VALUE str)
|
44
54
|
{
|
45
55
|
Check_Type(str, T_STRING);
|
46
|
-
extern unsigned char *serialized;
|
56
|
+
extern unsigned char *serialized, *serialized_end;
|
47
57
|
|
48
58
|
serialized = RSTRING_PTR(str);
|
49
|
-
|
59
|
+
serialized_end = serialized + RSTRING_LEN(str);
|
60
|
+
|
50
61
|
read_magic_numbers();
|
51
62
|
|
52
63
|
return read_object();
|
@@ -63,6 +74,7 @@ VALUE
|
|
63
74
|
read_object()
|
64
75
|
{
|
65
76
|
extern unsigned char *serialized;
|
77
|
+
check_pointer(serialized);
|
66
78
|
int type = *serialized++;
|
67
79
|
int size = read_extended_size();
|
68
80
|
|
@@ -90,7 +102,8 @@ void
|
|
90
102
|
read_n_hash_pairs(VALUE hash, int num)
|
91
103
|
{
|
92
104
|
extern unsigned char *serialized;
|
93
|
-
|
105
|
+
check_pointer(serialized);
|
106
|
+
|
94
107
|
if (num == 0) {
|
95
108
|
return;
|
96
109
|
}
|
@@ -122,7 +135,8 @@ void
|
|
122
135
|
read_n_array_entries(VALUE array, int num)
|
123
136
|
{
|
124
137
|
extern unsigned char *serialized;
|
125
|
-
|
138
|
+
check_pointer(serialized);
|
139
|
+
|
126
140
|
if (num == 0) {
|
127
141
|
return;
|
128
142
|
}
|
@@ -146,7 +160,8 @@ VALUE
|
|
146
160
|
read_string(bool extended_size)
|
147
161
|
{
|
148
162
|
extern unsigned char *serialized;
|
149
|
-
|
163
|
+
check_pointer(serialized);
|
164
|
+
|
150
165
|
int size = extended_size ? read_extended_size() : read_compact_size();
|
151
166
|
int actual_size = 0;
|
152
167
|
int rem;
|
@@ -154,17 +169,18 @@ read_string(bool extended_size)
|
|
154
169
|
|
155
170
|
if (size == 319) { // apparently Storable uses \000\000\001\077 to mean "read until n<10"
|
156
171
|
while (*tp++ >= 10) {
|
172
|
+
check_pointer(tp);
|
157
173
|
actual_size++;
|
158
174
|
}
|
159
175
|
size = actual_size;
|
160
176
|
}
|
161
|
-
|
162
177
|
rem = size;
|
163
178
|
|
164
179
|
char *np = malloc(size * sizeof(char) + 1);
|
165
180
|
char *cnp = np;
|
166
181
|
|
167
182
|
while (rem > 0) {
|
183
|
+
check_pointer(serialized);
|
168
184
|
rem--;
|
169
185
|
*cnp++ = *serialized++;
|
170
186
|
}
|
@@ -182,7 +198,8 @@ read_extended_size()
|
|
182
198
|
{
|
183
199
|
extern unsigned char *serialized;
|
184
200
|
int size = 0;
|
185
|
-
|
201
|
+
check_pointer(serialized);
|
202
|
+
|
186
203
|
serialized++;
|
187
204
|
serialized++;
|
188
205
|
size += 256*(*serialized++);
|
@@ -196,6 +213,7 @@ read_extended_size()
|
|
196
213
|
*/
|
197
214
|
int
|
198
215
|
read_compact_size() {
|
216
|
+
check_pointer(serialized);
|
199
217
|
extern unsigned char *serialized;
|
200
218
|
return *serialized++;
|
201
219
|
}
|
data/spec/rcstorable_spec.rb
CHANGED
@@ -2,11 +2,29 @@ require 'rcstorable'
|
|
2
2
|
|
3
3
|
describe RCStorable do
|
4
4
|
|
5
|
-
it "should correctly decode output" do
|
6
|
-
input = "\005\006#{File.read(File.join(File.dirname(__FILE__),'test.store'))}"
|
7
5
|
|
8
|
-
|
6
|
+
it "should correctly decode format A" do
|
7
|
+
store = "\005\006\003\000\000\000\005\005\000\000\000\aAaaaaaa\004\003\000\000\000\017\n\fasdjfsadfjfd\000\000\000\tdkjfjaksj\n\00560565\000\000\000\nksdjfksjdf\n\020aksjgjeijpqiwjff\000\000\000\020ksdjfkasjfkjefjj\n\022ewjfklqjkzxjfnvnds\000\000\000\ajfjwjwj\n\nlsakdvnnff\000\000\000\004qpwo\n\002jf\000\000\000\005qwojf\n\bzxemffew\000\000\000\005owjff\n\004zjxf\000\000\000\016jzxmfnfkneoqjf\n\003jas\000\000\000\aowfjjwf\n\003jfw\000\000\000\rjadsnzxfkjfej\n\005oqwfj\000\000\000\bqpwofjff\n\004owjf\000\000\000\njzxnfnqwwf\n\000\000\000\000\nqpwofjfjww\n\000\000\000\000\vpdofjfejqwj\n\002jf\000\000\000\vpwqofjwjqfj\000\000\000\apojsfww\005\000\000\000\005posjf\004\002\000\000\000\001\001\000\000\001?sdlkfjewigjepofjqwpfjsdalkfjqepogjgpojqwfpowejfqpowfjlksdjdslkgjsdflgjerhgi;oerhjgoiprejwgioperjgkl;djglsdk;fgjsdlkf;gjal;gfjerigjergkjarigojaegkl;sdfnga;lksfjwi;eogjieryjkw;ljfa;sdlgkhjaglkajwegf;lkajgaweklfhajklghawejklghawejkhawelurhaewtluiawehtawlkjehfasjkldfnasjklfhsadfjklashfluiewhwlquthqluitqwyeoiuthwefjklasdhfkjlasdfnbasjlkdfhqweuiltqytoeqwieutyqoeto9845yhsjdaghfajkewntewakjlhtawkerhweuirhaweruhewiurahwefljkshdafjhadkf\n\000\000\000\003oje\005\000\000\000\016lsdkfjlkfjqwtr"
|
8
|
+
|
9
|
+
expected = {"Aaaaaaa"=>nil, "lsdkfjlkfjqwtr"=>nil, "posjf"=>nil, "oje"=>["sdlkfjewigjepofjqwpfjsdalkfjqepogjgpojqwfpowejfqpowfjlksdjdslkgjsdflgjerhgi;oerhjgoiprejwgioperjgkl;djglsdk;fgjsdlkf;gjal;gfjerigjergkjarigojaegkl;sdfnga;lksfjwi;eogjieryjkw;ljfa;sdlgkhjaglkajwegf;lkajgaweklfhajklghawejklghawejkhawelurhaewtluiawehtawlkjehfasjkldfnasjklfhsadfjklashfluiewhwlquthqluitqwyeoiuthwefjklasdhfkjlasdfnbasjlkdfhqweuiltqytoeqwieutyqoeto9845yhsjdaghfajkewntewakjlhtawkerhweuirhaweruhewiurahwefljkshdafjhadkf\n"], "pojsfww"=>{"jfjwjwj"=>"ewjfklqjkzxjfnvnds", "qpwo"=>"lsakdvnnff", "ksdjfksjdf"=>"60565", "pdofjfejqwj"=>"", "owfjjwf"=>"jas", "owjff"=>"zxemffew", "jzxnfnqwwf"=>"owjf", "qpwofjff"=>"oqwfj", "jadsnzxfkjfej"=>"jfw", "ksdjfkasjfkjefjj"=>"aksjgjeijpqiwjff", "pwqofjwjqfj"=>"jf", "dkjfjaksj"=>"asdjfsadfjfd", "qpwofjfjww"=>"", "jzxmfnfkneoqjf"=>"zjxf", "qwojf"=>"jf"}}
|
10
|
+
|
11
|
+
RCStorable.thaw(store).should == expected
|
12
|
+
end
|
9
13
|
|
14
|
+
it "should correctly decode format B" do
|
15
|
+
store = "004\a\b12345678\004\b\b\b\003\004\000\000\000\004\003\002\000\000\000\n\004blah\001\000\000\000p\001>\002\000\000Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\001\000\000\000q\001\000\000\000c\n\00229\017\000\000\000longer_key_name\n\020A longish string\001\000\000\000a\n\00235\001\000\000\000b"
|
16
|
+
|
17
|
+
expected = {
|
18
|
+
"a" => "A longish string",
|
19
|
+
"b" => "35",
|
20
|
+
"c" => {
|
21
|
+
"p" => "blah",
|
22
|
+
"q" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
|
23
|
+
},
|
24
|
+
"longer_key_name" => "29"
|
25
|
+
}
|
26
|
+
|
27
|
+
RCStorable.thaw(store).should == expected
|
10
28
|
end
|
11
29
|
|
12
30
|
|