/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.24 from the
 * contents of GOST.xs. Do not edit this file, edit GOST.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "GOST.xs"
#define PERL_NO_GET_CONTEXT

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"

#include "src/gost.c"

static int
hex_encode (char *dest, const unsigned char *src, int len) {
    static const char hex[] = "0123456789abcdef";
    char *p = dest;
    const unsigned char *s = src;
    for (; len--; s++) {
        *p++ = hex[s[0] >> 4];
        *p++ = hex[s[0] & 0x0f];
    }
    return (int)(p - dest);
}

static int
base64_encode (char *dest, const unsigned char *src, int len) {
    static const char b64[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    char *p = dest;
    const unsigned char *s = src;
    const unsigned char *end = src + len - 2;

    for (; s < end; s += 3) {
        *p++ = b64[s[0] >> 2];
        *p++ = b64[((s[0] & 3) << 4) + (s[1] >> 4)];
        *p++ = b64[((s[1] & 0xf) << 2) + (s[2] >> 6)];
        *p++ = b64[s[2] & 0x3f];
    }
    switch (len % 3) {
    case 1:
        *p++ = b64[s[0] >> 2];
        *p++ = b64[(s[0] & 3) << 4];
        break;
    case 2:
        *p++ = b64[s[0] >> 2];
        *p++ = b64[((s[0] & 3) << 4) + (s[1] >> 4)];
        *p++ = b64[((s[1] & 0xf) << 2)];
        break;
    }
    return (int)(p - dest);
}

static SV *
make_mortal_sv(pTHX_ const unsigned char *src, int len, int enc) {
    char result[64];
    char *ret;

    switch (enc) {
    case 0:
        ret = (char *)src;
        break;
    case 1:
        len = hex_encode(result, src, len);
        ret = result;
        break;
    case 2:
        len = base64_encode(result, src, len);
        ret = result;
        break;
    }
    return sv_2mortal(newSVpv(ret, len));
}

typedef gost_ctx *Digest__GOST;
typedef gost_ctx *Digest__GOST__CryptoPro;

#line 84 "GOST.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
            Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
        else
            Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
        Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#ifdef PERL_IMPLICIT_CONTEXT
#define croak_xs_usage(a,b)    S_croak_xs_usage(aTHX_ a,b)
#else
#define croak_xs_usage        S_croak_xs_usage
#endif

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#line 226 "GOST.c"

XS_EUPXS(XS_Digest__GOST_gost); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_gost)
{
    dVAR; dXSARGS;
    dXSI32;
    PERL_UNUSED_VAR(cv); /* -W */
    {
#line 85 "GOST.xs"
    gost_ctx ctx;
    int i;
    unsigned char *data;
    unsigned char result[32];
    STRLEN len;
#line 241 "GOST.c"
#line 91 "GOST.xs"
    rhash_gost_init(&ctx);
    for (i = 0; i < items; i++) {
        data = (unsigned char *)(SvPV(ST(i), len));
        rhash_gost_update(&ctx, data, len);
    }
    rhash_gost_final(&ctx, result);
    ST(0) = make_mortal_sv(aTHX_ result, 32, ix);
    XSRETURN(1);
#line 251 "GOST.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST_new); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_new)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "class");
    {
	SV *	class = ST(0)
;
	Digest__GOST	RETVAL;
#line 104 "GOST.xs"
    Newx(RETVAL, 1, gost_ctx);
    rhash_gost_init(RETVAL);
#line 270 "GOST.c"
	ST(0) = sv_newmortal();
	sv_setref_pv(ST(0), "Digest::GOST", (void*)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST_clone); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_clone)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    {
	Digest__GOST	self;
	Digest__GOST	RETVAL;

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			"Digest::GOST::clone",
			"self", "Digest::GOST")
;
#line 113 "GOST.xs"
    Newx(RETVAL, 1, gost_ctx);
    Copy(self, RETVAL, 1, gost_ctx);
#line 300 "GOST.c"
	ST(0) = sv_newmortal();
	sv_setref_pv(ST(0), "Digest::GOST", (void*)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST_reset); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_reset)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Digest__GOST	self;

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			"Digest::GOST::reset",
			"self", "Digest::GOST")
;
#line 122 "GOST.xs"
    rhash_gost_init(self);
    XSRETURN(1);
#line 331 "GOST.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Digest__GOST_add); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_add)
{
    dVAR; dXSARGS;
    if (items < 1)
       croak_xs_usage(cv,  "self, ...");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Digest__GOST	self;
#line 129 "GOST.xs"
    int i;
    unsigned char *data;
    STRLEN len;
#line 352 "GOST.c"

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			"Digest::GOST::add",
			"self", "Digest::GOST")
;
#line 133 "GOST.xs"
    for (i = 1; i < items; i++) {
        data = (unsigned char *)(SvPV(ST(i), len));
        rhash_gost_update(self, data, len);
    }
    XSRETURN(1);
#line 369 "GOST.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Digest__GOST_digest); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_digest)
{
    dVAR; dXSARGS;
    dXSI32;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    {
	Digest__GOST	self;
#line 147 "GOST.xs"
    unsigned char result[32];
#line 387 "GOST.c"

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			GvNAME(CvGV(cv)),
			"self", "Digest::GOST")
;
#line 149 "GOST.xs"
    rhash_gost_final(self, result);
    rhash_gost_init(self);
    ST(0) = make_mortal_sv(aTHX_ result, 32, ix);
    XSRETURN(1);
#line 403 "GOST.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST_DESTROY); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST_DESTROY)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    {
	Digest__GOST	self;

	if (SvROK(ST(0))) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not a reference",
			"Digest::GOST::DESTROY",
			"self")
;
#line 158 "GOST.xs"
    Safefree(self);
#line 429 "GOST.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Digest__GOST__CryptoPro_gost); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST__CryptoPro_gost)
{
    dVAR; dXSARGS;
    dXSI32;
    PERL_UNUSED_VAR(cv); /* -W */
    {
#line 169 "GOST.xs"
    gost_ctx ctx;
    int i;
    unsigned char *data;
    unsigned char result[32];
    STRLEN len;
#line 448 "GOST.c"
#line 175 "GOST.xs"
    rhash_gost_cryptopro_init(&ctx);
    for (i = 0; i < items; i++) {
        data = (unsigned char *)(SvPV(ST(i), len));
        rhash_gost_update(&ctx, data, len);
    }
    rhash_gost_final(&ctx, result);
    ST(0) = make_mortal_sv(aTHX_ result, 32, ix);
    XSRETURN(1);
#line 458 "GOST.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST__CryptoPro_new); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST__CryptoPro_new)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "class");
    {
	SV *	class = ST(0)
;
	Digest__GOST__CryptoPro	RETVAL;
#line 188 "GOST.xs"
    Newx(RETVAL, 1, gost_ctx);
    rhash_gost_cryptopro_init(RETVAL);
#line 477 "GOST.c"
	ST(0) = sv_newmortal();
	sv_setref_pv(ST(0), "Digest::GOST::CryptoPro", (void*)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Digest__GOST__CryptoPro_reset); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST__CryptoPro_reset)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Digest__GOST__CryptoPro	self;

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST::CryptoPro")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST__CryptoPro,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			"Digest::GOST::CryptoPro::reset",
			"self", "Digest::GOST::CryptoPro")
;
#line 197 "GOST.xs"
    rhash_gost_cryptopro_init(self);
    XSRETURN(1);
#line 508 "GOST.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Digest__GOST__CryptoPro_digest); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Digest__GOST__CryptoPro_digest)
{
    dVAR; dXSARGS;
    dXSI32;
    if (items != 1)
       croak_xs_usage(cv,  "self");
    {
	Digest__GOST__CryptoPro	self;
#line 208 "GOST.xs"
    unsigned char result[32];
#line 526 "GOST.c"

	if (SvROK(ST(0)) && sv_derived_from(ST(0), "Digest::GOST::CryptoPro")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    self = INT2PTR(Digest__GOST__CryptoPro,tmp);
	}
	else
	    Perl_croak_nocontext("%s: %s is not of type %s",
			GvNAME(CvGV(cv)),
			"self", "Digest::GOST::CryptoPro")
;
#line 210 "GOST.xs"
    rhash_gost_final(self, result);
    rhash_gost_cryptopro_init(self);
    ST(0) = make_mortal_sv(aTHX_ result, 32, ix);
    XSRETURN(1);
#line 542 "GOST.c"
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Digest__GOST); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Digest__GOST)
{
    dVAR; dXSARGS;
#if (PERL_REVISION == 5 && PERL_VERSION < 9)
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#endif
    XS_VERSION_BOOTCHECK;

    {
        CV * cv;

        cv = newXSproto_portable("Digest::GOST::gost", XS_Digest__GOST_gost, file, ";@");
        XSANY.any_i32 = 0;
        cv = newXSproto_portable("Digest::GOST::gost_base64", XS_Digest__GOST_gost, file, ";@");
        XSANY.any_i32 = 2;
        cv = newXSproto_portable("Digest::GOST::gost_hex", XS_Digest__GOST_gost, file, ";@");
        XSANY.any_i32 = 1;
        (void)newXSproto_portable("Digest::GOST::new", XS_Digest__GOST_new, file, "$");
        (void)newXSproto_portable("Digest::GOST::clone", XS_Digest__GOST_clone, file, "$");
        (void)newXSproto_portable("Digest::GOST::reset", XS_Digest__GOST_reset, file, "$");
        (void)newXSproto_portable("Digest::GOST::add", XS_Digest__GOST_add, file, "$;@");
        cv = newXSproto_portable("Digest::GOST::b64digest", XS_Digest__GOST_digest, file, "$");
        XSANY.any_i32 = 2;
        cv = newXSproto_portable("Digest::GOST::digest", XS_Digest__GOST_digest, file, "$");
        XSANY.any_i32 = 0;
        cv = newXSproto_portable("Digest::GOST::hexdigest", XS_Digest__GOST_digest, file, "$");
        XSANY.any_i32 = 1;
        (void)newXSproto_portable("Digest::GOST::DESTROY", XS_Digest__GOST_DESTROY, file, "$");
        cv = newXSproto_portable("Digest::GOST::CryptoPro::gost", XS_Digest__GOST__CryptoPro_gost, file, ";@");
        XSANY.any_i32 = 0;
        cv = newXSproto_portable("Digest::GOST::CryptoPro::gost_base64", XS_Digest__GOST__CryptoPro_gost, file, ";@");
        XSANY.any_i32 = 2;
        cv = newXSproto_portable("Digest::GOST::CryptoPro::gost_hex", XS_Digest__GOST__CryptoPro_gost, file, ";@");
        XSANY.any_i32 = 1;
        (void)newXSproto_portable("Digest::GOST::CryptoPro::new", XS_Digest__GOST__CryptoPro_new, file, "$");
        (void)newXSproto_portable("Digest::GOST::CryptoPro::reset", XS_Digest__GOST__CryptoPro_reset, file, "$");
        cv = newXSproto_portable("Digest::GOST::CryptoPro::b64digest", XS_Digest__GOST__CryptoPro_digest, file, "$");
        XSANY.any_i32 = 2;
        cv = newXSproto_portable("Digest::GOST::CryptoPro::digest", XS_Digest__GOST__CryptoPro_digest, file, "$");
        XSANY.any_i32 = 0;
        cv = newXSproto_portable("Digest::GOST::CryptoPro::hexdigest", XS_Digest__GOST__CryptoPro_digest, file, "$");
        XSANY.any_i32 = 1;
    }
#if (PERL_REVISION == 5 && PERL_VERSION >= 9)
  if (PL_unitcheckav)
       call_list(PL_scopestack_ix, PL_unitcheckav);
#endif
    XSRETURN_YES;
}

