/* smtp-plain-authenticator.c generated by valac 0.56.19, the Vala compiler
 * generated from smtp-plain-authenticator.vala, do not modify */

/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

#include "geary-engine.h"
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.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  {
	GEARY_SMTP_PLAIN_AUTHENTICATOR_0_PROPERTY,
	GEARY_SMTP_PLAIN_AUTHENTICATOR_NUM_PROPERTIES
};
static GParamSpec* geary_smtp_plain_authenticator_properties[GEARY_SMTP_PLAIN_AUTHENTICATOR_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_bytes_unref0(var) ((var == NULL) ? NULL : (var = (g_bytes_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer geary_smtp_plain_authenticator_parent_class = NULL;
static guint8* geary_smtp_plain_authenticator_nul;
static gint geary_smtp_plain_authenticator_nul_length1;
static guint8* geary_smtp_plain_authenticator_nul = NULL;
static gint geary_smtp_plain_authenticator_nul_length1 = 0;
static gint _geary_smtp_plain_authenticator_nul_size_ = 0;

static GearySmtpRequest* geary_smtp_plain_authenticator_real_initiate (GearySmtpAuthenticator* base);
static GearyMemoryBuffer* geary_smtp_plain_authenticator_real_challenge (GearySmtpAuthenticator* base,
                                                                  gint step,
                                                                  GearySmtpResponse* response,
                                                                  GError** error);
static void geary_smtp_plain_authenticator_finalize (GObject * obj);
static GType geary_smtp_plain_authenticator_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);

GearySmtpPlainAuthenticator*
geary_smtp_plain_authenticator_construct (GType object_type,
                                          GearyCredentials* credentials)
{
	GearySmtpPlainAuthenticator * self = NULL;
	g_return_val_if_fail (GEARY_IS_CREDENTIALS (credentials), NULL);
	self = (GearySmtpPlainAuthenticator*) geary_smtp_authenticator_construct (object_type, "PLAIN", credentials);
	return self;
}

GearySmtpPlainAuthenticator*
geary_smtp_plain_authenticator_new (GearyCredentials* credentials)
{
	return geary_smtp_plain_authenticator_construct (GEARY_SMTP_TYPE_PLAIN_AUTHENTICATOR, credentials);
}

static GearySmtpRequest*
geary_smtp_plain_authenticator_real_initiate (GearySmtpAuthenticator* base)
{
	GearySmtpPlainAuthenticator * self;
	gchar* _tmp0_;
	gchar** _tmp1_;
	gchar** _tmp2_;
	gint _tmp2__length1;
	GearySmtpRequest* _tmp3_;
	GearySmtpRequest* _tmp4_;
	GearySmtpRequest* result;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_SMTP_TYPE_PLAIN_AUTHENTICATOR, GearySmtpPlainAuthenticator);
	_tmp0_ = g_strdup ("PLAIN");
	_tmp1_ = g_new0 (gchar*, 1 + 1);
	_tmp1_[0] = _tmp0_;
	_tmp2_ = _tmp1_;
	_tmp2__length1 = 1;
	_tmp3_ = geary_smtp_request_new (GEARY_SMTP_COMMAND_AUTH, _tmp2_, 1);
	_tmp4_ = _tmp3_;
	_tmp2_ = (_vala_array_free (_tmp2_, _tmp2__length1, (GDestroyNotify) g_free), NULL);
	result = _tmp4_;
	return result;
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static GearyMemoryBuffer*
geary_smtp_plain_authenticator_real_challenge (GearySmtpAuthenticator* base,
                                               gint step,
                                               GearySmtpResponse* response,
                                               GError** error)
{
	GearySmtpPlainAuthenticator * self;
	GearyMemoryGrowableBuffer* growable = NULL;
	GearyMemoryGrowableBuffer* _tmp0_;
	GearyMemoryGrowableBuffer* _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	GearyMemoryGrowableBuffer* _tmp3_;
	GearyCredentials* _tmp4_;
	GearyCredentials* _tmp5_;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	guint8* _tmp8_;
	gint _tmp8__length1;
	gint _tmp9_ = 0;
	guint8* _tmp10_;
	gint _tmp10__length1;
	GearyMemoryGrowableBuffer* _tmp11_;
	guint8* _tmp12_;
	gint _tmp12__length1;
	const gchar* _tmp13_ = NULL;
	GearyCredentials* _tmp14_;
	GearyCredentials* _tmp15_;
	const gchar* _tmp16_;
	const gchar* _tmp17_;
	GearyMemoryGrowableBuffer* _tmp18_;
	guint8* _tmp19_;
	gint _tmp19__length1;
	gint _tmp20_ = 0;
	guint8* _tmp21_;
	gint _tmp21__length1;
	GearyMemoryGrowableBuffer* _tmp22_;
	GBytes* _tmp23_;
	GBytes* _tmp24_;
	gsize _tmp25_ = 0;
	guint8* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	GearyMemoryStringBuffer* _tmp29_;
	GearyMemoryBuffer* _tmp30_;
	GearyMemoryBuffer* result;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_SMTP_TYPE_PLAIN_AUTHENTICATOR, GearySmtpPlainAuthenticator);
	g_return_val_if_fail (GEARY_SMTP_IS_RESPONSE (response), NULL);
	if (step > 0) {
		result = NULL;
		return result;
	}
	_tmp0_ = geary_memory_growable_buffer_new ();
	growable = _tmp0_;
	_tmp1_ = growable;
	_tmp2_ = geary_smtp_plain_authenticator_nul;
	_tmp2__length1 = geary_smtp_plain_authenticator_nul_length1;
	geary_memory_growable_buffer_append (_tmp1_, _tmp2_, (gint) _tmp2__length1);
	_tmp3_ = growable;
	_tmp4_ = geary_smtp_authenticator_get_credentials (G_TYPE_CHECK_INSTANCE_CAST (self, GEARY_SMTP_TYPE_AUTHENTICATOR, GearySmtpAuthenticator));
	_tmp5_ = _tmp4_;
	_tmp6_ = geary_credentials_get_user (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = string_get_data (_tmp7_, &_tmp9_);
	_tmp8__length1 = _tmp9_;
	_tmp10_ = _tmp8_;
	_tmp10__length1 = _tmp8__length1;
	geary_memory_growable_buffer_append (_tmp3_, _tmp10_, (gint) _tmp10__length1);
	_tmp11_ = growable;
	_tmp12_ = geary_smtp_plain_authenticator_nul;
	_tmp12__length1 = geary_smtp_plain_authenticator_nul_length1;
	geary_memory_growable_buffer_append (_tmp11_, _tmp12_, (gint) _tmp12__length1);
	_tmp14_ = geary_smtp_authenticator_get_credentials (G_TYPE_CHECK_INSTANCE_CAST (self, GEARY_SMTP_TYPE_AUTHENTICATOR, GearySmtpAuthenticator));
	_tmp15_ = _tmp14_;
	_tmp16_ = geary_credentials_get_token (_tmp15_);
	_tmp17_ = _tmp16_;
	_tmp13_ = _tmp17_;
	if (_tmp13_ == NULL) {
		_tmp13_ = "";
	}
	_tmp18_ = growable;
	_tmp19_ = string_get_data (_tmp13_, &_tmp20_);
	_tmp19__length1 = _tmp20_;
	_tmp21_ = _tmp19_;
	_tmp21__length1 = _tmp19__length1;
	geary_memory_growable_buffer_append (_tmp18_, _tmp21_, (gint) _tmp21__length1);
	_tmp22_ = growable;
	_tmp23_ = geary_memory_buffer_get_bytes (G_TYPE_CHECK_INSTANCE_CAST (_tmp22_, GEARY_MEMORY_TYPE_BUFFER, GearyMemoryBuffer));
	_tmp24_ = _tmp23_;
	_tmp26_ = g_bytes_get_data (_tmp24_, &_tmp25_);
	_tmp27_ = g_base64_encode (_tmp26_, (gint) _tmp25_);
	_tmp28_ = _tmp27_;
	_tmp29_ = geary_memory_string_buffer_new (_tmp28_);
	_tmp30_ = G_TYPE_CHECK_INSTANCE_CAST (_tmp29_, GEARY_MEMORY_TYPE_BUFFER, GearyMemoryBuffer);
	_g_free0 (_tmp28_);
	_g_bytes_unref0 (_tmp24_);
	result = _tmp30_;
	_g_object_unref0 (growable);
	return result;
}

static void
geary_smtp_plain_authenticator_class_init (GearySmtpPlainAuthenticatorClass * klass,
                                           gpointer klass_data)
{
	guint8* _tmp0_;
	geary_smtp_plain_authenticator_parent_class = g_type_class_peek_parent (klass);
	((GearySmtpAuthenticatorClass *) klass)->initiate = (GearySmtpRequest* (*) (GearySmtpAuthenticator*)) geary_smtp_plain_authenticator_real_initiate;
	((GearySmtpAuthenticatorClass *) klass)->challenge = (GearyMemoryBuffer* (*) (GearySmtpAuthenticator*, gint, GearySmtpResponse*, GError**)) geary_smtp_plain_authenticator_real_challenge;
	G_OBJECT_CLASS (klass)->finalize = geary_smtp_plain_authenticator_finalize;
	_tmp0_ = g_new0 (guint8, 1);
	_tmp0_[0] = (guint8) '\0';
	geary_smtp_plain_authenticator_nul = _tmp0_;
	geary_smtp_plain_authenticator_nul_length1 = 1;
	_geary_smtp_plain_authenticator_nul_size_ = geary_smtp_plain_authenticator_nul_length1;
}

static void
geary_smtp_plain_authenticator_instance_init (GearySmtpPlainAuthenticator * self,
                                              gpointer klass)
{
}

static void
geary_smtp_plain_authenticator_finalize (GObject * obj)
{
	GearySmtpPlainAuthenticator * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GEARY_SMTP_TYPE_PLAIN_AUTHENTICATOR, GearySmtpPlainAuthenticator);
	G_OBJECT_CLASS (geary_smtp_plain_authenticator_parent_class)->finalize (obj);
}

/**
 * SASL's PLAIN authentication schema implemented as an {@link Authenticator}.
 *
 * See [[http://tools.ietf.org/html/rfc4616]]
 */
 G_GNUC_NO_INLINE static GType
geary_smtp_plain_authenticator_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GearySmtpPlainAuthenticatorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) geary_smtp_plain_authenticator_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GearySmtpPlainAuthenticator), 0, (GInstanceInitFunc) geary_smtp_plain_authenticator_instance_init, NULL };
	GType geary_smtp_plain_authenticator_type_id;
	geary_smtp_plain_authenticator_type_id = g_type_register_static (GEARY_SMTP_TYPE_AUTHENTICATOR, "GearySmtpPlainAuthenticator", &g_define_type_info, 0);
	return geary_smtp_plain_authenticator_type_id;
}

GType
geary_smtp_plain_authenticator_get_type (void)
{
	static gsize geary_smtp_plain_authenticator_type_id__once = 0;
	if (g_once_init_enter (&geary_smtp_plain_authenticator_type_id__once)) {
		GType geary_smtp_plain_authenticator_type_id;
		geary_smtp_plain_authenticator_type_id = geary_smtp_plain_authenticator_get_type_once ();
		g_once_init_leave (&geary_smtp_plain_authenticator_type_id__once, geary_smtp_plain_authenticator_type_id);
	}
	return geary_smtp_plain_authenticator_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);
}

