<commit_before><?php

if (!function_exists('random_bytes')) {
    /**
     * PHP 5.2.0 - 5.6.x way to implement random_bytes()
     * 
     * We use conditional statements here to define the function in accordance
     * to the operating environment. It's a micro-optimization.
     * 
     * In order of preference:
     *   1. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
     *   2. fread() /dev/arandom if available
     *   3. fread() /dev/urandom if available
     *   4. COM('CAPICOM.Utilities.1')->GetRandom()
     *   5. openssl_random_pseudo_bytes()
     * 
     * See ERRATA.md for our reasoning behind this particular order
     */
    if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) {
        /**
         * Powered by ext/mcrypt (and thankfully NOT libmcrypt)
         * 
         * @ref https://bugs.php.net/bug.php?id=55169
         * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
         * 
         * @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'
                );
            }
            
            $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
            if ($buf !== false) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    /**
                     * Return our random entropy buffer here:
                     */
                    return $buf;
                }
            }
            /**
             * If we reach here, PHP has failed us.
             */
            throw new Exception(
                'PHP failed to generate random data.'
            );
        }
    } elseif (
        !ini_get('open_basedir') && 
        (
            is_readable('/dev/arandom') || is_readable('/dev/urandom')
        )
    ) {
        /**
         * Unless open_basedir is enabled, use /dev/arandom or /dev/urandom for
         * random numbers in accordance with best practices
         * 
         * @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) {
                /**
                 * If we don't set the stream's read buffer to 0, PHP will
                 * internally buffer 8192 bytes, which can waste entropy
                 * 
                 * stream_set_read_buffer returns 0 on success
                 */
                $streamset = stream_set_read_buffer($fp, 0);
                if ($streamset === 0) {
                    $remaining = $bytes;
                    $buf = '';
                    /**
                     * We use fread() in a loop to protect against partial reads
                     */
                    do {
                        $read = fread($fp, $remaining); 
                        if ($read === false) {
                            /**
                             * We cannot safely read from the file. Exit the
                             * do-while loop and trigger the exception condition
                             */
                            $buf = false;
                            break;
                        }
                        /**
                         * Decrease the number of bytes returned from remaining
                         */
                        $remaining -= RandomCompat_strlen($read);
                        $buf .= $read;
                    } while ($remaining > 0);
                    
                    /**
                     * Is our result valid?
                     */
                    if ($buf !== false) {
                        if (RandomCompat_strlen($buf) === $bytes) {
                            /**
                             * Return our random entropy buffer here:
                             */
                            return $buf;
                        }
                    }
                }
            }
            /**
             * If we reach here, PHP has failed us.
             */
            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<commit_msg>I had misread @lt and @sammyk's recommendation<commit_after><?php

if (!function_exists('random_bytes')) {
    /**
     * PHP 5.2.0 - 5.6.x way to implement random_bytes()
     * 
     * We use conditional statements here to define the function in accordance
     * to the operating environment. It's a micro-optimization.
     * 
     * In order of preference:
     *   1. fread() /dev/arandom if available
     *   2. fread() /dev/urandom if available
     *   3. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV)
     *   4. COM('CAPICOM.Utilities.1')->GetRandom()
     *   5. openssl_random_pseudo_bytes()
     * 
     * See ERRATA.md for our reasoning behind this particular order
     */
     if (
        !ini_get('open_basedir') && 
        (
            is_readable('/dev/arandom') || is_readable('/dev/urandom')
        )
    ) {
        /**
         * Unless open_basedir is enabled, use /dev/arandom or /dev/urandom for
         * random numbers in accordance with best practices
         * 
         * @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) {
                /**
                 * If we don't set the stream's read buffer to 0, PHP will
                 * internally buffer 8192 bytes, which can waste entropy
                 * 
                 * stream_set_read_buffer returns 0 on success
                 */
                $streamset = stream_set_read_buffer($fp, 0);
                if ($streamset === 0) {
                    $remaining = $bytes;
                    $buf = '';
                    /**
                     * We use fread() in a loop to protect against partial reads
                     */
                    do {
                        $read = fread($fp, $remaining); 
                        if ($read === false) {
                            /**
                             * We cannot safely read from the file. Exit the
                             * do-while loop and trigger the exception condition
                             */
                            $buf = false;
                            break;
                        }
                        /**
                         * Decrease the number of bytes returned from remaining
                         */
                        $remaining -= RandomCompat_strlen($read);
                        $buf .= $read;
                    } while ($remaining > 0);
                    
                    /**
                     * Is our result valid?
                     */
                    if ($buf !== false) {
                        if (RandomCompat_strlen($buf) === $bytes) {
                            /**
                             * Return our random entropy buffer here:
                             */
                            return $buf;
                        }
                    }
                }
            }
            /**
             * If we reach here, PHP has failed us.
             */
            throw new Exception(
                'PHP failed to generate random data.'
            );
        }
    } else
    if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) {
        /**
         * Powered by ext/mcrypt (and thankfully NOT libmcrypt)
         * 
         * @ref https://bugs.php.net/bug.php?id=55169
         * @ref https://github.com/php/php-src/blob/c568ffe5171d942161fc8dda066bce844bdef676/ext/mcrypt/mcrypt.c#L1321-L1386
         * 
         * @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'
                );
            }
            
            $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM);
            if ($buf !== false) {
                if (RandomCompat_strlen($buf) === $bytes) {
                    /**
                     * Return our random entropy buffer here:
                     */
                    return $buf;
                }
            }
            /**
             * If we reach here, PHP has failed us.
             */
            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
{'fraction_non_alphanumeric': 0.10031253150519205, 'fraction_numerical': 0.01471922572840004, 'mean_word_length': 0.9883744237322108, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, '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': '6149610', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2408, 'n_words': 640}