@@ -124,33 +124,41 @@ static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, V
124124 SET_LAST_ID (self , id_value );
125125}
126126
127- static int32_t int_to_zig_zag (int32_t n ) {
128- return (n << 1 ) ^ (n >> 31 );
127+ static uint32_t int_to_zig_zag (int32_t n ) {
128+ return ((( uint32_t ) n ) << 1 ) ^ (0U - ( uint32_t )( n < 0 ) );
129129}
130130
131131static uint64_t ll_to_zig_zag (int64_t n ) {
132- return (n << 1 ) ^ (n >> 63 );
132+ return (((uint64_t )n ) << 1 ) ^ (0ULL - (uint64_t )(n < 0 ));
133+ }
134+
135+ static uint32_t message_seqid_to_varint32 (int32_t seqid ) {
136+ return seqid < 0 ? (uint32_t )((int64_t )seqid + (INT64_C (1 ) << 32 )) : (uint32_t )seqid ;
137+ }
138+
139+ static int32_t message_seqid_from_varint32 (uint32_t seqid ) {
140+ return seqid > INT32_MAX ? (int32_t )((int64_t )seqid - (INT64_C (1 ) << 32 )) : (int32_t )seqid ;
133141}
134142
135143static void write_varint32 (VALUE transport , uint32_t n ) {
136144 while (true) {
137- if ((n & ~0x7F ) == 0 ) {
138- write_byte_direct (transport , n & 0x7f );
145+ if ((n & ~0x7FU ) == 0U ) {
146+ write_byte_direct (transport , n & 0x7FU );
139147 break ;
140148 } else {
141- write_byte_direct (transport , (n & 0x7F ) | 0x80 );
149+ write_byte_direct (transport , (n & 0x7FU ) | 0x80U );
142150 n = n >> 7 ;
143151 }
144152 }
145153}
146154
147155static void write_varint64 (VALUE transport , uint64_t n ) {
148156 while (true) {
149- if ((n & ~0x7F ) == 0 ) {
150- write_byte_direct (transport , n & 0x7f );
157+ if ((n & ~0x7FULL ) == 0ULL ) {
158+ write_byte_direct (transport , n & 0x7FULL );
151159 break ;
152160 } else {
153- write_byte_direct (transport , (n & 0x7F ) | 0x80 );
161+ write_byte_direct (transport , (n & 0x7FULL ) | 0x80ULL );
154162 n = n >> 7 ;
155163 }
156164 }
@@ -208,9 +216,10 @@ VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
208216
209217VALUE rb_thrift_compact_proto_write_message_begin (VALUE self , VALUE name , VALUE type , VALUE seqid ) {
210218 VALUE transport = GET_TRANSPORT (self );
219+ int32_t seqid_value = FIX2INT (seqid );
211220 write_byte_direct (transport , PROTOCOL_ID );
212221 write_byte_direct (transport , (VERSION & VERSION_MASK ) | ((FIX2INT (type ) << TYPE_SHIFT_AMOUNT ) & TYPE_MASK ));
213- write_varint32 (transport , FIX2INT ( seqid ));
222+ write_varint32 (transport , message_seqid_to_varint32 ( seqid_value ));
214223 rb_thrift_compact_proto_write_string (self , name );
215224
216225 return Qnil ;
@@ -419,20 +428,20 @@ static char read_byte_direct(VALUE self) {
419428 return (char )(FIX2INT (byte ));
420429}
421430
422- static int64_t zig_zag_to_ll (int64_t n ) {
423- return ((( uint64_t ) n ) >> 1 ) ^ - (n & 1 );
431+ static int64_t zig_zag_to_ll (uint64_t n ) {
432+ return (int64_t )(( n >> 1 ) ^ ( 0ULL - (n & 1ULL )) );
424433}
425434
426- static int32_t zig_zag_to_int (int32_t n ) {
427- return ((( uint32_t ) n ) >> 1 ) ^ - (n & 1 );
435+ static int32_t zig_zag_to_int (uint32_t n ) {
436+ return (int32_t )(( n >> 1 ) ^ ( 0U - (n & 1U )) );
428437}
429438
430- static int64_t read_varint64 (VALUE self ) {
439+ static uint64_t read_varint64 (VALUE self ) {
431440 int shift = 0 ;
432- int64_t result = 0 ;
441+ uint64_t result = 0 ;
433442 while (true) {
434443 int8_t b = read_byte_direct (self );
435- result = result | ((uint64_t )(b & 0x7f ) << shift );
444+ result |= ((uint64_t )(b & 0x7f ) << shift );
436445 if ((b & 0x80 ) != 0x80 ) {
437446 break ;
438447 }
@@ -441,8 +450,12 @@ static int64_t read_varint64(VALUE self) {
441450 return result ;
442451}
443452
453+ static uint32_t read_varint32 (VALUE self ) {
454+ return (uint32_t )read_varint64 (self );
455+ }
456+
444457static int16_t read_i16 (VALUE self ) {
445- return zig_zag_to_int (( int32_t ) read_varint64 (self ));
458+ return ( int16_t ) zig_zag_to_int ( read_varint32 (self ));
446459}
447460
448461VALUE rb_thrift_compact_proto_read_message_end (VALUE self ) {
@@ -494,7 +507,7 @@ VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
494507 }
495508
496509 int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT ) & TYPE_BITS ;
497- int32_t seqid = ( int32_t ) read_varint64 (self );
510+ int32_t seqid = message_seqid_from_varint32 ( read_varint32 (self ) );
498511 VALUE messageName = rb_thrift_compact_proto_read_string (self );
499512 return rb_ary_new3 (3 , messageName , INT2FIX (type ), INT2NUM (seqid ));
500513}
@@ -532,19 +545,19 @@ VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
532545}
533546
534547VALUE rb_thrift_compact_proto_read_map_begin (VALUE self ) {
535- int32_t size = ( int32_t ) read_varint64 (self );
548+ uint32_t size = read_varint32 (self );
536549 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 ));
550+ return rb_ary_new3 (3 , INT2FIX (get_ttype (key_and_value_type >> 4 )), INT2FIX (get_ttype (key_and_value_type & 0xf )), UINT2NUM (size ));
538551}
539552
540553VALUE rb_thrift_compact_proto_read_list_begin (VALUE self ) {
541554 uint8_t size_and_type = read_byte_direct (self );
542- int32_t size = (size_and_type >> 4 ) & 0x0f ;
555+ uint32_t size = (size_and_type >> 4 ) & 0x0f ;
543556 if (size == 15 ) {
544- size = ( int32_t ) read_varint64 (self );
557+ size = read_varint32 (self );
545558 }
546559 uint8_t type = get_ttype (size_and_type & 0x0f );
547- return rb_ary_new3 (2 , INT2FIX (type ), INT2FIX (size ));
560+ return rb_ary_new3 (2 , INT2FIX (type ), UINT2NUM (size ));
548561}
549562
550563VALUE rb_thrift_compact_proto_read_set_begin (VALUE self ) {
@@ -570,7 +583,7 @@ VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
570583}
571584
572585VALUE rb_thrift_compact_proto_read_i32 (VALUE self ) {
573- return INT2NUM (zig_zag_to_int (( int32_t ) read_varint64 (self )));
586+ return INT2NUM (zig_zag_to_int (read_varint32 (self )));
574587}
575588
576589VALUE rb_thrift_compact_proto_read_i64 (VALUE self ) {
@@ -603,8 +616,8 @@ VALUE rb_thrift_compact_proto_read_string(VALUE self) {
603616}
604617
605618VALUE rb_thrift_compact_proto_read_binary (VALUE self ) {
606- int64_t size = read_varint64 (self );
607- return READ ( self , size );
619+ uint32_t size = read_varint32 (self );
620+ return rb_funcall ( GET_TRANSPORT ( self ), read_all_method_id , 1 , UINT2NUM ( size ) );
608621}
609622
610623VALUE rb_thrift_compact_proto_read_uuid (VALUE self ) {
0 commit comments