binyo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
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
+ #if !defined(_BINYO_H_)
29
+ #define _BINYO_H_
30
+
31
+ #include RUBY_EXTCONF_H
32
+
33
+ #if defined(__cplusplus)
34
+ extern "C" {
35
+ #endif
36
+
37
+ #define RSTRING_NOT_MODIFIED 1
38
+ #define RUBY_READONLY_STRING 1
39
+
40
+ #include <ruby.h>
41
+
42
+ #if defined(HAVE_RUBY_IO_H)
43
+ #include <ruby/io.h>
44
+ #endif
45
+
46
+ #include "binyo-missing.h"
47
+
48
+ #ifdef _WIN32
49
+ #define binyo_last_sys_error() GetLastError()
50
+ #define binyo_clear_sys_error() SetLastError(0)
51
+ #else
52
+ #define binyo_last_sys_error() errno
53
+ #define binyo_clear_sys_error() errno=0
54
+ #endif
55
+
56
+ /* include headers */
57
+ #include "binyo-mem.h"
58
+ #include "binyo-error.h"
59
+ #include "binyo-error-internal.h"
60
+ #include "binyo-io.h"
61
+ #include "bytelist.h"
62
+ #include "byte.h"
63
+
64
+ extern VALUE mBinyo;
65
+
66
+ extern VALUE eBinyoError;
67
+ void Init_binyo(void);
68
+ void Init_binyo_io(void);
69
+
70
+ #if defined(__cplusplus)
71
+ }
72
+ #endif
73
+
74
+ #endif /* _BINYO_H_ */
75
+
@@ -0,0 +1,116 @@
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
+ VALUE cBinyoByte;
31
+
32
+ #define int_byte_get(obj, b) \
33
+ do { \
34
+ Data_Get_Struct((obj), uint8_t, (b)); \
35
+ if (!(b)) { \
36
+ rb_raise(eBinyoError, "Uninitialized byte"); \
37
+ } \
38
+ } while (0)
39
+
40
+ static void
41
+ int_byte_free(uint8_t *b)
42
+ {
43
+ if (b) BINYO_FREE(b);
44
+ }
45
+
46
+ static VALUE
47
+ binyo_byte_alloc(VALUE klass)
48
+ {
49
+ VALUE obj;
50
+
51
+ obj = Data_Wrap_Struct(klass, 0, int_byte_free, 0);
52
+ return obj;
53
+ }
54
+
55
+ VALUE
56
+ binyo_byte_new(uint8_t input)
57
+ {
58
+ VALUE obj;
59
+ uint8_t *b;
60
+
61
+ b = BINYO_ALLOC(uint8_t);
62
+ *b = input;
63
+ obj = Data_Wrap_Struct(cBinyoByte, 0, int_byte_free, b);
64
+ return obj;
65
+ }
66
+
67
+ static VALUE
68
+ binyo_byte_initialize(VALUE self, VALUE byteval)
69
+ {
70
+ uint8_t *b;
71
+
72
+ b = BINYO_ALLOC(uint8_t);
73
+ *b = NUM2INT(byteval) & 0xff;
74
+ DATA_PTR(self) = b;
75
+ return self;
76
+ }
77
+
78
+ static VALUE
79
+ binyo_byte_shiftr(VALUE self, VALUE vshift)
80
+ {
81
+ uint8_t *b;
82
+
83
+ int_byte_get(self, b);
84
+ return binyo_byte_new(*b >> (NUM2INT(vshift)));
85
+ }
86
+
87
+ static VALUE
88
+ binyo_byte_and(VALUE self, VALUE vmask)
89
+ {
90
+ uint8_t *b;
91
+
92
+ int_byte_get(self, b);
93
+ return binyo_byte_new(*b & (NUM2INT(vmask)));
94
+ }
95
+
96
+ static VALUE
97
+ binyo_byte_to_i(VALUE self)
98
+ {
99
+ uint8_t *b;
100
+
101
+ int_byte_get(self, b);
102
+ return INT2NUM(*b);
103
+ }
104
+
105
+ void
106
+ Init_binyo_byte(void)
107
+ {
108
+ cBinyoByte = rb_define_class_under(mBinyo, "Byte", rb_cObject);
109
+
110
+ rb_define_alloc_func(cBinyoByte, binyo_byte_alloc);
111
+ rb_define_method(cBinyoByte, "initialize", binyo_byte_initialize, 1);
112
+ rb_define_method(cBinyoByte, ">>", binyo_byte_shiftr, 1);
113
+ rb_define_method(cBinyoByte, "&", binyo_byte_and, 1);
114
+ rb_define_method(cBinyoByte, "to_i", binyo_byte_to_i, 0);
115
+ }
116
+
@@ -0,0 +1,39 @@
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
+ #if !defined(_BYTE_H_)
29
+ #define _BYTE_H_
30
+
31
+ extern VALUE cBinyoByte;
32
+
33
+ VALUE binyo_byte_new(uint8_t input);
34
+
35
+ void Init_binyo_byte(void);
36
+
37
+ #endif /* _BYTE_H_ */
38
+
39
+
@@ -0,0 +1,223 @@
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
+ VALUE cBinyoByteList;
31
+
32
+ struct binyo_bytelist_st
33
+ {
34
+ uint8_t *bytes;
35
+ size_t len;
36
+ size_t limit;
37
+ };
38
+
39
+ #define int_bytelist_get(obj, bytelist) \
40
+ do { \
41
+ Data_Get_Struct((obj), binyo_bytelist, (bytelist)); \
42
+ if (!(bytelist)) { \
43
+ rb_raise(eBinyoError, "Uninitialized byte list"); \
44
+ } \
45
+ } while (0)
46
+
47
+ static binyo_bytelist *
48
+ int_bytelist_new()
49
+ {
50
+ binyo_bytelist *bytelist;
51
+
52
+ bytelist = BINYO_ALLOC(binyo_bytelist);
53
+ memset(bytelist, 0, sizeof(binyo_bytelist));
54
+ return bytelist;
55
+ }
56
+
57
+ binyo_bytelist *
58
+ binyo_bytelist_new_size(size_t size)
59
+ {
60
+ binyo_bytelist *bytelist = int_bytelist_new();
61
+
62
+ bytelist->bytes = BINYO_ALLOC_N(uint8_t, size);
63
+ bytelist->limit = size;
64
+ return bytelist;
65
+ }
66
+
67
+ binyo_bytelist *
68
+ binyo_bytelist_new_size_default(size_t size, uint8_t default_value)
69
+ {
70
+ size_t i;
71
+ binyo_bytelist *bytelist = binyo_bytelist_new_size(size);
72
+
73
+ for (i=0; i < size; i++) {
74
+ bytelist->bytes[i] = default_value;
75
+ }
76
+
77
+ bytelist->len = size;
78
+ return bytelist;
79
+ }
80
+
81
+ binyo_bytelist *
82
+ binyo_bytelist_new_bytes(uint8_t *bytes, size_t len)
83
+ {
84
+ binyo_bytelist *bytelist = binyo_bytelist_new_size(len);
85
+
86
+ memcpy(bytelist->bytes, bytes, len);
87
+ bytelist->len = len;
88
+ return bytelist;
89
+ }
90
+
91
+ void
92
+ binyo_bytelist_free(binyo_bytelist *bytelist)
93
+ {
94
+ if (bytelist) {
95
+ if (bytelist->bytes)
96
+ BINYO_FREE(bytelist->bytes);
97
+ BINYO_FREE(bytelist);
98
+ }
99
+ }
100
+
101
+ static VALUE
102
+ binyo_bytelist_alloc(VALUE klass)
103
+ {
104
+ VALUE obj;
105
+
106
+ obj = Data_Wrap_Struct(klass, 0, binyo_bytelist_free, 0);
107
+ return obj;
108
+ }
109
+
110
+ static VALUE
111
+ binyo_bytelist_initialize(int argc, VALUE *argv, VALUE self)
112
+ {
113
+ VALUE vfirst;
114
+ VALUE vsize = Qnil;
115
+ binyo_bytelist *bytelist;
116
+
117
+ rb_scan_args(argc, argv, "11", &vfirst, &vsize);
118
+
119
+ if (NIL_P(vfirst))
120
+ rb_raise(rb_eArgError, "First argument may not be nil");
121
+ if (argc == 2 && NIL_P(vsize))
122
+ rb_raise(rb_eArgError, "Size argument may not be nil");
123
+
124
+ if (TYPE(vfirst) == T_STRING) {
125
+ bytelist = binyo_bytelist_new_bytes((uint8_t *) RSTRING_PTR(vfirst), (size_t) RSTRING_LEN(vfirst));
126
+ } else {
127
+ bytelist = binyo_bytelist_new_size((size_t) NUM2LONG(vfirst));
128
+ }
129
+
130
+ DATA_PTR(self) = bytelist;
131
+ return self;
132
+ }
133
+
134
+ static VALUE
135
+ binyo_bytelist_get_index(VALUE self, VALUE vidx)
136
+ {
137
+ binyo_bytelist *bytelist;
138
+ size_t i = (size_t) NUM2LONG(vidx);
139
+
140
+ int_bytelist_get(self, bytelist);
141
+
142
+ if (i >= bytelist->len)
143
+ rb_raise(eBinyoError, "Index out of bounds: %lu", i); /* TODO treat it like Array */
144
+
145
+ return INT2NUM(bytelist->bytes[i]);
146
+ }
147
+
148
+ static VALUE
149
+ binyo_bytelist_set_index(VALUE self, VALUE vidx, VALUE byteval)
150
+ {
151
+ binyo_bytelist *bytelist;
152
+ size_t i = (size_t) NUM2LONG(vidx);
153
+
154
+ int_bytelist_get(self, bytelist);
155
+
156
+ if (i >= bytelist->limit)
157
+ rb_raise(eBinyoError, "Index out of bounds: %lu", i); /* TODO treat it like Array */
158
+
159
+ bytelist->bytes[i] = NUM2INT(byteval) & 0xff;
160
+ if (i >= bytelist->len)
161
+ bytelist->len = i + 1;
162
+
163
+ return byteval;
164
+ }
165
+
166
+ static VALUE
167
+ binyo_bytelist_push(VALUE self, VALUE byteval)
168
+ {
169
+ binyo_bytelist *bytelist;
170
+
171
+ int_bytelist_get(self, bytelist);
172
+
173
+ if (bytelist->len == bytelist->limit)
174
+ rb_raise(eBinyoError, "Cannot append to ByteList"); /* TODO auto-resize */
175
+
176
+ bytelist->bytes[bytelist->len++] = NUM2INT(byteval) & 0xff;
177
+ return self;
178
+ }
179
+
180
+ static VALUE
181
+ binyo_bytelist_each(VALUE self)
182
+ {
183
+ binyo_bytelist *bytelist;
184
+ size_t i;
185
+
186
+ if (!rb_block_given_p()) {
187
+ /* TODO */
188
+ rb_raise(eBinyoError, "No block given");
189
+ }
190
+
191
+ int_bytelist_get(self, bytelist);
192
+
193
+ for (i=0; i < bytelist->len; i++) {
194
+ rb_yield(INT2NUM(bytelist->bytes[i]));
195
+ }
196
+
197
+ return self;
198
+ }
199
+
200
+ static VALUE
201
+ binyo_bytelist_to_s(VALUE self)
202
+ {
203
+ binyo_bytelist *bytelist;
204
+
205
+ int_bytelist_get(self, bytelist);
206
+
207
+ return rb_str_new((const char *)bytelist->bytes, (long) bytelist->len);
208
+ }
209
+
210
+ void
211
+ Init_binyo_bytelist(void)
212
+ {
213
+ cBinyoByteList = rb_define_class_under(mBinyo, "ByteList", rb_cObject);
214
+
215
+ rb_define_alloc_func(cBinyoByteList, binyo_bytelist_alloc);
216
+ rb_define_method(cBinyoByteList, "initialize", binyo_bytelist_initialize, -1);
217
+ rb_define_method(cBinyoByteList, "each", binyo_bytelist_each, 0);
218
+ rb_define_method(cBinyoByteList, "[]", binyo_bytelist_get_index, 1);
219
+ rb_define_method(cBinyoByteList, "[]=", binyo_bytelist_set_index, 2);
220
+ rb_define_method(cBinyoByteList, "<<", binyo_bytelist_push, 1);
221
+ rb_define_method(cBinyoByteList, "to_s", binyo_bytelist_to_s, 0);
222
+ }
223
+