libbin 1.0.8 → 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.
@@ -1,130 +1,273 @@
1
+ #include <string.h>
1
2
  #include "ruby.h"
2
3
  #include "./libbin_c.h"
3
4
 
4
5
  VALUE cDataShape;
6
+ VALUE cDataRange;
5
7
  VALUE cScalar;
6
8
 
7
- static ID id_read, id_write;
9
+ ID id_read, id_write, id_tell, id_seek, id_readline;
10
+ ID id_string, id_pos, id_pos_eq, id_closed_read_p, id_closed_write_p, id_StringIO;
11
+ ID id_default_big_p;
12
+ static ID id_bytesize;
13
+ static ID id_fill;
14
+ static ID id_Padding;
15
+ VALUE rb_str_nul;
16
+ VALUE cStringIO_cached = Qnil;
8
17
 
9
- /* size(value, previous_offset = 0, parent = nil, index = nil, length = nil)*/
18
+ int libbin_scalar_overridden = 0;
19
+ static int libbin_defining_scalars = 0;
20
+
21
+ static VALUE cScalar_classes[SCALAR_COUNT];
22
+
23
+ typedef VALUE (*scalar_load_fn_t)(libbin_io_t *io, int input_big, VALUE length);
24
+ typedef void (*scalar_dump_fn_t)(VALUE value, libbin_io_t *io, int output_big, VALUE length);
25
+ typedef VALUE (*scalar_convert_fn_t)(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length);
26
+
27
+ static scalar_load_fn_t scalar_load_fns[SCALAR_COUNT];
28
+ static scalar_dump_fn_t scalar_dump_fns[SCALAR_COUNT];
29
+ static scalar_convert_fn_t scalar_convert_fns[SCALAR_COUNT];
30
+
31
+ static VALUE cScalar_always_align(VALUE self) {
32
+ (void)self;
33
+ return Qfalse;
34
+ }
35
+
36
+ static VALUE cScalar_singleton_method_added(VALUE self, VALUE mid) {
37
+ (void)mid;
38
+ if (!libbin_defining_scalars &&
39
+ (self == cScalar || libbin_lookup_scalar_kind(self) != SCALAR_NONE))
40
+ LIBBIN_ATOMIC_STORE(&libbin_scalar_overridden, 1);
41
+ return Qnil;
42
+ }
43
+
44
+ /* Initialize a lightweight libbin_io_t for standalone Scalar.{load,dump,convert} calls:
45
+ * fast-path String and IO::Buffer directly (zero extra Ruby calls), keep StringIO/IO
46
+ * on generic read/write (1 Ruby call instead of closed?/string/pos/pos=). */
47
+ static inline void libbin_io_init_standalone(libbin_io_t *bio, VALUE io, int writable) {
48
+ if (RB_TYPE_P(io, T_STRING)) {
49
+ libbin_io_init(bio, io, writable);
50
+ return;
51
+ }
52
+ #ifdef LIBBIN_HAVE_IO_BUFFER
53
+ if (rb_obj_class(io) == rb_cIOBuffer) {
54
+ libbin_io_init(bio, io, writable);
55
+ return;
56
+ }
57
+ #endif
58
+ libbin_io_init_generic_raw(bio, io, writable);
59
+ }
60
+
61
+ /* align() */
62
+ #define MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
63
+ static VALUE CLASS ## _align(VALUE self) { \
64
+ (void)self; \
65
+ return INT2FIX(sizeof(MAPPED_TYPE)); \
66
+ }
67
+
68
+ /* size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil) */
10
69
  #define MAKE_TYPE_SIZE(CLASS, MAPPED_TYPE) \
11
70
  static VALUE CLASS ## _size(int argc, VALUE* argv, VALUE self) { \
12
71
  (void)self; \
13
72
  VALUE length; \
14
- rb_scan_args(argc, argv, "14", NULL, NULL, NULL, NULL, &length); \
15
- if (RTEST(length)) \
16
- return ULL2NUM(sizeof(MAPPED_TYPE)*NUM2ULL(length)); \
17
- else \
73
+ rb_scan_args(argc, argv, "05", NULL, NULL, NULL, NULL, &length); \
74
+ if (RTEST(length)) { \
75
+ long n = NUM2LONG(length); \
76
+ if (n < 0) \
77
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", n); \
78
+ return ULL2NUM(sizeof(MAPPED_TYPE) * (size_t)n); \
79
+ } else \
18
80
  return INT2FIX(sizeof(MAPPED_TYPE)); \
19
81
  }
20
82
 
21
- /* shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) */
83
+ /* shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) */
22
84
  #define MAKE_TYPE_SHAPE(CLASS, MAPPED_TYPE) \
23
85
  static VALUE CLASS ## _shape(int argc, VALUE* argv, VALUE self) { \
24
86
  (void)self; \
25
87
  VALUE previous_offset; \
26
88
  VALUE kind; \
27
89
  VALUE length; \
28
- rb_scan_args(argc, argv, "15", NULL, &previous_offset, NULL, NULL, &kind, &length); \
90
+ rb_scan_args(argc, argv, "06", NULL, &previous_offset, NULL, NULL, &kind, &length); \
29
91
  if (NIL_P(previous_offset)) \
30
92
  previous_offset = INT2FIX(0); \
31
93
  if (NIL_P(kind)) \
32
94
  kind = cDataShape; \
33
95
  if (NIL_P(length)) \
34
96
  length = INT2FIX(1); \
97
+ long long len = NUM2LL(length); \
98
+ if (len < 0) \
99
+ rb_raise(rb_eArgError, "length must be non-negative: %lld", len); \
35
100
  VALUE args[] = { previous_offset, \
36
- LL2NUM(NUM2LL(previous_offset) - 1 + NUM2LL(length) * (ptrdiff_t)sizeof(MAPPED_TYPE)) }; \
101
+ LL2NUM(NUM2LL(previous_offset) - 1 + len * (ptrdiff_t)sizeof(MAPPED_TYPE)) }; \
37
102
  return rb_class_new_instance(2, args, kind); \
38
103
  }
39
104
 
40
- #define MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
41
- do { \
42
- if (RTEST(length)) { \
43
- res = rb_ary_new_capa(n); \
44
- long i = 0; \
45
- for (MAPPED_TYPE *p = data; p != data + n; p++, i++) \
46
- rb_ary_store(res, i, RUBY_CONVERT(NATIVE_CONVERT(*p))); \
47
- } else \
48
- res = RUBY_CONVERT(NATIVE_CONVERT(*data)); \
105
+ static inline int libbin_all_plain_numbers(const VALUE *ptr, long n) {
106
+ for (long i = 0; i < n; i++) {
107
+ VALUE v = ptr[i];
108
+ if (!FIXNUM_P(v) && !RB_FLOAT_TYPE_P(v) && !RB_INTEGER_TYPE_P(v))
109
+ return 0;
110
+ }
111
+ return 1;
112
+ }
113
+
114
+ #define MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
115
+ do { \
116
+ if (RTEST(length)) { \
117
+ res = rb_ary_new_capa(n); \
118
+ for (long i = 0; i < n; i++) { \
119
+ MAPPED_TYPE elem; \
120
+ memcpy(&elem, data + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
121
+ rb_ary_push(res, RUBY_CONVERT(NATIVE_CONVERT(elem))); \
122
+ } \
123
+ } else { \
124
+ MAPPED_TYPE elem; \
125
+ memcpy(&elem, data, sizeof(MAPPED_TYPE)); \
126
+ res = RUBY_CONVERT(NATIVE_CONVERT(elem)); \
127
+ } \
49
128
  } while (0)
50
129
 
51
130
  #define MAKE_LOAD_ENDIAN(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
52
131
  do { \
53
- if (little) \
132
+ if (!input_big) \
54
133
  MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
55
134
  else \
56
135
  MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
57
136
  } while (0)
58
137
 
59
138
  #define MAKE_LOAD(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
60
- MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT)
139
+ do { \
140
+ (void)input_big; \
141
+ MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
142
+ } while (0)
61
143
 
62
144
  /* load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil) */
63
145
  #define MAKE_TYPE_LOAD(CLASS, MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT, LOAD) \
146
+ static VALUE CLASS ## _load_fast(libbin_io_t *bio, int input_big, VALUE length) { \
147
+ long n = RTEST(length) ? NUM2LONG(length) : 1; \
148
+ if (n < 0) \
149
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", n); \
150
+ size_t cnt = sizeof(MAPPED_TYPE) * (size_t)n; \
151
+ VALUE guard = Qnil; \
152
+ const char *data = libbin_io_read_bytes(bio, cnt, &guard); \
153
+ VALUE res; \
154
+ LOAD(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
155
+ RB_GC_GUARD(guard); \
156
+ return res; \
157
+ } \
64
158
  static VALUE CLASS ## _load(int argc, VALUE* argv, VALUE self) { \
65
159
  (void)self; \
66
160
  VALUE input; \
67
161
  VALUE input_big; \
162
+ VALUE parent; \
68
163
  VALUE length; \
69
- rb_scan_args(argc, argv, "14", &input, &input_big, NULL, NULL, &length); \
70
- if (NIL_P(input_big)) \
71
- input_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
72
- unsigned little = RTEST(input_big) ? 0 : 1; \
73
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
74
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
75
- VALUE res; \
76
- VALUE str = rb_funcall(input, id_read, 1, ULL2NUM(cnt)); \
77
- if (NIL_P(str) || RSTRING_LEN(str) < (long)cnt) \
78
- rb_raise(rb_eRuntimeError, \
79
- "could not read enough data: got %ld needed %zu", \
80
- NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
81
- MAPPED_TYPE *data = (MAPPED_TYPE *)RSTRING_PTR(str); \
82
- LOAD(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
83
- RB_GC_GUARD(str); \
84
- return res; \
164
+ rb_scan_args(argc, argv, "14", &input, &input_big, &parent, NULL, &length); \
165
+ int ibig = RTEST(NIL_P(input_big) ? rb_funcall(mLibBin, id_default_big_p, 0) : input_big); \
166
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0); \
167
+ if (pctx) { \
168
+ libbin_ctx_sync_from_ruby(pctx); \
169
+ VALUE res = CLASS ## _load_fast(&pctx->in_io, ibig, length); \
170
+ libbin_ctx_sync_to_ruby(pctx); \
171
+ return res; \
172
+ } \
173
+ libbin_io_t bio; \
174
+ libbin_io_init_standalone(&bio, input, 0); \
175
+ return CLASS ## _load_fast(&bio, ibig, length); \
85
176
  }
86
177
 
87
- #define MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
88
- do { \
89
- if (RTEST(length)) \
90
- for (long i = 0; i < n; i++) { \
91
- MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(rb_ary_entry(value, i))); \
92
- rb_str_cat(str, (char *)&v, sizeof(MAPPED_TYPE)); \
93
- } \
94
- else { \
95
- MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(value)); \
96
- rb_str_cat(str, (char *)&v, sizeof(MAPPED_TYPE)); \
97
- } \
178
+ #define MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
179
+ do { \
180
+ if (RTEST(length)) { \
181
+ Check_Type(value, T_ARRAY); \
182
+ if (RARRAY_LEN(value) < n) \
183
+ rb_raise(rb_eArgError, "array too short for length: got %ld needed %ld", \
184
+ RARRAY_LEN(value), n); \
185
+ if (cnt <= 256) { \
186
+ char sbuf[256]; \
187
+ if (libbin_all_plain_numbers(RARRAY_CONST_PTR(value), n)) { \
188
+ const VALUE *elems = RARRAY_CONST_PTR(value); \
189
+ for (long i = 0; i < n; i++) { \
190
+ MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(elems[i])); \
191
+ memcpy(sbuf + (size_t)i * sizeof(MAPPED_TYPE), &v, sizeof(MAPPED_TYPE)); \
192
+ } \
193
+ } else { \
194
+ for (long i = 0; i < n; i++) { \
195
+ MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(rb_ary_entry(value, i))); \
196
+ memcpy(sbuf + (size_t)i * sizeof(MAPPED_TYPE), &v, sizeof(MAPPED_TYPE)); \
197
+ } \
198
+ } \
199
+ libbin_io_write_bytes(bio, sbuf, cnt); \
200
+ } else if (bio->kind != LIBBIN_IO_GENERIC && \
201
+ libbin_all_plain_numbers(RARRAY_CONST_PTR(value), n)) { \
202
+ char *dst = libbin_io_write_reserve_direct(bio, cnt); \
203
+ const VALUE *elems = RARRAY_CONST_PTR(value); \
204
+ for (long i = 0; i < n; i++) { \
205
+ MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(elems[i])); \
206
+ memcpy(dst + (size_t)i * sizeof(MAPPED_TYPE), &v, sizeof(MAPPED_TYPE)); \
207
+ } \
208
+ RB_GC_GUARD(value); \
209
+ } else { \
210
+ VALUE tmp = rb_str_buf_new((long)cnt); \
211
+ char *dst = RSTRING_PTR(tmp); \
212
+ for (long i = 0; i < n; i++) { \
213
+ MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(rb_ary_entry(value, i))); \
214
+ memcpy(dst + (size_t)i * sizeof(MAPPED_TYPE), &v, sizeof(MAPPED_TYPE)); \
215
+ } \
216
+ rb_str_set_len(tmp, (long)cnt); \
217
+ if (bio->kind == LIBBIN_IO_GENERIC) \
218
+ libbin_io_write_ruby_str(bio, tmp); \
219
+ else \
220
+ libbin_io_write_bytes(bio, RSTRING_PTR(tmp), cnt); \
221
+ RB_GC_GUARD(tmp); \
222
+ } \
223
+ } else { \
224
+ MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(value)); \
225
+ libbin_io_write_bytes(bio, &v, sizeof(MAPPED_TYPE)); \
226
+ } \
98
227
  } while (0)
99
228
 
100
229
  #define MAKE_DUMP_ENDIAN(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
101
230
  do { \
102
- if (little) \
231
+ if (!output_big) \
103
232
  MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
104
233
  else \
105
234
  MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
106
235
  } while (0)
107
236
 
108
237
  #define MAKE_DUMP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
109
- MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT)
238
+ do { \
239
+ (void)output_big; \
240
+ MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
241
+ } while (0)
110
242
 
111
243
  /* dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil) */
112
244
  #define MAKE_TYPE_DUMP(CLASS, MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT, DUMP) \
245
+ static void CLASS ## _dump_fast(VALUE value, libbin_io_t *bio, int output_big, VALUE length) { \
246
+ long n = RTEST(length) ? NUM2LONG(length) : 1; \
247
+ if (n < 0) \
248
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", n); \
249
+ size_t cnt = sizeof(MAPPED_TYPE) * (size_t)n; \
250
+ DUMP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
251
+ } \
113
252
  static VALUE CLASS ## _dump(int argc, VALUE* argv, VALUE self) { \
114
253
  (void)self; \
115
254
  VALUE value; \
116
255
  VALUE output; \
117
256
  VALUE output_big; \
257
+ VALUE parent; \
118
258
  VALUE length; \
119
- rb_scan_args(argc, argv, "24", &value, &output, &output_big, NULL, NULL, &length); \
120
- if (NIL_P(output_big)) \
121
- output_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
122
- unsigned little = RTEST(output_big) ? 0 : 1; \
123
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
124
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
125
- VALUE str = rb_str_buf_new((long)cnt); \
126
- DUMP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
127
- rb_funcall(output, id_write, 1, str); \
259
+ rb_scan_args(argc, argv, "24", &value, &output, &output_big, &parent, NULL, &length); \
260
+ int obig = RTEST(NIL_P(output_big) ? rb_funcall(mLibBin, id_default_big_p, 0) : output_big); \
261
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1); \
262
+ if (pctx) { \
263
+ libbin_ctx_sync_from_ruby(pctx); \
264
+ CLASS ## _dump_fast(value, &pctx->out_io, obig, length); \
265
+ libbin_ctx_sync_to_ruby(pctx); \
266
+ return Qnil; \
267
+ } \
268
+ libbin_io_t bio; \
269
+ libbin_io_init_standalone(&bio, output, 1); \
270
+ CLASS ## _dump_fast(value, &bio, obig, length); \
128
271
  return Qnil; \
129
272
  }
130
273
 
@@ -132,84 +275,162 @@ static VALUE CLASS ## _dump(int argc, VALUE* argv, VALUE self) {
132
275
  do { \
133
276
  if (RTEST(length)) { \
134
277
  res = rb_ary_new_capa(n); \
135
- long i = 0; \
136
- for (MAPPED_TYPE *p = data; p != data + n; p++, i++) { \
137
- rb_ary_store(res, i, RUBY_CONVERT(NATIVE_CONVERT(*p))); \
138
- if (SWAP_COND) \
139
- *p = MAPPED_SWAP(*p); \
278
+ if (SWAP_COND) { \
279
+ if (cnt <= 256) { \
280
+ char sbuf[256]; \
281
+ for (long i = 0; i < n; i++) { \
282
+ MAPPED_TYPE elem; \
283
+ memcpy(&elem, src + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
284
+ rb_ary_push(res, RUBY_CONVERT(NATIVE_CONVERT(elem))); \
285
+ elem = MAPPED_SWAP(elem); \
286
+ memcpy(sbuf + (size_t)i * sizeof(MAPPED_TYPE), &elem, sizeof(MAPPED_TYPE)); \
287
+ } \
288
+ libbin_io_write_bytes(out_io, sbuf, cnt); \
289
+ } else if (out_io->kind != LIBBIN_IO_GENERIC && \
290
+ in_io->io != out_io->io && \
291
+ (NIL_P(in_io->buf_str) || in_io->buf_str != out_io->buf_str)) { \
292
+ for (long i = 0; i < n; i++) { \
293
+ MAPPED_TYPE elem; \
294
+ memcpy(&elem, src + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
295
+ rb_ary_push(res, RUBY_CONVERT(NATIVE_CONVERT(elem))); \
296
+ } \
297
+ if (in_io->kind == LIBBIN_IO_STRINGIO || in_io->kind == LIBBIN_IO_STRING) \
298
+ src = RSTRING_PTR(in_io->buf_str) + (in_io->pos - (ptrdiff_t)cnt); \
299
+ else if (in_io->kind == LIBBIN_IO_GENERIC) \
300
+ src = RSTRING_PTR(guard); \
301
+ char *dst = libbin_io_write_reserve_direct(out_io, cnt); \
302
+ for (long i = 0; i < n; i++) { \
303
+ MAPPED_TYPE elem; \
304
+ memcpy(&elem, src + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
305
+ elem = MAPPED_SWAP(elem); \
306
+ memcpy(dst + (size_t)i * sizeof(MAPPED_TYPE), &elem, sizeof(MAPPED_TYPE)); \
307
+ } \
308
+ } else { \
309
+ VALUE out_str = rb_str_buf_new((long)cnt); \
310
+ char *dst = RSTRING_PTR(out_str); \
311
+ for (long i = 0; i < n; i++) { \
312
+ MAPPED_TYPE elem; \
313
+ memcpy(&elem, src + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
314
+ rb_ary_push(res, RUBY_CONVERT(NATIVE_CONVERT(elem))); \
315
+ elem = MAPPED_SWAP(elem); \
316
+ memcpy(dst + (size_t)i * sizeof(MAPPED_TYPE), &elem, sizeof(MAPPED_TYPE)); \
317
+ } \
318
+ rb_str_set_len(out_str, (long)cnt); \
319
+ if (out_io->kind == LIBBIN_IO_GENERIC) \
320
+ libbin_io_write_ruby_str(out_io, out_str); \
321
+ else \
322
+ libbin_io_write_bytes(out_io, RSTRING_PTR(out_str), cnt); \
323
+ RB_GC_GUARD(out_str); \
324
+ } \
325
+ } else { \
326
+ for (long i = 0; i < n; i++) { \
327
+ MAPPED_TYPE elem; \
328
+ memcpy(&elem, src + (size_t)i * sizeof(MAPPED_TYPE), sizeof(MAPPED_TYPE)); \
329
+ rb_ary_push(res, RUBY_CONVERT(NATIVE_CONVERT(elem))); \
330
+ } \
331
+ if (out_io->kind == LIBBIN_IO_GENERIC && in_io->kind == LIBBIN_IO_GENERIC) \
332
+ libbin_io_write_ruby_str(out_io, guard); \
333
+ else \
334
+ libbin_io_write_bytes(out_io, src, cnt); \
140
335
  } \
141
336
  } else { \
142
- res = RUBY_CONVERT(NATIVE_CONVERT(*data)); \
143
- if (SWAP_COND) \
144
- *data = MAPPED_SWAP(*data); \
337
+ MAPPED_TYPE elem; \
338
+ memcpy(&elem, src, sizeof(MAPPED_TYPE)); \
339
+ res = RUBY_CONVERT(NATIVE_CONVERT(elem)); \
340
+ if (SWAP_COND) { \
341
+ elem = MAPPED_SWAP(elem); \
342
+ libbin_io_write_bytes(out_io, &elem, sizeof(MAPPED_TYPE)); \
343
+ } else if (out_io->kind == LIBBIN_IO_GENERIC && in_io->kind == LIBBIN_IO_GENERIC) { \
344
+ libbin_io_write_ruby_str(out_io, guard); \
345
+ } else { \
346
+ libbin_io_write_bytes(out_io, &elem, sizeof(MAPPED_TYPE)); \
347
+ } \
145
348
  } \
146
- } while(0)
147
-
148
- #define MAKE_CONVERT_ENDIAN(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
149
- do { \
150
- if (little_input) \
151
- MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, !little_output, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
152
- else \
153
- MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, little_output, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
154
349
  } while (0)
155
350
 
156
- #define MAKE_CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
157
- MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, 0, RUBY_CONVERT, NATIVE_CONVERT)
351
+ #define MAKE_CONVERT_ENDIAN(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
352
+ do { \
353
+ if (!input_big) \
354
+ MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, output_big, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
355
+ else \
356
+ MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, !output_big, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
357
+ } while (0)
158
358
 
359
+ #define MAKE_CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
360
+ do { \
361
+ (void)input_big; \
362
+ (void)output_big; \
363
+ MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, 0, RUBY_CONVERT, NATIVE_CONVERT); \
364
+ } while (0)
159
365
 
160
366
  /* convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil) */
161
- #define MAKE_TYPE_CONVERT(CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT, CONVERT) \
162
- static VALUE CLASS ## _convert(int argc, VALUE* argv, VALUE self) { \
163
- (void)self; \
164
- VALUE input; \
165
- VALUE output; \
166
- VALUE input_big; \
167
- VALUE output_big; \
168
- VALUE length; \
169
- rb_scan_args(argc, argv, "25", &input, &output, &input_big, &output_big, NULL, NULL, &length); \
170
- if (NIL_P(input_big)) \
171
- input_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
172
- unsigned little_input = RTEST(input_big) ? 0 : 1; \
173
- if (NIL_P(output_big)) \
174
- output_big = little_input ? Qtrue : Qfalse; \
175
- unsigned little_output = RTEST(output_big) ? 0 : 1; \
176
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
177
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
178
- VALUE res; \
179
- VALUE str = rb_funcall(input, id_read, 1, ULL2NUM(cnt)); \
180
- if (NIL_P(str) || RSTRING_LEN(str) < (long)cnt) \
181
- rb_raise(rb_eRuntimeError, "could not read enough data: got %ld needed %zu", \
182
- NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
183
- MAPPED_TYPE *data = (MAPPED_TYPE *)RSTRING_PTR(str); \
184
- CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT); \
185
- rb_funcall(output, id_write, 1, str); \
186
- return res; \
187
- }
188
-
189
- #define MAKE_CLASS_DEFINE(CLASS_NAME, CLASS) \
190
- static void define_ ## CLASS() { \
191
- CLASS = rb_define_class_under(cDataConverter, #CLASS_NAME, cScalar); \
192
- rb_define_singleton_method(CLASS, "size", CLASS ## _size, -1); \
193
- rb_define_singleton_method(CLASS, "shape", CLASS ## _shape, -1); \
194
- rb_define_singleton_method(CLASS, "load", CLASS ## _load, -1); \
195
- rb_define_singleton_method(CLASS, "dump", CLASS ## _dump, -1); \
196
- rb_define_singleton_method(CLASS, "convert", CLASS ## _convert, -1); \
367
+ #define MAKE_TYPE_CONVERT(CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT, CONVERT) \
368
+ static VALUE CLASS ## _convert_fast(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) { \
369
+ long n = RTEST(length) ? NUM2LONG(length) : 1; \
370
+ if (n < 0) \
371
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", n); \
372
+ size_t cnt = sizeof(MAPPED_TYPE) * (size_t)n; \
373
+ VALUE guard = Qnil; \
374
+ const char *src = libbin_io_read_bytes(in_io, cnt, &guard); \
375
+ VALUE res; \
376
+ CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT); \
377
+ RB_GC_GUARD(guard); \
378
+ return res; \
379
+ } \
380
+ static VALUE CLASS ## _convert(int argc, VALUE* argv, VALUE self) { \
381
+ (void)self; \
382
+ VALUE input; \
383
+ VALUE output; \
384
+ VALUE input_big; \
385
+ VALUE output_big; \
386
+ VALUE parent; \
387
+ VALUE length; \
388
+ rb_scan_args(argc, argv, "25", &input, &output, &input_big, &output_big, &parent, NULL, &length); \
389
+ int ibig = RTEST(NIL_P(input_big) ? rb_funcall(mLibBin, id_default_big_p, 0) : input_big); \
390
+ int obig = NIL_P(output_big) ? !ibig : RTEST(output_big); \
391
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2); \
392
+ if (pctx) { \
393
+ libbin_ctx_sync_from_ruby(pctx); \
394
+ VALUE res = CLASS ## _convert_fast(&pctx->in_io, &pctx->out_io, ibig, obig, length); \
395
+ libbin_ctx_sync_to_ruby(pctx); \
396
+ return res; \
397
+ } \
398
+ libbin_io_t in_io, out_io; \
399
+ libbin_io_init_standalone(&in_io, input, 0); \
400
+ libbin_io_init_standalone(&out_io, output, 1); \
401
+ return CLASS ## _convert_fast(&in_io, &out_io, ibig, obig, length); \
402
+ }
403
+
404
+ #define MAKE_CLASS_DEFINE(CLASS_NAME, CLASS, KIND) \
405
+ static void define_ ## CLASS(void) { \
406
+ CLASS = rb_define_class_under(cStructure, #CLASS_NAME, cScalar); \
407
+ rb_define_singleton_method(CLASS, "align", CLASS ## _align, 0); \
408
+ rb_define_singleton_method(CLASS, "size", CLASS ## _size, -1); \
409
+ rb_define_singleton_method(CLASS, "shape", CLASS ## _shape, -1); \
410
+ rb_define_singleton_method(CLASS, "load", CLASS ## _load, -1); \
411
+ rb_define_singleton_method(CLASS, "dump", CLASS ## _dump, -1); \
412
+ rb_define_singleton_method(CLASS, "convert", CLASS ## _convert, -1); \
413
+ cScalar_classes[KIND] = CLASS; \
414
+ scalar_load_fns[KIND] = CLASS ## _load_fast; \
415
+ scalar_dump_fns[KIND] = CLASS ## _dump_fast; \
416
+ scalar_convert_fns[KIND] = CLASS ## _convert_fast; \
197
417
  }
198
418
 
199
419
  #define MAKE_STATIC_OBJECT(CLASS) \
200
420
  static VALUE CLASS;
201
421
 
202
- #define MAKE_CLASS_TYPE_ENDIAN_EX(CLASS_NAME, CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
203
- MAKE_STATIC_OBJECT(CLASS) \
422
+ #define MAKE_CLASS_TYPE_ENDIAN_EX(CLASS_NAME, CLASS, KIND, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
423
+ MAKE_STATIC_OBJECT(CLASS) \
424
+ MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
204
425
  MAKE_TYPE_SIZE(CLASS, MAPPED_TYPE) \
205
426
  MAKE_TYPE_SHAPE(CLASS, MAPPED_TYPE) \
206
427
  MAKE_TYPE_LOAD(CLASS, MAPPED_TYPE, RUBY_CONVERT_TO, NATIVE_CONVERT_TO, MAKE_LOAD ## ENDIAN) \
207
428
  MAKE_TYPE_DUMP(CLASS, MAPPED_TYPE, RUBY_CONVERT_FROM, NATIVE_CONVERT_FROM, MAKE_DUMP ## ENDIAN) \
208
429
  MAKE_TYPE_CONVERT(CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, NATIVE_CONVERT_TO, MAKE_CONVERT ## ENDIAN) \
209
- MAKE_CLASS_DEFINE(CLASS_NAME, CLASS)
430
+ MAKE_CLASS_DEFINE(CLASS_NAME, CLASS, KIND)
210
431
 
211
- #define MAKE_CLASS_TYPE_ENDIAN(CLASS_NAME, CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
212
- MAKE_CLASS_TYPE_ENDIAN_EX(CLASS_NAME, CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN)
432
+ #define MAKE_CLASS_TYPE_ENDIAN(CLASS_NAME, CLASS, KIND, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
433
+ MAKE_CLASS_TYPE_ENDIAN_EX(CLASS_NAME, CLASS, KIND, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN)
213
434
 
214
435
  #define ENDIAN_little
215
436
  #define ENDIAN_big
@@ -229,11 +450,13 @@ static VALUE CLASS;
229
450
  #define MAKE_CONVERT_NAME_MAKE_SUFFIX(NAME, ENDIAN, MAKE_SUFFIX) MAKE_CONVERT_NAME_SUFFIX(NAME, MAKE_SUFFIX(ENDIAN))
230
451
  #define MAKE_CLASS_NAME(CLASS_NAME, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(CLASS_NAME, ENDIAN, MAKE_ENDIAN_SUFFIX)
231
452
  #define MAKE_C_CLASS_NAME(CLASS_NAME, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(c ## CLASS_NAME, ENDIAN, MAKE_ENDIAN_SUFFIX)
453
+ #define MAKE_KIND_NAME(KIND_PREFIX, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(SCALAR_ ## KIND_PREFIX, ENDIAN, MAKE_ENDIAN_SUFFIX)
232
454
  #define MAKE_CONVERT_NAME(NAME, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(NAME, ENDIAN, MAKE_ENDIAN_suffix)
233
455
 
234
- #define MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
456
+ #define MAKE_CLASS(CLASS_NAME, KIND_PREFIX, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, ENDIAN) \
235
457
  MAKE_CLASS_TYPE_ENDIAN(MAKE_CLASS_NAME(CLASS_NAME, ENDIAN), \
236
458
  MAKE_C_CLASS_NAME(CLASS_NAME, ENDIAN), \
459
+ MAKE_KIND_NAME(KIND_PREFIX, ENDIAN), \
237
460
  uint ## SIZE ## _t, \
238
461
  bswap_uint ## SIZE, \
239
462
  RUBY_CONVERT_TO, \
@@ -242,10 +465,10 @@ static VALUE CLASS;
242
465
  MAKE_CONVERT_NAME(NATIVE_CONVERT_FROM, ENDIAN), \
243
466
  MAKE_ENDIAN(ENDIAN))
244
467
 
245
- #define MAKE_CLASSES(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM) \
246
- MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, nil) \
247
- MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, little) \
248
- MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, big)
468
+ #define MAKE_CLASSES(CLASS_NAME, KIND_PREFIX, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM) \
469
+ MAKE_CLASS(CLASS_NAME, KIND_PREFIX, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, nil) \
470
+ MAKE_CLASS(CLASS_NAME, KIND_PREFIX, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, little) \
471
+ MAKE_CLASS(CLASS_NAME, KIND_PREFIX, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, big)
249
472
 
250
473
  #define MAKE_CALL_DEFINE(CLASS_NAME) \
251
474
  do { \
@@ -262,88 +485,86 @@ do { \
262
485
  MAKE_CALL_DEFINE_EX(MAKE_C_CLASS_NAME(CLASS_NAME, big)); \
263
486
  } while (0)
264
487
 
265
- static inline float half_to_float_le(uint16_t val) {
488
+ static inline double half_to_float_le(uint16_t val) {
266
489
  val = unpack_half_le(val);
267
- union float_u u;
268
- u.i = half_to_float(val);
269
- return u.f;
490
+ return half_to_double(val);
270
491
  }
271
492
 
272
- static inline float half_to_float_be(uint16_t val) {
493
+ static inline double half_to_float_be(uint16_t val) {
273
494
  val = unpack_half_be(val);
274
- union float_u u;
275
- u.i = half_to_float(val);
276
- return u.f;
495
+ return half_to_double(val);
277
496
  }
278
497
 
279
- static inline uint16_t float_to_half_le(float f) {
280
- uint16_t res;
281
- union float_u u;
282
- u.f = f;
283
- res = half_from_float(u.i);
284
- res = pack_half_le(res);
285
- return res;
498
+ static inline uint16_t float_to_half_le(double d) {
499
+ uint16_t res = half_from_double(d);
500
+ return pack_half_le(res);
286
501
  }
287
502
 
288
- static inline uint16_t float_to_half_be(float f) {
289
- uint16_t res;
290
- union float_u u;
291
- u.f = f;
292
- res = half_from_float(u.i);
293
- res = pack_half_be(res);
294
- return res;
503
+ static inline uint16_t float_to_half_be(double d) {
504
+ uint16_t res = half_from_double(d);
505
+ return pack_half_be(res);
295
506
  }
296
507
 
508
+ MAKE_CLASSES(Half, HALF, 16, DBL2NUM, NUM2DBL, half_to_float, float_to_half)
297
509
 
298
- MAKE_CLASSES(Half, 16, DBL2NUM, NUM2DBL, half_to_float, float_to_half)
299
-
300
- static inline float pghalf_to_float_le(uint16_t val) {
510
+ static inline double pghalf_to_float_le(uint16_t val) {
301
511
  val = unpack_pghalf_le(val);
302
- union float_u u;
303
- u.i = pghalf_to_float(val);
304
- return u.f;
512
+ return pghalf_to_double(val);
305
513
  }
306
514
 
307
- static inline float pghalf_to_float_be(uint16_t val) {
515
+ static inline double pghalf_to_float_be(uint16_t val) {
308
516
  val = unpack_pghalf_be(val);
309
- union float_u u;
310
- u.i = pghalf_to_float(val);
311
- return u.f;
517
+ return pghalf_to_double(val);
312
518
  }
313
519
 
314
- static inline uint16_t float_to_pghalf_le(float f) {
315
- uint16_t res;
316
- union float_u u;
317
- u.f = f;
318
- res = pghalf_from_float(u.i);
319
- res = pack_pghalf_le(res);
320
- return res;
520
+ static inline uint16_t float_to_pghalf_le(double d) {
521
+ uint16_t res = pghalf_from_double(d);
522
+ return pack_pghalf_le(res);
321
523
  }
322
524
 
323
- static inline uint16_t float_to_pghalf_be(float f) {
324
- uint16_t res;
325
- union float_u u;
326
- u.f = f;
327
- res = pghalf_from_float(u.i);
328
- res = pack_pghalf_be(res);
329
- return res;
525
+ static inline uint16_t float_to_pghalf_be(double d) {
526
+ uint16_t res = pghalf_from_double(d);
527
+ return pack_pghalf_be(res);
330
528
  }
331
529
 
332
- MAKE_CLASSES(PGHalf, 16, DBL2NUM, NUM2DBL, pghalf_to_float, float_to_pghalf)
530
+ MAKE_CLASSES(PGHalf, PGHALF, 16, DBL2NUM, NUM2DBL, pghalf_to_float, float_to_pghalf)
333
531
 
334
- MAKE_CLASSES(Int8, 8, INT2FIX, NUM2SHORT, unpack_int8, pack_int8)
335
- MAKE_CLASSES(UInt8, 8, USHORT2NUM, NUM2USHORT, unpack_uint8, pack_uint8)
336
- MAKE_CLASSES(Int16, 16, INT2FIX, NUM2SHORT, unpack_int16, pack_int16)
337
- MAKE_CLASSES(UInt16, 16, USHORT2NUM, NUM2USHORT, unpack_uint16, pack_uint16)
338
- MAKE_CLASSES(Int32, 32, INT2NUM, NUM2INT, unpack_int32, pack_int32)
339
- MAKE_CLASSES(UInt32, 32, UINT2NUM, NUM2UINT, unpack_uint32, pack_uint32)
340
- MAKE_CLASSES(Int64, 64, LL2NUM, NUM2LL, unpack_int64, pack_int64)
341
- MAKE_CLASSES(UInt64, 64, ULL2NUM, NUM2ULL, unpack_uint64, pack_uint64)
342
- MAKE_CLASSES(Flt, 32, DBL2NUM, NUM2DBL, unpack_float, pack_float)
343
- MAKE_CLASSES(Double, 64, DBL2NUM, NUM2DBL, unpack_double, pack_double)
532
+ static inline int8_t num2int8(VALUE val) {
533
+ long v = NUM2LONG(val);
534
+ if (v < INT8_MIN)
535
+ rb_raise(rb_eRangeError, "integer %ld too small to convert to `int8_t'", v);
536
+ if (v > INT8_MAX)
537
+ rb_raise(rb_eRangeError, "integer %ld too big to convert to `int8_t'", v);
538
+ return (int8_t)v;
539
+ }
540
+
541
+ static inline uint8_t num2uint8(VALUE val) {
542
+ long v = NUM2LONG(val);
543
+ if (v < INT8_MIN)
544
+ rb_raise(rb_eRangeError, "integer %ld too small to convert to `uint8_t'", v);
545
+ if (v > UINT8_MAX)
546
+ rb_raise(rb_eRangeError, "integer %ld too big to convert to `uint8_t'", v);
547
+ return (uint8_t)v;
548
+ }
549
+
550
+ MAKE_CLASSES(Int8, INT8, 8, INT2FIX, num2int8, unpack_int8, pack_int8)
551
+ MAKE_CLASSES(UInt8, UINT8, 8, INT2FIX, num2uint8, unpack_uint8, pack_uint8)
552
+ MAKE_CLASSES(Int16, INT16, 16, INT2FIX, NUM2SHORT, unpack_int16, pack_int16)
553
+ MAKE_CLASSES(UInt16, UINT16, 16, USHORT2NUM, NUM2USHORT, unpack_uint16, pack_uint16)
554
+ MAKE_CLASSES(Int32, INT32, 32, INT2NUM, NUM2INT, unpack_int32, pack_int32)
555
+ MAKE_CLASSES(UInt32, UINT32, 32, UINT2NUM, NUM2UINT, unpack_uint32, pack_uint32)
556
+ MAKE_CLASSES(Int64, INT64, 64, LL2NUM, NUM2LL, unpack_int64, pack_int64)
557
+ MAKE_CLASSES(UInt64, UINT64, 64, ULL2NUM, NUM2ULL, unpack_uint64, pack_uint64)
558
+ MAKE_CLASSES(Flt, FLT, 32, DBL2NUM, NUM2DBL, unpack_float, pack_float)
559
+ MAKE_CLASSES(Double, DOUBLE, 64, DBL2NUM, NUM2DBL, unpack_double, pack_double)
344
560
 
345
561
  static VALUE cStr;
346
562
 
563
+ static VALUE cStr_align(VALUE self) {
564
+ (void)self;
565
+ return INT2FIX(sizeof(char));
566
+ }
567
+
347
568
  static VALUE cStr_size(int argc, VALUE* argv, VALUE self) {
348
569
  (void)self;
349
570
  VALUE value;
@@ -352,7 +573,7 @@ static VALUE cStr_size(int argc, VALUE* argv, VALUE self) {
352
573
  if (RTEST(length))
353
574
  return length;
354
575
  else
355
- return rb_funcall(value, rb_intern("bytesize"), 0);
576
+ return rb_funcall(value, id_bytesize, 0);
356
577
  }
357
578
 
358
579
  static VALUE cStr_shape(int argc, VALUE* argv, VALUE self) {
@@ -367,87 +588,726 @@ static VALUE cStr_shape(int argc, VALUE* argv, VALUE self) {
367
588
  if (NIL_P(kind))
368
589
  kind = cDataShape;
369
590
  if (NIL_P(length)) {
591
+ Check_Type(value, T_STRING);
370
592
  VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + RSTRING_LEN(value)) };
371
593
  return rb_class_new_instance(2, args, kind);
372
594
  } else {
373
- VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + NUM2LL(length)) };
595
+ long long len = NUM2LL(length);
596
+ if (len < 0)
597
+ rb_raise(rb_eArgError, "length must be non-negative: %lld", len);
598
+ VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + len) };
374
599
  return rb_class_new_instance(2, args, kind);
375
600
  }
376
601
  }
377
602
 
603
+ static inline VALUE libbin_io_read_ruby_str(libbin_io_t *bio, long l, VALUE length) {
604
+ VALUE str = rb_funcall(bio->io, id_read, 1, length);
605
+ if (NIL_P(str) || RSTRING_LEN(str) < l)
606
+ rb_raise(eLibBinEOFError,
607
+ "could not read enough data: got %ld needed %zu",
608
+ NIL_P(str) ? 0L : RSTRING_LEN(str), (size_t)l);
609
+ if (bio->non_seekable)
610
+ bio->pos += (ptrdiff_t)l;
611
+ return str;
612
+ }
613
+
614
+ static VALUE cStr_load_fast(libbin_io_t *bio, int input_big, VALUE length) {
615
+ (void)input_big;
616
+ if (NIL_P(length))
617
+ return libbin_io_readline_nul(bio);
618
+ long l = NUM2LONG(length);
619
+ if (l < 0)
620
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
621
+ if (bio->kind == LIBBIN_IO_GENERIC)
622
+ return libbin_io_read_ruby_str(bio, l, length);
623
+ VALUE guard = Qnil;
624
+ const char *src = libbin_io_read_bytes(bio, (size_t)l, &guard);
625
+ VALUE res = rb_str_new(src, l);
626
+ RB_GC_GUARD(guard);
627
+ return res;
628
+ }
629
+
378
630
  static VALUE cStr_load(int argc, VALUE* argv, VALUE self) {
379
631
  (void)self;
380
- VALUE input;
381
- VALUE length;
382
- rb_scan_args(argc, argv, "14", &input, NULL, NULL, NULL, &length);
383
- if (NIL_P(length))
384
- return rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
385
- else {
386
- VALUE str = rb_funcall(input, id_read, 1, length);
387
- if (NIL_P(str) || RSTRING_LEN(str) < NUM2LONG(length))
388
- rb_raise(rb_eRuntimeError,
389
- "could not read enough data: got %ld needed %zu", RSTRING_LEN(str), NUM2LONG(length));
390
- return str;
632
+ VALUE input, parent, length;
633
+ rb_scan_args(argc, argv, "14", &input, NULL, &parent, NULL, &length);
634
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
635
+ if (pctx) {
636
+ libbin_ctx_sync_from_ruby(pctx);
637
+ VALUE res = cStr_load_fast(&pctx->in_io, 0, length);
638
+ libbin_ctx_sync_to_ruby(pctx);
639
+ return res;
391
640
  }
641
+ libbin_io_t bio;
642
+ libbin_io_init_standalone(&bio, input, 0);
643
+ return cStr_load_fast(&bio, 0, length);
392
644
  }
393
645
 
394
- static VALUE cStr_dump(int argc, VALUE* argv, VALUE self) {
395
- (void)self;
396
- VALUE value;
397
- VALUE output;
398
- VALUE length;
399
- rb_scan_args(argc, argv, "24", &value, &output, NULL, NULL, NULL, &length);
400
- if (NIL_P(length))
401
- rb_funcall(output, id_write, 1, value);
402
- else {
403
- long l = NUM2LONG(length);
404
- long vl = RSTRING_LEN(value);
646
+ static void cStr_dump_fixed(VALUE value, libbin_io_t *bio, long l) {
647
+ long vl = RSTRING_LEN(value);
648
+ if (bio->kind == LIBBIN_IO_GENERIC) {
405
649
  VALUE str;
406
650
  if (vl > l)
407
651
  str = rb_str_new(RSTRING_PTR(value), l);
652
+ else if (vl == l)
653
+ str = value;
408
654
  else {
409
655
  str = rb_str_buf_new(l);
410
656
  rb_str_cat(str, RSTRING_PTR(value), vl);
411
- for (long i = 0; i < l - vl; i++)
412
- rb_str_cat(str, "", 1);
657
+ rb_str_resize(str, l);
658
+ memset(RSTRING_PTR(str) + vl, 0, (size_t)(l - vl));
413
659
  }
414
- rb_funcall(output, id_write, 1, str);
660
+ libbin_io_write_ruby_str(bio, str);
661
+ } else {
662
+ long copy_len = vl > l ? l : vl;
663
+ libbin_io_write_bytes(bio, RSTRING_PTR(value), (size_t)copy_len);
664
+ if (l > copy_len)
665
+ libbin_io_write_zeros(bio, (size_t)(l - copy_len));
666
+ }
667
+ }
668
+
669
+ static void cStr_dump_fast(VALUE value, libbin_io_t *bio, int output_big, VALUE length) {
670
+ (void)output_big;
671
+ Check_Type(value, T_STRING);
672
+ if (NIL_P(length)) {
673
+ libbin_io_write_ruby_str(bio, value);
674
+ return;
675
+ }
676
+ long l = NUM2LONG(length);
677
+ if (l < 0)
678
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
679
+ cStr_dump_fixed(value, bio, l);
680
+ }
681
+
682
+ static VALUE cStr_dump(int argc, VALUE* argv, VALUE self) {
683
+ (void)self;
684
+ VALUE value, output, parent, length;
685
+ rb_scan_args(argc, argv, "24", &value, &output, NULL, &parent, NULL, &length);
686
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
687
+ if (pctx) {
688
+ libbin_ctx_sync_from_ruby(pctx);
689
+ cStr_dump_fast(value, &pctx->out_io, 0, length);
690
+ libbin_ctx_sync_to_ruby(pctx);
691
+ return Qnil;
415
692
  }
693
+ libbin_io_t bio;
694
+ libbin_io_init_standalone(&bio, output, 1);
695
+ cStr_dump_fast(value, &bio, 0, length);
416
696
  return Qnil;
417
697
  }
418
698
 
699
+ static VALUE cStr_convert_fast(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) {
700
+ (void)input_big;
701
+ (void)output_big;
702
+ VALUE str = cStr_load_fast(in_io, 0, length);
703
+ libbin_io_write_ruby_str(out_io, str);
704
+ return str;
705
+ }
706
+
419
707
  static VALUE cStr_convert(int argc, VALUE* argv, VALUE self) {
420
708
  (void)self;
421
- VALUE input;
422
- VALUE output;
423
- VALUE length;
424
- rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, NULL, NULL, &length);
425
- VALUE str;
426
- if (NIL_P(length))
427
- str = rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
428
- else {
429
- str = rb_funcall(input, id_read, 1, length);
430
- if (NIL_P(str) || RSTRING_LEN(str) < NUM2LONG(length))
431
- rb_raise(rb_eRuntimeError,
432
- "could not read enough data: got %ld needed %zu", RSTRING_LEN(str), NUM2LONG(length));
709
+ VALUE input, output, parent, length;
710
+ rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, &parent, NULL, &length);
711
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
712
+ if (pctx) {
713
+ libbin_ctx_sync_from_ruby(pctx);
714
+ VALUE res = cStr_convert_fast(&pctx->in_io, &pctx->out_io, 0, 0, length);
715
+ libbin_ctx_sync_to_ruby(pctx);
716
+ return res;
433
717
  }
434
- rb_funcall(output, id_write, 1, str);
435
- return str;
718
+ libbin_io_t in_io, out_io;
719
+ libbin_io_init_standalone(&in_io, input, 0);
720
+ libbin_io_init_standalone(&out_io, output, 1);
721
+ return cStr_convert_fast(&in_io, &out_io, 0, 0, length);
436
722
  }
437
723
 
438
- static void define_cStr() {
439
- cStr = rb_define_class_under(cDataConverter, "Str", cScalar);
724
+ static void define_cStr(void) {
725
+ cStr = rb_define_class_under(cStructure, "Str", cScalar);
726
+ rb_define_singleton_method(cStr, "align", cStr_align, 0);
440
727
  rb_define_singleton_method(cStr, "size", cStr_size, -1);
441
728
  rb_define_singleton_method(cStr, "shape", cStr_shape, -1);
442
729
  rb_define_singleton_method(cStr, "load", cStr_load, -1);
443
730
  rb_define_singleton_method(cStr, "dump", cStr_dump, -1);
444
731
  rb_define_singleton_method(cStr, "convert", cStr_convert, -1);
732
+ cScalar_classes[SCALAR_STR] = cStr;
733
+ scalar_load_fns[SCALAR_STR] = cStr_load_fast;
734
+ scalar_dump_fns[SCALAR_STR] = cStr_dump_fast;
735
+ scalar_convert_fns[SCALAR_STR] = cStr_convert_fast;
445
736
  }
446
737
 
447
- void define_cScalar() {
738
+ /* ZStr: NUL-terminated / NUL-trimmed C string */
739
+ static VALUE cZStr;
740
+
741
+ static VALUE cZStr_align(VALUE self) {
742
+ (void)self;
743
+ return INT2FIX(1);
744
+ }
745
+
746
+ static VALUE cZStr_size(int argc, VALUE *argv, VALUE self) {
747
+ (void)self;
748
+ VALUE value;
749
+ VALUE length;
750
+ rb_scan_args(argc, argv, "14", &value, NULL, NULL, NULL, &length);
751
+ if (RTEST(length)) {
752
+ long n = NUM2LONG(length);
753
+ if (n < 0)
754
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", n);
755
+ return length;
756
+ }
757
+ Check_Type(value, T_STRING);
758
+ return LONG2NUM(RSTRING_LEN(value) + 1);
759
+ }
760
+
761
+ static VALUE cZStr_shape(int argc, VALUE *argv, VALUE self) {
762
+ (void)self;
763
+ VALUE value, previous_offset, kind, length;
764
+ rb_scan_args(argc, argv, "15", &value, &previous_offset, NULL, NULL, &kind, &length);
765
+ if (NIL_P(previous_offset))
766
+ previous_offset = INT2FIX(0);
767
+ if (NIL_P(kind))
768
+ kind = cDataShape;
769
+ if (NIL_P(length)) {
770
+ Check_Type(value, T_STRING);
771
+ VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) + RSTRING_LEN(value)) };
772
+ return rb_class_new_instance(2, args, kind);
773
+ }
774
+ long long len = NUM2LL(length);
775
+ if (len < 0)
776
+ rb_raise(rb_eArgError, "length must be non-negative: %lld", len);
777
+ VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + len) };
778
+ return rb_class_new_instance(2, args, kind);
779
+ }
780
+
781
+ static inline VALUE cZStr_trim_trailing_nul(VALUE str) {
782
+ long slen = RSTRING_LEN(str);
783
+ if (slen > 0 && RSTRING_PTR(str)[slen - 1] == '\0')
784
+ rb_str_resize(str, slen - 1);
785
+ return str;
786
+ }
787
+
788
+ static VALUE cZStr_load_fast(libbin_io_t *bio, int input_big, VALUE length) {
789
+ (void)input_big;
790
+ if (NIL_P(length))
791
+ return cZStr_trim_trailing_nul(libbin_io_readline_nul(bio));
792
+ long l = NUM2LONG(length);
793
+ if (l < 0)
794
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
795
+ if (bio->kind == LIBBIN_IO_GENERIC) {
796
+ VALUE str = libbin_io_read_ruby_str(bio, l, length);
797
+ const char *ptr = RSTRING_PTR(str);
798
+ const char *nul = (const char *)memchr(ptr, '\0', (size_t)l);
799
+ if (nul)
800
+ return rb_str_new(ptr, (long)(nul - ptr));
801
+ return str;
802
+ }
803
+ VALUE guard = Qnil;
804
+ const char *src = libbin_io_read_bytes(bio, (size_t)l, &guard);
805
+ const char *nul = (const char *)memchr(src, '\0', (size_t)l);
806
+ long keep = nul ? (long)(nul - src) : l;
807
+ VALUE res = rb_str_new(src, keep);
808
+ RB_GC_GUARD(guard);
809
+ return res;
810
+ }
811
+
812
+ static VALUE cZStr_load(int argc, VALUE *argv, VALUE self) {
813
+ (void)self;
814
+ VALUE input, parent, length;
815
+ rb_scan_args(argc, argv, "14", &input, NULL, &parent, NULL, &length);
816
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
817
+ if (pctx) {
818
+ libbin_ctx_sync_from_ruby(pctx);
819
+ VALUE res = cZStr_load_fast(&pctx->in_io, 0, length);
820
+ libbin_ctx_sync_to_ruby(pctx);
821
+ return res;
822
+ }
823
+ libbin_io_t bio;
824
+ libbin_io_init_standalone(&bio, input, 0);
825
+ return cZStr_load_fast(&bio, 0, length);
826
+ }
827
+
828
+ static void cZStr_dump_fast(VALUE value, libbin_io_t *bio, int output_big, VALUE length) {
829
+ (void)output_big;
830
+ Check_Type(value, T_STRING);
831
+ if (NIL_P(length)) {
832
+ long vl = RSTRING_LEN(value);
833
+ if (bio->kind == LIBBIN_IO_GENERIC) {
834
+ VALUE tmp = rb_str_buf_new(vl + 1);
835
+ rb_str_cat(tmp, RSTRING_PTR(value), vl);
836
+ rb_str_cat(tmp, "\0", 1);
837
+ libbin_io_write_ruby_str(bio, tmp);
838
+ RB_GC_GUARD(tmp);
839
+ } else {
840
+ libbin_io_write_bytes(bio, RSTRING_PTR(value), (size_t)vl);
841
+ libbin_io_write_zeros(bio, 1);
842
+ }
843
+ return;
844
+ }
845
+ long l = NUM2LONG(length);
846
+ if (l < 0)
847
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", l);
848
+ cStr_dump_fixed(value, bio, l);
849
+ }
850
+
851
+ static VALUE cZStr_dump(int argc, VALUE *argv, VALUE self) {
852
+ (void)self;
853
+ VALUE value, output, parent, length;
854
+ rb_scan_args(argc, argv, "24", &value, &output, NULL, &parent, NULL, &length);
855
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
856
+ if (pctx) {
857
+ libbin_ctx_sync_from_ruby(pctx);
858
+ cZStr_dump_fast(value, &pctx->out_io, 0, length);
859
+ libbin_ctx_sync_to_ruby(pctx);
860
+ return Qnil;
861
+ }
862
+ libbin_io_t bio;
863
+ libbin_io_init_standalone(&bio, output, 1);
864
+ cZStr_dump_fast(value, &bio, 0, length);
865
+ return Qnil;
866
+ }
867
+
868
+ static VALUE cZStr_convert_fast(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) {
869
+ (void)input_big;
870
+ (void)output_big;
871
+ if (NIL_P(length)) {
872
+ VALUE raw = libbin_io_readline_nul(in_io);
873
+ libbin_io_write_ruby_str(out_io, raw);
874
+ return cZStr_trim_trailing_nul(raw);
875
+ }
876
+ VALUE raw = cStr_load_fast(in_io, 0, length);
877
+ libbin_io_write_ruby_str(out_io, raw);
878
+ long l = RSTRING_LEN(raw);
879
+ const char *ptr = RSTRING_PTR(raw);
880
+ const char *nul = (const char *)memchr(ptr, '\0', (size_t)l);
881
+ if (nul)
882
+ return rb_str_new(ptr, (long)(nul - ptr));
883
+ return raw;
884
+ }
885
+
886
+ static VALUE cZStr_convert(int argc, VALUE *argv, VALUE self) {
887
+ (void)self;
888
+ VALUE input, output, parent, length;
889
+ rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, &parent, NULL, &length);
890
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
891
+ if (pctx) {
892
+ libbin_ctx_sync_from_ruby(pctx);
893
+ VALUE res = cZStr_convert_fast(&pctx->in_io, &pctx->out_io, 0, 0, length);
894
+ libbin_ctx_sync_to_ruby(pctx);
895
+ return res;
896
+ }
897
+ libbin_io_t in_io, out_io;
898
+ libbin_io_init_standalone(&in_io, input, 0);
899
+ libbin_io_init_standalone(&out_io, output, 1);
900
+ return cZStr_convert_fast(&in_io, &out_io, 0, 0, length);
901
+ }
902
+
903
+ static void define_cZStr(void) {
904
+ cZStr = rb_define_class_under(cStructure, "ZStr", cScalar);
905
+ rb_define_singleton_method(cZStr, "align", cZStr_align, 0);
906
+ rb_define_singleton_method(cZStr, "size", cZStr_size, -1);
907
+ rb_define_singleton_method(cZStr, "shape", cZStr_shape, -1);
908
+ rb_define_singleton_method(cZStr, "load", cZStr_load, -1);
909
+ rb_define_singleton_method(cZStr, "dump", cZStr_dump, -1);
910
+ rb_define_singleton_method(cZStr, "convert", cZStr_convert, -1);
911
+ cScalar_classes[SCALAR_ZSTR] = cZStr;
912
+ scalar_load_fns[SCALAR_ZSTR] = cZStr_load_fast;
913
+ scalar_dump_fns[SCALAR_ZSTR] = cZStr_dump_fast;
914
+ scalar_convert_fns[SCALAR_ZSTR] = cZStr_convert_fast;
915
+ }
916
+
917
+ void libbin_copy_bytes(libbin_io_t *in_io, libbin_io_t *out_io, size_t cnt) {
918
+ size_t remaining = cnt;
919
+ while (remaining > 0) {
920
+ size_t chunk = remaining > 8192 ? 8192 : remaining;
921
+ VALUE guard = Qnil;
922
+ const char *src = libbin_io_read_bytes(in_io, chunk, &guard);
923
+ if (out_io->kind == LIBBIN_IO_GENERIC && in_io->kind == LIBBIN_IO_GENERIC &&
924
+ !NIL_P(guard) && RB_TYPE_P(guard, T_STRING) && (size_t)RSTRING_LEN(guard) == chunk)
925
+ libbin_io_write_ruby_str(out_io, guard);
926
+ else
927
+ libbin_io_write_bytes(out_io, src, chunk);
928
+ RB_GC_GUARD(guard);
929
+ remaining -= chunk;
930
+ }
931
+ }
932
+
933
+ static inline VALUE libbin_make_padding_shape(VALUE kind, VALUE start_val, VALUE end_val) {
934
+ VALUE shape_klass = kind;
935
+ if (shape_klass == cDataShape && rb_const_defined(cDataShape, id_Padding))
936
+ shape_klass = rb_const_get(cDataShape, id_Padding);
937
+ VALUE args[] = { start_val, end_val };
938
+ return rb_class_new_instance(2, args, shape_klass);
939
+ }
940
+
941
+ static inline uint8_t libbin_fill_byte_for_class(VALUE self) {
942
+ VALUE fill_val = rb_attr_get(self, id_fill);
943
+ if (NIL_P(fill_val))
944
+ return 0;
945
+ return (uint8_t)(NUM2INT(fill_val) & 0xff);
946
+ }
947
+
948
+ /* Skip: anonymous fixed-byte padding directive */
949
+ static VALUE cSkip;
950
+
951
+ static VALUE cSkip_align(VALUE self) {
952
+ (void)self;
953
+ return INT2FIX(1);
954
+ }
955
+
956
+ static VALUE cSkip_size(int argc, VALUE *argv, VALUE self) {
957
+ (void)self;
958
+ VALUE length;
959
+ rb_scan_args(argc, argv, "05", NULL, NULL, NULL, NULL, &length);
960
+ if (RTEST(length)) {
961
+ long long len = NUM2LL(length);
962
+ if (len < 0)
963
+ rb_raise(rb_eArgError, "length must be non-negative: %lld", len);
964
+ return length;
965
+ }
966
+ return INT2FIX(0);
967
+ }
968
+
969
+ static VALUE cSkip_shape(int argc, VALUE *argv, VALUE self) {
970
+ (void)self;
971
+ VALUE previous_offset, kind, length;
972
+ rb_scan_args(argc, argv, "06", NULL, &previous_offset, NULL, NULL, &kind, &length);
973
+ if (NIL_P(previous_offset))
974
+ previous_offset = INT2FIX(0);
975
+ if (NIL_P(kind))
976
+ kind = cDataShape;
977
+ long long len = RTEST(length) ? NUM2LL(length) : 0;
978
+ if (len < 0)
979
+ rb_raise(rb_eArgError, "length must be non-negative: %lld", len);
980
+ if (len == 0)
981
+ return Qnil;
982
+ ptrdiff_t pos = NUM2LL(previous_offset);
983
+ return libbin_make_padding_shape(kind, previous_offset, LL2NUM(pos + (ptrdiff_t)len - 1));
984
+ }
985
+
986
+ static VALUE cSkip_load_fast(libbin_io_t *bio, int input_big, VALUE length) {
987
+ (void)input_big;
988
+ long len = RTEST(length) ? NUM2LONG(length) : 0;
989
+ if (len < 0)
990
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", len);
991
+ libbin_io_skip_read(bio, (size_t)len);
992
+ return Qnil;
993
+ }
994
+
995
+ static VALUE cSkip_load(int argc, VALUE *argv, VALUE self) {
996
+ (void)self;
997
+ VALUE input, parent, length;
998
+ rb_scan_args(argc, argv, "14", &input, NULL, &parent, NULL, &length);
999
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
1000
+ if (pctx) {
1001
+ libbin_ctx_sync_from_ruby(pctx);
1002
+ VALUE res = cSkip_load_fast(&pctx->in_io, 0, length);
1003
+ libbin_ctx_sync_to_ruby(pctx);
1004
+ return res;
1005
+ }
1006
+ libbin_io_t bio;
1007
+ libbin_io_init_standalone(&bio, input, 0);
1008
+ return cSkip_load_fast(&bio, 0, length);
1009
+ }
1010
+
1011
+ static void cSkip_dump_fast(VALUE value, libbin_io_t *bio, int output_big, VALUE length) {
1012
+ (void)value;
1013
+ (void)output_big;
1014
+ long len = RTEST(length) ? NUM2LONG(length) : 0;
1015
+ if (len < 0)
1016
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", len);
1017
+ libbin_io_write_zeros(bio, (size_t)len);
1018
+ }
1019
+
1020
+ static VALUE cSkip_dump(int argc, VALUE *argv, VALUE self) {
1021
+ VALUE value, output, parent, length;
1022
+ rb_scan_args(argc, argv, "24", &value, &output, NULL, &parent, NULL, &length);
1023
+ (void)value;
1024
+ long len = RTEST(length) ? NUM2LONG(length) : 0;
1025
+ if (len < 0)
1026
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", len);
1027
+ uint8_t fill_byte = libbin_fill_byte_for_class(self);
1028
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
1029
+ if (pctx) {
1030
+ libbin_ctx_sync_from_ruby(pctx);
1031
+ libbin_io_write_fill(&pctx->out_io, fill_byte, (size_t)len);
1032
+ libbin_ctx_sync_to_ruby(pctx);
1033
+ return Qnil;
1034
+ }
1035
+ libbin_io_t bio;
1036
+ libbin_io_init_standalone(&bio, output, 1);
1037
+ libbin_io_write_fill(&bio, fill_byte, (size_t)len);
1038
+ return Qnil;
1039
+ }
1040
+
1041
+ static VALUE cSkip_convert_fast(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) {
1042
+ (void)input_big;
1043
+ (void)output_big;
1044
+ long len = RTEST(length) ? NUM2LONG(length) : 0;
1045
+ if (len < 0)
1046
+ rb_raise(rb_eArgError, "length must be non-negative: %ld", len);
1047
+ libbin_copy_bytes(in_io, out_io, (size_t)len);
1048
+ return Qnil;
1049
+ }
1050
+
1051
+ static VALUE cSkip_convert(int argc, VALUE *argv, VALUE self) {
1052
+ (void)self;
1053
+ VALUE input, output, parent, length;
1054
+ rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, &parent, NULL, &length);
1055
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
1056
+ if (pctx) {
1057
+ libbin_ctx_sync_from_ruby(pctx);
1058
+ VALUE res = cSkip_convert_fast(&pctx->in_io, &pctx->out_io, 0, 0, length);
1059
+ libbin_ctx_sync_to_ruby(pctx);
1060
+ return res;
1061
+ }
1062
+ libbin_io_t in_io, out_io;
1063
+ libbin_io_init_standalone(&in_io, input, 0);
1064
+ libbin_io_init_standalone(&out_io, output, 1);
1065
+ return cSkip_convert_fast(&in_io, &out_io, 0, 0, length);
1066
+ }
1067
+
1068
+ static void define_cSkip(void) {
1069
+ cSkip = rb_define_class_under(cStructure, "Skip", cScalar);
1070
+ rb_define_singleton_method(cSkip, "align", cSkip_align, 0);
1071
+ rb_define_singleton_method(cSkip, "size", cSkip_size, -1);
1072
+ rb_define_singleton_method(cSkip, "shape", cSkip_shape, -1);
1073
+ rb_define_singleton_method(cSkip, "load", cSkip_load, -1);
1074
+ rb_define_singleton_method(cSkip, "dump", cSkip_dump, -1);
1075
+ rb_define_singleton_method(cSkip, "convert", cSkip_convert, -1);
1076
+ cScalar_classes[SCALAR_SKIP] = cSkip;
1077
+ scalar_load_fns[SCALAR_SKIP] = cSkip_load_fast;
1078
+ scalar_dump_fns[SCALAR_SKIP] = cSkip_dump_fast;
1079
+ scalar_convert_fns[SCALAR_SKIP] = cSkip_convert_fast;
1080
+ }
1081
+
1082
+ /* AlignTo: pad/skip current stream position to a power-of-2 boundary */
1083
+ static VALUE cAlignTo;
1084
+
1085
+ static inline size_t libbin_validate_align_arg(VALUE length) {
1086
+ if (!RTEST(length))
1087
+ rb_raise(eLibBinAlignmentError, "alignment must be a power of 2");
1088
+ long long al = NUM2LL(length);
1089
+ if (al <= 0 || (al & (al - 1)) != 0)
1090
+ rb_raise(eLibBinAlignmentError, "alignment must be a power of 2");
1091
+ return (size_t)al;
1092
+ }
1093
+
1094
+ static VALUE cAlignTo_align(VALUE self) {
1095
+ (void)self;
1096
+ return INT2FIX(1);
1097
+ }
1098
+
1099
+ static VALUE cAlignTo_size(int argc, VALUE *argv, VALUE self) {
1100
+ (void)self;
1101
+ VALUE previous_offset, length;
1102
+ rb_scan_args(argc, argv, "05", NULL, &previous_offset, NULL, NULL, &length);
1103
+ size_t al = libbin_validate_align_arg(length);
1104
+ ptrdiff_t pos = NIL_P(previous_offset) ? 0 : NUM2LL(previous_offset);
1105
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1106
+ return ULL2NUM(pad);
1107
+ }
1108
+
1109
+ static VALUE cAlignTo_shape(int argc, VALUE *argv, VALUE self) {
1110
+ (void)self;
1111
+ VALUE previous_offset, kind, length;
1112
+ rb_scan_args(argc, argv, "06", NULL, &previous_offset, NULL, NULL, &kind, &length);
1113
+ if (NIL_P(previous_offset))
1114
+ previous_offset = INT2FIX(0);
1115
+ if (NIL_P(kind))
1116
+ kind = cDataShape;
1117
+ size_t al = libbin_validate_align_arg(length);
1118
+ ptrdiff_t pos = NUM2LL(previous_offset);
1119
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1120
+ if (pad == 0)
1121
+ return Qnil;
1122
+ return libbin_make_padding_shape(kind, previous_offset, LL2NUM(pos + (ptrdiff_t)pad - 1));
1123
+ }
1124
+
1125
+ static VALUE cAlignTo_load_fast(libbin_io_t *bio, int input_big, VALUE length) {
1126
+ (void)input_big;
1127
+ size_t al = libbin_validate_align_arg(length);
1128
+ ptrdiff_t pos = libbin_io_tell(bio);
1129
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1130
+ if (pad > 0)
1131
+ libbin_io_skip_read(bio, pad);
1132
+ return Qnil;
1133
+ }
1134
+
1135
+ static VALUE cAlignTo_load(int argc, VALUE *argv, VALUE self) {
1136
+ (void)self;
1137
+ VALUE input, parent, length;
1138
+ rb_scan_args(argc, argv, "14", &input, NULL, &parent, NULL, &length);
1139
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, Qnil, 0);
1140
+ if (pctx) {
1141
+ libbin_ctx_sync_from_ruby(pctx);
1142
+ VALUE res = cAlignTo_load_fast(&pctx->in_io, 0, length);
1143
+ libbin_ctx_sync_to_ruby(pctx);
1144
+ return res;
1145
+ }
1146
+ libbin_io_t bio;
1147
+ libbin_io_init(&bio, input, 0);
1148
+ return cAlignTo_load_fast(&bio, 0, length);
1149
+ }
1150
+
1151
+ static void cAlignTo_dump_fast(VALUE value, libbin_io_t *bio, int output_big, VALUE length) {
1152
+ (void)value;
1153
+ (void)output_big;
1154
+ size_t al = libbin_validate_align_arg(length);
1155
+ ptrdiff_t pos = libbin_io_tell(bio);
1156
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1157
+ if (pad > 0)
1158
+ libbin_io_write_zeros(bio, pad);
1159
+ }
1160
+
1161
+ static VALUE cAlignTo_dump(int argc, VALUE *argv, VALUE self) {
1162
+ VALUE value, output, parent, length;
1163
+ rb_scan_args(argc, argv, "24", &value, &output, NULL, &parent, NULL, &length);
1164
+ (void)value;
1165
+ size_t al = libbin_validate_align_arg(length);
1166
+ uint8_t fill_byte = libbin_fill_byte_for_class(self);
1167
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, Qnil, output, 1);
1168
+ if (pctx) {
1169
+ libbin_ctx_sync_from_ruby(pctx);
1170
+ ptrdiff_t pos = libbin_io_tell(&pctx->out_io);
1171
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1172
+ if (pad > 0)
1173
+ libbin_io_write_fill(&pctx->out_io, fill_byte, pad);
1174
+ libbin_ctx_sync_to_ruby(pctx);
1175
+ return Qnil;
1176
+ }
1177
+ libbin_io_t bio;
1178
+ libbin_io_init(&bio, output, 1);
1179
+ ptrdiff_t pos = libbin_io_tell(&bio);
1180
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1181
+ if (pad > 0)
1182
+ libbin_io_write_fill(&bio, fill_byte, pad);
1183
+ return Qnil;
1184
+ }
1185
+
1186
+ static VALUE cAlignTo_convert_fast(libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) {
1187
+ (void)input_big;
1188
+ (void)output_big;
1189
+ size_t al = libbin_validate_align_arg(length);
1190
+ ptrdiff_t pos = libbin_io_tell(in_io);
1191
+ size_t pad = (size_t)((-pos) & (ptrdiff_t)(al - 1));
1192
+ if (pad > 0)
1193
+ libbin_copy_bytes(in_io, out_io, pad);
1194
+ return Qnil;
1195
+ }
1196
+
1197
+ static VALUE cAlignTo_convert(int argc, VALUE *argv, VALUE self) {
1198
+ (void)self;
1199
+ VALUE input, output, parent, length;
1200
+ rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, &parent, NULL, &length);
1201
+ libbin_ctx_t *pctx = libbin_ctx_from_parent(parent, input, output, 2);
1202
+ if (pctx) {
1203
+ libbin_ctx_sync_from_ruby(pctx);
1204
+ VALUE res = cAlignTo_convert_fast(&pctx->in_io, &pctx->out_io, 0, 0, length);
1205
+ libbin_ctx_sync_to_ruby(pctx);
1206
+ return res;
1207
+ }
1208
+ libbin_io_t in_io, out_io;
1209
+ if (input == output) {
1210
+ libbin_io_init_generic(&in_io, input, 0);
1211
+ libbin_io_init_generic(&out_io, output, 1);
1212
+ } else {
1213
+ libbin_io_init(&in_io, input, 0);
1214
+ libbin_io_init(&out_io, output, 1);
1215
+ }
1216
+ return cAlignTo_convert_fast(&in_io, &out_io, 0, 0, length);
1217
+ }
1218
+
1219
+ static void define_cAlignTo(void) {
1220
+ cAlignTo = rb_define_class_under(cStructure, "AlignTo", cScalar);
1221
+ rb_define_singleton_method(cAlignTo, "align", cAlignTo_align, 0);
1222
+ rb_define_singleton_method(cAlignTo, "size", cAlignTo_size, -1);
1223
+ rb_define_singleton_method(cAlignTo, "shape", cAlignTo_shape, -1);
1224
+ rb_define_singleton_method(cAlignTo, "load", cAlignTo_load, -1);
1225
+ rb_define_singleton_method(cAlignTo, "dump", cAlignTo_dump, -1);
1226
+ rb_define_singleton_method(cAlignTo, "convert", cAlignTo_convert, -1);
1227
+ cScalar_classes[SCALAR_ALIGN_TO] = cAlignTo;
1228
+ scalar_load_fns[SCALAR_ALIGN_TO] = cAlignTo_load_fast;
1229
+ scalar_dump_fns[SCALAR_ALIGN_TO] = cAlignTo_dump_fast;
1230
+ scalar_convert_fns[SCALAR_ALIGN_TO] = cAlignTo_convert_fast;
1231
+ }
1232
+
1233
+ libbin_scalar_kind_t libbin_lookup_scalar_kind(VALUE klass) {
1234
+ if (!RB_TYPE_P(klass, T_CLASS))
1235
+ return SCALAR_NONE;
1236
+ for (int i = 1; i < SCALAR_COUNT; i++) {
1237
+ if (cScalar_classes[i] == klass)
1238
+ return (libbin_scalar_kind_t)i;
1239
+ }
1240
+ return SCALAR_NONE;
1241
+ }
1242
+
1243
+ size_t libbin_scalar_elem_size(libbin_scalar_kind_t kind) {
1244
+ switch (kind) {
1245
+ case SCALAR_INT8: case SCALAR_INT8_LE: case SCALAR_INT8_BE:
1246
+ case SCALAR_UINT8: case SCALAR_UINT8_LE: case SCALAR_UINT8_BE:
1247
+ case SCALAR_STR: case SCALAR_ZSTR: case SCALAR_SKIP:
1248
+ return 1;
1249
+ case SCALAR_HALF: case SCALAR_HALF_LE: case SCALAR_HALF_BE:
1250
+ case SCALAR_PGHALF: case SCALAR_PGHALF_LE: case SCALAR_PGHALF_BE:
1251
+ case SCALAR_INT16: case SCALAR_INT16_LE: case SCALAR_INT16_BE:
1252
+ case SCALAR_UINT16: case SCALAR_UINT16_LE: case SCALAR_UINT16_BE:
1253
+ return 2;
1254
+ case SCALAR_INT32: case SCALAR_INT32_LE: case SCALAR_INT32_BE:
1255
+ case SCALAR_UINT32: case SCALAR_UINT32_LE: case SCALAR_UINT32_BE:
1256
+ case SCALAR_FLT: case SCALAR_FLT_LE: case SCALAR_FLT_BE:
1257
+ return 4;
1258
+ case SCALAR_INT64: case SCALAR_INT64_LE: case SCALAR_INT64_BE:
1259
+ case SCALAR_UINT64: case SCALAR_UINT64_LE: case SCALAR_UINT64_BE:
1260
+ case SCALAR_DOUBLE: case SCALAR_DOUBLE_LE: case SCALAR_DOUBLE_BE:
1261
+ return 8;
1262
+ default:
1263
+ return 0;
1264
+ }
1265
+ }
1266
+
1267
+ VALUE libbin_scalar_load_fast(libbin_scalar_kind_t kind, libbin_io_t *io, int input_big, VALUE length) {
1268
+ return scalar_load_fns[kind](io, input_big, length);
1269
+ }
1270
+
1271
+ void libbin_scalar_dump_fast(libbin_scalar_kind_t kind, VALUE value, libbin_io_t *io, int output_big, VALUE length) {
1272
+ scalar_dump_fns[kind](value, io, output_big, length);
1273
+ }
1274
+
1275
+ VALUE libbin_scalar_convert_fast(libbin_scalar_kind_t kind, libbin_io_t *in_io, libbin_io_t *out_io, int input_big, int output_big, VALUE length) {
1276
+ return scalar_convert_fns[kind](in_io, out_io, input_big, output_big, length);
1277
+ }
1278
+
1279
+ void define_cScalar(void) {
448
1280
  id_read = rb_intern("read");
449
1281
  id_write = rb_intern("write");
450
- cScalar = rb_define_class_under(cDataConverter, "Scalar", rb_cObject);
1282
+ id_tell = rb_intern("tell");
1283
+ id_seek = rb_intern("seek");
1284
+ id_readline = rb_intern("readline");
1285
+ id_bytesize = rb_intern("bytesize");
1286
+ id_default_big_p = rb_intern("default_big?");
1287
+ id_string = rb_intern("string");
1288
+ id_pos = rb_intern("pos");
1289
+ id_pos_eq = rb_intern("pos=");
1290
+ id_closed_read_p = rb_intern("closed_read?");
1291
+ id_closed_write_p = rb_intern("closed_write?");
1292
+ id_StringIO = rb_intern("StringIO");
1293
+ id_fill = rb_intern("@fill");
1294
+ id_Padding = rb_intern("Padding");
1295
+
1296
+ rb_str_nul = rb_obj_freeze(rb_str_new("", 1));
1297
+ #ifdef HAVE_RB_RACTOR_MAKE_SHAREABLE
1298
+ rb_ractor_make_shareable(rb_str_nul);
1299
+ #endif
1300
+ rb_gc_register_mark_object(rb_str_nul);
1301
+
1302
+ if (rb_const_defined(rb_cObject, id_StringIO)) {
1303
+ cStringIO_cached = rb_const_get(rb_cObject, id_StringIO);
1304
+ rb_gc_register_mark_object(cStringIO_cached);
1305
+ }
1306
+
1307
+ libbin_defining_scalars = 1;
1308
+ cScalar = rb_define_class_under(cStructure, "Scalar", rb_cObject);
1309
+ rb_define_singleton_method(cScalar, "always_align", cScalar_always_align, 0);
1310
+ rb_define_singleton_method(cScalar, "singleton_method_added", cScalar_singleton_method_added, 1);
451
1311
  MAKE_CALL_DEFINES(Half);
452
1312
  MAKE_CALL_DEFINES(PGHalf);
453
1313
  MAKE_CALL_DEFINES(Int8);
@@ -461,6 +1321,8 @@ void define_cScalar() {
461
1321
  MAKE_CALL_DEFINES(Flt);
462
1322
  MAKE_CALL_DEFINES(Double);
463
1323
  define_cStr();
1324
+ define_cZStr();
1325
+ define_cSkip();
1326
+ define_cAlignTo();
1327
+ libbin_defining_scalars = 0;
464
1328
  }
465
-
466
-