<commit_before>#include "base64.h"
#include <string.h>
#include <stdlib.h>

#define b64pos(ch) (strchr(b64, ch)-b64)

static const char b64[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

char *b64encode(const char *data, size_t len){
    char ch[3], *ret = NULL;
    size_t i, j, pad;

    if(!len)
        goto end;

    ret = malloc(((len + 2) / 3) * 4 + 1);
    if(ret == NULL)
        goto end;

    i = j = 0;

    while(i < len){
        ch[1] = ch[2] = 0;
        ch[0] = data[i++];

        if(i < len){
            ch[1] = data[i++];
            if(i < len){
                ch[2] = data[i++];
            }
        }

        ret[j++] = b64[ch[0] >> 2];
        ret[j++] = b64[(ch[0] & 3) << 4 | ch[1] >> 4];
        ret[j++] = b64[(ch[1] & 0xf) << 2 | ch[2] >> 6];
        ret[j++] = b64[ch[2] & 0x3f];
    }

    pad = len%3;

    if(pad){
        ret[j-1] = '=';

        if(pad == 1){
            ret[j-2] = '=';
        }
    }

    ret[j] = 0x0;

    end:
    return ret;
}

char *b64decode(const char *encoded, size_t *len){
    size_t i, pos, strsize;
    char *ret;

    strsize = strlen(encoded);

    if(!isb64valid(encoded, strsize)){
        return NULL;
    }

    if((ret = malloc((strsize * 0.75) + 1))){
        return NULL;
    }

    i = pos = 0;

    while(i < strsize){
        ret[pos++] = b64pos(encoded[i]) << 2 | b64pos(encoded[i+1]) >> 4;

        if(encoded[i+2] == '=')
            break;

        ret[pos++] = b64pos(encoded[i+1]) << 4 | b64pos(encoded[i+2]) >> 2;

        if(encoded[i+3] == '=')
            break;

        ret[pos++] = b64pos(encoded[i+2]) << 6 | b64pos(encoded[i+3]);

        i += 4;
    }

    ret[pos] = 0x0;
    *len = pos;

    return ret;
}

int isb64valid(const char *encoded, size_t length){
    size_t i;
    int ret = 0;<commit_msg>fix a bug in b64decode<commit_after>#include "base64.h"
#include <string.h>
#include <stdlib.h>

#define b64pos(ch) (strchr(b64, ch)-b64)

static const char b64[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789+/";

char *b64encode(const char *data, size_t len){
    char ch[3], *ret = NULL;
    size_t i, j, pad;

    if(!len)
        goto end;

    ret = malloc(((len + 2) / 3) * 4 + 1);
    if(ret == NULL)
        goto end;

    i = j = 0;

    while(i < len){
        ch[1] = ch[2] = 0;
        ch[0] = data[i++];

        if(i < len){
            ch[1] = data[i++];
            if(i < len){
                ch[2] = data[i++];
            }
        }

        ret[j++] = b64[ch[0] >> 2];
        ret[j++] = b64[(ch[0] & 3) << 4 | ch[1] >> 4];
        ret[j++] = b64[(ch[1] & 0xf) << 2 | ch[2] >> 6];
        ret[j++] = b64[ch[2] & 0x3f];
    }

    pad = len%3;

    if(pad){
        ret[j-1] = '=';

        if(pad == 1){
            ret[j-2] = '=';
        }
    }

    ret[j] = 0x0;

    end:
    return ret;
}

char *b64decode(const char *encoded, size_t *len){
    size_t i, pos, strsize;
    char *ret;

    strsize = strlen(encoded);

    if(!isb64valid(encoded, strsize)){
        return NULL;
    }

    ret = malloc((strsize * 0.75) + 1);

    if(ret == NULL){
        return NULL;
    }

    i = pos = 0;

    while(i < strsize){
        ret[pos++] = b64pos(encoded[i]) << 2 | b64pos(encoded[i+1]) >> 4;

        if(encoded[i+2] == '=')
            break;

        ret[pos++] = b64pos(encoded[i+1]) << 4 | b64pos(encoded[i+2]) >> 2;

        if(encoded[i+3] == '=')
            break;

        ret[pos++] = b64pos(encoded[i+2]) << 6 | b64pos(encoded[i+3]);

        i += 4;
    }

    ret[pos] = 0x0;
    *len = pos;

    return ret;
}

int isb64valid(const char *encoded, size_t length){
    size_t i;
    int ret = 0;
{'fraction_non_alphanumeric': 0.1993543179983858, 'fraction_numerical': 0.05542103847188593, 'mean_word_length': 1.8188021228203184, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 27, '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': '1160267', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1516, 'n_words': 321}