binyo 0.0.1

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.
@@ -0,0 +1,143 @@
1
+ /*
2
+ * binyo - Fast binary IO for Ruby
3
+ *
4
+ * Copyright (c) 2012-2013
5
+ * Martin Bosslet <martin.bosslet@gmail.com>
6
+ * All rights reserved.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include "binyo-io-buffer.h"
29
+ #include "binyo-error.h"
30
+ #include "binyo-error-internal.h"
31
+
32
+ binyo_byte_buffer *
33
+ binyo_buffer_new(void)
34
+ {
35
+ binyo_byte_buffer *ret;
36
+ ret = ALLOC(binyo_byte_buffer);
37
+ memset(ret, 0, sizeof(binyo_byte_buffer));
38
+ return ret;
39
+ }
40
+
41
+ binyo_byte_buffer *
42
+ binyo_buffer_new_size(size_t size)
43
+ {
44
+ binyo_byte_buffer *ret;
45
+ ret = binyo_buffer_new();
46
+ ret->init_size = size;
47
+ return ret;
48
+ }
49
+
50
+ binyo_byte_buffer *
51
+ binyo_buffer_new_prealloc(uint8_t *b, size_t len)
52
+ {
53
+ binyo_byte_buffer *ret;
54
+ ret = binyo_buffer_new();
55
+ ret->data = b;
56
+ ret->limit = len;
57
+ ret->prealloc = 1;
58
+ return ret;
59
+ }
60
+
61
+ static const size_t BINYO_BUF_MAX = SIZE_MAX / BINYO_BYTE_BUFFER_GROWTH_FACTOR;
62
+
63
+ static int
64
+ int_buffer_grow(binyo_byte_buffer *buffer, size_t cur_len)
65
+ {
66
+ size_t new_size;
67
+
68
+ if (buffer->prealloc) {
69
+ binyo_error_add("Cannot grow preallocated buffer");
70
+ return BINYO_ERR;
71
+ }
72
+
73
+ if (buffer->data == NULL) {
74
+ size_t alloc_size = buffer->init_size > cur_len ? buffer->init_size : cur_len;
75
+ buffer->data = ALLOC_N(uint8_t, alloc_size);
76
+ buffer->limit = alloc_size;
77
+ return BINYO_OK;
78
+ }
79
+
80
+ /* avoid infinite loop for limit == 1 */
81
+ new_size = buffer->limit == 1 ? 2 : buffer->limit;
82
+
83
+ while (new_size - buffer->size < cur_len) {
84
+ if (new_size >= BINYO_BUF_MAX) {
85
+ binyo_error_add("Cannot grow buffer");
86
+ return BINYO_ERR;
87
+ }
88
+ new_size *= BINYO_BYTE_BUFFER_GROWTH_FACTOR;
89
+ }
90
+
91
+ REALLOC_N(buffer->data, uint8_t, new_size);
92
+ buffer->limit = new_size;
93
+ return BINYO_OK;
94
+ }
95
+
96
+ ssize_t
97
+ binyo_buffer_write(binyo_byte_buffer *buffer, uint8_t *b, size_t len)
98
+ {
99
+ if (!b) return BINYO_ERR;
100
+ if (len == 0) return 0;
101
+ if (len > SSIZE_MAX) return BINYO_ERR;
102
+
103
+ if (buffer->limit - buffer->size < len) {
104
+ if (int_buffer_grow(buffer, len) == BINYO_ERR)
105
+ return BINYO_ERR;
106
+ }
107
+
108
+ memcpy(buffer->data + buffer->size, b, len);
109
+ buffer->size += len;
110
+ return (ssize_t) len;
111
+ }
112
+
113
+ void
114
+ binyo_buffer_free_secure(binyo_byte_buffer *buffer)
115
+ {
116
+ if (buffer && buffer->data) {
117
+ memset(buffer->data, 0, buffer->limit);
118
+ }
119
+ binyo_buffer_free(buffer);
120
+ }
121
+
122
+ void
123
+ binyo_buffer_free(binyo_byte_buffer *buffer)
124
+ {
125
+ if (!buffer) return;
126
+ if (buffer->data && (!buffer->prealloc))
127
+ xfree(buffer->data);
128
+ xfree(buffer);
129
+ }
130
+
131
+ size_t
132
+ binyo_buffer_get_bytes_free(binyo_byte_buffer *buffer, uint8_t **out)
133
+ {
134
+ size_t ret;
135
+
136
+ if (!buffer) return 0;
137
+
138
+ *out = buffer->data;
139
+ ret = buffer->size;
140
+ xfree(buffer);
141
+ return ret;
142
+ }
143
+
@@ -0,0 +1,196 @@
1
+ /*
2
+ * binyo - Fast binary IO for Ruby
3
+ *
4
+ * Copyright (c) 2012-2013
5
+ * Martin Bosslet <martin.bosslet@gmail.com>
6
+ * All rights reserved.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include "binyo.h"
29
+
30
+ struct binyo_byte_ary_st {
31
+ uint8_t *p;
32
+ size_t len;
33
+ };
34
+
35
+ typedef struct binyo_instream_bytes_st {
36
+ binyo_instream_interface *methods;
37
+ struct binyo_byte_ary_st *src;
38
+ size_t num_read;
39
+ } binyo_instream_bytes;
40
+
41
+ #define int_safe_cast(out, in) binyo_safe_cast_instream((out), (in), BINYO_INSTREAM_TYPE_BYTES, binyo_instream_bytes)
42
+
43
+ static binyo_instream_bytes* int_bytes_alloc(void);
44
+ static ssize_t int_bytes_read(binyo_instream *in, uint8_t *buf, size_t len);
45
+ static ssize_t int_bytes_gets(binyo_instream *in, char *line, size_t len);
46
+ static int int_bytes_seek(binyo_instream *in, off_t offset, int whence);
47
+ static void int_bytes_free(binyo_instream *in);
48
+
49
+ static binyo_instream_interface binyo_interface_bytes = {
50
+ BINYO_INSTREAM_TYPE_BYTES,
51
+ int_bytes_read,
52
+ NULL,
53
+ int_bytes_gets,
54
+ int_bytes_seek,
55
+ NULL,
56
+ int_bytes_free
57
+ };
58
+
59
+ binyo_instream *
60
+ binyo_instream_new_bytes(uint8_t *bytes, size_t len)
61
+ {
62
+ binyo_instream_bytes *in;
63
+ struct binyo_byte_ary_st *byte_ary;
64
+
65
+ in = int_bytes_alloc();
66
+ byte_ary = ALLOC(struct binyo_byte_ary_st);
67
+ byte_ary->p = bytes;
68
+ byte_ary->len = len;
69
+ in->src = byte_ary;
70
+ return (binyo_instream *) in;
71
+ }
72
+
73
+ static binyo_instream_bytes*
74
+ int_bytes_alloc(void)
75
+ {
76
+ binyo_instream_bytes *ret;
77
+ ret = ALLOC(binyo_instream_bytes);
78
+ memset(ret, 0, sizeof(binyo_instream_bytes));
79
+ ret->methods = &binyo_interface_bytes;
80
+ return ret;
81
+ }
82
+
83
+ static ssize_t
84
+ int_bytes_read(binyo_instream *instream, uint8_t *buf, size_t len)
85
+ {
86
+ struct binyo_byte_ary_st *src;
87
+ size_t to_read;
88
+ binyo_instream_bytes *in;
89
+
90
+ int_safe_cast(in, instream);
91
+
92
+ if (!buf) return BINYO_ERR;
93
+
94
+ src = in->src;
95
+
96
+ if (in->num_read == src->len)
97
+ return BINYO_IO_EOF;
98
+
99
+ /* Check for SSIZE_MAX already done in io.c */
100
+ to_read = src->len - in->num_read < len ? src->len - in->num_read : len;
101
+ memcpy(buf, src->p, to_read);
102
+ src->p += to_read;
103
+ in->num_read += to_read;
104
+ return (ssize_t) to_read;
105
+ }
106
+
107
+ static ssize_t
108
+ int_bytes_gets(binyo_instream *instream, char *line, size_t len)
109
+ {
110
+ struct binyo_byte_ary_st *src;
111
+ binyo_instream_bytes *in;
112
+ ssize_t ret = 0;
113
+ size_t to_read;
114
+ char *d;
115
+ char *end;
116
+
117
+ int_safe_cast(in, instream);
118
+ src = in->src;
119
+
120
+ if (in->num_read == src->len)
121
+ return BINYO_IO_EOF;
122
+
123
+ d = line;
124
+ to_read = src->len - in->num_read < len ? src->len - in->num_read : len;
125
+ end = d + to_read;
126
+
127
+ /* ret cannot exceed SSIZE_MAX, check for len already done in io.c */
128
+ while (d < end) {
129
+ *d = *(src->p);
130
+ src->p++;
131
+ if (*d == '\n') {
132
+ in->num_read++;
133
+ break;
134
+ }
135
+ d++;
136
+ ret++;
137
+ }
138
+ in->num_read += ret;
139
+
140
+ if (*d == '\n' && *(d - 1) == '\r')
141
+ ret--;
142
+
143
+ return ret;
144
+ }
145
+
146
+ static int
147
+ int_bytes_set_pos(binyo_instream_bytes *in, off_t offset, size_t num_read)
148
+ {
149
+ struct binyo_byte_ary_st *src = in->src;
150
+
151
+ if (src->len - offset <= num_read) {
152
+ binyo_error_add("Unreachable seek position");
153
+ return BINYO_ERR;
154
+ }
155
+ src->p += offset;
156
+ in->num_read += offset;
157
+ return BINYO_OK;
158
+ }
159
+
160
+ /* TODO check overflow */
161
+ static int
162
+ int_bytes_seek(binyo_instream *instream, off_t offset, int whence)
163
+ {
164
+ struct binyo_byte_ary_st *src;
165
+ size_t num_read;
166
+ binyo_instream_bytes *in;
167
+
168
+ int_safe_cast(in, instream);
169
+
170
+ src = in->src;
171
+ num_read = in->num_read;
172
+
173
+ switch (whence) {
174
+ case SEEK_CUR:
175
+ return int_bytes_set_pos(in, offset, num_read);
176
+ case SEEK_SET:
177
+ return int_bytes_set_pos(in, offset - num_read, num_read);
178
+ case SEEK_END:
179
+ return int_bytes_set_pos(in, offset + src->len - num_read, num_read);
180
+ default:
181
+ binyo_error_add("Unknown whence: %d", whence);
182
+ return BINYO_ERR;
183
+ }
184
+ }
185
+
186
+ static void
187
+ int_bytes_free(binyo_instream *instream)
188
+ {
189
+ binyo_instream_bytes *in;
190
+
191
+ if (!instream) return;
192
+ int_safe_cast(in, instream);
193
+
194
+ xfree(in->src);
195
+ }
196
+
@@ -0,0 +1,144 @@
1
+ /*
2
+ * binyo - Fast binary IO for Ruby
3
+ *
4
+ * Copyright (c) 2012-2013
5
+ * Martin Bosslet <martin.bosslet@gmail.com>
6
+ * All rights reserved.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+
28
+ #include "binyo.h"
29
+
30
+ typedef struct binyo_instream_cache_st {
31
+ binyo_instream_interface *methods;
32
+ binyo_instream *inner;
33
+ binyo_outstream *bytes;
34
+ } binyo_instream_cache;
35
+
36
+ #define int_safe_cast(out, in) binyo_safe_cast_instream((out), (in), BINYO_INSTREAM_TYPE_CACHE, binyo_instream_cache)
37
+
38
+ static binyo_instream_cache* int_cache_alloc(void);
39
+ static ssize_t int_cache_read(binyo_instream *in, uint8_t *buf, size_t len);
40
+ static int int_cache_seek(binyo_instream *in, off_t offset, int whence);
41
+ static void int_cache_mark(binyo_instream *in);
42
+ static void int_cache_free(binyo_instream *in);
43
+
44
+ static binyo_instream_interface binyo_interface_cache = {
45
+ BINYO_INSTREAM_TYPE_CACHE,
46
+ int_cache_read,
47
+ NULL,
48
+ NULL,
49
+ int_cache_seek,
50
+ int_cache_mark,
51
+ int_cache_free
52
+ };
53
+
54
+ binyo_instream *
55
+ binyo_instream_new_cache(binyo_instream *original)
56
+ {
57
+ binyo_instream_cache *in;
58
+
59
+ in = int_cache_alloc();
60
+ in->inner = original;
61
+ in->bytes = binyo_outstream_new_bytes_size(1024);
62
+ return (binyo_instream *) in;
63
+ }
64
+
65
+ size_t
66
+ binyo_instream_cache_get_bytes(binyo_instream *instream, uint8_t **out)
67
+ {
68
+ binyo_instream_cache *in;
69
+ size_t ret;
70
+
71
+ int_safe_cast(in, instream);
72
+ ret = binyo_outstream_bytes_get_bytes_free(in->bytes, out);
73
+ in->bytes = binyo_outstream_new_bytes_size(1024);
74
+ return ret;
75
+ }
76
+
77
+ static binyo_instream_cache*
78
+ int_cache_alloc(void)
79
+ {
80
+ binyo_instream_cache *ret;
81
+ ret = ALLOC(binyo_instream_cache);
82
+ memset(ret, 0, sizeof(binyo_instream_cache));
83
+ ret->methods = &binyo_interface_cache;
84
+ return ret;
85
+ }
86
+
87
+ static ssize_t
88
+ int_cache_read(binyo_instream *instream, uint8_t *buf, size_t len)
89
+ {
90
+ ssize_t read;
91
+ binyo_instream_cache *in;
92
+
93
+ int_safe_cast(in, instream);
94
+
95
+ if (!buf) return BINYO_ERR;
96
+
97
+ read = binyo_instream_read(in->inner, buf, len);
98
+ if (read > 0)
99
+ binyo_outstream_write(in->bytes, buf, read);
100
+ return read;
101
+ }
102
+
103
+ static int
104
+ int_cache_seek(binyo_instream *instream, off_t offset, int whence)
105
+ {
106
+ binyo_instream_cache *in;
107
+
108
+ int_safe_cast(in, instream);
109
+ return binyo_instream_seek(in->inner, offset, whence);
110
+ }
111
+
112
+ static void
113
+ int_cache_mark(binyo_instream *instream)
114
+ {
115
+ binyo_instream_cache *in;
116
+
117
+ int_safe_cast(in, instream);
118
+ binyo_instream_mark(in->inner);
119
+ }
120
+
121
+ static void
122
+ int_cache_free(binyo_instream *instream)
123
+ {
124
+ binyo_instream_cache *in;
125
+
126
+ if (!instream) return;
127
+
128
+ int_safe_cast(in, instream);
129
+ binyo_instream_free(in->inner);
130
+ binyo_outstream_free(in->bytes);
131
+ }
132
+
133
+ void
134
+ binyo_instream_cache_free_wrapper(binyo_instream *instream)
135
+ {
136
+ binyo_instream_cache *in;
137
+
138
+ if (!instream) return;
139
+
140
+ int_safe_cast(in, instream);
141
+ binyo_outstream_free(in->bytes);
142
+ xfree(in);
143
+ }
144
+