libbin 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +454 -0
- data/ext/libbin/data_types.c +1055 -284
- data/ext/libbin/data_types.h +32 -1
- data/ext/libbin/extconf.rb +15 -0
- data/ext/libbin/half.c +110 -65
- data/ext/libbin/half.h +2 -0
- data/ext/libbin/libbin_c.c +2066 -420
- data/ext/libbin/libbin_c.h +48 -1
- data/ext/libbin/libbin_io.h +506 -0
- data/ext/libbin/pghalf.c +107 -228
- data/ext/libbin/pghalf.h +2 -0
- data/lib/libbin/bitfield.rb +426 -0
- data/lib/libbin/data_shape.rb +387 -0
- data/lib/libbin/data_types.rb +264 -511
- data/lib/libbin/enum.rb +159 -0
- data/lib/libbin/union.rb +314 -0
- data/lib/libbin/utf16.rb +183 -0
- data/lib/libbin/version.rb +4 -0
- data/lib/libbin.rb +165 -14
- data/libbin.gemspec +23 -4
- metadata +46 -5
data/ext/libbin/libbin_c.h
CHANGED
|
@@ -3,10 +3,57 @@
|
|
|
3
3
|
#include "./half.h"
|
|
4
4
|
#include "./pghalf.h"
|
|
5
5
|
#include "./libbin_endian.h"
|
|
6
|
-
#include "./
|
|
6
|
+
#include "./libbin_io.h"
|
|
7
|
+
#ifdef HAVE_RUBY_RACTOR_H
|
|
8
|
+
#include "ruby/ractor.h"
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
#ifdef HAVE_RB_GC_MARK_MOVABLE
|
|
12
|
+
#define LIBBIN_GC_MARK(v) rb_gc_mark_movable(v)
|
|
13
|
+
#else
|
|
14
|
+
#define LIBBIN_GC_MARK(v) rb_gc_mark(v)
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
18
|
+
#define LIBBIN_ATOMIC_LOAD(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
|
|
19
|
+
#define LIBBIN_ATOMIC_STORE(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELEASE)
|
|
20
|
+
#define LIBBIN_ATOMIC_INC(ptr) __atomic_add_fetch((ptr), 1, __ATOMIC_ACQ_REL)
|
|
21
|
+
#else
|
|
22
|
+
#define LIBBIN_ATOMIC_LOAD(ptr) (*(ptr))
|
|
23
|
+
#define LIBBIN_ATOMIC_STORE(ptr, val) (*(ptr) = (val))
|
|
24
|
+
#define LIBBIN_ATOMIC_INC(ptr) (++(*(ptr)))
|
|
25
|
+
#endif
|
|
7
26
|
|
|
27
|
+
#define LIBBIN_MAX_FRAMES 64
|
|
28
|
+
|
|
29
|
+
typedef struct libbin_frame {
|
|
30
|
+
VALUE self;
|
|
31
|
+
VALUE field;
|
|
32
|
+
VALUE index;
|
|
33
|
+
uint8_t mode; /* 0 = load, 1 = dump, 2 = convert */
|
|
34
|
+
} libbin_frame_t;
|
|
35
|
+
|
|
36
|
+
typedef struct libbin_ctx {
|
|
37
|
+
libbin_io_t in_io;
|
|
38
|
+
libbin_io_t out_io;
|
|
39
|
+
VALUE pending_index;
|
|
40
|
+
uint8_t has_in;
|
|
41
|
+
uint8_t has_out;
|
|
42
|
+
size_t frame_depth;
|
|
43
|
+
libbin_frame_t frames[LIBBIN_MAX_FRAMES];
|
|
44
|
+
} libbin_ctx_t;
|
|
45
|
+
|
|
46
|
+
libbin_ctx_t *libbin_ctx_from_parent(VALUE parent, VALUE input, VALUE output, uint8_t mode);
|
|
47
|
+
void libbin_ctx_sync_to_ruby(libbin_ctx_t *ctx);
|
|
48
|
+
void libbin_ctx_sync_from_ruby(libbin_ctx_t *ctx);
|
|
49
|
+
|
|
50
|
+
#include "./data_types.h"
|
|
8
51
|
|
|
9
52
|
extern VALUE mLibBin;
|
|
10
53
|
extern VALUE cField;
|
|
54
|
+
extern VALUE eLibBinError;
|
|
55
|
+
extern VALUE eLibBinEOFError;
|
|
56
|
+
extern VALUE eLibBinValidationError;
|
|
57
|
+
extern VALUE eLibBinAlignmentError;
|
|
11
58
|
|
|
12
59
|
#endif
|
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
#ifndef LIBBIN_IO_H__
|
|
2
|
+
#define LIBBIN_IO_H__
|
|
3
|
+
|
|
4
|
+
#include <stddef.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
#include "ruby.h"
|
|
8
|
+
|
|
9
|
+
#if defined(HAVE_RUBY_IO_BUFFER_H) && \
|
|
10
|
+
defined(HAVE_RB_IO_BUFFER_GET_BYTES) && \
|
|
11
|
+
defined(HAVE_RB_IO_BUFFER_GET_BYTES_FOR_READING) && \
|
|
12
|
+
defined(HAVE_RB_IO_BUFFER_GET_BYTES_FOR_WRITING)
|
|
13
|
+
#define LIBBIN_HAVE_IO_BUFFER 1
|
|
14
|
+
#include "ruby/io/buffer.h"
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
typedef enum {
|
|
18
|
+
LIBBIN_IO_GENERIC = 0,
|
|
19
|
+
LIBBIN_IO_STRINGIO = 1,
|
|
20
|
+
LIBBIN_IO_STRING = 2,
|
|
21
|
+
LIBBIN_IO_BUFFER = 3
|
|
22
|
+
} libbin_io_kind_t;
|
|
23
|
+
|
|
24
|
+
typedef struct {
|
|
25
|
+
VALUE io; /* original Ruby IO / StringIO / String / IO::Buffer */
|
|
26
|
+
VALUE buf_str; /* backing Ruby String for STRINGIO / STRING, else Qnil */
|
|
27
|
+
ptrdiff_t pos; /* current byte cursor */
|
|
28
|
+
libbin_io_kind_t kind;
|
|
29
|
+
uint8_t writable; /* 0 = read, 1 = write */
|
|
30
|
+
uint8_t dirty; /* 1 = StringIO#pos needs syncing back to Ruby */
|
|
31
|
+
uint8_t non_seekable; /* 1 = pipe / gzip / custom stream without working tell/seek */
|
|
32
|
+
uint8_t has_tell; /* 1 = io.tell succeeded during probe */
|
|
33
|
+
} libbin_io_t;
|
|
34
|
+
|
|
35
|
+
extern ID id_read;
|
|
36
|
+
extern ID id_write;
|
|
37
|
+
extern ID id_tell;
|
|
38
|
+
extern ID id_seek;
|
|
39
|
+
extern ID id_readline;
|
|
40
|
+
extern ID id_string;
|
|
41
|
+
extern ID id_pos;
|
|
42
|
+
extern ID id_pos_eq;
|
|
43
|
+
extern ID id_closed_read_p;
|
|
44
|
+
extern ID id_closed_write_p;
|
|
45
|
+
extern ID id_StringIO;
|
|
46
|
+
extern VALUE rb_str_nul;
|
|
47
|
+
extern VALUE cStringIO_cached;
|
|
48
|
+
extern VALUE eLibBinEOFError;
|
|
49
|
+
|
|
50
|
+
static VALUE libbin_protect_tell_call(VALUE io) {
|
|
51
|
+
return rb_funcall(io, id_tell, 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static inline int libbin_try_tell_io(VALUE io, ptrdiff_t *out_pos) {
|
|
55
|
+
if (NIL_P(io) || !rb_respond_to(io, id_tell))
|
|
56
|
+
return 0;
|
|
57
|
+
int state = 0;
|
|
58
|
+
VALUE res = rb_protect(libbin_protect_tell_call, io, &state);
|
|
59
|
+
if (state) {
|
|
60
|
+
VALUE err = rb_errinfo();
|
|
61
|
+
if (rb_obj_is_kind_of(err, rb_eSystemCallError) ||
|
|
62
|
+
rb_obj_is_kind_of(err, rb_eNotImpError) ||
|
|
63
|
+
rb_obj_is_kind_of(err, rb_eNoMethodError)) {
|
|
64
|
+
rb_set_errinfo(Qnil);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
rb_jump_tag(state);
|
|
68
|
+
}
|
|
69
|
+
*out_pos = NUM2LL(res);
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static inline VALUE libbin_get_stringio_class(void) {
|
|
74
|
+
return cStringIO_cached;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static inline void libbin_io_init_generic_raw(libbin_io_t *bio, VALUE io, int writable) {
|
|
78
|
+
bio->io = io;
|
|
79
|
+
bio->buf_str = Qnil;
|
|
80
|
+
bio->pos = 0;
|
|
81
|
+
bio->kind = LIBBIN_IO_GENERIC;
|
|
82
|
+
bio->writable = (uint8_t)writable;
|
|
83
|
+
bio->dirty = 0;
|
|
84
|
+
bio->non_seekable = 0;
|
|
85
|
+
bio->has_tell = 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static inline void libbin_io_init_generic(libbin_io_t *bio, VALUE io, int writable) {
|
|
89
|
+
libbin_io_init_generic_raw(bio, io, writable);
|
|
90
|
+
if (!NIL_P(io)) {
|
|
91
|
+
ptrdiff_t pos = 0;
|
|
92
|
+
if (libbin_try_tell_io(io, &pos)) {
|
|
93
|
+
bio->pos = pos;
|
|
94
|
+
bio->has_tell = 1;
|
|
95
|
+
if (!rb_respond_to(io, id_seek))
|
|
96
|
+
bio->non_seekable = 1;
|
|
97
|
+
} else {
|
|
98
|
+
bio->non_seekable = 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static inline void libbin_io_init(libbin_io_t *bio, VALUE io, int writable) {
|
|
104
|
+
bio->io = io;
|
|
105
|
+
bio->buf_str = Qnil;
|
|
106
|
+
bio->pos = 0;
|
|
107
|
+
bio->kind = LIBBIN_IO_GENERIC;
|
|
108
|
+
bio->writable = (uint8_t)writable;
|
|
109
|
+
bio->dirty = 0;
|
|
110
|
+
bio->non_seekable = 0;
|
|
111
|
+
bio->has_tell = 0;
|
|
112
|
+
|
|
113
|
+
if (NIL_P(io))
|
|
114
|
+
return;
|
|
115
|
+
|
|
116
|
+
if (RB_TYPE_P(io, T_STRING)) {
|
|
117
|
+
if (writable)
|
|
118
|
+
rb_check_frozen(io);
|
|
119
|
+
bio->kind = LIBBIN_IO_STRING;
|
|
120
|
+
bio->buf_str = io;
|
|
121
|
+
bio->pos = 0;
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
VALUE cStringIO = libbin_get_stringio_class();
|
|
126
|
+
if (!NIL_P(cStringIO) && rb_obj_class(io) == cStringIO) {
|
|
127
|
+
VALUE closed = rb_funcall(io, writable ? id_closed_write_p : id_closed_read_p, 0);
|
|
128
|
+
if (!RTEST(closed)) {
|
|
129
|
+
VALUE str = rb_funcall(io, id_string, 0);
|
|
130
|
+
if (RB_TYPE_P(str, T_STRING) && (!writable || !OBJ_FROZEN(str))) {
|
|
131
|
+
ptrdiff_t pos = NUM2LL(rb_funcall(io, id_pos, 0));
|
|
132
|
+
if (pos >= 0) {
|
|
133
|
+
bio->kind = LIBBIN_IO_STRINGIO;
|
|
134
|
+
bio->buf_str = str;
|
|
135
|
+
bio->pos = pos;
|
|
136
|
+
bio->dirty = 0;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
145
|
+
if (rb_obj_class(io) == rb_cIOBuffer) {
|
|
146
|
+
bio->kind = LIBBIN_IO_BUFFER;
|
|
147
|
+
bio->pos = 0;
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
#endif
|
|
151
|
+
|
|
152
|
+
libbin_io_init_generic(bio, io, writable);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static inline void libbin_io_sync_to_ruby(libbin_io_t *bio) {
|
|
156
|
+
if (bio->kind == LIBBIN_IO_STRINGIO && bio->dirty) {
|
|
157
|
+
rb_funcall(bio->io, id_pos_eq, 1, LL2NUM(bio->pos));
|
|
158
|
+
bio->dirty = 0;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static inline void libbin_io_sync_from_ruby(libbin_io_t *bio) {
|
|
163
|
+
if (bio->kind == LIBBIN_IO_STRINGIO) {
|
|
164
|
+
VALUE str = rb_funcall(bio->io, id_string, 0);
|
|
165
|
+
if (!RB_TYPE_P(str, T_STRING) || (bio->writable && OBJ_FROZEN(str))) {
|
|
166
|
+
bio->kind = LIBBIN_IO_GENERIC;
|
|
167
|
+
bio->buf_str = Qnil;
|
|
168
|
+
bio->dirty = 0;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
ptrdiff_t pos = NUM2LL(rb_funcall(bio->io, id_pos, 0));
|
|
172
|
+
if (pos < 0) {
|
|
173
|
+
bio->kind = LIBBIN_IO_GENERIC;
|
|
174
|
+
bio->buf_str = Qnil;
|
|
175
|
+
bio->dirty = 0;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
bio->buf_str = str;
|
|
179
|
+
bio->pos = pos;
|
|
180
|
+
bio->dirty = 0;
|
|
181
|
+
} else if (bio->kind == LIBBIN_IO_GENERIC && bio->non_seekable && bio->has_tell) {
|
|
182
|
+
ptrdiff_t pos = 0;
|
|
183
|
+
if (libbin_try_tell_io(bio->io, &pos))
|
|
184
|
+
bio->pos = pos;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static inline ptrdiff_t libbin_io_tell(libbin_io_t *bio) {
|
|
189
|
+
if (bio->kind != LIBBIN_IO_GENERIC || bio->non_seekable)
|
|
190
|
+
return bio->pos;
|
|
191
|
+
return NUM2LL(rb_funcall(bio->io, id_tell, 0));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
static inline void libbin_io_check_bounds(ptrdiff_t pos, size_t len, size_t cnt) {
|
|
195
|
+
if (pos < 0 || (size_t)pos > len || cnt > len - (size_t)pos) {
|
|
196
|
+
long got = (pos >= 0 && (size_t)pos < len) ? (long)(len - (size_t)pos) : 0L;
|
|
197
|
+
rb_raise(eLibBinEOFError, "could not read enough data: got %ld needed %zu", got, cnt);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
202
|
+
static inline char *libbin_io_buffer_prepare_write(VALUE io, ptrdiff_t pos, size_t cnt, size_t *out_end) {
|
|
203
|
+
void *base = NULL;
|
|
204
|
+
size_t size = 0;
|
|
205
|
+
rb_io_buffer_get_bytes(io, &base, &size);
|
|
206
|
+
size_t end_pos = (size_t)pos + cnt;
|
|
207
|
+
if (!base || end_pos > size)
|
|
208
|
+
rb_io_buffer_resize(io, end_pos);
|
|
209
|
+
rb_io_buffer_get_bytes_for_writing(io, &base, &size);
|
|
210
|
+
*out_end = end_pos;
|
|
211
|
+
return (char *)base + pos;
|
|
212
|
+
}
|
|
213
|
+
#endif
|
|
214
|
+
|
|
215
|
+
static inline const char *libbin_io_read_bytes(libbin_io_t *bio, size_t cnt, VALUE *guard) {
|
|
216
|
+
if (bio->kind == LIBBIN_IO_STRINGIO || bio->kind == LIBBIN_IO_STRING) {
|
|
217
|
+
if (cnt == 0) {
|
|
218
|
+
*guard = Qnil;
|
|
219
|
+
return "";
|
|
220
|
+
}
|
|
221
|
+
VALUE str = bio->buf_str;
|
|
222
|
+
long len = RSTRING_LEN(str);
|
|
223
|
+
libbin_io_check_bounds(bio->pos, (size_t)len, cnt);
|
|
224
|
+
const char *ptr = RSTRING_PTR(str) + bio->pos;
|
|
225
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
226
|
+
bio->dirty = 1;
|
|
227
|
+
*guard = str;
|
|
228
|
+
return ptr;
|
|
229
|
+
}
|
|
230
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
231
|
+
if (bio->kind == LIBBIN_IO_BUFFER) {
|
|
232
|
+
if (cnt == 0) {
|
|
233
|
+
*guard = Qnil;
|
|
234
|
+
return "";
|
|
235
|
+
}
|
|
236
|
+
void *raw_base = NULL;
|
|
237
|
+
size_t size = 0;
|
|
238
|
+
rb_io_buffer_get_bytes(bio->io, &raw_base, &size);
|
|
239
|
+
if (!raw_base)
|
|
240
|
+
rb_raise(eLibBinEOFError, "could not read enough data: got 0 needed %zu", cnt);
|
|
241
|
+
libbin_io_check_bounds(bio->pos, size, cnt);
|
|
242
|
+
const void *base;
|
|
243
|
+
rb_io_buffer_get_bytes_for_reading(bio->io, &base, &size);
|
|
244
|
+
const char *ptr = (const char *)base + bio->pos;
|
|
245
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
246
|
+
*guard = bio->io;
|
|
247
|
+
return ptr;
|
|
248
|
+
}
|
|
249
|
+
#endif
|
|
250
|
+
VALUE str = rb_funcall(bio->io, id_read, 1, ULL2NUM(cnt));
|
|
251
|
+
if (NIL_P(str) || (size_t)RSTRING_LEN(str) < cnt)
|
|
252
|
+
rb_raise(eLibBinEOFError,
|
|
253
|
+
"could not read enough data: got %ld needed %zu",
|
|
254
|
+
NIL_P(str) ? 0L : RSTRING_LEN(str), cnt);
|
|
255
|
+
if (bio->non_seekable)
|
|
256
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
257
|
+
*guard = str;
|
|
258
|
+
return RSTRING_PTR(str);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static inline void libbin_io_skip_read(libbin_io_t *bio, size_t cnt) {
|
|
262
|
+
if (cnt == 0)
|
|
263
|
+
return;
|
|
264
|
+
if (bio->kind == LIBBIN_IO_STRINGIO || bio->kind == LIBBIN_IO_STRING) {
|
|
265
|
+
VALUE str = bio->buf_str;
|
|
266
|
+
long len = RSTRING_LEN(str);
|
|
267
|
+
libbin_io_check_bounds(bio->pos, (size_t)len, cnt);
|
|
268
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
269
|
+
bio->dirty = 1;
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
273
|
+
if (bio->kind == LIBBIN_IO_BUFFER) {
|
|
274
|
+
void *raw_base = NULL;
|
|
275
|
+
size_t size = 0;
|
|
276
|
+
rb_io_buffer_get_bytes(bio->io, &raw_base, &size);
|
|
277
|
+
if (!raw_base)
|
|
278
|
+
rb_raise(eLibBinEOFError, "could not read enough data: got 0 needed %zu", cnt);
|
|
279
|
+
libbin_io_check_bounds(bio->pos, size, cnt);
|
|
280
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
#endif
|
|
284
|
+
size_t remaining = cnt;
|
|
285
|
+
while (remaining > 0) {
|
|
286
|
+
size_t chunk = remaining > 8192 ? 8192 : remaining;
|
|
287
|
+
VALUE str = rb_funcall(bio->io, id_read, 1, ULL2NUM(chunk));
|
|
288
|
+
long got = NIL_P(str) ? 0L : RSTRING_LEN(str);
|
|
289
|
+
if ((size_t)got < chunk) {
|
|
290
|
+
size_t total_got = (cnt - remaining) + (size_t)got;
|
|
291
|
+
rb_raise(eLibBinEOFError,
|
|
292
|
+
"could not read enough data: got %zu needed %zu",
|
|
293
|
+
total_got, cnt);
|
|
294
|
+
}
|
|
295
|
+
if (bio->non_seekable)
|
|
296
|
+
bio->pos += (ptrdiff_t)chunk;
|
|
297
|
+
remaining -= chunk;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static inline void libbin_str_ensure_space(VALUE str, size_t pos, size_t cnt) {
|
|
302
|
+
size_t end_pos = pos + cnt;
|
|
303
|
+
long cur_len = RSTRING_LEN(str);
|
|
304
|
+
if (end_pos > (size_t)cur_len) {
|
|
305
|
+
#ifdef HAVE_RB_STR_CAPACITY
|
|
306
|
+
size_t capa = rb_str_capacity(str);
|
|
307
|
+
if (end_pos > capa) {
|
|
308
|
+
size_t new_capa = capa < 64 ? 64 : capa;
|
|
309
|
+
while (new_capa < end_pos)
|
|
310
|
+
new_capa *= 2;
|
|
311
|
+
rb_str_modify_expand(str, (long)(new_capa - (size_t)cur_len));
|
|
312
|
+
} else {
|
|
313
|
+
rb_str_modify(str);
|
|
314
|
+
}
|
|
315
|
+
#else
|
|
316
|
+
rb_str_modify_expand(str, (long)(end_pos - (size_t)cur_len));
|
|
317
|
+
#endif
|
|
318
|
+
rb_str_set_len(str, (long)end_pos);
|
|
319
|
+
if (pos > (size_t)cur_len)
|
|
320
|
+
memset(RSTRING_PTR(str) + cur_len, 0, pos - (size_t)cur_len);
|
|
321
|
+
} else {
|
|
322
|
+
rb_str_modify(str);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
static inline void libbin_io_reserve_output(libbin_io_t *bio, size_t cnt) {
|
|
327
|
+
if (cnt == 0)
|
|
328
|
+
return;
|
|
329
|
+
if (bio->kind == LIBBIN_IO_STRINGIO || bio->kind == LIBBIN_IO_STRING) {
|
|
330
|
+
VALUE str = bio->buf_str;
|
|
331
|
+
long cur_len = RSTRING_LEN(str);
|
|
332
|
+
size_t need = (size_t)bio->pos + cnt;
|
|
333
|
+
#ifdef HAVE_RB_STR_CAPACITY
|
|
334
|
+
if (need > rb_str_capacity(str)) {
|
|
335
|
+
rb_str_modify_expand(str, (long)(need - (size_t)cur_len));
|
|
336
|
+
rb_str_set_len(str, cur_len);
|
|
337
|
+
}
|
|
338
|
+
#else
|
|
339
|
+
if (need > (size_t)cur_len) {
|
|
340
|
+
rb_str_modify_expand(str, (long)(need - (size_t)cur_len));
|
|
341
|
+
rb_str_set_len(str, cur_len);
|
|
342
|
+
}
|
|
343
|
+
#endif
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
static inline char *libbin_io_write_reserve_direct(libbin_io_t *bio, size_t cnt) {
|
|
348
|
+
if (bio->kind == LIBBIN_IO_STRINGIO || bio->kind == LIBBIN_IO_STRING) {
|
|
349
|
+
VALUE str = bio->buf_str;
|
|
350
|
+
libbin_str_ensure_space(str, (size_t)bio->pos, cnt);
|
|
351
|
+
char *dst = RSTRING_PTR(str) + bio->pos;
|
|
352
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
353
|
+
bio->dirty = 1;
|
|
354
|
+
return dst;
|
|
355
|
+
}
|
|
356
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
357
|
+
if (bio->kind == LIBBIN_IO_BUFFER) {
|
|
358
|
+
size_t end_pos = 0;
|
|
359
|
+
char *dst = libbin_io_buffer_prepare_write(bio->io, bio->pos, cnt, &end_pos);
|
|
360
|
+
bio->pos = (ptrdiff_t)end_pos;
|
|
361
|
+
return dst;
|
|
362
|
+
}
|
|
363
|
+
#endif
|
|
364
|
+
return NULL;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
static inline void libbin_io_write_bytes(libbin_io_t *bio, const void *src, size_t cnt) {
|
|
368
|
+
if (cnt == 0)
|
|
369
|
+
return;
|
|
370
|
+
char *dst = libbin_io_write_reserve_direct(bio, cnt);
|
|
371
|
+
if (dst) {
|
|
372
|
+
memcpy(dst, src, cnt);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
VALUE str = rb_str_new((const char *)src, (long)cnt);
|
|
376
|
+
rb_funcall(bio->io, id_write, 1, str);
|
|
377
|
+
if (bio->non_seekable)
|
|
378
|
+
bio->pos += (ptrdiff_t)cnt;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
static inline void libbin_io_write_ruby_str(libbin_io_t *bio, VALUE str) {
|
|
382
|
+
if (bio->kind == LIBBIN_IO_GENERIC) {
|
|
383
|
+
long len = RSTRING_LEN(str);
|
|
384
|
+
rb_funcall(bio->io, id_write, 1, str);
|
|
385
|
+
if (bio->non_seekable)
|
|
386
|
+
bio->pos += (ptrdiff_t)len;
|
|
387
|
+
} else {
|
|
388
|
+
libbin_io_write_bytes(bio, RSTRING_PTR(str), (size_t)RSTRING_LEN(str));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
static inline void libbin_io_write_fill(libbin_io_t *bio, uint8_t fill_byte, size_t cnt) {
|
|
393
|
+
if (cnt == 0)
|
|
394
|
+
return;
|
|
395
|
+
char *dst = libbin_io_write_reserve_direct(bio, cnt);
|
|
396
|
+
if (dst) {
|
|
397
|
+
memset(dst, fill_byte, cnt);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
size_t remaining = cnt;
|
|
401
|
+
size_t buf_sz = remaining > 8192 ? 8192 : remaining;
|
|
402
|
+
VALUE str = rb_str_buf_new((long)buf_sz);
|
|
403
|
+
rb_str_set_len(str, (long)buf_sz);
|
|
404
|
+
memset(RSTRING_PTR(str), fill_byte, buf_sz);
|
|
405
|
+
while (remaining > 0) {
|
|
406
|
+
size_t chunk = remaining > buf_sz ? buf_sz : remaining;
|
|
407
|
+
if (chunk < buf_sz)
|
|
408
|
+
rb_str_set_len(str, (long)chunk);
|
|
409
|
+
rb_funcall(bio->io, id_write, 1, str);
|
|
410
|
+
if (bio->non_seekable)
|
|
411
|
+
bio->pos += (ptrdiff_t)chunk;
|
|
412
|
+
remaining -= chunk;
|
|
413
|
+
}
|
|
414
|
+
RB_GC_GUARD(str);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
static inline void libbin_io_write_zeros(libbin_io_t *bio, size_t cnt) {
|
|
418
|
+
libbin_io_write_fill(bio, 0, cnt);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
static inline void libbin_io_seek(libbin_io_t *bio, ptrdiff_t pos) {
|
|
422
|
+
if (bio->kind != LIBBIN_IO_GENERIC && pos >= 0) {
|
|
423
|
+
bio->pos = pos;
|
|
424
|
+
bio->dirty = 1;
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (bio->non_seekable) {
|
|
428
|
+
if (pos == bio->pos)
|
|
429
|
+
return;
|
|
430
|
+
if (pos < bio->pos)
|
|
431
|
+
rb_raise(rb_eIOError, "cannot seek backwards on non-seekable stream (pos=%td, target=%td)", bio->pos, pos);
|
|
432
|
+
size_t delta = (size_t)(pos - bio->pos);
|
|
433
|
+
if (bio->writable)
|
|
434
|
+
libbin_io_write_zeros(bio, delta);
|
|
435
|
+
else
|
|
436
|
+
libbin_io_skip_read(bio, delta);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
libbin_io_sync_to_ruby(bio);
|
|
440
|
+
rb_funcall(bio->io, id_seek, 1, LL2NUM(pos));
|
|
441
|
+
libbin_io_sync_from_ruby(bio);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
static inline VALUE libbin_io_readline_nul(libbin_io_t *bio) {
|
|
445
|
+
if (bio->kind == LIBBIN_IO_STRINGIO || bio->kind == LIBBIN_IO_STRING) {
|
|
446
|
+
VALUE str = bio->buf_str;
|
|
447
|
+
long len = RSTRING_LEN(str);
|
|
448
|
+
if (bio->pos >= 0 && bio->pos < len) {
|
|
449
|
+
const char *start = RSTRING_PTR(str) + bio->pos;
|
|
450
|
+
size_t rem = (size_t)(len - bio->pos);
|
|
451
|
+
const char *nul = (const char *)memchr(start, '\0', rem);
|
|
452
|
+
size_t take = nul ? (size_t)(nul - start) + 1 : rem;
|
|
453
|
+
VALUE res = rb_str_new(start, (long)take);
|
|
454
|
+
bio->pos += (ptrdiff_t)take;
|
|
455
|
+
bio->dirty = 1;
|
|
456
|
+
RB_GC_GUARD(str);
|
|
457
|
+
return res;
|
|
458
|
+
}
|
|
459
|
+
if (bio->kind == LIBBIN_IO_STRING)
|
|
460
|
+
rb_raise(rb_eEOFError, "end of file reached");
|
|
461
|
+
/* StringIO at/past EOF: delegate to Ruby StringIO#readline("\0") for exact EOFError */
|
|
462
|
+
libbin_io_sync_to_ruby(bio);
|
|
463
|
+
return rb_funcall(bio->io, id_readline, 1, rb_str_nul);
|
|
464
|
+
}
|
|
465
|
+
#ifdef LIBBIN_HAVE_IO_BUFFER
|
|
466
|
+
if (bio->kind == LIBBIN_IO_BUFFER) {
|
|
467
|
+
void *raw_base = NULL;
|
|
468
|
+
size_t size = 0;
|
|
469
|
+
rb_io_buffer_get_bytes(bio->io, &raw_base, &size);
|
|
470
|
+
if (!raw_base || bio->pos < 0 || (size_t)bio->pos >= size)
|
|
471
|
+
rb_raise(rb_eEOFError, "end of file reached");
|
|
472
|
+
const void *base;
|
|
473
|
+
rb_io_buffer_get_bytes_for_reading(bio->io, &base, &size);
|
|
474
|
+
const char *start = (const char *)base + bio->pos;
|
|
475
|
+
size_t rem = size - (size_t)bio->pos;
|
|
476
|
+
const char *nul = (const char *)memchr(start, '\0', rem);
|
|
477
|
+
size_t take = nul ? (size_t)(nul - start) + 1 : rem;
|
|
478
|
+
VALUE res = rb_str_new(start, (long)take);
|
|
479
|
+
bio->pos += (ptrdiff_t)take;
|
|
480
|
+
return res;
|
|
481
|
+
}
|
|
482
|
+
#endif
|
|
483
|
+
if (rb_respond_to(bio->io, id_readline)) {
|
|
484
|
+
VALUE res = rb_funcall(bio->io, id_readline, 1, rb_str_nul);
|
|
485
|
+
if (bio->non_seekable && RB_TYPE_P(res, T_STRING))
|
|
486
|
+
bio->pos += (ptrdiff_t)RSTRING_LEN(res);
|
|
487
|
+
return res;
|
|
488
|
+
}
|
|
489
|
+
VALUE buf = rb_str_buf_new(32);
|
|
490
|
+
while (1) {
|
|
491
|
+
VALUE ch = rb_funcall(bio->io, id_read, 1, INT2FIX(1));
|
|
492
|
+
if (NIL_P(ch) || RSTRING_LEN(ch) == 0) {
|
|
493
|
+
if (RSTRING_LEN(buf) == 0)
|
|
494
|
+
rb_raise(rb_eEOFError, "end of file reached");
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
rb_str_buf_cat(buf, RSTRING_PTR(ch), 1);
|
|
498
|
+
if (bio->non_seekable)
|
|
499
|
+
bio->pos += 1;
|
|
500
|
+
if (RSTRING_PTR(ch)[0] == '\0')
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
return buf;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
#endif
|