/* Encode an ASN1 item, this is use by the standard 'i2d' function. 'out' points to a buffer to output the data to. The new i2d has one additional feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is allocated and populated with the encoding.  */
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it, int flags)
/* Encode an ASN1 item, this is use by the standard 'i2d' function. 'out' points to a buffer to output the data to. The new i2d has one additional feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is allocated and populated with the encoding.  */
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it, int flags)
{
    if (out && !*out) {
        unsigned char *p, *buf;
        int len;
        len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags);
        if (len <= 0)
            return len;
        buf = OPENSSL_malloc(len);
        if (!buf)
            return -1;
        p = buf;
        ASN1_item_ex_i2d(&val, &p, it, -1, flags);
        *out = buf;
        return len;
    }
    return ASN1_item_ex_i2d(&val, out, it, -1, flags);
}