static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)
{
    size_t packlen = pkt->written - sub->pwritten;
    if (packlen == 0
            && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)
        return 0;
    if (packlen == 0
            && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {
        if (!doclose)
            return 0;
        if ((pkt->curr - sub->lenbytes) == sub->packet_len) {
            pkt->written -= sub->lenbytes;
            pkt->curr -= sub->lenbytes;
        }
        sub->packet_len = 0;
        sub->lenbytes = 0;
    }
    if (sub->lenbytes > 0
                && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,
                              sub->lenbytes))
            return 0;
    if (doclose) {
        pkt->subs = sub->parent;
        OPENSSL_free(sub);
    }
    return 1;
}
['static int test_WPACKET_allocate_bytes(void)\n{\n    WPACKET pkt;\n    size_t written;\n    unsigned char *bytes;\n    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))\n            || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))\n        return cleanup(&pkt);\n    bytes[0] = 0xfe;\n    bytes[1] = 0xff;\n    if (!TEST_true(WPACKET_finish(&pkt))\n            || !TEST_true(WPACKET_get_total_written(&pkt, &written))\n            || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))\n        return cleanup(&pkt);\n    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))\n            || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))\n        return cleanup(&pkt);\n    bytes[0] = 0xfe;\n    bytes[1] = 0xff;\n    if (!TEST_true(WPACKET_finish(&pkt))\n            || !TEST_true(WPACKET_get_total_written(&pkt, &written))\n            || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))\n        return cleanup(&pkt);\n    return 1;\n}', 'int WPACKET_init_len(WPACKET *pkt, BUF_MEM *buf, size_t lenbytes)\n{\n    assert(buf != NULL);\n    if (buf == NULL)\n        return 0;\n    pkt->staticbuf = NULL;\n    pkt->buf = buf;\n    pkt->maxsize = maxmaxsize(lenbytes);\n    return wpacket_intern_init_len(pkt, lenbytes);\n}', 'static int wpacket_intern_init_len(WPACKET *pkt, size_t lenbytes)\n{\n    unsigned char *lenchars;\n    pkt->curr = 0;\n    pkt->written = 0;\n    pkt->subs = OPENSSL_zalloc(sizeof(*pkt->subs));\n    if (pkt->subs == NULL)\n        return 0;\n    if (lenbytes == 0)\n        return 1;\n    pkt->subs->pwritten = lenbytes;\n    pkt->subs->lenbytes = lenbytes;\n    if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars)) {\n        OPENSSL_free(pkt->subs);\n        pkt->subs = NULL;\n        return 0;\n    }\n    pkt->subs->packet_len = lenchars - GETBUF(pkt);\n    return 1;\n}', 'int WPACKET_finish(WPACKET *pkt)\n{\n    int ret;\n    if (pkt->subs == NULL || pkt->subs->parent != NULL)\n        return 0;\n    ret = wpacket_intern_close(pkt, pkt->subs, 1);\n    if (ret) {\n        OPENSSL_free(pkt->subs);\n        pkt->subs = NULL;\n    }\n    return ret;\n}', 'static int wpacket_intern_close(WPACKET *pkt, WPACKET_SUB *sub, int doclose)\n{\n    size_t packlen = pkt->written - sub->pwritten;\n    if (packlen == 0\n            && (sub->flags & WPACKET_FLAGS_NON_ZERO_LENGTH) != 0)\n        return 0;\n    if (packlen == 0\n            && sub->flags & WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH) {\n        if (!doclose)\n            return 0;\n        if ((pkt->curr - sub->lenbytes) == sub->packet_len) {\n            pkt->written -= sub->lenbytes;\n            pkt->curr -= sub->lenbytes;\n        }\n        sub->packet_len = 0;\n        sub->lenbytes = 0;\n    }\n    if (sub->lenbytes > 0\n                && !put_value(&GETBUF(pkt)[sub->packet_len], packlen,\n                              sub->lenbytes))\n            return 0;\n    if (doclose) {\n        pkt->subs = sub->parent;\n        OPENSSL_free(sub);\n    }\n    return 1;\n}']