/* util.c generated by valac 0.56.19, the Vala compiler
 * generated from util.vala, do not modify */

#include "omemo.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define _g_free0(var) (var = (g_free (var), NULL))

static guint8* _vala_array_dup38 (guint8* self,
                           gssize length);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static guint8*
_vala_array_dup38 (guint8* self,
                   gssize length)
{
	if (length > 0) {
		return _vala_memdup2 (self, length * sizeof (guint8));
	}
	return NULL;
}

gchar*
dino_plugins_omemo_fingerprint_from_base64 (const gchar* b64)
{
	guint8* arr = NULL;
	gsize _tmp0_ = 0;
	guchar* _tmp1_;
	gint arr_length1;
	gint _arr_size_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	guint8* _tmp3_;
	gint _tmp3__length1;
	guint8* _tmp4_;
	gint _tmp4__length1;
	gchar* s = NULL;
	gchar* _tmp5_;
	guint8* _tmp6_;
	gint _tmp6__length1;
	gchar* result;
	g_return_val_if_fail (b64 != NULL, NULL);
	_tmp1_ = g_base64_decode (b64, &_tmp0_);
	arr = _tmp1_;
	arr_length1 = _tmp0_;
	_arr_size_ = arr_length1;
	_tmp2_ = arr;
	_tmp2__length1 = arr_length1;
	_tmp3_ = arr;
	_tmp3__length1 = arr_length1;
	_tmp4_ = (_tmp2_ != NULL) ? _vala_array_dup38 (_tmp2_ + 1, _tmp3__length1 - 1) : _tmp2_;
	_tmp4__length1 = _tmp3__length1 - 1;
	arr = (g_free (arr), NULL);
	arr = _tmp4_;
	arr_length1 = _tmp4__length1;
	_arr_size_ = arr_length1;
	_tmp5_ = g_strdup ("");
	s = _tmp5_;
	_tmp6_ = arr;
	_tmp6__length1 = arr_length1;
	{
		guint8* i_collection = NULL;
		gint i_collection_length1 = 0;
		gint _i_collection_size_ = 0;
		gint i_it = 0;
		i_collection = _tmp6_;
		i_collection_length1 = _tmp6__length1;
		for (i_it = 0; i_it < i_collection_length1; i_it = i_it + 1) {
			guint8 i = 0U;
			i = i_collection[i_it];
			{
				gchar* tmp = NULL;
				gchar* _tmp7_;
				const gchar* _tmp8_;
				gint _tmp9_;
				gint _tmp10_;
				const gchar* _tmp13_;
				const gchar* _tmp14_;
				gchar* _tmp15_;
				_tmp7_ = g_strdup_printf ("%x", i);
				tmp = _tmp7_;
				_tmp8_ = tmp;
				_tmp9_ = strlen (_tmp8_);
				_tmp10_ = _tmp9_;
				if (_tmp10_ == 1) {
					const gchar* _tmp11_;
					gchar* _tmp12_;
					_tmp11_ = tmp;
					_tmp12_ = g_strconcat ("0", _tmp11_, NULL);
					_g_free0 (tmp);
					tmp = _tmp12_;
				}
				_tmp13_ = s;
				_tmp14_ = tmp;
				_tmp15_ = g_strconcat (_tmp13_, _tmp14_, NULL);
				_g_free0 (s);
				s = _tmp15_;
				_g_free0 (tmp);
			}
		}
	}
	result = s;
	arr = (g_free (arr), NULL);
	return result;
}

gchar*
dino_plugins_omemo_fingerprint_markup (const gchar* s)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	g_return_val_if_fail (s != NULL, NULL);
	_tmp0_ = dino_plugins_omemo_format_fingerprint (s);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat ("<span font_family='monospace' font='9'>", _tmp1_, NULL);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_strconcat (_tmp3_, "</span>", NULL);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_g_free0 (_tmp1_);
	result = _tmp5_;
	return result;
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

gchar*
dino_plugins_omemo_format_fingerprint (const gchar* s)
{
	gchar* markup = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (s != NULL, NULL);
	_tmp0_ = g_strdup ("");
	markup = _tmp0_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				gint _tmp2_;
				gint _tmp3_;
				gchar* four_chars = NULL;
				gchar* _tmp4_;
				gchar* _tmp5_;
				gchar* _tmp6_;
				gchar* _tmp7_;
				gboolean _tmp8_ = FALSE;
				const gchar* _tmp11_;
				const gchar* _tmp12_;
				gchar* _tmp13_;
				gboolean _tmp14_ = FALSE;
				gboolean _tmp17_ = FALSE;
				if (!_tmp1_) {
					i += 4;
				}
				_tmp1_ = FALSE;
				_tmp2_ = strlen (s);
				_tmp3_ = _tmp2_;
				if (!(i < _tmp3_)) {
					break;
				}
				_tmp4_ = string_substring (s, (glong) i, (glong) 4);
				_tmp5_ = _tmp4_;
				_tmp6_ = g_utf8_strdown (_tmp5_, (gssize) -1);
				_tmp7_ = _tmp6_;
				_g_free0 (_tmp5_);
				four_chars = _tmp7_;
				if ((i % 32) == 0) {
					_tmp8_ = i != 0;
				} else {
					_tmp8_ = FALSE;
				}
				if (_tmp8_) {
					const gchar* _tmp9_;
					gchar* _tmp10_;
					_tmp9_ = markup;
					_tmp10_ = g_strconcat (_tmp9_, "\n", NULL);
					_g_free0 (markup);
					markup = _tmp10_;
				}
				_tmp11_ = markup;
				_tmp12_ = four_chars;
				_tmp13_ = g_strconcat (_tmp11_, _tmp12_, NULL);
				_g_free0 (markup);
				markup = _tmp13_;
				if ((i % 16) == 12) {
					_tmp14_ = (i % 32) != 28;
				} else {
					_tmp14_ = FALSE;
				}
				if (_tmp14_) {
					const gchar* _tmp15_;
					gchar* _tmp16_;
					_tmp15_ = markup;
					_tmp16_ = g_strconcat (_tmp15_, " ", NULL);
					_g_free0 (markup);
					markup = _tmp16_;
				}
				if ((i % 8) == 4) {
					_tmp17_ = (i % 16) != 12;
				} else {
					_tmp17_ = FALSE;
				}
				if (_tmp17_) {
					const gchar* _tmp18_;
					gchar* _tmp19_;
					_tmp18_ = markup;
					_tmp19_ = g_strconcat (_tmp18_, "\u00a0", NULL);
					_g_free0 (markup);
					markup = _tmp19_;
				}
				_g_free0 (four_chars);
			}
		}
	}
	result = markup;
	return result;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

