int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
                                   int purpose, int trust)
{
    int idx;
    if (!purpose)
        purpose = def_purpose;
    if (purpose) {
        X509_PURPOSE *ptmp;
        idx = X509_PURPOSE_get_by_id(purpose);
        if (idx == -1) {
            X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                    X509_R_UNKNOWN_PURPOSE_ID);
            return 0;
        }
        ptmp = X509_PURPOSE_get0(idx);
        if (ptmp->trust == X509_TRUST_DEFAULT) {
            idx = X509_PURPOSE_get_by_id(def_purpose);
            if (idx == -1) {
                X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                        X509_R_UNKNOWN_PURPOSE_ID);
                return 0;
            }
            ptmp = X509_PURPOSE_get0(idx);
        }
        if (!trust)
            trust = ptmp->trust;
    }
    if (trust) {
        idx = X509_TRUST_get_by_id(trust);
        if (idx == -1) {
            X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                    X509_R_UNKNOWN_TRUST_ID);
            return 0;
        }
    }
    if (purpose && !ctx->param->purpose)
        ctx->param->purpose = purpose;
    if (trust && !ctx->param->trust)
        ctx->param->trust = trust;
    return 1;
}
['int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,\n                                   int purpose, int trust)\n{\n    int idx;\n    if (!purpose)\n        purpose = def_purpose;\n    if (purpose) {\n        X509_PURPOSE *ptmp;\n        idx = X509_PURPOSE_get_by_id(purpose);\n        if (idx == -1) {\n            X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n                    X509_R_UNKNOWN_PURPOSE_ID);\n            return 0;\n        }\n        ptmp = X509_PURPOSE_get0(idx);\n        if (ptmp->trust == X509_TRUST_DEFAULT) {\n            idx = X509_PURPOSE_get_by_id(def_purpose);\n            if (idx == -1) {\n                X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n                        X509_R_UNKNOWN_PURPOSE_ID);\n                return 0;\n            }\n            ptmp = X509_PURPOSE_get0(idx);\n        }\n        if (!trust)\n            trust = ptmp->trust;\n    }\n    if (trust) {\n        idx = X509_TRUST_get_by_id(trust);\n        if (idx == -1) {\n            X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,\n                    X509_R_UNKNOWN_TRUST_ID);\n            return 0;\n        }\n    }\n    if (purpose && !ctx->param->purpose)\n        ctx->param->purpose = purpose;\n    if (trust && !ctx->param->trust)\n        ctx->param->trust = trust;\n    return 1;\n}', 'int X509_PURPOSE_get_by_id(int purpose)\n{\n    X509_PURPOSE tmp;\n    int idx;\n    if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))\n        return purpose - X509_PURPOSE_MIN;\n    tmp.purpose = purpose;\n    if (!xptable)\n        return -1;\n    idx = sk_X509_PURPOSE_find(xptable, &tmp);\n    if (idx == -1)\n        return -1;\n    return idx + X509_PURPOSE_COUNT;\n}', 'DEFINE_STACK_OF(X509_PURPOSE)', 'int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)\n{\n    return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);\n}', 'static int internal_find(OPENSSL_STACK *st, const void *data,\n                         int ret_val_options)\n{\n    const void *r;\n    int i;\n    if (st == NULL)\n        return -1;\n    if (st->comp == NULL) {\n        for (i = 0; i < st->num; i++)\n            if (st->data[i] == data)\n                return (i);\n        return (-1);\n    }\n    OPENSSL_sk_sort(st);\n    if (data == NULL)\n        return (-1);\n    r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,\n                        ret_val_options);\n    if (r == NULL)\n        return (-1);\n    return (int)((const char **)r - st->data);\n}', 'X509_PURPOSE *X509_PURPOSE_get0(int idx)\n{\n    if (idx < 0)\n        return NULL;\n    if (idx < (int)X509_PURPOSE_COUNT)\n        return xstandard + idx;\n    return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);\n}']