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

#include "openpgp.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 inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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_open_pgp_markup_id (const gchar* s,
                                 gboolean is_fingerprint)
{
	const gchar* _tmp0_ = NULL;
	gchar* markup = NULL;
	gchar* _tmp1_;
	const gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* result;
	g_return_val_if_fail (s != NULL, NULL);
	if (is_fingerprint) {
		_tmp0_ = "";
	} else {
		_tmp0_ = "0x";
	}
	_tmp1_ = g_strdup (_tmp0_);
	markup = _tmp1_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				gint _tmp3_;
				gint _tmp4_;
				gchar* four_chars = NULL;
				gchar* _tmp5_;
				gchar* _tmp6_;
				gchar* _tmp7_;
				gchar* _tmp8_;
				const gchar* _tmp11_;
				const gchar* _tmp12_;
				gchar* _tmp13_;
				if (!_tmp2_) {
					i += 4;
				}
				_tmp2_ = FALSE;
				_tmp3_ = strlen (s);
				_tmp4_ = _tmp3_;
				if (!(i < _tmp4_)) {
					break;
				}
				_tmp5_ = string_substring (s, (glong) i, (glong) 4);
				_tmp6_ = _tmp5_;
				_tmp7_ = g_utf8_strdown (_tmp6_, (gssize) -1);
				_tmp8_ = _tmp7_;
				_g_free0 (_tmp6_);
				four_chars = _tmp8_;
				if (i == (4 * 5)) {
					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 (is_fingerprint) {
					const gchar* _tmp14_;
					gchar* _tmp15_;
					_tmp14_ = markup;
					_tmp15_ = g_strconcat (_tmp14_, " ", NULL);
					_g_free0 (markup);
					markup = _tmp15_;
				}
				_g_free0 (four_chars);
			}
		}
	}
	_tmp16_ = markup;
	_tmp17_ = g_strconcat ("<span font_family='monospace' font='9'>", _tmp16_, NULL);
	_tmp18_ = _tmp17_;
	_tmp19_ = g_strconcat (_tmp18_, "</span>", NULL);
	_tmp20_ = _tmp19_;
	_g_free0 (_tmp18_);
	result = _tmp20_;
	_g_free0 (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;
}

