Status validateBSONInternal( Buffer* buffer, int* bsonLength ) {
            const int start = buffer->position();

            int supposedSize;
            if ( !buffer->readNumber<int>(&supposedSize) )
                return Status( ErrorCodes::InvalidBSON, "bson size is larger than buffer size" );

            Status status = Status::OK();

            int sz;

            while ( true ) {
                char type;
                if ( !buffer->readNumber<char>(&type) )
                    return Status( ErrorCodes::InvalidBSON, "invalid bson" );

                if ( type == EOO )
                    break;

                StringData name;
                status = buffer->readCString( &name );
                if ( !status.isOK() )
                    return status;

                switch ( type ) {
                case MinKey:
                case MaxKey:
                case jstNULL:
                case Undefined:
                    break;

                case jstOID:
                    if ( !buffer->skip( sizeof(OID) ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    break;

                case NumberInt:
                    if ( !buffer->skip( sizeof(int32_t) ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    break;

                case Bool:
                    if ( !buffer->skip( sizeof(int8_t) ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    break;


                case NumberDouble:
                case NumberLong:
                case Timestamp:
                case Date:
                    if ( !buffer->skip( sizeof(int64_t) ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    break;

                case DBRef:
                    status = buffer->readUTF8String( NULL );
                    if ( !status.isOK() )
                        return status;
                    buffer->skip( sizeof(OID) );
                    break;

                case CodeWScope:
                    if ( !buffer->readNumber<int>( &sz ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );

                    status = buffer->readUTF8String( NULL );
                    if ( !status.isOK() )
                        return status;

                    status = validateBSONInternal( buffer, NULL );
                    if ( !status.isOK() )
                        return status;

                    break;

                case RegEx:
                    status = buffer->readCString( NULL );
                    if ( !status.isOK() )
                        return status;
                    status = buffer->readCString( NULL );
                    if ( !status.isOK() )
                        return status;

                    break;

                case Code:
                case Symbol:
                case String:
                    status = buffer->readUTF8String( NULL );
                    if ( !status.isOK() )
                        return status;
                    break;

                case BinData:
                    if ( !buffer->readNumber<int>( &sz ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    if ( !buffer->skip( 1 + sz ) )
                        return Status( ErrorCodes::InvalidBSON, "invalid bson" );
                    break;

                case Object:
                case Array:
                    status = validateBSONInternal( buffer, NULL );
                    if ( !status.isOK() )
                        return status;
                    break;

                default:
                    return Status( ErrorCodes::InvalidBSON, "invalid bson type" );
                }
            }

            const int end = buffer->position();

            if ( end - start != supposedSize )
                return Status( ErrorCodes::InvalidBSON, "bson length doesn't match what we found" );

            if ( bsonLength )
                *bsonLength = supposedSize;

            return Status::OK();
        }