<commit_before>#!/usr/bin/python

"""
Python wrapper of the botan crypto library
https://botan.randombit.net

(C) 2015,2017,2018,2019 <NAME>
(C) 2015 Uri  Blumenthal (extensions and patches)

Botan is released under the Simplified BSD License (see license.txt)

This module uses the ctypes module and is usable by programs running
under at least CPython 2.7, CPython 3.x, and PyPy

It uses botan's ffi module, which exposes a C API. This version of the
module requires FFI API version 20180713, which was introduced in
Botan 2.8

"""

from ctypes import CDLL, POINTER, byref, create_string_buffer, \
    c_void_p, c_size_t, c_uint8, c_uint32, c_uint64, c_int, c_uint, c_char_p

from sys import version_info, platform
from time import strptime, mktime, time as system_time
from binascii import hexlify
from datetime import datetime

BOTAN_FFI_VERSION = 20191214

#
# Base exception for all exceptions raised from this module
#
class BotanException(Exception):

    def __init__(self, message, rc=0):

        self.__rc = rc

        if rc == 0:
            super(BotanException, self).__init__(message)
        else:
            descr = _DLL.botan_error_description(rc).decode('ascii')
            super(BotanException, self).__init__("%s: %d (%s)" % (message, rc, descr))

    def error_code(self):
        return self.__rc

#
# Module initialization
#

def _load_botan_dll(expected_version):

    possible_dll_names = []

    if platform in ['win32', 'cygwin', 'msys']:
        possible_dll_names.append('botan-3.dll')
        possible_dll_names.append('botan.dll')
    elif platform in ['darwin', 'macos']:
        possible_dll_names.append('libbotan-3.dylib')
        possible_dll_names.append('libbotan-2.dylib')
    else:
        # assumed to be some Unix/Linux system
        possible_dll_names.append('libbotan-3.so')
        possible_dll_names += ['libbotan-3.so.%d' % (v) for v in reversed(range(0, 10))]
        possible_dll_names.append('libbotan-2.so')
        possible_dll_names += ['libbotan-2.so.%d' % (v) for v in reversed(range(13, 20))]

    for dll_name in possible_dll_names:
        try:
            dll = CDLL(dll_name)
            dll.botan_ffi_supports_api.argtypes = [c_uint32]
            dll.botan_ffi_supports_api.restype = c_int
            if dll.botan_ffi_supports_api(expected_version) == 0:
                return dll
        except OSError:
            pass

    raise BotanException("Could not find a usable Botan shared object library")

def _errcheck(rc, fn, _args):
    # This errcheck should only be used for int-returning functions
    assert isinstance(rc, int)

    if rc >= 0 or rc in fn.allowed_errors:
        return rc
    raise BotanException('%s failed' % (fn.__name__), rc)

def _set_prototypes(dll):
    # pylint: disable=too-many-statements,line-too-long
    def ffi_api(fn, args, allowed_errors=None):
        if allowed_errors is None:
            allowed_errors = [-10]
        fn.argtypes = args
        fn.restype = c_int
        fn.errcheck = _errcheck
        fn.allowed_errors = allowed_errors

    dll.botan_version_string.argtypes = []<commit_msg>Fix Python when loading a shared object without ffi module [ci skip]<commit_after>#!/usr/bin/python

"""
Python wrapper of the botan crypto library
https://botan.randombit.net

(C) 2015,2017,2018,2019 <NAME>
(C) 2015 Uri  Blumenthal (extensions and patches)

Botan is released under the Simplified BSD License (see license.txt)

This module uses the ctypes module and is usable by programs running
under at least CPython 2.7, CPython 3.x, and PyPy

It uses botan's ffi module, which exposes a C API. This version of the
module requires FFI API version 20180713, which was introduced in
Botan 2.8

"""

from ctypes import CDLL, POINTER, byref, create_string_buffer, \
    c_void_p, c_size_t, c_uint8, c_uint32, c_uint64, c_int, c_uint, c_char_p

from sys import version_info, platform
from time import strptime, mktime, time as system_time
from binascii import hexlify
from datetime import datetime

BOTAN_FFI_VERSION = 20191214

#
# Base exception for all exceptions raised from this module
#
class BotanException(Exception):

    def __init__(self, message, rc=0):

        self.__rc = rc

        if rc == 0:
            super(BotanException, self).__init__(message)
        else:
            descr = _DLL.botan_error_description(rc).decode('ascii')
            super(BotanException, self).__init__("%s: %d (%s)" % (message, rc, descr))

    def error_code(self):
        return self.__rc

#
# Module initialization
#

def _load_botan_dll(expected_version):

    possible_dll_names = []

    if platform in ['win32', 'cygwin', 'msys']:
        possible_dll_names.append('botan-3.dll')
        possible_dll_names.append('botan.dll')
    elif platform in ['darwin', 'macos']:
        possible_dll_names.append('libbotan-3.dylib')
        possible_dll_names.append('libbotan-2.dylib')
    else:
        # assumed to be some Unix/Linux system
        possible_dll_names.append('libbotan-3.so')
        possible_dll_names += ['libbotan-3.so.%d' % (v) for v in reversed(range(0, 10))]
        possible_dll_names.append('libbotan-2.so')
        possible_dll_names += ['libbotan-2.so.%d' % (v) for v in reversed(range(13, 20))]

    for dll_name in possible_dll_names:
        try:
            dll = CDLL(dll_name)
            if hasattr(dll, 'botan_ffi_supports_api'):
                dll.botan_ffi_supports_api.argtypes = [c_uint32]
                dll.botan_ffi_supports_api.restype = c_int
                if dll.botan_ffi_supports_api(expected_version) == 0:
                    return dll
        except OSError:
            pass

    raise BotanException("Could not find a usable Botan shared object library")

def _errcheck(rc, fn, _args):
    # This errcheck should only be used for int-returning functions
    assert isinstance(rc, int)

    if rc >= 0 or rc in fn.allowed_errors:
        return rc
    raise BotanException('%s failed' % (fn.__name__), rc)

def _set_prototypes(dll):
    # pylint: disable=too-many-statements,line-too-long
    def ffi_api(fn, args, allowed_errors=None):
        if allowed_errors is None:
            allowed_errors = [-10]
        fn.argtypes = args
        fn.restype = c_int
        fn.errcheck = _errcheck
        fn.allowed_errors = allowed_errors

    dll.botan_version_string.argtypes = []
{'fraction_non_alphanumeric': 0.09744560075685904, 'fraction_numerical': 0.02207505518763797, 'mean_word_length': 3.0843528654217645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517511', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2047, 'n_words': 568}