| Kevin Clark | 916f353 | 2009-03-20 04:21:39 +0000 | [diff] [blame] | 1 | /** |
| 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 Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 20 | #include <ruby.h> |
| 21 | #include <stdbool.h> |
| Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 22 | #include <stdint.h> |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 23 | #include <string.h> |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 24 | #include <constants.h> |
| 25 | #include <struct.h> |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 26 | #include <macros.h> |
| 27 | #include <bytes.h> |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 28 | #include <protocol.h> |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 29 | |
| 30 | VALUE rb_thrift_binary_proto_native_qmark(VALUE self) { |
| 31 | return Qtrue; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
| 36 | static int VERSION_1; |
| 37 | static int VERSION_MASK; |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 38 | static int TYPE_MASK; |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 39 | static ID rbuf_ivar_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 40 | |
| 41 | static void write_byte_direct(VALUE trans, int8_t b) { |
| 42 | WRITE(trans, (char*)&b, 1); |
| 43 | } |
| 44 | |
| 45 | static void write_i16_direct(VALUE trans, int16_t value) { |
| 46 | char data[2]; |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 47 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 48 | data[1] = value; |
| 49 | data[0] = (value >> 8); |
| 50 | |
| 51 | WRITE(trans, data, 2); |
| 52 | } |
| 53 | |
| 54 | static void write_i32_direct(VALUE trans, int32_t value) { |
| 55 | char data[4]; |
| 56 | |
| 57 | data[3] = value; |
| 58 | data[2] = (value >> 8); |
| 59 | data[1] = (value >> 16); |
| 60 | data[0] = (value >> 24); |
| 61 | |
| 62 | WRITE(trans, data, 4); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static void write_i64_direct(VALUE trans, int64_t value) { |
| 67 | char data[8]; |
| 68 | |
| 69 | data[7] = value; |
| 70 | data[6] = (value >> 8); |
| 71 | data[5] = (value >> 16); |
| 72 | data[4] = (value >> 24); |
| 73 | data[3] = (value >> 32); |
| 74 | data[2] = (value >> 40); |
| 75 | data[1] = (value >> 48); |
| 76 | data[0] = (value >> 56); |
| 77 | |
| 78 | WRITE(trans, data, 8); |
| 79 | } |
| 80 | |
| 81 | static void write_string_direct(VALUE trans, VALUE str) { |
| Bryan Duxbury | 3647fc6 | 2009-09-02 20:05:07 +0000 | [diff] [blame] | 82 | if (TYPE(str) != T_STRING) { |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 83 | rb_raise(rb_eStandardError, "Value should be a string"); |
| Bryan Duxbury | 3647fc6 | 2009-09-02 20:05:07 +0000 | [diff] [blame] | 84 | } |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 85 | str = convert_to_utf8_byte_buffer(str); |
| Jean Boussier | 1e84341 | 2021-10-13 12:36:28 +0200 | [diff] [blame] | 86 | write_i32_direct(trans, (int32_t)RSTRING_LEN(str)); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 87 | rb_funcall(trans, write_method_id, 1, str); |
| 88 | } |
| 89 | |
| 90 | //-------------------------------- |
| 91 | // interface writing methods |
| 92 | //-------------------------------- |
| 93 | |
| 94 | VALUE rb_thrift_binary_proto_write_message_end(VALUE self) { |
| 95 | return Qnil; |
| 96 | } |
| 97 | |
| 98 | VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) { |
| 99 | return Qnil; |
| 100 | } |
| 101 | |
| 102 | VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) { |
| 103 | return Qnil; |
| 104 | } |
| 105 | |
| 106 | VALUE rb_thrift_binary_proto_write_field_end(VALUE self) { |
| 107 | return Qnil; |
| 108 | } |
| 109 | |
| 110 | VALUE rb_thrift_binary_proto_write_map_end(VALUE self) { |
| 111 | return Qnil; |
| 112 | } |
| 113 | |
| 114 | VALUE rb_thrift_binary_proto_write_list_end(VALUE self) { |
| 115 | return Qnil; |
| 116 | } |
| 117 | |
| 118 | VALUE rb_thrift_binary_proto_write_set_end(VALUE self) { |
| 119 | return Qnil; |
| 120 | } |
| 121 | |
| 122 | VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) { |
| 123 | VALUE trans = GET_TRANSPORT(self); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 124 | VALUE strict_write = GET_STRICT_WRITE(self); |
| 125 | |
| 126 | if (strict_write == Qtrue) { |
| 127 | write_i32_direct(trans, VERSION_1 | FIX2INT(type)); |
| 128 | write_string_direct(trans, name); |
| 129 | write_i32_direct(trans, FIX2INT(seqid)); |
| 130 | } else { |
| 131 | write_string_direct(trans, name); |
| Bryan Duxbury | d40731e | 2009-03-20 02:21:05 +0000 | [diff] [blame] | 132 | write_byte_direct(trans, FIX2INT(type)); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 133 | write_i32_direct(trans, FIX2INT(seqid)); |
| 134 | } |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 135 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 136 | return Qnil; |
| 137 | } |
| 138 | |
| 139 | VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) { |
| 140 | VALUE trans = GET_TRANSPORT(self); |
| 141 | write_byte_direct(trans, FIX2INT(type)); |
| 142 | write_i16_direct(trans, FIX2INT(id)); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 143 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 144 | return Qnil; |
| 145 | } |
| 146 | |
| 147 | VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) { |
| 148 | write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP); |
| 149 | return Qnil; |
| 150 | } |
| 151 | |
| 152 | VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) { |
| 153 | VALUE trans = GET_TRANSPORT(self); |
| 154 | write_byte_direct(trans, FIX2INT(ktype)); |
| 155 | write_byte_direct(trans, FIX2INT(vtype)); |
| 156 | write_i32_direct(trans, FIX2INT(size)); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 157 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 158 | return Qnil; |
| 159 | } |
| 160 | |
| 161 | VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) { |
| 162 | VALUE trans = GET_TRANSPORT(self); |
| 163 | write_byte_direct(trans, FIX2INT(etype)); |
| 164 | write_i32_direct(trans, FIX2INT(size)); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 165 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 166 | return Qnil; |
| 167 | } |
| 168 | |
| 169 | VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) { |
| 170 | rb_thrift_binary_proto_write_list_begin(self, etype, size); |
| 171 | return Qnil; |
| 172 | } |
| 173 | |
| 174 | VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) { |
| 175 | write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0); |
| 176 | return Qnil; |
| 177 | } |
| 178 | |
| 179 | VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) { |
| 180 | CHECK_NIL(byte); |
| 181 | write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte)); |
| 182 | return Qnil; |
| 183 | } |
| 184 | |
| 185 | VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) { |
| 186 | CHECK_NIL(i16); |
| 187 | write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16)); |
| 188 | return Qnil; |
| 189 | } |
| 190 | |
| 191 | VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) { |
| 192 | CHECK_NIL(i32); |
| 193 | write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32)); |
| 194 | return Qnil; |
| 195 | } |
| 196 | |
| 197 | VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) { |
| 198 | CHECK_NIL(i64); |
| 199 | write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64)); |
| 200 | return Qnil; |
| 201 | } |
| 202 | |
| 203 | VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) { |
| 204 | CHECK_NIL(dub); |
| 205 | // Unfortunately, bitwise_cast doesn't work in C. Bad C! |
| 206 | union { |
| 207 | double f; |
| 208 | int64_t t; |
| 209 | } transfer; |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 210 | transfer.f = RFLOAT_VALUE(rb_Float(dub)); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 211 | write_i64_direct(GET_TRANSPORT(self), transfer.t); |
| 212 | |
| 213 | return Qnil; |
| 214 | } |
| 215 | |
| 216 | VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) { |
| 217 | CHECK_NIL(str); |
| 218 | VALUE trans = GET_TRANSPORT(self); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 219 | write_string_direct(trans, str); |
| 220 | return Qnil; |
| 221 | } |
| 222 | |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 223 | VALUE rb_thrift_binary_proto_write_binary(VALUE self, VALUE buf) { |
| 224 | CHECK_NIL(buf); |
| 225 | VALUE trans = GET_TRANSPORT(self); |
| 226 | buf = force_binary_encoding(buf); |
| Jean Boussier | 1e84341 | 2021-10-13 12:36:28 +0200 | [diff] [blame] | 227 | write_i32_direct(trans, (int32_t)RSTRING_LEN(buf)); |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 228 | rb_funcall(trans, write_method_id, 1, buf); |
| 229 | return Qnil; |
| 230 | } |
| 231 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 232 | VALUE rb_thrift_binary_proto_write_uuid(VALUE self, VALUE uuid) { |
| 233 | if (NIL_P(uuid) || TYPE(uuid) != T_STRING) { |
| 234 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("UUID must be a string"))); |
| 235 | } |
| 236 | |
| 237 | VALUE trans = GET_TRANSPORT(self); |
| 238 | char bytes[16]; |
| 239 | const char* str = RSTRING_PTR(uuid); |
| 240 | long len = RSTRING_LEN(uuid); |
| 241 | |
| 242 | // Parse UUID string (format: "550e8400-e29b-41d4-a716-446655440000") |
| 243 | // Expected length: 36 characters (32 hex + 4 hyphens) |
| 244 | if (len != 36 || str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-') { |
| 245 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("Invalid UUID format"))); |
| 246 | } |
| 247 | |
| 248 | // Parse hex string to bytes using direct conversion, skipping hyphens |
| 249 | int byte_idx = 0; |
| 250 | for (int i = 0; i < len && byte_idx < 16; i++) { |
| 251 | if (str[i] == '-') continue; |
| 252 | if (i + 1 >= len || str[i + 1] == '-') break; |
| 253 | |
| 254 | // Convert two hex characters to one byte |
| 255 | int high = hex_char_to_int(str[i]); |
| 256 | int low = hex_char_to_int(str[i + 1]); |
| 257 | |
| 258 | if (high < 0 || low < 0) break; |
| 259 | |
| 260 | bytes[byte_idx++] = (unsigned char)((high << 4) | low); |
| 261 | i++; // skip next char since we processed two |
| 262 | } |
| 263 | |
| 264 | if (byte_idx != 16) { |
| 265 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("Invalid UUID format"))); |
| 266 | } |
| 267 | |
| 268 | WRITE(trans, bytes, 16); |
| 269 | return Qnil; |
| 270 | } |
| 271 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 272 | //--------------------------------------- |
| 273 | // interface reading methods |
| 274 | //--------------------------------------- |
| 275 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 276 | VALUE rb_thrift_binary_proto_read_string(VALUE self); |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 277 | VALUE rb_thrift_binary_proto_read_binary(VALUE self); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 278 | VALUE rb_thrift_binary_proto_read_byte(VALUE self); |
| 279 | VALUE rb_thrift_binary_proto_read_i32(VALUE self); |
| 280 | VALUE rb_thrift_binary_proto_read_i16(VALUE self); |
| 281 | |
| 282 | static char read_byte_direct(VALUE self) { |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 283 | VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0); |
| 284 | return (char)(FIX2INT(byte)); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int16_t read_i16_direct(VALUE self) { |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 288 | VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id); |
| 289 | rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(2)); |
| 290 | return (int16_t)(((uint8_t)(RSTRING_PTR(rbuf)[1])) | ((uint16_t)((RSTRING_PTR(rbuf)[0]) << 8))); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int32_t read_i32_direct(VALUE self) { |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 294 | VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id); |
| 295 | rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(4)); |
| 296 | return ((uint8_t)(RSTRING_PTR(rbuf)[3])) | |
| 297 | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) | |
| 298 | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) | |
| 299 | (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static int64_t read_i64_direct(VALUE self) { |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 303 | VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id); |
| 304 | rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8)); |
| 305 | uint64_t hi = ((uint8_t)(RSTRING_PTR(rbuf)[3])) | |
| 306 | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) | |
| 307 | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) | |
| 308 | (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24); |
| 309 | uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[7])) | |
| 310 | (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 8) | |
| 311 | (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 16) | |
| 312 | (((uint8_t)(RSTRING_PTR(rbuf)[4])) << 24); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 313 | return (hi << 32) | lo; |
| 314 | } |
| 315 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 316 | VALUE rb_thrift_binary_proto_read_message_end(VALUE self) { |
| 317 | return Qnil; |
| 318 | } |
| 319 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 320 | VALUE rb_thrift_binary_proto_read_struct_begin(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 321 | return Qnil; |
| 322 | } |
| 323 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 324 | VALUE rb_thrift_binary_proto_read_struct_end(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 325 | return Qnil; |
| 326 | } |
| 327 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 328 | VALUE rb_thrift_binary_proto_read_field_end(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 329 | return Qnil; |
| 330 | } |
| 331 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 332 | VALUE rb_thrift_binary_proto_read_map_end(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 333 | return Qnil; |
| 334 | } |
| 335 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 336 | VALUE rb_thrift_binary_proto_read_list_end(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 337 | return Qnil; |
| 338 | } |
| 339 | |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 340 | VALUE rb_thrift_binary_proto_read_set_end(VALUE self) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 341 | return Qnil; |
| 342 | } |
| 343 | |
| 344 | VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) { |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 345 | VALUE strict_read = GET_STRICT_READ(self); |
| 346 | VALUE name, seqid; |
| 347 | int type; |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 348 | |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 349 | int version = read_i32_direct(self); |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 350 | |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 351 | if (version < 0) { |
| 352 | if ((version & VERSION_MASK) != VERSION_1) { |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 353 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_BAD_VERSION), rb_str_new2("Missing version identifier"))); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 354 | } |
| 355 | type = version & TYPE_MASK; |
| 356 | name = rb_thrift_binary_proto_read_string(self); |
| 357 | seqid = rb_thrift_binary_proto_read_i32(self); |
| 358 | } else { |
| 359 | if (strict_read == Qtrue) { |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 360 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_BAD_VERSION), rb_str_new2("No version identifier, old protocol client?"))); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 361 | } |
| 362 | name = READ(self, version); |
| Bryan Duxbury | ac002d3 | 2009-03-31 23:48:36 +0000 | [diff] [blame] | 363 | type = read_byte_direct(self); |
| Kevin Clark | ead3382 | 2009-02-04 22:43:59 +0000 | [diff] [blame] | 364 | seqid = rb_thrift_binary_proto_read_i32(self); |
| 365 | } |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 366 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 367 | return rb_ary_new3(3, name, INT2FIX(type), seqid); |
| 368 | } |
| 369 | |
| 370 | VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) { |
| 371 | int type = read_byte_direct(self); |
| 372 | if (type == TTYPE_STOP) { |
| 373 | return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0)); |
| 374 | } else { |
| 375 | VALUE id = rb_thrift_binary_proto_read_i16(self); |
| 376 | return rb_ary_new3(3, Qnil, INT2FIX(type), id); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) { |
| 381 | VALUE ktype = rb_thrift_binary_proto_read_byte(self); |
| 382 | VALUE vtype = rb_thrift_binary_proto_read_byte(self); |
| 383 | VALUE size = rb_thrift_binary_proto_read_i32(self); |
| 384 | return rb_ary_new3(3, ktype, vtype, size); |
| 385 | } |
| 386 | |
| 387 | VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) { |
| 388 | VALUE etype = rb_thrift_binary_proto_read_byte(self); |
| 389 | VALUE size = rb_thrift_binary_proto_read_i32(self); |
| 390 | return rb_ary_new3(2, etype, size); |
| 391 | } |
| 392 | |
| 393 | VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) { |
| 394 | return rb_thrift_binary_proto_read_list_begin(self); |
| 395 | } |
| 396 | |
| 397 | VALUE rb_thrift_binary_proto_read_bool(VALUE self) { |
| 398 | char byte = read_byte_direct(self); |
| Bryan Duxbury | 5b8b484 | 2009-04-01 20:10:15 +0000 | [diff] [blame] | 399 | return byte != 0 ? Qtrue : Qfalse; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | VALUE rb_thrift_binary_proto_read_byte(VALUE self) { |
| 403 | return INT2FIX(read_byte_direct(self)); |
| 404 | } |
| 405 | |
| 406 | VALUE rb_thrift_binary_proto_read_i16(VALUE self) { |
| 407 | return INT2FIX(read_i16_direct(self)); |
| 408 | } |
| 409 | |
| 410 | VALUE rb_thrift_binary_proto_read_i32(VALUE self) { |
| 411 | return INT2NUM(read_i32_direct(self)); |
| 412 | } |
| 413 | |
| 414 | VALUE rb_thrift_binary_proto_read_i64(VALUE self) { |
| 415 | return LL2NUM(read_i64_direct(self)); |
| 416 | } |
| 417 | |
| 418 | VALUE rb_thrift_binary_proto_read_double(VALUE self) { |
| 419 | union { |
| 420 | double f; |
| 421 | int64_t t; |
| 422 | } transfer; |
| 423 | transfer.t = read_i64_direct(self); |
| 424 | return rb_float_new(transfer.f); |
| 425 | } |
| 426 | |
| 427 | VALUE rb_thrift_binary_proto_read_string(VALUE self) { |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 428 | VALUE buffer = rb_thrift_binary_proto_read_binary(self); |
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 429 | return convert_to_string(buffer); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 432 | VALUE rb_thrift_binary_proto_read_binary(VALUE self) { |
| 433 | int size = read_i32_direct(self); |
| 434 | return READ(self, size); |
| 435 | } |
| 436 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 437 | VALUE rb_thrift_binary_proto_read_uuid(VALUE self) { |
| 438 | VALUE data = READ(self, 16); |
| 439 | const unsigned char* bytes = (const unsigned char*)RSTRING_PTR(data); |
| 440 | |
| 441 | // Format as UUID string: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" |
| 442 | char uuid_str[37]; |
| 443 | char* p = uuid_str; |
| 444 | |
| 445 | for (int i = 0; i < 16; i++) { |
| 446 | *p++ = int_to_hex_char((bytes[i] >> 4) & 0x0F); |
| 447 | *p++ = int_to_hex_char(bytes[i] & 0x0F); |
| 448 | if (i == 3 || i == 5 || i == 7 || i == 9) { |
| 449 | *p++ = '-'; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | *p = '\0'; |
| 454 | |
| 455 | return rb_str_new(uuid_str, 36); |
| 456 | } |
| 457 | |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 458 | void Init_binary_protocol_accelerated(void) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 459 | VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol")); |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 460 | |
| Jean Boussier | 1e84341 | 2021-10-13 12:36:28 +0200 | [diff] [blame] | 461 | VERSION_1 = (int)rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1"))); |
| 462 | VERSION_MASK = (int)rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK"))); |
| 463 | TYPE_MASK = (int)rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK"))); |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 464 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 465 | VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class); |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 466 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 467 | rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0); |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 468 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 469 | rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3); |
| 470 | rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3); |
| 471 | rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0); |
| 472 | rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3); |
| 473 | rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2); |
| 474 | rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2); |
| 475 | rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1); |
| 476 | rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1); |
| 477 | rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1); |
| 478 | rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1); |
| 479 | rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1); |
| 480 | rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1); |
| 481 | rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1); |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 482 | rb_define_method(bpa_class, "write_binary", rb_thrift_binary_proto_write_binary, 1); |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 483 | rb_define_method(bpa_class, "write_uuid", rb_thrift_binary_proto_write_uuid, 1); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 484 | // unused methods |
| 485 | rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0); |
| 486 | rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1); |
| 487 | rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0); |
| 488 | rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0); |
| 489 | rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0); |
| 490 | rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0); |
| 491 | rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 492 | |
| 493 | rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0); |
| 494 | rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0); |
| 495 | rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0); |
| 496 | rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0); |
| 497 | rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0); |
| 498 | rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0); |
| 499 | rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0); |
| 500 | rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0); |
| 501 | rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0); |
| 502 | rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0); |
| 503 | rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0); |
| 504 | rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0); |
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 505 | rb_define_method(bpa_class, "read_binary", rb_thrift_binary_proto_read_binary, 0); |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 506 | rb_define_method(bpa_class, "read_uuid", rb_thrift_binary_proto_read_uuid, 0); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 507 | // unused methods |
| 508 | rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0); |
| Roger Meier | a2d12b6 | 2015-03-24 21:15:06 +0100 | [diff] [blame] | 509 | rb_define_method(bpa_class, "read_struct_begin", rb_thrift_binary_proto_read_struct_begin, 0); |
| 510 | rb_define_method(bpa_class, "read_struct_end", rb_thrift_binary_proto_read_struct_end, 0); |
| 511 | rb_define_method(bpa_class, "read_field_end", rb_thrift_binary_proto_read_field_end, 0); |
| 512 | rb_define_method(bpa_class, "read_map_end", rb_thrift_binary_proto_read_map_end, 0); |
| 513 | rb_define_method(bpa_class, "read_list_end", rb_thrift_binary_proto_read_list_end, 0); |
| 514 | rb_define_method(bpa_class, "read_set_end", rb_thrift_binary_proto_read_set_end, 0); |
| Bryan Duxbury | ad0ad82 | 2011-06-28 18:46:03 +0000 | [diff] [blame] | 515 | |
| 516 | rbuf_ivar_id = rb_intern("@rbuf"); |
| Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 517 | } |