/*
 * This file was generated automatically by xsubpp version 1.9508 from the
 * contents of LibMagic.xs. Do not edit this file, edit LibMagic.xs instead.
 *
 *	ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "LibMagic.xs"
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <magic.h>
#include <string.h>

#include "const-c.inc"

/* I don't know anything about perlxs, just trying my best. ;)
*/

#line 23 "LibMagic.c"

/* INCLUDE:  Including 'const-xs.inc' from 'LibMagic.xs' */


XS(XS_File__LibMagic_constant); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_constant)
{
    dXSARGS;
    if (items != 1)
	Perl_croak(aTHX_ "Usage: File::LibMagic::constant(sv)");
    SP -= items;
    {
#line 4 "const-xs.inc"
#ifdef dXSTARG
	dXSTARG; /* Faster if we have it.  */
#else
	dTARGET;
#endif
	STRLEN		len;
        int		type;
	IV		iv;
	/* NV		nv;	Uncomment this if you need to return NVs */
	/* const char	*pv;	Uncomment this if you need to return PVs */
#line 47 "LibMagic.c"
	SV *	sv = ST(0);
	const char *	s = SvPV(sv, len);
#line 18 "const-xs.inc"
        /* Change this to constant(aTHX_ s, len, &iv, &nv);
           if you need to return both NVs and IVs */
	type = constant(aTHX_ s, len, &iv);
      /* Return 1 or 2 items. First is error message, or undef if no error.
           Second, if present, is found value */
        switch (type) {
        case PERL_constant_NOTFOUND:
          sv = sv_2mortal(newSVpvf("%s is not a valid File::LibMagic macro", s));
          PUSHs(sv);
          break;
        case PERL_constant_NOTDEF:
          sv = sv_2mortal(newSVpvf(
	    "Your vendor has not defined File::LibMagic macro %s, used", s));
          PUSHs(sv);
          break;
        case PERL_constant_ISIV:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHi(iv);
          break;
	/* Uncomment this if you need to return NOs
        case PERL_constant_ISNO:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHs(&PL_sv_no);
          break; */
	/* Uncomment this if you need to return NVs
        case PERL_constant_ISNV:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHn(nv);
          break; */
	/* Uncomment this if you need to return PVs
        case PERL_constant_ISPV:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHp(pv, strlen(pv));
          break; */
	/* Uncomment this if you need to return PVNs
        case PERL_constant_ISPVN:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHp(pv, iv);
          break; */
	/* Uncomment this if you need to return SVs
        case PERL_constant_ISSV:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHs(sv);
          break; */
	/* Uncomment this if you need to return UNDEFs
        case PERL_constant_ISUNDEF:
          break; */
	/* Uncomment this if you need to return UVs
        case PERL_constant_ISUV:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHu((UV)iv);
          break; */
	/* Uncomment this if you need to return YESs
        case PERL_constant_ISYES:
          EXTEND(SP, 1);
          PUSHs(&PL_sv_undef);
          PUSHs(&PL_sv_yes);
          break; */
        default:
          sv = sv_2mortal(newSVpvf(
	    "Unexpected return type %d while processing File::LibMagic macro %s, used",
               type, s));
          PUSHs(sv);
        }
#line 122 "LibMagic.c"
	PUTBACK;
	return;
    }
}


/* INCLUDE: Returning to 'LibMagic.xs' from 'const-xs.inc' */


XS(XS_File__LibMagic_MagicBuffer); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_MagicBuffer)
{
    dXSARGS;
    if (items != 1)
	Perl_croak(aTHX_ "Usage: File::LibMagic::MagicBuffer(buffer)");
    {
	SV *	buffer = ST(0);
#line 23 "LibMagic.xs"
         char * ret;
         STRLEN len;
         int ret_i;
         char * buffer_value;
         magic_t m;
#line 146 "LibMagic.c"
	SV *	RETVAL;
#line 29 "LibMagic.xs"
	     /* First make sure they actually gave us a defined scalar */
         if ( !SvOK(buffer) ) {
            Perl_croak(aTHX_ "MagicBuffer requires defined content");
         }

         m = magic_open(MAGIC_NONE);
         if ( m == NULL ) {
             Perl_croak(aTHX_ "libmagic out of memory");
         }
         ret_i = magic_load(m,NULL);
         if ( ret_i < 0 ) {
             Perl_croak(aTHX_ "libmagic %s", magic_error(m));
         }
         buffer_value = SvPV(buffer, len);
         ret = (char*) magic_buffer(m,buffer_value,len);
         if ( ret == NULL ) {
             Perl_croak(aTHX_ "libmagic %s", magic_error(m));
         }
         RETVAL = newSVpvn(ret, strlen(ret));
         magic_close(m);
#line 169 "LibMagic.c"
	ST(0) = RETVAL;
	sv_2mortal(ST(0));
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_MagicFile); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_MagicFile)
{
    dXSARGS;
    if (items != 1)
	Perl_croak(aTHX_ "Usage: File::LibMagic::MagicFile(buffer)");
    {
	SV *	buffer = ST(0);
#line 55 "LibMagic.xs"
         char * ret;
         int ret_i;
         magic_t m;
         char * buffer_value;
#line 190 "LibMagic.c"
	SV *	RETVAL;
#line 60 "LibMagic.xs"
	     /* First make sure they actually gave us a defined scalar */
         if ( !SvOK(buffer) ) {
            Perl_croak(aTHX_ "MagicFile requires a filename");
         }

         m = magic_open(MAGIC_NONE);
         if ( m == NULL ) {
             Perl_croak(aTHX_ "libmagic out of memory");
         }
         ret_i = magic_load(m,NULL);
         if ( ret_i < 0 ) {
             Perl_croak(aTHX_ "libmagic %s", magic_error(m));
         }
	 buffer_value = SvPV_nolen(buffer);
	 ret=(char*) magic_file(m,buffer_value);
         if ( ret == NULL ) {
             Perl_croak(aTHX_ "libmagic %s", magic_error(m));
         }
         RETVAL = newSVpvn(ret, strlen(ret));
         magic_close(m);
#line 213 "LibMagic.c"
	ST(0) = RETVAL;
	sv_2mortal(ST(0));
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_magic_open); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_open)
{
    dXSARGS;
    if (items != 1)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_open(flags)");
    {
	int	flags = (int)SvIV(ST(0));
#line 87 "LibMagic.xs"
       	    magic_t m;
#line 231 "LibMagic.c"
	IV	RETVAL;
	dXSTARG;
#line 89 "LibMagic.xs"
             m=magic_open(flags);
             if ( m == NULL ) {
                 Perl_croak( aTHX_ "libmagic out of memory" );
             }
	         RETVAL=(long) m;
#line 240 "LibMagic.c"
	XSprePUSH; PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_magic_close); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_close)
{
    dXSARGS;
    if (items != 1)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_close(handle)");
    {
	long	handle = (long)SvIV(ST(0));
#line 100 "LibMagic.xs"
		magic_t m;
#line 257 "LibMagic.c"
#line 102 "LibMagic.xs"
        if ( !handle ) {
            Perl_croak( aTHX_ "magic_close requires a defined handle" );
        }
		m=(magic_t) handle;
		magic_close(m);
#line 264 "LibMagic.c"
    }
    XSRETURN_EMPTY;
}


XS(XS_File__LibMagic_magic_load); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_load)
{
    dXSARGS;
    if (items != 2)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_load(handle, dbnames)");
    {
	long	handle = (long)SvIV(ST(0));
	SV *	dbnames = ST(1);
#line 112 "LibMagic.xs"
		magic_t m;
		STRLEN len = 0;
		char * dbnames_value;
#line 283 "LibMagic.c"
	IV	RETVAL;
	dXSTARG;
#line 116 "LibMagic.xs"
        if ( !handle ) {
            Perl_croak( aTHX_ "magic_load requires a defined handle" );
        }
		m=(magic_t) handle;
		if ( SvOK(dbnames) ) {  // is dbnames defined?
		    dbnames_value = SvPV(dbnames, len);
		}
		RETVAL = ! magic_load(m, len > 0 ? dbnames_value : NULL);
        if ( RETVAL < 0 ) {
            Perl_croak( aTHX_ "libmagic %s", magic_error(m) );
        }
#line 298 "LibMagic.c"
	XSprePUSH; PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_magic_buffer); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_buffer)
{
    dXSARGS;
    if (items != 2)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_buffer(handle, buffer)");
    {
	long	handle = (long)SvIV(ST(0));
	SV *	buffer = ST(1);
#line 134 "LibMagic.xs"
		magic_t m;
		char * ret;
		STRLEN len;
		char * buffer_value;
#line 319 "LibMagic.c"
	SV *	RETVAL;
#line 139 "LibMagic.xs"
        if ( !handle ) {
            Perl_croak( aTHX_ "magic_buffer requires a defined handle" );
        }
        /* First make sure they actually gave us a defined scalar */
        if ( !SvOK(buffer) ) {
            Perl_croak(aTHX_ "magic_buffer requires defined content");
        }

		m = (magic_t) handle;
        buffer_value = SvPV(buffer, len);
        ret = (char*) magic_buffer(m,buffer_value,len);
        if ( ret == NULL ) {
            Perl_croak(aTHX_ "libmagic %s", magic_error(m));
        }
        RETVAL = newSVpvn(ret, strlen(ret));
#line 337 "LibMagic.c"
	ST(0) = RETVAL;
	sv_2mortal(ST(0));
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_magic_buffer_offset); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_buffer_offset)
{
    dXSARGS;
    if (items != 4)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_buffer_offset(handle, buffer, offset, BuffLen)");
    {
	long	handle = (long)SvIV(ST(0));
	SV *	buffer = ST(1);
	long	offset = (long)SvIV(ST(2));
	long	BuffLen = (long)SvIV(ST(3));
#line 163 "LibMagic.xs"
		magic_t m;
		char * ret;
		STRLEN len;
		char * buffer_value;
		long MyLen;
#line 362 "LibMagic.c"
	SV *	RETVAL;
#line 169 "LibMagic.xs"
        if ( !handle ) {
            Perl_croak( aTHX_ "magic_buffer requires a defined handle" );
        }
        /* First make sure they actually gave us a defined scalar */
        if ( !SvOK(buffer) ) {
            Perl_croak(aTHX_ "magic_buffer requires defined content");
        }

		m = (magic_t) handle;
        buffer_value = SvPV(buffer, len);
	/* FIXME check length for out of bound errors */
	MyLen= (long) BuffLen;
        ret = (char*) magic_buffer(m, (char *) &buffer_value[ (long) offset],MyLen);
        if ( ret == NULL ) {
            Perl_croak(aTHX_ "libmagic %s", magic_error(m));
        }
        RETVAL = newSVpvn(ret, strlen(ret));
#line 382 "LibMagic.c"
	ST(0) = RETVAL;
	sv_2mortal(ST(0));
    }
    XSRETURN(1);
}


XS(XS_File__LibMagic_magic_file); /* prototype to pass -Wmissing-prototypes */
XS(XS_File__LibMagic_magic_file)
{
    dXSARGS;
    if (items != 2)
	Perl_croak(aTHX_ "Usage: File::LibMagic::magic_file(handle, buffer)");
    {
	long	handle = (long)SvIV(ST(0));
	SV *	buffer = ST(1);
#line 193 "LibMagic.xs"
         char * ret;
         char * buffer_value;
         magic_t m;
#line 403 "LibMagic.c"
	SV *	RETVAL;
#line 197 "LibMagic.xs"
         if ( !handle ) {
             Perl_croak( aTHX_ "magic_file requires a defined handle" );
         }
         /* First make sure they actually gave us a defined scalar */
         if ( !SvOK(buffer) ) {
             Perl_croak(aTHX_ "magic_file requires a filename");
         }

         m = (magic_t) handle;
         buffer_value = SvPV_nolen(buffer);
         ret = (char*) magic_file(m,buffer_value);
         if ( ret == NULL ) {
             Perl_croak(aTHX_ "libmagic %s", magic_error(m));
         }
         RETVAL = newSVpvn(ret, strlen(ret));
#line 421 "LibMagic.c"
	ST(0) = RETVAL;
	sv_2mortal(ST(0));
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS(boot_File__LibMagic); /* prototype to pass -Wmissing-prototypes */
XS(boot_File__LibMagic)
{
    dXSARGS;
    char* file = __FILE__;

    XS_VERSION_BOOTCHECK ;

        newXS("File::LibMagic::constant", XS_File__LibMagic_constant, file);
        newXSproto("File::LibMagic::MagicBuffer", XS_File__LibMagic_MagicBuffer, file, "$");
        newXSproto("File::LibMagic::MagicFile", XS_File__LibMagic_MagicFile, file, "$");
        newXSproto("File::LibMagic::magic_open", XS_File__LibMagic_magic_open, file, "$");
        newXSproto("File::LibMagic::magic_close", XS_File__LibMagic_magic_close, file, "$");
        newXSproto("File::LibMagic::magic_load", XS_File__LibMagic_magic_load, file, "$$");
        newXSproto("File::LibMagic::magic_buffer", XS_File__LibMagic_magic_buffer, file, "$$");
        newXSproto("File::LibMagic::magic_buffer_offset", XS_File__LibMagic_magic_buffer_offset, file, "$$$$");
        newXSproto("File::LibMagic::magic_file", XS_File__LibMagic_magic_file, file, "$$");
    XSRETURN_YES;
}

