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