sereal 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 740e7a99360590057c85f528f9b73fd17415283d
4
- data.tar.gz: 338ad8c8127e570f13a64ff876d59a547408110c
3
+ metadata.gz: 658547490d95c374a8fad16740ca8bdb463394cf
4
+ data.tar.gz: ff7871ff2e838a756d0a62c7094302068355e89f
5
5
  SHA512:
6
- metadata.gz: b7c9123137dbc21efd1b803a5ae4d3ad6be6bffef51407f1279abe798ab0a2263ff79cb0d98434f081ed30efc1bd0ec95b6437598a1524ed3de733de17020557
7
- data.tar.gz: 88f9ae7c6244c38ba9f28996ed5eb1505a603f9d5c0c16ef2b37ac296b7e1d52d3c3a784c0cf35df7b6fe3dc15453296b7a627720cbebb22490f803b4dc6ca41
6
+ metadata.gz: af647c218413b546b956392a909e97dc54302a5c729ed2b0f45eeec8c2431b611c7403c343025c9913421a5ebcda477921d5452f080bd02b29c47f93438f4f2e
7
+ data.tar.gz: fff160c78207616f73f61380d78384b148053a89bf2b31c2519bfda961fb2dbb261772517f87f0b977cf7c2a17172b8a99fa27c94c42519fc141855b90294f7f
data/bin/rsrl CHANGED
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
- require 'sereal'
2
+ begin
3
+ require './lib/sereal'
4
+ rescue LoadError
5
+ require 'sereal'
6
+ end
3
7
 
4
8
  content = ARGF.read
5
9
  compress = Sereal::RAW
@@ -14,5 +18,5 @@ if content[0..3] == '=srl'
14
18
  STDOUT.write(x)
15
19
  end
16
20
  else
17
- STDOUT.write(Sereal.encode(content, compress))
21
+ STDOUT.write(Sereal.encode(eval(content), compress))
18
22
  end
data/ext/sereal/buffer.h CHANGED
@@ -16,6 +16,8 @@ static inline sereal_t * s_create(void) {
16
16
  }
17
17
 
18
18
  static inline void s_destroy(sereal_t *s) {
19
+ if (s->tracked != Qnil)
20
+ rb_gc_unregister_address(&s->tracked);
19
21
  if (s->data && (s->flags & FLAG_NOT_MINE) == 0)
20
22
  free(s->data);
21
23
 
@@ -53,9 +55,16 @@ static inline void *s_get_p_at_pos(sereal_t *s, u32 pos,u32 req) {
53
55
  return &s->data[pos];
54
56
  }
55
57
 
58
+ static inline void *s_get_p_at_pos_bang(sereal_t *s, u32 pos,u32 req) {
59
+ void *p = s_get_p_at_pos(s,pos,req);
60
+ s->pos += req;
61
+ return p;
62
+ }
63
+
56
64
  static inline void *s_get_p(sereal_t *s) {
57
65
  return s_get_p_at_pos(s,s->pos,0);
58
66
  }
67
+
59
68
  static inline u8 s_get_u8(sereal_t *s) {
60
69
  return *((u8 *) s_get_p(s));
61
70
  }
data/ext/sereal/decode.c CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  static VALUE s_default_reader(sereal_t *s, u8 tag) {
7
7
  // s_dump(s);
8
- s_raise(s,rb_eTypeError,"unsupported tag %d",tag);
8
+ s_raise(s,rb_eTypeError,"unsupported tag %d [ 0x%x ]",tag,tag);
9
9
  return Qnil;
10
10
  }
11
11
 
@@ -42,19 +42,19 @@ static VALUE s_read_varint(sereal_t *s, u8 tag) {
42
42
 
43
43
  /* FLOAT */
44
44
  static VALUE s_read_float(sereal_t *s, u8 tag) {
45
- float *f = (float *) s_get_p_at_pos(s,s->pos,sizeof(*f));
45
+ float *f = (float *) s_get_p_at_pos_bang(s,s->pos,sizeof(*f));
46
46
  return DBL2NUM((double) *f);
47
47
  }
48
48
 
49
49
  /* DOUBLE */
50
50
  static VALUE s_read_double(sereal_t *s, u8 tag) {
51
- double *d = (double *) s_get_p_at_pos(s,s->pos,sizeof(*d));
51
+ double *d = (double *) s_get_p_at_pos_bang(s,s->pos,sizeof(*d));
52
52
  return DBL2NUM(*d);
53
53
  }
54
54
 
55
55
  /* LONG DOUBLE */
56
56
  static VALUE s_read_long_double(sereal_t *s, u8 tag) {
57
- long double *d = (long double *) s_get_p_at_pos(s,s->pos,sizeof(*d));
57
+ long double *d = (long double *) s_get_p_at_pos_bang(s,s->pos,sizeof(*d));
58
58
  return DBL2NUM((double) *d);
59
59
  }
60
60
 
@@ -68,7 +68,7 @@ static VALUE s_read_small_negative_int(sereal_t *s, u8 tag) {
68
68
  return INT2FIX(tag - 32);
69
69
  }
70
70
 
71
- /* ARRAY */
71
+ /* ARRAY */
72
72
  static inline VALUE s_read_array_with_len(sereal_t *s, u32 len) {
73
73
  VALUE arr[len];
74
74
  register u32 i;
@@ -98,24 +98,28 @@ static inline VALUE s_read_hash_with_len(sereal_t *s, u32 len) {
98
98
  }
99
99
  return hash;
100
100
  }
101
+
101
102
  /* HASH */
102
103
  static VALUE s_read_hash(sereal_t *s, u8 tag) {
103
104
  return s_read_hash_with_len(s,s_get_varint_bang(s));
104
105
  }
106
+
105
107
  /* HASH */
106
108
  static VALUE s_read_hashref(sereal_t *s, u8 tag) {
107
109
  return s_read_hash_with_len(s,tag & SRL_MASK_HASHREF_COUNT);
108
110
  }
111
+
109
112
  static VALUE s_read_rb_string_bang(sereal_t *s,u8 t) {
110
113
  u32 len = 0;
111
114
  VALUE string;
112
- #define RETURN_STRING(fx_l,fx_gen) \
113
- do { \
114
- len = fx_l; \
115
- string = fx_gen; \
116
- s_shift_position_bang(s,len); \
117
- return string; \
115
+ #define RETURN_STRING(fx_l,fx_gen) \
116
+ do { \
117
+ len = fx_l; \
118
+ string = fx_gen; \
119
+ s_shift_position_bang(s,len); \
120
+ return string; \
118
121
  } while(0);
122
+
119
123
  if (t == SRL_HDR_STR_UTF8) {
120
124
  RETURN_STRING(s_get_varint_bang(s),
121
125
  rb_enc_str_new(s_get_p(s),len,
@@ -127,7 +131,7 @@ static VALUE s_read_rb_string_bang(sereal_t *s,u8 t) {
127
131
  RETURN_STRING((t & SRL_MASK_SHORT_BINARY_LEN),
128
132
  rb_str_new(s_get_p(s), len));
129
133
  }
130
- #undef RETURN_STRING
134
+ #undef RETURN_STRING
131
135
  s_raise(s,rb_eTypeError, "undefined string type %d",t);
132
136
  }
133
137
 
@@ -171,20 +175,46 @@ static VALUE s_read_pad(sereal_t *s, u8 tag) {
171
175
  /* just skip this byte and go forward */
172
176
  return sereal_to_rb_object(s);
173
177
  }
178
+
174
179
  static VALUE s_read_extend(sereal_t *s, u8 tag) {
175
180
  s_raise(s,rb_eArgError,"extend tags are not supported");
176
181
  }
177
182
 
183
+ static VALUE s_read_ref(sereal_t *s, u8 tag) {
184
+ u64 off = s_get_varint_bang(s);
185
+ if (s->tracked == Qnil)
186
+ s_raise(s,rb_eArgError,"there are no references stored");
187
+ return rb_hash_aref(s->tracked,INT2FIX(off + s->hdr_end));
188
+ }
189
+
190
+ static VALUE s_read_copy(sereal_t *s, u8 tag) {
191
+ VALUE ref = s_red_ref(s,tag);
192
+ return rb_obj_dup(ref);
193
+ }
194
+
178
195
  VALUE sereal_to_rb_object(sereal_t *s) {
179
- u8 t;
196
+ u8 t, tracked;
180
197
  S_RECURSE_INC(s);
198
+ u32 pos;
181
199
  while (s->pos < s->size) {
182
200
  t = s_get_u8_bang(s);
183
- if (t & SRL_HDR_TRACK_FLAG)
184
- s_raise(s,rb_eArgError, "trackable objects are not supported");
201
+ tracked = (t & SRL_HDR_TRACK_FLAG ? 1 : 0);
202
+ t &= ~SRL_HDR_TRACK_FLAG;
203
+ pos = s->pos;
204
+
185
205
  S_RECURSE_DEC(s);
186
- return (*READERS[t])(s,t);
206
+
207
+ VALUE decoded = (*READERS[t])(s,t);
208
+ if (tracked) {
209
+ if (s->tracked == Qnil) {
210
+ s->tracked = rb_hash_new();
211
+ rb_gc_register_address(&s->tracked);
212
+ }
213
+ rb_hash_aset(s->tracked,INT2FIX(pos),decoded);
214
+ }
215
+ return decoded;
187
216
  }
217
+ s_raise(s,rb_eArgError,"bad packet, or broken decoder");
188
218
  return Qnil;
189
219
  }
190
220
 
@@ -207,6 +237,7 @@ again:
207
237
  s->data = RSTRING_PTR(payload) + offset;
208
238
  s->size = RSTRING_LEN(payload) - offset;
209
239
  s->pos = 0;
240
+ s->tracked = Qnil;
210
241
  if (s->size < 6 || s_get_u32_bang(s) != SRL_MAGIC_STRING_LILIPUTIAN)
211
242
  s_raise(s,rb_eTypeError,"invalid header");
212
243
 
@@ -249,7 +280,7 @@ again:
249
280
  s->pos = 0;
250
281
  s->flags &= ~FLAG_NOT_MINE;
251
282
  }
252
-
283
+ s->hdr_end = s->pos;
253
284
  VALUE result = sereal_to_rb_object(s);
254
285
  if (is_compressed && have_block) {
255
286
  free(s->data);
data/ext/sereal/decode.h CHANGED
@@ -21,6 +21,8 @@ static VALUE s_read_true(sereal_t *s, u8 tag);
21
21
  static VALUE s_read_false(sereal_t *s, u8 tag);
22
22
  static VALUE s_read_pad(sereal_t *s, u8 tag);
23
23
  static VALUE s_read_extend(sereal_t *s, u8 tag);
24
+ static VALUE s_read_ref(sereal_t *s, u8 tag);
25
+ static VALUE s_read_copy(sereal_t *s, u8 tag);
24
26
 
25
27
  static VALUE (*READERS[256])(sereal_t *, u8) = {
26
28
  s_read_small_positive_int, // 0 SRL_HDR_POS_LOW
@@ -63,14 +65,14 @@ static VALUE (*READERS[256])(sereal_t *, u8) = {
63
65
  s_read_nil, // 37 SRL_HDR_UNDEF
64
66
  s_read_rb_string_bang, // 38 SRL_HDR_BINARY
65
67
  s_read_rb_string_bang, // 39 SRL_HDR_STR_UTF8
66
- s_default_reader, /* XXX */ // 40 SRL_HDR_REFN
67
- s_default_reader, /* XXX */ // 41 SRL_HDR_REFP
68
+ s_read_pad, // 40 SRL_HDR_REFN
69
+ s_read_ref, // 41 SRL_HDR_REFP
68
70
  s_read_hash, // 42 SRL_HDR_HASH
69
71
  s_read_array, // 43 SRL_HDR_ARRAY
70
72
  s_default_reader, /* XXX */ // 44 SRL_HDR_OBJECT
71
73
  s_default_reader, /* XXX */ // 45 SRL_HDR_OBJECTV
72
- s_default_reader, /* XXX */ // 46 SRL_HDR_ALIAS
73
- s_default_reader, /* XXX */ // 47 SRL_HDR_COPY
74
+ s_read_ref, // 46 SRL_HDR_ALIAS
75
+ s_read_copy, // 47 SRL_HDR_COPY
74
76
  s_default_reader, /* XXX */ // 48 SRL_HDR_WEAKEN
75
77
  s_read_regexp, // 49 SRL_HDR_REGEXP
76
78
  s_default_reader, /* XXX */ // 50 SRL_HDR_RESERVED_LOW
data/ext/sereal/sereal.h CHANGED
@@ -54,6 +54,8 @@ struct _sereal {
54
54
  u32 rsize;
55
55
  u32 level;
56
56
  u8 flags;
57
+ VALUE tracked;
58
+ u32 hdr_end;
57
59
  };
58
60
 
59
61
  VALUE method_sereal_encode(VALUE self, VALUE args);
@@ -62,9 +64,9 @@ VALUE method_sereal_decode(VALUE self, VALUE payload);
62
64
  #define S_RECURSE_INC(s) \
63
65
  do { \
64
66
  if((s)->level++ > MAX_RECURSION_DEPTH) \
65
- rb_raise(rb_eArgError, \
66
- "max recursion depth reached: %d", \
67
- MAX_RECURSION_DEPTH); \
67
+ s_raise((s),rb_eArgError, \
68
+ "max recursion depth reached: %d (level: %d)",\
69
+ MAX_RECURSION_DEPTH, s->level); \
68
70
  } while(0);
69
71
 
70
72
  #define S_RECURSE_DEC(s) ((s)->level--)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sereal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Borislav Nikolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-09 00:00:00.000000000 Z
11
+ date: 2014-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.0.3
70
+ rubygems_version: 2.0.14
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: Sereal encoder/decoder