<commit_before>/**
 * FreeRDP: A Remote Desktop Protocol client.
 * NSCodec Codec
 *
 * Copyright 2011 Samsung, Author <NAME>
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <freerdp/codec/nsc.h>
#include <freerdp/utils/memory.h>

#include "nsc_types.h"

#ifndef NSC_INIT_SIMD
#define NSC_INIT_SIMD(_nsc_context) do { } while (0)
#endif

#define ROUND_UP_TO(_b, _n) (_b + ((~(_b & (_n-1)) + 0x1) & (_n-1)))
#define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))

static void nsc_decode(NSC_CONTEXT* context)
{
	uint16 x;
	uint16 y;
	uint16 rw;
	uint8 shift;
	uint8* yplane;
	uint8* coplane;
	uint8* cgplane;
	uint8* aplane;
	sint16 y_val;
	sint16 co_val;
	sint16 cg_val;
	sint16 r_val;
	sint16 g_val;
	sint16 b_val;
	uint8* bmpdata;

	bmpdata = context->bmpdata;
	rw = ROUND_UP_TO(context->width, 8);
	shift = context->nsc_stream.ColorLossLevel - 1; /* colorloss recovery + YCoCg shift */

	for (y = 0; y < context->height; y++)
	{
		if (context->nsc_stream.ChromaSubSamplingLevel > 0)
		{
			yplane = context->priv->plane_buf[0] + y * rw; /* Y */
			coplane = context->priv->plane_buf[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
			cgplane = context->priv->plane_buf[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
		}
		else
		{
			yplane = context->priv->plane_buf[0] + y * context->width; /* Y */
			coplane = context->priv->plane_buf[1] + y * context->width; /* Co */
			cgplane = context->priv->plane_buf[2] + y * context->width; /* Cg */
		}
		aplane = context->priv->plane_buf[3] + y * context->width; /* A */
		for (x = 0; x < context->width; x++)
		{
			y_val = (sint16) *yplane;
			co_val = (sint16) (sint8) (*coplane << shift);
			cg_val = (sint16) (sint8) (*cgplane << shift);
			r_val = y_val + co_val - cg_val;
			g_val = y_val + cg_val;
			b_val = y_val - co_val - cg_val;
			*bmpdata++ = MINMAX(b_val, 0, 0xFF);
			*bmpdata++ = MINMAX(g_val, 0, 0xFF);
			*bmpdata++ = MINMAX(r_val, 0, 0xFF);
			*bmpdata++ = *aplane;
			yplane++;
			coplane += (context->nsc_stream.ChromaSubSamplingLevel > 0 ? x % 2 : 1);
			cgplane += (context->nsc_stream.ChromaSubSamplingLevel > 0 ? x % 2 : 1);
			aplane++;
		}
	}
}

static void nsc_rle_decode(uint8* in, uint8* out, uint32 origsz)
{
	int i;
	uint32 len;
	uint32 left;
	uint8 value;

	left = origsz;
	while (left > 4)
	{
		value = *in++;

		if (left == 5)
		{
			*out++ = value;
			left--;
		}
		else if (value == *in)
		{
			in++;
			if (*in < 0xFF)
			{
				len = (uint32) *in++;
				len += 2;
			}
			else
			{
				in++;
				len = *((uint32*) in);
				in += 4;
			}
			for (i = 0; i < len; i++)
				*out++ = value;
			left -= len;
		}
		else
		{
			*out++ = value;
			left--;
		}
	}

	*((uint32*)out) = *((uint32*)in);
}

static void nsc_rle_decompress_data(NSC_CONTEXT* context)
{<commit_msg>nsc: use memcpy instead of loop.<commit_after>/**
 * FreeRDP: A Remote Desktop Protocol client.
 * NSCodec Codec
 *
 * Copyright 2011 Samsung, Author <NAME>
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <freerdp/codec/nsc.h>
#include <freerdp/utils/memory.h>

#include "nsc_types.h"

#ifndef NSC_INIT_SIMD
#define NSC_INIT_SIMD(_nsc_context) do { } while (0)
#endif

#define ROUND_UP_TO(_b, _n) (_b + ((~(_b & (_n-1)) + 0x1) & (_n-1)))
#define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))

static void nsc_decode(NSC_CONTEXT* context)
{
	uint16 x;
	uint16 y;
	uint16 rw;
	uint8 shift;
	uint8* yplane;
	uint8* coplane;
	uint8* cgplane;
	uint8* aplane;
	sint16 y_val;
	sint16 co_val;
	sint16 cg_val;
	sint16 r_val;
	sint16 g_val;
	sint16 b_val;
	uint8* bmpdata;

	bmpdata = context->bmpdata;
	rw = ROUND_UP_TO(context->width, 8);
	shift = context->nsc_stream.ColorLossLevel - 1; /* colorloss recovery + YCoCg shift */

	for (y = 0; y < context->height; y++)
	{
		if (context->nsc_stream.ChromaSubSamplingLevel > 0)
		{
			yplane = context->priv->plane_buf[0] + y * rw; /* Y */
			coplane = context->priv->plane_buf[1] + (y >> 1) * (rw >> 1); /* Co, supersampled */
			cgplane = context->priv->plane_buf[2] + (y >> 1) * (rw >> 1); /* Cg, supersampled */
		}
		else
		{
			yplane = context->priv->plane_buf[0] + y * context->width; /* Y */
			coplane = context->priv->plane_buf[1] + y * context->width; /* Co */
			cgplane = context->priv->plane_buf[2] + y * context->width; /* Cg */
		}
		aplane = context->priv->plane_buf[3] + y * context->width; /* A */
		for (x = 0; x < context->width; x++)
		{
			y_val = (sint16) *yplane;
			co_val = (sint16) (sint8) (*coplane << shift);
			cg_val = (sint16) (sint8) (*cgplane << shift);
			r_val = y_val + co_val - cg_val;
			g_val = y_val + cg_val;
			b_val = y_val - co_val - cg_val;
			*bmpdata++ = MINMAX(b_val, 0, 0xFF);
			*bmpdata++ = MINMAX(g_val, 0, 0xFF);
			*bmpdata++ = MINMAX(r_val, 0, 0xFF);
			*bmpdata++ = *aplane;
			yplane++;
			coplane += (context->nsc_stream.ChromaSubSamplingLevel > 0 ? x % 2 : 1);
			cgplane += (context->nsc_stream.ChromaSubSamplingLevel > 0 ? x % 2 : 1);
			aplane++;
		}
	}
}

static void nsc_rle_decode(uint8* in, uint8* out, uint32 origsz)
{
	uint32 len;
	uint32 left;
	uint8 value;

	left = origsz;
	while (left > 4)
	{
		value = *in++;

		if (left == 5)
		{
			*out++ = value;
			left--;
		}
		else if (value == *in)
		{
			in++;
			if (*in < 0xFF)
			{
				len = (uint32) *in++;
				len += 2;
			}
			else
			{
				in++;
				len = *((uint32*) in);
				in += 4;
			}
			memset(out, value, len);
			out += len;
			left -= len;
		}
		else
		{
			*out++ = value;
			left--;
		}
	}

	*((uint32*)out) = *((uint32*)in);
}

static void nsc_rle_decompress_data(NSC_CONTEXT* context)
{
{'fraction_non_alphanumeric': 0.16933235509904623, 'fraction_numerical': 0.029200293470286135, 'mean_word_length': 3.1918819188191883, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2605493', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2762, 'n_words': 716}