예제 #1
0
 def _encode_bytes(name, value, dummy0, dummy1):
     """Encode a python str (python 2.x)."""
     try:
         _utf_8_decode(value, None, True)
     except UnicodeError:
         raise InvalidStringData("strings in documents must be valid " "UTF-8: %r" % (value,))
     return b"\x02" + name + _PACK_INT(len(value) + 1) + value + b"\x00"
예제 #2
0
 def _encode_bytes(name, value, dummy0, dummy1):
     """Encode a python str (python 2.x)."""
     try:
         _utf_8_decode(value, None, True)
     except UnicodeError:
         raise InvalidStringData("strings in documents must be valid "
                                 "UTF-8: %r" % (value, ))
     return b"\x02" + name + _PACK_INT(len(value) + 1) + value + b"\x00"
예제 #3
0
def _make_c_string(string: Union[str, bytes]) -> bytes:
    """Make a 'C' string."""
    if isinstance(string, bytes):
        try:
            _utf_8_decode(string, None, True)
            return string + b"\x00"
        except UnicodeError:
            raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string)
    else:
        return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
예제 #4
0
def _make_c_string(string):
    """Make a 'C' string."""
    if isinstance(string, bytes):
        try:
            _utf_8_decode(string, None, True)
            return string + b"\x00"
        except UnicodeError:
            raise InvalidStringData("strings in documents must be valid " "UTF-8: %r" % string)
    else:
        return _utf_8_encode(string)[0] + b"\x00"
예제 #5
0
def _make_c_string_check(string: Union[str, bytes]) -> bytes:
    """Make a 'C' string, checking for embedded NUL characters."""
    if isinstance(string, bytes):
        if b"\x00" in string:
            raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character")
        try:
            _utf_8_decode(string, None, True)
            return string + b"\x00"
        except UnicodeError:
            raise InvalidStringData("strings in documents must be valid UTF-8: %r" % string)
    else:
        if "\x00" in string:
            raise InvalidDocument("BSON keys / regex patterns must not contain a NUL character")
        return cast(bytes, _utf_8_encode(string)[0]) + b"\x00"
예제 #6
0
def _make_c_string_check(string):
    """Make a 'C' string, checking for embedded NUL characters."""
    if isinstance(string, bytes):
        if b"\x00" in string:
            raise InvalidDocument("BSON keys / regex patterns must not " "contain a NUL character")
        try:
            _utf_8_decode(string, None, True)
            return string + b"\x00"
        except UnicodeError:
            raise InvalidStringData("strings in documents must be valid " "UTF-8: %r" % string)
    else:
        if "\x00" in string:
            raise InvalidDocument("BSON keys / regex patterns must not " "contain a NUL character")
        return _utf_8_encode(string)[0] + b"\x00"
def _get_c_string(data, position, opts):
    """Decode a BSON 'C' string to python unicode string."""
    if opts.use_unicode:
        end = data.index(b"\x00", position)
        return _utf_8_decode(data[position:end],
                             opts.unicode_decode_error_handler,
                             True)[0], end + 1
    else:
        return data[position:], len(data) - position
예제 #8
0
def _get_string(data, position, obj_end, dummy):
    """Decode a BSON string to python unicode string."""
    length = _UNPACK_INT(data[position:position + 4])[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    end = position + length - 1
    if data[end:end + 1] != b"\x00":
        raise InvalidBSON("invalid end of string")
    return _utf_8_decode(data[position:end], None, True)[0], end + 1
예제 #9
0
파일: __init__.py 프로젝트: songjundev/b
def _get_string(data, position, obj_end, dummy):
    """Decode a BSON string to python unicode string."""
    length = _UNPACK_INT(data[position:position + 4])[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    end = position + length - 1
    if data[end:end + 1] != b"\x00":
        raise InvalidBSON("invalid end of string")
    return _utf_8_decode(data[position:end], None, True)[0], end + 1
예제 #10
0
def _get_string(data, view, position, obj_end, opts, dummy):
    """Decode a BSON string to python unicode string."""
    length = _UNPACK_INT_FROM(data, position)[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    end = position + length - 1
    if data[end] != 0:
        raise InvalidBSON("invalid end of string")
    return _utf_8_decode(view[position:end], opts.unicode_decode_error_handler,
                         True)[0], end + 1
예제 #11
0
def _get_string(
    data: Any, view: Any, position: int, obj_end: int, opts: CodecOptions, dummy: Any
) -> Tuple[str, int]:
    """Decode a BSON string to python str."""
    length = _UNPACK_INT_FROM(data, position)[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    end = position + length - 1
    if data[end] != 0:
        raise InvalidBSON("invalid end of string")
    return _utf_8_decode(view[position:end], opts.unicode_decode_error_handler, True)[0], end + 1
def _get_string(data, position, obj_end, opts, dummy):
    """Decode a BSON string to python unicode string."""
    length = _UNPACK_INT(data[position:position + 4])[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    if opts.use_unicode:
        end = position + length - 1
        if data[end:end + 1] != b"\x00":
            raise InvalidBSON("invalid end of string")
        return _utf_8_decode(data[position:end],
                             opts.unicode_decode_error_handler,
                             True)[0], end + 1
    else:
        return data[position:], len(data) - position
예제 #13
0
def _get_c_string(data, view, position, opts):
    """Decode a BSON 'C' string to python unicode string."""
    end = data.index(b"\x00", position)
    return _utf_8_decode(view[position:end], opts.unicode_decode_error_handler,
                         True)[0], end + 1
예제 #14
0
파일: __init__.py 프로젝트: songjundev/b
def _get_c_string(data, position):
    """Decode a BSON 'C' string to python unicode string."""
    end = data.index(b"\x00", position)
    return _utf_8_decode(data[position:end], None, True)[0], end + 1
예제 #15
0
def _get_c_string(data, position):
    """Decode a BSON 'C' string to python unicode string."""
    end = data.index(b"\x00", position)
    return _utf_8_decode(data[position:end], None, True)[0], end + 1
예제 #16
0
def _get_c_string(data, position, opts):
    """Decode a BSON 'C' string to python unicode string."""
    end = data.index(b"\x00", position)
    return _utf_8_decode(data[position:end],
                         opts.unicode_decode_error_handler, True)[0], end + 1
예제 #17
0
def _get_c_string(data: Any, view: Any, position: int, opts: CodecOptions) -> Tuple[str, int]:
    """Decode a BSON 'C' string to python str."""
    end = data.index(b"\x00", position)
    return _utf_8_decode(view[position:end], opts.unicode_decode_error_handler, True)[0], end + 1