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
@@ -115,7 +116,7 @@ struct deserialize_opts {
115116#define STR_NEW (p ,n ) \
116117 ({ \
117118 VALUE _str = rb_enc_str_new((p), (n), rb_utf8_encoding()); \
118- rb_encoding* internal_encoding = rb_default_internal_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 } \
@@ -167,11 +168,11 @@ static void write_utf8(bson_buffer_t buffer, VALUE string, int allow_null) {
167168 }
168169#define FREE_INTSTRING (buffer ) free(buffer)
169170#else
170- #define INT2STRING (buffer , i ) \
171- { \
171+ #define INT2STRING (buffer , i ) \
172+ { \
172173 int vslength = snprintf(NULL, 0, "%d", i) + 1; \
173- *buffer = malloc(vslength); \
174- snprintf(*buffer, vslength, "%d", i); \
174+ *buffer = malloc(vslength); \
175+ snprintf(*buffer, vslength, "%d", i); \
175176 }
176177#define FREE_INTSTRING (buffer ) free(buffer)
177178#endif
@@ -435,10 +436,10 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
435436 const char * cls = rb_obj_classname (value );
436437 if (strcmp (cls , "BSON::Binary" ) == 0 ||
437438 strcmp (cls , "ByteBuffer" ) == 0 ) {
438- const char subtype = strcmp (cls , "ByteBuffer" ) ?
439- (const char )FIX2INT (rb_funcall (value , rb_intern ("subtype" ), 0 )) : 2 ;
440439 VALUE string_data = rb_funcall (value , rb_intern ("to_s" ), 0 );
441440 int length = RSTRING_LENINT (string_data );
441+ const char subtype = strcmp (cls , "ByteBuffer" ) ?
442+ (const char )FIX2INT (rb_funcall (value , rb_intern ("subtype" ), 0 )) : 2 ;
442443 write_name_and_type (buffer , key , 0x05 );
443444 if (subtype == 2 ) {
444445 const int other_length = length + 4 ;
@@ -453,8 +454,8 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
453454 break ;
454455 }
455456 if (strcmp (cls , "BSON::ObjectId" ) == 0 ) {
456- VALUE as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
457457 int i ;
458+ VALUE as_array = rb_funcall (value , rb_intern ("to_a" ), 0 );
458459 write_name_and_type (buffer , key , 0x07 );
459460 for (i = 0 ; i < 12 ; i ++ ) {
460461 char byte = (char )FIX2INT (rb_ary_entry (as_array , i ));
@@ -658,9 +659,10 @@ static void write_doc(bson_buffer_t buffer, VALUE hash, VALUE check_keys, VALUE
658659
659660 // we have to check for an OrderedHash and handle that specially
660661 if (strcmp (rb_obj_classname (hash ), "BSON::OrderedHash" ) == 0 ) {
661- VALUE keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
662662 int i ;
663- for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
663+ VALUE keys = rb_funcall (hash , rb_intern ("keys" ), 0 );
664+
665+ for (i = 0 ; i < RARRAY_LEN (keys ); i ++ ) {
664666 VALUE key = rb_ary_entry (keys , i );
665667 VALUE value = rb_hash_aref (hash , key );
666668
@@ -739,10 +741,11 @@ static VALUE get_value(const char* buffer, int* position,
739741 int size ;
740742 memcpy (& size , buffer + * position , 4 );
741743 if (strcmp (buffer + * position + 5 , "$ref" ) == 0 ) { // DBRef
742- int offset = * position + 10 ;
743744 VALUE argv [2 ];
744- int collection_length = * (int * )(buffer + offset ) - 1 ;
745745 unsigned char id_type ;
746+ int offset = * position + 10 ;
747+ int collection_length = * (int * )(buffer + offset ) - 1 ;
748+
746749 offset += 4 ;
747750
748751 argv [0 ] = STR_NEW (buffer + offset , collection_length );
@@ -766,9 +769,9 @@ static VALUE get_value(const char* buffer, int* position,
766769
767770 value = rb_ary_new ();
768771 while (* position < end ) {
772+ VALUE to_append ;
769773 unsigned char type = (unsigned char )buffer [(* position )++ ];
770774 int key_size = (int )strlen (buffer + * position );
771- VALUE to_append ;
772775
773776 * position += key_size + 1 ; // just skip the key, they're in order.
774777 to_append = get_value (buffer , position , type , opts );
@@ -847,11 +850,11 @@ static VALUE get_value(const char* buffer, int* position,
847850 int pattern_length = (int )strlen (buffer + * position );
848851 VALUE pattern = STR_NEW (buffer + * position , pattern_length );
849852 int flags_length ;
850- VALUE argv [3 ];
853+ VALUE argv [3 ], flags_str ;
851854 * position += pattern_length + 1 ;
852855
853856 flags_length = (int )strlen (buffer + * position );
854- VALUE flags_str = STR_NEW (buffer + * position , flags_length );
857+ flags_str = STR_NEW (buffer + * position , flags_length );
855858 argv [0 ] = pattern ;
856859 argv [1 ] = flags_str ;
857860 value = rb_class_new_instance (2 , argv , BSONRegex );
@@ -951,13 +954,13 @@ static VALUE get_value(const char* buffer, int* position,
951954}
952955
953956static VALUE elements_to_hash (const char * buffer , int max , struct deserialize_opts * opts ) {
954- VALUE hash = rb_class_new_instance (0 , NULL , OrderedHash );
955957 int position = 0 ;
958+ VALUE hash = rb_class_new_instance (0 , NULL , OrderedHash );
956959 while (position < max ) {
960+ VALUE value ;
957961 unsigned char type = (unsigned char )buffer [position ++ ];
958962 int name_length = (int )strlen (buffer + position );
959963 VALUE name = STR_NEW (buffer + position , name_length );
960- VALUE value ;
961964 position += name_length + 1 ;
962965 value = get_value (buffer , & position , type , opts );
963966 rb_funcall (hash , element_assignment_method , 2 , name , value );
@@ -969,6 +972,7 @@ static VALUE method_deserialize(VALUE self, VALUE bson, VALUE opts) {
969972 const char * buffer = RSTRING_PTR (bson );
970973 int remaining = RSTRING_LENINT (bson );
971974 struct deserialize_opts deserialize_opts ;
975+
972976 deserialize_opts .compile_regex = 1 ;
973977 if (rb_funcall (opts , rb_intern ("has_key?" ), 1 , ID2SYM (rb_intern ("compile_regex" ))) == Qtrue &&
974978 rb_hash_aref (opts , ID2SYM (rb_intern ("compile_regex" ))) == Qfalse ) {
0 commit comments