<commit_before>/*
 * Copyright 2017-2020 AVSystem <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <avs_commons_init.h>

#if defined(AVS_COMMONS_WITH_AVS_CRYPTO)           \
        && (defined(AVS_UNIT_TESTING)              \
            || (!defined(AVS_COMMONS_WITH_MBEDTLS) \
                && !defined(AVS_COMMONS_WITH_OPENSSL)))

#    define MODULE_NAME avs_crypto_prng
#    include <avs_x_log_config.h>

#    include <avsystem/commons/avs_memory.h>
#    include <avsystem/commons/avs_prng.h>
#    include <avsystem/commons/avs_time.h>
#    include <avsystem/commons/avs_utils.h>

VISIBILITY_SOURCE_BEGIN

struct avs_crypto_prng_ctx_struct {
    avs_rand_seed_t seed;
};

int seed_callback(unsigned char *out_buf, size_t out_buf_len, void *user_ptr) {
    (void) user_ptr;
    AVS_STATIC_ASSERT(sizeof(avs_rand_seed_t) == sizeof(uint32_t),
                      seed_size_does_not_match);
    uint32_t seed = (avs_time_real_now().since_real_epoch.seconds
                     ^ avs_time_real_now().since_real_epoch.nanoseconds)
                    % UINT32_MAX;
    memcpy(out_buf, (unsigned char *) &seed, out_buf_len);
    return 0;
}

avs_crypto_prng_ctx_t *avs_crypto_prng_new(avs_prng_entropy_callback_t seed_cb,
                                           void *user_ptr) {
    avs_crypto_prng_ctx_t *ctx =
            (avs_crypto_prng_ctx_t *) avs_malloc(sizeof(avs_crypto_prng_ctx_t));

    if (!seed_cb) {
        seed_cb = seed_callback;<commit_msg>Add static<commit_after>/*
 * Copyright 2017-2020 AVSystem <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <avs_commons_init.h>

#if defined(AVS_COMMONS_WITH_AVS_CRYPTO)           \
        && (defined(AVS_UNIT_TESTING)              \
            || (!defined(AVS_COMMONS_WITH_MBEDTLS) \
                && !defined(AVS_COMMONS_WITH_OPENSSL)))

#    define MODULE_NAME avs_crypto_prng
#    include <avs_x_log_config.h>

#    include <avsystem/commons/avs_memory.h>
#    include <avsystem/commons/avs_prng.h>
#    include <avsystem/commons/avs_time.h>
#    include <avsystem/commons/avs_utils.h>

VISIBILITY_SOURCE_BEGIN

struct avs_crypto_prng_ctx_struct {
    avs_rand_seed_t seed;
};

static int
seed_callback(unsigned char *out_buf, size_t out_buf_len, void *user_ptr) {
    (void) user_ptr;
    AVS_STATIC_ASSERT(sizeof(avs_rand_seed_t) == sizeof(uint32_t),
                      seed_size_does_not_match);
    uint32_t seed = (avs_time_real_now().since_real_epoch.seconds
                     ^ avs_time_real_now().since_real_epoch.nanoseconds)
                    % UINT32_MAX;
    memcpy(out_buf, (unsigned char *) &seed, out_buf_len);
    return 0;
}

avs_crypto_prng_ctx_t *avs_crypto_prng_new(avs_prng_entropy_callback_t seed_cb,
                                           void *user_ptr) {
    avs_crypto_prng_ctx_t *ctx =
            (avs_crypto_prng_ctx_t *) avs_malloc(sizeof(avs_crypto_prng_ctx_t));

    if (!seed_cb) {
        seed_cb = seed_callback;
{'fraction_non_alphanumeric': 0.0885, 'fraction_numerical': 0.0095, 'mean_word_length': 3.128998968008256, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2749163', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1332, 'n_words': 293}