blob: 725d3381a3348dbc775f976bf04bce51ef56dad7 [file] [log] [blame]
Kevin Clark916f3532009-03-20 04:21:39 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Bryan Duxburyd815c212009-03-19 18:57:43 +000020#include <ruby.h>
21#include <stdbool.h>
22#include <stdint.h>
Jake Farrellb5a18a12012-10-09 01:10:43 +000023#include <constants.h>
24#include <struct.h>
25#include <macros.h>
26#include <bytes.h>
Bryan Duxburyd815c212009-03-19 18:57:43 +000027
28#define LAST_ID(obj) FIX2INT(rb_ary_pop(rb_ivar_get(obj, last_field_id)))
29#define SET_LAST_ID(obj, val) rb_ary_push(rb_ivar_get(obj, last_field_id), val)
30
31VALUE rb_thrift_compact_proto_native_qmark(VALUE self) {
32 return Qtrue;
33}
34
35static ID last_field_id;
36static ID boolean_field_id;
37static ID bool_value_id;
Bryan Duxburyad0ad822011-06-28 18:46:03 +000038static ID rbuf_ivar_id;
Bryan Duxburyd815c212009-03-19 18:57:43 +000039
40static int VERSION;
41static int VERSION_MASK;
42static int TYPE_MASK;
43static int TYPE_SHIFT_AMOUNT;
44static int PROTOCOL_ID;
45
46static VALUE thrift_compact_protocol_class;
47
48static int CTYPE_BOOLEAN_TRUE = 0x01;
49static int CTYPE_BOOLEAN_FALSE = 0x02;
50static int CTYPE_BYTE = 0x03;
51static int CTYPE_I16 = 0x04;
52static int CTYPE_I32 = 0x05;
53static int CTYPE_I64 = 0x06;
54static int CTYPE_DOUBLE = 0x07;
55static int CTYPE_BINARY = 0x08;
56static int CTYPE_LIST = 0x09;
57static int CTYPE_SET = 0x0A;
58static int CTYPE_MAP = 0x0B;
59static int CTYPE_STRUCT = 0x0C;
60
61VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16);
62
63// TODO: implement this
64static int get_compact_type(VALUE type_value) {
65 int type = FIX2INT(type_value);
66 if (type == TTYPE_BOOL) {
67 return CTYPE_BOOLEAN_TRUE;
68 } else if (type == TTYPE_BYTE) {
69 return CTYPE_BYTE;
70 } else if (type == TTYPE_I16) {
71 return CTYPE_I16;
72 } else if (type == TTYPE_I32) {
73 return CTYPE_I32;
74 } else if (type == TTYPE_I64) {
75 return CTYPE_I64;
76 } else if (type == TTYPE_DOUBLE) {
77 return CTYPE_DOUBLE;
78 } else if (type == TTYPE_STRING) {
79 return CTYPE_BINARY;
80 } else if (type == TTYPE_LIST) {
81 return CTYPE_LIST;
82 } else if (type == TTYPE_SET) {
83 return CTYPE_SET;
84 } else if (type == TTYPE_MAP) {
85 return CTYPE_MAP;
86 } else if (type == TTYPE_STRUCT) {
87 return CTYPE_STRUCT;
88 } else {
89 char str[50];
90 sprintf(str, "don't know what type: %d", type);
Bryan Duxburyfd58c552009-09-01 22:32:50 +000091 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +000092 return 0;
93 }
94}
95
96static void write_byte_direct(VALUE transport, int8_t b) {
97 WRITE(transport, (char*)&b, 1);
98}
99
100static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, VALUE type_override) {
101 int id = FIX2INT(id_value);
102 int last_id = LAST_ID(self);
103 VALUE transport = GET_TRANSPORT(self);
104
105 // if there's a type override, use that.
106 int8_t type_to_write = RTEST(type_override) ? FIX2INT(type_override) : get_compact_type(type);
107 // check if we can use delta encoding for the field id
108 int diff = id - last_id;
109 if (diff > 0 && diff <= 15) {
110 // write them together
111 write_byte_direct(transport, diff << 4 | (type_to_write & 0x0f));
112 } else {
113 // write them separate
114 write_byte_direct(transport, type_to_write & 0x0f);
115 rb_thrift_compact_proto_write_i16(self, id_value);
116 }
117
118 SET_LAST_ID(self, id_value);
119}
120
121static int32_t int_to_zig_zag(int32_t n) {
122 return (n << 1) ^ (n >> 31);
123}
124
125static uint64_t ll_to_zig_zag(int64_t n) {
126 return (n << 1) ^ (n >> 63);
127}
128
129static void write_varint32(VALUE transport, uint32_t n) {
130 while (true) {
131 if ((n & ~0x7F) == 0) {
132 write_byte_direct(transport, n & 0x7f);
133 break;
134 } else {
135 write_byte_direct(transport, (n & 0x7F) | 0x80);
136 n = n >> 7;
137 }
138 }
139}
140
141static void write_varint64(VALUE transport, uint64_t n) {
142 while (true) {
143 if ((n & ~0x7F) == 0) {
144 write_byte_direct(transport, n & 0x7f);
145 break;
146 } else {
147 write_byte_direct(transport, (n & 0x7F) | 0x80);
148 n = n >> 7;
149 }
150 }
151}
152
153static void write_collection_begin(VALUE transport, VALUE elem_type, VALUE size_value) {
154 int size = FIX2INT(size_value);
155 if (size <= 14) {
156 write_byte_direct(transport, size << 4 | get_compact_type(elem_type));
157 } else {
158 write_byte_direct(transport, 0xf0 | get_compact_type(elem_type));
159 write_varint32(transport, size);
160 }
161}
162
163
164//--------------------------------
165// interface writing methods
166//--------------------------------
167
168VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
169VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
Roger Meier19dbbef2012-12-27 01:24:20 +0100170VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000171
172VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
173 return Qnil;
174}
175
176VALUE rb_thrift_compact_proto_write_struct_begin(VALUE self, VALUE name) {
177 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
178 return Qnil;
179}
180
181VALUE rb_thrift_compact_proto_write_struct_end(VALUE self) {
182 rb_ary_pop(rb_ivar_get(self, last_field_id));
183 return Qnil;
184}
185
186VALUE rb_thrift_compact_proto_write_field_end(VALUE self) {
187 return Qnil;
188}
189
190VALUE rb_thrift_compact_proto_write_map_end(VALUE self) {
191 return Qnil;
192}
193
194VALUE rb_thrift_compact_proto_write_list_end(VALUE self) {
195 return Qnil;
196}
197
198VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
199 return Qnil;
200}
201
202VALUE rb_thrift_compact_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
203 VALUE transport = GET_TRANSPORT(self);
204 write_byte_direct(transport, PROTOCOL_ID);
205 write_byte_direct(transport, (VERSION & VERSION_MASK) | ((FIX2INT(type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
206 write_varint32(transport, FIX2INT(seqid));
207 rb_thrift_compact_proto_write_string(self, name);
208
209 return Qnil;
210}
211
212VALUE rb_thrift_compact_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
213 if (FIX2INT(type) == TTYPE_BOOL) {
214 // we want to possibly include the value, so we'll wait.
215 rb_ivar_set(self, boolean_field_id, rb_ary_new3(2, type, id));
216 } else {
217 write_field_begin_internal(self, type, id, Qnil);
218 }
219
220 return Qnil;
221}
222
223VALUE rb_thrift_compact_proto_write_field_stop(VALUE self) {
224 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
225 return Qnil;
226}
227
228VALUE rb_thrift_compact_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size_value) {
229 int size = FIX2INT(size_value);
230 VALUE transport = GET_TRANSPORT(self);
231 if (size == 0) {
232 write_byte_direct(transport, 0);
233 } else {
234 write_varint32(transport, size);
235 write_byte_direct(transport, get_compact_type(ktype) << 4 | get_compact_type(vtype));
236 }
237 return Qnil;
238}
239
240VALUE rb_thrift_compact_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
241 write_collection_begin(GET_TRANSPORT(self), etype, size);
242 return Qnil;
243}
244
245VALUE rb_thrift_compact_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
246 write_collection_begin(GET_TRANSPORT(self), etype, size);
247 return Qnil;
248}
249
250VALUE rb_thrift_compact_proto_write_bool(VALUE self, VALUE b) {
251 int8_t type = b == Qtrue ? CTYPE_BOOLEAN_TRUE : CTYPE_BOOLEAN_FALSE;
252 VALUE boolean_field = rb_ivar_get(self, boolean_field_id);
253 if (NIL_P(boolean_field)) {
254 // we're not part of a field, so just write the value.
255 write_byte_direct(GET_TRANSPORT(self), type);
256 } else {
257 // we haven't written the field header yet
258 write_field_begin_internal(self, rb_ary_entry(boolean_field, 0), rb_ary_entry(boolean_field, 1), INT2FIX(type));
259 rb_ivar_set(self, boolean_field_id, Qnil);
260 }
261 return Qnil;
262}
263
264VALUE rb_thrift_compact_proto_write_byte(VALUE self, VALUE byte) {
265 CHECK_NIL(byte);
266 write_byte_direct(GET_TRANSPORT(self), FIX2INT(byte));
267 return Qnil;
268}
269
270VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16) {
271 rb_thrift_compact_proto_write_i32(self, i16);
272 return Qnil;
273}
274
275VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32) {
276 CHECK_NIL(i32);
277 write_varint32(GET_TRANSPORT(self), int_to_zig_zag(NUM2INT(i32)));
278 return Qnil;
279}
280
281VALUE rb_thrift_compact_proto_write_i64(VALUE self, VALUE i64) {
282 CHECK_NIL(i64);
283 write_varint64(GET_TRANSPORT(self), ll_to_zig_zag(NUM2LL(i64)));
284 return Qnil;
285}
286
287VALUE rb_thrift_compact_proto_write_double(VALUE self, VALUE dub) {
288 CHECK_NIL(dub);
289 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
290 union {
291 double f;
292 int64_t l;
293 } transfer;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000294 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000295 char buf[8];
296 buf[0] = transfer.l & 0xff;
297 buf[1] = (transfer.l >> 8) & 0xff;
298 buf[2] = (transfer.l >> 16) & 0xff;
299 buf[3] = (transfer.l >> 24) & 0xff;
300 buf[4] = (transfer.l >> 32) & 0xff;
301 buf[5] = (transfer.l >> 40) & 0xff;
302 buf[6] = (transfer.l >> 48) & 0xff;
303 buf[7] = (transfer.l >> 56) & 0xff;
304 WRITE(GET_TRANSPORT(self), buf, 8);
305 return Qnil;
306}
307
308VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
Jake Farrellb5a18a12012-10-09 01:10:43 +0000309 str = convert_to_utf8_byte_buffer(str);
Roger Meier19dbbef2012-12-27 01:24:20 +0100310 rb_thrift_compact_proto_write_binary(self, str);
311 return Qnil;
312}
313
314VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf) {
315 buf = force_binary_encoding(buf);
316 VALUE transport = GET_TRANSPORT(self);
317 write_varint32(transport, RSTRING_LEN(buf));
Roger Meier02405722014-01-12 23:29:11 +0100318 WRITE(transport, StringValuePtr(buf), RSTRING_LEN(buf));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000319 return Qnil;
320}
321
322//---------------------------------------
323// interface reading methods
324//---------------------------------------
325
326#define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
327
328VALUE rb_thrift_compact_proto_read_string(VALUE self);
Roger Meier19dbbef2012-12-27 01:24:20 +0100329VALUE rb_thrift_compact_proto_read_binary(VALUE self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000330VALUE rb_thrift_compact_proto_read_byte(VALUE self);
331VALUE rb_thrift_compact_proto_read_i32(VALUE self);
332VALUE rb_thrift_compact_proto_read_i16(VALUE self);
333
334static int8_t get_ttype(int8_t ctype) {
335 if (ctype == TTYPE_STOP) {
336 return TTYPE_STOP;
337 } else if (ctype == CTYPE_BOOLEAN_TRUE || ctype == CTYPE_BOOLEAN_FALSE) {
338 return TTYPE_BOOL;
339 } else if (ctype == CTYPE_BYTE) {
340 return TTYPE_BYTE;
341 } else if (ctype == CTYPE_I16) {
342 return TTYPE_I16;
343 } else if (ctype == CTYPE_I32) {
344 return TTYPE_I32;
345 } else if (ctype == CTYPE_I64) {
346 return TTYPE_I64;
347 } else if (ctype == CTYPE_DOUBLE) {
348 return TTYPE_DOUBLE;
349 } else if (ctype == CTYPE_BINARY) {
350 return TTYPE_STRING;
351 } else if (ctype == CTYPE_LIST) {
352 return TTYPE_LIST;
353 } else if (ctype == CTYPE_SET) {
354 return TTYPE_SET;
355 } else if (ctype == CTYPE_MAP) {
356 return TTYPE_MAP;
357 } else if (ctype == CTYPE_STRUCT) {
358 return TTYPE_STRUCT;
359 } else {
360 char str[50];
361 sprintf(str, "don't know what type: %d", ctype);
Bryan Duxburyfd58c552009-09-01 22:32:50 +0000362 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000363 return 0;
364 }
365}
366
367static char read_byte_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000368 VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
369 return (char)(FIX2INT(byte));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000370}
371
372static int64_t zig_zag_to_ll(int64_t n) {
373 return (((uint64_t)n) >> 1) ^ -(n & 1);
374}
375
376static int32_t zig_zag_to_int(int32_t n) {
377 return (((uint32_t)n) >> 1) ^ -(n & 1);
378}
379
380static int64_t read_varint64(VALUE self) {
381 int shift = 0;
382 int64_t result = 0;
383 while (true) {
384 int8_t b = read_byte_direct(self);
385 result = result | ((uint64_t)(b & 0x7f) << shift);
386 if ((b & 0x80) != 0x80) {
387 break;
388 }
389 shift += 7;
390 }
391 return result;
392}
393
394static int16_t read_i16(VALUE self) {
395 return zig_zag_to_int((int32_t)read_varint64(self));
396}
397
398static VALUE get_protocol_exception(VALUE code, VALUE message) {
399 VALUE args[2];
400 args[0] = code;
401 args[1] = message;
402 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
403}
404
405VALUE rb_thrift_compact_proto_read_message_end(VALUE self) {
406 return Qnil;
407}
408
409VALUE rb_thrift_compact_proto_read_struct_begin(VALUE self) {
410 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
411 return Qnil;
412}
413
414VALUE rb_thrift_compact_proto_read_struct_end(VALUE self) {
415 rb_ary_pop(rb_ivar_get(self, last_field_id));
416 return Qnil;
417}
418
419VALUE rb_thrift_compact_proto_read_field_end(VALUE self) {
420 return Qnil;
421}
422
423VALUE rb_thrift_compact_proto_read_map_end(VALUE self) {
424 return Qnil;
425}
426
427VALUE rb_thrift_compact_proto_read_list_end(VALUE self) {
428 return Qnil;
429}
430
431VALUE rb_thrift_compact_proto_read_set_end(VALUE self) {
432 return Qnil;
433}
434
435VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
436 int8_t protocol_id = read_byte_direct(self);
437 if (protocol_id != PROTOCOL_ID) {
438 char buf[100];
439 int len = sprintf(buf, "Expected protocol id %d but got %d", PROTOCOL_ID, protocol_id);
440 buf[len] = 0;
441 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
442 }
443
444 int8_t version_and_type = read_byte_direct(self);
445 int8_t version = version_and_type & VERSION_MASK;
446 if (version != VERSION) {
447 char buf[100];
448 int len = sprintf(buf, "Expected version id %d but got %d", version, VERSION);
449 buf[len] = 0;
450 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
451 }
452
453 int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03;
454 int32_t seqid = read_varint64(self);
455 VALUE messageName = rb_thrift_compact_proto_read_string(self);
456 return rb_ary_new3(3, messageName, INT2FIX(type), INT2NUM(seqid));
457}
458
459VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
460 int8_t type = read_byte_direct(self);
461 // if it's a stop, then we can return immediately, as the struct is over.
462 if ((type & 0x0f) == TTYPE_STOP) {
463 return rb_ary_new3(3, Qnil, INT2FIX(0), INT2FIX(0));
464 } else {
465 int field_id = 0;
466
467 // mask off the 4 MSB of the type header. it could contain a field id delta.
468 uint8_t modifier = ((type & 0xf0) >> 4);
469
470 if (modifier == 0) {
471 // not a delta. look ahead for the zigzag varint field id.
Bryan Duxbury09d13c22010-08-11 18:37:25 +0000472 (void) LAST_ID(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000473 field_id = read_i16(self);
474 } else {
475 // has a delta. add the delta to the last read field id.
476 field_id = LAST_ID(self) + modifier;
477 }
478
479 // if this happens to be a boolean field, the value is encoded in the type
480 if (is_bool_type(type)) {
481 // save the boolean value in a special instance variable.
482 rb_ivar_set(self, bool_value_id, (type & 0x0f) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse);
483 }
484
485 // push the new field onto the field stack so we can keep the deltas going.
486 SET_LAST_ID(self, INT2FIX(field_id));
487 return rb_ary_new3(3, Qnil, INT2FIX(get_ttype(type & 0x0f)), INT2FIX(field_id));
488 }
489}
490
491VALUE rb_thrift_compact_proto_read_map_begin(VALUE self) {
492 int32_t size = read_varint64(self);
493 uint8_t key_and_value_type = size == 0 ? 0 : read_byte_direct(self);
494 return rb_ary_new3(3, INT2FIX(get_ttype(key_and_value_type >> 4)), INT2FIX(get_ttype(key_and_value_type & 0xf)), INT2FIX(size));
495}
496
497VALUE rb_thrift_compact_proto_read_list_begin(VALUE self) {
498 uint8_t size_and_type = read_byte_direct(self);
499 int32_t size = (size_and_type >> 4) & 0x0f;
500 if (size == 15) {
501 size = read_varint64(self);
502 }
503 uint8_t type = get_ttype(size_and_type & 0x0f);
504 return rb_ary_new3(2, INT2FIX(type), INT2FIX(size));
505}
506
507VALUE rb_thrift_compact_proto_read_set_begin(VALUE self) {
508 return rb_thrift_compact_proto_read_list_begin(self);
509}
510
511VALUE rb_thrift_compact_proto_read_bool(VALUE self) {
512 VALUE bool_value = rb_ivar_get(self, bool_value_id);
513 if (NIL_P(bool_value)) {
514 return read_byte_direct(self) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse;
515 } else {
516 rb_ivar_set(self, bool_value_id, Qnil);
517 return bool_value;
518 }
519}
520
521VALUE rb_thrift_compact_proto_read_byte(VALUE self) {
522 return INT2FIX(read_byte_direct(self));
523}
524
525VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
526 return INT2FIX(read_i16(self));
527}
528
529VALUE rb_thrift_compact_proto_read_i32(VALUE self) {
530 return INT2NUM(zig_zag_to_int(read_varint64(self)));
531}
532
533VALUE rb_thrift_compact_proto_read_i64(VALUE self) {
534 return LL2NUM(zig_zag_to_ll(read_varint64(self)));
535}
536
537VALUE rb_thrift_compact_proto_read_double(VALUE self) {
538 union {
539 double f;
540 int64_t l;
541 } transfer;
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000542 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
543 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
544 uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[0]))
545 | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 8)
546 | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 16)
547 | (((uint8_t)(RSTRING_PTR(rbuf)[3])) << 24);
548 uint64_t hi = (((uint8_t)(RSTRING_PTR(rbuf)[4])))
549 | (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 8)
550 | (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 16)
551 | (((uint8_t)(RSTRING_PTR(rbuf)[7])) << 24);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000552 transfer.l = (hi << 32) | lo;
553
554 return rb_float_new(transfer.f);
555}
556
557VALUE rb_thrift_compact_proto_read_string(VALUE self) {
Roger Meier19dbbef2012-12-27 01:24:20 +0100558 VALUE buffer = rb_thrift_compact_proto_read_binary(self);
Jake Farrellb5a18a12012-10-09 01:10:43 +0000559 return convert_to_string(buffer);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000560}
561
Roger Meier19dbbef2012-12-27 01:24:20 +0100562VALUE rb_thrift_compact_proto_read_binary(VALUE self) {
563 int64_t size = read_varint64(self);
564 return READ(self, size);
565}
566
Bryan Duxburyd815c212009-03-19 18:57:43 +0000567static void Init_constants() {
568 thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
Roger Meier19dbbef2012-12-27 01:24:20 +0100569
Bryan Duxburyd815c212009-03-19 18:57:43 +0000570 VERSION = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
571 VERSION_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
572 TYPE_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
573 TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
574 PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
Roger Meier19dbbef2012-12-27 01:24:20 +0100575
Bryan Duxburyd815c212009-03-19 18:57:43 +0000576 last_field_id = rb_intern("@last_field");
577 boolean_field_id = rb_intern("@boolean_field");
578 bool_value_id = rb_intern("@bool_value");
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000579 rbuf_ivar_id = rb_intern("@rbuf");
Bryan Duxburyd815c212009-03-19 18:57:43 +0000580}
581
582static void Init_rb_methods() {
583 rb_define_method(thrift_compact_protocol_class, "native?", rb_thrift_compact_proto_native_qmark, 0);
584
585 rb_define_method(thrift_compact_protocol_class, "write_message_begin", rb_thrift_compact_proto_write_message_begin, 3);
586 rb_define_method(thrift_compact_protocol_class, "write_field_begin", rb_thrift_compact_proto_write_field_begin, 3);
587 rb_define_method(thrift_compact_protocol_class, "write_field_stop", rb_thrift_compact_proto_write_field_stop, 0);
588 rb_define_method(thrift_compact_protocol_class, "write_map_begin", rb_thrift_compact_proto_write_map_begin, 3);
589 rb_define_method(thrift_compact_protocol_class, "write_list_begin", rb_thrift_compact_proto_write_list_begin, 2);
590 rb_define_method(thrift_compact_protocol_class, "write_set_begin", rb_thrift_compact_proto_write_set_begin, 2);
591 rb_define_method(thrift_compact_protocol_class, "write_byte", rb_thrift_compact_proto_write_byte, 1);
592 rb_define_method(thrift_compact_protocol_class, "write_bool", rb_thrift_compact_proto_write_bool, 1);
593 rb_define_method(thrift_compact_protocol_class, "write_i16", rb_thrift_compact_proto_write_i16, 1);
594 rb_define_method(thrift_compact_protocol_class, "write_i32", rb_thrift_compact_proto_write_i32, 1);
595 rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
596 rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
597 rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
Roger Meier19dbbef2012-12-27 01:24:20 +0100598 rb_define_method(thrift_compact_protocol_class, "write_binary", rb_thrift_compact_proto_write_binary, 1);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000599
600 rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
601 rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
602 rb_define_method(thrift_compact_protocol_class, "write_struct_end", rb_thrift_compact_proto_write_struct_end, 0);
603 rb_define_method(thrift_compact_protocol_class, "write_field_end", rb_thrift_compact_proto_write_field_end, 0);
604 rb_define_method(thrift_compact_protocol_class, "write_map_end", rb_thrift_compact_proto_write_map_end, 0);
605 rb_define_method(thrift_compact_protocol_class, "write_list_end", rb_thrift_compact_proto_write_list_end, 0);
606 rb_define_method(thrift_compact_protocol_class, "write_set_end", rb_thrift_compact_proto_write_set_end, 0);
607
608
609 rb_define_method(thrift_compact_protocol_class, "read_message_begin", rb_thrift_compact_proto_read_message_begin, 0);
610 rb_define_method(thrift_compact_protocol_class, "read_field_begin", rb_thrift_compact_proto_read_field_begin, 0);
611 rb_define_method(thrift_compact_protocol_class, "read_map_begin", rb_thrift_compact_proto_read_map_begin, 0);
612 rb_define_method(thrift_compact_protocol_class, "read_list_begin", rb_thrift_compact_proto_read_list_begin, 0);
613 rb_define_method(thrift_compact_protocol_class, "read_set_begin", rb_thrift_compact_proto_read_set_begin, 0);
614 rb_define_method(thrift_compact_protocol_class, "read_byte", rb_thrift_compact_proto_read_byte, 0);
615 rb_define_method(thrift_compact_protocol_class, "read_bool", rb_thrift_compact_proto_read_bool, 0);
616 rb_define_method(thrift_compact_protocol_class, "read_i16", rb_thrift_compact_proto_read_i16, 0);
617 rb_define_method(thrift_compact_protocol_class, "read_i32", rb_thrift_compact_proto_read_i32, 0);
618 rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
619 rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
620 rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
Roger Meier19dbbef2012-12-27 01:24:20 +0100621 rb_define_method(thrift_compact_protocol_class, "read_binary", rb_thrift_compact_proto_read_binary, 0);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000622
623 rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
624 rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);
625 rb_define_method(thrift_compact_protocol_class, "read_struct_end", rb_thrift_compact_proto_read_struct_end, 0);
626 rb_define_method(thrift_compact_protocol_class, "read_field_end", rb_thrift_compact_proto_read_field_end, 0);
627 rb_define_method(thrift_compact_protocol_class, "read_map_end", rb_thrift_compact_proto_read_map_end, 0);
628 rb_define_method(thrift_compact_protocol_class, "read_list_end", rb_thrift_compact_proto_read_list_end, 0);
629 rb_define_method(thrift_compact_protocol_class, "read_set_end", rb_thrift_compact_proto_read_set_end, 0);
630}
631
Bryan Duxburyd815c212009-03-19 18:57:43 +0000632void Init_compact_protocol() {
633 Init_constants();
634 Init_rb_methods();
Bryan Duxburyd815c212009-03-19 18:57:43 +0000635}