Apparent error in the bsonspec encoding examples

397 views
Skip to first unread message

Frederic Marand

unread,
Oct 11, 2013, 3:49:56 PM10/11/13
to bs...@googlegroups.com
I think there is an error in the encoding for the second document in the Bsonspec examples. The document starts as follows:
Source: {"BSON":["awesome", ...

Encoded: \x31\x00\x00\x00\x04BSON\x00\x26\x00\x00\x00\x020\x00\x08aweseome\x00


Should be: \x31\x00\x00\x00\x04BSON\x00\x26\x00\x00\x00\x02\x30\x00awesome\x00


31 00 00 00: length
04 array
BSON 00 key name
26 00 00 00 document length
02 string value <- this is where the reference fails: 020 instead of 02 30
30 00
awesome 00 key name

Attached test validates the encoding I am suggesting using the PHP driver.
ExtensionEncoderTest.php

Tyler Brock

unread,
Oct 23, 2013, 12:34:09 PM10/23/13
to bs...@googlegroups.com
The example seems correct.

I'll break that part of the document down:

Outer document
Byte 0-3:   \x31\x00\x00\x00 - Length of the document 49 bytes (\x31 in hex)
Byte 4:     \x04             - First element is an Array (type 4)
Byte 5-9:   BSON \x00        - BSON e_name cstring (trailing null byte)

    Inner document (Array)
    Byte 10-13: \x26\x00\x00\x00 - Value for an array element is a document with index values keys
    Byte 14:    \x02             - First element of array is a String (type 2)
    Byte 15-16: '0'\x00          - '0' e_name cstring for the first element
        
        Value of key '0'
        Byte 17-20: \x08\x00\x00\x00 - Length of the string
        Byte 21-29: awesome\x00      - The null terminated string

    Byte 30:    \x01             - Second element of array is a double (type 1)
    Byte 31-32: '1'\x00          - '1' ename cstring for the second element
    ...

In the area of concern we need \x02 (string type) followed by the e_name which is the character '0'.

Note: This is not octal 020. (even though it may look that way)

The \x02'0' bytes are then followed by the null byte to terminate the cstring in the e_name followed by the length of the string (8 bytes) which is awesome (7 bytes) and the null terminator for the string (1 byte). This leaves you with:

type[\x02] e_name['0'\x00] string[\x08awesome\x00]

Make sense?
Reply all
Reply to author
Forward
0 new messages