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.
@@ -1,3 +1,4 @@
1
+ #include <string.h>
1
2
  #include "ruby.h"
2
3
  #include "./libbin_c.h"
3
4
 
@@ -5,12 +6,58 @@ VALUE cDataShape;
5
6
  VALUE cDataRange;
6
7
  VALUE cScalar;
7
8
 
8
- 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;
17
+
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];
9
30
 
10
31
  static VALUE cScalar_always_align(VALUE self) {
32
+ (void)self;
11
33
  return Qfalse;
12
34
  }
13
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
+
14
61
  /* align() */
15
62
  #define MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
16
63
  static VALUE CLASS ## _align(VALUE self) { \
@@ -24,9 +71,12 @@ static VALUE CLASS ## _size(int argc, VALUE* argv, VALUE self) { \
24
71
  (void)self; \
25
72
  VALUE length; \
26
73
  rb_scan_args(argc, argv, "05", NULL, NULL, NULL, NULL, &length); \
27
- if (RTEST(length)) \
28
- return ULL2NUM(sizeof(MAPPED_TYPE)*NUM2ULL(length)); \
29
- else \
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 \
30
80
  return INT2FIX(sizeof(MAPPED_TYPE)); \
31
81
  }
32
82
 
@@ -44,99 +94,180 @@ static VALUE CLASS ## _shape(int argc, VALUE* argv, VALUE self) {
44
94
  kind = cDataShape; \
45
95
  if (NIL_P(length)) \
46
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); \
47
100
  VALUE args[] = { previous_offset, \
48
- LL2NUM(NUM2LL(previous_offset) - 1 + NUM2LL(length) * (ptrdiff_t)sizeof(MAPPED_TYPE)) }; \
101
+ LL2NUM(NUM2LL(previous_offset) - 1 + len * (ptrdiff_t)sizeof(MAPPED_TYPE)) }; \
49
102
  return rb_class_new_instance(2, args, kind); \
50
103
  }
51
104
 
52
- #define MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
53
- do { \
54
- if (RTEST(length)) { \
55
- res = rb_ary_new_capa(n); \
56
- long i = 0; \
57
- for (MAPPED_TYPE *p = data; p != data + n; p++, i++) \
58
- rb_ary_store(res, i, RUBY_CONVERT(NATIVE_CONVERT(*p))); \
59
- } else \
60
- 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
+ } \
61
128
  } while (0)
62
129
 
63
130
  #define MAKE_LOAD_ENDIAN(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
64
131
  do { \
65
- if (little) \
132
+ if (!input_big) \
66
133
  MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
67
134
  else \
68
135
  MAKE_LOAD_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
69
136
  } while (0)
70
137
 
71
138
  #define MAKE_LOAD(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
72
- 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)
73
143
 
74
144
  /* load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil) */
75
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
+ } \
76
158
  static VALUE CLASS ## _load(int argc, VALUE* argv, VALUE self) { \
77
159
  (void)self; \
78
160
  VALUE input; \
79
161
  VALUE input_big; \
162
+ VALUE parent; \
80
163
  VALUE length; \
81
- rb_scan_args(argc, argv, "14", &input, &input_big, NULL, NULL, &length); \
82
- if (NIL_P(input_big)) \
83
- input_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
84
- unsigned little = RTEST(input_big) ? 0 : 1; \
85
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
86
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
87
- VALUE res; \
88
- VALUE str = rb_funcall(input, id_read, 1, ULL2NUM(cnt)); \
89
- if (NIL_P(str) || RSTRING_LEN(str) < (long)cnt) \
90
- rb_raise(rb_eRuntimeError, \
91
- "could not read enough data: got %ld needed %zu", \
92
- NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
93
- MAPPED_TYPE *data = (MAPPED_TYPE *)RSTRING_PTR(str); \
94
- LOAD(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
95
- RB_GC_GUARD(str); \
96
- 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); \
97
176
  }
98
177
 
99
- #define MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
100
- do { \
101
- if (RTEST(length)) \
102
- for (long i = 0; i < n; i++) { \
103
- MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(rb_ary_entry(value, i))); \
104
- rb_str_cat(str, (char *)&v, sizeof(MAPPED_TYPE)); \
105
- } \
106
- else { \
107
- MAPPED_TYPE v = NATIVE_CONVERT(RUBY_CONVERT(value)); \
108
- rb_str_cat(str, (char *)&v, sizeof(MAPPED_TYPE)); \
109
- } \
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
+ } \
110
227
  } while (0)
111
228
 
112
229
  #define MAKE_DUMP_ENDIAN(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
113
230
  do { \
114
- if (little) \
231
+ if (!output_big) \
115
232
  MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
116
233
  else \
117
234
  MAKE_DUMP_LOOP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
118
235
  } while (0)
119
236
 
120
237
  #define MAKE_DUMP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT) \
121
- 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)
122
242
 
123
243
  /* dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil) */
124
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
+ } \
125
252
  static VALUE CLASS ## _dump(int argc, VALUE* argv, VALUE self) { \
126
253
  (void)self; \
127
254
  VALUE value; \
128
255
  VALUE output; \
129
256
  VALUE output_big; \
257
+ VALUE parent; \
130
258
  VALUE length; \
131
- rb_scan_args(argc, argv, "24", &value, &output, &output_big, NULL, NULL, &length); \
132
- if (NIL_P(output_big)) \
133
- output_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
134
- unsigned little = RTEST(output_big) ? 0 : 1; \
135
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
136
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
137
- VALUE str = rb_str_buf_new((long)cnt); \
138
- DUMP(MAPPED_TYPE, RUBY_CONVERT, NATIVE_CONVERT); \
139
- 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); \
140
271
  return Qnil; \
141
272
  }
142
273
 
@@ -144,75 +275,151 @@ static VALUE CLASS ## _dump(int argc, VALUE* argv, VALUE self) {
144
275
  do { \
145
276
  if (RTEST(length)) { \
146
277
  res = rb_ary_new_capa(n); \
147
- long i = 0; \
148
- for (MAPPED_TYPE *p = data; p != data + n; p++, i++) { \
149
- rb_ary_store(res, i, RUBY_CONVERT(NATIVE_CONVERT(*p))); \
150
- if (SWAP_COND) \
151
- *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); \
152
335
  } \
153
336
  } else { \
154
- res = RUBY_CONVERT(NATIVE_CONVERT(*data)); \
155
- if (SWAP_COND) \
156
- *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
+ } \
157
348
  } \
158
- } while(0)
159
-
160
- #define MAKE_CONVERT_ENDIAN(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
161
- do { \
162
- if (little_input) \
163
- MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, !little_output, RUBY_CONVERT, NATIVE_CONVERT ## _ ## le ); \
164
- else \
165
- MAKE_CONVERT_LOOP(MAPPED_TYPE, MAPPED_SWAP, little_output, RUBY_CONVERT, NATIVE_CONVERT ## _ ## be ); \
166
349
  } while (0)
167
350
 
168
- #define MAKE_CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT) \
169
- 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)
170
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)
171
365
 
172
366
  /* convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil) */
173
- #define MAKE_TYPE_CONVERT(CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT, CONVERT) \
174
- static VALUE CLASS ## _convert(int argc, VALUE* argv, VALUE self) { \
175
- (void)self; \
176
- VALUE input; \
177
- VALUE output; \
178
- VALUE input_big; \
179
- VALUE output_big; \
180
- VALUE length; \
181
- rb_scan_args(argc, argv, "25", &input, &output, &input_big, &output_big, NULL, NULL, &length); \
182
- if (NIL_P(input_big)) \
183
- input_big = rb_funcall(mLibBin, rb_intern("default_big?"), 0); \
184
- unsigned little_input = RTEST(input_big) ? 0 : 1; \
185
- if (NIL_P(output_big)) \
186
- output_big = little_input ? Qtrue : Qfalse; \
187
- unsigned little_output = RTEST(output_big) ? 0 : 1; \
188
- long n = RTEST(length) ? NUM2LONG(length) : 1; \
189
- size_t cnt = sizeof(MAPPED_TYPE) * n; \
190
- VALUE res; \
191
- VALUE str = rb_funcall(input, id_read, 1, ULL2NUM(cnt)); \
192
- if (NIL_P(str) || RSTRING_LEN(str) < (long)cnt) \
193
- rb_raise(rb_eRuntimeError, "could not read enough data: got %ld needed %zu", \
194
- NIL_P(str) ? 0 : RSTRING_LEN(str), cnt); \
195
- MAPPED_TYPE *data = (MAPPED_TYPE *)RSTRING_PTR(str); \
196
- CONVERT(MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT, NATIVE_CONVERT); \
197
- rb_funcall(output, id_write, 1, str); \
198
- return res; \
199
- }
200
-
201
- #define MAKE_CLASS_DEFINE(CLASS_NAME, CLASS) \
202
- static void define_ ## CLASS() { \
203
- CLASS = rb_define_class_under(cStructure, #CLASS_NAME, cScalar); \
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); \
204
407
  rb_define_singleton_method(CLASS, "align", CLASS ## _align, 0); \
205
408
  rb_define_singleton_method(CLASS, "size", CLASS ## _size, -1); \
206
409
  rb_define_singleton_method(CLASS, "shape", CLASS ## _shape, -1); \
207
410
  rb_define_singleton_method(CLASS, "load", CLASS ## _load, -1); \
208
411
  rb_define_singleton_method(CLASS, "dump", CLASS ## _dump, -1); \
209
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; \
210
417
  }
211
418
 
212
419
  #define MAKE_STATIC_OBJECT(CLASS) \
213
420
  static VALUE CLASS;
214
421
 
215
- #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) \
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) \
216
423
  MAKE_STATIC_OBJECT(CLASS) \
217
424
  MAKE_TYPE_ALIGN(CLASS, MAPPED_TYPE) \
218
425
  MAKE_TYPE_SIZE(CLASS, MAPPED_TYPE) \
@@ -220,10 +427,10 @@ static VALUE CLASS;
220
427
  MAKE_TYPE_LOAD(CLASS, MAPPED_TYPE, RUBY_CONVERT_TO, NATIVE_CONVERT_TO, MAKE_LOAD ## ENDIAN) \
221
428
  MAKE_TYPE_DUMP(CLASS, MAPPED_TYPE, RUBY_CONVERT_FROM, NATIVE_CONVERT_FROM, MAKE_DUMP ## ENDIAN) \
222
429
  MAKE_TYPE_CONVERT(CLASS, MAPPED_TYPE, MAPPED_SWAP, RUBY_CONVERT_TO, NATIVE_CONVERT_TO, MAKE_CONVERT ## ENDIAN) \
223
- MAKE_CLASS_DEFINE(CLASS_NAME, CLASS)
430
+ MAKE_CLASS_DEFINE(CLASS_NAME, CLASS, KIND)
224
431
 
225
- #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) \
226
- 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)
227
434
 
228
435
  #define ENDIAN_little
229
436
  #define ENDIAN_big
@@ -243,11 +450,13 @@ static VALUE CLASS;
243
450
  #define MAKE_CONVERT_NAME_MAKE_SUFFIX(NAME, ENDIAN, MAKE_SUFFIX) MAKE_CONVERT_NAME_SUFFIX(NAME, MAKE_SUFFIX(ENDIAN))
244
451
  #define MAKE_CLASS_NAME(CLASS_NAME, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(CLASS_NAME, ENDIAN, MAKE_ENDIAN_SUFFIX)
245
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)
246
454
  #define MAKE_CONVERT_NAME(NAME, ENDIAN) MAKE_CONVERT_NAME_MAKE_SUFFIX(NAME, ENDIAN, MAKE_ENDIAN_suffix)
247
455
 
248
- #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) \
249
457
  MAKE_CLASS_TYPE_ENDIAN(MAKE_CLASS_NAME(CLASS_NAME, ENDIAN), \
250
458
  MAKE_C_CLASS_NAME(CLASS_NAME, ENDIAN), \
459
+ MAKE_KIND_NAME(KIND_PREFIX, ENDIAN), \
251
460
  uint ## SIZE ## _t, \
252
461
  bswap_uint ## SIZE, \
253
462
  RUBY_CONVERT_TO, \
@@ -256,10 +465,10 @@ static VALUE CLASS;
256
465
  MAKE_CONVERT_NAME(NATIVE_CONVERT_FROM, ENDIAN), \
257
466
  MAKE_ENDIAN(ENDIAN))
258
467
 
259
- #define MAKE_CLASSES(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM) \
260
- MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, nil) \
261
- MAKE_CLASS(CLASS_NAME, SIZE, RUBY_CONVERT_TO, RUBY_CONVERT_FROM, NATIVE_CONVERT_TO, NATIVE_CONVERT_FROM, little) \
262
- 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)
263
472
 
264
473
  #define MAKE_CALL_DEFINE(CLASS_NAME) \
265
474
  do { \
@@ -276,85 +485,78 @@ do { \
276
485
  MAKE_CALL_DEFINE_EX(MAKE_C_CLASS_NAME(CLASS_NAME, big)); \
277
486
  } while (0)
278
487
 
279
- static inline float half_to_float_le(uint16_t val) {
488
+ static inline double half_to_float_le(uint16_t val) {
280
489
  val = unpack_half_le(val);
281
- union float_u u;
282
- u.i = half_to_float(val);
283
- return u.f;
490
+ return half_to_double(val);
284
491
  }
285
492
 
286
- static inline float half_to_float_be(uint16_t val) {
493
+ static inline double half_to_float_be(uint16_t val) {
287
494
  val = unpack_half_be(val);
288
- union float_u u;
289
- u.i = half_to_float(val);
290
- return u.f;
495
+ return half_to_double(val);
291
496
  }
292
497
 
293
- static inline uint16_t float_to_half_le(float f) {
294
- uint16_t res;
295
- union float_u u;
296
- u.f = f;
297
- res = half_from_float(u.i);
298
- res = pack_half_le(res);
299
- 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);
300
501
  }
301
502
 
302
- static inline uint16_t float_to_half_be(float f) {
303
- uint16_t res;
304
- union float_u u;
305
- u.f = f;
306
- res = half_from_float(u.i);
307
- res = pack_half_be(res);
308
- 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);
309
506
  }
310
507
 
508
+ MAKE_CLASSES(Half, HALF, 16, DBL2NUM, NUM2DBL, half_to_float, float_to_half)
311
509
 
312
- MAKE_CLASSES(Half, 16, DBL2NUM, NUM2DBL, half_to_float, float_to_half)
313
-
314
- static inline float pghalf_to_float_le(uint16_t val) {
510
+ static inline double pghalf_to_float_le(uint16_t val) {
315
511
  val = unpack_pghalf_le(val);
316
- union float_u u;
317
- u.i = pghalf_to_float(val);
318
- return u.f;
512
+ return pghalf_to_double(val);
319
513
  }
320
514
 
321
- static inline float pghalf_to_float_be(uint16_t val) {
515
+ static inline double pghalf_to_float_be(uint16_t val) {
322
516
  val = unpack_pghalf_be(val);
323
- union float_u u;
324
- u.i = pghalf_to_float(val);
325
- return u.f;
517
+ return pghalf_to_double(val);
326
518
  }
327
519
 
328
- static inline uint16_t float_to_pghalf_le(float f) {
329
- uint16_t res;
330
- union float_u u;
331
- u.f = f;
332
- res = pghalf_from_float(u.i);
333
- res = pack_pghalf_le(res);
334
- 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);
335
523
  }
336
524
 
337
- static inline uint16_t float_to_pghalf_be(float f) {
338
- uint16_t res;
339
- union float_u u;
340
- u.f = f;
341
- res = pghalf_from_float(u.i);
342
- res = pack_pghalf_be(res);
343
- 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);
344
528
  }
345
529
 
346
- 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)
531
+
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
+ }
347
549
 
348
- MAKE_CLASSES(Int8, 8, INT2FIX, NUM2SHORT, unpack_int8, pack_int8)
349
- MAKE_CLASSES(UInt8, 8, USHORT2NUM, NUM2USHORT, unpack_uint8, pack_uint8)
350
- MAKE_CLASSES(Int16, 16, INT2FIX, NUM2SHORT, unpack_int16, pack_int16)
351
- MAKE_CLASSES(UInt16, 16, USHORT2NUM, NUM2USHORT, unpack_uint16, pack_uint16)
352
- MAKE_CLASSES(Int32, 32, INT2NUM, NUM2INT, unpack_int32, pack_int32)
353
- MAKE_CLASSES(UInt32, 32, UINT2NUM, NUM2UINT, unpack_uint32, pack_uint32)
354
- MAKE_CLASSES(Int64, 64, LL2NUM, NUM2LL, unpack_int64, pack_int64)
355
- MAKE_CLASSES(UInt64, 64, ULL2NUM, NUM2ULL, unpack_uint64, pack_uint64)
356
- MAKE_CLASSES(Flt, 32, DBL2NUM, NUM2DBL, unpack_float, pack_float)
357
- MAKE_CLASSES(Double, 64, DBL2NUM, NUM2DBL, unpack_double, pack_double)
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)
358
560
 
359
561
  static VALUE cStr;
360
562
 
@@ -371,7 +573,7 @@ static VALUE cStr_size(int argc, VALUE* argv, VALUE self) {
371
573
  if (RTEST(length))
372
574
  return length;
373
575
  else
374
- return rb_funcall(value, rb_intern("bytesize"), 0);
576
+ return rb_funcall(value, id_bytesize, 0);
375
577
  }
376
578
 
377
579
  static VALUE cStr_shape(int argc, VALUE* argv, VALUE self) {
@@ -386,159 +588,726 @@ static VALUE cStr_shape(int argc, VALUE* argv, VALUE self) {
386
588
  if (NIL_P(kind))
387
589
  kind = cDataShape;
388
590
  if (NIL_P(length)) {
591
+ Check_Type(value, T_STRING);
389
592
  VALUE args[] = { previous_offset, LL2NUM(NUM2LL(previous_offset) - 1 + RSTRING_LEN(value)) };
390
593
  return rb_class_new_instance(2, args, kind);
391
594
  } else {
392
- 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) };
393
599
  return rb_class_new_instance(2, args, kind);
394
600
  }
395
601
  }
396
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
+
397
630
  static VALUE cStr_load(int argc, VALUE* argv, VALUE self) {
398
631
  (void)self;
399
- VALUE input;
400
- VALUE length;
401
- rb_scan_args(argc, argv, "14", &input, NULL, NULL, NULL, &length);
402
- if (NIL_P(length))
403
- return rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
404
- else {
405
- VALUE str = rb_funcall(input, id_read, 1, length);
406
- if (NIL_P(str) || RSTRING_LEN(str) < NUM2LONG(length))
407
- rb_raise(rb_eRuntimeError,
408
- "could not read enough data: got %ld needed %zu", RSTRING_LEN(str), NUM2LONG(length));
409
- 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;
410
640
  }
641
+ libbin_io_t bio;
642
+ libbin_io_init_standalone(&bio, input, 0);
643
+ return cStr_load_fast(&bio, 0, length);
411
644
  }
412
645
 
413
- static VALUE cStr_dump(int argc, VALUE* argv, VALUE self) {
414
- (void)self;
415
- VALUE value;
416
- VALUE output;
417
- VALUE length;
418
- rb_scan_args(argc, argv, "24", &value, &output, NULL, NULL, NULL, &length);
419
- if (NIL_P(length))
420
- rb_funcall(output, id_write, 1, value);
421
- else {
422
- long l = NUM2LONG(length);
423
- 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) {
424
649
  VALUE str;
425
650
  if (vl > l)
426
651
  str = rb_str_new(RSTRING_PTR(value), l);
652
+ else if (vl == l)
653
+ str = value;
427
654
  else {
428
655
  str = rb_str_buf_new(l);
429
656
  rb_str_cat(str, RSTRING_PTR(value), vl);
430
- for (long i = 0; i < l - vl; i++)
431
- rb_str_cat(str, "", 1);
657
+ rb_str_resize(str, l);
658
+ memset(RSTRING_PTR(str) + vl, 0, (size_t)(l - vl));
432
659
  }
433
- 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;
434
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;
692
+ }
693
+ libbin_io_t bio;
694
+ libbin_io_init_standalone(&bio, output, 1);
695
+ cStr_dump_fast(value, &bio, 0, length);
435
696
  return Qnil;
436
697
  }
437
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
+
438
707
  static VALUE cStr_convert(int argc, VALUE* argv, VALUE self) {
439
708
  (void)self;
440
- VALUE input;
441
- VALUE output;
442
- VALUE length;
443
- rb_scan_args(argc, argv, "25", &input, &output, NULL, NULL, NULL, NULL, &length);
444
- VALUE str;
445
- if (NIL_P(length))
446
- str = rb_funcall(input, rb_intern("readline"), 1, rb_str_new_static("", 1));
447
- else {
448
- str = rb_funcall(input, id_read, 1, length);
449
- if (NIL_P(str) || RSTRING_LEN(str) < NUM2LONG(length))
450
- rb_raise(rb_eRuntimeError,
451
- "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;
452
717
  }
453
- rb_funcall(output, id_write, 1, str);
454
- 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);
455
722
  }
456
723
 
457
- static void define_cStr() {
724
+ static void define_cStr(void) {
458
725
  cStr = rb_define_class_under(cStructure, "Str", cScalar);
459
- /**
460
- * Returns the alignement of the underlying character type.
461
- * @return [Integer]
462
- */
463
726
  rb_define_singleton_method(cStr, "align", cStr_align, 0);
464
- /**
465
- * @overload size(value, offset = 0, parent = nil, index = nil, length = nil)
466
- * Returns the size of a string.
467
- * @param value [Object] string to dump.
468
- * @param offset [Integer] ignored.
469
- * @param parent [Structure] ignored.
470
- * @param index [Integer] ignored.
471
- * @param length [Integer] if given the length of the vector. Else
472
- * the size of the string.
473
- * @return [Integer] the size of the string or <tt>sizeof(char) * length</tt>.
474
- */
475
727
  rb_define_singleton_method(cStr, "size", cStr_size, -1);
476
- /**
477
- * @overload shape(value, offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
478
- * Returns the shape of a string field
479
- * @param value [Object] ignored.
480
- * @param offset [Integer] start of the shape.
481
- * @param parent [Structure] ignored.
482
- * @param index [Integer] ignored.
483
- * @param kind [Class] shape class. Will be instantiated through
484
- * new with the +offset+ and <tt>offset + sizeof($3) * length - 1</tt>.
485
- * @param length [Integer] if given the length of the string to
486
- * consider. Else the length is the size of the string.
487
- * @return [kind] a new instance of +kind+
488
- */
489
728
  rb_define_singleton_method(cStr, "shape", cStr_shape, -1);
490
- /**
491
- * @overload load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
492
- * Load a string field from +input+, and return it.
493
- * @param input [IO] the stream to load the field from.
494
- * @param input_big [Boolean] the endianness of +input+
495
- * @param parent [Structure] ignored.
496
- * @param index [Integer] ignored.
497
- * @param length [Integer] if given the length of the string. Else
498
- * the string is considered NULL terminated.
499
- * @return [String] the Ruby representation of the string.
500
- */
501
729
  rb_define_singleton_method(cStr, "load", cStr_load, -1);
502
- /**
503
- * @overload dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
504
- * Dump a string field to +output+.
505
- * @param value [Numeric, Array<Numeric>] the Ruby representation
506
- * of the string.
507
- * @param output [IO] the stream to dump the field to.
508
- * @param output_big [Boolean] the endianness of +output+.
509
- * @param parent [Structure] ignored.
510
- * @param index [Integer] ignored.
511
- * @param length [Integer] if given the length of the string to dump. Else
512
- * the length is the size of the string.
513
- * @return [nil]
514
- */
515
730
  rb_define_singleton_method(cStr, "dump", cStr_dump, -1);
516
- /**
517
- * @overload convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
518
- * Convert a string field by loading it from +input+,
519
- * dumping it to +output+, and returning the loaded field.
520
- * @param input [IO] the stream to load the field from.
521
- * @param output [IO] the stream to dump the field to.
522
- * @param input_big [Boolean] the endianness of +input+
523
- * @param output_big [Boolean] the endianness of +output+.
524
- * @param parent [Structure] ignored.
525
- * @param index [Integer] ignored.
526
- * @param length [Integer] if given the length of the string to reqd. Else
527
- * the string is considered NULL terminated.
528
- * @return [String] the Ruby representation of the string.
529
- */
530
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;
736
+ }
737
+
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);
531
849
  }
532
850
 
533
- void define_cScalar() {
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) {
534
1280
  id_read = rb_intern("read");
535
1281
  id_write = rb_intern("write");
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;
536
1308
  cScalar = rb_define_class_under(cStructure, "Scalar", rb_cObject);
537
- /**
538
- * Returns false as scalars are not required to be aligned.
539
- * @return [false] return false.
540
- */
541
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);
542
1311
  MAKE_CALL_DEFINES(Half);
543
1312
  MAKE_CALL_DEFINES(PGHalf);
544
1313
  MAKE_CALL_DEFINES(Int8);
@@ -552,6 +1321,8 @@ void define_cScalar() {
552
1321
  MAKE_CALL_DEFINES(Flt);
553
1322
  MAKE_CALL_DEFINES(Double);
554
1323
  define_cStr();
1324
+ define_cZStr();
1325
+ define_cSkip();
1326
+ define_cAlignTo();
1327
+ libbin_defining_scalars = 0;
555
1328
  }
556
-
557
-