int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
{
    PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
    int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
                                                (diff > (0 - (PTRDIFF_T)len)));
    return overlapped;
}
['int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)\n{\n    unsigned char *senc = NULL;\n    EVP_CIPHER_CTX *ctx = NULL;\n    HMAC_CTX *hctx = NULL;\n    unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;\n    const unsigned char *const_p;\n    int len, slen_full, slen, lenfinal;\n    SSL_SESSION *sess;\n    unsigned int hlen;\n    SSL_CTX *tctx = s->initial_ctx;\n    unsigned char iv[EVP_MAX_IV_LENGTH];\n    unsigned char key_name[TLSEXT_KEYNAME_LENGTH];\n    int iv_len, al = SSL_AD_INTERNAL_ERROR;\n    size_t macoffset, macendoffset;\n    union {\n        unsigned char age_add_c[sizeof(uint32_t)];\n        uint32_t age_add;\n    } age_add_u;\n    if (SSL_IS_TLS13(s)) {\n        if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0)\n            goto err;\n        s->session->ext.tick_age_add = age_add_u.age_add;\n    }\n    slen_full = i2d_SSL_SESSION(s->session, NULL);\n    if (slen_full == 0 || slen_full > 0xFF00) {\n        ossl_statem_set_error(s);\n        return 0;\n    }\n    senc = OPENSSL_malloc(slen_full);\n    if (senc == NULL) {\n        ossl_statem_set_error(s);\n        return 0;\n    }\n    ctx = EVP_CIPHER_CTX_new();\n    hctx = HMAC_CTX_new();\n    if (ctx == NULL || hctx == NULL) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    p = senc;\n    if (!i2d_SSL_SESSION(s->session, &p))\n        goto err;\n    const_p = senc;\n    sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);\n    if (sess == NULL)\n        goto err;\n    sess->session_id_length = 0;\n    slen = i2d_SSL_SESSION(sess, NULL);\n    if (slen == 0 || slen > slen_full) {\n        SSL_SESSION_free(sess);\n        goto err;\n    }\n    p = senc;\n    if (!i2d_SSL_SESSION(sess, &p)) {\n        SSL_SESSION_free(sess);\n        goto err;\n    }\n    SSL_SESSION_free(sess);\n    if (tctx->ext.ticket_key_cb) {\n        int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,\n                                             hctx, 1);\n        if (ret == 0) {\n            if (!WPACKET_put_bytes_u32(pkt, 0)\n                    || !WPACKET_put_bytes_u16(pkt, 0)) {\n                SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,\n                       ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            OPENSSL_free(senc);\n            EVP_CIPHER_CTX_free(ctx);\n            HMAC_CTX_free(hctx);\n            return 1;\n        }\n        if (ret < 0)\n            goto err;\n        iv_len = EVP_CIPHER_CTX_iv_length(ctx);\n    } else {\n        const EVP_CIPHER *cipher = EVP_aes_256_cbc();\n        iv_len = EVP_CIPHER_iv_length(cipher);\n        if (RAND_bytes(iv, iv_len) <= 0)\n            goto err;\n        if (!EVP_EncryptInit_ex(ctx, cipher, NULL,\n                                tctx->ext.tick_aes_key, iv))\n            goto err;\n        if (!HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,\n                          sizeof(tctx->ext.tick_hmac_key),\n                          EVP_sha256(), NULL))\n            goto err;\n        memcpy(key_name, tctx->ext.tick_key_name,\n               sizeof(tctx->ext.tick_key_name));\n    }\n    if (!WPACKET_put_bytes_u32(pkt, s->hit ? 0 : s->session->timeout)\n            || (SSL_IS_TLS13(s)\n                && !WPACKET_put_bytes_u32(pkt, age_add_u.age_add))\n            || !WPACKET_start_sub_packet_u16(pkt)\n            || !WPACKET_get_total_written(pkt, &macoffset)\n            || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))\n            || !WPACKET_memcpy(pkt, iv, iv_len)\n            || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,\n                                      &encdata1)\n            || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)\n            || !WPACKET_allocate_bytes(pkt, len, &encdata2)\n            || encdata1 != encdata2\n            || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)\n            || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)\n            || encdata1 + len != encdata2\n            || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH\n            || !WPACKET_get_total_written(pkt, &macendoffset)\n            || !HMAC_Update(hctx,\n                            (unsigned char *)s->init_buf->data + macoffset,\n                            macendoffset - macoffset)\n            || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)\n            || !HMAC_Final(hctx, macdata1, &hlen)\n            || hlen > EVP_MAX_MD_SIZE\n            || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)\n            || macdata1 != macdata2\n            || !WPACKET_close(pkt)\n            || (SSL_IS_TLS13(s)\n                && !tls_construct_extensions(s, pkt,\n                                             EXT_TLS1_3_NEW_SESSION_TICKET,\n                                             NULL, 0, &al))) {\n        SSLerr(SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    EVP_CIPHER_CTX_free(ctx);\n    HMAC_CTX_free(hctx);\n    OPENSSL_free(senc);\n    return 1;\n err:\n    OPENSSL_free(senc);\n    EVP_CIPHER_CTX_free(ctx);\n    HMAC_CTX_free(hctx);\n    ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);\n    return 0;\n}', 'int i2d_SSL_SESSION(SSL_SESSION *in, unsigned char **pp)\n{\n    SSL_SESSION_ASN1 as;\n    ASN1_OCTET_STRING cipher;\n    unsigned char cipher_data[2];\n    ASN1_OCTET_STRING master_key, session_id, sid_ctx;\n#ifndef OPENSSL_NO_COMP\n    ASN1_OCTET_STRING comp_id;\n    unsigned char comp_id_data;\n#endif\n    ASN1_OCTET_STRING tlsext_hostname, tlsext_tick;\n#ifndef OPENSSL_NO_SRP\n    ASN1_OCTET_STRING srp_username;\n#endif\n#ifndef OPENSSL_NO_PSK\n    ASN1_OCTET_STRING psk_identity, psk_identity_hint;\n#endif\n    long l;\n    if ((in == NULL) || ((in->cipher == NULL) && (in->cipher_id == 0)))\n        return 0;\n    memset(&as, 0, sizeof(as));\n    as.version = SSL_SESSION_ASN1_VERSION;\n    as.ssl_version = in->ssl_version;\n    if (in->cipher == NULL)\n        l = in->cipher_id;\n    else\n        l = in->cipher->id;\n    cipher_data[0] = ((unsigned char)(l >> 8L)) & 0xff;\n    cipher_data[1] = ((unsigned char)(l)) & 0xff;\n    ssl_session_oinit(&as.cipher, &cipher, cipher_data, 2);\n#ifndef OPENSSL_NO_COMP\n    if (in->compress_meth) {\n        comp_id_data = (unsigned char)in->compress_meth;\n        ssl_session_oinit(&as.comp_id, &comp_id, &comp_id_data, 1);\n    }\n#endif\n    ssl_session_oinit(&as.master_key, &master_key,\n                      in->master_key, in->master_key_length);\n    ssl_session_oinit(&as.session_id, &session_id,\n                      in->session_id, in->session_id_length);\n    ssl_session_oinit(&as.session_id_context, &sid_ctx,\n                      in->sid_ctx, in->sid_ctx_length);\n    as.time = in->time;\n    as.timeout = in->timeout;\n    as.verify_result = in->verify_result;\n    as.peer = in->peer;\n    ssl_session_sinit(&as.tlsext_hostname, &tlsext_hostname,\n                      in->ext.hostname);\n    if (in->ext.tick) {\n        ssl_session_oinit(&as.tlsext_tick, &tlsext_tick,\n                          in->ext.tick, in->ext.ticklen);\n    }\n    if (in->ext.tick_lifetime_hint > 0)\n        as.tlsext_tick_lifetime_hint = in->ext.tick_lifetime_hint;\n    as.tlsext_tick_age_add = in->ext.tick_age_add;\n#ifndef OPENSSL_NO_PSK\n    ssl_session_sinit(&as.psk_identity_hint, &psk_identity_hint,\n                      in->psk_identity_hint);\n    ssl_session_sinit(&as.psk_identity, &psk_identity, in->psk_identity);\n#endif\n#ifndef OPENSSL_NO_SRP\n    ssl_session_sinit(&as.srp_username, &srp_username, in->srp_username);\n#endif\n    as.flags = in->flags;\n    return i2d_SSL_SESSION_ASN1(&as, pp);\n}', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n                      const unsigned char *in, int inl)\n{\n    int i, j, bl;\n    bl = ctx->cipher->block_size;\n    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n        if (bl == 1 && is_partially_overlapping(out, in, inl)) {\n            EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n            return 0;\n        }\n        i = ctx->cipher->do_cipher(ctx, out, in, inl);\n        if (i < 0)\n            return 0;\n        else\n            *outl = i;\n        return 1;\n    }\n    if (inl <= 0) {\n        *outl = 0;\n        return inl == 0;\n    }\n    if (is_partially_overlapping(out + ctx->buf_len, in, inl)) {\n        EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n        return 0;\n    }\n    if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {\n        if (ctx->cipher->do_cipher(ctx, out, in, inl)) {\n            *outl = inl;\n            return 1;\n        } else {\n            *outl = 0;\n            return 0;\n        }\n    }\n    i = ctx->buf_len;\n    OPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n    if (i != 0) {\n        if (bl - i > inl) {\n            memcpy(&(ctx->buf[i]), in, inl);\n            ctx->buf_len += inl;\n            *outl = 0;\n            return 1;\n        } else {\n            j = bl - i;\n            memcpy(&(ctx->buf[i]), in, j);\n            inl -= j;\n            in += j;\n            if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))\n                return 0;\n            out += bl;\n            *outl = bl;\n        }\n    } else\n        *outl = 0;\n    i = inl & (bl - 1);\n    inl -= i;\n    if (inl > 0) {\n        if (!ctx->cipher->do_cipher(ctx, out, in, inl))\n            return 0;\n        *outl += inl;\n    }\n    if (i != 0)\n        memcpy(ctx->buf, &(in[inl]), i);\n    ctx->buf_len = i;\n    return 1;\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n    PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n    int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n                                                (diff > (0 - (PTRDIFF_T)len)));\n    return overlapped;\n}']