2222
2323#include "ruby.h"
2424#include "version.h"
25+ #include <arpa/inet.h>
2526
2627/* Ensure compatibility with early releases of Ruby 1.8.5 */
2728#ifndef RSTRING_PTR
@@ -113,9 +114,9 @@ struct deserialize_opts {
113114#if HAVE_RUBY_ENCODING_H
114115#include "ruby/encoding.h"
115116#define STR_NEW (p ,n ) \
116- ({ \
117- VALUE _str = rb_enc_str_new((p), (n), rb_utf8_encoding()); \
118- rb_encoding* internal_encoding = rb_default_internal_encoding(); \
117+ ({ VALUE _str; \
118+ _str = rb_enc_str_new((p), (n), rb_utf8_encoding()); \
119+ rb_encoding* internal_encoding = rb_default_internal_encoding();\
119120 if (internal_encoding) { \
120121 _str = rb_str_export_to_enc(_str, internal_encoding); \
121122 } \
@@ -126,7 +127,8 @@ struct deserialize_opts {
126127#endif
127128
128129static void write_utf8 (bson_buffer_t buffer , VALUE string , int allow_null ) {
129- result_t status = validate_utf8_encoding (
130+ result_t status ;
131+ status = validate_utf8_encoding (
130132 (const char * )RSTRING_PTR (string ), RSTRING_LEN (string ), allow_null );
131133
132134 if (status == HAS_NULL ) {
@@ -161,15 +163,17 @@ static void write_utf8(bson_buffer_t buffer, VALUE string, int allow_null) {
161163#ifdef _WIN32 || _MSC_VER
162164#define INT2STRING (buffer , i ) \
163165 { \
164- int vslength = _scprintf("%d", i) + 1; \
166+ int vslength; \
167+ vslength = _scprintf("%d", i) + 1; \
165168 *buffer = malloc(vslength); \
166169 _snprintf(*buffer, vslength, "%d", i); \
167170 }
168171#define FREE_INTSTRING (buffer ) free(buffer)
169172#else
170173#define INT2STRING (buffer , i ) \
171174 { \
172- int vslength = snprintf(NULL, 0, "%d", i) + 1; \
175+ int vslength; \
176+ vslength = snprintf(NULL, 0, "%d", i) + 1; \
173177 *buffer = malloc(vslength); \
174178 snprintf(*buffer, vslength, "%d", i); \
175179 }
@@ -282,8 +286,10 @@ static void serialize_regex(bson_buffer_t buffer, VALUE key, VALUE pattern, long
282286
283287 has_extra = rb_funcall (value , rb_intern ("respond_to?" ), 1 , rb_str_new2 ("extra_options_str" ));
284288 if (TYPE (has_extra ) == T_TRUE ) {
285- VALUE extra = rb_funcall (value , rb_intern ("extra_options_str" ), 0 );
286- bson_buffer_position old_position = bson_buffer_get_position (buffer );
289+ VALUE extra ;
290+ bson_buffer_position old_position ;
291+ extra = rb_funcall (value , rb_intern ("extra_options_str" ), 0 );
292+ old_position = bson_buffer_get_position (buffer );
287293 SAFE_WRITE (buffer , RSTRING_PTR (extra ), RSTRING_LENINT (extra ));
288294 qsort (bson_buffer_get_buffer (buffer ) + old_position , RSTRING_LEN (extra ), sizeof (char ), cmp_char );
289295 }
@@ -292,8 +298,10 @@ static void serialize_regex(bson_buffer_t buffer, VALUE key, VALUE pattern, long
292298}
293299
294300static int write_element (VALUE key , VALUE value , VALUE extra , int allow_id ) {
295- bson_buffer_t buffer = (bson_buffer_t )NUM2LL (rb_ary_entry (extra , 0 ));
296- VALUE check_keys = rb_ary_entry (extra , 1 );
301+ VALUE check_keys ;
302+ bson_buffer_t buffer ;
303+ buffer = (bson_buffer_t )NUM2LL (rb_ary_entry (extra , 0 ));
304+ check_keys = rb_ary_entry (extra , 1 );
297305
298306 if (TYPE (key ) == T_SYMBOL ) {
299307 // TODO better way to do this... ?
@@ -364,7 +372,8 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
364372 }
365373 case T_FLOAT :
366374 {
367- double d = NUM2DBL (value );
375+ double d ;
376+ d = NUM2DBL (value );
368377 write_name_and_type (buffer , key , 0x01 );
369378 SAFE_WRITE (buffer , (char * )& d , 8 );
370379 break ;
@@ -422,8 +431,10 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
422431 }
423432 case T_SYMBOL :
424433 {
425- const char * str_value = rb_id2name (SYM2ID (value ));
426- int length = (int )strlen (str_value ) + 1 ;
434+ const char * str_value ;
435+ int length ;
436+ str_value = rb_id2name (SYM2ID (value ));
437+ length = (int )strlen (str_value ) + 1 ;
427438 write_name_and_type (buffer , key , 0x0E );
428439 SAFE_WRITE (buffer , (char * )& length , 4 );
429440 SAFE_WRITE (buffer , str_value , length );
@@ -435,10 +446,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
435446 const char * cls = rb_obj_classname (value );
436447 if (strcmp (cls , "BSON::Binary" ) == 0 ||
437448 strcmp (cls , "ByteBuffer" ) == 0 ) {
449+ VALUE string_data ;
450+ int length ;
438451 const char subtype = strcmp (cls , "ByteBuffer" ) ?
439452 (const char )FIX2INT (rb_funcall (value , rb_intern ("subtype" ), 0 )) : 2 ;
440- VALUE string_data = rb_funcall (value , rb_intern ("to_s" ), 0 );
441- int length = RSTRING_LENINT (string_data );
453+ string_data = rb_funcall (value , rb_intern ("to_s" ), 0 );
454+ length = RSTRING_LENINT (string_data );
442455 write_name_and_type (buffer , key , 0x05 );
443456 if (subtype == 2 ) {
444457 const int other_length = length + 4 ;
@@ -453,11 +466,13 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
453466 break ;
454467 }
455468 if (strcmp (cls , "BSON::ObjectId" ) == 0 ) {
456- VALUE as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
469+ VALUE as_array ;
470+ as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
457471 int i ;
458472 write_name_and_type (buffer , key , 0x07 );
459473 for (i = 0 ; i < 12 ; i ++ ) {
460- char byte = (char )FIX2INT (rb_ary_entry (as_array , i ));
474+ char byte ;
475+ byte = (char )FIX2INT (rb_ary_entry (as_array , i ));
461476 SAFE_WRITE (buffer , & byte , 1 );
462477 }
463478 break ;
@@ -544,7 +559,8 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
544559 }
545560 if (strcmp (cls , "ActiveSupport::Multibyte::Chars" ) == 0 ) {
546561 int length ;
547- VALUE str = StringValue (value );
562+ VALUE str ;
563+ str = StringValue (value );
548564 write_name_and_type (buffer , key , 0x02 );
549565 length = RSTRING_LENINT (str ) + 1 ;
550566 SAFE_WRITE (buffer , (char * )& length , 4 );
@@ -563,10 +579,13 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
563579 }
564580 case T_DATA :
565581 {
566- const char * cls = rb_obj_classname (value );
582+ const char * cls ;
583+ cls = rb_obj_classname (value );
567584 if (strcmp (cls , "Time" ) == 0 ) {
568- double t = NUM2DBL (rb_funcall (value , rb_intern ("to_f" ), 0 ));
569- long long time_since_epoch = (long long )round (t * 1000 );
585+ double t ;
586+ long long time_since_epoch ;
587+ t = NUM2DBL (rb_funcall (value , rb_intern ("to_f" ), 0 ));
588+ time_since_epoch = (long long )round (t * 1000 );
570589 write_name_and_type (buffer , key , 0x09 );
571590 SAFE_WRITE (buffer , (const char * )& time_since_epoch , 8 );
572591 break ;
@@ -588,14 +607,17 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
588607 }
589608 case T_REGEXP :
590609 {
591- VALUE pattern = RREGEXP_SRC (value );
592- long flags = RREGEXP_OPTIONS (value );
610+ VALUE pattern ;
611+ long flags ;
612+ pattern = RREGEXP_SRC (value );
613+ flags = RREGEXP_OPTIONS (value );
593614 serialize_regex (buffer , key , pattern , flags , value , 1 );
594615 break ;
595616 }
596617 default :
597618 {
598- const char * cls = rb_obj_classname (value );
619+ const char * cls ;
620+ cls = rb_obj_classname (value );
599621 bson_buffer_free (buffer );
600622 rb_raise (InvalidDocument , "Cannot serialize an object of class %s (type %d) into BSON." , cls , TYPE (value ));
601623 break ;
@@ -613,14 +635,17 @@ static int write_element_with_id(VALUE key, VALUE value, VALUE extra) {
613635}
614636
615637static void write_doc (bson_buffer_t buffer , VALUE hash , VALUE check_keys , VALUE move_id ) {
616- bson_buffer_position start_position = bson_buffer_get_position (buffer );
617- bson_buffer_position length_location = bson_buffer_save_space (buffer , 4 );
638+ bson_buffer_position start_position , length_location ;
618639 bson_buffer_position length ;
619640 int allow_id ;
620641 int max_size ;
621642 int (* write_function )(VALUE , VALUE , VALUE ) = NULL ;
622- VALUE id_str = rb_str_new2 ("_id" );
623- VALUE id_sym = ID2SYM (rb_intern ("_id" ));
643+ VALUE id_str , id_sym ;
644+
645+ start_position = bson_buffer_get_position (buffer );
646+ length_location = bson_buffer_save_space (buffer , 4 );
647+ id_str = rb_str_new2 ("_id" );
648+ id_sym = ID2SYM (rb_intern ("_id" ));
624649
625650 if (length_location == -1 ) {
626651 rb_raise (rb_eNoMemError , "failed to allocate memory in buffer.c" );
@@ -630,10 +655,12 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
630655 if (move_id == Qtrue ) {
631656 allow_id = 0 ;
632657 if (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_str ) == Qtrue ) {
633- VALUE id = rb_hash_aref (hash , id_str );
658+ VALUE id ;
659+ id = rb_hash_aref (hash , id_str );
634660 write_element_with_id (id_str , id , pack_extra (buffer , check_keys ));
635661 } else if (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_sym ) == Qtrue ) {
636- VALUE id = rb_hash_aref (hash , id_sym );
662+ VALUE id ;
663+ id = rb_hash_aref (hash , id_sym );
637664 write_element_with_id (id_sym , id , pack_extra (buffer , check_keys ));
638665 }
639666 }
@@ -643,7 +670,8 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
643670 if ((rb_obj_classname (hash ), "Hash" ) == 0 ) {
644671 if ((rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_str ) == Qtrue ) &&
645672 (rb_funcall (hash , rb_intern ("has_key?" ), 1 , id_sym ) == Qtrue )) {
646- VALUE oid_sym = rb_hash_delete (hash , id_sym );
673+ VALUE oid_sym ;
674+ oid_sym = rb_hash_delete (hash , id_sym );
647675 rb_funcall (hash , rb_intern ("[]=" ), 2 , id_str , oid_sym );
648676 }
649677 }
@@ -658,11 +686,13 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
658686
659687 // we have to check for an OrderedHash and handle that specially
660688 if (strcmp (rb_obj_classname (hash ), "BSON::OrderedHash" ) == 0 ) {
661- VALUE keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
689+ VALUE keys ;
690+ keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
662691 int i ;
663- for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
664- VALUE key = rb_ary_entry (keys , i );
665- VALUE value = rb_hash_aref (hash , key );
692+ VALUE key , value ;
693+ for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
694+ key = rb_ary_entry (keys , i );
695+ value = rb_hash_aref (hash , key );
666696
667697 write_function (key , value , pack_extra (buffer , check_keys ));
668698 }
@@ -692,7 +722,8 @@ static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys,
692722 VALUE move_id , VALUE max_size ) {
693723
694724 VALUE result ;
695- bson_buffer_t buffer = bson_buffer_new ();
725+ bson_buffer_t buffer ;
726+ buffer = bson_buffer_new ();
696727 if (buffer == NULL ) {
697728 rb_raise (rb_eNoMemError , "failed to allocate memory in buffer.c" );
698729 }
@@ -739,9 +770,10 @@ static VALUE get_value(const char* buffer, int* position,
739770 int size ;
740771 memcpy (& size , buffer + * position , 4 );
741772 if (strcmp (buffer + * position + 5 , "$ref" ) == 0 ) { // DBRef
742- int offset = * position + 10 ;
773+ int offset , collection_length ;
774+ offset = * position + 10 ;
743775 VALUE argv [2 ];
744- int collection_length = * (int * )(buffer + offset ) - 1 ;
776+ collection_length = * (int * )(buffer + offset ) - 1 ;
745777 unsigned char id_type ;
746778 offset += 4 ;
747779
@@ -766,8 +798,10 @@ static VALUE get_value(const char* buffer, int* position,
766798
767799 value = rb_ary_new ();
768800 while (* position < end ) {
769- unsigned char type = (unsigned char )buffer [(* position )++ ];
770- int key_size = (int )strlen (buffer + * position );
801+ unsigned char type ;
802+ type = (unsigned char )buffer [(* position )++ ];
803+ int key_size ;
804+ key_size = (int )strlen (buffer + * position );
771805 VALUE to_append ;
772806
773807 * position += key_size + 1 ; // just skip the key, they're in order.
@@ -803,8 +837,9 @@ static VALUE get_value(const char* buffer, int* position,
803837 }
804838 case 7 :
805839 {
806- VALUE str = rb_str_new (buffer + * position , 12 );
807- VALUE oid = rb_funcall (str , unpack_method , 1 , rb_str_new2 ("C*" ));
840+ VALUE str , oid ;
841+ str = rb_str_new (buffer + * position , 12 );
842+ oid = rb_funcall (str , unpack_method , 1 , rb_str_new2 ("C*" ));
808843 value = rb_class_new_instance (1 , & oid , ObjectId );
809844 * position += 12 ;
810845 break ;
@@ -844,14 +879,15 @@ static VALUE get_value(const char* buffer, int* position,
844879 }
845880 case 11 :
846881 {
847- int pattern_length = ( int ) strlen ( buffer + * position ) ;
848- VALUE pattern = STR_NEW ( buffer + * position , pattern_length ) ;
882+ int pattern_length ;
883+ VALUE pattern , argv [ 3 ], flags_str ;
849884 int flags_length ;
850- VALUE argv [3 ];
885+ pattern_length = (int )strlen (buffer + * position );
886+ pattern = STR_NEW (buffer + * position , pattern_length );
851887 * position += pattern_length + 1 ;
852888
853889 flags_length = (int )strlen (buffer + * position );
854- VALUE flags_str = STR_NEW (buffer + * position , flags_length );
890+ flags_str = STR_NEW (buffer + * position , flags_length );
855891 argv [0 ] = pattern ;
856892 argv [1 ] = flags_str ;
857893 value = rb_class_new_instance (2 , argv , BSONRegex );
@@ -951,13 +987,16 @@ static VALUE get_value(const char* buffer, int* position,
951987}
952988
953989static VALUE elements_to_hash (const char * buffer , int max , struct deserialize_opts * opts ) {
954- VALUE hash = rb_class_new_instance (0 , NULL , OrderedHash );
990+ VALUE hash ;
991+ hash = rb_class_new_instance (0 , NULL , OrderedHash );
955992 int position = 0 ;
956993 while (position < max ) {
957- unsigned char type = (unsigned char )buffer [position ++ ];
958- int name_length = (int )strlen (buffer + position );
959- VALUE name = STR_NEW (buffer + position , name_length );
960- VALUE value ;
994+ unsigned char type ;
995+ int name_length ;
996+ VALUE name , value ;
997+ type = (unsigned char )buffer [position ++ ];
998+ name_length = (int )strlen (buffer + position );
999+ name = STR_NEW (buffer + position , name_length );
9611000 position += name_length + 1 ;
9621001 value = get_value (buffer , & position , type , opts );
9631002 rb_funcall (hash , element_assignment_method , 2 , name , value );
@@ -966,8 +1005,10 @@ static VALUE elements_to_hash(const char* buffer, int max, struct deserialize_op
9661005}
9671006
9681007static VALUE method_deserialize (VALUE self , VALUE bson , VALUE opts ) {
969- const char * buffer = RSTRING_PTR (bson );
970- int remaining = RSTRING_LENINT (bson );
1008+ const char * buffer ;
1009+ int remaining ;
1010+ buffer = RSTRING_PTR (bson );
1011+ remaining = RSTRING_LENINT (bson );
9711012 struct deserialize_opts deserialize_opts ;
9721013 deserialize_opts .compile_regex = 1 ;
9731014 if (rb_funcall (opts , rb_intern ("has_key?" ), 1 , ID2SYM (rb_intern ("compile_regex" ))) == Qtrue &&
0 commit comments