<commit_before>/* ENESIM - Drawing Library
 * Copyright (C) 2007-2013 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "enesim_private.h"

#include "enesim_main.h"
#include "enesim_stream.h"
#include "enesim_stream_private.h"
#include <ctype.h>
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
/** @cond internal */
#define ENESIM_LOG_DEFAULT enesim_log_global

typedef struct _Enesim_Stream_Base64
{
	/* passed in */
	Enesim_Stream *data;
	/* last decoded data */
	unsigned char last[3];
	/* the next value to read */
	int last_offset;
	size_t enclen;
	size_t declen;
	char *buf;
	char *curr;
	char *end;
	ssize_t offset;
} Enesim_Stream_Base64;

static Eina_Bool _base64_decode_digit(unsigned char c, unsigned char *v)
{
	switch (c)
	{
		case '+': *v = 62;
		break;

		case '/': *v = 63;
		break;

		case '=': *v = 0;
		break;

		default:
		if (isdigit(c)) *v = c - '0' + 52;
		else if (islower(c)) *v = c - 'a' + 26;
		else if (isupper(c)) *v = c - 'A';
		else return EINA_FALSE;
		break;
	}
	return EINA_TRUE;
}

static void _base64_decode(unsigned char *in, unsigned char *out)
{
	out[0] = (unsigned char)(in[0] << 2 | in[1] >> 4);
	out[1] = (unsigned char)(in[1] << 4 | in[2] >> 2);
	out[2] = (unsigned char)(((in[2] << 6) & 0xc0) | in[3]);
}

/* 4 bytes in base64 give 3 decoded bytes
 * so the output mut be multiple of 3
 */
static void _base64_decode_stream(unsigned char *in, size_t ilen, int *iread,
		unsigned char *out, size_t olen, int *owrite)
{
	unsigned char dec[4];
	unsigned char *i = in;
	unsigned char *o = out;
	int count = 0;

	while (ilen > 0 && olen > 0)
	{
		if (!_base64_decode_digit(*i, &dec[count]))
			goto next;
		/* read the four input bytes */
		count++;
		if (count == 4)
		{
			_base64_decode(dec, o);
			count = 0;
			/* increment the buffer */
			o += 3;
			olen -= 3;
		}
next:
		ilen--;
		i++;
	}
	*iread = i - in;
	*owrite = o - out;
}
/*----------------------------------------------------------------------------*
 *                      The Enesim Image Data interface                       *
 *----------------------------------------------------------------------------*/
/* when the user requests 3 bytes of base64 decoded data we need to read 4 bytes
 * so we always read more from the real source
 */
static ssize_t _enesim_stream_base64_read(void *data, void *buffer, size_t len)
{
	Enesim_Stream_Base64 *thiz = data;
	unsigned char *b = buffer;
	int enclen;
	int declen;
	int decwrite;
	int encread;
	int rest;
	size_t extra = 0;
	size_t ret = 0;

	/* first write the missing bytes from the previous read */
	if (thiz->last_offset)
	{
		int offset;
		size_t i;

		offset = thiz->last_offset;
		extra = 3 - offset;
		if (len < extra)
		{
			extra = len;
		}
		for (i = offset; i < offset + extra; i++)
		{
			*b++ = thiz->last[i];
			len--;<commit_msg>In case we have read the stream already just exit<commit_after>/* ENESIM - Drawing Library
 * Copyright (C) 2007-2013 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "enesim_private.h"

#include "enesim_main.h"
#include "enesim_stream.h"
#include "enesim_stream_private.h"
#include <ctype.h>
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
/** @cond internal */
#define ENESIM_LOG_DEFAULT enesim_log_global

typedef struct _Enesim_Stream_Base64
{
	/* passed in */
	Enesim_Stream *data;
	/* last decoded data */
	unsigned char last[3];
	/* the next value to read */
	int last_offset;
	size_t enclen;
	size_t declen;
	char *buf;
	char *curr;
	char *end;
	ssize_t offset;
} Enesim_Stream_Base64;

static Eina_Bool _base64_decode_digit(unsigned char c, unsigned char *v)
{
	switch (c)
	{
		case '+': *v = 62;
		break;

		case '/': *v = 63;
		break;

		case '=': *v = 0;
		break;

		default:
		if (isdigit(c)) *v = c - '0' + 52;
		else if (islower(c)) *v = c - 'a' + 26;
		else if (isupper(c)) *v = c - 'A';
		else return EINA_FALSE;
		break;
	}
	return EINA_TRUE;
}

static void _base64_decode(unsigned char *in, unsigned char *out)
{
	out[0] = (unsigned char)(in[0] << 2 | in[1] >> 4);
	out[1] = (unsigned char)(in[1] << 4 | in[2] >> 2);
	out[2] = (unsigned char)(((in[2] << 6) & 0xc0) | in[3]);
}

/* 4 bytes in base64 give 3 decoded bytes
 * so the output mut be multiple of 3
 */
static void _base64_decode_stream(unsigned char *in, size_t ilen, int *iread,
		unsigned char *out, size_t olen, int *owrite)
{
	unsigned char dec[4];
	unsigned char *i = in;
	unsigned char *o = out;
	int count = 0;

	while (ilen > 0 && olen > 0)
	{
		if (!_base64_decode_digit(*i, &dec[count]))
			goto next;
		/* read the four input bytes */
		count++;
		if (count == 4)
		{
			_base64_decode(dec, o);
			count = 0;
			/* increment the buffer */
			o += 3;
			olen -= 3;
		}
next:
		ilen--;
		i++;
	}
	*iread = i - in;
	*owrite = o - out;
}
/*----------------------------------------------------------------------------*
 *                      The Enesim Image Data interface                       *
 *----------------------------------------------------------------------------*/
/* when the user requests 3 bytes of base64 decoded data we need to read 4 bytes
 * so we always read more from the real source
 */
static ssize_t _enesim_stream_base64_read(void *data, void *buffer, size_t len)
{
	Enesim_Stream_Base64 *thiz = data;
	unsigned char *b = buffer;
	int enclen;
	int declen;
	int decwrite;
	int encread;
	int rest;
	size_t extra = 0;
	size_t ret = 0;

	/* early exit */
	if (thiz->curr == thiz->end)
	{
		return 0;
	}
	/* first write the missing bytes from the previous read */
	if (thiz->last_offset)
	{
		int offset;
		size_t i;

		offset = thiz->last_offset;
		extra = 3 - offset;
		if (len < extra)
		{
			extra = len;
		}
		for (i = offset; i < offset + extra; i++)
		{
			*b++ = thiz->last[i];
			len--;
{'fraction_non_alphanumeric': 0.19878048780487806, 'fraction_numerical': 0.02046070460704607, 'mean_word_length': 2.5760658914728682, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '656045', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2406, 'n_words': 843}