static int expand(OPENSSL_LHASH *lh)
{
    OPENSSL_LH_NODE **n, **n1, **n2, *np;
    unsigned int p, pmax, nni, j;
    unsigned long hash;
    nni = lh->num_alloc_nodes;
    p = lh->p;
    pmax = lh->pmax;
    if (p + 1 >= pmax) {
        j = nni * 2;
        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);
        if (n == NULL) {
            lh->error++;
            return 0;
        }
        lh->b = n;
        memset(n + nni, 0, sizeof(*n) * (j - nni));
        lh->pmax = nni;
        lh->num_alloc_nodes = j;
        lh->num_expand_reallocs++;
        lh->p = 0;
    } else {
        lh->p++;
    }
    lh->num_nodes++;
    lh->num_expands++;
    n1 = &(lh->b[p]);
    n2 = &(lh->b[p + pmax]);
    *n2 = NULL;
    for (np = *n1; np != NULL;) {
        hash = np->hash;
        if ((hash % nni) != p) {
            *n1 = (*n1)->next;
            np->next = *n2;
            *n2 = np;
        } else
            n1 = &((*n1)->next);
        np = *n1;
    }
    return 1;
}
['static int create_new_vfile(char *userid, char *password, const char *filename)\n{\n    char *gNid = NULL;\n    OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));\n    TXT_DB *db = NULL;\n    int ret = 0;\n    BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);\n    size_t i;\n    if (!TEST_ptr(dummy) || !TEST_ptr(row))\n        goto end;\n    gNid = SRP_create_verifier(userid, password, &row[DB_srpsalt],\n                               &row[DB_srpverifier], NULL, NULL);\n    if (!TEST_ptr(gNid))\n        goto end;\n    db = TXT_DB_read(dummy, DB_NUMBER);\n    if (!TEST_ptr(db))\n        goto end;\n    out = BIO_new_file(filename, "w");\n    if (!TEST_ptr(out))\n        goto end;\n    row[DB_srpid] = OPENSSL_strdup(userid);\n    row[DB_srptype] = OPENSSL_strdup("V");\n    row[DB_srpgN] = OPENSSL_strdup(gNid);\n    if (!TEST_ptr(row[DB_srpid])\n            || !TEST_ptr(row[DB_srptype])\n            || !TEST_ptr(row[DB_srpgN])\n            || !TEST_true(TXT_DB_insert(db, row)))\n        goto end;\n    row = NULL;\n    if (!TXT_DB_write(out, db))\n        goto end;\n    ret = 1;\n end:\n    if (row != NULL) {\n        for (i = 0; i < DB_NUMBER; i++)\n            OPENSSL_free(row[i]);\n    }\n    OPENSSL_free(row);\n    BIO_free(dummy);\n    BIO_free(out);\n    TXT_DB_free(db);\n    return ret;\n}', 'char *SRP_create_verifier(const char *user, const char *pass, char **salt,\n                          char **verifier, const char *N, const char *g)\n{\n    int len;\n    char *result = NULL, *vf = NULL;\n    const BIGNUM *N_bn = NULL, *g_bn = NULL;\n    BIGNUM *N_bn_alloc = NULL, *g_bn_alloc = NULL, *s = NULL, *v = NULL;\n    unsigned char tmp[MAX_LEN];\n    unsigned char tmp2[MAX_LEN];\n    char *defgNid = NULL;\n    int vfsize = 0;\n    if ((user == NULL) ||\n        (pass == NULL) || (salt == NULL) || (verifier == NULL))\n        goto err;\n    if (N) {\n        if ((len = t_fromb64(tmp, sizeof(tmp), N)) <= 0)\n            goto err;\n        N_bn_alloc = BN_bin2bn(tmp, len, NULL);\n        N_bn = N_bn_alloc;\n        if ((len = t_fromb64(tmp, sizeof(tmp) ,g)) <= 0)\n            goto err;\n        g_bn_alloc = BN_bin2bn(tmp, len, NULL);\n        g_bn = g_bn_alloc;\n        defgNid = "*";\n    } else {\n        SRP_gN *gN = SRP_get_gN_by_id(g, NULL);\n        if (gN == NULL)\n            goto err;\n        N_bn = gN->N;\n        g_bn = gN->g;\n        defgNid = gN->id;\n    }\n    if (*salt == NULL) {\n        if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n            goto err;\n        s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n    } else {\n        if ((len = t_fromb64(tmp2, sizeof(tmp2), *salt)) <= 0)\n            goto err;\n        s = BN_bin2bn(tmp2, len, NULL);\n    }\n    if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))\n        goto err;\n    BN_bn2bin(v, tmp);\n    vfsize = BN_num_bytes(v) * 2;\n    if (((vf = OPENSSL_malloc(vfsize)) == NULL))\n        goto err;\n    t_tob64(vf, tmp, BN_num_bytes(v));\n    if (*salt == NULL) {\n        char *tmp_salt;\n        if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {\n            goto err;\n        }\n        t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);\n        *salt = tmp_salt;\n    }\n    *verifier = vf;\n    vf = NULL;\n    result = defgNid;\n err:\n    BN_free(N_bn_alloc);\n    BN_free(g_bn_alloc);\n    OPENSSL_clear_free(vf, vfsize);\n    BN_clear_free(s);\n    BN_clear_free(v);\n    return result;\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n                           BIGNUM **verifier, const BIGNUM *N,\n                           const BIGNUM *g)\n{\n    int result = 0;\n    BIGNUM *x = NULL;\n    BN_CTX *bn_ctx = BN_CTX_new();\n    unsigned char tmp2[MAX_LEN];\n    BIGNUM *salttmp = NULL;\n    if ((user == NULL) ||\n        (pass == NULL) ||\n        (salt == NULL) ||\n        (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n        goto err;\n    if (*salt == NULL) {\n        if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n            goto err;\n        salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n    } else {\n        salttmp = *salt;\n    }\n    x = SRP_Calc_x(salttmp, user, pass);\n    *verifier = BN_new();\n    if (*verifier == NULL)\n        goto err;\n    if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n        BN_clear_free(*verifier);\n        goto err;\n    }\n    result = 1;\n    *salt = salttmp;\n err:\n    if (salt != NULL && *salt != salttmp)\n        BN_clear_free(salttmp);\n    BN_clear_free(x);\n    BN_CTX_free(bn_ctx);\n    return result;\n}', 'BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)\n{\n    unsigned char dig[SHA_DIGEST_LENGTH];\n    EVP_MD_CTX *ctxt;\n    unsigned char *cs = NULL;\n    BIGNUM *res = NULL;\n    if ((s == NULL) || (user == NULL) || (pass == NULL))\n        return NULL;\n    ctxt = EVP_MD_CTX_new();\n    if (ctxt == NULL)\n        return NULL;\n    if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)\n        goto err;\n    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)\n        || !EVP_DigestUpdate(ctxt, user, strlen(user))\n        || !EVP_DigestUpdate(ctxt, ":", 1)\n        || !EVP_DigestUpdate(ctxt, pass, strlen(pass))\n        || !EVP_DigestFinal_ex(ctxt, dig, NULL)\n        || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))\n        goto err;\n    BN_bn2bin(s, cs);\n    if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))\n        goto err;\n    if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))\n        || !EVP_DigestFinal_ex(ctxt, dig, NULL))\n        goto err;\n    res = BN_bin2bn(dig, sizeof(dig), NULL);\n err:\n    OPENSSL_free(cs);\n    EVP_MD_CTX_free(ctxt);\n    return res;\n}', 'int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)\n{\n    EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);\n#ifndef OPENSSL_NO_ENGINE\n    if (ctx->engine && ctx->digest &&\n        (type == NULL || (type->type == ctx->digest->type)))\n        goto skip_to_init;\n    if (type) {\n        ENGINE_finish(ctx->engine);\n        if (impl != NULL) {\n            if (!ENGINE_init(impl)) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                return 0;\n            }\n        } else {\n            impl = ENGINE_get_digest_engine(type->type);\n        }\n        if (impl != NULL) {\n            const EVP_MD *d = ENGINE_get_digest(impl, type->type);\n            if (d == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_INITIALIZATION_ERROR);\n                ENGINE_finish(impl);\n                return 0;\n            }\n            type = d;\n            ctx->engine = impl;\n        } else\n            ctx->engine = NULL;\n    } else {\n        if (!ctx->digest) {\n            EVPerr(EVP_F_EVP_DIGESTINIT_EX, EVP_R_NO_DIGEST_SET);\n            return 0;\n        }\n        type = ctx->digest;\n    }\n#endif\n    if (ctx->digest != type) {\n        if (ctx->digest && ctx->digest->ctx_size) {\n            OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size);\n            ctx->md_data = NULL;\n        }\n        ctx->digest = type;\n        if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {\n            ctx->update = type->update;\n            ctx->md_data = OPENSSL_zalloc(type->ctx_size);\n            if (ctx->md_data == NULL) {\n                EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);\n                return 0;\n            }\n        }\n    }\n#ifndef OPENSSL_NO_ENGINE\n skip_to_init:\n#endif\n    if (ctx->pctx) {\n        int r;\n        r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,\n                              EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);\n        if (r <= 0 && (r != -2))\n            return 0;\n    }\n    if (ctx->flags & EVP_MD_CTX_FLAG_NO_INIT)\n        return 1;\n    return ctx->digest->init(ctx);\n}', 'ENGINE *ENGINE_get_digest_engine(int nid)\n{\n    return engine_table_select(&digest_table, nid);\n}', 'ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)\n#else\nENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,\n                                int l)\n#endif\n{\n    ENGINE *ret = NULL;\n    ENGINE_PILE tmplate, *fnd = NULL;\n    int initres, loop = 0;\n    if (!(*table)) {\n#ifdef ENGINE_TABLE_DEBUG\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "\n                "registered!\\n", f, l, nid);\n#endif\n        return NULL;\n    }\n    ERR_set_mark();\n    CRYPTO_THREAD_write_lock(global_engine_lock);\n    if (!int_table_check(table, 0))\n        goto end;\n    tmplate.nid = nid;\n    fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);\n    if (!fnd)\n        goto end;\n    if (fnd->funct && engine_unlocked_init(fnd->funct)) {\n#ifdef ENGINE_TABLE_DEBUG\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n                "ENGINE \'%s\' cached\\n", f, l, nid, fnd->funct->id);\n#endif\n        ret = fnd->funct;\n        goto end;\n    }\n    if (fnd->uptodate) {\n        ret = fnd->funct;\n        goto end;\n    }\n trynext:\n    ret = sk_ENGINE_value(fnd->sk, loop++);\n    if (!ret) {\n#ifdef ENGINE_TABLE_DEBUG\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "\n                "registered implementations would initialise\\n", f, l, nid);\n#endif\n        goto end;\n    }\n    if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))\n        initres = engine_unlocked_init(ret);\n    else\n        initres = 0;\n    if (initres) {\n        if ((fnd->funct != ret) && engine_unlocked_init(ret)) {\n            if (fnd->funct)\n                engine_unlocked_finish(fnd->funct, 0);\n            fnd->funct = ret;\n#ifdef ENGINE_TABLE_DEBUG\n            fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "\n                    "setting default to \'%s\'\\n", f, l, nid, ret->id);\n#endif\n        }\n#ifdef ENGINE_TABLE_DEBUG\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "\n                "newly initialised \'%s\'\\n", f, l, nid, ret->id);\n#endif\n        goto end;\n    }\n    goto trynext;\n end:\n    if (fnd)\n        fnd->uptodate = 1;\n#ifdef ENGINE_TABLE_DEBUG\n    if (ret)\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n                "ENGINE \'%s\'\\n", f, l, nid, ret->id);\n    else\n        fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "\n                "\'no matching ENGINE\'\\n", f, l, nid);\n#endif\n    CRYPTO_THREAD_unlock(global_engine_lock);\n    ERR_pop_to_mark();\n    return ret;\n}', 'static int int_table_check(ENGINE_TABLE **t, int create)\n{\n    LHASH_OF(ENGINE_PILE) *lh;\n    if (*t)\n        return 1;\n    if (!create)\n        return 0;\n    if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)\n        return 0;\n    *t = (ENGINE_TABLE *)lh;\n    return 1;\n}', 'DEFINE_LHASH_OF(ENGINE_PILE)', 'OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)\n{\n    OPENSSL_LHASH *ret;\n    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n        return NULL;\n    }\n    if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n        goto err;\n    ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);\n    ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);\n    ret->num_nodes = MIN_NODES / 2;\n    ret->num_alloc_nodes = MIN_NODES;\n    ret->pmax = MIN_NODES / 2;\n    ret->up_load = UP_LOAD;\n    ret->down_load = DOWN_LOAD;\n    return ret;\nerr:\n    OPENSSL_free(ret->b);\n    OPENSSL_free(ret);\n    return NULL;\n}', 'int TXT_DB_insert(TXT_DB *db, OPENSSL_STRING *row)\n{\n    int i;\n    OPENSSL_STRING *r;\n    for (i = 0; i < db->num_fields; i++) {\n        if (db->index[i] != NULL) {\n            if ((db->qual[i] != NULL) && (db->qual[i] (row) == 0))\n                continue;\n            r = lh_OPENSSL_STRING_retrieve(db->index[i], row);\n            if (r != NULL) {\n                db->error = DB_ERROR_INDEX_CLASH;\n                db->arg1 = i;\n                db->arg_row = r;\n                goto err;\n            }\n        }\n    }\n    for (i = 0; i < db->num_fields; i++) {\n        if (db->index[i] != NULL) {\n            if ((db->qual[i] != NULL) && (db->qual[i] (row) == 0))\n                continue;\n            (void)lh_OPENSSL_STRING_insert(db->index[i], row);\n            if (lh_OPENSSL_STRING_retrieve(db->index[i], row) == NULL)\n                goto err1;\n        }\n    }\n    if (!sk_OPENSSL_PSTRING_push(db->data, row))\n        goto err1;\n    return 1;\n err1:\n    db->error = DB_ERROR_MALLOC;\n    while (i-- > 0) {\n        if (db->index[i] != NULL) {\n            if ((db->qual[i] != NULL) && (db->qual[i] (row) == 0))\n                continue;\n            (void)lh_OPENSSL_STRING_delete(db->index[i], row);\n        }\n    }\n err:\n    return 0;\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n    unsigned long hash;\n    OPENSSL_LH_NODE *nn, **rn;\n    void *ret;\n    lh->error = 0;\n    if ((lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)) && !expand(lh))\n        return NULL;\n    rn = getrn(lh, data, &hash);\n    if (*rn == NULL) {\n        if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n            lh->error++;\n            return NULL;\n        }\n        nn->data = data;\n        nn->next = NULL;\n        nn->hash = hash;\n        *rn = nn;\n        ret = NULL;\n        lh->num_insert++;\n        lh->num_items++;\n    } else {\n        ret = (*rn)->data;\n        (*rn)->data = data;\n        lh->num_replace++;\n    }\n    return ret;\n}', 'static int expand(OPENSSL_LHASH *lh)\n{\n    OPENSSL_LH_NODE **n, **n1, **n2, *np;\n    unsigned int p, pmax, nni, j;\n    unsigned long hash;\n    nni = lh->num_alloc_nodes;\n    p = lh->p;\n    pmax = lh->pmax;\n    if (p + 1 >= pmax) {\n        j = nni * 2;\n        n = OPENSSL_realloc(lh->b, sizeof(OPENSSL_LH_NODE *) * j);\n        if (n == NULL) {\n            lh->error++;\n            return 0;\n        }\n        lh->b = n;\n        memset(n + nni, 0, sizeof(*n) * (j - nni));\n        lh->pmax = nni;\n        lh->num_alloc_nodes = j;\n        lh->num_expand_reallocs++;\n        lh->p = 0;\n    } else {\n        lh->p++;\n    }\n    lh->num_nodes++;\n    lh->num_expands++;\n    n1 = &(lh->b[p]);\n    n2 = &(lh->b[p + pmax]);\n    *n2 = NULL;\n    for (np = *n1; np != NULL;) {\n        hash = np->hash;\n        if ((hash % nni) != p) {\n            *n1 = (*n1)->next;\n            np->next = *n2;\n            *n2 = np;\n        } else\n            n1 = &((*n1)->next);\n        np = *n1;\n    }\n    return 1;\n}']