Skip to content

Commit

Permalink
Length is encoded as little endian
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 10, 2015
1 parent 307d571 commit 1792a2e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ext/bson/native.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,17 +400,19 @@ VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i)
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string)
{
byte_buffer_t *b;
int32_t length_le;

char *str = RSTRING_PTR(string);
const int32_t length = BSON_UINT32_TO_LE(RSTRING_LEN(string) + 1);
const int32_t length = RSTRING_LEN(string) + 1;
length_le = BSON_UINT32_TO_LE(length);

if (!rb_bson_utf8_validate(str, length - 1, true)) {
rb_raise(rb_eArgError, "String %s is not valid UTF-8.", str);
}

TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
ENSURE_BSON_WRITE(b, length + 4);
memcpy(WRITE_PTR(b), (char*)&length, 4);
memcpy(WRITE_PTR(b), (char*)&length_le, 4);
b->write_position += 4;
memcpy(WRITE_PTR(b), str, length);
b->write_position += length;
Expand Down

0 comments on commit 1792a2e

Please sign in to comment.