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

#include "protonplus_core.h"
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.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

enum  {
	PROTON_PLUS_UTILS_ARCHIVE_HELPER_0_PROPERTY,
	PROTON_PLUS_UTILS_ARCHIVE_HELPER_NUM_PROPERTIES
};
static GParamSpec* proton_plus_utils_archive_helper_properties[PROTON_PLUS_UTILS_ARCHIVE_HELPER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))

static gpointer proton_plus_utils_archive_helper_parent_class = NULL;

static gchar** proton_plus_utils_archive_helper_get_supported_extensions (gboolean allow_plain_tar,
                                                                   gint* result_length1);
static GType proton_plus_utils_archive_helper_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static gchar**
proton_plus_utils_archive_helper_get_supported_extensions (gboolean allow_plain_tar,
                                                           gint* result_length1)
{
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	gchar** _tmp23_;
	gchar** _tmp24_;
	gint _tmp24__length1;
	gchar** result;
	if (allow_plain_tar) {
		gchar* _tmp0_;
		gchar* _tmp1_;
		gchar* _tmp2_;
		gchar* _tmp3_;
		gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		gchar** _tmp11_;
		gchar** _tmp12_;
		gint _tmp12__length1;
		_tmp0_ = g_strdup (".tar.gz");
		_tmp1_ = g_strdup (".tgz");
		_tmp2_ = g_strdup (".tar.xz");
		_tmp3_ = g_strdup (".tar.bz2");
		_tmp4_ = g_strdup (".tbz2");
		_tmp5_ = g_strdup (".tar.zst");
		_tmp6_ = g_strdup (".tzst");
		_tmp7_ = g_strdup (".zip");
		_tmp8_ = g_strdup (".7z");
		_tmp9_ = g_strdup (".rar");
		_tmp10_ = g_strdup (".tar");
		_tmp11_ = g_new0 (gchar*, 11 + 1);
		_tmp11_[0] = _tmp0_;
		_tmp11_[1] = _tmp1_;
		_tmp11_[2] = _tmp2_;
		_tmp11_[3] = _tmp3_;
		_tmp11_[4] = _tmp4_;
		_tmp11_[5] = _tmp5_;
		_tmp11_[6] = _tmp6_;
		_tmp11_[7] = _tmp7_;
		_tmp11_[8] = _tmp8_;
		_tmp11_[9] = _tmp9_;
		_tmp11_[10] = _tmp10_;
		_tmp12_ = _tmp11_;
		_tmp12__length1 = 11;
		if (result_length1) {
			*result_length1 = _tmp12__length1;
		}
		result = _tmp12_;
		return result;
	}
	_tmp13_ = g_strdup (".tar.gz");
	_tmp14_ = g_strdup (".tgz");
	_tmp15_ = g_strdup (".tar.xz");
	_tmp16_ = g_strdup (".tar.bz2");
	_tmp17_ = g_strdup (".tbz2");
	_tmp18_ = g_strdup (".tar.zst");
	_tmp19_ = g_strdup (".tzst");
	_tmp20_ = g_strdup (".zip");
	_tmp21_ = g_strdup (".7z");
	_tmp22_ = g_strdup (".rar");
	_tmp23_ = g_new0 (gchar*, 10 + 1);
	_tmp23_[0] = _tmp13_;
	_tmp23_[1] = _tmp14_;
	_tmp23_[2] = _tmp15_;
	_tmp23_[3] = _tmp16_;
	_tmp23_[4] = _tmp17_;
	_tmp23_[5] = _tmp18_;
	_tmp23_[6] = _tmp19_;
	_tmp23_[7] = _tmp20_;
	_tmp23_[8] = _tmp21_;
	_tmp23_[9] = _tmp22_;
	_tmp24_ = _tmp23_;
	_tmp24__length1 = 10;
	if (result_length1) {
		*result_length1 = _tmp24__length1;
	}
	result = _tmp24_;
	return result;
}

gchar*
proton_plus_utils_archive_helper_get_archive_extension (const gchar* value,
                                                        gboolean allow_plain_tar)
{
	gchar* lowered_value = NULL;
	gchar* _tmp0_;
	gint _tmp1_ = 0;
	gchar** _tmp2_;
	gchar* result;
	g_return_val_if_fail (value != NULL, NULL);
	_tmp0_ = g_ascii_strdown (value, (gssize) -1);
	lowered_value = _tmp0_;
	_tmp2_ = proton_plus_utils_archive_helper_get_supported_extensions (allow_plain_tar, &_tmp1_);
	{
		gchar** extension_collection = NULL;
		gint extension_collection_length1 = 0;
		gint _extension_collection_size_ = 0;
		gint extension_it = 0;
		extension_collection = _tmp2_;
		extension_collection_length1 = _tmp1_;
		for (extension_it = 0; extension_it < extension_collection_length1; extension_it = extension_it + 1) {
			gchar* _tmp3_;
			gchar* extension = NULL;
			_tmp3_ = g_strdup (extension_collection[extension_it]);
			extension = _tmp3_;
			{
				const gchar* _tmp4_;
				const gchar* _tmp5_;
				_tmp4_ = lowered_value;
				_tmp5_ = extension;
				if (g_str_has_suffix (_tmp4_, _tmp5_)) {
					result = extension;
					extension_collection = (_vala_array_free (extension_collection, extension_collection_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (lowered_value);
					return result;
				}
				_g_free0 (extension);
			}
		}
		extension_collection = (_vala_array_free (extension_collection, extension_collection_length1, (GDestroyNotify) g_free), NULL);
	}
	result = NULL;
	_g_free0 (lowered_value);
	return result;
}

gboolean
proton_plus_utils_archive_helper_is_archive_name (const gchar* value,
                                                  gboolean allow_plain_tar)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	gboolean _tmp2_;
	gboolean result;
	g_return_val_if_fail (value != NULL, FALSE);
	_tmp0_ = proton_plus_utils_archive_helper_get_archive_extension (value, allow_plain_tar);
	_tmp1_ = _tmp0_;
	_tmp2_ = _tmp1_ != NULL;
	_g_free0 (_tmp1_);
	result = _tmp2_;
	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*
proton_plus_utils_archive_helper_strip_archive_extension (const gchar* value,
                                                          gboolean allow_plain_tar)
{
	gchar* extension = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	gint _tmp3_;
	gint _tmp4_;
	const gchar* _tmp5_;
	gint _tmp6_;
	gint _tmp7_;
	gchar* _tmp8_;
	gchar* result;
	g_return_val_if_fail (value != NULL, NULL);
	_tmp0_ = proton_plus_utils_archive_helper_get_archive_extension (value, allow_plain_tar);
	extension = _tmp0_;
	_tmp1_ = extension;
	if (_tmp1_ == NULL) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (value);
		result = _tmp2_;
		_g_free0 (extension);
		return result;
	}
	_tmp3_ = strlen (value);
	_tmp4_ = _tmp3_;
	_tmp5_ = extension;
	_tmp6_ = strlen (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = string_substring (value, (glong) 0, (glong) (_tmp4_ - _tmp7_));
	result = _tmp8_;
	_g_free0 (extension);
	return result;
}

ProtonPlusUtilsArchiveHelper*
proton_plus_utils_archive_helper_construct (GType object_type)
{
	ProtonPlusUtilsArchiveHelper * self = NULL;
	self = (ProtonPlusUtilsArchiveHelper*) g_object_new (object_type, NULL);
	return self;
}

ProtonPlusUtilsArchiveHelper*
proton_plus_utils_archive_helper_new (void)
{
	return proton_plus_utils_archive_helper_construct (PROTON_PLUS_UTILS_TYPE_ARCHIVE_HELPER);
}

static void
proton_plus_utils_archive_helper_class_init (ProtonPlusUtilsArchiveHelperClass * klass,
                                             gpointer klass_data)
{
	proton_plus_utils_archive_helper_parent_class = g_type_class_peek_parent (klass);
}

static void
proton_plus_utils_archive_helper_instance_init (ProtonPlusUtilsArchiveHelper * self,
                                                gpointer klass)
{
}

static GType
proton_plus_utils_archive_helper_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ProtonPlusUtilsArchiveHelperClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) proton_plus_utils_archive_helper_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ProtonPlusUtilsArchiveHelper), 0, (GInstanceInitFunc) proton_plus_utils_archive_helper_instance_init, NULL };
	GType proton_plus_utils_archive_helper_type_id;
	proton_plus_utils_archive_helper_type_id = g_type_register_static (G_TYPE_OBJECT, "ProtonPlusUtilsArchiveHelper", &g_define_type_info, 0);
	return proton_plus_utils_archive_helper_type_id;
}

GType
proton_plus_utils_archive_helper_get_type (void)
{
	static volatile gsize proton_plus_utils_archive_helper_type_id__once = 0;
	if (g_once_init_enter (&proton_plus_utils_archive_helper_type_id__once)) {
		GType proton_plus_utils_archive_helper_type_id;
		proton_plus_utils_archive_helper_type_id = proton_plus_utils_archive_helper_get_type_once ();
		g_once_init_leave (&proton_plus_utils_archive_helper_type_id__once, proton_plus_utils_archive_helper_type_id);
	}
	return proton_plus_utils_archive_helper_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

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;
}

