libbin 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +454 -0
- data/ext/libbin/data_types.c +1055 -284
- data/ext/libbin/data_types.h +32 -1
- data/ext/libbin/extconf.rb +15 -0
- data/ext/libbin/half.c +110 -65
- data/ext/libbin/half.h +2 -0
- data/ext/libbin/libbin_c.c +2066 -420
- data/ext/libbin/libbin_c.h +48 -1
- data/ext/libbin/libbin_io.h +506 -0
- data/ext/libbin/pghalf.c +107 -228
- data/ext/libbin/pghalf.h +2 -0
- data/lib/libbin/bitfield.rb +426 -0
- data/lib/libbin/data_shape.rb +387 -0
- data/lib/libbin/data_types.rb +264 -511
- data/lib/libbin/enum.rb +159 -0
- data/lib/libbin/union.rb +314 -0
- data/lib/libbin/utf16.rb +183 -0
- data/lib/libbin/version.rb +4 -0
- data/lib/libbin.rb +165 -14
- data/libbin.gemspec +23 -4
- metadata +46 -5
data/ext/libbin/libbin_c.c
CHANGED
|
@@ -4,12 +4,26 @@
|
|
|
4
4
|
VALUE cField;
|
|
5
5
|
VALUE mLibBin;
|
|
6
6
|
VALUE cStructure;
|
|
7
|
+
VALUE eLibBinError;
|
|
8
|
+
VALUE eLibBinEOFError;
|
|
9
|
+
VALUE eLibBinValidationError;
|
|
10
|
+
VALUE eLibBinAlignmentError;
|
|
11
|
+
static VALUE cStructure_sclass;
|
|
7
12
|
|
|
8
|
-
static VALUE
|
|
13
|
+
static VALUE rb_re_dot_dot;
|
|
9
14
|
static VALUE rb_str___parent;
|
|
10
15
|
static VALUE rb_str_backslash;
|
|
11
16
|
static VALUE rb_str_dot;
|
|
12
17
|
|
|
18
|
+
static ID id_ivar_structure;
|
|
19
|
+
static ID id_ivar_field;
|
|
20
|
+
static ID id_ivar_offset;
|
|
21
|
+
static ID id_ivar_index;
|
|
22
|
+
|
|
23
|
+
#ifdef HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY
|
|
24
|
+
static rb_ractor_local_key_t libbin_ractor_flags_key;
|
|
25
|
+
#endif
|
|
26
|
+
|
|
13
27
|
struct cField_data {
|
|
14
28
|
VALUE name;
|
|
15
29
|
VALUE type;
|
|
@@ -21,15 +35,47 @@ struct cField_data {
|
|
|
21
35
|
VALUE relative_offset;
|
|
22
36
|
VALUE align;
|
|
23
37
|
VALUE expect;
|
|
38
|
+
VALUE null_offset;
|
|
39
|
+
/* non-VALUE fields below; cField_mark / cField_compact cover all VALUEs above */
|
|
24
40
|
ID getter;
|
|
25
41
|
ID setter;
|
|
42
|
+
ID ivar;
|
|
43
|
+
uint8_t scalar_kind;
|
|
44
|
+
uint8_t is_static_simple;
|
|
45
|
+
uint8_t type_is_struct;
|
|
26
46
|
};
|
|
27
47
|
|
|
28
48
|
static void cField_mark(void* data) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
49
|
+
struct cField_data *d = (struct cField_data *)data;
|
|
50
|
+
LIBBIN_GC_MARK(d->name);
|
|
51
|
+
LIBBIN_GC_MARK(d->type);
|
|
52
|
+
LIBBIN_GC_MARK(d->length);
|
|
53
|
+
LIBBIN_GC_MARK(d->count);
|
|
54
|
+
LIBBIN_GC_MARK(d->offset);
|
|
55
|
+
LIBBIN_GC_MARK(d->sequence);
|
|
56
|
+
LIBBIN_GC_MARK(d->condition);
|
|
57
|
+
LIBBIN_GC_MARK(d->relative_offset);
|
|
58
|
+
LIBBIN_GC_MARK(d->align);
|
|
59
|
+
LIBBIN_GC_MARK(d->expect);
|
|
60
|
+
LIBBIN_GC_MARK(d->null_offset);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#ifdef HAVE_RB_GC_LOCATION
|
|
64
|
+
static void cField_compact(void* data) {
|
|
65
|
+
struct cField_data *d = (struct cField_data *)data;
|
|
66
|
+
d->name = rb_gc_location(d->name);
|
|
67
|
+
d->type = rb_gc_location(d->type);
|
|
68
|
+
d->length = rb_gc_location(d->length);
|
|
69
|
+
d->count = rb_gc_location(d->count);
|
|
70
|
+
d->offset = rb_gc_location(d->offset);
|
|
71
|
+
d->sequence = rb_gc_location(d->sequence);
|
|
72
|
+
d->condition = rb_gc_location(d->condition);
|
|
73
|
+
d->relative_offset = rb_gc_location(d->relative_offset);
|
|
74
|
+
d->align = rb_gc_location(d->align);
|
|
75
|
+
d->expect = rb_gc_location(d->expect);
|
|
76
|
+
d->null_offset = rb_gc_location(d->null_offset);
|
|
32
77
|
}
|
|
78
|
+
#endif
|
|
33
79
|
|
|
34
80
|
static size_t cField_size(const void* data) {
|
|
35
81
|
(void)data;
|
|
@@ -42,9 +88,15 @@ static const rb_data_type_t cField_type = {
|
|
|
42
88
|
.dmark = cField_mark,
|
|
43
89
|
.dfree = RUBY_DEFAULT_FREE,
|
|
44
90
|
.dsize = cField_size,
|
|
91
|
+
#ifdef HAVE_RB_GC_LOCATION
|
|
92
|
+
.dcompact = cField_compact,
|
|
93
|
+
#endif
|
|
45
94
|
},
|
|
46
95
|
.data = NULL,
|
|
47
96
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
97
|
+
#ifdef RUBY_TYPED_FROZEN_SHAREABLE
|
|
98
|
+
| RUBY_TYPED_FROZEN_SHAREABLE
|
|
99
|
+
#endif
|
|
48
100
|
};
|
|
49
101
|
|
|
50
102
|
static VALUE cField_alloc(VALUE self) {
|
|
@@ -54,13 +106,28 @@ static VALUE cField_alloc(VALUE self) {
|
|
|
54
106
|
}
|
|
55
107
|
|
|
56
108
|
static ID id_gsub;
|
|
109
|
+
static ID id_eval;
|
|
110
|
+
static VALUE rb_field_eval_binding;
|
|
111
|
+
|
|
112
|
+
#ifdef HAVE_RB_RACTOR_MAKE_SHAREABLE
|
|
113
|
+
static VALUE cField_protect_make_shareable(VALUE obj) {
|
|
114
|
+
return rb_ractor_make_shareable(obj);
|
|
115
|
+
}
|
|
116
|
+
#endif
|
|
57
117
|
|
|
58
|
-
static inline VALUE cField_preprocess_expression(VALUE
|
|
118
|
+
static inline VALUE cField_preprocess_expression(VALUE expression) {
|
|
59
119
|
if (T_STRING == TYPE(expression)) {
|
|
60
120
|
VALUE proc = rb_str_new_cstr("proc {");
|
|
61
|
-
rb_str_buf_append(proc, rb_funcall(rb_funcall(expression, id_gsub, 2,
|
|
121
|
+
rb_str_buf_append(proc, rb_funcall(rb_funcall(expression, id_gsub, 2, rb_re_dot_dot, rb_str___parent), id_gsub, 2, rb_str_backslash, rb_str_dot));
|
|
62
122
|
rb_str_cat_cstr(proc, "}");
|
|
63
|
-
|
|
123
|
+
VALUE res = rb_funcall(rb_mKernel, id_eval, 2, proc, rb_field_eval_binding);
|
|
124
|
+
#ifdef HAVE_RB_RACTOR_MAKE_SHAREABLE
|
|
125
|
+
int state = 0;
|
|
126
|
+
rb_protect(cField_protect_make_shareable, res, &state);
|
|
127
|
+
if (state)
|
|
128
|
+
rb_set_errinfo(Qnil);
|
|
129
|
+
#endif
|
|
130
|
+
return res;
|
|
64
131
|
} else
|
|
65
132
|
return expression;
|
|
66
133
|
}
|
|
@@ -76,28 +143,67 @@ static VALUE cField_initialize(
|
|
|
76
143
|
VALUE condition,
|
|
77
144
|
VALUE relative_offset,
|
|
78
145
|
VALUE align,
|
|
79
|
-
VALUE expect
|
|
80
|
-
|
|
146
|
+
VALUE expect,
|
|
147
|
+
VALUE null_offset) {
|
|
81
148
|
struct cField_data *data;
|
|
82
149
|
TypedData_Get_Struct(self, struct cField_data, &cField_type, data);
|
|
83
150
|
if (RTEST(align)) {
|
|
84
|
-
|
|
85
|
-
if (a & (a-1))
|
|
86
|
-
rb_raise(
|
|
151
|
+
long long a = NUM2LL(align);
|
|
152
|
+
if (a <= 0 || (a & (a - 1)))
|
|
153
|
+
rb_raise(eLibBinAlignmentError, "alignment is not a power of 2: %lld", a);
|
|
87
154
|
}
|
|
155
|
+
if (RTEST(null_offset))
|
|
156
|
+
(void)NUM2LL(null_offset);
|
|
88
157
|
data->name = name;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
158
|
+
if (NIL_P(name)) {
|
|
159
|
+
data->getter = 0;
|
|
160
|
+
data->setter = 0;
|
|
161
|
+
data->ivar = 0;
|
|
162
|
+
} else {
|
|
163
|
+
VALUE tmp = rb_str_dup(rb_obj_as_string(name));
|
|
164
|
+
data->getter = rb_intern_str(tmp);
|
|
165
|
+
VALUE ivar_str = rb_str_new_cstr("@");
|
|
166
|
+
rb_str_buf_append(ivar_str, tmp);
|
|
167
|
+
data->ivar = rb_intern_str(ivar_str);
|
|
168
|
+
data->setter = rb_intern_str(rb_str_cat(tmp, "=", 1));
|
|
169
|
+
}
|
|
170
|
+
data->type = cField_preprocess_expression(type);
|
|
171
|
+
data->length = cField_preprocess_expression(length);
|
|
172
|
+
data->count = cField_preprocess_expression(count);
|
|
173
|
+
data->offset = cField_preprocess_expression(offset);
|
|
96
174
|
data->sequence = sequence;
|
|
97
|
-
data->condition = cField_preprocess_expression(
|
|
175
|
+
data->condition = cField_preprocess_expression(condition);
|
|
98
176
|
data->relative_offset = relative_offset;
|
|
99
177
|
data->align = align;
|
|
100
178
|
data->expect = expect;
|
|
179
|
+
data->null_offset = null_offset;
|
|
180
|
+
data->scalar_kind = (uint8_t)libbin_lookup_scalar_kind(data->type);
|
|
181
|
+
data->type_is_struct = (RB_TYPE_P(data->type, T_CLASS) && RTEST(rb_class_inherited_p(data->type, cStructure))) ? 1 : 0;
|
|
182
|
+
data->is_static_simple = (
|
|
183
|
+
!RTEST(data->sequence) &&
|
|
184
|
+
!RTEST(data->condition) &&
|
|
185
|
+
!RTEST(data->offset) &&
|
|
186
|
+
!RTEST(data->align) &&
|
|
187
|
+
!RTEST(data->count) &&
|
|
188
|
+
NIL_P(data->expect) &&
|
|
189
|
+
!rb_obj_is_proc(data->type) &&
|
|
190
|
+
!rb_obj_is_proc(data->length)
|
|
191
|
+
) ? 1 : 0;
|
|
192
|
+
#if defined(HAVE_RB_RACTOR_SHAREABLE_P) && defined(HAVE_RB_RACTOR_MAKE_SHAREABLE)
|
|
193
|
+
if (rb_ractor_shareable_p(data->name) &&
|
|
194
|
+
rb_ractor_shareable_p(data->type) &&
|
|
195
|
+
rb_ractor_shareable_p(data->length) &&
|
|
196
|
+
rb_ractor_shareable_p(data->count) &&
|
|
197
|
+
rb_ractor_shareable_p(data->offset) &&
|
|
198
|
+
rb_ractor_shareable_p(data->sequence) &&
|
|
199
|
+
rb_ractor_shareable_p(data->condition) &&
|
|
200
|
+
rb_ractor_shareable_p(data->relative_offset) &&
|
|
201
|
+
rb_ractor_shareable_p(data->align) &&
|
|
202
|
+
rb_ractor_shareable_p(data->expect) &&
|
|
203
|
+
rb_ractor_shareable_p(data->null_offset)) {
|
|
204
|
+
rb_ractor_make_shareable(self);
|
|
205
|
+
}
|
|
206
|
+
#endif
|
|
101
207
|
return self;
|
|
102
208
|
}
|
|
103
209
|
|
|
@@ -126,6 +232,7 @@ CREATE_FIELD_STATE_GETTER(condition)
|
|
|
126
232
|
CREATE_FIELD_STATE_GETTER(relative_offset)
|
|
127
233
|
CREATE_FIELD_STATE_GETTER(align)
|
|
128
234
|
CREATE_FIELD_STATE_GETTER(expect)
|
|
235
|
+
CREATE_FIELD_STATE_GETTER(null_offset)
|
|
129
236
|
|
|
130
237
|
struct cStructure_data {
|
|
131
238
|
VALUE __input;
|
|
@@ -144,13 +251,49 @@ struct cStructure_data {
|
|
|
144
251
|
VALUE __count;
|
|
145
252
|
VALUE __iterator;
|
|
146
253
|
VALUE __value;
|
|
254
|
+
/* non-VALUE fields below; cStructure_mark / cStructure_compact cover all VALUEs above */
|
|
255
|
+
libbin_ctx_t *active_ctx;
|
|
147
256
|
};
|
|
148
257
|
|
|
149
258
|
static void cStructure_mark(void* data) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
259
|
+
struct cStructure_data *d = (struct cStructure_data *)data;
|
|
260
|
+
LIBBIN_GC_MARK(d->__input);
|
|
261
|
+
LIBBIN_GC_MARK(d->__output);
|
|
262
|
+
LIBBIN_GC_MARK(d->__input_big);
|
|
263
|
+
LIBBIN_GC_MARK(d->__output_big);
|
|
264
|
+
LIBBIN_GC_MARK(d->__parent);
|
|
265
|
+
LIBBIN_GC_MARK(d->__index);
|
|
266
|
+
LIBBIN_GC_MARK(d->__position);
|
|
267
|
+
LIBBIN_GC_MARK(d->__cur_position);
|
|
268
|
+
LIBBIN_GC_MARK(d->__offset);
|
|
269
|
+
LIBBIN_GC_MARK(d->__condition);
|
|
270
|
+
LIBBIN_GC_MARK(d->__type);
|
|
271
|
+
LIBBIN_GC_MARK(d->__length);
|
|
272
|
+
LIBBIN_GC_MARK(d->__count);
|
|
273
|
+
LIBBIN_GC_MARK(d->__iterator);
|
|
274
|
+
LIBBIN_GC_MARK(d->__value);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
#ifdef HAVE_RB_GC_LOCATION
|
|
278
|
+
static void cStructure_compact(void* data) {
|
|
279
|
+
struct cStructure_data *d = (struct cStructure_data *)data;
|
|
280
|
+
d->__input = rb_gc_location(d->__input);
|
|
281
|
+
d->__output = rb_gc_location(d->__output);
|
|
282
|
+
d->__input_big = rb_gc_location(d->__input_big);
|
|
283
|
+
d->__output_big = rb_gc_location(d->__output_big);
|
|
284
|
+
d->__parent = rb_gc_location(d->__parent);
|
|
285
|
+
d->__index = rb_gc_location(d->__index);
|
|
286
|
+
d->__position = rb_gc_location(d->__position);
|
|
287
|
+
d->__cur_position = rb_gc_location(d->__cur_position);
|
|
288
|
+
d->__offset = rb_gc_location(d->__offset);
|
|
289
|
+
d->__condition = rb_gc_location(d->__condition);
|
|
290
|
+
d->__type = rb_gc_location(d->__type);
|
|
291
|
+
d->__length = rb_gc_location(d->__length);
|
|
292
|
+
d->__count = rb_gc_location(d->__count);
|
|
293
|
+
d->__iterator = rb_gc_location(d->__iterator);
|
|
294
|
+
d->__value = rb_gc_location(d->__value);
|
|
153
295
|
}
|
|
296
|
+
#endif
|
|
154
297
|
|
|
155
298
|
static size_t cStructure_size(const void* data) {
|
|
156
299
|
(void)data;
|
|
@@ -163,20 +306,15 @@ static const rb_data_type_t cStructure_type = {
|
|
|
163
306
|
.dmark = cStructure_mark,
|
|
164
307
|
.dfree = RUBY_DEFAULT_FREE,
|
|
165
308
|
.dsize = cStructure_size,
|
|
309
|
+
#ifdef HAVE_RB_GC_LOCATION
|
|
310
|
+
.dcompact = cStructure_compact,
|
|
311
|
+
#endif
|
|
166
312
|
},
|
|
167
313
|
.data = NULL,
|
|
168
314
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
169
315
|
};
|
|
170
316
|
|
|
171
|
-
static
|
|
172
|
-
struct cStructure_data *data;
|
|
173
|
-
VALUE res = TypedData_Make_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
174
|
-
return res;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
static VALUE cStructure_initialize(VALUE self) {
|
|
178
|
-
struct cStructure_data *data;
|
|
179
|
-
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
317
|
+
static inline void cStructure_init_data(struct cStructure_data *data) {
|
|
180
318
|
data->__input = Qnil;
|
|
181
319
|
data->__output = Qnil;
|
|
182
320
|
data->__input_big = Qnil;
|
|
@@ -191,13 +329,28 @@ static VALUE cStructure_initialize(VALUE self) {
|
|
|
191
329
|
data->__length = Qnil;
|
|
192
330
|
data->__count = Qnil;
|
|
193
331
|
data->__iterator = Qnil;
|
|
332
|
+
data->__value = Qnil;
|
|
333
|
+
data->active_ctx = NULL;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static VALUE cStructure_alloc(VALUE self) {
|
|
337
|
+
struct cStructure_data *data;
|
|
338
|
+
VALUE res = TypedData_Make_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
339
|
+
cStructure_init_data(data);
|
|
340
|
+
return res;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static VALUE cStructure_initialize(VALUE self) {
|
|
344
|
+
struct cStructure_data *data;
|
|
345
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
346
|
+
cStructure_init_data(data);
|
|
194
347
|
return self;
|
|
195
348
|
}
|
|
196
349
|
|
|
197
350
|
#define STRUCT_STATE_GETTER(propname) cStructure_get_ ## propname
|
|
198
351
|
#define STRUCT_STATE_SETTER(propname) cStructure_set_ ## propname
|
|
199
352
|
|
|
200
|
-
#define CREATE_STRUCT_STATE_ACCESSORS(propname)
|
|
353
|
+
#define CREATE_STRUCT_STATE_ACCESSORS(propname) \
|
|
201
354
|
static VALUE STRUCT_STATE_GETTER(propname) (VALUE self) { \
|
|
202
355
|
struct cStructure_data *data; \
|
|
203
356
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data); \
|
|
@@ -232,9 +385,460 @@ CREATE_STRUCT_STATE_ACCESSORS(count)
|
|
|
232
385
|
CREATE_STRUCT_STATE_ACCESSORS(iterator)
|
|
233
386
|
CREATE_STRUCT_STATE_ACCESSORS(value)
|
|
234
387
|
|
|
235
|
-
|
|
388
|
+
/* Cached IDs */
|
|
389
|
+
static ID id_instance_exec;
|
|
390
|
+
static ID id_fields;
|
|
391
|
+
static ID id_last, id_values, id_compact, id_output, id_print;
|
|
392
|
+
static ID id_unpack, id_pack, id_first;
|
|
393
|
+
static ID id_initialize, id_instance_method, id_method, id_owner, id_source_location;
|
|
394
|
+
static ID id___libbin_flags, id___libbin_flags_gen;
|
|
395
|
+
static ID id_stride, id___extend_range;
|
|
396
|
+
|
|
397
|
+
static ID id___set_load_state, id___unset_load_state;
|
|
398
|
+
static ID id___load_field, id___load_fields, id_load, id___load;
|
|
399
|
+
static ID id___set_dump_state, id___unset_dump_state;
|
|
400
|
+
static ID id___dump_field, id___dump_fields, id_dump, id___dump;
|
|
401
|
+
static ID id___set_convert_state, id___unset_convert_state;
|
|
402
|
+
static ID id___convert_field, id___convert_fields, id_convert, id___convert;
|
|
403
|
+
static ID id___set_size_state, id___unset_size_state;
|
|
404
|
+
static ID id___shape_field, id___shape_fields, id_shape, id___shape;
|
|
405
|
+
|
|
406
|
+
/* Override detection bitmask for Structure subclasses */
|
|
407
|
+
#define LIBBIN_OVR_INIT (1u << 0)
|
|
408
|
+
#define LIBBIN_OVR_INST_LOAD (1u << 1)
|
|
409
|
+
#define LIBBIN_OVR_SINGL_LOAD (1u << 2)
|
|
410
|
+
#define LIBBIN_OVR_INST_DUMP (1u << 3)
|
|
411
|
+
#define LIBBIN_OVR_SINGL_DUMP (1u << 4)
|
|
412
|
+
#define LIBBIN_OVR_INST_CONVERT (1u << 5)
|
|
413
|
+
#define LIBBIN_OVR_SINGL_CONVERT (1u << 6)
|
|
414
|
+
#define LIBBIN_OVR_LOAD_FIELDS (1u << 7)
|
|
415
|
+
#define LIBBIN_OVR_DUMP_FIELDS (1u << 8)
|
|
416
|
+
#define LIBBIN_OVR_CONVERT_FIELDS (1u << 9)
|
|
417
|
+
#define LIBBIN_OVR_LOAD_FIELD (1u << 10)
|
|
418
|
+
#define LIBBIN_OVR_DUMP_FIELD (1u << 11)
|
|
419
|
+
#define LIBBIN_OVR_CONVERT_FIELD (1u << 12)
|
|
420
|
+
#define LIBBIN_OVR_SET_LOAD (1u << 13)
|
|
421
|
+
#define LIBBIN_OVR_SET_DUMP (1u << 14)
|
|
422
|
+
#define LIBBIN_OVR_SET_CONVERT (1u << 15)
|
|
423
|
+
#define LIBBIN_OVR_ACCESSORS (1u << 16)
|
|
424
|
+
#define LIBBIN_ALL_SCALAR_LEAF (1u << 17)
|
|
425
|
+
#define LIBBIN_HAS_STRIDE (1u << 18)
|
|
426
|
+
#define LIBBIN_HAS_OFFSET_FIELD (1u << 19)
|
|
427
|
+
|
|
428
|
+
#define LIBBIN_FAST_LOAD_MASK \
|
|
429
|
+
(LIBBIN_OVR_INST_LOAD | LIBBIN_OVR_SET_LOAD | LIBBIN_OVR_LOAD_FIELDS | LIBBIN_OVR_LOAD_FIELD)
|
|
430
|
+
#define LIBBIN_FAST_DUMP_MASK \
|
|
431
|
+
(LIBBIN_OVR_INST_DUMP | LIBBIN_OVR_SET_DUMP | LIBBIN_OVR_DUMP_FIELDS | LIBBIN_OVR_DUMP_FIELD)
|
|
432
|
+
#define LIBBIN_FAST_CONVERT_MASK \
|
|
433
|
+
(LIBBIN_OVR_INST_CONVERT | LIBBIN_OVR_SET_CONVERT | LIBBIN_OVR_CONVERT_FIELDS | LIBBIN_OVR_CONVERT_FIELD)
|
|
434
|
+
|
|
435
|
+
static uint64_t libbin_override_gen = 1;
|
|
436
|
+
static VALUE rb_data_types_file = Qnil;
|
|
437
|
+
|
|
438
|
+
static VALUE cStructure_method_added(VALUE self, VALUE mid) {
|
|
439
|
+
(void)self;
|
|
440
|
+
(void)mid;
|
|
441
|
+
LIBBIN_ATOMIC_INC(&libbin_override_gen);
|
|
442
|
+
return Qnil;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
static VALUE cStructure_singleton_method_added(VALUE self, VALUE mid) {
|
|
446
|
+
(void)self;
|
|
447
|
+
(void)mid;
|
|
448
|
+
LIBBIN_ATOMIC_INC(&libbin_override_gen);
|
|
449
|
+
return Qnil;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
static VALUE cStructure_bump_override_gen(VALUE self) {
|
|
453
|
+
(void)self;
|
|
454
|
+
LIBBIN_ATOMIC_INC(&libbin_override_gen);
|
|
455
|
+
return Qnil;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
static VALUE cStructure_preprocess_expression(VALUE self, VALUE expr) {
|
|
459
|
+
(void)self;
|
|
460
|
+
return cField_preprocess_expression(expr);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
static VALUE cStructure_set_data_types_file(VALUE self, VALUE path) {
|
|
464
|
+
(void)self;
|
|
465
|
+
Check_Type(path, T_STRING);
|
|
466
|
+
rb_data_types_file = rb_obj_freeze(rb_str_dup(path));
|
|
467
|
+
#ifdef HAVE_RB_RACTOR_MAKE_SHAREABLE
|
|
468
|
+
rb_ractor_make_shareable(rb_data_types_file);
|
|
469
|
+
#endif
|
|
470
|
+
rb_gc_register_mark_object(rb_data_types_file);
|
|
471
|
+
LIBBIN_ATOMIC_INC(&libbin_override_gen);
|
|
472
|
+
return Qnil;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
static inline int is_default_accessor_method(VALUE klass, ID mid) {
|
|
476
|
+
VALUE um = rb_funcall(klass, id_instance_method, 1, ID2SYM(mid));
|
|
477
|
+
if (!RTEST(rb_class_inherited_p(rb_funcall(um, id_owner, 0), cStructure)))
|
|
478
|
+
return 0;
|
|
479
|
+
VALUE sl = rb_funcall(um, id_source_location, 0);
|
|
480
|
+
if (!RB_TYPE_P(sl, T_ARRAY) || RARRAY_LEN(sl) < 1)
|
|
481
|
+
return 0;
|
|
482
|
+
if (NIL_P(rb_data_types_file))
|
|
483
|
+
return 0;
|
|
484
|
+
return RTEST(rb_str_equal(rb_ary_entry(sl, 0), rb_data_types_file));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
static inline int method_owned_by(VALUE klass, ID mid, VALUE expected_owner) {
|
|
488
|
+
VALUE um = rb_funcall(klass, id_instance_method, 1, ID2SYM(mid));
|
|
489
|
+
return rb_funcall(um, id_owner, 0) == expected_owner;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
static inline int singl_method_owned_by(VALUE klass, ID mid, VALUE expected_owner) {
|
|
493
|
+
VALUE m = rb_funcall(klass, id_method, 1, ID2SYM(mid));
|
|
494
|
+
return rb_funcall(m, id_owner, 0) == expected_owner;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
static uint32_t libbin_compute_structure_flags(VALUE klass) {
|
|
498
|
+
uint32_t flags = 0;
|
|
499
|
+
if (!method_owned_by(klass, id_initialize, cStructure))
|
|
500
|
+
flags |= LIBBIN_OVR_INIT;
|
|
501
|
+
if (!method_owned_by(klass, id___load, cStructure))
|
|
502
|
+
flags |= LIBBIN_OVR_INST_LOAD;
|
|
503
|
+
if (!singl_method_owned_by(klass, id_load, cStructure_sclass))
|
|
504
|
+
flags |= LIBBIN_OVR_SINGL_LOAD;
|
|
505
|
+
if (!method_owned_by(klass, id___dump, cStructure))
|
|
506
|
+
flags |= LIBBIN_OVR_INST_DUMP;
|
|
507
|
+
if (!singl_method_owned_by(klass, id_dump, cStructure_sclass))
|
|
508
|
+
flags |= LIBBIN_OVR_SINGL_DUMP;
|
|
509
|
+
if (!method_owned_by(klass, id___convert, cStructure))
|
|
510
|
+
flags |= LIBBIN_OVR_INST_CONVERT;
|
|
511
|
+
if (!singl_method_owned_by(klass, id_convert, cStructure_sclass))
|
|
512
|
+
flags |= LIBBIN_OVR_SINGL_CONVERT;
|
|
513
|
+
if (!method_owned_by(klass, id___load_fields, cStructure))
|
|
514
|
+
flags |= LIBBIN_OVR_LOAD_FIELDS;
|
|
515
|
+
if (!method_owned_by(klass, id___dump_fields, cStructure))
|
|
516
|
+
flags |= LIBBIN_OVR_DUMP_FIELDS;
|
|
517
|
+
if (!method_owned_by(klass, id___convert_fields, cStructure))
|
|
518
|
+
flags |= LIBBIN_OVR_CONVERT_FIELDS;
|
|
519
|
+
if (!method_owned_by(klass, id___load_field, cStructure))
|
|
520
|
+
flags |= LIBBIN_OVR_LOAD_FIELD;
|
|
521
|
+
if (!method_owned_by(klass, id___dump_field, cStructure))
|
|
522
|
+
flags |= LIBBIN_OVR_DUMP_FIELD;
|
|
523
|
+
if (!method_owned_by(klass, id___convert_field, cStructure))
|
|
524
|
+
flags |= LIBBIN_OVR_CONVERT_FIELD;
|
|
525
|
+
if (!method_owned_by(klass, id___set_load_state, cStructure) ||
|
|
526
|
+
!method_owned_by(klass, id___unset_load_state, cStructure))
|
|
527
|
+
flags |= LIBBIN_OVR_SET_LOAD;
|
|
528
|
+
if (!method_owned_by(klass, id___set_dump_state, cStructure) ||
|
|
529
|
+
!method_owned_by(klass, id___unset_dump_state, cStructure))
|
|
530
|
+
flags |= LIBBIN_OVR_SET_DUMP;
|
|
531
|
+
if (!method_owned_by(klass, id___set_convert_state, cStructure) ||
|
|
532
|
+
!method_owned_by(klass, id___unset_convert_state, cStructure))
|
|
533
|
+
flags |= LIBBIN_OVR_SET_CONVERT;
|
|
534
|
+
|
|
535
|
+
VALUE stride = rb_attr_get(klass, id_stride);
|
|
536
|
+
int has_stride = RTEST(stride);
|
|
537
|
+
if (has_stride)
|
|
538
|
+
flags |= LIBBIN_HAS_STRIDE;
|
|
539
|
+
|
|
540
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
541
|
+
if (!RB_TYPE_P(fields, T_ARRAY)) {
|
|
542
|
+
flags |= LIBBIN_OVR_ACCESSORS;
|
|
543
|
+
} else {
|
|
544
|
+
int all_scalar_leaf = !has_stride;
|
|
545
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
546
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
547
|
+
struct cField_data *fd;
|
|
548
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, fd);
|
|
549
|
+
if (RTEST(fd->offset))
|
|
550
|
+
flags |= LIBBIN_HAS_OFFSET_FIELD;
|
|
551
|
+
if (!fd->is_static_simple || fd->scalar_kind == SCALAR_NONE)
|
|
552
|
+
all_scalar_leaf = 0;
|
|
553
|
+
if (!NIL_P(fd->name)) {
|
|
554
|
+
if (!is_default_accessor_method(klass, fd->getter) ||
|
|
555
|
+
!is_default_accessor_method(klass, fd->setter)) {
|
|
556
|
+
flags |= LIBBIN_OVR_ACCESSORS;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (all_scalar_leaf && !(flags & LIBBIN_OVR_ACCESSORS))
|
|
561
|
+
flags |= LIBBIN_ALL_SCALAR_LEAF;
|
|
562
|
+
}
|
|
563
|
+
return flags;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
struct libbin_set_flags_args {
|
|
567
|
+
VALUE klass;
|
|
568
|
+
uint32_t flags;
|
|
569
|
+
uint64_t gen;
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
static VALUE libbin_set_class_flags_cb(VALUE arg) {
|
|
573
|
+
struct libbin_set_flags_args *a = (struct libbin_set_flags_args *)arg;
|
|
574
|
+
rb_ivar_set(a->klass, id___libbin_flags, ULONG2NUM(a->flags));
|
|
575
|
+
rb_ivar_set(a->klass, id___libbin_flags_gen, ULL2NUM(a->gen));
|
|
576
|
+
return Qnil;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
static inline uint32_t libbin_get_structure_flags(VALUE klass) {
|
|
580
|
+
uint64_t cur_gen = LIBBIN_ATOMIC_LOAD(&libbin_override_gen);
|
|
581
|
+
VALUE gen = rb_attr_get(klass, id___libbin_flags_gen);
|
|
582
|
+
if (!NIL_P(gen) && NUM2ULL(gen) == cur_gen) {
|
|
583
|
+
return (uint32_t)NUM2ULONG(rb_attr_get(klass, id___libbin_flags));
|
|
584
|
+
}
|
|
585
|
+
#ifdef HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY
|
|
586
|
+
VALUE rhash = rb_ractor_local_storage_value(libbin_ractor_flags_key);
|
|
587
|
+
if (RB_TYPE_P(rhash, T_HASH)) {
|
|
588
|
+
VALUE entry = rb_hash_lookup(rhash, klass);
|
|
589
|
+
if (RB_TYPE_P(entry, T_ARRAY) && RARRAY_LEN(entry) == 2 &&
|
|
590
|
+
NUM2ULL(rb_ary_entry(entry, 0)) == cur_gen) {
|
|
591
|
+
return (uint32_t)NUM2ULONG(rb_ary_entry(entry, 1));
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
#endif
|
|
595
|
+
uint32_t flags = libbin_compute_structure_flags(klass);
|
|
596
|
+
struct libbin_set_flags_args args = { klass, flags, cur_gen };
|
|
597
|
+
int state = 0;
|
|
598
|
+
rb_protect(libbin_set_class_flags_cb, (VALUE)&args, &state);
|
|
599
|
+
if (state) {
|
|
600
|
+
VALUE err = rb_errinfo();
|
|
601
|
+
if (rb_obj_is_kind_of(err, rb_eStandardError)) {
|
|
602
|
+
rb_set_errinfo(Qnil);
|
|
603
|
+
#ifdef HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY
|
|
604
|
+
if (!RB_TYPE_P(rhash, T_HASH)) {
|
|
605
|
+
rhash = rb_hash_new();
|
|
606
|
+
rb_ractor_local_storage_value_set(libbin_ractor_flags_key, rhash);
|
|
607
|
+
}
|
|
608
|
+
rb_hash_aset(rhash, klass, rb_ary_new_from_args(2, ULL2NUM(cur_gen), ULONG2NUM(flags)));
|
|
609
|
+
#endif
|
|
610
|
+
} else {
|
|
611
|
+
rb_jump_tag(state);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return flags;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
void libbin_ctx_sync_to_ruby(libbin_ctx_t *ctx) {
|
|
618
|
+
if (!ctx)
|
|
619
|
+
return;
|
|
620
|
+
if (ctx->has_in)
|
|
621
|
+
libbin_io_sync_to_ruby(&ctx->in_io);
|
|
622
|
+
if (ctx->has_out)
|
|
623
|
+
libbin_io_sync_to_ruby(&ctx->out_io);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
void libbin_ctx_sync_from_ruby(libbin_ctx_t *ctx) {
|
|
627
|
+
if (!ctx)
|
|
628
|
+
return;
|
|
629
|
+
if (ctx->has_in)
|
|
630
|
+
libbin_io_sync_from_ruby(&ctx->in_io);
|
|
631
|
+
if (ctx->has_out)
|
|
632
|
+
libbin_io_sync_from_ruby(&ctx->out_io);
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
libbin_ctx_t *libbin_ctx_from_parent(VALUE parent, VALUE input, VALUE output, uint8_t mode) {
|
|
636
|
+
if (!RTEST(parent) || !rb_obj_is_kind_of(parent, cStructure))
|
|
637
|
+
return NULL;
|
|
638
|
+
struct cStructure_data *pdata;
|
|
639
|
+
TypedData_Get_Struct(parent, struct cStructure_data, &cStructure_type, pdata);
|
|
640
|
+
libbin_ctx_t *ctx = pdata->active_ctx;
|
|
641
|
+
if (!ctx)
|
|
642
|
+
return NULL;
|
|
643
|
+
if (mode == 0 && (!ctx->has_in || ctx->in_io.io != input))
|
|
644
|
+
return NULL;
|
|
645
|
+
if (mode == 1 && (!ctx->has_out || ctx->out_io.io != output))
|
|
646
|
+
return NULL;
|
|
647
|
+
if (mode == 2 && (!ctx->has_in || !ctx->has_out || ctx->in_io.io != input || ctx->out_io.io != output))
|
|
648
|
+
return NULL;
|
|
649
|
+
return ctx;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
static inline VALUE cStructure_restore_context(VALUE self) {
|
|
653
|
+
struct cStructure_data *data;
|
|
654
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
655
|
+
data->__iterator = Qnil;
|
|
656
|
+
data->__type = Qnil;
|
|
657
|
+
data->__length = Qnil;
|
|
658
|
+
data->__count = Qnil;
|
|
659
|
+
data->__offset = Qnil;
|
|
660
|
+
data->__condition = Qnil;
|
|
661
|
+
data->__value = Qnil;
|
|
662
|
+
return Qnil;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
static inline void libbin_unset_frame_state(VALUE self, uint8_t mode) {
|
|
666
|
+
struct cStructure_data *data;
|
|
667
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
668
|
+
if (mode == 0 || mode == 2) {
|
|
669
|
+
data->__input = Qnil;
|
|
670
|
+
data->__input_big = Qnil;
|
|
671
|
+
}
|
|
672
|
+
if (mode == 1 || mode == 2) {
|
|
673
|
+
data->__output = Qnil;
|
|
674
|
+
data->__output_big = Qnil;
|
|
675
|
+
}
|
|
676
|
+
data->__parent = Qnil;
|
|
677
|
+
data->__index = Qnil;
|
|
678
|
+
data->__position = Qnil;
|
|
679
|
+
data->__cur_position = Qnil;
|
|
680
|
+
data->active_ctx = NULL;
|
|
681
|
+
cStructure_restore_context(self);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
static void libbin_print_frame_error(VALUE self, VALUE field) {
|
|
685
|
+
VALUE out = rb_funcall(mLibBin, id_output, 0);
|
|
686
|
+
if (NIL_P(out))
|
|
687
|
+
return;
|
|
688
|
+
if (NIL_P(field)) {
|
|
689
|
+
struct cStructure_data *data;
|
|
690
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
691
|
+
rb_funcall(out, id_print, 6,
|
|
692
|
+
rb_obj_class(self),
|
|
693
|
+
rb_str_new_cstr(": could not load fields, index: "),
|
|
694
|
+
data->__index,
|
|
695
|
+
rb_str_new_cstr(", current position: "),
|
|
696
|
+
data->__cur_position,
|
|
697
|
+
rb_str_new_cstr("\n"));
|
|
698
|
+
} else {
|
|
699
|
+
struct cField_data *field_data;
|
|
700
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
701
|
+
rb_funcall(out, id_print, 6,
|
|
702
|
+
rb_obj_class(self),
|
|
703
|
+
rb_str_new_cstr(": "),
|
|
704
|
+
field_data->name,
|
|
705
|
+
rb_str_new_cstr("("),
|
|
706
|
+
field_data->type,
|
|
707
|
+
rb_str_new_cstr(")\n"));
|
|
708
|
+
}
|
|
709
|
+
}
|
|
236
710
|
|
|
237
|
-
static
|
|
711
|
+
static void libbin_attach_error_context(
|
|
712
|
+
VALUE exception,
|
|
713
|
+
VALUE self,
|
|
714
|
+
VALUE field,
|
|
715
|
+
libbin_ctx_t *ctx,
|
|
716
|
+
uint8_t mode,
|
|
717
|
+
VALUE frame_index)
|
|
718
|
+
{
|
|
719
|
+
if (!rb_obj_is_kind_of(exception, eLibBinError))
|
|
720
|
+
return;
|
|
721
|
+
if (!NIL_P(rb_attr_get(exception, id_ivar_structure)))
|
|
722
|
+
return;
|
|
723
|
+
|
|
724
|
+
rb_ivar_set(exception, id_ivar_structure, rb_obj_class(self));
|
|
725
|
+
|
|
726
|
+
struct cField_data *fd = NULL;
|
|
727
|
+
if (!NIL_P(field)) {
|
|
728
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, fd);
|
|
729
|
+
rb_ivar_set(exception, id_ivar_field, fd->name);
|
|
730
|
+
} else {
|
|
731
|
+
rb_ivar_set(exception, id_ivar_field, Qnil);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (NIL_P(rb_attr_get(exception, id_ivar_offset))) {
|
|
735
|
+
VALUE off = Qnil;
|
|
736
|
+
if (ctx) {
|
|
737
|
+
libbin_io_t *bio = (mode == 1 && ctx->has_out)
|
|
738
|
+
? &ctx->out_io
|
|
739
|
+
: (ctx->has_in ? &ctx->in_io : NULL);
|
|
740
|
+
if (bio) {
|
|
741
|
+
if (bio->kind != LIBBIN_IO_GENERIC || bio->non_seekable) {
|
|
742
|
+
off = LL2NUM(bio->pos);
|
|
743
|
+
} else {
|
|
744
|
+
ptrdiff_t pos = 0;
|
|
745
|
+
if (libbin_try_tell_io(bio->io, &pos))
|
|
746
|
+
off = LL2NUM(pos);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (NIL_P(off)) {
|
|
751
|
+
struct cStructure_data *data;
|
|
752
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
753
|
+
off = !NIL_P(data->__cur_position) ? data->__cur_position : data->__position;
|
|
754
|
+
}
|
|
755
|
+
rb_ivar_set(exception, id_ivar_offset, off);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
if (NIL_P(rb_attr_get(exception, id_ivar_index))) {
|
|
759
|
+
struct cStructure_data *data;
|
|
760
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
761
|
+
VALUE idx = Qnil;
|
|
762
|
+
if (fd && RTEST(fd->count) && !NIL_P(data->__iterator)) {
|
|
763
|
+
idx = data->__iterator;
|
|
764
|
+
} else if (!NIL_P(data->__index)) {
|
|
765
|
+
idx = data->__index;
|
|
766
|
+
} else if (!NIL_P(frame_index)) {
|
|
767
|
+
idx = frame_index;
|
|
768
|
+
}
|
|
769
|
+
rb_ivar_set(exception, id_ivar_index, idx);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
static VALUE libbin_ctx_rescue(VALUE ctx_p, VALUE exception) {
|
|
774
|
+
libbin_ctx_t *ctx = (libbin_ctx_t *)ctx_p;
|
|
775
|
+
while (ctx->frame_depth > 0) {
|
|
776
|
+
libbin_frame_t f = ctx->frames[--ctx->frame_depth];
|
|
777
|
+
libbin_attach_error_context(exception, f.self, f.field, ctx, f.mode, f.index);
|
|
778
|
+
libbin_print_frame_error(f.self, f.field);
|
|
779
|
+
libbin_unset_frame_state(f.self, f.mode);
|
|
780
|
+
}
|
|
781
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
782
|
+
rb_exc_raise(exception);
|
|
783
|
+
return Qnil;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
static VALUE libbin_ctx_ensure(VALUE ctx_p) {
|
|
787
|
+
libbin_ctx_t *ctx = (libbin_ctx_t *)ctx_p;
|
|
788
|
+
while (ctx->frame_depth > 0) {
|
|
789
|
+
libbin_frame_t f = ctx->frames[--ctx->frame_depth];
|
|
790
|
+
libbin_unset_frame_state(f.self, f.mode);
|
|
791
|
+
}
|
|
792
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
793
|
+
return Qnil;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
static inline void libbin_ctx_init_load(libbin_ctx_t *ctx, VALUE input) {
|
|
797
|
+
libbin_io_init(&ctx->in_io, input, 0);
|
|
798
|
+
ctx->pending_index = Qnil;
|
|
799
|
+
ctx->has_in = 1;
|
|
800
|
+
ctx->has_out = 0;
|
|
801
|
+
ctx->frame_depth = 0;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
static inline void libbin_ctx_init_dump(libbin_ctx_t *ctx, VALUE output) {
|
|
805
|
+
libbin_io_init(&ctx->out_io, output, 1);
|
|
806
|
+
ctx->pending_index = Qnil;
|
|
807
|
+
ctx->has_in = 0;
|
|
808
|
+
ctx->has_out = 1;
|
|
809
|
+
ctx->frame_depth = 0;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
static inline void libbin_ctx_init_convert(libbin_ctx_t *ctx, VALUE input, VALUE output) {
|
|
813
|
+
if (input == output) {
|
|
814
|
+
libbin_io_init_generic(&ctx->in_io, input, 0);
|
|
815
|
+
libbin_io_init_generic(&ctx->out_io, output, 1);
|
|
816
|
+
} else {
|
|
817
|
+
libbin_io_init(&ctx->in_io, input, 0);
|
|
818
|
+
libbin_io_init(&ctx->out_io, output, 1);
|
|
819
|
+
}
|
|
820
|
+
ctx->pending_index = Qnil;
|
|
821
|
+
ctx->has_in = 1;
|
|
822
|
+
ctx->has_out = 1;
|
|
823
|
+
ctx->frame_depth = 0;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
static inline size_t libbin_ctx_push_frame(libbin_ctx_t *ctx, VALUE self, uint8_t mode, VALUE frame_index) {
|
|
827
|
+
size_t depth = ctx->frame_depth;
|
|
828
|
+
if (depth < LIBBIN_MAX_FRAMES) {
|
|
829
|
+
ctx->frames[depth].self = self;
|
|
830
|
+
ctx->frames[depth].field = Qnil;
|
|
831
|
+
ctx->frames[depth].index = frame_index;
|
|
832
|
+
ctx->frames[depth].mode = mode;
|
|
833
|
+
ctx->frame_depth = depth + 1;
|
|
834
|
+
}
|
|
835
|
+
return depth;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
static inline void libbin_ctx_pop_frame(libbin_ctx_t *ctx, size_t depth) {
|
|
839
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
840
|
+
ctx->frame_depth = depth;
|
|
841
|
+
}
|
|
238
842
|
|
|
239
843
|
static VALUE cStructure_set_convert_state(
|
|
240
844
|
VALUE self,
|
|
@@ -253,28 +857,46 @@ static VALUE cStructure_set_convert_state(
|
|
|
253
857
|
data->__output_big = output_big;
|
|
254
858
|
data->__parent = parent;
|
|
255
859
|
data->__index = index;
|
|
256
|
-
|
|
860
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
|
|
861
|
+
if (pctx) {
|
|
862
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
863
|
+
data->active_ctx = pctx;
|
|
864
|
+
data->__position = LL2NUM(libbin_io_tell(&pctx->in_io));
|
|
865
|
+
} else {
|
|
866
|
+
ptrdiff_t pos = 0;
|
|
867
|
+
data->active_ctx = NULL;
|
|
868
|
+
data->__position = libbin_try_tell_io(input, &pos) ? LL2NUM(pos) : INT2FIX(0);
|
|
869
|
+
}
|
|
257
870
|
data->__cur_position = data->__position;
|
|
258
871
|
return Qnil;
|
|
259
872
|
}
|
|
260
873
|
|
|
261
|
-
static
|
|
262
|
-
|
|
263
|
-
static VALUE cStructure_unset_convert_state(VALUE self) {
|
|
874
|
+
static inline VALUE cStructure_unset_state_mode(VALUE self, uint8_t mode) {
|
|
264
875
|
struct cStructure_data *data;
|
|
265
876
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
266
|
-
data->
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
877
|
+
if (data->active_ctx) {
|
|
878
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
879
|
+
data->active_ctx = NULL;
|
|
880
|
+
}
|
|
881
|
+
if (mode == 0 || mode == 2) {
|
|
882
|
+
data->__input = Qnil;
|
|
883
|
+
data->__input_big = Qnil;
|
|
884
|
+
}
|
|
885
|
+
if (mode == 1 || mode == 2) {
|
|
886
|
+
data->__output = Qnil;
|
|
887
|
+
data->__output_big = Qnil;
|
|
888
|
+
}
|
|
270
889
|
data->__parent = Qnil;
|
|
271
890
|
data->__index = Qnil;
|
|
272
891
|
data->__position = Qnil;
|
|
273
892
|
data->__cur_position = Qnil;
|
|
893
|
+
cStructure_restore_context(self);
|
|
274
894
|
return Qnil;
|
|
275
895
|
}
|
|
276
896
|
|
|
277
|
-
static
|
|
897
|
+
static VALUE cStructure_unset_convert_state(VALUE self) {
|
|
898
|
+
return cStructure_unset_state_mode(self, 2);
|
|
899
|
+
}
|
|
278
900
|
|
|
279
901
|
static VALUE cStructure_set_size_state(
|
|
280
902
|
VALUE self,
|
|
@@ -291,8 +913,6 @@ static VALUE cStructure_set_size_state(
|
|
|
291
913
|
return Qnil;
|
|
292
914
|
}
|
|
293
915
|
|
|
294
|
-
static ID id___unset_size_state;
|
|
295
|
-
|
|
296
916
|
static VALUE cStructure_unset_size_state(VALUE self) {
|
|
297
917
|
struct cStructure_data *data;
|
|
298
918
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
@@ -300,11 +920,10 @@ static VALUE cStructure_unset_size_state(VALUE self) {
|
|
|
300
920
|
data->__index = Qnil;
|
|
301
921
|
data->__position = Qnil;
|
|
302
922
|
data->__cur_position = Qnil;
|
|
923
|
+
cStructure_restore_context(self);
|
|
303
924
|
return Qnil;
|
|
304
925
|
}
|
|
305
926
|
|
|
306
|
-
static ID id___set_load_state;
|
|
307
|
-
|
|
308
927
|
static VALUE cStructure_set_load_state(
|
|
309
928
|
VALUE self,
|
|
310
929
|
VALUE input,
|
|
@@ -318,27 +937,24 @@ static VALUE cStructure_set_load_state(
|
|
|
318
937
|
data->__input_big = input_big;
|
|
319
938
|
data->__parent = parent;
|
|
320
939
|
data->__index = index;
|
|
321
|
-
|
|
940
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
|
|
941
|
+
if (pctx) {
|
|
942
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
943
|
+
data->active_ctx = pctx;
|
|
944
|
+
data->__position = LL2NUM(libbin_io_tell(&pctx->in_io));
|
|
945
|
+
} else {
|
|
946
|
+
ptrdiff_t pos = 0;
|
|
947
|
+
data->active_ctx = NULL;
|
|
948
|
+
data->__position = libbin_try_tell_io(input, &pos) ? LL2NUM(pos) : INT2FIX(0);
|
|
949
|
+
}
|
|
322
950
|
data->__cur_position = data->__position;
|
|
323
951
|
return Qnil;
|
|
324
952
|
}
|
|
325
953
|
|
|
326
|
-
static ID id___unset_load_state;
|
|
327
|
-
|
|
328
954
|
static VALUE cStructure_unset_load_state(VALUE self) {
|
|
329
|
-
|
|
330
|
-
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
331
|
-
data->__input = Qnil;
|
|
332
|
-
data->__input_big = Qnil;
|
|
333
|
-
data->__parent = Qnil;
|
|
334
|
-
data->__index = Qnil;
|
|
335
|
-
data->__position = Qnil;
|
|
336
|
-
data->__cur_position = Qnil;
|
|
337
|
-
return Qnil;
|
|
955
|
+
return cStructure_unset_state_mode(self, 0);
|
|
338
956
|
}
|
|
339
957
|
|
|
340
|
-
static ID id___set_dump_state;
|
|
341
|
-
|
|
342
958
|
static VALUE cStructure_set_dump_state(
|
|
343
959
|
VALUE self,
|
|
344
960
|
VALUE output,
|
|
@@ -352,60 +968,95 @@ static VALUE cStructure_set_dump_state(
|
|
|
352
968
|
data->__output_big = output_big;
|
|
353
969
|
data->__parent = parent;
|
|
354
970
|
data->__index = index;
|
|
355
|
-
|
|
971
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
|
|
972
|
+
if (pctx) {
|
|
973
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
974
|
+
data->active_ctx = pctx;
|
|
975
|
+
data->__position = LL2NUM(libbin_io_tell(&pctx->out_io));
|
|
976
|
+
} else {
|
|
977
|
+
ptrdiff_t pos = 0;
|
|
978
|
+
data->active_ctx = NULL;
|
|
979
|
+
data->__position = libbin_try_tell_io(output, &pos) ? LL2NUM(pos) : INT2FIX(0);
|
|
980
|
+
}
|
|
356
981
|
data->__cur_position = data->__position;
|
|
357
982
|
return Qnil;
|
|
358
983
|
}
|
|
359
984
|
|
|
360
|
-
static ID id___unset_dump_state;
|
|
361
|
-
|
|
362
985
|
static VALUE cStructure_unset_dump_state(VALUE self) {
|
|
986
|
+
return cStructure_unset_state_mode(self, 1);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
static inline VALUE cStructure_decode_expression_ctx(VALUE self, libbin_ctx_t *ctx, VALUE expression) {
|
|
990
|
+
if (rb_obj_is_proc(expression)) {
|
|
991
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
992
|
+
VALUE res = rb_funcall_with_block(self, id_instance_exec, 0, NULL, expression);
|
|
993
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
994
|
+
return res;
|
|
995
|
+
}
|
|
996
|
+
return expression;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
static VALUE cStructure_decode_expression(VALUE self, VALUE expression) {
|
|
363
1000
|
struct cStructure_data *data;
|
|
364
1001
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
365
|
-
data->
|
|
366
|
-
data->__output_big = Qnil;
|
|
367
|
-
data->__parent = Qnil;
|
|
368
|
-
data->__index = Qnil;
|
|
369
|
-
data->__position = Qnil;
|
|
370
|
-
data->__cur_position = Qnil;
|
|
371
|
-
return Qnil;
|
|
1002
|
+
return cStructure_decode_expression_ctx(self, data->active_ctx, expression);
|
|
372
1003
|
}
|
|
373
1004
|
|
|
374
|
-
static
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
else
|
|
380
|
-
return expression;
|
|
1005
|
+
static inline void cStructure_raise_validation_error(VALUE msg, ptrdiff_t field_start) {
|
|
1006
|
+
VALUE exc = rb_exc_new_str(eLibBinValidationError, msg);
|
|
1007
|
+
if (field_start >= 0)
|
|
1008
|
+
rb_ivar_set(exc, id_ivar_offset, LL2NUM(field_start));
|
|
1009
|
+
rb_exc_raise(exc);
|
|
381
1010
|
}
|
|
382
1011
|
|
|
383
|
-
static inline VALUE
|
|
1012
|
+
static inline VALUE cStructure_decode_expect_ctx(VALUE self, libbin_ctx_t *ctx, VALUE expect, VALUE val, ptrdiff_t field_start) {
|
|
384
1013
|
if (NIL_P(expect))
|
|
385
1014
|
return val;
|
|
386
1015
|
if (rb_obj_is_proc(expect)) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (!RTEST(
|
|
391
|
-
|
|
1016
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1017
|
+
VALUE ok = rb_funcall_with_block(self, id_instance_exec, 1, &val, expect);
|
|
1018
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1019
|
+
if (!RTEST(ok))
|
|
1020
|
+
cStructure_raise_validation_error(rb_sprintf("could not validate field value: %"PRIsVALUE, val), field_start);
|
|
1021
|
+
} else if (!RTEST(rb_equal(expect, val))) {
|
|
1022
|
+
cStructure_raise_validation_error(
|
|
1023
|
+
rb_sprintf("could not validate field value: %"PRIsVALUE" expected: %"PRIsVALUE, val, expect),
|
|
1024
|
+
field_start);
|
|
1025
|
+
}
|
|
392
1026
|
return val;
|
|
393
1027
|
}
|
|
394
1028
|
|
|
395
|
-
|
|
1029
|
+
static VALUE cStructure_decode_expect(VALUE self, VALUE expect, VALUE val) {
|
|
1030
|
+
struct cStructure_data *data;
|
|
1031
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1032
|
+
return cStructure_decode_expect_ctx(self, data->active_ctx, expect, val, -1);
|
|
1033
|
+
}
|
|
396
1034
|
|
|
397
|
-
static inline VALUE
|
|
1035
|
+
static inline VALUE cStructure_decode_seek_offset_ctx(
|
|
1036
|
+
VALUE self,
|
|
1037
|
+
libbin_ctx_t *ctx,
|
|
1038
|
+
VALUE offset,
|
|
1039
|
+
VALUE relative_offset,
|
|
1040
|
+
VALUE align,
|
|
1041
|
+
VALUE null_offset)
|
|
1042
|
+
{
|
|
398
1043
|
struct cStructure_data *data;
|
|
399
1044
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
400
1045
|
ptrdiff_t cur_pos;
|
|
401
1046
|
if (!RTEST(offset)) {
|
|
402
1047
|
if (!RTEST(align))
|
|
403
1048
|
return Qnil;
|
|
404
|
-
if (
|
|
405
|
-
cur_pos =
|
|
406
|
-
else if (
|
|
407
|
-
cur_pos =
|
|
408
|
-
else
|
|
1049
|
+
if (ctx && ctx->has_in)
|
|
1050
|
+
cur_pos = libbin_io_tell(&ctx->in_io);
|
|
1051
|
+
else if (ctx && ctx->has_out)
|
|
1052
|
+
cur_pos = libbin_io_tell(&ctx->out_io);
|
|
1053
|
+
else if (RTEST(data->__input)) {
|
|
1054
|
+
ptrdiff_t pos = 0;
|
|
1055
|
+
cur_pos = libbin_try_tell_io(data->__input, &pos) ? pos : NUM2LL(data->__cur_position);
|
|
1056
|
+
} else if (RTEST(data->__output)) {
|
|
1057
|
+
ptrdiff_t pos = 0;
|
|
1058
|
+
cur_pos = libbin_try_tell_io(data->__output, &pos) ? pos : NUM2LL(data->__cur_position);
|
|
1059
|
+
} else
|
|
409
1060
|
cur_pos = NUM2LL(data->__cur_position);
|
|
410
1061
|
ptrdiff_t al = NUM2LL(align);
|
|
411
1062
|
ptrdiff_t pad;
|
|
@@ -413,8 +1064,11 @@ static inline VALUE cStructure_decode_seek_offset(VALUE self, VALUE offset, VALU
|
|
|
413
1064
|
return Qnil;
|
|
414
1065
|
cur_pos += pad;
|
|
415
1066
|
} else {
|
|
416
|
-
|
|
417
|
-
if (
|
|
1067
|
+
VALUE off_val = cStructure_decode_expression_ctx(self, ctx, offset);
|
|
1068
|
+
if (NIL_P(off_val) || off_val == Qfalse)
|
|
1069
|
+
return Qfalse;
|
|
1070
|
+
cur_pos = NUM2LL(off_val);
|
|
1071
|
+
if (RTEST(null_offset) && cur_pos == NUM2LL(null_offset))
|
|
418
1072
|
return Qfalse;
|
|
419
1073
|
if (RTEST(relative_offset))
|
|
420
1074
|
cur_pos += NUM2LL(data->__position);
|
|
@@ -424,36 +1078,81 @@ static inline VALUE cStructure_decode_seek_offset(VALUE self, VALUE offset, VALU
|
|
|
424
1078
|
}
|
|
425
1079
|
}
|
|
426
1080
|
data->__cur_position = LL2NUM(cur_pos);
|
|
427
|
-
if (
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
1081
|
+
if (ctx) {
|
|
1082
|
+
if (ctx->has_in)
|
|
1083
|
+
libbin_io_seek(&ctx->in_io, cur_pos);
|
|
1084
|
+
if (ctx->has_out)
|
|
1085
|
+
libbin_io_seek(&ctx->out_io, cur_pos);
|
|
1086
|
+
} else {
|
|
1087
|
+
if (RTEST(data->__input))
|
|
1088
|
+
rb_funcall(data->__input, id_seek, 1, data->__cur_position);
|
|
1089
|
+
if (RTEST(data->__output))
|
|
1090
|
+
rb_funcall(data->__output, id_seek, 1, data->__cur_position);
|
|
1091
|
+
}
|
|
431
1092
|
return data->__cur_position;
|
|
432
1093
|
}
|
|
433
1094
|
|
|
434
|
-
static
|
|
1095
|
+
static VALUE cStructure_decode_seek_offset(
|
|
1096
|
+
VALUE self,
|
|
1097
|
+
VALUE offset,
|
|
1098
|
+
VALUE relative_offset,
|
|
1099
|
+
VALUE align,
|
|
1100
|
+
VALUE null_offset) {
|
|
1101
|
+
struct cStructure_data *data;
|
|
1102
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1103
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1104
|
+
VALUE res = cStructure_decode_seek_offset_ctx(self, data->active_ctx, offset, relative_offset, align, null_offset);
|
|
1105
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1106
|
+
return res;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
static inline VALUE cStructure_decode_condition_ctx(VALUE self, libbin_ctx_t *ctx, VALUE condition) {
|
|
435
1110
|
if (!RTEST(condition))
|
|
436
1111
|
return Qtrue;
|
|
437
|
-
return
|
|
1112
|
+
return cStructure_decode_expression_ctx(self, ctx, condition);
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
static VALUE cStructure_decode_condition(VALUE self, VALUE condition) {
|
|
1116
|
+
struct cStructure_data *data;
|
|
1117
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1118
|
+
return cStructure_decode_condition_ctx(self, data->active_ctx, condition);
|
|
438
1119
|
}
|
|
439
1120
|
|
|
440
|
-
static inline VALUE
|
|
1121
|
+
static inline VALUE cStructure_decode_count_ctx(VALUE self, libbin_ctx_t *ctx, VALUE count) {
|
|
441
1122
|
if (!RTEST(count))
|
|
442
1123
|
return INT2FIX(1);
|
|
443
|
-
return
|
|
1124
|
+
return cStructure_decode_expression_ctx(self, ctx, count);
|
|
444
1125
|
}
|
|
445
1126
|
|
|
446
|
-
static
|
|
447
|
-
|
|
1127
|
+
static VALUE cStructure_decode_count(VALUE self, VALUE count) {
|
|
1128
|
+
struct cStructure_data *data;
|
|
1129
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1130
|
+
return cStructure_decode_count_ctx(self, data->active_ctx, count);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
static inline VALUE cStructure_decode_type_ctx(VALUE self, libbin_ctx_t *ctx, VALUE type) {
|
|
1134
|
+
return cStructure_decode_expression_ctx(self, ctx, type);
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
static VALUE cStructure_decode_type(VALUE self, VALUE type) {
|
|
1138
|
+
struct cStructure_data *data;
|
|
1139
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1140
|
+
return cStructure_decode_type_ctx(self, data->active_ctx, type);
|
|
448
1141
|
}
|
|
449
1142
|
|
|
450
|
-
static inline VALUE
|
|
1143
|
+
static inline VALUE cStructure_decode_length_ctx(VALUE self, libbin_ctx_t *ctx, VALUE length) {
|
|
451
1144
|
if (NIL_P(length))
|
|
452
1145
|
return Qnil;
|
|
453
|
-
return
|
|
1146
|
+
return cStructure_decode_expression_ctx(self, ctx, length);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
static VALUE cStructure_decode_length(VALUE self, VALUE length) {
|
|
1150
|
+
struct cStructure_data *data;
|
|
1151
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1152
|
+
return cStructure_decode_length_ctx(self, data->active_ctx, length);
|
|
454
1153
|
}
|
|
455
1154
|
|
|
456
|
-
static inline VALUE
|
|
1155
|
+
static inline VALUE cStructure_decode_static_conditions_ctx(VALUE self, libbin_ctx_t *ctx, VALUE field) {
|
|
457
1156
|
struct cStructure_data *data;
|
|
458
1157
|
struct cField_data *field_data;
|
|
459
1158
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
@@ -464,20 +1163,26 @@ static inline VALUE cStructure_decode_static_conditions(VALUE self, VALUE field)
|
|
|
464
1163
|
data->__length = Qnil;
|
|
465
1164
|
data->__count = Qnil;
|
|
466
1165
|
if (!RTEST(field_data->sequence)) {
|
|
467
|
-
data->
|
|
468
|
-
if (!data->__offset)
|
|
469
|
-
return Qnil;
|
|
470
|
-
data->__condition = cStructure_decode_condition(self, field_data->condition);
|
|
1166
|
+
data->__condition = cStructure_decode_condition_ctx(self, ctx, field_data->condition);
|
|
471
1167
|
if (!RTEST(data->__condition))
|
|
472
1168
|
return Qnil;
|
|
473
|
-
data->
|
|
474
|
-
data->
|
|
1169
|
+
data->__offset = cStructure_decode_seek_offset_ctx(self, ctx, field_data->offset, field_data->relative_offset, field_data->align, field_data->null_offset);
|
|
1170
|
+
if (data->__offset == Qfalse)
|
|
1171
|
+
return Qnil;
|
|
1172
|
+
data->__type = cStructure_decode_type_ctx(self, ctx, field_data->type);
|
|
1173
|
+
data->__length = cStructure_decode_length_ctx(self, ctx, field_data->length);
|
|
475
1174
|
}
|
|
476
|
-
data->__count =
|
|
1175
|
+
data->__count = cStructure_decode_count_ctx(self, ctx, field_data->count);
|
|
477
1176
|
return Qtrue;
|
|
478
1177
|
}
|
|
479
1178
|
|
|
480
|
-
static
|
|
1179
|
+
static VALUE cStructure_decode_static_conditions(VALUE self, VALUE field) {
|
|
1180
|
+
struct cStructure_data *data;
|
|
1181
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1182
|
+
return cStructure_decode_static_conditions_ctx(self, data->active_ctx, field);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
static inline VALUE cStructure_decode_dynamic_conditions_ctx(VALUE self, libbin_ctx_t *ctx, VALUE field) {
|
|
481
1186
|
struct cStructure_data *data;
|
|
482
1187
|
struct cField_data *field_data;
|
|
483
1188
|
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
@@ -488,74 +1193,170 @@ static inline VALUE cStructure_decode_dynamic_conditions(VALUE self, VALUE field
|
|
|
488
1193
|
data->__condition = Qnil;
|
|
489
1194
|
data->__type = Qnil;
|
|
490
1195
|
data->__length = Qnil;
|
|
491
|
-
data->
|
|
492
|
-
if (!data->__offset)
|
|
493
|
-
return Qfalse;
|
|
494
|
-
data->__condition = cStructure_decode_condition(self, field_data->condition);
|
|
1196
|
+
data->__condition = cStructure_decode_condition_ctx(self, ctx, field_data->condition);
|
|
495
1197
|
if (!RTEST(data->__condition))
|
|
496
1198
|
return Qfalse;
|
|
497
|
-
data->
|
|
498
|
-
data->
|
|
1199
|
+
data->__offset = cStructure_decode_seek_offset_ctx(self, ctx, field_data->offset, field_data->relative_offset, field_data->align, field_data->null_offset);
|
|
1200
|
+
if (data->__offset == Qfalse)
|
|
1201
|
+
return Qfalse;
|
|
1202
|
+
data->__type = cStructure_decode_type_ctx(self, ctx, field_data->type);
|
|
1203
|
+
data->__length = cStructure_decode_length_ctx(self, ctx, field_data->length);
|
|
499
1204
|
return Qtrue;
|
|
500
1205
|
}
|
|
501
1206
|
|
|
502
|
-
static
|
|
1207
|
+
static VALUE cStructure_decode_dynamic_conditions(VALUE self, VALUE field) {
|
|
503
1208
|
struct cStructure_data *data;
|
|
504
1209
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
505
|
-
data->
|
|
506
|
-
data->__type = Qnil;
|
|
507
|
-
data->__length = Qnil;
|
|
508
|
-
data->__count = Qnil;
|
|
509
|
-
data->__offset = Qnil;
|
|
510
|
-
data->__condition = Qnil;
|
|
511
|
-
data->__value = Qnil;
|
|
512
|
-
return Qnil;
|
|
1210
|
+
return cStructure_decode_dynamic_conditions_ctx(self, data->active_ctx, field);
|
|
513
1211
|
}
|
|
514
1212
|
|
|
515
|
-
|
|
516
|
-
static
|
|
1213
|
+
/* Forward declarations for mutually recursive Structure <-> Field fast paths */
|
|
1214
|
+
static VALUE cStructure_load_fast(VALUE self, libbin_ctx_t *ctx, VALUE input_big, VALUE parent, VALUE index, uint32_t flags);
|
|
1215
|
+
static VALUE cStructure_dump_fast(VALUE self, libbin_ctx_t *ctx, VALUE output_big, VALUE parent, VALUE index, uint32_t flags);
|
|
1216
|
+
static VALUE cStructure_convert_fast(VALUE self, libbin_ctx_t *ctx, VALUE input_big, VALUE output_big, VALUE parent, VALUE index, uint32_t flags);
|
|
1217
|
+
static VALUE cStructure_singl_load_ctx(VALUE klass, libbin_ctx_t *ctx, VALUE input_big, VALUE parent, VALUE index, VALUE length, uint32_t flags);
|
|
1218
|
+
static VALUE cStructure_singl_dump_ctx(VALUE klass, VALUE value, libbin_ctx_t *ctx, VALUE output_big, VALUE parent, VALUE index, VALUE length, uint32_t flags);
|
|
1219
|
+
static VALUE cStructure_singl_convert_ctx(VALUE klass, libbin_ctx_t *ctx, VALUE input_big, VALUE output_big, VALUE parent, VALUE index, VALUE length, uint32_t flags);
|
|
517
1220
|
|
|
518
|
-
static inline VALUE
|
|
519
|
-
|
|
1221
|
+
static inline VALUE dispatch_type_load(
|
|
1222
|
+
VALUE self,
|
|
1223
|
+
struct cStructure_data *data,
|
|
1224
|
+
libbin_ctx_t *ctx,
|
|
1225
|
+
VALUE type,
|
|
1226
|
+
uint8_t known_scalar,
|
|
1227
|
+
uint8_t known_struct,
|
|
1228
|
+
VALUE iterator,
|
|
1229
|
+
VALUE length)
|
|
1230
|
+
{
|
|
1231
|
+
if (ctx && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1232
|
+
libbin_scalar_kind_t sk = known_scalar ? (libbin_scalar_kind_t)known_scalar : libbin_lookup_scalar_kind(type);
|
|
1233
|
+
if (sk != SCALAR_NONE)
|
|
1234
|
+
return libbin_scalar_load_fast(sk, &ctx->in_io, RTEST(data->__input_big), length);
|
|
1235
|
+
}
|
|
1236
|
+
if (ctx && (known_struct || (RB_TYPE_P(type, T_CLASS) && RTEST(rb_class_inherited_p(type, cStructure))))) {
|
|
1237
|
+
uint32_t tflags = libbin_get_structure_flags(type);
|
|
1238
|
+
if (!(tflags & LIBBIN_OVR_SINGL_LOAD))
|
|
1239
|
+
return cStructure_singl_load_ctx(type, ctx, data->__input_big, self, iterator, length, tflags);
|
|
1240
|
+
}
|
|
1241
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1242
|
+
VALUE res = rb_funcall(type, id_load, 5,
|
|
1243
|
+
data->__input,
|
|
1244
|
+
data->__input_big,
|
|
1245
|
+
self,
|
|
1246
|
+
iterator,
|
|
1247
|
+
length);
|
|
1248
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1249
|
+
return res;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
static inline void dispatch_type_dump(
|
|
1253
|
+
VALUE self,
|
|
1254
|
+
struct cStructure_data *data,
|
|
1255
|
+
libbin_ctx_t *ctx,
|
|
1256
|
+
VALUE type,
|
|
1257
|
+
uint8_t known_scalar,
|
|
1258
|
+
uint8_t known_struct,
|
|
1259
|
+
VALUE val,
|
|
1260
|
+
VALUE iterator,
|
|
1261
|
+
VALUE length)
|
|
1262
|
+
{
|
|
1263
|
+
if (ctx && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1264
|
+
libbin_scalar_kind_t sk = known_scalar ? (libbin_scalar_kind_t)known_scalar : libbin_lookup_scalar_kind(type);
|
|
1265
|
+
if (sk != SCALAR_NONE) {
|
|
1266
|
+
libbin_scalar_dump_fast(sk, val, &ctx->out_io, RTEST(data->__output_big), length);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
if (ctx && (known_struct || (RB_TYPE_P(type, T_CLASS) && RTEST(rb_class_inherited_p(type, cStructure))))) {
|
|
1271
|
+
uint32_t tflags = libbin_get_structure_flags(type);
|
|
1272
|
+
if (!(tflags & LIBBIN_OVR_SINGL_DUMP)) {
|
|
1273
|
+
cStructure_singl_dump_ctx(type, val, ctx, data->__output_big, self, iterator, length, tflags);
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1278
|
+
rb_funcall(type, id_dump, 6,
|
|
1279
|
+
val,
|
|
1280
|
+
data->__output,
|
|
1281
|
+
data->__output_big,
|
|
1282
|
+
self,
|
|
1283
|
+
iterator,
|
|
1284
|
+
length);
|
|
1285
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
static inline VALUE dispatch_type_convert(
|
|
1289
|
+
VALUE self,
|
|
1290
|
+
struct cStructure_data *data,
|
|
1291
|
+
libbin_ctx_t *ctx,
|
|
1292
|
+
VALUE type,
|
|
1293
|
+
uint8_t known_scalar,
|
|
1294
|
+
uint8_t known_struct,
|
|
1295
|
+
VALUE iterator,
|
|
1296
|
+
VALUE length)
|
|
1297
|
+
{
|
|
1298
|
+
if (ctx && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1299
|
+
libbin_scalar_kind_t sk = known_scalar ? (libbin_scalar_kind_t)known_scalar : libbin_lookup_scalar_kind(type);
|
|
1300
|
+
if (sk != SCALAR_NONE)
|
|
1301
|
+
return libbin_scalar_convert_fast(sk, &ctx->in_io, &ctx->out_io, RTEST(data->__input_big), RTEST(data->__output_big), length);
|
|
1302
|
+
}
|
|
1303
|
+
if (ctx && (known_struct || (RB_TYPE_P(type, T_CLASS) && RTEST(rb_class_inherited_p(type, cStructure))))) {
|
|
1304
|
+
uint32_t tflags = libbin_get_structure_flags(type);
|
|
1305
|
+
if (!(tflags & LIBBIN_OVR_SINGL_CONVERT))
|
|
1306
|
+
return cStructure_singl_convert_ctx(type, ctx, data->__input_big, data->__output_big, self, iterator, length, tflags);
|
|
1307
|
+
}
|
|
1308
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1309
|
+
VALUE res = rb_funcall(type, id_convert, 7,
|
|
1310
|
+
data->__input,
|
|
1311
|
+
data->__output,
|
|
1312
|
+
data->__input_big,
|
|
1313
|
+
data->__output_big,
|
|
1314
|
+
self,
|
|
1315
|
+
iterator,
|
|
1316
|
+
length);
|
|
1317
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1318
|
+
return res;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
static inline VALUE cStructure_convert_field_ctx(VALUE self, libbin_ctx_t *ctx, VALUE field) {
|
|
520
1322
|
struct cStructure_data *data;
|
|
521
1323
|
struct cField_data *field_data;
|
|
522
|
-
if (NIL_P(cStructure_decode_static_conditions(self, field)))
|
|
523
|
-
return Qnil;
|
|
524
1324
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
525
1325
|
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
526
1326
|
|
|
1327
|
+
if (field_data->is_static_simple && ctx) {
|
|
1328
|
+
return dispatch_type_convert(self, data, ctx, field_data->type,
|
|
1329
|
+
field_data->scalar_kind, field_data->type_is_struct, INT2FIX(0), field_data->length);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
if (NIL_P(cStructure_decode_static_conditions_ctx(self, ctx, field)))
|
|
1333
|
+
return Qnil;
|
|
1334
|
+
|
|
1335
|
+
VALUE res;
|
|
527
1336
|
if (RTEST(field_data->count)) {
|
|
528
1337
|
long count = NUM2LONG(data->__count);
|
|
1338
|
+
if (count < 0)
|
|
1339
|
+
rb_raise(rb_eArgError, "count must be non-negative: %ld", count);
|
|
529
1340
|
res = rb_ary_new_capa(count);
|
|
530
1341
|
|
|
531
1342
|
for (long i = 0; i < count; i++) {
|
|
532
1343
|
data->__iterator = LONG2NUM(i);
|
|
533
|
-
if (RTEST(
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
data->__input_big,
|
|
538
|
-
data->__output_big,
|
|
539
|
-
self,
|
|
540
|
-
data->__iterator,
|
|
541
|
-
data->__length);
|
|
1344
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1345
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_in) ? libbin_io_tell(&ctx->in_io) : -1;
|
|
1346
|
+
data->__value = dispatch_type_convert(self, data, ctx, data->__type,
|
|
1347
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__iterator, data->__length);
|
|
542
1348
|
rb_ary_store(res, i,
|
|
543
|
-
|
|
1349
|
+
cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start));
|
|
544
1350
|
} else
|
|
545
1351
|
rb_ary_store(res, i, Qnil);
|
|
546
1352
|
}
|
|
547
1353
|
} else {
|
|
548
1354
|
data->__iterator = INT2FIX(0);
|
|
549
|
-
if (RTEST(
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
data->
|
|
553
|
-
|
|
554
|
-
data->__output_big,
|
|
555
|
-
self,
|
|
556
|
-
data->__iterator,
|
|
557
|
-
data->__length);
|
|
558
|
-
res = cStructure_decode_expect(self, field_data->expect, data->__value);
|
|
1355
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1356
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_in) ? libbin_io_tell(&ctx->in_io) : -1;
|
|
1357
|
+
data->__value = dispatch_type_convert(self, data, ctx, data->__type,
|
|
1358
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__iterator, data->__length);
|
|
1359
|
+
res = cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start);
|
|
559
1360
|
} else
|
|
560
1361
|
res = Qnil;
|
|
561
1362
|
}
|
|
@@ -564,46 +1365,54 @@ static inline VALUE cStructure_convert_field(VALUE self, VALUE field) {
|
|
|
564
1365
|
return res;
|
|
565
1366
|
}
|
|
566
1367
|
|
|
567
|
-
static
|
|
568
|
-
|
|
1368
|
+
static VALUE cStructure_convert_field(VALUE self, VALUE field) {
|
|
1369
|
+
struct cStructure_data *data;
|
|
1370
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1371
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1372
|
+
VALUE res = cStructure_convert_field_ctx(self, data->active_ctx, field);
|
|
1373
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1374
|
+
return res;
|
|
1375
|
+
}
|
|
569
1376
|
|
|
570
|
-
static inline VALUE
|
|
571
|
-
VALUE res;
|
|
1377
|
+
static inline VALUE cStructure_load_field_ctx(VALUE self, libbin_ctx_t *ctx, VALUE field) {
|
|
572
1378
|
struct cStructure_data *data;
|
|
573
1379
|
struct cField_data *field_data;
|
|
574
|
-
if (NIL_P(cStructure_decode_static_conditions(self, field)))
|
|
575
|
-
return Qnil;
|
|
576
1380
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
577
1381
|
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
578
1382
|
|
|
1383
|
+
if (field_data->is_static_simple && ctx) {
|
|
1384
|
+
return dispatch_type_load(self, data, ctx, field_data->type,
|
|
1385
|
+
field_data->scalar_kind, field_data->type_is_struct, INT2FIX(0), field_data->length);
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
if (NIL_P(cStructure_decode_static_conditions_ctx(self, ctx, field)))
|
|
1389
|
+
return Qnil;
|
|
1390
|
+
|
|
1391
|
+
VALUE res;
|
|
579
1392
|
if (RTEST(field_data->count)) {
|
|
580
1393
|
long count = NUM2LONG(data->__count);
|
|
1394
|
+
if (count < 0)
|
|
1395
|
+
rb_raise(rb_eArgError, "count must be non-negative: %ld", count);
|
|
581
1396
|
res = rb_ary_new_capa(count);
|
|
582
1397
|
|
|
583
1398
|
for (long i = 0; i < count; i++) {
|
|
584
1399
|
data->__iterator = LONG2NUM(i);
|
|
585
|
-
if (RTEST(
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
data->
|
|
589
|
-
self,
|
|
590
|
-
data->__iterator,
|
|
591
|
-
data->__length);
|
|
1400
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1401
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_in) ? libbin_io_tell(&ctx->in_io) : -1;
|
|
1402
|
+
data->__value = dispatch_type_load(self, data, ctx, data->__type,
|
|
1403
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__iterator, data->__length);
|
|
592
1404
|
rb_ary_store(res, i,
|
|
593
|
-
|
|
1405
|
+
cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start));
|
|
594
1406
|
} else
|
|
595
1407
|
rb_ary_store(res, i, Qnil);
|
|
596
1408
|
}
|
|
597
1409
|
} else {
|
|
598
1410
|
data->__iterator = INT2FIX(0);
|
|
599
|
-
if (RTEST(
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
data->
|
|
603
|
-
|
|
604
|
-
data->__iterator,
|
|
605
|
-
data->__length);
|
|
606
|
-
res = cStructure_decode_expect(self, field_data->expect, data->__value);
|
|
1411
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1412
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_in) ? libbin_io_tell(&ctx->in_io) : -1;
|
|
1413
|
+
data->__value = dispatch_type_load(self, data, ctx, data->__type,
|
|
1414
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__iterator, data->__length);
|
|
1415
|
+
res = cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start);
|
|
607
1416
|
} else
|
|
608
1417
|
res = Qnil;
|
|
609
1418
|
}
|
|
@@ -611,54 +1420,72 @@ static inline VALUE cStructure_load_field(VALUE self, VALUE field) {
|
|
|
611
1420
|
return res;
|
|
612
1421
|
}
|
|
613
1422
|
|
|
614
|
-
static
|
|
615
|
-
|
|
1423
|
+
static VALUE cStructure_load_field(VALUE self, VALUE field) {
|
|
1424
|
+
struct cStructure_data *data;
|
|
1425
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1426
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1427
|
+
VALUE res = cStructure_load_field_ctx(self, data->active_ctx, field);
|
|
1428
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1429
|
+
return res;
|
|
1430
|
+
}
|
|
616
1431
|
|
|
617
|
-
static inline VALUE
|
|
1432
|
+
static inline VALUE cStructure_dump_field_ctx(VALUE self, libbin_ctx_t *ctx, VALUE values, VALUE field) {
|
|
618
1433
|
struct cStructure_data *data;
|
|
619
1434
|
struct cField_data *field_data;
|
|
620
|
-
if (NIL_P(cStructure_decode_static_conditions(self, field)))
|
|
621
|
-
return Qnil;
|
|
622
1435
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
623
1436
|
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
624
1437
|
|
|
1438
|
+
if (field_data->is_static_simple && ctx) {
|
|
1439
|
+
dispatch_type_dump(self, data, ctx, field_data->type,
|
|
1440
|
+
field_data->scalar_kind, field_data->type_is_struct, values, INT2FIX(0), field_data->length);
|
|
1441
|
+
return Qnil;
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
if (NIL_P(cStructure_decode_static_conditions_ctx(self, ctx, field)))
|
|
1445
|
+
return Qnil;
|
|
1446
|
+
|
|
625
1447
|
if (RTEST(field_data->count)) {
|
|
626
|
-
long count =
|
|
1448
|
+
long count = NUM2LONG(data->__count);
|
|
1449
|
+
if (count < 0)
|
|
1450
|
+
rb_raise(rb_eArgError, "count must be non-negative: %ld", count);
|
|
1451
|
+
if (!NIL_P(field_data->name) || !NIL_P(values)) {
|
|
1452
|
+
Check_Type(values, T_ARRAY);
|
|
1453
|
+
if (RARRAY_LEN(values) < count)
|
|
1454
|
+
rb_raise(rb_eArgError, "array too short for count: got %ld needed %ld", RARRAY_LEN(values), count);
|
|
1455
|
+
}
|
|
627
1456
|
|
|
628
1457
|
for (long i = 0; i < count; i++) {
|
|
629
1458
|
data->__iterator = LONG2NUM(i);
|
|
630
|
-
if (RTEST(
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
data->
|
|
636
|
-
data->__output_big,
|
|
637
|
-
self,
|
|
638
|
-
data->__iterator,
|
|
639
|
-
data->__length);
|
|
1459
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1460
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_out) ? libbin_io_tell(&ctx->out_io) : -1;
|
|
1461
|
+
data->__value = (NIL_P(field_data->name) && NIL_P(values)) ? Qnil : rb_ary_entry(values, i);
|
|
1462
|
+
cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start);
|
|
1463
|
+
dispatch_type_dump(self, data, ctx, data->__type,
|
|
1464
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__value, data->__iterator, data->__length);
|
|
640
1465
|
}
|
|
641
1466
|
}
|
|
642
1467
|
} else {
|
|
643
1468
|
data->__iterator = INT2FIX(0);
|
|
644
|
-
if (RTEST(
|
|
1469
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, ctx, field))) {
|
|
1470
|
+
ptrdiff_t field_start = (!NIL_P(field_data->expect) && ctx && ctx->has_out) ? libbin_io_tell(&ctx->out_io) : -1;
|
|
645
1471
|
data->__value = values;
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
data->__value,
|
|
649
|
-
data->__output,
|
|
650
|
-
data->__output_big,
|
|
651
|
-
self,
|
|
652
|
-
data->__iterator,
|
|
653
|
-
data->__length);
|
|
1472
|
+
cStructure_decode_expect_ctx(self, ctx, field_data->expect, data->__value, field_start);
|
|
1473
|
+
dispatch_type_dump(self, data, ctx, data->__type,
|
|
1474
|
+
field_data->scalar_kind, field_data->type_is_struct, data->__value, data->__iterator, data->__length);
|
|
654
1475
|
}
|
|
655
1476
|
}
|
|
656
1477
|
cStructure_restore_context(self);
|
|
657
1478
|
return Qnil;
|
|
658
1479
|
}
|
|
659
1480
|
|
|
660
|
-
static
|
|
661
|
-
|
|
1481
|
+
static VALUE cStructure_dump_field(VALUE self, VALUE values, VALUE field) {
|
|
1482
|
+
struct cStructure_data *data;
|
|
1483
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1484
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1485
|
+
VALUE res = cStructure_dump_field_ctx(self, data->active_ctx, values, field);
|
|
1486
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1487
|
+
return res;
|
|
1488
|
+
}
|
|
662
1489
|
|
|
663
1490
|
static inline VALUE cStructure_shape_field(
|
|
664
1491
|
VALUE self,
|
|
@@ -669,38 +1496,48 @@ static inline VALUE cStructure_shape_field(
|
|
|
669
1496
|
VALUE res = Qnil;
|
|
670
1497
|
struct cStructure_data *data;
|
|
671
1498
|
struct cField_data *field_data;
|
|
672
|
-
if (NIL_P(
|
|
1499
|
+
if (NIL_P(cStructure_decode_static_conditions_ctx(self, NULL, field)))
|
|
673
1500
|
return Qnil;
|
|
674
1501
|
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
675
1502
|
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
676
1503
|
|
|
677
1504
|
if (RTEST(field_data->count)) {
|
|
678
|
-
long count =
|
|
1505
|
+
long count = NUM2LONG(data->__count);
|
|
1506
|
+
if (count < 0)
|
|
1507
|
+
rb_raise(rb_eArgError, "count must be non-negative: %ld", count);
|
|
1508
|
+
if (!NIL_P(field_data->name) || !NIL_P(values)) {
|
|
1509
|
+
Check_Type(values, T_ARRAY);
|
|
1510
|
+
if (RARRAY_LEN(values) < count)
|
|
1511
|
+
rb_raise(rb_eArgError, "array too short for count: got %ld needed %ld", RARRAY_LEN(values), count);
|
|
1512
|
+
}
|
|
679
1513
|
res = rb_ary_new_capa(count);
|
|
680
1514
|
|
|
681
1515
|
for (long i = 0; i < count; i++) {
|
|
682
1516
|
data->__iterator = LONG2NUM(i);
|
|
683
|
-
if (RTEST(
|
|
1517
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, NULL, field))) {
|
|
1518
|
+
VALUE elem_val = (NIL_P(field_data->name) && NIL_P(values)) ? Qnil : rb_ary_entry(values, i);
|
|
684
1519
|
VALUE shape = rb_funcall(data->__type, id_shape, 6,
|
|
685
|
-
|
|
1520
|
+
elem_val,
|
|
686
1521
|
data->__cur_position,
|
|
687
1522
|
self,
|
|
688
1523
|
data->__iterator,
|
|
689
1524
|
kind,
|
|
690
1525
|
data->__length);
|
|
691
1526
|
rb_ary_store(res, i, shape);
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
1527
|
+
if (!NIL_P(shape)) {
|
|
1528
|
+
VALUE last = rb_funcall(shape, id_last, 0);
|
|
1529
|
+
if (RTEST(last)) {
|
|
1530
|
+
ptrdiff_t pos = NUM2LL(last);
|
|
1531
|
+
if (pos >= 0)
|
|
1532
|
+
data->__cur_position = LL2NUM(pos + 1);
|
|
1533
|
+
}
|
|
697
1534
|
}
|
|
698
1535
|
}
|
|
699
1536
|
}
|
|
700
1537
|
res = rb_class_new_instance(1, &res, kind);
|
|
701
1538
|
} else {
|
|
702
1539
|
data->__iterator = INT2FIX(0);
|
|
703
|
-
if (RTEST(
|
|
1540
|
+
if (RTEST(cStructure_decode_dynamic_conditions_ctx(self, NULL, field))) {
|
|
704
1541
|
res = rb_funcall(data->__type, id_shape, 6,
|
|
705
1542
|
values,
|
|
706
1543
|
data->__cur_position,
|
|
@@ -708,11 +1545,13 @@ static inline VALUE cStructure_shape_field(
|
|
|
708
1545
|
data->__iterator,
|
|
709
1546
|
kind,
|
|
710
1547
|
data->__length);
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
1548
|
+
if (!NIL_P(res)) {
|
|
1549
|
+
VALUE last = rb_funcall(res, id_last, 0);
|
|
1550
|
+
if (RTEST(last)) {
|
|
1551
|
+
ptrdiff_t pos = NUM2LL(last);
|
|
1552
|
+
if (pos >= 0)
|
|
1553
|
+
data->__cur_position = LL2NUM(pos + 1);
|
|
1554
|
+
}
|
|
716
1555
|
}
|
|
717
1556
|
}
|
|
718
1557
|
}
|
|
@@ -720,103 +1559,283 @@ static inline VALUE cStructure_shape_field(
|
|
|
720
1559
|
return res;
|
|
721
1560
|
}
|
|
722
1561
|
|
|
723
|
-
static ID id_fields;
|
|
724
|
-
|
|
725
1562
|
struct fields_state {
|
|
726
1563
|
VALUE self;
|
|
727
1564
|
VALUE fields;
|
|
728
1565
|
VALUE field;
|
|
729
1566
|
};
|
|
730
1567
|
|
|
731
|
-
static
|
|
1568
|
+
static VALUE cStructure_fields_rescue(VALUE state_p, VALUE exception) {
|
|
732
1569
|
struct fields_state *state = (struct fields_state *)state_p;
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
1570
|
+
libbin_attach_error_context(exception, state->self, state->field, NULL, 0, Qnil);
|
|
1571
|
+
libbin_print_frame_error(state->self, state->field);
|
|
1572
|
+
rb_exc_raise(exception);
|
|
1573
|
+
return state->self;
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
static void libbin_apply_stride(VALUE self, libbin_ctx_t *ctx, uint32_t flags, uint8_t mode, VALUE stride_expr) {
|
|
1577
|
+
long long stride_val = NUM2LL(cStructure_decode_expression_ctx(self, ctx, stride_expr));
|
|
1578
|
+
if (stride_val <= 0)
|
|
1579
|
+
return;
|
|
1580
|
+
struct cStructure_data *data;
|
|
1581
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1582
|
+
ptrdiff_t start_pos = NUM2LL(data->__position);
|
|
1583
|
+
ptrdiff_t target = start_pos + (ptrdiff_t)stride_val;
|
|
1584
|
+
if (mode == 0) {
|
|
1585
|
+
ptrdiff_t cur = libbin_io_tell(&ctx->in_io);
|
|
1586
|
+
if (cur < target) {
|
|
1587
|
+
libbin_io_skip_read(&ctx->in_io, (size_t)(target - cur));
|
|
1588
|
+
} else if (cur > target) {
|
|
1589
|
+
if (!ctx->in_io.non_seekable && (flags & LIBBIN_HAS_OFFSET_FIELD))
|
|
1590
|
+
libbin_io_seek(&ctx->in_io, target);
|
|
1591
|
+
else
|
|
1592
|
+
rb_raise(eLibBinError, "structure %"PRIsVALUE" exceeded stride %lld (consumed %td bytes)",
|
|
1593
|
+
rb_obj_class(self), stride_val, cur - start_pos);
|
|
1594
|
+
}
|
|
1595
|
+
} else if (mode == 1) {
|
|
1596
|
+
ptrdiff_t cur = libbin_io_tell(&ctx->out_io);
|
|
1597
|
+
if (cur < target) {
|
|
1598
|
+
libbin_io_write_zeros(&ctx->out_io, (size_t)(target - cur));
|
|
1599
|
+
} else if (cur > target) {
|
|
1600
|
+
if (!ctx->out_io.non_seekable && (flags & LIBBIN_HAS_OFFSET_FIELD))
|
|
1601
|
+
libbin_io_seek(&ctx->out_io, target);
|
|
1602
|
+
else
|
|
1603
|
+
rb_raise(eLibBinError, "structure %"PRIsVALUE" exceeded stride %lld (consumed %td bytes)",
|
|
1604
|
+
rb_obj_class(self), stride_val, cur - start_pos);
|
|
1605
|
+
}
|
|
744
1606
|
} else {
|
|
1607
|
+
ptrdiff_t cur = libbin_io_tell(&ctx->in_io);
|
|
1608
|
+
if (cur < target) {
|
|
1609
|
+
libbin_copy_bytes(&ctx->in_io, &ctx->out_io, (size_t)(target - cur));
|
|
1610
|
+
} else if (cur > target) {
|
|
1611
|
+
if (!ctx->in_io.non_seekable && !ctx->out_io.non_seekable && (flags & LIBBIN_HAS_OFFSET_FIELD)) {
|
|
1612
|
+
libbin_io_seek(&ctx->in_io, target);
|
|
1613
|
+
libbin_io_seek(&ctx->out_io, target);
|
|
1614
|
+
} else {
|
|
1615
|
+
rb_raise(eLibBinError, "structure %"PRIsVALUE" exceeded stride %lld (consumed %td bytes)",
|
|
1616
|
+
rb_obj_class(self), stride_val, cur - start_pos);
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
static inline void cStructure_load_fields_ctx(VALUE self, libbin_ctx_t *ctx, uint32_t flags) {
|
|
1623
|
+
VALUE klass = rb_obj_class(self);
|
|
1624
|
+
if (CLASS_OF(self) != klass) {
|
|
1625
|
+
flags |= LIBBIN_OVR_ACCESSORS | LIBBIN_OVR_LOAD_FIELD;
|
|
1626
|
+
flags &= ~LIBBIN_ALL_SCALAR_LEAF;
|
|
1627
|
+
}
|
|
1628
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1629
|
+
int use_ivar = !(flags & LIBBIN_OVR_ACCESSORS);
|
|
1630
|
+
int use_fast_field = !(flags & LIBBIN_OVR_LOAD_FIELD);
|
|
1631
|
+
|
|
1632
|
+
VALUE fidx = ctx->pending_index;
|
|
1633
|
+
ctx->pending_index = Qnil;
|
|
1634
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 0, fidx);
|
|
1635
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1636
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1637
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1638
|
+
ctx->frames[depth].field = field;
|
|
1639
|
+
struct cField_data *field_data;
|
|
1640
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
1641
|
+
VALUE val;
|
|
1642
|
+
if (use_fast_field) {
|
|
1643
|
+
val = cStructure_load_field_ctx(self, ctx, field);
|
|
1644
|
+
} else {
|
|
1645
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1646
|
+
val = rb_funcall(self, id___load_field, 1, field);
|
|
1647
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1648
|
+
}
|
|
1649
|
+
if (!NIL_P(field_data->name)) {
|
|
1650
|
+
if (use_ivar) {
|
|
1651
|
+
rb_ivar_set(self, field_data->ivar, val);
|
|
1652
|
+
} else {
|
|
1653
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1654
|
+
rb_funcall(self, field_data->setter, 1, val);
|
|
1655
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
if (flags & LIBBIN_HAS_STRIDE) {
|
|
1660
|
+
VALUE stride = rb_attr_get(klass, id_stride);
|
|
1661
|
+
if (RTEST(stride))
|
|
1662
|
+
libbin_apply_stride(self, ctx, flags, 0, stride);
|
|
1663
|
+
}
|
|
1664
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
static inline void cStructure_dump_fields_ctx(VALUE self, libbin_ctx_t *ctx, uint32_t flags) {
|
|
1668
|
+
VALUE klass = rb_obj_class(self);
|
|
1669
|
+
if (CLASS_OF(self) != klass) {
|
|
1670
|
+
flags |= LIBBIN_OVR_ACCESSORS | LIBBIN_OVR_DUMP_FIELD;
|
|
1671
|
+
flags &= ~LIBBIN_ALL_SCALAR_LEAF;
|
|
1672
|
+
}
|
|
1673
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1674
|
+
int use_ivar = !(flags & LIBBIN_OVR_ACCESSORS);
|
|
1675
|
+
int use_fast_field = !(flags & LIBBIN_OVR_DUMP_FIELD);
|
|
1676
|
+
|
|
1677
|
+
VALUE fidx = ctx->pending_index;
|
|
1678
|
+
ctx->pending_index = Qnil;
|
|
1679
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 1, fidx);
|
|
1680
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1681
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1682
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1683
|
+
ctx->frames[depth].field = field;
|
|
745
1684
|
struct cField_data *field_data;
|
|
746
|
-
TypedData_Get_Struct(
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
1685
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
1686
|
+
VALUE val;
|
|
1687
|
+
if (NIL_P(field_data->name)) {
|
|
1688
|
+
val = Qnil;
|
|
1689
|
+
} else if (use_ivar) {
|
|
1690
|
+
val = rb_ivar_get(self, field_data->ivar);
|
|
1691
|
+
} else {
|
|
1692
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1693
|
+
val = rb_funcall(self, field_data->getter, 0);
|
|
1694
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1695
|
+
}
|
|
1696
|
+
if (use_fast_field) {
|
|
1697
|
+
cStructure_dump_field_ctx(self, ctx, val, field);
|
|
1698
|
+
} else {
|
|
1699
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1700
|
+
rb_funcall(self, id___dump_field, 2, val, field);
|
|
1701
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1702
|
+
}
|
|
755
1703
|
}
|
|
756
|
-
|
|
757
|
-
|
|
1704
|
+
if (flags & LIBBIN_HAS_STRIDE) {
|
|
1705
|
+
VALUE stride = rb_attr_get(klass, id_stride);
|
|
1706
|
+
if (RTEST(stride))
|
|
1707
|
+
libbin_apply_stride(self, ctx, flags, 1, stride);
|
|
1708
|
+
}
|
|
1709
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
758
1710
|
}
|
|
759
1711
|
|
|
760
|
-
static inline
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1712
|
+
static inline void cStructure_convert_fields_ctx(VALUE self, libbin_ctx_t *ctx, uint32_t flags) {
|
|
1713
|
+
VALUE klass = rb_obj_class(self);
|
|
1714
|
+
if (CLASS_OF(self) != klass) {
|
|
1715
|
+
flags |= LIBBIN_OVR_ACCESSORS | LIBBIN_OVR_CONVERT_FIELD;
|
|
1716
|
+
flags &= ~LIBBIN_ALL_SCALAR_LEAF;
|
|
1717
|
+
}
|
|
1718
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1719
|
+
int use_ivar = !(flags & LIBBIN_OVR_ACCESSORS);
|
|
1720
|
+
int use_fast_field = !(flags & LIBBIN_OVR_CONVERT_FIELD);
|
|
1721
|
+
|
|
1722
|
+
VALUE fidx = ctx->pending_index;
|
|
1723
|
+
ctx->pending_index = Qnil;
|
|
1724
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 2, fidx);
|
|
1725
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1726
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1727
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1728
|
+
ctx->frames[depth].field = field;
|
|
765
1729
|
struct cField_data *field_data;
|
|
766
|
-
TypedData_Get_Struct(
|
|
767
|
-
|
|
1730
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, field_data);
|
|
1731
|
+
VALUE val;
|
|
1732
|
+
if (use_fast_field) {
|
|
1733
|
+
val = cStructure_convert_field_ctx(self, ctx, field);
|
|
1734
|
+
} else {
|
|
1735
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1736
|
+
val = rb_funcall(self, id___convert_field, 1, field);
|
|
1737
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1738
|
+
}
|
|
1739
|
+
if (!NIL_P(field_data->name)) {
|
|
1740
|
+
if (use_ivar) {
|
|
1741
|
+
rb_ivar_set(self, field_data->ivar, val);
|
|
1742
|
+
} else {
|
|
1743
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1744
|
+
rb_funcall(self, field_data->setter, 1, val);
|
|
1745
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
768
1748
|
}
|
|
769
|
-
|
|
1749
|
+
if (flags & LIBBIN_HAS_STRIDE) {
|
|
1750
|
+
VALUE stride = rb_attr_get(klass, id_stride);
|
|
1751
|
+
if (RTEST(stride))
|
|
1752
|
+
libbin_apply_stride(self, ctx, flags, 2, stride);
|
|
1753
|
+
}
|
|
1754
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
770
1755
|
}
|
|
771
1756
|
|
|
772
|
-
|
|
1757
|
+
struct top_fields_args {
|
|
1758
|
+
VALUE self;
|
|
1759
|
+
libbin_ctx_t *ctx;
|
|
1760
|
+
uint32_t flags;
|
|
1761
|
+
uint8_t mode;
|
|
1762
|
+
};
|
|
773
1763
|
|
|
774
|
-
static VALUE
|
|
775
|
-
struct
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
1764
|
+
static VALUE top_fields_body(VALUE args_p) {
|
|
1765
|
+
struct top_fields_args *a = (struct top_fields_args *)args_p;
|
|
1766
|
+
if (a->mode == 0)
|
|
1767
|
+
cStructure_load_fields_ctx(a->self, a->ctx, a->flags);
|
|
1768
|
+
else if (a->mode == 1)
|
|
1769
|
+
cStructure_dump_fields_ctx(a->self, a->ctx, a->flags);
|
|
1770
|
+
else
|
|
1771
|
+
cStructure_convert_fields_ctx(a->self, a->ctx, a->flags);
|
|
1772
|
+
return a->self;
|
|
779
1773
|
}
|
|
780
1774
|
|
|
781
|
-
static
|
|
782
|
-
struct
|
|
783
|
-
|
|
784
|
-
for (long i = 0; i < RARRAY_LEN(state->fields); i++) {
|
|
785
|
-
state->field = rb_ary_entry(state->fields, i);
|
|
786
|
-
struct cField_data *field_data;
|
|
787
|
-
TypedData_Get_Struct(state->field, struct cField_data, &cField_type, field_data);
|
|
788
|
-
rb_funcall(state->self, id___dump_field, 2, rb_funcall(state->self, field_data->getter, 0), state->field);
|
|
789
|
-
}
|
|
790
|
-
return state->self;
|
|
1775
|
+
static VALUE top_fields_rescue_wrapper(VALUE args_p) {
|
|
1776
|
+
struct top_fields_args *a = (struct top_fields_args *)args_p;
|
|
1777
|
+
return rb_rescue(top_fields_body, args_p, libbin_ctx_rescue, (VALUE)a->ctx);
|
|
791
1778
|
}
|
|
792
1779
|
|
|
793
|
-
static
|
|
1780
|
+
static VALUE top_fields_ensure(VALUE args_p) {
|
|
1781
|
+
struct top_fields_args *a = (struct top_fields_args *)args_p;
|
|
1782
|
+
struct cStructure_data *data;
|
|
1783
|
+
TypedData_Get_Struct(a->self, struct cStructure_data, &cStructure_type, data);
|
|
1784
|
+
data->active_ctx = NULL;
|
|
1785
|
+
return libbin_ctx_ensure((VALUE)a->ctx);
|
|
1786
|
+
}
|
|
794
1787
|
|
|
795
|
-
static VALUE
|
|
796
|
-
struct
|
|
797
|
-
|
|
798
|
-
|
|
1788
|
+
static VALUE cStructure_load_fields(VALUE self) {
|
|
1789
|
+
struct cStructure_data *data;
|
|
1790
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1791
|
+
uint32_t flags = libbin_get_structure_flags(rb_obj_class(self));
|
|
1792
|
+
if (data->active_ctx) {
|
|
1793
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1794
|
+
cStructure_load_fields_ctx(self, data->active_ctx, flags);
|
|
1795
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1796
|
+
return self;
|
|
1797
|
+
}
|
|
1798
|
+
libbin_ctx_t ctx;
|
|
1799
|
+
libbin_ctx_init_load(&ctx, data->__input);
|
|
1800
|
+
data->active_ctx = &ctx;
|
|
1801
|
+
struct top_fields_args args = { self, &ctx, flags, 0 };
|
|
1802
|
+
rb_ensure(top_fields_rescue_wrapper, (VALUE)&args, top_fields_ensure, (VALUE)&args);
|
|
799
1803
|
return self;
|
|
800
1804
|
}
|
|
801
1805
|
|
|
802
|
-
static
|
|
803
|
-
struct
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
1806
|
+
static VALUE cStructure_dump_fields(VALUE self) {
|
|
1807
|
+
struct cStructure_data *data;
|
|
1808
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1809
|
+
uint32_t flags = libbin_get_structure_flags(rb_obj_class(self));
|
|
1810
|
+
if (data->active_ctx) {
|
|
1811
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1812
|
+
cStructure_dump_fields_ctx(self, data->active_ctx, flags);
|
|
1813
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1814
|
+
return self;
|
|
810
1815
|
}
|
|
811
|
-
|
|
1816
|
+
libbin_ctx_t ctx;
|
|
1817
|
+
libbin_ctx_init_dump(&ctx, data->__output);
|
|
1818
|
+
data->active_ctx = &ctx;
|
|
1819
|
+
struct top_fields_args args = { self, &ctx, flags, 1 };
|
|
1820
|
+
rb_ensure(top_fields_rescue_wrapper, (VALUE)&args, top_fields_ensure, (VALUE)&args);
|
|
1821
|
+
return self;
|
|
812
1822
|
}
|
|
813
1823
|
|
|
814
|
-
static ID id___convert_fields;
|
|
815
|
-
|
|
816
1824
|
static VALUE cStructure_convert_fields(VALUE self) {
|
|
817
|
-
struct
|
|
818
|
-
|
|
819
|
-
|
|
1825
|
+
struct cStructure_data *data;
|
|
1826
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1827
|
+
uint32_t flags = libbin_get_structure_flags(rb_obj_class(self));
|
|
1828
|
+
if (data->active_ctx) {
|
|
1829
|
+
libbin_ctx_sync_from_ruby(data->active_ctx);
|
|
1830
|
+
cStructure_convert_fields_ctx(self, data->active_ctx, flags);
|
|
1831
|
+
libbin_ctx_sync_to_ruby(data->active_ctx);
|
|
1832
|
+
return self;
|
|
1833
|
+
}
|
|
1834
|
+
libbin_ctx_t ctx;
|
|
1835
|
+
libbin_ctx_init_convert(&ctx, data->__input, data->__output);
|
|
1836
|
+
data->active_ctx = &ctx;
|
|
1837
|
+
struct top_fields_args args = { self, &ctx, flags, 2 };
|
|
1838
|
+
rb_ensure(top_fields_rescue_wrapper, (VALUE)&args, top_fields_ensure, (VALUE)&args);
|
|
820
1839
|
return self;
|
|
821
1840
|
}
|
|
822
1841
|
|
|
@@ -827,167 +1846,735 @@ struct shape_fields_state {
|
|
|
827
1846
|
VALUE kind;
|
|
828
1847
|
};
|
|
829
1848
|
|
|
830
|
-
static
|
|
1849
|
+
static VALUE cStructure_shape_fields_wrapper(VALUE state_p) {
|
|
831
1850
|
struct shape_fields_state *state = (struct shape_fields_state *)state_p;
|
|
832
|
-
state->fields =
|
|
1851
|
+
state->fields = rb_attr_get(rb_obj_class(state->self), id_fields);
|
|
833
1852
|
VALUE members = rb_hash_new();
|
|
834
1853
|
for (long i = 0; i < RARRAY_LEN(state->fields); i++) {
|
|
835
1854
|
state->field = rb_ary_entry(state->fields, i);
|
|
836
1855
|
struct cField_data *field_data;
|
|
837
1856
|
TypedData_Get_Struct(state->field, struct cField_data, &cField_type, field_data);
|
|
838
|
-
|
|
839
|
-
|
|
1857
|
+
if (NIL_P(field_data->name)) {
|
|
1858
|
+
VALUE sh = rb_funcall(state->self, id___shape_field, 3, Qnil, state->kind, state->field);
|
|
1859
|
+
if (!NIL_P(sh)) {
|
|
1860
|
+
VALUE pad_key = ID2SYM(rb_intern_str(rb_sprintf("__pad_%ld", i)));
|
|
1861
|
+
rb_hash_aset(members, pad_key, sh);
|
|
1862
|
+
}
|
|
1863
|
+
} else {
|
|
1864
|
+
rb_hash_aset(members, ID2SYM(field_data->getter),
|
|
1865
|
+
rb_funcall(state->self, id___shape_field, 3, rb_funcall(state->self, field_data->getter, 0), state->kind, state->field));
|
|
1866
|
+
}
|
|
840
1867
|
}
|
|
841
1868
|
return members;
|
|
842
1869
|
}
|
|
843
1870
|
|
|
844
|
-
static ID id___shape_fields;
|
|
845
|
-
|
|
846
1871
|
static VALUE cStructure_shape_fields(VALUE self, VALUE kind) {
|
|
847
1872
|
struct shape_fields_state state = {self, Qnil, Qnil, kind};
|
|
848
1873
|
return rb_rescue(&cStructure_shape_fields_wrapper, (VALUE)&state,
|
|
849
1874
|
&cStructure_fields_rescue, (VALUE)&state);
|
|
850
1875
|
}
|
|
851
1876
|
|
|
852
|
-
static
|
|
1877
|
+
static VALUE cStructure_load_fast(VALUE self, libbin_ctx_t *ctx, VALUE input_big, VALUE parent, VALUE index, uint32_t flags) {
|
|
1878
|
+
VALUE klass = rb_obj_class(self);
|
|
1879
|
+
if (CLASS_OF(self) != klass)
|
|
1880
|
+
flags = ~0u;
|
|
1881
|
+
if ((flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_LOAD_MASK) && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1882
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1883
|
+
int ibig = RTEST(input_big);
|
|
1884
|
+
VALUE fidx = !NIL_P(index) ? index : ctx->pending_index;
|
|
1885
|
+
ctx->pending_index = Qnil;
|
|
1886
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 0, fidx);
|
|
1887
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1888
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1889
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1890
|
+
ctx->frames[depth].field = field;
|
|
1891
|
+
struct cField_data *fd;
|
|
1892
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, fd);
|
|
1893
|
+
VALUE val = libbin_scalar_load_fast((libbin_scalar_kind_t)fd->scalar_kind, &ctx->in_io, ibig, fd->length);
|
|
1894
|
+
if (!NIL_P(fd->name))
|
|
1895
|
+
rb_ivar_set(self, fd->ivar, val);
|
|
1896
|
+
}
|
|
1897
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
1898
|
+
return self;
|
|
1899
|
+
}
|
|
1900
|
+
if (!(flags & (LIBBIN_OVR_SET_LOAD | LIBBIN_OVR_LOAD_FIELDS))) {
|
|
1901
|
+
struct cStructure_data *data;
|
|
1902
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1903
|
+
if (!NIL_P(index))
|
|
1904
|
+
ctx->pending_index = index;
|
|
1905
|
+
data->__input = ctx->in_io.io;
|
|
1906
|
+
data->__input_big = input_big;
|
|
1907
|
+
data->__parent = parent;
|
|
1908
|
+
data->__index = index;
|
|
1909
|
+
data->__position = LL2NUM(libbin_io_tell(&ctx->in_io));
|
|
1910
|
+
data->__cur_position = data->__position;
|
|
1911
|
+
data->active_ctx = ctx;
|
|
1912
|
+
cStructure_load_fields_ctx(self, ctx, flags);
|
|
1913
|
+
libbin_unset_frame_state(self, 0);
|
|
1914
|
+
return self;
|
|
1915
|
+
}
|
|
1916
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1917
|
+
rb_funcall(self, id___load, 4, ctx->in_io.io, input_big, parent, index);
|
|
1918
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1919
|
+
return self;
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
static VALUE cStructure_dump_fast(VALUE self, libbin_ctx_t *ctx, VALUE output_big, VALUE parent, VALUE index, uint32_t flags) {
|
|
1923
|
+
VALUE klass = rb_obj_class(self);
|
|
1924
|
+
if (CLASS_OF(self) != klass)
|
|
1925
|
+
flags = ~0u;
|
|
1926
|
+
if ((flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_DUMP_MASK) && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1927
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1928
|
+
int obig = RTEST(output_big);
|
|
1929
|
+
VALUE fidx = !NIL_P(index) ? index : ctx->pending_index;
|
|
1930
|
+
ctx->pending_index = Qnil;
|
|
1931
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 1, fidx);
|
|
1932
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1933
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1934
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1935
|
+
ctx->frames[depth].field = field;
|
|
1936
|
+
struct cField_data *fd;
|
|
1937
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, fd);
|
|
1938
|
+
VALUE val = NIL_P(fd->name) ? Qnil : rb_ivar_get(self, fd->ivar);
|
|
1939
|
+
libbin_scalar_dump_fast((libbin_scalar_kind_t)fd->scalar_kind, val, &ctx->out_io, obig, fd->length);
|
|
1940
|
+
}
|
|
1941
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
1942
|
+
return self;
|
|
1943
|
+
}
|
|
1944
|
+
if (!(flags & (LIBBIN_OVR_SET_DUMP | LIBBIN_OVR_DUMP_FIELDS))) {
|
|
1945
|
+
struct cStructure_data *data;
|
|
1946
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1947
|
+
if (!NIL_P(index))
|
|
1948
|
+
ctx->pending_index = index;
|
|
1949
|
+
data->__output = ctx->out_io.io;
|
|
1950
|
+
data->__output_big = output_big;
|
|
1951
|
+
data->__parent = parent;
|
|
1952
|
+
data->__index = index;
|
|
1953
|
+
data->__position = LL2NUM(libbin_io_tell(&ctx->out_io));
|
|
1954
|
+
data->__cur_position = data->__position;
|
|
1955
|
+
data->active_ctx = ctx;
|
|
1956
|
+
cStructure_dump_fields_ctx(self, ctx, flags);
|
|
1957
|
+
libbin_unset_frame_state(self, 1);
|
|
1958
|
+
return self;
|
|
1959
|
+
}
|
|
1960
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
1961
|
+
rb_funcall(self, id___dump, 4, ctx->out_io.io, output_big, parent, index);
|
|
1962
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
1963
|
+
return self;
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
static VALUE cStructure_convert_fast(VALUE self, libbin_ctx_t *ctx, VALUE input_big, VALUE output_big, VALUE parent, VALUE index, uint32_t flags) {
|
|
1967
|
+
VALUE klass = rb_obj_class(self);
|
|
1968
|
+
if (CLASS_OF(self) != klass)
|
|
1969
|
+
flags = ~0u;
|
|
1970
|
+
if ((flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_CONVERT_MASK) && !LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
1971
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
1972
|
+
int ibig = RTEST(input_big);
|
|
1973
|
+
int obig = RTEST(output_big);
|
|
1974
|
+
VALUE fidx = !NIL_P(index) ? index : ctx->pending_index;
|
|
1975
|
+
ctx->pending_index = Qnil;
|
|
1976
|
+
size_t depth = libbin_ctx_push_frame(ctx, self, 2, fidx);
|
|
1977
|
+
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
|
1978
|
+
VALUE field = rb_ary_entry(fields, i);
|
|
1979
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
1980
|
+
ctx->frames[depth].field = field;
|
|
1981
|
+
struct cField_data *fd;
|
|
1982
|
+
TypedData_Get_Struct(field, struct cField_data, &cField_type, fd);
|
|
1983
|
+
VALUE val = libbin_scalar_convert_fast((libbin_scalar_kind_t)fd->scalar_kind, &ctx->in_io, &ctx->out_io, ibig, obig, fd->length);
|
|
1984
|
+
if (!NIL_P(fd->name))
|
|
1985
|
+
rb_ivar_set(self, fd->ivar, val);
|
|
1986
|
+
}
|
|
1987
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
1988
|
+
return self;
|
|
1989
|
+
}
|
|
1990
|
+
if (!(flags & (LIBBIN_OVR_SET_CONVERT | LIBBIN_OVR_CONVERT_FIELDS))) {
|
|
1991
|
+
struct cStructure_data *data;
|
|
1992
|
+
TypedData_Get_Struct(self, struct cStructure_data, &cStructure_type, data);
|
|
1993
|
+
if (!NIL_P(index))
|
|
1994
|
+
ctx->pending_index = index;
|
|
1995
|
+
data->__input = ctx->in_io.io;
|
|
1996
|
+
data->__output = ctx->out_io.io;
|
|
1997
|
+
data->__input_big = input_big;
|
|
1998
|
+
data->__output_big = output_big;
|
|
1999
|
+
data->__parent = parent;
|
|
2000
|
+
data->__index = index;
|
|
2001
|
+
data->__position = LL2NUM(libbin_io_tell(&ctx->in_io));
|
|
2002
|
+
data->__cur_position = data->__position;
|
|
2003
|
+
data->active_ctx = ctx;
|
|
2004
|
+
cStructure_convert_fields_ctx(self, ctx, flags);
|
|
2005
|
+
libbin_unset_frame_state(self, 2);
|
|
2006
|
+
return self;
|
|
2007
|
+
}
|
|
2008
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2009
|
+
rb_funcall(self, id___convert, 6, ctx->in_io.io, ctx->out_io.io, input_big, output_big, parent, index);
|
|
2010
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2011
|
+
return self;
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
static VALUE cStructure_load_body(VALUE self) {
|
|
2015
|
+
return rb_funcall(self, id___load_fields, 0);
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
static VALUE cStructure_load_ensure(VALUE self) {
|
|
2019
|
+
return rb_funcall(self, id___unset_load_state, 0);
|
|
2020
|
+
}
|
|
853
2021
|
|
|
854
|
-
|
|
855
|
-
VALUE
|
|
2022
|
+
struct top_inst_args {
|
|
2023
|
+
VALUE self;
|
|
2024
|
+
libbin_ctx_t *ctx;
|
|
856
2025
|
VALUE input_big;
|
|
2026
|
+
VALUE output_big;
|
|
857
2027
|
VALUE parent;
|
|
858
2028
|
VALUE index;
|
|
2029
|
+
uint32_t flags;
|
|
2030
|
+
uint8_t mode;
|
|
2031
|
+
};
|
|
2032
|
+
|
|
2033
|
+
static VALUE top_inst_body(VALUE args_p) {
|
|
2034
|
+
struct top_inst_args *a = (struct top_inst_args *)args_p;
|
|
2035
|
+
if (a->mode == 0)
|
|
2036
|
+
return cStructure_load_fast(a->self, a->ctx, a->input_big, a->parent, a->index, a->flags);
|
|
2037
|
+
else if (a->mode == 1)
|
|
2038
|
+
return cStructure_dump_fast(a->self, a->ctx, a->output_big, a->parent, a->index, a->flags);
|
|
2039
|
+
else
|
|
2040
|
+
return cStructure_convert_fast(a->self, a->ctx, a->input_big, a->output_big, a->parent, a->index, a->flags);
|
|
2041
|
+
}
|
|
2042
|
+
|
|
2043
|
+
static VALUE top_inst_rescue_wrapper(VALUE args_p) {
|
|
2044
|
+
struct top_inst_args *a = (struct top_inst_args *)args_p;
|
|
2045
|
+
return rb_rescue(top_inst_body, args_p, libbin_ctx_rescue, (VALUE)a->ctx);
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
static VALUE cStructure_load(int argc, VALUE *argv, VALUE self) {
|
|
2049
|
+
VALUE input, input_big, parent, index;
|
|
859
2050
|
rb_scan_args(argc, argv, "22", &input, &input_big, &parent, &index);
|
|
2051
|
+
uint32_t flags = (CLASS_OF(self) == rb_obj_class(self)) ? libbin_get_structure_flags(rb_obj_class(self)) : ~0u;
|
|
2052
|
+
if (!(flags & (LIBBIN_OVR_SET_LOAD | LIBBIN_OVR_LOAD_FIELDS))) {
|
|
2053
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
|
|
2054
|
+
if (pctx) {
|
|
2055
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2056
|
+
cStructure_load_fast(self, pctx, input_big, parent, index, flags);
|
|
2057
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2058
|
+
return self;
|
|
2059
|
+
}
|
|
2060
|
+
libbin_ctx_t ctx;
|
|
2061
|
+
libbin_ctx_init_load(&ctx, input);
|
|
2062
|
+
struct top_inst_args args = { self, &ctx, input_big, Qnil, parent, index, flags, 0 };
|
|
2063
|
+
rb_ensure(top_inst_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
2064
|
+
return self;
|
|
2065
|
+
}
|
|
860
2066
|
rb_funcall(self, id___set_load_state, 4, input, input_big, parent, index);
|
|
861
|
-
|
|
862
|
-
rb_funcall(self, id___unset_load_state, 0);
|
|
2067
|
+
rb_ensure(cStructure_load_body, self, cStructure_load_ensure, self);
|
|
863
2068
|
return self;
|
|
864
2069
|
}
|
|
865
2070
|
|
|
866
|
-
static
|
|
2071
|
+
static VALUE cStructure_dump_body(VALUE self) {
|
|
2072
|
+
return rb_funcall(self, id___dump_fields, 0);
|
|
2073
|
+
}
|
|
867
2074
|
|
|
868
|
-
static
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
2075
|
+
static VALUE cStructure_dump_ensure(VALUE self) {
|
|
2076
|
+
return rb_funcall(self, id___unset_dump_state, 0);
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
static VALUE cStructure_dump(int argc, VALUE *argv, VALUE self) {
|
|
2080
|
+
VALUE output, output_big, parent, index;
|
|
873
2081
|
rb_scan_args(argc, argv, "22", &output, &output_big, &parent, &index);
|
|
2082
|
+
uint32_t flags = (CLASS_OF(self) == rb_obj_class(self)) ? libbin_get_structure_flags(rb_obj_class(self)) : ~0u;
|
|
2083
|
+
if (!(flags & (LIBBIN_OVR_SET_DUMP | LIBBIN_OVR_DUMP_FIELDS))) {
|
|
2084
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
|
|
2085
|
+
if (pctx) {
|
|
2086
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2087
|
+
cStructure_dump_fast(self, pctx, output_big, parent, index, flags);
|
|
2088
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2089
|
+
return self;
|
|
2090
|
+
}
|
|
2091
|
+
libbin_ctx_t ctx;
|
|
2092
|
+
libbin_ctx_init_dump(&ctx, output);
|
|
2093
|
+
struct top_inst_args args = { self, &ctx, Qnil, output_big, parent, index, flags, 1 };
|
|
2094
|
+
rb_ensure(top_inst_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
2095
|
+
return self;
|
|
2096
|
+
}
|
|
874
2097
|
rb_funcall(self, id___set_dump_state, 4, output, output_big, parent, index);
|
|
875
|
-
|
|
876
|
-
rb_funcall(self, id___unset_dump_state, 0);
|
|
2098
|
+
rb_ensure(cStructure_dump_body, self, cStructure_dump_ensure, self);
|
|
877
2099
|
return self;
|
|
878
2100
|
}
|
|
879
2101
|
|
|
880
|
-
static
|
|
2102
|
+
static VALUE cStructure_convert_body(VALUE self) {
|
|
2103
|
+
return rb_funcall(self, id___convert_fields, 0);
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
static VALUE cStructure_convert_ensure(VALUE self) {
|
|
2107
|
+
return rb_funcall(self, id___unset_convert_state, 0);
|
|
2108
|
+
}
|
|
881
2109
|
|
|
882
2110
|
static VALUE cStructure_convert(int argc, VALUE *argv, VALUE self) {
|
|
883
|
-
VALUE input;
|
|
884
|
-
VALUE output;
|
|
885
|
-
VALUE input_big;
|
|
886
|
-
VALUE output_big;
|
|
887
|
-
VALUE parent;
|
|
888
|
-
VALUE index;
|
|
2111
|
+
VALUE input, output, input_big, output_big, parent, index;
|
|
889
2112
|
rb_scan_args(argc, argv, "42", &input, &output, &input_big, &output_big, &parent, &index);
|
|
2113
|
+
uint32_t flags = (CLASS_OF(self) == rb_obj_class(self)) ? libbin_get_structure_flags(rb_obj_class(self)) : ~0u;
|
|
2114
|
+
if (!(flags & (LIBBIN_OVR_SET_CONVERT | LIBBIN_OVR_CONVERT_FIELDS))) {
|
|
2115
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
|
|
2116
|
+
if (pctx) {
|
|
2117
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2118
|
+
cStructure_convert_fast(self, pctx, input_big, output_big, parent, index, flags);
|
|
2119
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2120
|
+
return self;
|
|
2121
|
+
}
|
|
2122
|
+
libbin_ctx_t ctx;
|
|
2123
|
+
libbin_ctx_init_convert(&ctx, input, output);
|
|
2124
|
+
struct top_inst_args args = { self, &ctx, input_big, output_big, parent, index, flags, 2 };
|
|
2125
|
+
rb_ensure(top_inst_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
2126
|
+
return self;
|
|
2127
|
+
}
|
|
890
2128
|
rb_funcall(self, id___set_convert_state, 6, input, output, input_big, output_big, parent, index);
|
|
891
|
-
|
|
892
|
-
rb_funcall(self, id___unset_convert_state, 0);
|
|
2129
|
+
rb_ensure(cStructure_convert_body, self, cStructure_convert_ensure, self);
|
|
893
2130
|
return self;
|
|
894
2131
|
}
|
|
895
2132
|
|
|
896
|
-
|
|
2133
|
+
struct shape_body_args {
|
|
2134
|
+
VALUE self;
|
|
2135
|
+
VALUE kind;
|
|
2136
|
+
long long stride_val;
|
|
2137
|
+
};
|
|
2138
|
+
|
|
2139
|
+
static VALUE cStructure_shape_body(VALUE args_p) {
|
|
2140
|
+
struct shape_body_args *args = (struct shape_body_args *)args_p;
|
|
2141
|
+
VALUE members = rb_funcall(args->self, id___shape_fields, 1, args->kind);
|
|
2142
|
+
VALUE stride_expr = rb_attr_get(rb_obj_class(args->self), id_stride);
|
|
2143
|
+
if (RTEST(stride_expr))
|
|
2144
|
+
args->stride_val = NUM2LL(cStructure_decode_expression_ctx(args->self, NULL, stride_expr));
|
|
2145
|
+
return members;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
static VALUE cStructure_shape_ensure(VALUE self) {
|
|
2149
|
+
return rb_funcall(self, id___unset_size_state, 0);
|
|
2150
|
+
}
|
|
897
2151
|
|
|
898
2152
|
static VALUE cStructure_shape(int argc, VALUE *argv, VALUE self) {
|
|
899
|
-
VALUE previous_offset;
|
|
900
|
-
VALUE parent;
|
|
901
|
-
VALUE index;
|
|
902
|
-
VALUE kind;
|
|
2153
|
+
VALUE previous_offset, parent, index, kind;
|
|
903
2154
|
rb_scan_args(argc, argv, "04", &previous_offset, &parent, &index, &kind);
|
|
904
2155
|
if (NIL_P(previous_offset))
|
|
905
2156
|
previous_offset = INT2FIX(0);
|
|
906
2157
|
if (NIL_P(kind))
|
|
907
2158
|
kind = cDataShape;
|
|
908
2159
|
rb_funcall(self, id___set_size_state, 3, previous_offset, parent, index);
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
2160
|
+
struct shape_body_args body_args = { self, kind, 0 };
|
|
2161
|
+
VALUE members = rb_ensure(cStructure_shape_body, (VALUE)&body_args, cStructure_shape_ensure, self);
|
|
2162
|
+
long has_members = RARRAY_LEN(rb_funcall(rb_funcall(members, id_values, 0), id_compact, 0)) > 0;
|
|
2163
|
+
if (body_args.stride_val > 0) {
|
|
2164
|
+
ptrdiff_t prev_off = NUM2LL(previous_offset);
|
|
2165
|
+
VALUE target_end = LL2NUM(prev_off + (ptrdiff_t)body_args.stride_val - 1);
|
|
2166
|
+
if (!has_members) {
|
|
2167
|
+
VALUE empty_args[] = { previous_offset, target_end };
|
|
2168
|
+
return rb_class_new_instance(2, empty_args, kind);
|
|
2169
|
+
}
|
|
2170
|
+
VALUE shape = rb_class_new_instance(1, &members, kind);
|
|
2171
|
+
rb_funcall(shape, id___extend_range, 2, previous_offset, target_end);
|
|
2172
|
+
return shape;
|
|
2173
|
+
}
|
|
2174
|
+
if (!has_members)
|
|
912
2175
|
return Qnil;
|
|
913
2176
|
return rb_class_new_instance(1, &members, kind);
|
|
914
2177
|
}
|
|
915
2178
|
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
2179
|
+
#define LIBBIN_LEAF_CACHE_MAX 64
|
|
2180
|
+
|
|
2181
|
+
static inline long libbin_extract_leaf_fields(
|
|
2182
|
+
VALUE fields,
|
|
2183
|
+
struct cField_data **fds,
|
|
2184
|
+
VALUE *field_objs,
|
|
2185
|
+
size_t *out_fixed_bytes)
|
|
2186
|
+
{
|
|
2187
|
+
long nf = RARRAY_LEN(fields);
|
|
2188
|
+
if (nf > LIBBIN_LEAF_CACHE_MAX)
|
|
2189
|
+
return -1;
|
|
2190
|
+
size_t total = 0;
|
|
2191
|
+
int fixed = 1;
|
|
2192
|
+
for (long j = 0; j < nf; j++) {
|
|
2193
|
+
VALUE fobj = RARRAY_CONST_PTR(fields)[j];
|
|
2194
|
+
struct cField_data *fd;
|
|
2195
|
+
TypedData_Get_Struct(fobj, struct cField_data, &cField_type, fd);
|
|
2196
|
+
field_objs[j] = fobj;
|
|
2197
|
+
fds[j] = fd;
|
|
2198
|
+
if (fixed) {
|
|
2199
|
+
size_t esz = libbin_scalar_elem_size((libbin_scalar_kind_t)fd->scalar_kind);
|
|
2200
|
+
if (esz == 0) {
|
|
2201
|
+
fixed = 0;
|
|
2202
|
+
} else if (NIL_P(fd->length)) {
|
|
2203
|
+
if (fd->scalar_kind == SCALAR_STR || fd->scalar_kind == SCALAR_ZSTR || fd->scalar_kind == SCALAR_SKIP)
|
|
2204
|
+
fixed = 0;
|
|
2205
|
+
else
|
|
2206
|
+
total += esz;
|
|
2207
|
+
} else if (FIXNUM_P(fd->length) || RB_INTEGER_TYPE_P(fd->length)) {
|
|
2208
|
+
long flen = NUM2LONG(fd->length);
|
|
2209
|
+
if (flen < 0)
|
|
2210
|
+
fixed = 0;
|
|
2211
|
+
else
|
|
2212
|
+
total += esz * (size_t)flen;
|
|
2213
|
+
} else {
|
|
2214
|
+
fixed = 0;
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
*out_fixed_bytes = fixed ? total : 0;
|
|
2219
|
+
return nf;
|
|
2220
|
+
}
|
|
2221
|
+
|
|
2222
|
+
static VALUE cStructure_singl_load_ctx(
|
|
2223
|
+
VALUE klass,
|
|
2224
|
+
libbin_ctx_t *ctx,
|
|
2225
|
+
VALUE input_big,
|
|
2226
|
+
VALUE parent,
|
|
2227
|
+
VALUE index,
|
|
2228
|
+
VALUE length,
|
|
2229
|
+
uint32_t flags)
|
|
2230
|
+
{
|
|
2231
|
+
int fast_alloc = !(flags & LIBBIN_OVR_INIT);
|
|
2232
|
+
int fast_inst = !(flags & (LIBBIN_OVR_INST_LOAD | LIBBIN_OVR_SET_LOAD | LIBBIN_OVR_LOAD_FIELDS));
|
|
925
2233
|
VALUE res;
|
|
926
2234
|
if (!NIL_P(length)) {
|
|
927
2235
|
long l = NUM2LONG(length);
|
|
2236
|
+
if (l < 0)
|
|
2237
|
+
rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
|
|
928
2238
|
res = rb_ary_new_capa(l);
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
2239
|
+
if (fast_alloc && fast_inst && l > 0 &&
|
|
2240
|
+
(flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_LOAD_MASK) &&
|
|
2241
|
+
!LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
2242
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
2243
|
+
struct cField_data *fds[LIBBIN_LEAF_CACHE_MAX];
|
|
2244
|
+
VALUE fobjs[LIBBIN_LEAF_CACHE_MAX];
|
|
2245
|
+
size_t elem_bytes = 0;
|
|
2246
|
+
long nf = libbin_extract_leaf_fields(fields, fds, fobjs, &elem_bytes);
|
|
2247
|
+
if (nf >= 0) {
|
|
2248
|
+
int ibig = RTEST(input_big);
|
|
2249
|
+
size_t depth = libbin_ctx_push_frame(ctx, Qnil, 0, Qnil);
|
|
2250
|
+
for (long i = 0; i < l; i++) {
|
|
2251
|
+
VALUE obj = cStructure_alloc(klass);
|
|
2252
|
+
if (depth < LIBBIN_MAX_FRAMES) {
|
|
2253
|
+
ctx->frames[depth].self = obj;
|
|
2254
|
+
ctx->frames[depth].index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2255
|
+
}
|
|
2256
|
+
for (long j = 0; j < nf; j++) {
|
|
2257
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
2258
|
+
ctx->frames[depth].field = fobjs[j];
|
|
2259
|
+
VALUE val = libbin_scalar_load_fast((libbin_scalar_kind_t)fds[j]->scalar_kind, &ctx->in_io, ibig, fds[j]->length);
|
|
2260
|
+
if (!NIL_P(fds[j]->name))
|
|
2261
|
+
rb_ivar_set(obj, fds[j]->ivar, val);
|
|
2262
|
+
}
|
|
2263
|
+
rb_ary_push(res, obj);
|
|
2264
|
+
}
|
|
2265
|
+
RB_GC_GUARD(fields);
|
|
2266
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
2267
|
+
return res;
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
if (fast_alloc && fast_inst) {
|
|
2271
|
+
for (long i = 0; i < l; i++) {
|
|
2272
|
+
VALUE obj = cStructure_alloc(klass);
|
|
2273
|
+
ctx->pending_index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2274
|
+
cStructure_load_fast(obj, ctx, input_big, parent, index, flags);
|
|
2275
|
+
rb_ary_push(res, obj);
|
|
2276
|
+
}
|
|
2277
|
+
ctx->pending_index = Qnil;
|
|
2278
|
+
} else if (fast_inst) {
|
|
2279
|
+
for (long i = 0; i < l; i++) {
|
|
2280
|
+
VALUE obj = rb_class_new_instance(0, NULL, klass);
|
|
2281
|
+
ctx->pending_index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2282
|
+
cStructure_load_fast(obj, ctx, input_big, parent, index, flags);
|
|
2283
|
+
rb_ary_push(res, obj);
|
|
2284
|
+
}
|
|
2285
|
+
ctx->pending_index = Qnil;
|
|
2286
|
+
} else {
|
|
2287
|
+
for (long i = 0; i < l; i++) {
|
|
2288
|
+
VALUE obj = fast_alloc ? cStructure_alloc(klass) : rb_class_new_instance(0, NULL, klass);
|
|
2289
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2290
|
+
rb_funcall(obj, id___load, 4, ctx->in_io.io, input_big, parent, index);
|
|
2291
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2292
|
+
rb_ary_push(res, obj);
|
|
2293
|
+
}
|
|
933
2294
|
}
|
|
934
2295
|
return res;
|
|
935
2296
|
} else {
|
|
936
|
-
res = rb_class_new_instance(0, NULL,
|
|
937
|
-
|
|
2297
|
+
res = fast_alloc ? cStructure_alloc(klass) : rb_class_new_instance(0, NULL, klass);
|
|
2298
|
+
if (fast_inst) {
|
|
2299
|
+
cStructure_load_fast(res, ctx, input_big, parent, index, flags);
|
|
2300
|
+
} else {
|
|
2301
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2302
|
+
rb_funcall(res, id___load, 4, ctx->in_io.io, input_big, parent, index);
|
|
2303
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2304
|
+
}
|
|
2305
|
+
return res;
|
|
938
2306
|
}
|
|
939
|
-
return res;
|
|
940
2307
|
}
|
|
941
2308
|
|
|
942
|
-
static VALUE
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
2309
|
+
static VALUE cStructure_singl_dump_ctx(
|
|
2310
|
+
VALUE klass,
|
|
2311
|
+
VALUE value,
|
|
2312
|
+
libbin_ctx_t *ctx,
|
|
2313
|
+
VALUE output_big,
|
|
2314
|
+
VALUE parent,
|
|
2315
|
+
VALUE index,
|
|
2316
|
+
VALUE length,
|
|
2317
|
+
uint32_t flags)
|
|
2318
|
+
{
|
|
2319
|
+
(void)klass;
|
|
2320
|
+
int fast_inst = !(flags & (LIBBIN_OVR_INST_DUMP | LIBBIN_OVR_SET_DUMP | LIBBIN_OVR_DUMP_FIELDS));
|
|
952
2321
|
if (!NIL_P(length)) {
|
|
2322
|
+
Check_Type(value, T_ARRAY);
|
|
953
2323
|
long l = NUM2LONG(length);
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
2324
|
+
if (l < 0)
|
|
2325
|
+
rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
|
|
2326
|
+
if (RARRAY_LEN(value) < l)
|
|
2327
|
+
rb_raise(rb_eArgError, "array too short for length: got %ld needed %ld", RARRAY_LEN(value), l);
|
|
2328
|
+
if (fast_inst && l > 0 &&
|
|
2329
|
+
(flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_DUMP_MASK) &&
|
|
2330
|
+
!LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
2331
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
2332
|
+
struct cField_data *fds[LIBBIN_LEAF_CACHE_MAX];
|
|
2333
|
+
VALUE fobjs[LIBBIN_LEAF_CACHE_MAX];
|
|
2334
|
+
size_t elem_bytes = 0;
|
|
2335
|
+
long nf = libbin_extract_leaf_fields(fields, fds, fobjs, &elem_bytes);
|
|
2336
|
+
if (nf >= 0) {
|
|
2337
|
+
if (elem_bytes > 0)
|
|
2338
|
+
libbin_io_reserve_output(&ctx->out_io, (size_t)l * elem_bytes);
|
|
2339
|
+
int obig = RTEST(output_big);
|
|
2340
|
+
size_t depth = libbin_ctx_push_frame(ctx, Qnil, 1, Qnil);
|
|
2341
|
+
for (long i = 0; i < l; i++) {
|
|
2342
|
+
VALUE elem = rb_ary_entry(value, i);
|
|
2343
|
+
VALUE fidx = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2344
|
+
if (RB_LIKELY(rb_obj_class(elem) == klass && CLASS_OF(elem) == klass)) {
|
|
2345
|
+
if (depth < LIBBIN_MAX_FRAMES) {
|
|
2346
|
+
ctx->frames[depth].self = elem;
|
|
2347
|
+
ctx->frames[depth].index = fidx;
|
|
2348
|
+
}
|
|
2349
|
+
for (long j = 0; j < nf; j++) {
|
|
2350
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
2351
|
+
ctx->frames[depth].field = fobjs[j];
|
|
2352
|
+
VALUE val = NIL_P(fds[j]->name) ? Qnil : rb_ivar_get(elem, fds[j]->ivar);
|
|
2353
|
+
libbin_scalar_dump_fast((libbin_scalar_kind_t)fds[j]->scalar_kind, val, &ctx->out_io, obig, fds[j]->length);
|
|
2354
|
+
}
|
|
2355
|
+
} else {
|
|
2356
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2357
|
+
rb_funcall(elem, id___dump, 4, ctx->out_io.io, output_big, parent, index);
|
|
2358
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2359
|
+
}
|
|
2360
|
+
}
|
|
2361
|
+
RB_GC_GUARD(fields);
|
|
2362
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
2363
|
+
return value;
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
if (fast_inst) {
|
|
2367
|
+
for (long i = 0; i < l; i++) {
|
|
2368
|
+
VALUE elem = rb_ary_entry(value, i);
|
|
2369
|
+
if (rb_obj_class(elem) == klass) {
|
|
2370
|
+
ctx->pending_index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2371
|
+
cStructure_dump_fast(elem, ctx, output_big, parent, index, flags);
|
|
2372
|
+
} else {
|
|
2373
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2374
|
+
rb_funcall(elem, id___dump, 4, ctx->out_io.io, output_big, parent, index);
|
|
2375
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
ctx->pending_index = Qnil;
|
|
2379
|
+
} else {
|
|
2380
|
+
for (long i = 0; i < l; i++) {
|
|
2381
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2382
|
+
rb_funcall(rb_ary_entry(value, i), id___dump, 4, ctx->out_io.io, output_big, parent, index);
|
|
2383
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2384
|
+
}
|
|
2385
|
+
}
|
|
2386
|
+
} else {
|
|
2387
|
+
if (fast_inst && rb_obj_class(value) == klass) {
|
|
2388
|
+
cStructure_dump_fast(value, ctx, output_big, parent, index, flags);
|
|
2389
|
+
} else {
|
|
2390
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2391
|
+
rb_funcall(value, id___dump, 4, ctx->out_io.io, output_big, parent, index);
|
|
2392
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
958
2395
|
return value;
|
|
959
2396
|
}
|
|
960
2397
|
|
|
961
|
-
static VALUE
|
|
962
|
-
|
|
963
|
-
|
|
2398
|
+
static VALUE cStructure_singl_convert_ctx(
|
|
2399
|
+
VALUE klass,
|
|
2400
|
+
libbin_ctx_t *ctx,
|
|
2401
|
+
VALUE input_big,
|
|
2402
|
+
VALUE output_big,
|
|
2403
|
+
VALUE parent,
|
|
2404
|
+
VALUE index,
|
|
2405
|
+
VALUE length,
|
|
2406
|
+
uint32_t flags)
|
|
2407
|
+
{
|
|
2408
|
+
int fast_alloc = !(flags & LIBBIN_OVR_INIT);
|
|
2409
|
+
int fast_inst = !(flags & (LIBBIN_OVR_INST_CONVERT | LIBBIN_OVR_SET_CONVERT | LIBBIN_OVR_CONVERT_FIELDS));
|
|
2410
|
+
VALUE res;
|
|
2411
|
+
if (!NIL_P(length)) {
|
|
2412
|
+
long l = NUM2LONG(length);
|
|
2413
|
+
if (l < 0)
|
|
2414
|
+
rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
|
|
2415
|
+
res = rb_ary_new_capa(l);
|
|
2416
|
+
if (fast_alloc && fast_inst && l > 0 &&
|
|
2417
|
+
(flags & LIBBIN_ALL_SCALAR_LEAF) && !(flags & LIBBIN_FAST_CONVERT_MASK) &&
|
|
2418
|
+
!LIBBIN_ATOMIC_LOAD(&libbin_scalar_overridden)) {
|
|
2419
|
+
VALUE fields = rb_attr_get(klass, id_fields);
|
|
2420
|
+
struct cField_data *fds[LIBBIN_LEAF_CACHE_MAX];
|
|
2421
|
+
VALUE fobjs[LIBBIN_LEAF_CACHE_MAX];
|
|
2422
|
+
size_t elem_bytes = 0;
|
|
2423
|
+
long nf = libbin_extract_leaf_fields(fields, fds, fobjs, &elem_bytes);
|
|
2424
|
+
if (nf >= 0) {
|
|
2425
|
+
if (elem_bytes > 0)
|
|
2426
|
+
libbin_io_reserve_output(&ctx->out_io, (size_t)l * elem_bytes);
|
|
2427
|
+
int ibig = RTEST(input_big);
|
|
2428
|
+
int obig = RTEST(output_big);
|
|
2429
|
+
size_t depth = libbin_ctx_push_frame(ctx, Qnil, 2, Qnil);
|
|
2430
|
+
for (long i = 0; i < l; i++) {
|
|
2431
|
+
VALUE obj = cStructure_alloc(klass);
|
|
2432
|
+
if (depth < LIBBIN_MAX_FRAMES) {
|
|
2433
|
+
ctx->frames[depth].self = obj;
|
|
2434
|
+
ctx->frames[depth].index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2435
|
+
}
|
|
2436
|
+
for (long j = 0; j < nf; j++) {
|
|
2437
|
+
if (depth < LIBBIN_MAX_FRAMES)
|
|
2438
|
+
ctx->frames[depth].field = fobjs[j];
|
|
2439
|
+
VALUE val = libbin_scalar_convert_fast((libbin_scalar_kind_t)fds[j]->scalar_kind, &ctx->in_io, &ctx->out_io, ibig, obig, fds[j]->length);
|
|
2440
|
+
if (!NIL_P(fds[j]->name))
|
|
2441
|
+
rb_ivar_set(obj, fds[j]->ivar, val);
|
|
2442
|
+
}
|
|
2443
|
+
rb_ary_push(res, obj);
|
|
2444
|
+
}
|
|
2445
|
+
RB_GC_GUARD(fields);
|
|
2446
|
+
libbin_ctx_pop_frame(ctx, depth);
|
|
2447
|
+
return res;
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2450
|
+
if (fast_alloc && fast_inst) {
|
|
2451
|
+
for (long i = 0; i < l; i++) {
|
|
2452
|
+
VALUE obj = cStructure_alloc(klass);
|
|
2453
|
+
ctx->pending_index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2454
|
+
cStructure_convert_fast(obj, ctx, input_big, output_big, parent, index, flags);
|
|
2455
|
+
rb_ary_push(res, obj);
|
|
2456
|
+
}
|
|
2457
|
+
ctx->pending_index = Qnil;
|
|
2458
|
+
} else if (fast_inst) {
|
|
2459
|
+
for (long i = 0; i < l; i++) {
|
|
2460
|
+
VALUE obj = rb_class_new_instance(0, NULL, klass);
|
|
2461
|
+
ctx->pending_index = NIL_P(index) ? LONG2NUM(i) : index;
|
|
2462
|
+
cStructure_convert_fast(obj, ctx, input_big, output_big, parent, index, flags);
|
|
2463
|
+
rb_ary_push(res, obj);
|
|
2464
|
+
}
|
|
2465
|
+
ctx->pending_index = Qnil;
|
|
2466
|
+
} else {
|
|
2467
|
+
for (long i = 0; i < l; i++) {
|
|
2468
|
+
VALUE obj = fast_alloc ? cStructure_alloc(klass) : rb_class_new_instance(0, NULL, klass);
|
|
2469
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2470
|
+
rb_funcall(obj, id___convert, 6, ctx->in_io.io, ctx->out_io.io, input_big, output_big, parent, index);
|
|
2471
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2472
|
+
rb_ary_push(res, obj);
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
return res;
|
|
2476
|
+
} else {
|
|
2477
|
+
res = fast_alloc ? cStructure_alloc(klass) : rb_class_new_instance(0, NULL, klass);
|
|
2478
|
+
if (fast_inst) {
|
|
2479
|
+
cStructure_convert_fast(res, ctx, input_big, output_big, parent, index, flags);
|
|
2480
|
+
} else {
|
|
2481
|
+
libbin_ctx_sync_to_ruby(ctx);
|
|
2482
|
+
rb_funcall(res, id___convert, 6, ctx->in_io.io, ctx->out_io.io, input_big, output_big, parent, index);
|
|
2483
|
+
libbin_ctx_sync_from_ruby(ctx);
|
|
2484
|
+
}
|
|
2485
|
+
return res;
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
struct top_singl_args {
|
|
2490
|
+
VALUE klass;
|
|
2491
|
+
VALUE value;
|
|
2492
|
+
libbin_ctx_t *ctx;
|
|
964
2493
|
VALUE input_big;
|
|
965
2494
|
VALUE output_big;
|
|
966
2495
|
VALUE parent;
|
|
967
2496
|
VALUE index;
|
|
968
2497
|
VALUE length;
|
|
2498
|
+
uint32_t flags;
|
|
2499
|
+
uint8_t mode;
|
|
2500
|
+
};
|
|
2501
|
+
|
|
2502
|
+
static VALUE top_singl_body(VALUE args_p) {
|
|
2503
|
+
struct top_singl_args *a = (struct top_singl_args *)args_p;
|
|
2504
|
+
if (a->mode == 0)
|
|
2505
|
+
return cStructure_singl_load_ctx(a->klass, a->ctx, a->input_big, a->parent, a->index, a->length, a->flags);
|
|
2506
|
+
else if (a->mode == 1)
|
|
2507
|
+
return cStructure_singl_dump_ctx(a->klass, a->value, a->ctx, a->output_big, a->parent, a->index, a->length, a->flags);
|
|
2508
|
+
else
|
|
2509
|
+
return cStructure_singl_convert_ctx(a->klass, a->ctx, a->input_big, a->output_big, a->parent, a->index, a->length, a->flags);
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
static VALUE top_singl_rescue_wrapper(VALUE args_p) {
|
|
2513
|
+
struct top_singl_args *a = (struct top_singl_args *)args_p;
|
|
2514
|
+
return rb_rescue(top_singl_body, args_p, libbin_ctx_rescue, (VALUE)a->ctx);
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
static VALUE cStructure_singl_load(int argc, VALUE *argv, VALUE self) {
|
|
2518
|
+
VALUE input, input_big, parent, index, length;
|
|
2519
|
+
rb_scan_args(argc, argv, "14", &input, &input_big, &parent, &index, &length);
|
|
2520
|
+
if (NIL_P(input_big))
|
|
2521
|
+
input_big = rb_funcall(mLibBin, id_default_big_p, 0);
|
|
2522
|
+
uint32_t flags = libbin_get_structure_flags(self);
|
|
2523
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
|
|
2524
|
+
if (pctx) {
|
|
2525
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2526
|
+
VALUE res = cStructure_singl_load_ctx(self, pctx, input_big, parent, index, length, flags);
|
|
2527
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2528
|
+
return res;
|
|
2529
|
+
}
|
|
2530
|
+
libbin_ctx_t ctx;
|
|
2531
|
+
libbin_ctx_init_load(&ctx, input);
|
|
2532
|
+
struct top_singl_args args = { self, Qnil, &ctx, input_big, Qnil, parent, index, length, flags, 0 };
|
|
2533
|
+
return rb_ensure(top_singl_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
static VALUE cStructure_singl_dump(int argc, VALUE *argv, VALUE self) {
|
|
2537
|
+
VALUE value, output, output_big, parent, index, length;
|
|
2538
|
+
rb_scan_args(argc, argv, "24", &value, &output, &output_big, &parent, &index, &length);
|
|
2539
|
+
if (NIL_P(output_big))
|
|
2540
|
+
output_big = rb_funcall(mLibBin, id_default_big_p, 0);
|
|
2541
|
+
uint32_t flags = libbin_get_structure_flags(self);
|
|
2542
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
|
|
2543
|
+
if (pctx) {
|
|
2544
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2545
|
+
VALUE res = cStructure_singl_dump_ctx(self, value, pctx, output_big, parent, index, length, flags);
|
|
2546
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2547
|
+
return res;
|
|
2548
|
+
}
|
|
2549
|
+
libbin_ctx_t ctx;
|
|
2550
|
+
libbin_ctx_init_dump(&ctx, output);
|
|
2551
|
+
struct top_singl_args args = { self, value, &ctx, Qnil, output_big, parent, index, length, flags, 1 };
|
|
2552
|
+
return rb_ensure(top_singl_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
static VALUE cStructure_singl_convert(int argc, VALUE *argv, VALUE self) {
|
|
2556
|
+
VALUE input, output, input_big, output_big, parent, index, length;
|
|
969
2557
|
rb_scan_args(argc, argv, "25", &input, &output, &input_big, &output_big, &parent, &index, &length);
|
|
970
2558
|
if (NIL_P(input_big))
|
|
971
|
-
input_big = rb_funcall(mLibBin,
|
|
2559
|
+
input_big = rb_funcall(mLibBin, id_default_big_p, 0);
|
|
972
2560
|
if (NIL_P(output_big))
|
|
973
2561
|
output_big = RTEST(input_big) ? Qfalse : Qtrue;
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
rb_ary_store(res, i, obj);
|
|
982
|
-
}
|
|
983
|
-
} else {
|
|
984
|
-
res = rb_class_new_instance(0, NULL, self);
|
|
985
|
-
rb_funcall(res, id___convert, 6, input, output, input_big, output_big, parent, index);
|
|
2562
|
+
uint32_t flags = libbin_get_structure_flags(self);
|
|
2563
|
+
libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
|
|
2564
|
+
if (pctx) {
|
|
2565
|
+
libbin_ctx_sync_from_ruby(pctx);
|
|
2566
|
+
VALUE res = cStructure_singl_convert_ctx(self, pctx, input_big, output_big, parent, index, length, flags);
|
|
2567
|
+
libbin_ctx_sync_to_ruby(pctx);
|
|
2568
|
+
return res;
|
|
986
2569
|
}
|
|
987
|
-
|
|
2570
|
+
libbin_ctx_t ctx;
|
|
2571
|
+
libbin_ctx_init_convert(&ctx, input, output);
|
|
2572
|
+
struct top_singl_args args = { self, Qnil, &ctx, input_big, output_big, parent, index, length, flags, 2 };
|
|
2573
|
+
return rb_ensure(top_singl_rescue_wrapper, (VALUE)&args, libbin_ctx_ensure, (VALUE)&ctx);
|
|
988
2574
|
}
|
|
989
2575
|
|
|
990
2576
|
static VALUE cStructure_singl_shape(int argc, VALUE *argv, VALUE self) {
|
|
2577
|
+
(void)self;
|
|
991
2578
|
VALUE value;
|
|
992
2579
|
VALUE previous_offset;
|
|
993
2580
|
VALUE parent;
|
|
@@ -1001,21 +2588,52 @@ static VALUE cStructure_singl_shape(int argc, VALUE *argv, VALUE self) {
|
|
|
1001
2588
|
kind = cDataShape;
|
|
1002
2589
|
VALUE res;
|
|
1003
2590
|
if (!NIL_P(length)) {
|
|
2591
|
+
Check_Type(value, T_ARRAY);
|
|
1004
2592
|
long l = NUM2LONG(length);
|
|
2593
|
+
if (l < 0)
|
|
2594
|
+
rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
|
|
2595
|
+
if (RARRAY_LEN(value) < l)
|
|
2596
|
+
rb_raise(rb_eArgError, "array too short for length: got %ld needed %ld", RARRAY_LEN(value), l);
|
|
2597
|
+
VALUE cur_offset = previous_offset;
|
|
1005
2598
|
res = rb_ary_new_capa(l);
|
|
1006
|
-
for (long i = 0; i < l; i++)
|
|
1007
|
-
|
|
2599
|
+
for (long i = 0; i < l; i++) {
|
|
2600
|
+
VALUE sh = rb_funcall(rb_ary_entry(value, i), id___shape, 4, cur_offset, parent, index, kind);
|
|
2601
|
+
rb_ary_store(res, i, sh);
|
|
2602
|
+
if (!NIL_P(sh)) {
|
|
2603
|
+
VALUE last = rb_funcall(sh, id_last, 0);
|
|
2604
|
+
if (RTEST(last)) {
|
|
2605
|
+
ptrdiff_t pos = NUM2LL(last);
|
|
2606
|
+
if (pos >= 0)
|
|
2607
|
+
cur_offset = LL2NUM(pos + 1);
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
1008
2611
|
res = rb_class_new_instance(1, &res, kind);
|
|
1009
2612
|
} else
|
|
1010
2613
|
res = rb_funcall(value, id___shape, 4, previous_offset, parent, index, kind);
|
|
1011
2614
|
return res;
|
|
1012
2615
|
}
|
|
1013
2616
|
|
|
1014
|
-
static void define_cStructure() {
|
|
1015
|
-
id_tell = rb_intern("tell");
|
|
1016
|
-
id_seek = rb_intern("seek");
|
|
2617
|
+
static void define_cStructure(void) {
|
|
1017
2618
|
id_fields = rb_intern("@fields");
|
|
1018
2619
|
id_instance_exec = rb_intern("instance_exec");
|
|
2620
|
+
id_last = rb_intern("last");
|
|
2621
|
+
id_values = rb_intern("values");
|
|
2622
|
+
id_compact = rb_intern("compact");
|
|
2623
|
+
id_output = rb_intern("output");
|
|
2624
|
+
id_print = rb_intern("print");
|
|
2625
|
+
id_unpack = rb_intern("unpack");
|
|
2626
|
+
id_pack = rb_intern("pack");
|
|
2627
|
+
id_first = rb_intern("first");
|
|
2628
|
+
id_initialize = rb_intern("initialize");
|
|
2629
|
+
id_instance_method = rb_intern("instance_method");
|
|
2630
|
+
id_method = rb_intern("method");
|
|
2631
|
+
id_owner = rb_intern("owner");
|
|
2632
|
+
id_source_location = rb_intern("source_location");
|
|
2633
|
+
id___libbin_flags = rb_intern("@__libbin_flags");
|
|
2634
|
+
id___libbin_flags_gen = rb_intern("@__libbin_flags_gen");
|
|
2635
|
+
id_stride = rb_intern("@stride");
|
|
2636
|
+
id___extend_range = rb_intern("__extend_range");
|
|
1019
2637
|
|
|
1020
2638
|
id___set_load_state = rb_intern("__set_load_state");
|
|
1021
2639
|
id___unset_load_state = rb_intern("__unset_load_state");
|
|
@@ -1046,7 +2664,17 @@ static void define_cStructure() {
|
|
|
1046
2664
|
id___shape = rb_intern("__shape");
|
|
1047
2665
|
|
|
1048
2666
|
cStructure = rb_define_class_under(mLibBin, "Structure", rb_cObject);
|
|
2667
|
+
cStructure_sclass = rb_singleton_class(cStructure);
|
|
2668
|
+
rb_gc_register_mark_object(cStructure_sclass);
|
|
2669
|
+
#ifdef HAVE_RB_RACTOR_LOCAL_STORAGE_VALUE_NEWKEY
|
|
2670
|
+
libbin_ractor_flags_key = rb_ractor_local_storage_value_newkey();
|
|
2671
|
+
#endif
|
|
1049
2672
|
rb_define_alloc_func(cStructure, cStructure_alloc);
|
|
2673
|
+
rb_define_singleton_method(cStructure, "method_added", cStructure_method_added, 1);
|
|
2674
|
+
rb_define_singleton_method(cStructure, "singleton_method_added", cStructure_singleton_method_added, 1);
|
|
2675
|
+
rb_define_singleton_method(cStructure, "__bump_override_gen", cStructure_bump_override_gen, 0);
|
|
2676
|
+
rb_define_singleton_method(cStructure, "__set_data_types_file", cStructure_set_data_types_file, 1);
|
|
2677
|
+
rb_define_singleton_method(cStructure, "__preprocess_expression", cStructure_preprocess_expression, 1);
|
|
1050
2678
|
/**
|
|
1051
2679
|
* Create new Structure object.
|
|
1052
2680
|
* @return [Structure] a new Structure instance with state set to nil.
|
|
@@ -1251,17 +2879,18 @@ static void define_cStructure() {
|
|
|
1251
2879
|
*/
|
|
1252
2880
|
rb_define_method(cStructure, "__decode_expect", cStructure_decode_expect, 2);
|
|
1253
2881
|
/**
|
|
1254
|
-
* @overload __decode_seek_offset(offset, relative_offset, align)
|
|
2882
|
+
* @overload __decode_seek_offset(offset, relative_offset, align, null_offset)
|
|
1255
2883
|
* Decode the offset and seek to this position in the active streams.
|
|
1256
2884
|
* @param offset [Proc,Integer,nil] the expression to decode
|
|
1257
2885
|
* @param relative_offset [Boolean] the offset should be relative to the structure base (+__position+)
|
|
1258
2886
|
* @param align [false,Integer] the required alignement of the field
|
|
2887
|
+
* @param null_offset [nil,false,Integer] sentinel offset value treated as absent
|
|
1259
2888
|
* @return [nil,false,Integer] return nil if no offset was specified, an Integer if the active streams
|
|
1260
|
-
* had to seek, or false if a
|
|
2889
|
+
* had to seek, or false if a null offset was computed.
|
|
1261
2890
|
* @note Do not overload, this is not called through the usual ruby dispatch for performance reasons
|
|
1262
2891
|
* @example
|
|
1263
2892
|
* # Original Ruby implementation
|
|
1264
|
-
* def __decode_seek_offset(offset, relative_offset, align)
|
|
2893
|
+
* def __decode_seek_offset(offset, relative_offset, align, null_offset)
|
|
1265
2894
|
* cur_pos = nil
|
|
1266
2895
|
* if !offset
|
|
1267
2896
|
* return nil unless align
|
|
@@ -1278,7 +2907,8 @@ static void define_cStructure() {
|
|
|
1278
2907
|
* cur_pos += align - pad
|
|
1279
2908
|
* else
|
|
1280
2909
|
* cur_pos = __decode_expression(offset)
|
|
1281
|
-
* return false if cur_pos ==
|
|
2910
|
+
* return false if cur_pos.nil? || cur_pos == false
|
|
2911
|
+
* return false if null_offset && cur_pos == null_offset
|
|
1282
2912
|
* cur_pos += __position if relative_offset
|
|
1283
2913
|
* cur_pos += align - (cur_pos % align) if align && cur_pos % align > 0
|
|
1284
2914
|
* end
|
|
@@ -1288,7 +2918,7 @@ static void define_cStructure() {
|
|
|
1288
2918
|
* cur_pos
|
|
1289
2919
|
* end
|
|
1290
2920
|
*/
|
|
1291
|
-
rb_define_method(cStructure, "__decode_seek_offset", cStructure_decode_seek_offset,
|
|
2921
|
+
rb_define_method(cStructure, "__decode_seek_offset", cStructure_decode_seek_offset, 4);
|
|
1292
2922
|
/**
|
|
1293
2923
|
* @overload __decode_condition(condition)
|
|
1294
2924
|
* Decode the condition expression
|
|
@@ -1362,10 +2992,10 @@ static void define_cStructure() {
|
|
|
1362
2992
|
* __length = nil
|
|
1363
2993
|
* __count = nil
|
|
1364
2994
|
* unless field.sequence?
|
|
1365
|
-
* __offset = __decode_seek_offset(field.offset, field.relative_offset?, field.align)
|
|
1366
|
-
* return nil if __offset == false
|
|
1367
2995
|
* __condition = __decode_condition(field.condition)
|
|
1368
2996
|
* return nil unless __condition
|
|
2997
|
+
* __offset = __decode_seek_offset(field.offset, field.relative_offset?, field.align)
|
|
2998
|
+
* return nil if __offset == false
|
|
1369
2999
|
* __type = __decode_type(field.type)
|
|
1370
3000
|
* __length = __decode_length(field.length)
|
|
1371
3001
|
* end
|
|
@@ -1389,10 +3019,10 @@ static void define_cStructure() {
|
|
|
1389
3019
|
* __condition = nil
|
|
1390
3020
|
* __type = nil
|
|
1391
3021
|
* __length = nil
|
|
1392
|
-
* __offset = __decode_seek_offset(field.offset, field.relative_offset?, field.align)
|
|
1393
|
-
* return false if __offset == false
|
|
1394
3022
|
* __condition = __decode_condition(field.condition)
|
|
1395
3023
|
* return false unless __condition
|
|
3024
|
+
* __offset = __decode_seek_offset(field.offset, field.relative_offset?, field.align)
|
|
3025
|
+
* return false if __offset == false
|
|
1396
3026
|
* __type = __decode_type(field.type)
|
|
1397
3027
|
* __length = __decode_length(field.length)
|
|
1398
3028
|
* return true
|
|
@@ -1777,69 +3407,68 @@ static VALUE pghalf_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
|
|
|
1777
3407
|
(void)self;
|
|
1778
3408
|
Check_Type(str, T_STRING);
|
|
1779
3409
|
Check_Type(pack_str, T_STRING);
|
|
1780
|
-
VALUE arr = rb_funcall(str,
|
|
1781
|
-
uint16_t val = NUM2USHORT(rb_funcall(arr,
|
|
1782
|
-
union float_u res;
|
|
3410
|
+
VALUE arr = rb_funcall(str, id_unpack, 1, pack_str);
|
|
3411
|
+
uint16_t val = NUM2USHORT(rb_funcall(arr, id_first, 0));
|
|
1783
3412
|
|
|
1784
|
-
|
|
1785
|
-
return DBL2NUM(res.f);
|
|
3413
|
+
return DBL2NUM(pghalf_to_double(val));
|
|
1786
3414
|
}
|
|
1787
3415
|
|
|
1788
3416
|
static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
|
|
1789
3417
|
(void)self;
|
|
1790
3418
|
Check_Type(str, T_STRING);
|
|
1791
3419
|
Check_Type(pack_str, T_STRING);
|
|
1792
|
-
VALUE arr = rb_funcall(str,
|
|
1793
|
-
uint16_t val = NUM2USHORT(rb_funcall(arr,
|
|
1794
|
-
union float_u res;
|
|
3420
|
+
VALUE arr = rb_funcall(str, id_unpack, 1, pack_str);
|
|
3421
|
+
uint16_t val = NUM2USHORT(rb_funcall(arr, id_first, 0));
|
|
1795
3422
|
|
|
1796
|
-
|
|
1797
|
-
return DBL2NUM(res.f);
|
|
3423
|
+
return DBL2NUM(half_to_double(val));
|
|
1798
3424
|
}
|
|
1799
3425
|
|
|
1800
3426
|
static VALUE pghalf_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
|
|
1801
3427
|
(void)self;
|
|
1802
|
-
Check_Type(
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
val.f = NUM2DBL(number);
|
|
1807
|
-
res = pghalf_from_float(val.i);
|
|
1808
|
-
VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
|
|
3428
|
+
Check_Type(pack_str, T_STRING);
|
|
3429
|
+
uint16_t res = pghalf_from_double(NUM2DBL(number));
|
|
3430
|
+
VALUE arr = rb_ary_new3(1, UINT2NUM(res));
|
|
1809
3431
|
|
|
1810
|
-
return rb_funcall(arr,
|
|
3432
|
+
return rb_funcall(arr, id_pack, 1, pack_str);
|
|
1811
3433
|
}
|
|
1812
3434
|
|
|
1813
3435
|
static VALUE half_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
|
|
1814
3436
|
(void)self;
|
|
1815
|
-
Check_Type(
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
val.f = NUM2DBL(number);
|
|
1820
|
-
res = half_from_float(val.i);
|
|
1821
|
-
VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
|
|
3437
|
+
Check_Type(pack_str, T_STRING);
|
|
3438
|
+
uint16_t res = half_from_double(NUM2DBL(number));
|
|
3439
|
+
VALUE arr = rb_ary_new3(1, UINT2NUM(res));
|
|
1822
3440
|
|
|
1823
|
-
return rb_funcall(arr,
|
|
3441
|
+
return rb_funcall(arr, id_pack, 1, pack_str);
|
|
1824
3442
|
}
|
|
1825
3443
|
|
|
1826
|
-
static void define_cField() {
|
|
3444
|
+
static void define_cField(void) {
|
|
1827
3445
|
id_gsub = rb_intern("gsub");
|
|
3446
|
+
id_eval = rb_intern("eval");
|
|
1828
3447
|
|
|
1829
|
-
|
|
1830
|
-
|
|
3448
|
+
rb_field_eval_binding = rb_eval_string(
|
|
3449
|
+
"module LibBin; class Structure; lambda { binding }.call; end; end");
|
|
3450
|
+
rb_gc_register_mark_object(rb_field_eval_binding);
|
|
3451
|
+
|
|
3452
|
+
rb_re_dot_dot = rb_obj_freeze(rb_reg_new_str(rb_str_new_cstr("(?<![\\w.])\\.\\.(?=[\\\\.]|$)"), 0));
|
|
1831
3453
|
rb_str___parent = rb_obj_freeze(rb_str_new_cstr("__parent"));
|
|
1832
|
-
rb_gc_register_mark_object(rb_str___parent);
|
|
1833
3454
|
rb_str_backslash = rb_obj_freeze(rb_str_new_cstr("\\"));
|
|
1834
|
-
rb_gc_register_mark_object(rb_str_backslash);
|
|
1835
3455
|
rb_str_dot = rb_obj_freeze(rb_str_new_cstr("."));
|
|
3456
|
+
#ifdef HAVE_RB_RACTOR_MAKE_SHAREABLE
|
|
3457
|
+
rb_ractor_make_shareable(rb_re_dot_dot);
|
|
3458
|
+
rb_ractor_make_shareable(rb_str___parent);
|
|
3459
|
+
rb_ractor_make_shareable(rb_str_backslash);
|
|
3460
|
+
rb_ractor_make_shareable(rb_str_dot);
|
|
3461
|
+
#endif
|
|
3462
|
+
rb_gc_register_mark_object(rb_re_dot_dot);
|
|
3463
|
+
rb_gc_register_mark_object(rb_str___parent);
|
|
3464
|
+
rb_gc_register_mark_object(rb_str_backslash);
|
|
1836
3465
|
rb_gc_register_mark_object(rb_str_dot);
|
|
1837
3466
|
|
|
1838
3467
|
cField = rb_define_class_under(cStructure, "Field", rb_cObject);
|
|
1839
3468
|
rb_define_alloc_func(cField, cField_alloc);
|
|
1840
3469
|
/**
|
|
1841
|
-
* @overload initialize(name, type, length, count, offset, sequence, condition, relative_offset, align, expect)
|
|
1842
|
-
* @param name [Symbol, String] the name of the field.
|
|
3470
|
+
* @overload initialize(name, type, length, count, offset, sequence, condition, relative_offset, align, expect, null_offset)
|
|
3471
|
+
* @param name [Symbol, String, nil] the name of the field (or +nil+ for anonymous padding directives).
|
|
1843
3472
|
* @param type [Class, String, Proc] the type of the field, as a Class, or
|
|
1844
3473
|
* as a String or Proc that will be evaluated in the context of the
|
|
1845
3474
|
* {Structure} instance.
|
|
@@ -1871,9 +3500,10 @@ static void define_cField() {
|
|
|
1871
3500
|
* object will be tested for equality, and an exception will be raised if
|
|
1872
3501
|
* objects were not equal. Proc is evaluated in the context of the {Structure}
|
|
1873
3502
|
* instance and #__value will be set. Each repetition is validated.
|
|
3503
|
+
* @param null_offset [nil, false, Integer] offset sentinel treated as absent.
|
|
1874
3504
|
* @return [Field] new Field
|
|
1875
3505
|
*/
|
|
1876
|
-
rb_define_method(cField, "initialize", cField_initialize,
|
|
3506
|
+
rb_define_method(cField, "initialize", cField_initialize, 11);
|
|
1877
3507
|
DEFINE_FIELD_STATE_GETTER(name);
|
|
1878
3508
|
DEFINE_FIELD_STATE_GETTER(type);
|
|
1879
3509
|
DEFINE_FIELD_STATE_GETTER(length);
|
|
@@ -1884,10 +3514,26 @@ static void define_cField() {
|
|
|
1884
3514
|
DEFINE_FIELD_STATE_GETTER_BOOL(relative_offset);
|
|
1885
3515
|
DEFINE_FIELD_STATE_GETTER(align);
|
|
1886
3516
|
DEFINE_FIELD_STATE_GETTER(expect);
|
|
3517
|
+
DEFINE_FIELD_STATE_GETTER(null_offset);
|
|
1887
3518
|
}
|
|
1888
3519
|
|
|
1889
|
-
void Init_libbin_c() {
|
|
3520
|
+
void Init_libbin_c(void) {
|
|
3521
|
+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
3522
|
+
rb_ext_ractor_safe(true);
|
|
3523
|
+
#endif
|
|
3524
|
+
rb_require("stringio");
|
|
1890
3525
|
mLibBin = rb_define_module("LibBin");
|
|
3526
|
+
|
|
3527
|
+
eLibBinError = rb_define_class_under(mLibBin, "Error", rb_eRuntimeError);
|
|
3528
|
+
eLibBinEOFError = rb_define_class_under(mLibBin, "EOFError", eLibBinError);
|
|
3529
|
+
eLibBinValidationError = rb_define_class_under(mLibBin, "ValidationError", eLibBinError);
|
|
3530
|
+
eLibBinAlignmentError = rb_define_class_under(mLibBin, "AlignmentError", eLibBinError);
|
|
3531
|
+
|
|
3532
|
+
id_ivar_structure = rb_intern("@structure");
|
|
3533
|
+
id_ivar_field = rb_intern("@field");
|
|
3534
|
+
id_ivar_offset = rb_intern("@offset");
|
|
3535
|
+
id_ivar_index = rb_intern("@index");
|
|
3536
|
+
|
|
1891
3537
|
/**
|
|
1892
3538
|
* @overload half_from_string(str, unpack_str)
|
|
1893
3539
|
* Load (unpacks) a half precision floating point.
|