<commit_before>
#include <iostream>
#include "ecrypt-sync.h"

static const u32 delta = 0x9e3779b9;

void ECRYPT_TEA::ECRYPT_init() { }

void ECRYPT_TEA::ECRYPT_keysetup(void *ctx, const u8 *key, u32 keysize, u32 ivsize)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    //copy key of u8 to array of u32
    for(int i = 0; i < 4; i++) teaCtx->key[i] = U8TO32_LITTLE(key + 4*i);
    for(int j = 0; j < 2; j++) teaCtx->input[j] = 0;
}

void ECRYPT_TEA::ECRYPT_ivsetup(void *ctx, const u8 *iv) { }

void ECRYPT_TEA::ECRYPT_encrypt_bytes(void *ctx, const u8 *plaintext, u8 *ciphertext, u32 msglen)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    if(!msglen) return;
    for(unsigned int i = 0; i < msglen + 8; i += 8) {
        //copy plaintext of u8 to array of u32
        for(int j = 0; j < 2; j++) teaCtx->input[j] = U8TO32_LITTLE(plaintext + i + 4*j);

        u32 sum = 0;
        for (int j = 0; j < numRounds; j++) {
            sum += delta;
            teaCtx->input[0] += ((teaCtx->input[1]<<4) + teaCtx->key[0])
                    ^ (teaCtx->input[1] + sum)
                    ^ ((teaCtx->input[1]>>5) + teaCtx->key[1]);

            teaCtx->input[1] += ((teaCtx->input[0]<<4) + teaCtx->key[2])
                    ^ (teaCtx->input[0] + sum)
                    ^ ((teaCtx->input[0]>>5) + teaCtx->key[3]);
        }
        //debug
        u32 tmp0 = teaCtx->input[0];
        u32 tmp1 = teaCtx->input[1];
        for(int j = 0; j < 2; j++) U32TO8_LITTLE(ciphertext + i + 4*j, teaCtx->input[j]);
    }
}

void ECRYPT_TEA::ECRYPT_decrypt_bytes(void *ctx, const u8 *ciphertext, u8 *plaintext, u32 msglen)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    if(!msglen) return;
    for(unsigned int i = 0; i < msglen + 8; i += 8) {
        //copy plaintext of u8 to array of u32
        for(int j = 0; j < 2; j++) teaCtx->input[j] = U8TO32_LITTLE(ciphertext + i + 4*j);

        u32 sum = delta * numRounds;

        for (int j = 0; j < numRounds; j++) {
            teaCtx->input[1] -= ((teaCtx->input[0]<<4) + teaCtx->key[2])
                    ^ (teaCtx->input[0] + sum)
                    ^ ((teaCtx->input[0]>>5) + teaCtx->key[3]);

            teaCtx->input[0] -= ((teaCtx->input[1]<<4) + teaCtx->key[0])
                    ^ (teaCtx->input[1] + sum)
                    ^ ((teaCtx->input[1]>>5) + teaCtx->key[1]);
            sum -= delta;
        }
        //debug
        u32 tmp0 = teaCtx->input[0];
        u32 tmp1 = teaCtx->input[1];
        for(int j = 0; j < 2; j++) U32TO8_LITTLE(plaintext + i + 4*j, teaCtx->input[j]);
    }
}<commit_msg>[TEA] fixed bug with writing out of ciphertext allocated vector<commit_after>
#include <iostream>
#include "ecrypt-sync.h"

static const u32 delta = 0x9e3779b9;

void ECRYPT_TEA::ECRYPT_init() { }

void ECRYPT_TEA::ECRYPT_keysetup(void *ctx, const u8 *key, u32 keysize, u32 ivsize)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    //copy key of u8 to array of u32
    for(int i = 0; i < 4; i++) teaCtx->key[i] = U8TO32_LITTLE(key + 4*i);
    for(int j = 0; j < 2; j++) teaCtx->input[j] = 0;
}

void ECRYPT_TEA::ECRYPT_ivsetup(void *ctx, const u8 *iv) { }

void ECRYPT_TEA::ECRYPT_encrypt_bytes(void *ctx, const u8 *plaintext, u8 *ciphertext, u32 msglen)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    if(!msglen) return;
    for(unsigned int i = 0; i < msglen; i += 8) {
        //copy plaintext of u8 to array of u32
        for(int j = 0; j < 2; j++) teaCtx->input[j] = U8TO32_LITTLE(plaintext + i + 4*j);

        u32 sum = 0;
        for (int j = 0; j < numRounds; j++) {
            sum += delta;
            teaCtx->input[0] += ((teaCtx->input[1]<<4) + teaCtx->key[0])
                    ^ (teaCtx->input[1] + sum)
                    ^ ((teaCtx->input[1]>>5) + teaCtx->key[1]);

            teaCtx->input[1] += ((teaCtx->input[0]<<4) + teaCtx->key[2])
                    ^ (teaCtx->input[0] + sum)
                    ^ ((teaCtx->input[0]>>5) + teaCtx->key[3]);
        }
        for (int j = 0; j < 2; j++)
            U32TO8_LITTLE(ciphertext + i + 4*j, teaCtx->input[j]);
    }
}

void ECRYPT_TEA::ECRYPT_decrypt_bytes(void *ctx, const u8 *ciphertext, u8 *plaintext, u32 msglen)
{
    TEA_ctx* teaCtx = (TEA_ctx*) ctx;
    if(!msglen) return;
    for(unsigned int i = 0; i < msglen; i += 8) {
        //copy plaintext of u8 to array of u32
        for(int j = 0; j < 2; j++) teaCtx->input[j] = U8TO32_LITTLE(ciphertext + i + 4*j);

        u32 sum = delta * numRounds;

        for (int j = 0; j < numRounds; j++) {
            teaCtx->input[1] -= ((teaCtx->input[0]<<4) + teaCtx->key[2])
                    ^ (teaCtx->input[0] + sum)
                    ^ ((teaCtx->input[0]>>5) + teaCtx->key[3]);

            teaCtx->input[0] -= ((teaCtx->input[1]<<4) + teaCtx->key[0])
                    ^ (teaCtx->input[1] + sum)
                    ^ ((teaCtx->input[1]>>5) + teaCtx->key[1]);
            sum -= delta;
        }
        for(int j = 0; j < 2; j++) 
            U32TO8_LITTLE(plaintext + i + 4*j, teaCtx->input[j]);
    }
}
{'fraction_non_alphanumeric': 0.1823293172690763, 'fraction_numerical': 0.046987951807228916, 'mean_word_length': 2.176658163265306, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7303298', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2057, 'n_words': 454}