<commit_before><?php

if (!function_exists('random_bytes')) {
    /**
     * PHP 5.2.0 - 5.6.x way to implement random_bytes()
     */
    if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) {
        /**
         * Powered by ext/mcrypt
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            if (!is_int($bytes)) {
                 throw new Exception('Length must be an integer');
            }
            if ($bytes < 1) {
                 throw new Exception('Length must be greater than 0');
            }
            // See PHP bug #55169 for why 5.3.7 is required
            $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
            if ($buf !== false) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    return $buf;
                }
            }
            // If we failed, throw an exception.
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (is_readable('/dev/arandom') || is_readable('/dev/urandom') && !ini_get('open_basedir')) {
        /**
         * Use /dev/arandom or /dev/urandom for random numbers
         * 
         * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            static $fp = null;
            if ($fp === null) {
                if (is_readable('/dev/arandom')) {
                    $fp = fopen('/dev/arandom', 'rb');
                } else {
                    $fp = fopen('/dev/urandom', 'rb');
                }
            }
            if ($fp !== false) {
                $streamset = stream_set_read_buffer($fp, 0);
                if ($streamset === 0) {
                    $remaining = $bytes;
                    $buf = '';
                    do {
                        $read = fread($fp, $remaining); 
                        if ($read === false) {
                            // We cannot safely read from urandom.
                            $buf = false;
                            break;
                        }
                        // Decrease the number of bytes returned from remaining
                        $remaining -= RandomCompat_strlen($read);
                        $buf .= $read;
                    } while ($remaining > 0);
                    if ($buf !== false) {
                        if (RandomCompat_strlen($buf) === $bytes) {
                            return $buf;
                        }
                    }
                }
            }
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (extension_loaded('com_dotnet')) {
        /**
         * Windows with PHP < 5.3.0 will not have the function
         * openssl_random_pseudo_bytes() available, so let's use
         * CAPICOM to work around this deficiency.
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
             try {
                $buf = '';
                $util = new COM('CAPICOM.Utilities.1');
                $execs = 0;
                /**
                 * Let's not let it loop forever. If we run N times and fail to
                 * get N bytes of random data, then CAPICOM has failed us.
                 */
                do {
                    $buf .= base64_decode($util->GetRandom($bytes, 0));
                    if (RandomCompat_strlen($buf) >= $bytes) {
                        return RandomCompat_substr($buf, 0, $bytes);
                    }
                    ++$execs; 
                } while ($execs < $bytes);
            } catch (Exception $e) {
                unset($e); // Let's not let CAPICOM errors kill our app 
            }
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (function_exists('openssl_random_pseudo_bytes')) {
        /**
         * Since openssl_random_pseudo_bytes() uses openssl's 
         * RAND_pseudo_bytes() API, which has been marked as deprecated by the
         * OpenSSL team, this is our last resort before failure.
         * 
         * @ref https://www.openssl.org/docs/crypto/RAND_bytes.html
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            $secure = true;
            $buf = openssl_random_pseudo_bytes($bytes, $secure);
            if ($buf !== false && $secure) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    return $buf;
                }
            }
        }
    } else {
        throw new Exception('There is no suitable CSPRNG installed on your system');
    }
}

if (!function_exists('random_int')) {
    /**
     * Fetch a random integer between $min and $max inclusive
     * 
     * @param int $min
     * @param int $max
     * 
     * @return int
     */
    function random_int($min, $max)
    {
        if (!is_int($min)) {
             throw new Exception('random_int(): $min must be an integer');
        }
        if (!is_int($max)) {
             throw new Exception('random_int(): $max must be an integer');
        }
        if ($min > $max) {
             throw new Exception('Minimum value must be less than or equal to the maximum value');
        }
        $range = $max - $min + 1;
        
        /**
         * Do we have a meaningful range? If not, return the minimum value.
         */
        if ($range < 1) {
            return $min;
        }

        /**
         * Test for integer overflow:
         */
        if (!is_int($range)) {
            /**
             * Still safely calculate wider ranges.
             * Provided by @CodesInChaos
             * 
             * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
             * 
             * $reject is equivalent to (PHP_INT_MAX + 1) % range, but avoids
             *      int overflows (assuming PHP_INT_MAX + 1 is a power-of-two<commit_msg>More explicitly compare $min and $max<commit_after><?php

if (!function_exists('random_bytes')) {
    /**
     * PHP 5.2.0 - 5.6.x way to implement random_bytes()
     */
    if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) {
        /**
         * Powered by ext/mcrypt
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            if (!is_int($bytes)) {
                 throw new Exception('Length must be an integer');
            }
            if ($bytes < 1) {
                 throw new Exception('Length must be greater than 0');
            }
            // See PHP bug #55169 for why 5.3.7 is required
            $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
            if ($buf !== false) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    return $buf;
                }
            }
            // If we failed, throw an exception.
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (is_readable('/dev/arandom') || is_readable('/dev/urandom') && !ini_get('open_basedir')) {
        /**
         * Use /dev/arandom or /dev/urandom for random numbers
         * 
         * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            static $fp = null;
            if ($fp === null) {
                if (is_readable('/dev/arandom')) {
                    $fp = fopen('/dev/arandom', 'rb');
                } else {
                    $fp = fopen('/dev/urandom', 'rb');
                }
            }
            if ($fp !== false) {
                $streamset = stream_set_read_buffer($fp, 0);
                if ($streamset === 0) {
                    $remaining = $bytes;
                    $buf = '';
                    do {
                        $read = fread($fp, $remaining); 
                        if ($read === false) {
                            // We cannot safely read from urandom.
                            $buf = false;
                            break;
                        }
                        // Decrease the number of bytes returned from remaining
                        $remaining -= RandomCompat_strlen($read);
                        $buf .= $read;
                    } while ($remaining > 0);
                    if ($buf !== false) {
                        if (RandomCompat_strlen($buf) === $bytes) {
                            return $buf;
                        }
                    }
                }
            }
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (extension_loaded('com_dotnet')) {
        /**
         * Windows with PHP < 5.3.0 will not have the function
         * openssl_random_pseudo_bytes() available, so let's use
         * CAPICOM to work around this deficiency.
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
             try {
                $buf = '';
                $util = new COM('CAPICOM.Utilities.1');
                $execs = 0;
                /**
                 * Let's not let it loop forever. If we run N times and fail to
                 * get N bytes of random data, then CAPICOM has failed us.
                 */
                do {
                    $buf .= base64_decode($util->GetRandom($bytes, 0));
                    if (RandomCompat_strlen($buf) >= $bytes) {
                        return RandomCompat_substr($buf, 0, $bytes);
                    }
                    ++$execs; 
                } while ($execs < $bytes);
            } catch (Exception $e) {
                unset($e); // Let's not let CAPICOM errors kill our app 
            }
            throw new Exception('PHP failed to generate random data.');
        }
    } elseif (function_exists('openssl_random_pseudo_bytes')) {
        /**
         * Since openssl_random_pseudo_bytes() uses openssl's 
         * RAND_pseudo_bytes() API, which has been marked as deprecated by the
         * OpenSSL team, this is our last resort before failure.
         * 
         * @ref https://www.openssl.org/docs/crypto/RAND_bytes.html
         * 
         * @param int $bytes
         * @return string
         */
        function random_bytes($bytes)
        {
            $secure = true;
            $buf = openssl_random_pseudo_bytes($bytes, $secure);
            if ($buf !== false && $secure) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    return $buf;
                }
            }
        }
    } else {
        throw new Exception('There is no suitable CSPRNG installed on your system');
    }
}

if (!function_exists('random_int')) {
    /**
     * Fetch a random integer between $min and $max inclusive
     * 
     * @param int $min
     * @param int $max
     * 
     * @return int
     */
    function random_int($min, $max)
    {
        if (!is_int($min)) {
             throw new Exception('random_int(): $min must be an integer');
        }
        if (!is_int($max)) {
             throw new Exception('random_int(): $max must be an integer');
        }
        if ($min > $max) {
             throw new Exception('Minimum value must be less than or equal to the maximum value');
        }
        if ($max === $min) {
            return $min;
        }
        $range = $max - $min + 1;

        /**
         * Test for integer overflow:
         */
        if (!is_int($range)) {
            /**
             * Still safely calculate wider ranges.
             * Provided by @CodesInChaos
             * 
             * @ref https://gist.github.com/CodesInChaos/03f9ea0b58e8b2b8d435
             * 
             * $reject is equivalent to (PHP_INT_MAX + 1) % range, but avoids
             *      int overflows (assuming PHP_INT_MAX + 1 is a power-of-two
{'fraction_non_alphanumeric': 0.11451006052674628, 'fraction_numerical': 0.00891542614101096, 'mean_word_length': 1.2275460010930952, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6667651', 'n_tokens_mistral': 3370, 'n_tokens_neox': 3180, 'n_words': 878}