int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
                  unsigned long cflag)
{
    long l;
    int ret = 0, i;
    char *m = NULL, mlch = ' ';
    int nmindent = 0;
    ASN1_INTEGER *bs;
    EVP_PKEY *pkey = NULL;
    const char *neg;
    if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
        mlch = '\n';
        nmindent = 12;
    }
    if (nmflags == X509_FLAG_COMPAT)
        nmindent = 16;
    if (!(cflag & X509_FLAG_NO_HEADER)) {
        if (BIO_write(bp, "Certificate:\n", 13) <= 0)
            goto err;
        if (BIO_write(bp, "    Data:\n", 10) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_VERSION)) {
        l = X509_get_version(x);
        if (BIO_printf(bp, "%8sVersion: %lu (0x%lx)\n", "", l + 1, l) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_SERIAL)) {
        if (BIO_write(bp, "        Serial Number:", 22) <= 0)
            goto err;
        bs = X509_get_serialNumber(x);
        if (bs->length <= (int)sizeof(long)) {
                ERR_set_mark();
                l = ASN1_INTEGER_get(bs);
                ERR_pop_to_mark();
        } else {
            l = -1;
        }
        if (l != -1) {
            if (bs->type == V_ASN1_NEG_INTEGER) {
                l = -l;
                neg = "-";
            } else
                neg = "";
            if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, l, neg, l) <= 0)
                goto err;
        } else {
            neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
            if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0)
                goto err;
            for (i = 0; i < bs->length; i++) {
                if (BIO_printf(bp, "%02x%c", bs->data[i],
                               ((i + 1 == bs->length) ? '\n' : ':')) <= 0)
                    goto err;
            }
        }
    }
    if (!(cflag & X509_FLAG_NO_SIGNAME)) {
        X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x);
        if (X509_signature_print(bp, tsig_alg, NULL) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_ISSUER)) {
        if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0)
            goto err;
        if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)
            < 0)
            goto err;
        if (BIO_write(bp, "\n", 1) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_VALIDITY)) {
        if (BIO_write(bp, "        Validity\n", 17) <= 0)
            goto err;
        if (BIO_write(bp, "            Not Before: ", 24) <= 0)
            goto err;
        if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))
            goto err;
        if (BIO_write(bp, "\n            Not After : ", 25) <= 0)
            goto err;
        if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))
            goto err;
        if (BIO_write(bp, "\n", 1) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_SUBJECT)) {
        if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
            goto err;
        if (X509_NAME_print_ex
            (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)
            goto err;
        if (BIO_write(bp, "\n", 1) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_PUBKEY)) {
        X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);
        ASN1_OBJECT *xpoid;
        X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);
        if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0)
            goto err;
        if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
            goto err;
        if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)
            goto err;
        if (BIO_puts(bp, "\n") <= 0)
            goto err;
        pkey = X509_get0_pubkey(x);
        if (pkey == NULL) {
            BIO_printf(bp, "%12sUnable to load Public Key\n", "");
            ERR_print_errors(bp);
        } else {
            EVP_PKEY_print_public(bp, pkey, 16, NULL);
        }
    }
    if (!(cflag & X509_FLAG_NO_IDS)) {
        ASN1_BIT_STRING *iuid, *suid;
        X509_get0_uids(&iuid, &suid, x);
        if (iuid != NULL) {
            if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)
                goto err;
            if (!X509_signature_dump(bp, iuid, 12))
                goto err;
        }
        if (suid != NULL) {
            if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)
                goto err;
            if (!X509_signature_dump(bp, suid, 12))
                goto err;
        }
    }
    if (!(cflag & X509_FLAG_NO_EXTENSIONS))
        X509V3_extensions_print(bp, "X509v3 extensions",
                                X509_get0_extensions(x), cflag, 8);
    if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
        X509_ALGOR *sig_alg;
        ASN1_BIT_STRING *sig;
        X509_get0_signature(&sig, &sig_alg, x);
        if (X509_signature_print(bp, sig_alg, sig) <= 0)
            goto err;
    }
    if (!(cflag & X509_FLAG_NO_AUX)) {
        if (!X509_aux_print(bp, x, 0))
            goto err;
    }
    ret = 1;
 err:
    OPENSSL_free(m);
    return (ret);
}
['int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,\n                  unsigned long cflag)\n{\n    long l;\n    int ret = 0, i;\n    char *m = NULL, mlch = \' \';\n    int nmindent = 0;\n    ASN1_INTEGER *bs;\n    EVP_PKEY *pkey = NULL;\n    const char *neg;\n    if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {\n        mlch = \'\\n\';\n        nmindent = 12;\n    }\n    if (nmflags == X509_FLAG_COMPAT)\n        nmindent = 16;\n    if (!(cflag & X509_FLAG_NO_HEADER)) {\n        if (BIO_write(bp, "Certificate:\\n", 13) <= 0)\n            goto err;\n        if (BIO_write(bp, "    Data:\\n", 10) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_VERSION)) {\n        l = X509_get_version(x);\n        if (BIO_printf(bp, "%8sVersion: %lu (0x%lx)\\n", "", l + 1, l) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_SERIAL)) {\n        if (BIO_write(bp, "        Serial Number:", 22) <= 0)\n            goto err;\n        bs = X509_get_serialNumber(x);\n        if (bs->length <= (int)sizeof(long)) {\n                ERR_set_mark();\n                l = ASN1_INTEGER_get(bs);\n                ERR_pop_to_mark();\n        } else {\n            l = -1;\n        }\n        if (l != -1) {\n            if (bs->type == V_ASN1_NEG_INTEGER) {\n                l = -l;\n                neg = "-";\n            } else\n                neg = "";\n            if (BIO_printf(bp, " %s%lu (%s0x%lx)\\n", neg, l, neg, l) <= 0)\n                goto err;\n        } else {\n            neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";\n            if (BIO_printf(bp, "\\n%12s%s", "", neg) <= 0)\n                goto err;\n            for (i = 0; i < bs->length; i++) {\n                if (BIO_printf(bp, "%02x%c", bs->data[i],\n                               ((i + 1 == bs->length) ? \'\\n\' : \':\')) <= 0)\n                    goto err;\n            }\n        }\n    }\n    if (!(cflag & X509_FLAG_NO_SIGNAME)) {\n        X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x);\n        if (X509_signature_print(bp, tsig_alg, NULL) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_ISSUER)) {\n        if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0)\n            goto err;\n        if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags)\n            < 0)\n            goto err;\n        if (BIO_write(bp, "\\n", 1) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_VALIDITY)) {\n        if (BIO_write(bp, "        Validity\\n", 17) <= 0)\n            goto err;\n        if (BIO_write(bp, "            Not Before: ", 24) <= 0)\n            goto err;\n        if (!ASN1_TIME_print(bp, X509_get_notBefore(x)))\n            goto err;\n        if (BIO_write(bp, "\\n            Not After : ", 25) <= 0)\n            goto err;\n        if (!ASN1_TIME_print(bp, X509_get_notAfter(x)))\n            goto err;\n        if (BIO_write(bp, "\\n", 1) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_SUBJECT)) {\n        if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)\n            goto err;\n        if (X509_NAME_print_ex\n            (bp, X509_get_subject_name(x), nmindent, nmflags) < 0)\n            goto err;\n        if (BIO_write(bp, "\\n", 1) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_PUBKEY)) {\n        X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x);\n        ASN1_OBJECT *xpoid;\n        X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey);\n        if (BIO_write(bp, "        Subject Public Key Info:\\n", 33) <= 0)\n            goto err;\n        if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)\n            goto err;\n        if (i2a_ASN1_OBJECT(bp, xpoid) <= 0)\n            goto err;\n        if (BIO_puts(bp, "\\n") <= 0)\n            goto err;\n        pkey = X509_get0_pubkey(x);\n        if (pkey == NULL) {\n            BIO_printf(bp, "%12sUnable to load Public Key\\n", "");\n            ERR_print_errors(bp);\n        } else {\n            EVP_PKEY_print_public(bp, pkey, 16, NULL);\n        }\n    }\n    if (!(cflag & X509_FLAG_NO_IDS)) {\n        ASN1_BIT_STRING *iuid, *suid;\n        X509_get0_uids(&iuid, &suid, x);\n        if (iuid != NULL) {\n            if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0)\n                goto err;\n            if (!X509_signature_dump(bp, iuid, 12))\n                goto err;\n        }\n        if (suid != NULL) {\n            if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0)\n                goto err;\n            if (!X509_signature_dump(bp, suid, 12))\n                goto err;\n        }\n    }\n    if (!(cflag & X509_FLAG_NO_EXTENSIONS))\n        X509V3_extensions_print(bp, "X509v3 extensions",\n                                X509_get0_extensions(x), cflag, 8);\n    if (!(cflag & X509_FLAG_NO_SIGDUMP)) {\n        X509_ALGOR *sig_alg;\n        ASN1_BIT_STRING *sig;\n        X509_get0_signature(&sig, &sig_alg, x);\n        if (X509_signature_print(bp, sig_alg, sig) <= 0)\n            goto err;\n    }\n    if (!(cflag & X509_FLAG_NO_AUX)) {\n        if (!X509_aux_print(bp, x, 0))\n            goto err;\n    }\n    ret = 1;\n err:\n    OPENSSL_free(m);\n    return (ret);\n}', 'long X509_get_version(const X509 *x)\n{\n    return ASN1_INTEGER_get(x->cert_info.version);\n}', 'long ASN1_INTEGER_get(const ASN1_INTEGER *a)\n{\n    int i;\n    int64_t r;\n    if (a == NULL)\n        return 0;\n    i = ASN1_INTEGER_get_int64(&r, a);\n    if (i == 0)\n        return -1;\n    if (r > LONG_MAX || r < LONG_MIN)\n        return -1;\n    return (long)r;\n}']