/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here. Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include "circularbuffer.h"

#define DEFAULT_BUF_SIZE 256

/******************************************************************************
 * Structs
 *****************************************************************************/
typedef struct {
	/* A pointer to the starting address of our chunk of memory. */
	gchar *buffer;

	/* The incremental amount to increase this buffer by when the buffer is not
	 * big enough to hold incoming data, in bytes. */
	gsize growsize;

	/* The length of this buffer, in bytes. */
	gsize buflen;

	/* The number of bytes of this buffer that contain unread data. */
	gsize bufused;

	/* A pointer to the next byte where new incoming data is buffered to. */
	gchar *input;

	/* A pointer to the next byte of buffered data that should be read by the
	 * consumer. */
	gchar *output;
} PurpleCircularBufferPrivate;

/******************************************************************************
 * Enums
 *****************************************************************************/
enum {
	PROP_0,
	PROP_GROW_SIZE,
	PROP_BUFFER_USED,
	PROP_INPUT,
	PROP_OUTPUT,
	N_PROPERTIES,
};

/******************************************************************************
 * Globals
 *****************************************************************************/
static GParamSpec *properties[N_PROPERTIES] = {NULL, };

G_DEFINE_TYPE_WITH_PRIVATE(PurpleCircularBuffer, purple_circular_buffer,
		G_TYPE_OBJECT);

/******************************************************************************
 * Circular Buffer Implementation
 *****************************************************************************/
static void
purple_circular_buffer_real_grow(PurpleCircularBuffer *buffer, gsize len) {
	PurpleCircularBufferPrivate *priv = NULL;
	gsize in_offset = 0, out_offset = 0;
	gsize start_buflen;
	GObject *obj;

	priv = purple_circular_buffer_get_instance_private(buffer);

	start_buflen = priv->buflen;

	while((priv->buflen - priv->bufused) < len)
		priv->buflen += priv->growsize;

	if(priv->input != NULL) {
		in_offset = priv->input - priv->buffer;
		out_offset = priv->output - priv->buffer;
	}

	priv->buffer = g_realloc(priv->buffer, priv->buflen);

	/* adjust the fill and remove pointer locations */
	if(priv->input == NULL) {
		priv->input = priv->output = priv->buffer;
	} else {
		priv->input = priv->buffer + in_offset;
		priv->output = priv->buffer + out_offset;
	}

	/* If the fill pointer is wrapped to before the remove
	 * pointer, we need to shift the data */
	if (in_offset < out_offset
			|| (in_offset == out_offset && priv->bufused > 0))
	{
		gsize shift_n = MIN(priv->buflen - start_buflen, in_offset);
		memcpy(priv->buffer + start_buflen, priv->buffer, shift_n);

		/* If we couldn't fit the wrapped read buffer at the end */
		if (shift_n < in_offset) {
			memmove(priv->buffer, priv->buffer + shift_n, in_offset - shift_n);
			priv->input = priv->buffer + (in_offset - shift_n);
		} else {
			priv->input = priv->buffer + start_buflen + in_offset;
		}
	}

	obj = G_OBJECT(buffer);
	g_object_freeze_notify(obj);
	g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
	g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
	g_object_thaw_notify(obj);
}

static void
purple_circular_buffer_real_append(PurpleCircularBuffer *buffer,
                                   gconstpointer src, gsize len)
{
	PurpleCircularBufferPrivate *priv = NULL;
	gsize len_stored;
	GObject *obj;

	priv = purple_circular_buffer_get_instance_private(buffer);

	/* Grow the buffer, if necessary */
	if((priv->buflen - priv->bufused) < len)
		purple_circular_buffer_grow(buffer, len);

	/* If there is not enough room to copy all of src before hitting
	 * the end of the buffer then we will need to do two copies.
	 * One copy from input to the end of the buffer, and the
	 * second copy from the start of the buffer to the end of src. */
	if(priv->input >= priv->output)
		len_stored = MIN(len, priv->buflen - (priv->input - priv->buffer));
	else
		len_stored = len;

	if(len_stored > 0)
		memcpy(priv->input, src, len_stored);

	if(len_stored < len) {
		memcpy(priv->buffer, (char*)src + len_stored, len - len_stored);
		priv->input = priv->buffer + (len - len_stored);
	} else {
		priv->input += len_stored;
	}

	priv->bufused += len;

	obj = G_OBJECT(buffer);
	g_object_freeze_notify(obj);
	g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]);
	g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
	g_object_thaw_notify(obj);
}

static gsize
purple_circular_buffer_real_max_read_size(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferPrivate *priv = NULL;
	gsize max_read;

	priv = purple_circular_buffer_get_instance_private(buffer);

	if(priv->bufused == 0)
		max_read = 0;
	else if((priv->output - priv->input) >= 0)
		max_read = priv->buflen - (priv->output - priv->buffer);
	else
		max_read = priv->input - priv->output;

	return max_read;
}

static gboolean
purple_circular_buffer_real_mark_read(PurpleCircularBuffer *buffer,
                                      gsize len)
{
	PurpleCircularBufferPrivate *priv = NULL;
	GObject *obj;

	g_return_val_if_fail(purple_circular_buffer_get_max_read(buffer) >= len, FALSE);

	priv = purple_circular_buffer_get_instance_private(buffer);

	priv->output += len;
	priv->bufused -= len;

	/* wrap to the start if we're at the end */
	if ((gsize)(priv->output - priv->buffer) == priv->buflen)
		priv->output = priv->buffer;

	obj = G_OBJECT(buffer);
	g_object_freeze_notify(obj);
	g_object_notify_by_pspec(obj, properties[PROP_BUFFER_USED]);
	g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
	g_object_thaw_notify(obj);

	return TRUE;
}

/******************************************************************************
 * Private API
 *****************************************************************************/
static void
purple_circular_buffer_set_grow_size(PurpleCircularBuffer *buffer,
                                     gsize grow_size)
{
	PurpleCircularBufferPrivate *priv =
			purple_circular_buffer_get_instance_private(buffer);

	priv->growsize = (grow_size != 0) ? grow_size : DEFAULT_BUF_SIZE;

	g_object_notify_by_pspec(G_OBJECT(buffer), properties[PROP_GROW_SIZE]);
}

static const gchar *
purple_circular_buffer_get_input(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL);

	priv = purple_circular_buffer_get_instance_private(buffer);

	return priv->input;
}

/******************************************************************************
 * Object Stuff
 *****************************************************************************/
static void
purple_circular_buffer_init(G_GNUC_UNUSED PurpleCircularBuffer *buffer)
{
}

static void
purple_circular_buffer_finalize(GObject *obj) {
	PurpleCircularBufferPrivate *priv =
			purple_circular_buffer_get_instance_private(
					PURPLE_CIRCULAR_BUFFER(obj));

	g_free(priv->buffer);

	G_OBJECT_CLASS(purple_circular_buffer_parent_class)->finalize(obj);
}

static void
purple_circular_buffer_get_property(GObject *obj, guint param_id,
                                    GValue *value, GParamSpec *pspec)
{
	PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);

	switch(param_id) {
		case PROP_GROW_SIZE:
			g_value_set_uint64(value,
			                   purple_circular_buffer_get_grow_size(buffer));
			break;
		case PROP_BUFFER_USED:
			g_value_set_uint64(value, purple_circular_buffer_get_used(buffer));
			break;
		case PROP_INPUT:
			g_value_set_pointer(value,
			                    (void*) purple_circular_buffer_get_input(buffer));
			break;
		case PROP_OUTPUT:
			g_value_set_pointer(value,
			                    (void*) purple_circular_buffer_get_output(buffer));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_circular_buffer_set_property(GObject *obj, guint param_id,
                                    const GValue *value, GParamSpec *pspec)
{
	PurpleCircularBuffer *buffer = PURPLE_CIRCULAR_BUFFER(obj);

	switch(param_id) {
		case PROP_GROW_SIZE:
			purple_circular_buffer_set_grow_size(buffer,
			                                     g_value_get_uint64(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
purple_circular_buffer_class_init(PurpleCircularBufferClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	PurpleCircularBufferClass *buffer_class = PURPLE_CIRCULAR_BUFFER_CLASS(klass);

	obj_class->finalize = purple_circular_buffer_finalize;
	obj_class->get_property = purple_circular_buffer_get_property;
	obj_class->set_property = purple_circular_buffer_set_property;

	buffer_class->grow = purple_circular_buffer_real_grow;
	buffer_class->append = purple_circular_buffer_real_append;
	buffer_class->max_read_size = purple_circular_buffer_real_max_read_size;
	buffer_class->mark_read = purple_circular_buffer_real_mark_read;

	/**
	 * PurpleCircularBuffer:grow-size:
	 *
	 * The grow size of the buffer.
	 *
	 * Since: 3.0
	 */
	properties[PROP_GROW_SIZE] = g_param_spec_uint64(
		"grow-size", NULL, NULL,
		0, G_MAXSIZE, 0,
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleCircularBuffer:buffer-used:
	 *
	 * How much of the buffer that has been used.
	 *
	 * Since: 3.0
	 */
	properties[PROP_BUFFER_USED] = g_param_spec_uint64(
		"buffer-used", NULL, NULL,
		0, G_MAXSIZE, 0,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleCircularBuffer:input:
	 *
	 * The input pointer of the buffer.
	 *
	 * Since: 3.0
	 */
	properties[PROP_INPUT] = g_param_spec_pointer(
		"input", NULL, NULL,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	/**
	 * PurpleCircularBuffer:output:
	 *
	 * The output pointer of the buffer.
	 *
	 * Since: 3.0
	 */
	properties[PROP_OUTPUT] = g_param_spec_pointer(
		"output", NULL, NULL,
		G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
}

/******************************************************************************
 * API
 *****************************************************************************/
PurpleCircularBuffer *
purple_circular_buffer_new(gsize growsize) {
	return g_object_new(PURPLE_TYPE_CIRCULAR_BUFFER,
	                    "grow-size", (guint64)(growsize ? growsize : DEFAULT_BUF_SIZE),
	                    NULL);
}

void
purple_circular_buffer_grow(PurpleCircularBuffer *buffer, gsize len) {
	PurpleCircularBufferClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));

	klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
	if(klass && klass->grow)
		klass->grow(buffer, len);
}

void
purple_circular_buffer_append(PurpleCircularBuffer *buffer, gconstpointer src,
                              gsize len)
{
	PurpleCircularBufferClass *klass = NULL;

	g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));
	g_return_if_fail(src != NULL);

	klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
	if(klass && klass->append)
		klass->append(buffer, src, len);
}

gsize
purple_circular_buffer_get_max_read(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);

	klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
	if(klass && klass->max_read_size)
		return klass->max_read_size(buffer);

	return 0;
}

gboolean
purple_circular_buffer_mark_read(PurpleCircularBuffer *buffer, gsize len) {
	PurpleCircularBufferClass *klass = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), FALSE);

	klass = PURPLE_CIRCULAR_BUFFER_GET_CLASS(buffer);
	if(klass && klass->mark_read)
		return klass->mark_read(buffer, len);

	return FALSE;
}

gsize
purple_circular_buffer_get_grow_size(PurpleCircularBuffer *buffer) {

	PurpleCircularBufferPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);

	priv = purple_circular_buffer_get_instance_private(buffer);

	return priv->growsize;
}

gsize
purple_circular_buffer_get_used(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), 0);

	priv = purple_circular_buffer_get_instance_private(buffer);

	return priv->bufused;
}

const gchar *
purple_circular_buffer_get_output(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferPrivate *priv = NULL;

	g_return_val_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer), NULL);

	priv = purple_circular_buffer_get_instance_private(buffer);

	return priv->output;
}

void
purple_circular_buffer_reset(PurpleCircularBuffer *buffer) {
	PurpleCircularBufferPrivate *priv = NULL;
	GObject *obj;

	g_return_if_fail(PURPLE_IS_CIRCULAR_BUFFER(buffer));

	priv = purple_circular_buffer_get_instance_private(buffer);

	priv->input = priv->buffer;
	priv->output = priv->buffer;

	obj = G_OBJECT(buffer);
	g_object_freeze_notify(obj);
	g_object_notify_by_pspec(obj, properties[PROP_INPUT]);
	g_object_notify_by_pspec(obj, properties[PROP_OUTPUT]);
	g_object_thaw_notify(obj);
}

