def varint_to_buffer(value: int, buffer: bytearray, offset: int, size: int) -> int:
    """
    Set integer value into variable data buffer with range [offset, offset + size)

    :param value:  int value
    :param buffer: data buffer
    :param offset: data view offset
    :param size:   data view size
    :return: data length
    """
    pos = offset
    end = offset + size
    while value > 0x7F:
        if pos >= end:
            raise OverflowError('out of range: [%d, %d), value=%d' % (offset, end, value))
        buffer[pos] = (value & 0x7F) | 0x80
        value >>= 7
        pos += 1
    if pos >= end:
        raise OverflowError('out of range: [%d, %d), value=%d' % (offset, end, value))
    buffer[pos] = (value & 0x7F)
    return pos - offset + 1