static int right_encode(unsigned char *out, int *out_len, size_t bits)
{
    unsigned int len = get_encode_size(bits);
    int i;
    if (len > 0xFF)
        return 0;
    for (i = len - 1; i >= 0; --i) {
        out[i] = (unsigned char)(bits & 0xFF);
        bits >>= 8;
    }
    out[len] = (unsigned char)len;
    *out_len = len + 1;
    return 1;
}
['static int kmac_final(EVP_MAC_IMPL *kctx, unsigned char *out)\n{\n    EVP_MD_CTX *ctx = kctx->ctx;\n    int lbits, len;\n    unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];\n    lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));\n    return right_encode(encoded_outlen, &len, lbits)\n           && EVP_DigestUpdate(ctx, encoded_outlen, len)\n           && EVP_DigestFinalXOF(ctx, out, kctx->out_len);\n}', 'static int right_encode(unsigned char *out, int *out_len, size_t bits)\n{\n    unsigned int len = get_encode_size(bits);\n    int i;\n    if (len > 0xFF)\n        return 0;\n    for (i = len - 1; i >= 0; --i) {\n        out[i] = (unsigned char)(bits & 0xFF);\n        bits >>= 8;\n    }\n    out[len] = (unsigned char)len;\n    *out_len = len + 1;\n    return 1;\n}', 'static unsigned int get_encode_size(size_t bits)\n{\n    unsigned int cnt = 0, sz = sizeof(size_t);\n    while (bits && (cnt < sz)) {\n        ++cnt;\n        bits >>= 8;\n    }\n    if (cnt == 0)\n        cnt = 1;\n    return cnt;\n}']