blob: 8c864e815f02a507b5f243c77d8b4cf5caff4b24 [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>
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050023#include <string.h>
Jake Farrellb5a18a12012-10-09 01:10:43 +000024#include <constants.h>
25#include <struct.h>
26#include <macros.h>
27#include <bytes.h>
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050028#include <protocol.h>
Bryan Duxburyd815c212009-03-19 18:57:43 +000029
30#define LAST_ID(obj) FIX2INT(rb_ary_pop(rb_ivar_get(obj, last_field_id)))
31#define SET_LAST_ID(obj, val) rb_ary_push(rb_ivar_get(obj, last_field_id), val)
32
33VALUE rb_thrift_compact_proto_native_qmark(VALUE self) {
34 return Qtrue;
35}
36
37static ID last_field_id;
38static ID boolean_field_id;
39static ID bool_value_id;
Bryan Duxburyad0ad822011-06-28 18:46:03 +000040static ID rbuf_ivar_id;
Bryan Duxburyd815c212009-03-19 18:57:43 +000041
42static int VERSION;
43static int VERSION_MASK;
44static int TYPE_MASK;
Jens Geyera86886e2014-09-17 22:25:48 +020045static int TYPE_BITS;
Bryan Duxburyd815c212009-03-19 18:57:43 +000046static int TYPE_SHIFT_AMOUNT;
47static int PROTOCOL_ID;
48
49static VALUE thrift_compact_protocol_class;
50
51static int CTYPE_BOOLEAN_TRUE = 0x01;
52static int CTYPE_BOOLEAN_FALSE = 0x02;
53static int CTYPE_BYTE = 0x03;
54static int CTYPE_I16 = 0x04;
55static int CTYPE_I32 = 0x05;
56static int CTYPE_I64 = 0x06;
57static int CTYPE_DOUBLE = 0x07;
58static int CTYPE_BINARY = 0x08;
59static int CTYPE_LIST = 0x09;
60static int CTYPE_SET = 0x0A;
61static int CTYPE_MAP = 0x0B;
62static int CTYPE_STRUCT = 0x0C;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050063static int CTYPE_UUID = 0x0D;
Bryan Duxburyd815c212009-03-19 18:57:43 +000064
65VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16);
66
67// TODO: implement this
68static int get_compact_type(VALUE type_value) {
69 int type = FIX2INT(type_value);
70 if (type == TTYPE_BOOL) {
71 return CTYPE_BOOLEAN_TRUE;
72 } else if (type == TTYPE_BYTE) {
73 return CTYPE_BYTE;
74 } else if (type == TTYPE_I16) {
75 return CTYPE_I16;
76 } else if (type == TTYPE_I32) {
77 return CTYPE_I32;
78 } else if (type == TTYPE_I64) {
79 return CTYPE_I64;
80 } else if (type == TTYPE_DOUBLE) {
81 return CTYPE_DOUBLE;
82 } else if (type == TTYPE_STRING) {
83 return CTYPE_BINARY;
84 } else if (type == TTYPE_LIST) {
85 return CTYPE_LIST;
86 } else if (type == TTYPE_SET) {
87 return CTYPE_SET;
88 } else if (type == TTYPE_MAP) {
89 return CTYPE_MAP;
90 } else if (type == TTYPE_STRUCT) {
91 return CTYPE_STRUCT;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050092 } else if (type == TTYPE_UUID) {
93 return CTYPE_UUID;
Bryan Duxburyd815c212009-03-19 18:57:43 +000094 } else {
95 char str[50];
96 sprintf(str, "don't know what type: %d", type);
Bryan Duxburyfd58c552009-09-01 22:32:50 +000097 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +000098 return 0;
99 }
100}
101
102static void write_byte_direct(VALUE transport, int8_t b) {
103 WRITE(transport, (char*)&b, 1);
104}
105
106static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, VALUE type_override) {
107 int id = FIX2INT(id_value);
108 int last_id = LAST_ID(self);
109 VALUE transport = GET_TRANSPORT(self);
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500110
Bryan Duxburyd815c212009-03-19 18:57:43 +0000111 // if there's a type override, use that.
112 int8_t type_to_write = RTEST(type_override) ? FIX2INT(type_override) : get_compact_type(type);
113 // check if we can use delta encoding for the field id
114 int diff = id - last_id;
115 if (diff > 0 && diff <= 15) {
116 // write them together
117 write_byte_direct(transport, diff << 4 | (type_to_write & 0x0f));
118 } else {
119 // write them separate
120 write_byte_direct(transport, type_to_write & 0x0f);
121 rb_thrift_compact_proto_write_i16(self, id_value);
122 }
123
124 SET_LAST_ID(self, id_value);
125}
126
127static int32_t int_to_zig_zag(int32_t n) {
128 return (n << 1) ^ (n >> 31);
129}
130
131static uint64_t ll_to_zig_zag(int64_t n) {
132 return (n << 1) ^ (n >> 63);
133}
134
135static void write_varint32(VALUE transport, uint32_t n) {
136 while (true) {
137 if ((n & ~0x7F) == 0) {
138 write_byte_direct(transport, n & 0x7f);
139 break;
140 } else {
141 write_byte_direct(transport, (n & 0x7F) | 0x80);
142 n = n >> 7;
143 }
144 }
145}
146
147static void write_varint64(VALUE transport, uint64_t n) {
148 while (true) {
149 if ((n & ~0x7F) == 0) {
150 write_byte_direct(transport, n & 0x7f);
151 break;
152 } else {
153 write_byte_direct(transport, (n & 0x7F) | 0x80);
154 n = n >> 7;
155 }
156 }
157}
158
159static void write_collection_begin(VALUE transport, VALUE elem_type, VALUE size_value) {
160 int size = FIX2INT(size_value);
161 if (size <= 14) {
162 write_byte_direct(transport, size << 4 | get_compact_type(elem_type));
163 } else {
164 write_byte_direct(transport, 0xf0 | get_compact_type(elem_type));
165 write_varint32(transport, size);
166 }
167}
168
169
170//--------------------------------
171// interface writing methods
172//--------------------------------
173
174VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
175VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
Roger Meier19dbbef2012-12-27 01:24:20 +0100176VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf);
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500177VALUE rb_thrift_compact_proto_write_uuid(VALUE self, VALUE uuid);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000178
179VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
180 return Qnil;
181}
182
183VALUE rb_thrift_compact_proto_write_struct_begin(VALUE self, VALUE name) {
184 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
185 return Qnil;
186}
187
188VALUE rb_thrift_compact_proto_write_struct_end(VALUE self) {
189 rb_ary_pop(rb_ivar_get(self, last_field_id));
190 return Qnil;
191}
192
193VALUE rb_thrift_compact_proto_write_field_end(VALUE self) {
194 return Qnil;
195}
196
197VALUE rb_thrift_compact_proto_write_map_end(VALUE self) {
198 return Qnil;
199}
200
201VALUE rb_thrift_compact_proto_write_list_end(VALUE self) {
202 return Qnil;
203}
204
205VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
206 return Qnil;
207}
208
209VALUE rb_thrift_compact_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
210 VALUE transport = GET_TRANSPORT(self);
211 write_byte_direct(transport, PROTOCOL_ID);
212 write_byte_direct(transport, (VERSION & VERSION_MASK) | ((FIX2INT(type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
213 write_varint32(transport, FIX2INT(seqid));
214 rb_thrift_compact_proto_write_string(self, name);
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500215
Bryan Duxburyd815c212009-03-19 18:57:43 +0000216 return Qnil;
217}
218
219VALUE rb_thrift_compact_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
220 if (FIX2INT(type) == TTYPE_BOOL) {
221 // we want to possibly include the value, so we'll wait.
222 rb_ivar_set(self, boolean_field_id, rb_ary_new3(2, type, id));
223 } else {
224 write_field_begin_internal(self, type, id, Qnil);
225 }
226
227 return Qnil;
228}
229
230VALUE rb_thrift_compact_proto_write_field_stop(VALUE self) {
231 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
232 return Qnil;
233}
234
235VALUE rb_thrift_compact_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size_value) {
236 int size = FIX2INT(size_value);
237 VALUE transport = GET_TRANSPORT(self);
238 if (size == 0) {
239 write_byte_direct(transport, 0);
240 } else {
241 write_varint32(transport, size);
242 write_byte_direct(transport, get_compact_type(ktype) << 4 | get_compact_type(vtype));
243 }
244 return Qnil;
245}
246
247VALUE rb_thrift_compact_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
248 write_collection_begin(GET_TRANSPORT(self), etype, size);
249 return Qnil;
250}
251
252VALUE rb_thrift_compact_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
253 write_collection_begin(GET_TRANSPORT(self), etype, size);
254 return Qnil;
255}
256
257VALUE rb_thrift_compact_proto_write_bool(VALUE self, VALUE b) {
258 int8_t type = b == Qtrue ? CTYPE_BOOLEAN_TRUE : CTYPE_BOOLEAN_FALSE;
259 VALUE boolean_field = rb_ivar_get(self, boolean_field_id);
260 if (NIL_P(boolean_field)) {
261 // we're not part of a field, so just write the value.
262 write_byte_direct(GET_TRANSPORT(self), type);
263 } else {
264 // we haven't written the field header yet
265 write_field_begin_internal(self, rb_ary_entry(boolean_field, 0), rb_ary_entry(boolean_field, 1), INT2FIX(type));
266 rb_ivar_set(self, boolean_field_id, Qnil);
267 }
268 return Qnil;
269}
270
271VALUE rb_thrift_compact_proto_write_byte(VALUE self, VALUE byte) {
272 CHECK_NIL(byte);
273 write_byte_direct(GET_TRANSPORT(self), FIX2INT(byte));
274 return Qnil;
275}
276
277VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16) {
278 rb_thrift_compact_proto_write_i32(self, i16);
279 return Qnil;
280}
281
282VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32) {
283 CHECK_NIL(i32);
284 write_varint32(GET_TRANSPORT(self), int_to_zig_zag(NUM2INT(i32)));
285 return Qnil;
286}
287
288VALUE rb_thrift_compact_proto_write_i64(VALUE self, VALUE i64) {
289 CHECK_NIL(i64);
290 write_varint64(GET_TRANSPORT(self), ll_to_zig_zag(NUM2LL(i64)));
291 return Qnil;
292}
293
294VALUE rb_thrift_compact_proto_write_double(VALUE self, VALUE dub) {
295 CHECK_NIL(dub);
296 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
297 union {
298 double f;
299 int64_t l;
300 } transfer;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000301 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000302 char buf[8];
303 buf[0] = transfer.l & 0xff;
304 buf[1] = (transfer.l >> 8) & 0xff;
305 buf[2] = (transfer.l >> 16) & 0xff;
306 buf[3] = (transfer.l >> 24) & 0xff;
307 buf[4] = (transfer.l >> 32) & 0xff;
308 buf[5] = (transfer.l >> 40) & 0xff;
309 buf[6] = (transfer.l >> 48) & 0xff;
310 buf[7] = (transfer.l >> 56) & 0xff;
311 WRITE(GET_TRANSPORT(self), buf, 8);
312 return Qnil;
313}
314
315VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
Jake Farrellb5a18a12012-10-09 01:10:43 +0000316 str = convert_to_utf8_byte_buffer(str);
Roger Meier19dbbef2012-12-27 01:24:20 +0100317 rb_thrift_compact_proto_write_binary(self, str);
318 return Qnil;
319}
320
321VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf) {
322 buf = force_binary_encoding(buf);
323 VALUE transport = GET_TRANSPORT(self);
Jean Boussier1e843412021-10-13 12:36:28 +0200324 write_varint32(transport, (uint32_t)RSTRING_LEN(buf));
Roger Meier02405722014-01-12 23:29:11 +0100325 WRITE(transport, StringValuePtr(buf), RSTRING_LEN(buf));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000326 return Qnil;
327}
328
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500329VALUE rb_thrift_compact_proto_write_uuid(VALUE self, VALUE uuid) {
330 if (NIL_P(uuid) || TYPE(uuid) != T_STRING) {
331 rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("UUID must be a string")));
332 }
333
334 VALUE transport = GET_TRANSPORT(self);
335 char bytes[16];
336 const char* str = RSTRING_PTR(uuid);
337 long len = RSTRING_LEN(uuid);
338
339 // Parse UUID string (format: "550e8400-e29b-41d4-a716-446655440000")
340 // Expected length: 36 characters (32 hex + 4 hyphens)
341 if (len != 36 || str[8] != '-' || str[13] != '-' || str[18] != '-' || str[23] != '-') {
342 rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("Invalid UUID format")));
343 }
344
345 // Parse hex string to bytes using direct conversion, skipping hyphens
346 int byte_idx = 0;
347 for (int i = 0; i < len && byte_idx < 16; i++) {
348 if (str[i] == '-') continue;
349 if (i + 1 >= len || str[i + 1] == '-') break;
350
351 // Convert two hex characters to one byte
352 int high = hex_char_to_int(str[i]);
353 int low = hex_char_to_int(str[i + 1]);
354
355 if (high < 0 || low < 0) break;
356
357 bytes[byte_idx++] = (unsigned char)((high << 4) | low);
358 i++; // skip next char since we processed two
359 }
360
361 if (byte_idx != 16) {
362 rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("Invalid UUID format")));
363 }
364
365 WRITE(transport, bytes, 16);
366 return Qnil;
367}
368
Bryan Duxburyd815c212009-03-19 18:57:43 +0000369//---------------------------------------
370// interface reading methods
371//---------------------------------------
372
373#define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
374
375VALUE rb_thrift_compact_proto_read_string(VALUE self);
Roger Meier19dbbef2012-12-27 01:24:20 +0100376VALUE rb_thrift_compact_proto_read_binary(VALUE self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000377VALUE rb_thrift_compact_proto_read_byte(VALUE self);
378VALUE rb_thrift_compact_proto_read_i32(VALUE self);
379VALUE rb_thrift_compact_proto_read_i16(VALUE self);
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500380VALUE rb_thrift_compact_proto_read_uuid(VALUE self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000381
382static int8_t get_ttype(int8_t ctype) {
383 if (ctype == TTYPE_STOP) {
384 return TTYPE_STOP;
385 } else if (ctype == CTYPE_BOOLEAN_TRUE || ctype == CTYPE_BOOLEAN_FALSE) {
386 return TTYPE_BOOL;
387 } else if (ctype == CTYPE_BYTE) {
388 return TTYPE_BYTE;
389 } else if (ctype == CTYPE_I16) {
390 return TTYPE_I16;
391 } else if (ctype == CTYPE_I32) {
392 return TTYPE_I32;
393 } else if (ctype == CTYPE_I64) {
394 return TTYPE_I64;
395 } else if (ctype == CTYPE_DOUBLE) {
396 return TTYPE_DOUBLE;
397 } else if (ctype == CTYPE_BINARY) {
398 return TTYPE_STRING;
399 } else if (ctype == CTYPE_LIST) {
400 return TTYPE_LIST;
401 } else if (ctype == CTYPE_SET) {
402 return TTYPE_SET;
403 } else if (ctype == CTYPE_MAP) {
404 return TTYPE_MAP;
405 } else if (ctype == CTYPE_STRUCT) {
406 return TTYPE_STRUCT;
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500407 } else if (ctype == CTYPE_UUID) {
408 return TTYPE_UUID;
Bryan Duxburyd815c212009-03-19 18:57:43 +0000409 } else {
410 char str[50];
411 sprintf(str, "don't know what type: %d", ctype);
Bryan Duxburyfd58c552009-09-01 22:32:50 +0000412 rb_raise(rb_eStandardError, "%s", str);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000413 return 0;
414 }
415}
416
417static char read_byte_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000418 VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
419 return (char)(FIX2INT(byte));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000420}
421
422static int64_t zig_zag_to_ll(int64_t n) {
423 return (((uint64_t)n) >> 1) ^ -(n & 1);
424}
425
426static int32_t zig_zag_to_int(int32_t n) {
427 return (((uint32_t)n) >> 1) ^ -(n & 1);
428}
429
430static int64_t read_varint64(VALUE self) {
431 int shift = 0;
432 int64_t result = 0;
433 while (true) {
434 int8_t b = read_byte_direct(self);
435 result = result | ((uint64_t)(b & 0x7f) << shift);
436 if ((b & 0x80) != 0x80) {
437 break;
438 }
439 shift += 7;
440 }
441 return result;
442}
443
444static int16_t read_i16(VALUE self) {
445 return zig_zag_to_int((int32_t)read_varint64(self));
446}
447
Bryan Duxburyd815c212009-03-19 18:57:43 +0000448VALUE rb_thrift_compact_proto_read_message_end(VALUE self) {
449 return Qnil;
450}
451
452VALUE rb_thrift_compact_proto_read_struct_begin(VALUE self) {
453 rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
454 return Qnil;
455}
456
457VALUE rb_thrift_compact_proto_read_struct_end(VALUE self) {
458 rb_ary_pop(rb_ivar_get(self, last_field_id));
459 return Qnil;
460}
461
462VALUE rb_thrift_compact_proto_read_field_end(VALUE self) {
463 return Qnil;
464}
465
466VALUE rb_thrift_compact_proto_read_map_end(VALUE self) {
467 return Qnil;
468}
469
470VALUE rb_thrift_compact_proto_read_list_end(VALUE self) {
471 return Qnil;
472}
473
474VALUE rb_thrift_compact_proto_read_set_end(VALUE self) {
475 return Qnil;
476}
477
478VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
479 int8_t protocol_id = read_byte_direct(self);
480 if (protocol_id != PROTOCOL_ID) {
481 char buf[100];
482 int len = sprintf(buf, "Expected protocol id %d but got %d", PROTOCOL_ID, protocol_id);
483 buf[len] = 0;
484 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
485 }
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500486
Bryan Duxburyd815c212009-03-19 18:57:43 +0000487 int8_t version_and_type = read_byte_direct(self);
488 int8_t version = version_and_type & VERSION_MASK;
489 if (version != VERSION) {
490 char buf[100];
491 int len = sprintf(buf, "Expected version id %d but got %d", version, VERSION);
492 buf[len] = 0;
493 rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
494 }
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500495
Jens Geyera86886e2014-09-17 22:25:48 +0200496 int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT) & TYPE_BITS;
Jean Boussier1e843412021-10-13 12:36:28 +0200497 int32_t seqid = (int32_t)read_varint64(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000498 VALUE messageName = rb_thrift_compact_proto_read_string(self);
499 return rb_ary_new3(3, messageName, INT2FIX(type), INT2NUM(seqid));
500}
501
502VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
503 int8_t type = read_byte_direct(self);
504 // if it's a stop, then we can return immediately, as the struct is over.
505 if ((type & 0x0f) == TTYPE_STOP) {
506 return rb_ary_new3(3, Qnil, INT2FIX(0), INT2FIX(0));
507 } else {
508 int field_id = 0;
509
510 // mask off the 4 MSB of the type header. it could contain a field id delta.
511 uint8_t modifier = ((type & 0xf0) >> 4);
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500512
Bryan Duxburyd815c212009-03-19 18:57:43 +0000513 if (modifier == 0) {
514 // not a delta. look ahead for the zigzag varint field id.
Bryan Duxbury09d13c22010-08-11 18:37:25 +0000515 (void) LAST_ID(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000516 field_id = read_i16(self);
517 } else {
518 // has a delta. add the delta to the last read field id.
519 field_id = LAST_ID(self) + modifier;
520 }
521
522 // if this happens to be a boolean field, the value is encoded in the type
523 if (is_bool_type(type)) {
524 // save the boolean value in a special instance variable.
525 rb_ivar_set(self, bool_value_id, (type & 0x0f) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse);
526 }
527
528 // push the new field onto the field stack so we can keep the deltas going.
529 SET_LAST_ID(self, INT2FIX(field_id));
530 return rb_ary_new3(3, Qnil, INT2FIX(get_ttype(type & 0x0f)), INT2FIX(field_id));
531 }
532}
533
534VALUE rb_thrift_compact_proto_read_map_begin(VALUE self) {
Jean Boussier1e843412021-10-13 12:36:28 +0200535 int32_t size = (int32_t)read_varint64(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000536 uint8_t key_and_value_type = size == 0 ? 0 : read_byte_direct(self);
537 return rb_ary_new3(3, INT2FIX(get_ttype(key_and_value_type >> 4)), INT2FIX(get_ttype(key_and_value_type & 0xf)), INT2FIX(size));
538}
539
540VALUE rb_thrift_compact_proto_read_list_begin(VALUE self) {
541 uint8_t size_and_type = read_byte_direct(self);
542 int32_t size = (size_and_type >> 4) & 0x0f;
543 if (size == 15) {
Jean Boussier1e843412021-10-13 12:36:28 +0200544 size = (int32_t)read_varint64(self);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000545 }
546 uint8_t type = get_ttype(size_and_type & 0x0f);
547 return rb_ary_new3(2, INT2FIX(type), INT2FIX(size));
548}
549
550VALUE rb_thrift_compact_proto_read_set_begin(VALUE self) {
551 return rb_thrift_compact_proto_read_list_begin(self);
552}
553
554VALUE rb_thrift_compact_proto_read_bool(VALUE self) {
555 VALUE bool_value = rb_ivar_get(self, bool_value_id);
556 if (NIL_P(bool_value)) {
557 return read_byte_direct(self) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse;
558 } else {
559 rb_ivar_set(self, bool_value_id, Qnil);
560 return bool_value;
561 }
562}
563
564VALUE rb_thrift_compact_proto_read_byte(VALUE self) {
565 return INT2FIX(read_byte_direct(self));
566}
567
568VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
569 return INT2FIX(read_i16(self));
570}
571
572VALUE rb_thrift_compact_proto_read_i32(VALUE self) {
Jean Boussier1e843412021-10-13 12:36:28 +0200573 return INT2NUM(zig_zag_to_int((int32_t)read_varint64(self)));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000574}
575
576VALUE rb_thrift_compact_proto_read_i64(VALUE self) {
577 return LL2NUM(zig_zag_to_ll(read_varint64(self)));
578}
579
580VALUE rb_thrift_compact_proto_read_double(VALUE self) {
581 union {
582 double f;
583 int64_t l;
584 } transfer;
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000585 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
586 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
587 uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[0]))
588 | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 8)
589 | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 16)
590 | (((uint8_t)(RSTRING_PTR(rbuf)[3])) << 24);
591 uint64_t hi = (((uint8_t)(RSTRING_PTR(rbuf)[4])))
592 | (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 8)
593 | (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 16)
594 | (((uint8_t)(RSTRING_PTR(rbuf)[7])) << 24);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000595 transfer.l = (hi << 32) | lo;
596
597 return rb_float_new(transfer.f);
598}
599
600VALUE rb_thrift_compact_proto_read_string(VALUE self) {
Roger Meier19dbbef2012-12-27 01:24:20 +0100601 VALUE buffer = rb_thrift_compact_proto_read_binary(self);
Jake Farrellb5a18a12012-10-09 01:10:43 +0000602 return convert_to_string(buffer);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000603}
604
Roger Meier19dbbef2012-12-27 01:24:20 +0100605VALUE rb_thrift_compact_proto_read_binary(VALUE self) {
606 int64_t size = read_varint64(self);
607 return READ(self, size);
608}
609
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500610VALUE rb_thrift_compact_proto_read_uuid(VALUE self) {
611 VALUE data = READ(self, 16);
612 const unsigned char* bytes = (const unsigned char*)RSTRING_PTR(data);
613
614 // Format as UUID string: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
615 char uuid_str[37];
616 char* p = uuid_str;
617
618 for (int i = 0; i < 16; i++) {
619 *p++ = int_to_hex_char((bytes[i] >> 4) & 0x0F);
620 *p++ = int_to_hex_char(bytes[i] & 0x0F);
621 if (i == 3 || i == 5 || i == 7 || i == 9) {
622 *p++ = '-';
623 }
624 }
625
626 *p = '\0';
627
628 return rb_str_new(uuid_str, 36);
629}
630
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500631static void Init_constants(void) {
Bryan Duxburyd815c212009-03-19 18:57:43 +0000632 thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
Stan Hucc70b4e2021-03-11 03:49:57 +0530633 rb_global_variable(&thrift_compact_protocol_class);
Roger Meier19dbbef2012-12-27 01:24:20 +0100634
Jean Boussier1e843412021-10-13 12:36:28 +0200635 VERSION = (int32_t)rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
636 VERSION_MASK = (int32_t)rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
637 TYPE_MASK = (int32_t)rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
638 TYPE_BITS = (int32_t)rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_BITS")));
Bryan Duxburyd815c212009-03-19 18:57:43 +0000639 TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
640 PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
Roger Meier19dbbef2012-12-27 01:24:20 +0100641
Bryan Duxburyd815c212009-03-19 18:57:43 +0000642 last_field_id = rb_intern("@last_field");
643 boolean_field_id = rb_intern("@boolean_field");
644 bool_value_id = rb_intern("@bool_value");
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000645 rbuf_ivar_id = rb_intern("@rbuf");
Bryan Duxburyd815c212009-03-19 18:57:43 +0000646}
647
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500648static void Init_rb_methods(void) {
Bryan Duxburyd815c212009-03-19 18:57:43 +0000649 rb_define_method(thrift_compact_protocol_class, "native?", rb_thrift_compact_proto_native_qmark, 0);
650
651 rb_define_method(thrift_compact_protocol_class, "write_message_begin", rb_thrift_compact_proto_write_message_begin, 3);
652 rb_define_method(thrift_compact_protocol_class, "write_field_begin", rb_thrift_compact_proto_write_field_begin, 3);
653 rb_define_method(thrift_compact_protocol_class, "write_field_stop", rb_thrift_compact_proto_write_field_stop, 0);
654 rb_define_method(thrift_compact_protocol_class, "write_map_begin", rb_thrift_compact_proto_write_map_begin, 3);
655 rb_define_method(thrift_compact_protocol_class, "write_list_begin", rb_thrift_compact_proto_write_list_begin, 2);
656 rb_define_method(thrift_compact_protocol_class, "write_set_begin", rb_thrift_compact_proto_write_set_begin, 2);
657 rb_define_method(thrift_compact_protocol_class, "write_byte", rb_thrift_compact_proto_write_byte, 1);
658 rb_define_method(thrift_compact_protocol_class, "write_bool", rb_thrift_compact_proto_write_bool, 1);
659 rb_define_method(thrift_compact_protocol_class, "write_i16", rb_thrift_compact_proto_write_i16, 1);
660 rb_define_method(thrift_compact_protocol_class, "write_i32", rb_thrift_compact_proto_write_i32, 1);
661 rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
662 rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
663 rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
Roger Meier19dbbef2012-12-27 01:24:20 +0100664 rb_define_method(thrift_compact_protocol_class, "write_binary", rb_thrift_compact_proto_write_binary, 1);
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500665 rb_define_method(thrift_compact_protocol_class, "write_uuid", rb_thrift_compact_proto_write_uuid, 1);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000666
667 rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
668 rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
669 rb_define_method(thrift_compact_protocol_class, "write_struct_end", rb_thrift_compact_proto_write_struct_end, 0);
670 rb_define_method(thrift_compact_protocol_class, "write_field_end", rb_thrift_compact_proto_write_field_end, 0);
671 rb_define_method(thrift_compact_protocol_class, "write_map_end", rb_thrift_compact_proto_write_map_end, 0);
672 rb_define_method(thrift_compact_protocol_class, "write_list_end", rb_thrift_compact_proto_write_list_end, 0);
673 rb_define_method(thrift_compact_protocol_class, "write_set_end", rb_thrift_compact_proto_write_set_end, 0);
674
675
676 rb_define_method(thrift_compact_protocol_class, "read_message_begin", rb_thrift_compact_proto_read_message_begin, 0);
677 rb_define_method(thrift_compact_protocol_class, "read_field_begin", rb_thrift_compact_proto_read_field_begin, 0);
678 rb_define_method(thrift_compact_protocol_class, "read_map_begin", rb_thrift_compact_proto_read_map_begin, 0);
679 rb_define_method(thrift_compact_protocol_class, "read_list_begin", rb_thrift_compact_proto_read_list_begin, 0);
680 rb_define_method(thrift_compact_protocol_class, "read_set_begin", rb_thrift_compact_proto_read_set_begin, 0);
681 rb_define_method(thrift_compact_protocol_class, "read_byte", rb_thrift_compact_proto_read_byte, 0);
682 rb_define_method(thrift_compact_protocol_class, "read_bool", rb_thrift_compact_proto_read_bool, 0);
683 rb_define_method(thrift_compact_protocol_class, "read_i16", rb_thrift_compact_proto_read_i16, 0);
684 rb_define_method(thrift_compact_protocol_class, "read_i32", rb_thrift_compact_proto_read_i32, 0);
685 rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
686 rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
687 rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
Roger Meier19dbbef2012-12-27 01:24:20 +0100688 rb_define_method(thrift_compact_protocol_class, "read_binary", rb_thrift_compact_proto_read_binary, 0);
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -0500689 rb_define_method(thrift_compact_protocol_class, "read_uuid", rb_thrift_compact_proto_read_uuid, 0);
Bryan Duxburyd815c212009-03-19 18:57:43 +0000690
691 rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
692 rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);
693 rb_define_method(thrift_compact_protocol_class, "read_struct_end", rb_thrift_compact_proto_read_struct_end, 0);
694 rb_define_method(thrift_compact_protocol_class, "read_field_end", rb_thrift_compact_proto_read_field_end, 0);
695 rb_define_method(thrift_compact_protocol_class, "read_map_end", rb_thrift_compact_proto_read_map_end, 0);
696 rb_define_method(thrift_compact_protocol_class, "read_list_end", rb_thrift_compact_proto_read_list_end, 0);
697 rb_define_method(thrift_compact_protocol_class, "read_set_end", rb_thrift_compact_proto_read_set_end, 0);
698}
699
Dmytro Shteflyuk0d18fb22025-12-20 12:13:46 -0500700void Init_compact_protocol(void) {
Bryan Duxburyd815c212009-03-19 18:57:43 +0000701 Init_constants();
702 Init_rb_methods();
Bryan Duxburyd815c212009-03-19 18:57:43 +0000703}