void CRYPTO_free(void *str, const char *file, int line)
{
    INCREMENT(free_count);
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_free(str, 0, file, line);
        free(str);
        CRYPTO_mem_debug_free(str, 1, file, line);
    } else {
        free(str);
    }
#else
    free(str);
#endif
}
['int smime_main(int argc, char **argv)\n{\n    BIO *in = NULL, *out = NULL, *indata = NULL;\n    EVP_PKEY *key = NULL;\n    PKCS7 *p7 = NULL;\n    STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;\n    STACK_OF(X509) *encerts = NULL, *other = NULL;\n    X509 *cert = NULL, *recip = NULL, *signer = NULL;\n    X509_STORE *store = NULL;\n    X509_VERIFY_PARAM *vpm = NULL;\n    const EVP_CIPHER *cipher = NULL;\n    const EVP_MD *sign_md = NULL;\n    const char *CAfile = NULL, *CApath = NULL, *prog = NULL;\n    char *certfile = NULL, *keyfile = NULL, *contfile = NULL;\n    char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;\n    char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL, *subject = NULL;\n    OPTION_CHOICE o;\n    int noCApath = 0, noCAfile = 0;\n    int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;\n    int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =\n        FORMAT_PEM;\n    int vpmtouched = 0, rv = 0;\n    ENGINE *e = NULL;\n    const char *mime_eol = "\\n";\n    if ((vpm = X509_VERIFY_PARAM_new()) == NULL)\n        return 1;\n    prog = opt_init(argc, argv, smime_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(smime_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))\n                goto opthelp;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))\n                goto opthelp;\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_ENCRYPT:\n            operation = SMIME_ENCRYPT;\n            break;\n        case OPT_DECRYPT:\n            operation = SMIME_DECRYPT;\n            break;\n        case OPT_SIGN:\n            operation = SMIME_SIGN;\n            break;\n        case OPT_RESIGN:\n            operation = SMIME_RESIGN;\n            break;\n        case OPT_VERIFY:\n            operation = SMIME_VERIFY;\n            break;\n        case OPT_PK7OUT:\n            operation = SMIME_PK7OUT;\n            break;\n        case OPT_TEXT:\n            flags |= PKCS7_TEXT;\n            break;\n        case OPT_NOINTERN:\n            flags |= PKCS7_NOINTERN;\n            break;\n        case OPT_NOVERIFY:\n            flags |= PKCS7_NOVERIFY;\n            break;\n        case OPT_NOCHAIN:\n            flags |= PKCS7_NOCHAIN;\n            break;\n        case OPT_NOCERTS:\n            flags |= PKCS7_NOCERTS;\n            break;\n        case OPT_NOATTR:\n            flags |= PKCS7_NOATTR;\n            break;\n        case OPT_NODETACH:\n            flags &= ~PKCS7_DETACHED;\n            break;\n        case OPT_NOSMIMECAP:\n            flags |= PKCS7_NOSMIMECAP;\n            break;\n        case OPT_BINARY:\n            flags |= PKCS7_BINARY;\n            break;\n        case OPT_NOSIGS:\n            flags |= PKCS7_NOSIGS;\n            break;\n        case OPT_STREAM:\n        case OPT_INDEF:\n            indef = 1;\n            break;\n        case OPT_NOINDEF:\n            indef = 0;\n            break;\n        case OPT_CRLFEOL:\n            flags |= PKCS7_CRLFEOL;\n            mime_eol = "\\r\\n";\n            break;\n        case OPT_R_CASES:\n            if (!opt_rand(o))\n                goto end;\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_TO:\n            to = opt_arg();\n            break;\n        case OPT_FROM:\n            from = opt_arg();\n            break;\n        case OPT_SUBJECT:\n            subject = opt_arg();\n            break;\n        case OPT_SIGNER:\n            if (signerfile != NULL) {\n                if (sksigners == NULL\n                    && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(sksigners, signerfile);\n                if (keyfile == NULL)\n                    keyfile = signerfile;\n                if (skkeys == NULL\n                    && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(skkeys, keyfile);\n                keyfile = NULL;\n            }\n            signerfile = opt_arg();\n            break;\n        case OPT_RECIP:\n            recipfile = opt_arg();\n            break;\n        case OPT_MD:\n            if (!opt_md(opt_arg(), &sign_md))\n                goto opthelp;\n            break;\n        case OPT_CIPHER:\n            if (!opt_cipher(opt_unknown(), &cipher))\n                goto opthelp;\n            break;\n        case OPT_INKEY:\n            if (keyfile != NULL) {\n                if (signerfile == NULL) {\n                    BIO_printf(bio_err,\n                               "%s: Must have -signer before -inkey\\n", prog);\n                    goto opthelp;\n                }\n                if (sksigners == NULL\n                    && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(sksigners, signerfile);\n                signerfile = NULL;\n                if (skkeys == NULL\n                    && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(skkeys, keyfile);\n            }\n            keyfile = opt_arg();\n            break;\n        case OPT_KEYFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))\n                goto opthelp;\n            break;\n        case OPT_CERTFILE:\n            certfile = opt_arg();\n            break;\n        case OPT_CAFILE:\n            CAfile = opt_arg();\n            break;\n        case OPT_CAPATH:\n            CApath = opt_arg();\n            break;\n        case OPT_NOCAFILE:\n            noCAfile = 1;\n            break;\n        case OPT_NOCAPATH:\n            noCApath = 1;\n            break;\n        case OPT_CONTENT:\n            contfile = opt_arg();\n            break;\n        case OPT_V_CASES:\n            if (!opt_verify(o, vpm))\n                goto opthelp;\n            vpmtouched++;\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {\n        BIO_puts(bio_err, "Multiple signers or keys not allowed\\n");\n        goto opthelp;\n    }\n    if (operation & SMIME_SIGNERS) {\n        if (keyfile && !signerfile) {\n            BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n            goto opthelp;\n        }\n        if (signerfile != NULL) {\n            if (sksigners == NULL\n                && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            sk_OPENSSL_STRING_push(sksigners, signerfile);\n            if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            if (!keyfile)\n                keyfile = signerfile;\n            sk_OPENSSL_STRING_push(skkeys, keyfile);\n        }\n        if (sksigners == NULL) {\n            BIO_printf(bio_err, "No signer certificate specified\\n");\n            goto opthelp;\n        }\n        signerfile = NULL;\n        keyfile = NULL;\n    } else if (operation == SMIME_DECRYPT) {\n        if (recipfile == NULL && keyfile == NULL) {\n            BIO_printf(bio_err,\n                       "No recipient certificate or key specified\\n");\n            goto opthelp;\n        }\n    } else if (operation == SMIME_ENCRYPT) {\n        if (argc == 0) {\n            BIO_printf(bio_err, "No recipient(s) certificate(s) specified\\n");\n            goto opthelp;\n        }\n    } else if (!operation) {\n        goto opthelp;\n    }\n    if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n        BIO_printf(bio_err, "Error getting password\\n");\n        goto end;\n    }\n    ret = 2;\n    if (!(operation & SMIME_SIGNERS))\n        flags &= ~PKCS7_DETACHED;\n    if (!(operation & SMIME_OP)) {\n        if (flags & PKCS7_BINARY)\n            outformat = FORMAT_BINARY;\n    }\n    if (!(operation & SMIME_IP)) {\n        if (flags & PKCS7_BINARY)\n            informat = FORMAT_BINARY;\n    }\n    if (operation == SMIME_ENCRYPT) {\n        if (cipher == NULL) {\n#ifndef OPENSSL_NO_DES\n            cipher = EVP_des_ede3_cbc();\n#else\n            BIO_printf(bio_err, "No cipher selected\\n");\n            goto end;\n#endif\n        }\n        encerts = sk_X509_new_null();\n        if (encerts == NULL)\n            goto end;\n        while (*argv != NULL) {\n            cert = load_cert(*argv, FORMAT_PEM,\n                             "recipient certificate file");\n            if (cert == NULL)\n                goto end;\n            sk_X509_push(encerts, cert);\n            cert = NULL;\n            argv++;\n        }\n    }\n    if (certfile != NULL) {\n        if (!load_certs(certfile, &other, FORMAT_PEM, NULL,\n                        "certificate file")) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (recipfile != NULL && (operation == SMIME_DECRYPT)) {\n        if ((recip = load_cert(recipfile, FORMAT_PEM,\n                               "recipient certificate file")) == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (operation == SMIME_DECRYPT) {\n        if (keyfile == NULL)\n            keyfile = recipfile;\n    } else if (operation == SMIME_SIGN) {\n        if (keyfile == NULL)\n            keyfile = signerfile;\n    } else {\n        keyfile = NULL;\n    }\n    if (keyfile != NULL) {\n        key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n        if (key == NULL)\n            goto end;\n    }\n    in = bio_open_default(infile, \'r\', informat);\n    if (in == NULL)\n        goto end;\n    if (operation & SMIME_IP) {\n        if (informat == FORMAT_SMIME) {\n            p7 = SMIME_read_PKCS7(in, &indata);\n        } else if (informat == FORMAT_PEM) {\n            p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);\n        } else if (informat == FORMAT_ASN1) {\n            p7 = d2i_PKCS7_bio(in, NULL);\n        } else {\n            BIO_printf(bio_err, "Bad input format for PKCS#7 file\\n");\n            goto end;\n        }\n        if (p7 == NULL) {\n            BIO_printf(bio_err, "Error reading S/MIME message\\n");\n            goto end;\n        }\n        if (contfile != NULL) {\n            BIO_free(indata);\n            if ((indata = BIO_new_file(contfile, "rb")) == NULL) {\n                BIO_printf(bio_err, "Can\'t read content file %s\\n", contfile);\n                goto end;\n            }\n        }\n    }\n    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\n    if (operation == SMIME_VERIFY) {\n        if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)\n            goto end;\n        X509_STORE_set_verify_cb(store, smime_cb);\n        if (vpmtouched)\n            X509_STORE_set1_param(store, vpm);\n    }\n    ret = 3;\n    if (operation == SMIME_ENCRYPT) {\n        if (indef)\n            flags |= PKCS7_STREAM;\n        p7 = PKCS7_encrypt(encerts, in, cipher, flags);\n    } else if (operation & SMIME_SIGNERS) {\n        int i;\n        if (operation == SMIME_SIGN) {\n            if (flags & PKCS7_DETACHED) {\n                if (outformat == FORMAT_SMIME)\n                    flags |= PKCS7_STREAM;\n            } else if (indef) {\n                flags |= PKCS7_STREAM;\n            }\n            flags |= PKCS7_PARTIAL;\n            p7 = PKCS7_sign(NULL, NULL, other, in, flags);\n            if (p7 == NULL)\n                goto end;\n            if (flags & PKCS7_NOCERTS) {\n                for (i = 0; i < sk_X509_num(other); i++) {\n                    X509 *x = sk_X509_value(other, i);\n                    PKCS7_add_certificate(p7, x);\n                }\n            }\n        } else {\n            flags |= PKCS7_REUSE_DIGEST;\n        }\n        for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {\n            signerfile = sk_OPENSSL_STRING_value(sksigners, i);\n            keyfile = sk_OPENSSL_STRING_value(skkeys, i);\n            signer = load_cert(signerfile, FORMAT_PEM,\n                               "signer certificate");\n            if (signer == NULL)\n                goto end;\n            key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n            if (key == NULL)\n                goto end;\n            if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))\n                goto end;\n            X509_free(signer);\n            signer = NULL;\n            EVP_PKEY_free(key);\n            key = NULL;\n        }\n        if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {\n            if (!PKCS7_final(p7, in, flags))\n                goto end;\n        }\n    }\n    if (p7 == NULL) {\n        BIO_printf(bio_err, "Error creating PKCS#7 structure\\n");\n        goto end;\n    }\n    ret = 4;\n    if (operation == SMIME_DECRYPT) {\n        if (!PKCS7_decrypt(p7, key, recip, out, flags)) {\n            BIO_printf(bio_err, "Error decrypting PKCS#7 structure\\n");\n            goto end;\n        }\n    } else if (operation == SMIME_VERIFY) {\n        STACK_OF(X509) *signers;\n        if (PKCS7_verify(p7, other, store, indata, out, flags))\n            BIO_printf(bio_err, "Verification successful\\n");\n        else {\n            BIO_printf(bio_err, "Verification failure\\n");\n            goto end;\n        }\n        signers = PKCS7_get0_signers(p7, other, flags);\n        if (!save_certs(signerfile, signers)) {\n            BIO_printf(bio_err, "Error writing signers to %s\\n", signerfile);\n            ret = 5;\n            goto end;\n        }\n        sk_X509_free(signers);\n    } else if (operation == SMIME_PK7OUT) {\n        PEM_write_bio_PKCS7(out, p7);\n    } else {\n        if (to)\n            BIO_printf(out, "To: %s%s", to, mime_eol);\n        if (from)\n            BIO_printf(out, "From: %s%s", from, mime_eol);\n        if (subject)\n            BIO_printf(out, "Subject: %s%s", subject, mime_eol);\n        if (outformat == FORMAT_SMIME) {\n            if (operation == SMIME_RESIGN)\n                rv = SMIME_write_PKCS7(out, p7, indata, flags);\n            else\n                rv = SMIME_write_PKCS7(out, p7, in, flags);\n        } else if (outformat == FORMAT_PEM) {\n            rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);\n        } else if (outformat == FORMAT_ASN1) {\n            rv = i2d_PKCS7_bio_stream(out, p7, in, flags);\n        } else {\n            BIO_printf(bio_err, "Bad output format for PKCS#7 file\\n");\n            goto end;\n        }\n        if (rv == 0) {\n            BIO_printf(bio_err, "Error writing output\\n");\n            ret = 3;\n            goto end;\n        }\n    }\n    ret = 0;\n end:\n    if (ret)\n        ERR_print_errors(bio_err);\n    sk_X509_pop_free(encerts, X509_free);\n    sk_X509_pop_free(other, X509_free);\n    X509_VERIFY_PARAM_free(vpm);\n    sk_OPENSSL_STRING_free(sksigners);\n    sk_OPENSSL_STRING_free(skkeys);\n    X509_STORE_free(store);\n    X509_free(cert);\n    X509_free(recip);\n    X509_free(signer);\n    EVP_PKEY_free(key);\n    PKCS7_free(p7);\n    release_engine(e);\n    BIO_free(in);\n    BIO_free(indata);\n    BIO_free_all(out);\n    OPENSSL_free(passin);\n    return ret;\n}', 'DEFINE_STACK_OF(X509)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n    if (st == NULL)\n        return -1;\n    return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n    if (st == NULL || st->num == max_nodes)\n        return 0;\n    if (!sk_reserve(st, 1, 0))\n        return 0;\n    if ((loc >= st->num) || (loc < 0)) {\n        st->data[st->num] = data;\n    } else {\n        memmove(&st->data[loc + 1], &st->data[loc],\n                sizeof(st->data[0]) * (st->num - loc));\n        st->data[loc] = data;\n    }\n    st->num++;\n    st->sorted = 0;\n    return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n    const void **tmpdata;\n    int num_alloc;\n    if (n > max_nodes - st->num)\n        return 0;\n    num_alloc = st->num + n;\n    if (num_alloc < min_nodes)\n        num_alloc = min_nodes;\n    if (st->data == NULL) {\n        st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);\n        if (st->data == NULL)\n            return 0;\n        st->num_alloc = num_alloc;\n        return 1;\n    }\n    if (!exact) {\n        if (num_alloc <= st->num_alloc)\n            return 1;\n        num_alloc = compute_growth(num_alloc, st->num_alloc);\n        if (num_alloc == 0)\n            return 0;\n    } else if (num_alloc == st->num_alloc) {\n        return 1;\n    }\n    tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n    if (tmpdata == NULL)\n        return 0;\n    st->data = tmpdata;\n    st->num_alloc = num_alloc;\n    return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n    INCREMENT(realloc_count);\n    if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n        return realloc_impl(str, num, file, line);\n    FAILTEST();\n    if (str == NULL)\n        return CRYPTO_malloc(num, file, line);\n    if (num == 0) {\n        CRYPTO_free(str, file, line);\n        return NULL;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        void *ret;\n        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n        ret = realloc(str, num);\n        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n        return ret;\n    }\n#else\n    (void)(file); (void)(line);\n#endif\n    return realloc(str, num);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}']