msgpack 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,26 @@
1
+ /*
2
+ * MessagePack unpacking routine for Ruby
3
+ *
4
+ * Copyright (C) 2008 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef UNPACK_H__
19
+ #define UNPACK_H__
20
+
21
+ #include "ruby.h"
22
+
23
+ void Init_msgpack_unpack(VALUE mMessagePack);
24
+
25
+ #endif /* unpack.h */
26
+
@@ -0,0 +1,35 @@
1
+ /*
2
+ * MessagePack unpacking routine for Ruby
3
+ *
4
+ * Copyright (C) 2008 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef UNPACK_CONTEXT_H__
19
+ #define UNPACK_CONTEXT_H__
20
+
21
+ #include "ruby.h"
22
+ #include <stddef.h>
23
+ #include <stdbool.h>
24
+
25
+ typedef VALUE msgpack_object;
26
+
27
+ typedef struct {
28
+ bool finished;
29
+ } msgpack_unpack_context;
30
+
31
+
32
+ #include "msgpack/unpack/inline_context.h"
33
+
34
+ #endif /* unpack_context.h */
35
+
@@ -0,0 +1,81 @@
1
+ /*
2
+ * MessagePack unpacking routine for Ruby
3
+ *
4
+ * Copyright (C) 2008 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #include "unpack_context.h"
19
+
20
+ static inline VALUE msgpack_unpack_init(msgpack_unpack_context* x)
21
+ { return Qnil; }
22
+
23
+ static inline VALUE msgpack_unpack_unsigned_int_8(msgpack_unpack_context* x, uint8_t d)
24
+ { return INT2FIX(d); }
25
+
26
+ static inline VALUE msgpack_unpack_unsigned_int_16(msgpack_unpack_context* x, uint16_t d)
27
+ { return INT2FIX(d); }
28
+
29
+ static inline VALUE msgpack_unpack_unsigned_int_32(msgpack_unpack_context* x, uint32_t d)
30
+ { return UINT2NUM(d); }
31
+
32
+ static inline VALUE msgpack_unpack_unsigned_int_64(msgpack_unpack_context* x, uint64_t d)
33
+ { return UINT2NUM(d); } // FIXME
34
+
35
+ static inline VALUE msgpack_unpack_signed_int_8(msgpack_unpack_context* x, int8_t d)
36
+ { return INT2FIX((long)d); }
37
+
38
+ static inline VALUE msgpack_unpack_signed_int_16(msgpack_unpack_context* x, int16_t d)
39
+ { return INT2FIX((long)d); }
40
+
41
+ static inline VALUE msgpack_unpack_signed_int_32(msgpack_unpack_context* x, int32_t d)
42
+ { return INT2NUM((long)d); }
43
+
44
+ static inline VALUE msgpack_unpack_signed_int_64(msgpack_unpack_context* x, int64_t d)
45
+ { return INT2NUM(d); } // FIXME
46
+
47
+ static inline VALUE msgpack_unpack_float(msgpack_unpack_context* x, float d)
48
+ { return rb_float_new(d); }
49
+
50
+ static inline VALUE msgpack_unpack_double(msgpack_unpack_context* x, double d)
51
+ { return rb_float_new(d); }
52
+
53
+ static inline VALUE msgpack_unpack_nil(msgpack_unpack_context* x)
54
+ { return Qnil; }
55
+
56
+ static inline VALUE msgpack_unpack_true(msgpack_unpack_context* x)
57
+ { return Qtrue; }
58
+
59
+ static inline VALUE msgpack_unpack_false(msgpack_unpack_context* x)
60
+ { return Qfalse; }
61
+
62
+ static inline VALUE msgpack_unpack_array_start(msgpack_unpack_context* x, unsigned int n)
63
+ { return rb_ary_new2(n); }
64
+
65
+ static inline void msgpack_unpack_array_item(msgpack_unpack_context* x, VALUE c, VALUE o)
66
+ { rb_ary_push(c, o); }
67
+
68
+ static inline VALUE msgpack_unpack_map_start(msgpack_unpack_context* x, unsigned int n)
69
+ { return rb_hash_new(); }
70
+
71
+ static inline void msgpack_unpack_map_item(msgpack_unpack_context* x, VALUE c, VALUE k, VALUE v)
72
+ { rb_hash_aset(c, k, v); }
73
+
74
+ static inline VALUE msgpack_unpack_string(msgpack_unpack_context* x, const void* b, size_t l)
75
+ { return rb_str_new(b, l); }
76
+
77
+ static inline VALUE msgpack_unpack_raw(msgpack_unpack_context* x, const void* b, size_t l)
78
+ { return rb_str_new(b, l); }
79
+
80
+ #include "msgpack/unpack/inline_impl.h"
81
+
@@ -0,0 +1,9 @@
1
+ module MessagePack
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,287 @@
1
+ /*
2
+ * MessagePack packing routine
3
+ *
4
+ * Copyright (C) 2008 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_PACK_INLINE_IMPL_H__
19
+ #define MSGPACK_PACK_INLINE_IMPL_H__
20
+
21
+ #include <string.h>
22
+ #include <arpa/inet.h>
23
+
24
+ #ifdef __LITTLE_ENDIAN__
25
+
26
+ #define STORE_16(d) \
27
+ ((char*)&d)[1], ((char*)&d)[0]
28
+
29
+ #define STORE_32(d) \
30
+ ((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
31
+
32
+ #define STORE_64(d) \
33
+ ((char*)&d)[7], ((char*)&d)[6], ((char*)&d)[5], ((char*)&d)[4], \
34
+ ((char*)&d)[3], ((char*)&d)[2], ((char*)&d)[1], ((char*)&d)[0]
35
+
36
+ #elif __BIG_ENDIAN__
37
+
38
+ #define STORE_16(d) \
39
+ ((char*)&d)[2], ((char*)&d)[3]
40
+
41
+ #define STORE_32(d) \
42
+ ((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3]
43
+
44
+ #define STORE_32(d) \
45
+ ((char*)&d)[0], ((char*)&d)[1], ((char*)&d)[2], ((char*)&d)[3], \
46
+ ((char*)&d)[4], ((char*)&d)[5], ((char*)&d)[6], ((char*)&d)[7]
47
+
48
+ #endif
49
+
50
+
51
+ /*
52
+ * Integer
53
+ */
54
+
55
+ // wrapper
56
+ inline void msgpack_pack_int(msgpack_pack_context x, int d)
57
+ {
58
+ if(d < -32) {
59
+ if(d < -32768) { // signed 32
60
+ const unsigned char buf[5] = {0xd2, STORE_32(d)};
61
+ msgpack_pack_append_buffer(x, buf, 5);
62
+ } else if(d < -128) { // signed 16
63
+ const unsigned char buf[3] = {0xd1, STORE_16(d)};
64
+ msgpack_pack_append_buffer(x, buf, 3);
65
+ } else { // signed 8
66
+ const unsigned char buf[2] = {0xd0, (uint8_t)d};
67
+ msgpack_pack_append_buffer(x, buf, 2);
68
+ }
69
+ } else if(d < 128) { // fixnum
70
+ msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
71
+ } else {
72
+ if(d < 256) {
73
+ // unsigned 8
74
+ const unsigned char buf[2] = {0xcc, (uint8_t)d};
75
+ msgpack_pack_append_buffer(x, buf, 2);
76
+ } else if(d < 65536) {
77
+ // unsigned 16
78
+ const unsigned char buf[3] = {0xcd, STORE_16(d)};
79
+ msgpack_pack_append_buffer(x, buf, 3);
80
+ } else {
81
+ // unsigned 32
82
+ const unsigned char buf[5] = {0xce, STORE_32(d)};
83
+ msgpack_pack_append_buffer(x, buf, 5);
84
+ }
85
+ }
86
+ }
87
+
88
+ // wrapper
89
+ inline void msgpack_pack_unsigned_int(msgpack_pack_context x, unsigned int d)
90
+ {
91
+ if(d < 128) {
92
+ // fixnum
93
+ msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
94
+ } else if(d < 256) {
95
+ // unsigned 8
96
+ const unsigned char buf[2] = {0xcc, (uint8_t)d};
97
+ msgpack_pack_append_buffer(x, buf, 2);
98
+ } else if(d < 65536) {
99
+ // unsigned 16
100
+ const unsigned char buf[3] = {0xcd, STORE_16(d)};
101
+ msgpack_pack_append_buffer(x, buf, 3);
102
+ } else {
103
+ // unsigned 32
104
+ const unsigned char buf[5] = {0xce, STORE_32(d)};
105
+ msgpack_pack_append_buffer(x, buf, 5);
106
+ }
107
+ }
108
+
109
+ inline void msgpack_pack_unsigned_int_8(msgpack_pack_context x, uint8_t d)
110
+ {
111
+ if(d < 128) {
112
+ msgpack_pack_append_buffer(x, &d, 1);
113
+ } else {
114
+ const unsigned char buf[2] = {0xcc, d};
115
+ msgpack_pack_append_buffer(x, buf, 2);
116
+ }
117
+ }
118
+
119
+ inline void msgpack_pack_unsigned_int_16(msgpack_pack_context x, uint16_t d)
120
+ {
121
+ const unsigned char buf[3] = {0xcd, STORE_16(d)};
122
+ msgpack_pack_append_buffer(x, buf, 3);
123
+ }
124
+
125
+ inline void msgpack_pack_unsigned_int_32(msgpack_pack_context x, uint32_t d)
126
+ {
127
+ const unsigned char buf[5] = {0xce, STORE_32(d)};
128
+ msgpack_pack_append_buffer(x, buf, 5);
129
+ }
130
+
131
+ inline void msgpack_pack_unsigned_int_64(msgpack_pack_context x, uint64_t d)
132
+ {
133
+ // FIXME
134
+ const unsigned char buf[9] = {0xcf, STORE_64(d)};
135
+ msgpack_pack_append_buffer(x, buf, 9);
136
+ }
137
+
138
+ inline void msgpack_pack_signed_int_8(msgpack_pack_context x, int8_t d)
139
+ {
140
+ if(d > 0) {
141
+ msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
142
+ } else if(d >= -32) {
143
+ msgpack_pack_append_buffer(x, (uint8_t*)&d, 1);
144
+ } else {
145
+ const unsigned char buf[2] = {0xd0, d};
146
+ msgpack_pack_append_buffer(x, buf, 2);
147
+ }
148
+ }
149
+
150
+ inline void msgpack_pack_signed_int_16(msgpack_pack_context x, int16_t d)
151
+ {
152
+ const unsigned char buf[3] = {0xd1, STORE_16(d)};
153
+ msgpack_pack_append_buffer(x, buf, 3);
154
+ }
155
+
156
+ inline void msgpack_pack_signed_int_32(msgpack_pack_context x, int32_t d)
157
+ {
158
+ const unsigned char buf[5] = {0xd2, STORE_32(d)};
159
+ msgpack_pack_append_buffer(x, buf, 5);
160
+ }
161
+
162
+ inline void msgpack_pack_signed_int_64(msgpack_pack_context x, int64_t d)
163
+ {
164
+ // FIXME
165
+ const unsigned char buf[9] = {0xd3, STORE_64(d)};
166
+ msgpack_pack_append_buffer(x, buf, 9);
167
+ }
168
+
169
+
170
+ /*
171
+ * Float
172
+ */
173
+
174
+ inline void msgpack_pack_float(msgpack_pack_context x, float d)
175
+ {
176
+ uint32_t n = *((uint32_t*)&d); // FIXME
177
+ const unsigned char buf[5] = {0xca, STORE_32(n)};
178
+ msgpack_pack_append_buffer(x, buf, 5);
179
+ }
180
+
181
+ inline void msgpack_pack_double(msgpack_pack_context x, double d)
182
+ {
183
+ uint64_t n = *((uint64_t*)&d); // FIXME
184
+ const unsigned char buf[9] = {0xcb, STORE_64(n)};
185
+ msgpack_pack_append_buffer(x, buf, 9);
186
+ }
187
+
188
+
189
+ /*
190
+ * Nil
191
+ */
192
+
193
+ inline void msgpack_pack_nil(msgpack_pack_context x)
194
+ {
195
+ static const unsigned char d = 0xc0;
196
+ msgpack_pack_append_buffer(x, &d, 1);
197
+ }
198
+
199
+
200
+ /*
201
+ * Boolean
202
+ */
203
+ inline void msgpack_pack_true(msgpack_pack_context x)
204
+ {
205
+ static const unsigned char d = 0xc3;
206
+ msgpack_pack_append_buffer(x, &d, 1);
207
+ }
208
+
209
+ inline void msgpack_pack_false(msgpack_pack_context x)
210
+ {
211
+ static const unsigned char d = 0xc2;
212
+ msgpack_pack_append_buffer(x, &d, 1);
213
+ }
214
+
215
+
216
+ /*
217
+ * Array
218
+ */
219
+
220
+ inline void msgpack_pack_array(msgpack_pack_context x, unsigned int n)
221
+ {
222
+ if(n < 16) {
223
+ unsigned char d = 0x90 | n;
224
+ msgpack_pack_append_buffer(x, &d, 1);
225
+ } else if(n < 65536) {
226
+ uint16_t d = (uint16_t)n;
227
+ unsigned char buf[3] = {0xdc, STORE_16(d)};
228
+ msgpack_pack_append_buffer(x, buf, 3);
229
+ } else {
230
+ uint32_t d = (uint32_t)n;
231
+ unsigned char buf[5] = {0xdd, STORE_32(d)};
232
+ msgpack_pack_append_buffer(x, buf, 5);
233
+ }
234
+ }
235
+
236
+
237
+ /*
238
+ * Map
239
+ */
240
+
241
+ inline void msgpack_pack_map(msgpack_pack_context x, unsigned int n)
242
+ {
243
+ if(n < 16) {
244
+ unsigned char d = 0x80 | n;
245
+ msgpack_pack_append_buffer(x, &d, 1);
246
+ } else if(n < 65536) {
247
+ uint16_t d = (uint16_t)n;
248
+ unsigned char buf[3] = {0xde, STORE_16(d)};
249
+ msgpack_pack_append_buffer(x, buf, 3);
250
+ } else {
251
+ uint32_t d = (uint32_t)n;
252
+ unsigned char buf[5] = {0xdf, STORE_32(d)};
253
+ msgpack_pack_append_buffer(x, buf, 5);
254
+ }
255
+ }
256
+
257
+
258
+ /*
259
+ * String
260
+ */
261
+
262
+ inline void msgpack_pack_string(msgpack_pack_context x, const char* b)
263
+ {
264
+ uint32_t l = strlen(b);
265
+ msgpack_pack_append_buffer(x, (const unsigned char*)b, l+1);
266
+ }
267
+
268
+ inline void msgpack_pack_raw(msgpack_pack_context x, const void* b, size_t l)
269
+ {
270
+ if(l < 32) {
271
+ unsigned char d = 0xa0 | l;
272
+ msgpack_pack_append_buffer(x, &d, 1);
273
+ } else if(l < 65536) {
274
+ uint16_t d = (uint16_t)l;
275
+ unsigned char buf[3] = {0xda, STORE_16(d)};
276
+ msgpack_pack_append_buffer(x, buf, 3);
277
+ } else {
278
+ uint32_t d = (uint32_t)l;
279
+ unsigned char buf[5] = {0xdb, STORE_32(d)};
280
+ msgpack_pack_append_buffer(x, buf, 5);
281
+ }
282
+ msgpack_pack_append_buffer(x, b, l);
283
+ }
284
+
285
+
286
+ #endif /* msgpack/pack/inline_impl.h */
287
+
@@ -0,0 +1,52 @@
1
+ /*
2
+ * MessagePack unpacking routine
3
+ *
4
+ * Copyright (C) 2008 FURUHASHI Sadayuki
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ #ifndef MSGPACK_UNPACK_INLINE_CONTEXT_H__
19
+ #define MSGPACK_UNPACK_INLINE_CONTEXT_H__
20
+
21
+ #include <stddef.h>
22
+ #include <stdint.h>
23
+
24
+ #ifndef MSG_STACK_SIZE
25
+ #define MSG_STACK_SIZE 16
26
+ #endif
27
+
28
+ typedef struct {
29
+ msgpack_object obj;
30
+ size_t count;
31
+ unsigned int ct;
32
+ union {
33
+ const unsigned char* terminal_trail_start;
34
+ msgpack_object map_key;
35
+ } tmp;
36
+ } msgpack_unpacker_stack;
37
+
38
+ typedef struct {
39
+ msgpack_unpack_context user; // must be first
40
+ unsigned int cs;
41
+ size_t trail;
42
+ unsigned int top;
43
+ msgpack_unpacker_stack stack[MSG_STACK_SIZE];
44
+ } msgpack_unpacker;
45
+
46
+ void msgpack_unpacker_init(msgpack_unpacker* ctx);
47
+ int msgpack_unpacker_execute(msgpack_unpacker* ctx, const char* data, size_t len, size_t* off);
48
+ #define msgpack_unpacker_data(unpacker) (unpacker)->stack[0].obj
49
+
50
+
51
+ #endif /* msgpack/unpack/inline_context.h */
52
+