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

/*///////////////////////////////////////////////////////////////////////*/
/* Copyright 2011-2021 Simon Schneegans*/
/**/
/* Permission is hereby granted, free of charge, to any person obtaining*/
/* a copy of this software and associated documentation files (the*/
/* "Software"), to deal in the Software without restriction, including*/
/* without limitation the rights to use, copy, modify, merge, publish,*/
/* distribute, sublicense, and/or sell copies of the Software, and to*/
/* permit persons to whom the Software is furnished to do so, subject to*/
/* the following conditions:*/
/* */
/* The above copyright notice and this permission notice shall be*/
/* included in all copies or substantial portions of the Software.*/
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,*/
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF*/
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY*/
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,*/
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE*/
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
/*///////////////////////////////////////////////////////////////////////*/

#include <glib-object.h>
#include <archive.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <archive_entry.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define GNOME_PIE_TYPE_ARCHIVE_READER (gnome_pie_archive_reader_get_type ())
#define GNOME_PIE_ARCHIVE_READER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNOME_PIE_TYPE_ARCHIVE_READER, GnomePieArchiveReader))
#define GNOME_PIE_ARCHIVE_READER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GNOME_PIE_TYPE_ARCHIVE_READER, GnomePieArchiveReaderClass))
#define GNOME_PIE_IS_ARCHIVE_READER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GNOME_PIE_TYPE_ARCHIVE_READER))
#define GNOME_PIE_IS_ARCHIVE_READER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNOME_PIE_TYPE_ARCHIVE_READER))
#define GNOME_PIE_ARCHIVE_READER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GNOME_PIE_TYPE_ARCHIVE_READER, GnomePieArchiveReaderClass))

typedef struct _GnomePieArchiveReader GnomePieArchiveReader;
typedef struct _GnomePieArchiveReaderClass GnomePieArchiveReaderClass;
typedef struct _GnomePieArchiveReaderPrivate GnomePieArchiveReaderPrivate;
enum  {
	GNOME_PIE_ARCHIVE_READER_0_PROPERTY,
	GNOME_PIE_ARCHIVE_READER_NUM_PROPERTIES
};
static GParamSpec* gnome_pie_archive_reader_properties[GNOME_PIE_ARCHIVE_READER_NUM_PROPERTIES];
#define _archive_read_free0(var) ((var == NULL) ? NULL : (var = (archive_read_free (var), NULL)))
#define _archive_write_free0(var) ((var == NULL) ? NULL : (var = (archive_write_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _GnomePieArchiveReader {
	GObject parent_instance;
	GnomePieArchiveReaderPrivate * priv;
};

struct _GnomePieArchiveReaderClass {
	GObjectClass parent_class;
};

struct _GnomePieArchiveReaderPrivate {
	struct archive* archive;
	struct archive* writer;
};

static gint GnomePieArchiveReader_private_offset;
static gpointer gnome_pie_archive_reader_parent_class = NULL;

VALA_EXTERN GType gnome_pie_archive_reader_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GnomePieArchiveReader, g_object_unref)
VALA_EXTERN GnomePieArchiveReader* gnome_pie_archive_reader_new (void);
VALA_EXTERN GnomePieArchiveReader* gnome_pie_archive_reader_construct (GType object_type);
VALA_EXTERN gboolean gnome_pie_archive_reader_open (GnomePieArchiveReader* self,
                                        const gchar* path);
VALA_EXTERN gboolean gnome_pie_archive_reader_extract_to (GnomePieArchiveReader* self,
                                              const gchar* directory);
static guint8* _vala_array_dup2 (guint8* self,
                          gssize length);
VALA_EXTERN void gnome_pie_archive_reader_close (GnomePieArchiveReader* self);
static void gnome_pie_archive_reader_finalize (GObject * obj);
static GType gnome_pie_archive_reader_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
gnome_pie_archive_reader_get_instance_private (GnomePieArchiveReader* self)
{
	return G_STRUCT_MEMBER_P (self, GnomePieArchiveReader_private_offset);
}

GnomePieArchiveReader*
gnome_pie_archive_reader_construct (GType object_type)
{
	GnomePieArchiveReader * self = NULL;
	struct archive* _tmp0_;
	struct archive* _tmp1_;
	struct archive* _tmp2_;
	struct archive* _tmp3_;
	struct archive* _tmp4_;
	struct archive* _tmp5_;
	self = (GnomePieArchiveReader*) g_object_new (object_type, NULL);
	_tmp0_ = archive_read_new ();
	_archive_read_free0 (self->priv->archive);
	self->priv->archive = _tmp0_;
	_tmp1_ = self->priv->archive;
	archive_read_support_format_all (_tmp1_);
	_tmp2_ = self->priv->archive;
	archive_read_support_filter_all (_tmp2_);
	_tmp3_ = archive_write_disk_new ();
	_archive_write_free0 (self->priv->writer);
	self->priv->writer = _tmp3_;
	_tmp4_ = self->priv->writer;
	archive_write_disk_set_options (_tmp4_, ((ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM) | ARCHIVE_EXTRACT_ACL) | ARCHIVE_EXTRACT_FFLAGS);
	_tmp5_ = self->priv->writer;
	archive_write_disk_set_standard_lookup (_tmp5_);
	return self;
}

GnomePieArchiveReader*
gnome_pie_archive_reader_new (void)
{
	return gnome_pie_archive_reader_construct (GNOME_PIE_TYPE_ARCHIVE_READER);
}

gboolean
gnome_pie_archive_reader_open (GnomePieArchiveReader* self,
                               const gchar* path)
{
	struct archive* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (path != NULL, FALSE);
	_tmp0_ = self->priv->archive;
	result = archive_read_open_filename (_tmp0_, path, (gsize) 10240) == ARCHIVE_OK;
	return result;
}

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

gboolean
gnome_pie_archive_reader_extract_to (GnomePieArchiveReader* self,
                                     const gchar* directory)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (directory != NULL, FALSE);
	while (TRUE) {
		struct archive_entry* entry = NULL;
		int r = 0;
		struct archive* _tmp0_;
		struct archive_entry* _tmp1_ = NULL;
		int _tmp2_;
		struct archive_entry* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		struct archive_entry* _tmp8_;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		struct archive* _tmp12_;
		struct archive_entry* _tmp13_;
		struct archive_entry* _tmp16_;
		struct archive* _tmp27_;
		_tmp0_ = self->priv->archive;
		_tmp2_ = archive_read_next_header (_tmp0_, &_tmp1_);
		entry = _tmp1_;
		r = _tmp2_;
		if (r == ARCHIVE_EOF) {
			break;
		}
		if (r != ARCHIVE_OK) {
			struct archive* _tmp3_;
			const gchar* _tmp4_;
			_tmp3_ = self->priv->archive;
			_tmp4_ = archive_error_string (_tmp3_);
			g_warning ("archiveReader.vala:77: %s", _tmp4_);
			result = FALSE;
			return result;
		}
		_tmp5_ = entry;
		_tmp6_ = g_strconcat (directory, "/", NULL);
		_tmp7_ = _tmp6_;
		_tmp8_ = entry;
		_tmp9_ = archive_entry_pathname (_tmp8_);
		_tmp10_ = g_strconcat (_tmp7_, _tmp9_, NULL);
		_tmp11_ = _tmp10_;
		archive_entry_set_pathname (_tmp5_, _tmp11_);
		_g_free0 (_tmp11_);
		_g_free0 (_tmp7_);
		_tmp12_ = self->priv->writer;
		_tmp13_ = entry;
		r = archive_write_header (_tmp12_, _tmp13_);
		if (r != ARCHIVE_OK) {
			struct archive* _tmp14_;
			const gchar* _tmp15_;
			_tmp14_ = self->priv->writer;
			_tmp15_ = archive_error_string (_tmp14_);
			g_warning ("archiveReader.vala:86: %s", _tmp15_);
			result = FALSE;
			return result;
		}
		_tmp16_ = entry;
		if (archive_entry_size (_tmp16_) > ((la_int64_t) 0)) {
			while (TRUE) {
				gsize offset = 0UL;
				guint8* buff = NULL;
				gint buff_length1 = 0;
				gint _buff_size_ = 0;
				struct archive* _tmp17_;
				guint8* _tmp18_ = NULL;
				size_t _tmp19_ = 0;
				la_int64_t _tmp20_ = {0};
				int _tmp21_;
				guint8* _tmp22_;
				gint _tmp22__length1;
				struct archive* _tmp25_;
				guint8* _tmp26_;
				gint _tmp26__length1;
				_tmp17_ = self->priv->archive;
				_tmp21_ = archive_read_data_block (_tmp17_, &_tmp18_, &_tmp19_, &_tmp20_);
				buff = (g_free (buff), NULL);
				_tmp22_ = (_tmp18_ != NULL) ? _vala_array_dup2 (_tmp18_, _tmp19_) : _tmp18_;
				_tmp22__length1 = _tmp19_;
				buff = _tmp22_;
				buff_length1 = _tmp22__length1;
				_buff_size_ = buff_length1;
				offset = (gsize) _tmp20_;
				r = _tmp21_;
				if (r == ARCHIVE_EOF) {
					buff = (g_free (buff), NULL);
					break;
				}
				if (r != ARCHIVE_OK) {
					struct archive* _tmp23_;
					const gchar* _tmp24_;
					_tmp23_ = self->priv->archive;
					_tmp24_ = archive_error_string (_tmp23_);
					g_warning ("archiveReader.vala:107: %s", _tmp24_);
					result = FALSE;
					buff = (g_free (buff), NULL);
					return result;
				}
				_tmp25_ = self->priv->writer;
				_tmp26_ = buff;
				_tmp26__length1 = buff_length1;
				archive_write_data_block (_tmp25_, _tmp26_, (size_t) _tmp26__length1, (la_int64_t) offset);
				buff = (g_free (buff), NULL);
			}
		}
		_tmp27_ = self->priv->writer;
		r = archive_write_finish_entry (_tmp27_);
		if (r != ARCHIVE_OK) {
			struct archive* _tmp28_;
			const gchar* _tmp29_;
			_tmp28_ = self->priv->writer;
			_tmp29_ = archive_error_string (_tmp28_);
			g_warning ("archiveReader.vala:122: %s", _tmp29_);
			result = FALSE;
			return result;
		}
	}
	result = TRUE;
	return result;
}

void
gnome_pie_archive_reader_close (GnomePieArchiveReader* self)
{
	struct archive* _tmp0_;
	struct archive* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->archive;
	archive_read_close (_tmp0_);
	_tmp1_ = self->priv->writer;
	archive_write_close (_tmp1_);
}

static void
gnome_pie_archive_reader_class_init (GnomePieArchiveReaderClass * klass,
                                     gpointer klass_data)
{
	gnome_pie_archive_reader_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GnomePieArchiveReader_private_offset);
	G_OBJECT_CLASS (klass)->finalize = gnome_pie_archive_reader_finalize;
}

static void
gnome_pie_archive_reader_instance_init (GnomePieArchiveReader * self,
                                        gpointer klass)
{
	self->priv = gnome_pie_archive_reader_get_instance_private (self);
}

static void
gnome_pie_archive_reader_finalize (GObject * obj)
{
	GnomePieArchiveReader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_PIE_TYPE_ARCHIVE_READER, GnomePieArchiveReader);
	_archive_read_free0 (self->priv->archive);
	_archive_write_free0 (self->priv->writer);
	G_OBJECT_CLASS (gnome_pie_archive_reader_parent_class)->finalize (obj);
}

static GType
gnome_pie_archive_reader_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GnomePieArchiveReaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gnome_pie_archive_reader_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GnomePieArchiveReader), 0, (GInstanceInitFunc) gnome_pie_archive_reader_instance_init, NULL };
	GType gnome_pie_archive_reader_type_id;
	gnome_pie_archive_reader_type_id = g_type_register_static (G_TYPE_OBJECT, "GnomePieArchiveReader", &g_define_type_info, 0);
	GnomePieArchiveReader_private_offset = g_type_add_instance_private (gnome_pie_archive_reader_type_id, sizeof (GnomePieArchiveReaderPrivate));
	return gnome_pie_archive_reader_type_id;
}

GType
gnome_pie_archive_reader_get_type (void)
{
	static volatile gsize gnome_pie_archive_reader_type_id__once = 0;
	if (g_once_init_enter (&gnome_pie_archive_reader_type_id__once)) {
		GType gnome_pie_archive_reader_type_id;
		gnome_pie_archive_reader_type_id = gnome_pie_archive_reader_get_type_once ();
		g_once_init_leave (&gnome_pie_archive_reader_type_id__once, gnome_pie_archive_reader_type_id);
	}
	return gnome_pie_archive_reader_type_id__once;
}

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

