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

#line 1 "Expat.xs"
/*****************************************************************
** Expat.xs
**
** Copyright 1998 Larry Wall and Clark Cooper
** All rights reserved.
**
** This program is free software; you can redistribute it and/or
** modify it under the same terms as Perl itself.
**
*/

#include <expat.h>

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

#undef convert

#include "encoding.h"

#define BUFSIZE 32768

#define NSDELIM  '|'

/* Macro to update handler fields. Used in the various handler setting
   XSUBS */

#define XMLP_UPD(fld) \
  RETVAL = cbv->fld ? newSVsv(cbv->fld) : &PL_sv_undef;\
  if (cbv->fld) {\
    if (cbv->fld != fld)\
      sv_setsv(cbv->fld, fld);\
  }\
  else\
    cbv->fld = newSVsv(fld)

/* Macro to push old handler value onto return stack. */

#define PUSHRET \
  ST(0) = RETVAL;\
  if (RETVAL != &PL_sv_undef && SvREFCNT(RETVAL)) sv_2mortal(RETVAL)

typedef struct {
  SV* self_sv;
  XML_Parser p;

  AV* context;
  AV* new_prefix_list;
  HV *nstab;
  AV *nslst;

  unsigned int st_serial;
  unsigned int st_serial_stackptr;
  unsigned int st_serial_stacksize;
  unsigned int * st_serial_stack;

  unsigned int skip_until;

  SV *recstring;
  char * delim;
  STRLEN delimlen;

  unsigned ns:1;
  unsigned no_expand:1;
  unsigned parseparam:1;
  unsigned pe_implicit:1;

  char *doctype_sysid;  /* DOCTYPE SYSTEM id for PE vs external DTD distinction */

  /* Callback handlers */

  SV* start_sv;
  SV* end_sv;
  SV* char_sv;
  SV* proc_sv;
  SV* cmnt_sv;
  SV* dflt_sv;

  SV* entdcl_sv;
  SV* eledcl_sv;
  SV* attdcl_sv;
  SV* doctyp_sv;
  SV* doctypfin_sv;
  SV* xmldec_sv;

  SV* unprsd_sv;
  SV* notation_sv;

  SV* extent_sv;
  SV* extfin_sv;

  SV* startcd_sv;
  SV* endcd_sv;
} CallbackVector;


static HV* EncodingTable = NULL;

static XML_Char nsdelim[] = {NSDELIM, '\0'};

static const char *QuantChar[] = {"", "?", "*", "+"};

/* Forward declarations */

static void suspend_callbacks(CallbackVector *);
static void resume_callbacks(CallbackVector *);

static SV *
newUTF8SVpv(const char *s, STRLEN len) {
  SV *sv;

  sv = newSVpv(s, len);
  SvUTF8_on(sv);
  return sv;
}  /* End new UTF8SVpv */

static SV *
newUTF8SVpvn(const char *s, STRLEN len) {
  SV *sv;

  sv = newSV(0);
  sv_setpvn(sv, s, len);
  SvUTF8_on(sv);
  return sv;
}

static void*
mymalloc(size_t size) {
  return safemalloc(size);
}

static void*
myrealloc(void *p, size_t s) {
  return saferealloc(p, s);
}

static void
myfree(void *p) {
  Safefree(p);
}

static XML_Memory_Handling_Suite ms = {mymalloc, myrealloc, myfree};

static void
free_cbv(CallbackVector *cbv)
{
  if (cbv) {
    SvREFCNT_dec(cbv->self_sv);
    Safefree(cbv->st_serial_stack);
    Safefree(cbv->doctype_sysid);
    Safefree(cbv);
  }
}

static void
append_error(XML_Parser parser, const char * err)
{
  dSP;
  CallbackVector * cbv;
  SV ** errstr;

  cbv = (CallbackVector*) XML_GetUserData(parser);
  errstr = hv_fetch((HV*)SvRV(cbv->self_sv),
		    "ErrorMessage", 12, 0);

  if (errstr && SvPOK(*errstr)) {
    SV ** errctx = hv_fetch((HV*) SvRV(cbv->self_sv),
			    "ErrorContext", 12, 0);
    int dopos = !err && errctx && SvOK(*errctx);

    if (! err)
      err = XML_ErrorString(XML_GetErrorCode(parser));

    /* Avoid truncation on 32-bit perls when expat is built with
       XML_LARGE_SIZE (long long types).  Use NV (double, 53-bit
       integer precision) to preserve values up to ~9 PB.
       See https://github.com/cpan-authors/XML-Parser/issues/36
       and https://github.com/cpan-authors/XML-Parser/issues/48 */
#if (defined(XML_LARGE_SIZE) && IVSIZE < 8)
    sv_catpvf(*errstr, "\n%s at line %.0" NVff ", column %.0" NVff ", byte %.0" NVff "%s",
	      err,
	      (NV)XML_GetCurrentLineNumber(parser),
	      (NV)XML_GetCurrentColumnNumber(parser),
	      (NV)XML_GetCurrentByteIndex(parser),
	      dopos ? ":\n" : "");
#else
    sv_catpvf(*errstr, "\n%s at line %" UVuf ", column %" UVuf ", byte %" IVdf "%s",
	      err,
	      (UV)XML_GetCurrentLineNumber(parser),
	      (UV)XML_GetCurrentColumnNumber(parser),
	      (IV)XML_GetCurrentByteIndex(parser),
	      dopos ? ":\n" : "");
#endif
	      
    if (dopos)
      {
	int count;

	ENTER ;
	SAVETMPS ;
	PUSHMARK(sp);
	XPUSHs(cbv->self_sv);
	XPUSHs(*errctx);
	PUTBACK ;

	count = call_method("position_in_context", G_SCALAR);

	SPAGAIN ;

	if (count >= 1) {
	  sv_catsv(*errstr, POPs);
	}

	PUTBACK ;
	FREETMPS ;
	LEAVE ;
      }

    if (XML_GetErrorCode(parser) == XML_ERROR_INVALID_TOKEN) {
      sv_catpv(*errstr,
        "(Hint: \"not well-formed\" often indicates unescaped '<', '>' or '&'"
        " in content — use &lt; &gt; or &amp; instead)\n");
    }
  }
}  /* End append_error */

static SV *
generate_model(XML_Content *model) {
  HV * hash = newHV();
  SV * obj = newRV_noinc((SV *) hash);

  sv_bless(obj, gv_stashpv("XML::Parser::ContentModel", 1));

  hv_store(hash, "Type", 4, newSViv(model->type), 0);
  if (model->quant != XML_CQUANT_NONE) {
    hv_store(hash, "Quant", 5, newSVpv(QuantChar[model->quant], 1), 0);
  }

  switch(model->type) {
  case XML_CTYPE_NAME:
    hv_store(hash, "Tag", 3, newUTF8SVpv(model->name, 0), 0);
    break;

  case XML_CTYPE_MIXED:
  case XML_CTYPE_CHOICE:
  case XML_CTYPE_SEQ:
    if (model->children && model->numchildren)
      {
	AV * children = newAV();
	int i;

	for (i = 0; i < model->numchildren; i++) {
	  av_push(children, generate_model(&model->children[i]));
	}

	hv_store(hash, "Children", 8, newRV_noinc((SV *) children), 0);
      }
    break;

  case XML_CTYPE_EMPTY:
  case XML_CTYPE_ANY:
    break;
  }

  return obj;
}  /* End generate_model */

static int
parse_stream(XML_Parser parser, SV * ioref)
{
  dSP;
  SV *		tbuff = NULL;
  SV *		tsiz = NULL;
  char *	linebuff = NULL;
  STRLEN	lblen;
  STRLEN	br = 0;
  int		buffsize;
  int		done = 0;
  int		ret = 1;
  CallbackVector * cbv;
  cbv = (CallbackVector*) XML_GetUserData(parser);

  ENTER;
  SAVETMPS;

  if (cbv->delim) {
    int cnt;
    SV * tline;

    PUSHMARK(SP);
    XPUSHs(ioref);
    PUTBACK ;

    cnt = call_method("getline", G_SCALAR);

    SPAGAIN;

    if (cnt != 1)
      croak("getline method call failed");

    tline = POPs;

    if (! SvOK(tline)) {
      lblen = 0;
    }
    else {
      linebuff = SvPV(tline, lblen);

      if (lblen > cbv->delimlen + 1) {
	char *chk = &linebuff[lblen - cbv->delimlen - 1];

	if (*chk == *cbv->delim
	    && chk[cbv->delimlen] == '\n'
	    && strnEQ(chk + 1, cbv->delim + 1, cbv->delimlen - 1))
	  lblen -= cbv->delimlen + 1;
      }
    }

    PUTBACK ;
    buffsize = lblen;
    done = lblen == 0;
  }
  else {
    tbuff = newSV(0);
    tsiz = newSViv(BUFSIZE);
    buffsize = BUFSIZE;
    /* Register for cleanup so croak() in the loop won't leak them */
    SAVEFREESV(tbuff);
    SAVEFREESV(tsiz);
  }

  while (! done)
    {
      char *buffer = XML_GetBuffer(parser, buffsize);

      if (! buffer)
	croak("Ran out of memory for input buffer");

      SAVETMPS;

      if (cbv->delim) {
	Copy(linebuff, buffer, lblen, char);
	br = lblen;
	done = 1;
      }
      else {
	int cnt;
	SV * rdres;
	char * tb;

	PUSHMARK(SP);
	EXTEND(SP, 3);
	PUSHs(ioref);
	PUSHs(tbuff);
	PUSHs(tsiz);
	PUTBACK ;

	cnt = call_method("read", G_SCALAR);

	SPAGAIN ;

	if (cnt != 1)
	  croak("read method call failed");

	rdres = POPs;

	if (! SvOK(rdres))
	  croak("read error");

	tb = SvPV(tbuff, br);
	if (br > 0) {
	  if (br > buffsize) {
	    /* The byte count from SvPV can exceed buffsize when the
	       filehandle has a :utf8 layer, since Perl reads buffsize
	       characters but multi-byte UTF-8 chars produce more bytes.
	       Re-obtain the buffer at the required size. */
	    buffer = XML_GetBuffer(parser, br);
	    if (! buffer)
	      croak("Ran out of memory for input buffer");
	  }
	  Copy(tb, buffer, br, char);
	} else
	  done = 1;

	PUTBACK ;
      }

      ret = XML_ParseBuffer(parser, br, done);

      SPAGAIN; /* resync local SP in case callbacks changed global stack */

      if (! ret)
	break;

      FREETMPS;
    }

  if (! ret)
    append_error(parser, NULL);

  /* tbuff and tsiz are freed by SAVEFREESV via FREETMPS/LEAVE below */
      
  FREETMPS;
  LEAVE;

  return ret;
}  /* End parse_stream */

static SV *
gen_ns_name(const char * name, HV * ns_table, AV * ns_list)
{
  char	*pos = strchr(name, NSDELIM);
  SV * ret;

  if (pos && pos > name)
    {
      SV ** name_ent = hv_fetch(ns_table, name,
				pos - name, TRUE);
      ret = newUTF8SVpv(&pos[1], 0);

      if (name_ent)
	{
	  int index;

	  if (SvOK(*name_ent))
	    {
	      index = SvIV(*name_ent);
	    }
	  else
	    {
	      av_push(ns_list,  newUTF8SVpv(name, pos - name));
	      index = av_len(ns_list);
	      sv_setiv(*name_ent, (IV) index);
	    }

	  sv_setiv(ret, (IV) index);
	  SvPOK_on(ret);
	}
    }
  else
    ret = newUTF8SVpv(name, 0);

  return ret;
}  /* End gen_ns_name */

static void
characterData(void *userData, const char *s, int len)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 2);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpvn((char*)s,len)));
  PUTBACK;
  call_sv(cbv->char_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End characterData */

static void
startElement(void *userData, const char *name, const char **atts)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;
  unsigned   do_ns = cbv->ns;
  unsigned   skipping = 0;
  SV *  elname;

  cbv->st_serial++;

  if (cbv->skip_until) {
    skipping = cbv->st_serial < cbv->skip_until;
    if (! skipping) {
      resume_callbacks(cbv);
      cbv->skip_until = 0;
    }
  }

  if (cbv->st_serial_stackptr + 1 >= cbv->st_serial_stacksize) {
    unsigned int newsize = cbv->st_serial_stacksize + 512;

    Renew(cbv->st_serial_stack, newsize, unsigned int);
    cbv->st_serial_stacksize = newsize;
  }

  cbv->st_serial_stack[++cbv->st_serial_stackptr] =  cbv->st_serial;
  
  if (do_ns)
    elname = gen_ns_name(name, cbv->nstab, cbv->nslst);
  else
    elname = newUTF8SVpv(name, 0);

  if (! skipping && SvTRUE(cbv->start_sv))
    {
      const char **attlim = atts;

      while (*attlim)
	attlim++;

      ENTER;
      SAVETMPS;
      SAVEFREESV(elname);

      PUSHMARK(sp);
      EXTEND(sp, attlim - atts + 2);
      PUSHs(cbv->self_sv);
      PUSHs(elname);
      while (*atts)
	{
	  SV * attname;

	  attname = (do_ns ? gen_ns_name(*atts, cbv->nstab, cbv->nslst)
		     : newUTF8SVpv(*atts, 0));

	  atts++;
	  PUSHs(sv_2mortal(attname));
	  if (*atts)
	    PUSHs(sv_2mortal(newUTF8SVpv((char*)*atts++,0)));
	}
      PUTBACK;
      call_sv(cbv->start_sv, G_DISCARD|G_VOID);

      SvREFCNT_inc_simple_void(elname);
      FREETMPS;
      LEAVE;
    }

  av_push(cbv->context, elname);

  if (cbv->ns) {
    av_clear(cbv->new_prefix_list);
  }
} /* End startElement */

static void
endElement(void *userData, const char *name)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;
  SV *elname;

  elname = av_pop(cbv->context);
  
  if (! cbv->st_serial_stackptr) {
    croak("endElement: Start tag serial number stack underflow");
  }

  if (! cbv->skip_until && SvTRUE(cbv->end_sv))
    {
      ENTER;
      SAVETMPS;
      SAVEFREESV(elname);

      PUSHMARK(sp);
      EXTEND(sp, 2);
      PUSHs(cbv->self_sv);
      PUSHs(elname);
      PUTBACK;
      call_sv(cbv->end_sv, G_DISCARD|G_VOID);

      SvREFCNT_inc_simple_void(elname);
      FREETMPS;
      LEAVE;
    }

  cbv->st_serial_stackptr--;

  SvREFCNT_dec(elname);
}  /* End endElement */

static void
processingInstruction(void *userData, const char *target, const char *data)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 3);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv((char*)target,0)));
  PUSHs(sv_2mortal(newUTF8SVpv((char*)data,0)));
  PUTBACK;
  call_sv(cbv->proc_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End processingInstruction */

static void
commenthandle(void *userData, const char *string)
{
  dSP;
  CallbackVector * cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 2);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv((char*) string, 0)));
  PUTBACK;
  call_sv(cbv->cmnt_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End commenthandler */

static void
startCdata(void *userData)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  if (cbv->startcd_sv) {
    ENTER;
    SAVETMPS;

    PUSHMARK(sp);
    XPUSHs(cbv->self_sv);
    PUTBACK;
    call_sv(cbv->startcd_sv, G_DISCARD|G_VOID);

    FREETMPS;
    LEAVE;
  }
}  /* End startCdata */

static void
endCdata(void *userData)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  if (cbv->endcd_sv) {
    ENTER;
    SAVETMPS;

    PUSHMARK(sp);
    XPUSHs(cbv->self_sv);
    PUTBACK;
    call_sv(cbv->endcd_sv, G_DISCARD|G_VOID);

    FREETMPS;
    LEAVE;
  }
}  /* End endCdata */

static void
nsStart(void *userdata, const XML_Char *prefix, const XML_Char *uri){
  dSP;
  CallbackVector* cbv = (CallbackVector*) userdata;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 3);
  PUSHs(cbv->self_sv);
  PUSHs(prefix ? sv_2mortal(newUTF8SVpv(prefix, 0)) : &PL_sv_undef);
  PUSHs(uri ? sv_2mortal(newUTF8SVpv(uri, 0)) : &PL_sv_undef);
  PUTBACK;
  call_method("NamespaceStart", G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End nsStart */

static void
nsEnd(void *userdata, const XML_Char *prefix) {
  dSP;
  CallbackVector* cbv = (CallbackVector*) userdata;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 2);
  PUSHs(cbv->self_sv);
  PUSHs(prefix ? sv_2mortal(newUTF8SVpv(prefix, 0)) : &PL_sv_undef);
  PUTBACK;
  call_method("NamespaceEnd", G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End nsEnd */

static void
defaulthandle(void *userData, const char *string, int len)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 2);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpvn((char*)string, len)));
  PUTBACK;
  call_sv(cbv->dflt_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End defaulthandle */

static void
elementDecl(void *data,
	    const char *name,
	    XML_Content *model) {
  dSP;
  CallbackVector *cbv = (CallbackVector*) data;
  SV *cmod;

  ENTER;
  SAVETMPS;


  cmod = generate_model(model);

  XML_FreeContentModel(cbv->p, model);
  PUSHMARK(sp);
  EXTEND(sp, 3);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv(name, 0)));
  PUSHs(sv_2mortal(cmod));
  PUTBACK;
  call_sv(cbv->eledcl_sv, G_DISCARD|G_VOID);
  FREETMPS;
  LEAVE;

}  /* End elementDecl */

static void
attributeDecl(void *data,
	      const char * elname,
	      const char * attname,
	      const char * att_type,
	      const char * dflt,
	      int          reqorfix) {
  dSP;
  CallbackVector *cbv = (CallbackVector*) data;
  SV * dfltsv;

  if (dflt) {
    dfltsv = newUTF8SVpv("'", 1);
    sv_catpv(dfltsv, dflt);
    sv_catpv(dfltsv, "'");
  }
  else {
    dfltsv = newUTF8SVpv(reqorfix ? "#REQUIRED" : "#IMPLIED", 0);
  }

  ENTER;
  SAVETMPS;
  PUSHMARK(sp);
  EXTEND(sp, 5);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv(elname, 0)));
  PUSHs(sv_2mortal(newUTF8SVpv(attname, 0)));
  PUSHs(sv_2mortal(newUTF8SVpv(att_type, 0)));
  PUSHs(sv_2mortal(dfltsv));
  if (dflt && reqorfix)
    XPUSHs(&PL_sv_yes);
  PUTBACK;
  call_sv(cbv->attdcl_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End attributeDecl */

static void
entityDecl(void *data,
	   const char *name,
	   int isparam,
	   const char *value,
	   int vlen,
	   const char *base,
	   const char *sysid,
	   const char *pubid,
	   const char *notation) {
  dSP;
  CallbackVector *cbv = (CallbackVector*) data;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 6);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv(name, 0)));
  PUSHs(value ? sv_2mortal(newUTF8SVpvn(value, vlen)) : &PL_sv_undef);
  PUSHs(sysid ? sv_2mortal(newUTF8SVpv(sysid, 0)) : &PL_sv_undef);
  PUSHs(pubid ? sv_2mortal(newUTF8SVpv(pubid, 0)) : &PL_sv_undef);
  PUSHs(notation ? sv_2mortal(newUTF8SVpv(notation, 0)) : &PL_sv_undef);
  if (isparam)
    XPUSHs(&PL_sv_yes);
  PUTBACK;
  call_sv(cbv->entdcl_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End entityDecl */

static void
doctypeStart(void *userData,
	     const char* name,
	     const char* sysid,
	     const char* pubid,
	     int hasinternal) {
  CallbackVector *cbv = (CallbackVector*) userData;

  /* Always track the DOCTYPE sysid so externalEntityRef can distinguish
     PE references from external DTD references when pe_implicit is set. */
  Safefree(cbv->doctype_sysid);
  cbv->doctype_sysid = NULL;
  if (sysid) {
    Newx(cbv->doctype_sysid, strlen(sysid) + 1, char);
    Copy(sysid, cbv->doctype_sysid, strlen(sysid) + 1, char);
  }

  if (cbv->doctyp_sv && SvTRUE(cbv->doctyp_sv)) {
    dSP;

    ENTER;
    SAVETMPS;

    PUSHMARK(sp);
    EXTEND(sp, 5);
    PUSHs(cbv->self_sv);
    PUSHs(sv_2mortal(newUTF8SVpv((char*)name, 0)));
    PUSHs(sysid ? sv_2mortal(newUTF8SVpv((char*)sysid, 0)) : &PL_sv_undef);
    PUSHs(pubid ? sv_2mortal(newUTF8SVpv((char*)pubid, 0)) : &PL_sv_undef);
    PUSHs(hasinternal ? &PL_sv_yes : &PL_sv_no);
    PUTBACK;
    call_sv(cbv->doctyp_sv, G_DISCARD|G_VOID);
    FREETMPS;
    LEAVE;
  }
}  /* End doctypeStart */

static void
doctypeEnd(void *userData) {
  dSP;
  CallbackVector *cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 1);
  PUSHs(cbv->self_sv);
  PUTBACK;
  call_sv(cbv->doctypfin_sv, G_DISCARD|G_VOID);
  FREETMPS;
  LEAVE;
}  /* End doctypeEnd */

static void
xmlDecl(void *userData,
	const char *version,
	const char *encoding,
	int standalone) {
  dSP;
  CallbackVector *cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 4);
  PUSHs(cbv->self_sv);
  PUSHs(version ? sv_2mortal(newUTF8SVpv(version, 0))
	: &PL_sv_undef);
  PUSHs(encoding ? sv_2mortal(newUTF8SVpv(encoding, 0))
	: &PL_sv_undef);
  PUSHs(standalone == -1 ? &PL_sv_undef
	: (standalone ? &PL_sv_yes : &PL_sv_no));
  PUTBACK;
  call_sv(cbv->xmldec_sv, G_DISCARD|G_VOID);
  FREETMPS;
  LEAVE;
}  /* End xmlDecl */

static void
unparsedEntityDecl(void *userData,
		   const char* entity,
		   const char* base,
		   const char* sysid,
		   const char* pubid,
		   const char* notation)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 6);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv(entity, 0)));
  PUSHs(base ? sv_2mortal(newUTF8SVpv(base, 0)) : &PL_sv_undef);
  PUSHs(sv_2mortal(newUTF8SVpv(sysid, 0)));
  PUSHs(pubid ? sv_2mortal(newUTF8SVpv(pubid, 0)) : &PL_sv_undef);
  PUSHs(sv_2mortal(newUTF8SVpv(notation, 0)));
  PUTBACK;
  call_sv(cbv->unprsd_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End unparsedEntityDecl */

static void
notationDecl(void *userData,
	     const char *name,
	     const char *base,
	     const char *sysid,
	     const char *pubid)
{
  dSP;
  CallbackVector* cbv = (CallbackVector*) userData;

  ENTER;
  SAVETMPS;

  PUSHMARK(sp);
  EXTEND(sp, 5);
  PUSHs(cbv->self_sv);
  PUSHs(sv_2mortal(newUTF8SVpv(name, 0)));
  PUSHs(base ? sv_2mortal(newUTF8SVpv(base, 0)) : &PL_sv_undef);
  PUSHs(sysid ? sv_2mortal(newUTF8SVpv(sysid, 0)) : &PL_sv_undef);
  PUSHs(pubid ? sv_2mortal(newUTF8SVpv(pubid, 0)) : &PL_sv_undef);
  PUTBACK;
  call_sv(cbv->notation_sv, G_DISCARD|G_VOID);

  FREETMPS;
  LEAVE;
}  /* End notationDecl */

static int
externalEntityRef(XML_Parser parser,
		  const char* open,
		  const char* base,
		  const char* sysid,
		  const char* pubid)
{
  dSP;

  int count;
  int ret = 0;
  int parse_done = 0;

  CallbackVector* cbv = (CallbackVector*) XML_GetUserData(parser);

  /* For parameter entities and DTD (context is NULL per expat docs),
     when the user did not explicitly request ParseParamEnt, silently
     treat the PE as empty and let expat continue processing subsequent
     DTD declarations normally.  See GH #53. */
  /* For parameter entities and DTD (context is NULL per expat docs),
     when the user did not explicitly request ParseParamEnt, treat the
     PE as empty content so expat continues dispatching subsequent DTD
     declarations to their proper handlers (Attlist, Element, etc.).

     We must actually create the sub-parser and feed it an empty document
     rather than simply returning 1, because expat needs the sub-parser's
     parse completion to finalize entity processing.  Without this, expat
     routes all subsequent DTD declarations to the Default handler instead
     of the dedicated handlers.  See GH #53 and GH #173. */
  if (open == NULL && !cbv->parseparam) {
    /* When PE parsing was implicitly enabled for declaration handlers,
       route PE reference text to the Default handler so DTD
       round-tripping still works (e.g. XML::Twig).

       We must NOT fire XML_DefaultCurrent for the external DTD subset
       reference — only for PE references in the internal subset.
       Distinguish them by comparing sysid to the stored DOCTYPE sysid:
       the external DTD uses the same sysid as the DOCTYPE declaration,
       while PE references use the sysid from their entity declaration. */
    if (cbv->pe_implicit) {
      int is_ext_dtd = (sysid == NULL && cbv->doctype_sysid == NULL)
        || (sysid != NULL && cbv->doctype_sysid != NULL
            && strcmp(sysid, cbv->doctype_sysid) == 0);
      if (!is_ext_dtd)
        XML_DefaultCurrent(parser);
    }

    XML_Parser entpar = XML_ExternalEntityParserCreate(parser, open, 0);
    if (entpar) {
      XML_Parse(entpar, "", 0, 1);
      XML_ParserFree(entpar);
    }
    return 1;
  }

  if (! cbv->extent_sv)
    return 0;

  ENTER ;
  SAVETMPS ;
  PUSHMARK(sp);
  EXTEND(sp, pubid ? 4 : 3);
  PUSHs(cbv->self_sv);
  PUSHs(base ? sv_2mortal(newUTF8SVpv((char*) base, 0)) : &PL_sv_undef);
  PUSHs(sysid ? sv_2mortal(newUTF8SVpv((char*) sysid, 0)) : &PL_sv_undef);
  if (pubid)
    PUSHs(sv_2mortal(newUTF8SVpv((char*) pubid, 0)));
  PUTBACK ;
  count = call_sv(cbv->extent_sv, G_SCALAR);

  SPAGAIN ;

  if (count >= 1) {
    SV * result = POPs;
    int type;

    if (result && (type = SvTYPE(result)) > 0) {
      SV **pval = hv_fetch((HV*) SvRV(cbv->self_sv), "Parser", 6, 0);

      if (! pval || ! SvIOK(*pval))
	append_error(parser, "Can't find parser entry in XML::Parser object");
      else {
	XML_Parser entpar;
	char *errmsg = NULL;

	entpar = XML_ExternalEntityParserCreate(parser, open, 0);
	if (! entpar) {
	  append_error(parser,
	    "Couldn't create external entity sub-parser");
	  goto Extparse_Cleanup;
	}

	XML_SetBase(entpar, XML_GetBase(parser));

	sv_setiv(*pval, PTR2IV(entpar));

	cbv->p = entpar;

	PUSHMARK(sp);
	EXTEND(sp, 2);
	PUSHs(*pval);
	PUSHs(result);
	PUTBACK;
	count = call_pv("XML::Parser::Expat::Do_External_Parse",
			     G_SCALAR | G_EVAL);
	SPAGAIN;

	if (SvTRUE(ERRSV)) {
	  char *hold;
	  STRLEN   len;

	  (void)POPs;
	  hold = SvPV(ERRSV, len);
	  Newx(errmsg, len + 1, char);
	  if (len)
	    Copy(hold, errmsg, len, char);
	  errmsg[len] = '\0';
	  goto Extparse_Cleanup;
	}

	if (count > 0)
	  ret = POPi;

	parse_done = 1;

      Extparse_Cleanup:
	cbv->p = parser;
	sv_setiv(*pval, PTR2IV(parser));
	XML_ParserFree(entpar);

	if (cbv->extfin_sv) {
	  PUSHMARK(sp);
	  PUSHs(cbv->self_sv);
	  PUTBACK;
	  call_sv(cbv->extfin_sv, G_DISCARD|G_VOID);
	  SPAGAIN;
	}

	/* Use saved error from Do_External_Parse if available,
	   since the extfin callback above may have cleared ERRSV */
	if (errmsg)
	  append_error(parser, errmsg);
	else if (SvTRUE(ERRSV))
	  append_error(parser, SvPV_nolen(ERRSV));

	Safefree(errmsg);
      }
    }
  }

  if (! ret && ! parse_done)
    append_error(parser, "Handler couldn't resolve external entity");

  PUTBACK ;
  FREETMPS ;
  LEAVE ;

  return ret;
}  /* End externalEntityRef */

/*================================================================
** This is the function that expat calls to convert multi-byte sequences
** for external encodings. Each byte in the sequence is used to index
** into the current map to either set the next map or, in the case of
** the final byte, to get the corresponding Unicode scalar, which is
** returned.
*/

static int
convert_to_unicode(void *data, const char *seq) {
  Encinfo *enc = (Encinfo *) data;
  PrefixMap *curpfx;
  int count;
  int index = 0;

  for (count = 0; count < 4; count++) {
    unsigned char byte = (unsigned char) seq[count];
    unsigned char bndx;
    unsigned char bmsk;
    int offset;

    curpfx = &enc->prefixes[index];
    offset = ((int) byte) - curpfx->min;
    if (offset < 0)
      break;
    if (offset >= curpfx->len && curpfx->len != 0)
      break;

    bndx = byte >> 3;
    bmsk = 1 << (byte & 0x7);

    if (curpfx->ispfx[bndx] & bmsk) {
      index = enc->bytemap[curpfx->bmap_start + offset];
    }
    else if (curpfx->ischar[bndx] & bmsk) {
      return enc->bytemap[curpfx->bmap_start + offset];
    }
    else
      break;
  }

  return -1;
}  /* End convert_to_unicode */

static int
unknownEncoding(void *unused, const char *name, XML_Encoding *info)
{
  SV ** encinfptr;
  PERL_UNUSED_VAR(unused);
  Encinfo *enc;
  int namelen;
  int i;
  char buff[42];

  namelen = strlen(name);
  if (namelen > 40)
    return 0;

  /* Make uppercase */
  for (i = 0; i < namelen; i++) {
    char c = name[i];
    if (c >= 'a' && c <= 'z')
      c -= 'a' - 'A';
    buff[i] = c;
  }

  if (! EncodingTable) {
    EncodingTable = get_hv("XML::Parser::Expat::Encoding_Table", FALSE);
    if (! EncodingTable)
      croak("Can't find XML::Parser::Expat::Encoding_Table");
  }

  encinfptr = hv_fetch(EncodingTable, buff, namelen, 0);

  if (! encinfptr || ! SvOK(*encinfptr)) {
    /* Not found, so try to autoload */
    dSP;

    ENTER;
    SAVETMPS;
    PUSHMARK(sp);
    XPUSHs(sv_2mortal(newSVpvn(buff,namelen)));
    PUTBACK;
    call_pv("XML::Parser::Expat::load_encoding", G_DISCARD|G_VOID);
    
    encinfptr = hv_fetch(EncodingTable, buff, namelen, 0);
    FREETMPS;
    LEAVE;

    if (! encinfptr || ! SvOK(*encinfptr))
      return 0;
  }

  if (! sv_derived_from(*encinfptr, "XML::Parser::Encinfo"))
    croak("Entry in XML::Parser::Expat::Encoding_Table not an Encinfo object");

  enc = INT2PTR(Encinfo *, SvIV((SV*)SvRV(*encinfptr)));
  Copy(enc->firstmap, info->map, 256, int);
  info->release = NULL;
  if (enc->prefixes_size) {
    info->data = (void *) enc;
    info->convert = convert_to_unicode;
  }
  else {
    info->data = NULL;
    info->convert = NULL;
  }

  return 1;
}  /* End unknownEncoding */


static void
recString(void *userData, const char *string, int len)
{
  CallbackVector *cbv = (CallbackVector*) userData;

  if (cbv->recstring) {
    sv_catpvn(cbv->recstring, string, len);
  }
  else {
    cbv->recstring = newUTF8SVpvn(string, len);
  }
}  /* End recString */

static void
suspend_callbacks(CallbackVector *cbv) {
  if (SvTRUE(cbv->char_sv)) {
    XML_SetCharacterDataHandler(cbv->p,
				(XML_CharacterDataHandler) 0);
  }

  if (SvTRUE(cbv->proc_sv)) {
    XML_SetProcessingInstructionHandler(cbv->p,
					(XML_ProcessingInstructionHandler) 0);
  }

  if (SvTRUE(cbv->cmnt_sv)) {
    XML_SetCommentHandler(cbv->p,
			  (XML_CommentHandler) 0);
  }

  if (SvTRUE(cbv->startcd_sv)
      || SvTRUE(cbv->endcd_sv)) {
    XML_SetCdataSectionHandler(cbv->p,
			       (XML_StartCdataSectionHandler) 0,
			       (XML_EndCdataSectionHandler) 0);
  }

  if (SvTRUE(cbv->unprsd_sv)) {
    XML_SetUnparsedEntityDeclHandler(cbv->p,
				     (XML_UnparsedEntityDeclHandler) 0);
  }

  if (SvTRUE(cbv->notation_sv)) {
    XML_SetNotationDeclHandler(cbv->p,
			       (XML_NotationDeclHandler) 0);
  }

  if (SvTRUE(cbv->extent_sv)) {
    XML_SetExternalEntityRefHandler(cbv->p,
				    (XML_ExternalEntityRefHandler) 0);
  }

}  /* End suspend_callbacks */

static void
resume_callbacks(CallbackVector *cbv) {
  if (SvTRUE(cbv->char_sv)) {
    XML_SetCharacterDataHandler(cbv->p, characterData);
  }

  if (SvTRUE(cbv->proc_sv)) {
    XML_SetProcessingInstructionHandler(cbv->p, processingInstruction);
  }

  if (SvTRUE(cbv->cmnt_sv)) {
    XML_SetCommentHandler(cbv->p, commenthandle);
  }

  if (SvTRUE(cbv->startcd_sv)
      || SvTRUE(cbv->endcd_sv)) {
    XML_SetCdataSectionHandler(cbv->p, startCdata, endCdata);
  }

  if (SvTRUE(cbv->unprsd_sv)) {
    XML_SetUnparsedEntityDeclHandler(cbv->p, unparsedEntityDecl);
  }

  if (SvTRUE(cbv->notation_sv)) {
    XML_SetNotationDeclHandler(cbv->p, notationDecl);
  }

  if (SvTRUE(cbv->extent_sv)) {
    XML_SetExternalEntityRefHandler(cbv->p, externalEntityRef);
  }

}  /* End resume_callbacks */


#line 1323 "Expat.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(const CV *const cv, const char *const params);

static void
S_croak_xs_usage(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_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#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) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

/* simple backcompat versions of the TARGx() macros with no optimisation */
#ifndef TARGi
#  define TARGi(iv, do_taint) sv_setiv_mg(TARG, iv)
#  define TARGu(uv, do_taint) sv_setuv_mg(TARG, uv)
#  define TARGn(nv, do_taint) sv_setnv_mg(TARG, nv)
#endif

#line 1474 "Expat.c"

XS_EUPXS(XS_XML__Parser__Expat_ParserCreate); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParserCreate)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "self_sv, enc_sv, namespaces");
    {
	SV *	self_sv = ST(0)
;
	SV *	enc_sv = ST(1)
;
	int	namespaces = (int)SvIV(ST(2))
;
	XML_Parser	RETVAL;
	dXSTARG;
#line 1321 "Expat.xs"
	{
	  CallbackVector *cbv;
	  char *enc = SvTRUE(enc_sv) ? SvPV_nolen(enc_sv) : NULL;
	  SV ** spp;

	  Newxz(cbv, 1, CallbackVector);
	  cbv->self_sv = SvREFCNT_inc(self_sv);
	  Newxz(cbv->st_serial_stack, 1024, unsigned int);
	  cbv->st_serial_stacksize = 1024;
	  spp = hv_fetch((HV*)SvRV(cbv->self_sv), "NoExpand", 8, 0);
	  if (spp && SvTRUE(*spp))
	    cbv->no_expand = 1;

	  spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Context", 7, 0);
	  if (! spp || ! *spp || !SvROK(*spp)) {
	    free_cbv(cbv);
	    croak("XML::Parser instance missing Context");
	  }

	  cbv->context = (AV*) SvRV(*spp);

	  cbv->ns = (unsigned) namespaces;
	  if (namespaces)
	    {
	      spp = hv_fetch((HV*)SvRV(cbv->self_sv), "New_Prefixes", 12, 0);
	      if (! spp || ! *spp || !SvROK(*spp)) {
	        free_cbv(cbv);
	        croak("XML::Parser instance missing New_Prefixes");
	      }

	      cbv->new_prefix_list = (AV *) SvRV(*spp);

	      spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Namespace_Table",
			     15, FALSE);
	      if (! spp || ! *spp || !SvROK(*spp)) {
	        free_cbv(cbv);
	        croak("XML::Parser instance missing Namespace_Table");
	      }

	      cbv->nstab = (HV *) SvRV(*spp);

	      spp = hv_fetch((HV*)SvRV(cbv->self_sv), "Namespace_List",
			     14, FALSE);
	      if (! spp || ! *spp || !SvROK(*spp)) {
	        free_cbv(cbv);
	        croak("XML::Parser instance missing Namespace_List");
	      }

	      cbv->nslst = (AV *) SvRV(*spp);

	      RETVAL = XML_ParserCreate_MM(enc, &ms, nsdelim);
	      if (! RETVAL) {
	        free_cbv(cbv);
	        croak("Could not create XML parser");
	      }
	      XML_SetNamespaceDeclHandler(RETVAL,nsStart, nsEnd);
	    }
	    else
	    {
	      RETVAL = XML_ParserCreate_MM(enc, &ms, NULL);
	      if (! RETVAL) {
	        free_cbv(cbv);
	        croak("Could not create XML parser");
	      }
	    }

	  cbv->p = RETVAL;
	  XML_SetUserData(RETVAL, (void *) cbv);
	  XML_SetElementHandler(RETVAL, startElement, endElement);
	  XML_SetUnknownEncodingHandler(RETVAL, unknownEncoding, 0);
	  XML_SetStartDoctypeDeclHandler(RETVAL, doctypeStart);
	  spp = hv_fetch((HV*)SvRV(cbv->self_sv), "ParseParamEnt",
			 13, FALSE);

	  if (spp && SvTRUE(*spp)) {
	    cbv->parseparam = 1;
	    XML_SetParamEntityParsing(RETVAL,
	      XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
	    XML_SetExternalEntityRefHandler(RETVAL, externalEntityRef);
	  }

	  spp = hv_fetch((HV*)SvRV(cbv->self_sv), "UseForeignDTD",
			 13, FALSE);

	  if (spp && SvTRUE(*spp))
	    XML_UseForeignDTD(RETVAL, XML_TRUE);
	}
#line 1579 "Expat.c"
	TARGi(PTR2IV(RETVAL), 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_ParserRelease); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParserRelease)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
#line 1415 "Expat.xs"
      {
        CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	SvREFCNT_dec(cbv->self_sv);
	cbv->self_sv = NULL;
      }
#line 1603 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_ParserFree); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParserFree)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
#line 1426 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	  Safefree(cbv->st_serial_stack);
	  Safefree(cbv->doctype_sysid);

	  /* Clean up any SVs that we have */
	  /* (Note that self_sv must already be taken care of
	     or we couldn't be here */

	  if (cbv->recstring)
	    SvREFCNT_dec(cbv->recstring);

	  if (cbv->start_sv)
	    SvREFCNT_dec(cbv->start_sv);

	  if (cbv->end_sv)
	    SvREFCNT_dec(cbv->end_sv);

	  if (cbv->char_sv)
	    SvREFCNT_dec(cbv->char_sv);

	  if (cbv->proc_sv)
	    SvREFCNT_dec(cbv->proc_sv);

	  if (cbv->cmnt_sv)
	    SvREFCNT_dec(cbv->cmnt_sv);

	  if (cbv->dflt_sv)
	    SvREFCNT_dec(cbv->dflt_sv);

	  if (cbv->entdcl_sv)
	    SvREFCNT_dec(cbv->entdcl_sv);

	  if (cbv->eledcl_sv)
	    SvREFCNT_dec(cbv->eledcl_sv);

	  if (cbv->attdcl_sv)
	    SvREFCNT_dec(cbv->attdcl_sv);

	  if (cbv->doctyp_sv)
	    SvREFCNT_dec(cbv->doctyp_sv);

	  if (cbv->doctypfin_sv)
	    SvREFCNT_dec(cbv->doctypfin_sv);

	  if (cbv->xmldec_sv)
	    SvREFCNT_dec(cbv->xmldec_sv);

	  if (cbv->unprsd_sv)
	    SvREFCNT_dec(cbv->unprsd_sv);

	  if (cbv->notation_sv)
	    SvREFCNT_dec(cbv->notation_sv);

	  if (cbv->extent_sv)
	    SvREFCNT_dec(cbv->extent_sv);

	  if (cbv->extfin_sv)
	    SvREFCNT_dec(cbv->extfin_sv);

	  if (cbv->startcd_sv)
	    SvREFCNT_dec(cbv->startcd_sv);

	  if (cbv->endcd_sv)
	    SvREFCNT_dec(cbv->endcd_sv);

	  /* ================ */

	  Safefree(cbv);
	  XML_ParserFree(parser);
	}
#line 1691 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_ParseString); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParseString)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	sv = ST(1)
;
	int	RETVAL;
	dXSTARG;
#line 1504 "Expat.xs"
        {
	  STRLEN len;
	  char *s = SvPV(sv, len);

	  RETVAL = XML_Parse(parser, s, len, 1);
	  SPAGAIN; /* XML_Parse might have changed stack pointer */
	  if (! RETVAL)
	    append_error(parser, NULL);
	}

#line 1721 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_ParseStream); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParseStream)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "parser, ioref, delim");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	ioref = ST(1)
;
	SV *	delim = ST(2)
;
	int	RETVAL;
	dXSTARG;
#line 1523 "Expat.xs"
	{
	  CallbackVector * cbv;

	  cbv = (CallbackVector *) XML_GetUserData(parser);
	  if (SvOK(delim)) {
	    cbv->delim = SvPV(delim, cbv->delimlen);
	  }
	  else {
	    cbv->delim = NULL;
	  }

	  RETVAL = parse_stream(parser, ioref);
	  SPAGAIN; /* parse_stream might have changed stack pointer */
	}

#line 1760 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_ParsePartial); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParsePartial)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	sv = ST(1)
;
	int	RETVAL;
	dXSTARG;
#line 1546 "Expat.xs"
	{
	  STRLEN len;
	  char *s = SvPV(sv, len);

	  RETVAL = XML_Parse(parser, s, len, 0);
	  if (! RETVAL)
	    append_error(parser, NULL);
	}

#line 1791 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_ParseDone); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ParseDone)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	RETVAL;
	dXSTARG;
#line 1563 "Expat.xs"
	{
	  RETVAL = XML_Parse(parser, "", 0, 1);
	  if (! RETVAL)
	    append_error(parser, NULL);
	}

#line 1817 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetStartElementHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetStartElementHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, start_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	start_sv = ST(1)
;
	SV *	RETVAL;
#line 1577 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);
	  XMLP_UPD(start_sv);
	  PUSHRET;
	}
#line 1843 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetEndElementHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetEndElementHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, end_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	end_sv = ST(1)
;
	SV *	RETVAL;
#line 1588 "Expat.xs"
	{
	  CallbackVector *cbv = (CallbackVector*) XML_GetUserData(parser);
	  XMLP_UPD(end_sv);
	  PUSHRET;
	}
#line 1867 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetCharacterDataHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetCharacterDataHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, char_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	char_sv = ST(1)
;
	SV *	RETVAL;
#line 1599 "Expat.xs"
	{
	  XML_CharacterDataHandler charhndl = (XML_CharacterDataHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(char_sv);
	  if (SvTRUE(char_sv))
	    charhndl = characterData;

	  XML_SetCharacterDataHandler(parser, charhndl);
	  PUSHRET;
	}
#line 1897 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetProcessingInstructionHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetProcessingInstructionHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, proc_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	proc_sv = ST(1)
;
	SV *	RETVAL;
#line 1616 "Expat.xs"
	{
	  XML_ProcessingInstructionHandler prochndl =
	    (XML_ProcessingInstructionHandler) 0;
	  CallbackVector* cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(proc_sv);
	  if (SvTRUE(proc_sv))
	    prochndl = processingInstruction;

	  XML_SetProcessingInstructionHandler(parser, prochndl);
	  PUSHRET;
	}
#line 1928 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetCommentHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetCommentHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, cmnt_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	cmnt_sv = ST(1)
;
	SV *	RETVAL;
#line 1634 "Expat.xs"
	{
	  XML_CommentHandler cmnthndl = (XML_CommentHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(cmnt_sv);
	  if (SvTRUE(cmnt_sv))
	    cmnthndl = commenthandle;

	  XML_SetCommentHandler(parser, cmnthndl);
	  PUSHRET;
	}
#line 1958 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetDefaultHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetDefaultHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, dflt_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	dflt_sv = ST(1)
;
	SV *	RETVAL;
#line 1651 "Expat.xs"
	{
	  XML_DefaultHandler dflthndl = (XML_DefaultHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(dflt_sv);
	  if (SvTRUE(dflt_sv))
	    dflthndl = defaulthandle;

	  if (cbv->no_expand)
	    XML_SetDefaultHandler(parser, dflthndl);
	  else
	    XML_SetDefaultHandlerExpand(parser, dflthndl);

	  PUSHRET;
	}
#line 1992 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetUnparsedEntityDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetUnparsedEntityDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, unprsd_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	unprsd_sv = ST(1)
;
	SV *	RETVAL;
#line 1672 "Expat.xs"
	{
	  XML_UnparsedEntityDeclHandler unprsdhndl =
	    (XML_UnparsedEntityDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(unprsd_sv);
	  if (SvTRUE(unprsd_sv))
	    unprsdhndl = unparsedEntityDecl;

	  XML_SetUnparsedEntityDeclHandler(parser, unprsdhndl);
	  PUSHRET;
	}
#line 2023 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetNotationDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetNotationDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, notation_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	notation_sv = ST(1)
;
	SV *	RETVAL;
#line 1690 "Expat.xs"
	{
	  XML_NotationDeclHandler nothndlr = (XML_NotationDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(notation_sv);
	  if (SvTRUE(notation_sv))
	    nothndlr = notationDecl;

	  XML_SetNotationDeclHandler(parser, nothndlr);
	  PUSHRET;
	}
#line 2053 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetExternalEntityRefHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetExternalEntityRefHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, extent_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	extent_sv = ST(1)
;
	SV *	RETVAL;
#line 1707 "Expat.xs"
	{
	  XML_ExternalEntityRefHandler exthndlr =
	    (XML_ExternalEntityRefHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(extent_sv);
	  if (SvTRUE(extent_sv))
	    exthndlr = externalEntityRef;
	  else if (cbv->pe_implicit)
	    exthndlr = externalEntityRef;  /* keep C handler for PE support */

	  XML_SetExternalEntityRefHandler(parser, exthndlr);
	  PUSHRET;
	}
#line 2086 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetExtEntFinishHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetExtEntFinishHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, extfin_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	extfin_sv = ST(1)
;
	SV *	RETVAL;
#line 1727 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	  /* There is no corresponding handler for this in expat. This is
	     called from the externalEntityRef function above after parsing
	     the external entity. */

	  XMLP_UPD(extfin_sv);
	  PUSHRET;
	}
#line 2115 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetEntityDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetEntityDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, entdcl_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	entdcl_sv = ST(1)
;
	SV *	RETVAL;
#line 1744 "Expat.xs"
	{
	  XML_EntityDeclHandler enthndlr =
	    (XML_EntityDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(entdcl_sv);
	  if (SvTRUE(entdcl_sv))
	    enthndlr = entityDecl;

	  XML_SetEntityDeclHandler(parser, enthndlr);

	  if (SvTRUE(entdcl_sv) && !cbv->parseparam && !cbv->pe_implicit) {
	    cbv->pe_implicit = 1;
	    XML_SetParamEntityParsing(parser,
	      XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
	    XML_SetExternalEntityRefHandler(parser, externalEntityRef);
	  }
	  PUSHRET;
	}
#line 2153 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetElementDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetElementDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, eledcl_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	eledcl_sv = ST(1)
;
	SV *	RETVAL;
#line 1769 "Expat.xs"
	{
	  XML_ElementDeclHandler eldeclhndlr =
	    (XML_ElementDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(eledcl_sv);
	  if (SvTRUE(eledcl_sv))
	    eldeclhndlr = elementDecl;

	  XML_SetElementDeclHandler(parser, eldeclhndlr);

	  if (SvTRUE(eledcl_sv) && !cbv->parseparam && !cbv->pe_implicit) {
	    cbv->pe_implicit = 1;
	    XML_SetParamEntityParsing(parser,
	      XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
	    XML_SetExternalEntityRefHandler(parser, externalEntityRef);
	  }
	  PUSHRET;
	}
#line 2191 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetAttListDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetAttListDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, attdcl_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	attdcl_sv = ST(1)
;
	SV *	RETVAL;
#line 1794 "Expat.xs"
	{
	  XML_AttlistDeclHandler attdeclhndlr =
	    (XML_AttlistDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(attdcl_sv);
	  if (SvTRUE(attdcl_sv))
	    attdeclhndlr = attributeDecl;

	  XML_SetAttlistDeclHandler(parser, attdeclhndlr);

	  if (SvTRUE(attdcl_sv) && !cbv->parseparam && !cbv->pe_implicit) {
	    cbv->pe_implicit = 1;
	    XML_SetParamEntityParsing(parser,
	      XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
	    XML_SetExternalEntityRefHandler(parser, externalEntityRef);
	  }
	  PUSHRET;
	}
#line 2229 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetDoctypeHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetDoctypeHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, doctyp_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	doctyp_sv = ST(1)
;
	SV *	RETVAL;
#line 1819 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(doctyp_sv);
	  /* Always keep the C handler active for doctype_sysid tracking;
	     doctypeStart checks doctyp_sv before calling Perl. */
	  XML_SetStartDoctypeDeclHandler(parser, doctypeStart);
	  PUSHRET;
	}
#line 2257 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetEndDoctypeHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetEndDoctypeHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, doctypfin_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	doctypfin_sv = ST(1)
;
	SV *	RETVAL;
#line 1834 "Expat.xs"
	{
	  XML_EndDoctypeDeclHandler dtendhndlr =
	    (XML_EndDoctypeDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector*) XML_GetUserData(parser);

	  XMLP_UPD(doctypfin_sv);
	  if (SvTRUE(doctypfin_sv))
	    dtendhndlr = doctypeEnd;

	  XML_SetEndDoctypeDeclHandler(parser, dtendhndlr);
	  PUSHRET;
	}
#line 2288 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetXMLDeclHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetXMLDeclHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, xmldec_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	xmldec_sv = ST(1)
;
	SV *	RETVAL;
#line 1853 "Expat.xs"
	{
	  XML_XmlDeclHandler xmldechndlr =
	    (XML_XmlDeclHandler) 0;
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	  XMLP_UPD(xmldec_sv);
	  if (SvTRUE(xmldec_sv))
	    xmldechndlr = xmlDecl;

	  XML_SetXmlDeclHandler(parser, xmldechndlr);
	  PUSHRET;
	}
#line 2319 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetBase); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetBase)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, base");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	base = ST(1)
;
#line 1872 "Expat.xs"
	{
	  char * b;

	  if (! SvOK(base)) {
	    b = NULL;
	  }
	  else {
	    b = SvPV_nolen(base);
	  }

	  XML_SetBase(parser, b);
	}	
#line 2349 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_GetBase); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetBase)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
#line 1890 "Expat.xs"
	{
	  const char *ret = XML_GetBase(parser);
	  if (ret) {
	    XPUSHs(sv_2mortal(newSVpv(ret, 0)));
	  }
	  else {
	    XPUSHs(&PL_sv_undef);
	  }
	}
#line 2376 "Expat.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_XML__Parser__Expat_PositionContext); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_PositionContext)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, lines");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	lines = (int)SvIV(ST(1))
;
#line 1905 "Expat.xs"
	int parsepos;
        int size;
        const char *pos = XML_GetInputContext(parser, &parsepos, &size);
	const char *markbeg;
	const char *limit;
	const char *markend;
	int length, relpos;
	int  cnt;

#line 2406 "Expat.c"
#line 1915 "Expat.xs"
	  if (! pos)
            return;

	  for (markbeg = &pos[parsepos], cnt = 0; markbeg >= pos; markbeg--)
	    {
	      if (*markbeg == '\n')
		{
		  cnt++;
		  if (cnt > lines)
		    break;
		}
	    }

	  markbeg++;

          relpos = 0;
	  limit = &pos[size];
	  for (markend = &pos[parsepos + 1], cnt = 0;
	       markend < limit;
	       markend++)
	    {
	      if (*markend == '\n')
		{
		  if (cnt == 0)
                     relpos = (markend - markbeg) + 1;
		  cnt++;
		  if (cnt > lines)
		    {
		      markend++;
		      break;
		    }
		}
	    }

	  length = markend - markbeg;
          if (relpos == 0)
            relpos = length;

          EXTEND(sp, 2);
	  PUSHs(sv_2mortal(newSVpvn(markbeg, length)));
	  PUSHs(sv_2mortal(newSViv(relpos)));
#line 2449 "Expat.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_XML__Parser__Expat_GenerateNSName); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GenerateNSName)
{
    dVAR; dXSARGS;
    if (items != 4)
       croak_xs_usage(cv,  "name, xml_namespace, table, list");
    {
	SV *	name = ST(0)
;
	SV *	xml_namespace = ST(1)
;
	SV *	table = ST(2)
;
	SV *	list = ST(3)
;
	SV *	RETVAL;
#line 1964 "Expat.xs"
	{
	  STRLEN	nmlen, nslen;
	  char *	nmstr;
	  char *	nsstr;
	  char *	buff;
	  char *	bp;
	  char *	blim;

	  nmstr = SvPV(name, nmlen);
	  nsstr = SvPV(xml_namespace, nslen);

	  /* Form a namespace-name string that looks like expat's */
	  Newx(buff, nmlen + nslen + 2, char);
	  bp = buff;
	  blim = bp + nslen;
	  while (bp < blim)
	    *bp++ = *nsstr++;
	  *bp++ = NSDELIM;
	  blim = bp + nmlen;
	  while (bp < blim)
	    *bp++ = *nmstr++;
	  *bp = '\0';

	  RETVAL = gen_ns_name(buff, (HV *) SvRV(table), (AV *) SvRV(list));
	  Safefree(buff);
	}	
#line 2499 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_DefaultCurrent); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_DefaultCurrent)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
#line 1997 "Expat.xs"
	{
	  XML_DefaultCurrent(parser);
	}
#line 2520 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_RecognizedString); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_RecognizedString)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	RETVAL;
#line 2005 "Expat.xs"
	{
	  XML_DefaultHandler dflthndl = (XML_DefaultHandler) 0;
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	  if (cbv->dflt_sv) {
	    dflthndl = defaulthandle;
	  }

	  if (cbv->recstring) {
	    sv_setpvn(cbv->recstring, "", 0);
	  }

	  if (cbv->no_expand)
	    XML_SetDefaultHandler(parser, recString);
	  else
	    XML_SetDefaultHandlerExpand(parser, recString);

	  XML_DefaultCurrent(parser);

	  if (cbv->no_expand)
	    XML_SetDefaultHandler(parser, dflthndl);
	  else
	    XML_SetDefaultHandlerExpand(parser, dflthndl);

	  RETVAL = newSVsv(cbv->recstring);
	}
#line 2563 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetErrorCode); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetErrorCode)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	RETVAL;
	dXSTARG;

	RETVAL = XML_GetErrorCode(parser);
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetCurrentLineNumber); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetCurrentLineNumber)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	RETVAL;
#line 2042 "Expat.xs"
	{
	    XML_Size line = XML_GetCurrentLineNumber(parser);
#if (defined(XML_LARGE_SIZE) && UVSIZE < 8)
	    /* XML_Size is unsigned long long but UV is 32-bit; use NV */
	    RETVAL = newSVnv((NV)line);
#else
	    RETVAL = newSVuv((UV)line);
#endif
	}
#line 2611 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetCurrentColumnNumber); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetCurrentColumnNumber)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	RETVAL;
#line 2059 "Expat.xs"
	{
	    XML_Size col = XML_GetCurrentColumnNumber(parser);
#if (defined(XML_LARGE_SIZE) && UVSIZE < 8)
	    /* XML_Size is unsigned long long but UV is 32-bit; use NV */
	    RETVAL = newSVnv((NV)col);
#else
	    RETVAL = newSVuv((UV)col);
#endif
	}
#line 2639 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetCurrentByteIndex); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetCurrentByteIndex)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	RETVAL;
#line 2075 "Expat.xs"
	{
	    XML_Index byte_index = XML_GetCurrentByteIndex(parser);
#if (defined(XML_LARGE_SIZE) && IVSIZE < 8)
	    /* XML_Index is long long but IV is 32-bit; use NV to avoid overflow */
	    RETVAL = newSVnv((NV)byte_index);
#else
	    RETVAL = newSViv((IV)byte_index);
#endif
	}
#line 2667 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetCurrentByteCount); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetCurrentByteCount)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	RETVAL;
	dXSTARG;

	RETVAL = XML_GetCurrentByteCount(parser);
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_GetSpecifiedAttributeCount); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_GetSpecifiedAttributeCount)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	RETVAL;
	dXSTARG;

	RETVAL = XML_GetSpecifiedAttributeCount(parser);
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_ErrorString); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ErrorString)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "code");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	int	code = (int)SvIV(ST(0))
;
#line 2099 "Expat.xs"
	{
	  const char *ret = XML_ErrorString(code);
	  XPUSHs(sv_2mortal(newSVpv(ret, 0)));
	}
#line 2731 "Expat.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_XML__Parser__Expat_ExpatVersion); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ExpatVersion)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
#line 2107 "Expat.xs"
	const XML_LChar *ver = XML_ExpatVersion();
	XPUSHs(sv_2mortal(newSVpv((const char *)ver, 0)));
#line 2750 "Expat.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_XML__Parser__Expat_ExpatVersionInfo); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ExpatVersionInfo)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
#line 2113 "Expat.xs"
	const char *ver = (const char *)XML_ExpatVersion();
	/* Parse "expat_X.Y.Z" to extract runtime version components.
	   Avoids XML_ExpatVersionInfo() struct return which is broken
	   under -fpcc-struct-return ABI mismatch. */
	int major = 0, minor = 0, micro = 0;
	const char *p = ver;
	while (*p && *p != '_') p++;
	if (*p == '_') p++;
	major = atoi(p);
	while (*p && *p != '.') p++;
	if (*p == '.') p++;
	minor = atoi(p);
	while (*p && *p != '.') p++;
	if (*p == '.') p++;
	micro = atoi(p);
	EXTEND(SP, 6);
	PUSHs(sv_2mortal(newSVpv("major", 5)));
	PUSHs(sv_2mortal(newSViv(major)));
	PUSHs(sv_2mortal(newSVpv("minor", 5)));
	PUSHs(sv_2mortal(newSViv(minor)));
	PUSHs(sv_2mortal(newSVpv("micro", 5)));
	PUSHs(sv_2mortal(newSViv(micro)));
#line 2789 "Expat.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_XML__Parser__Expat_LoadEncoding); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_LoadEncoding)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "data, size");
    {
	char *	data = (char *)SvPV_nolen(ST(0))
;
	int	size = (int)SvIV(ST(1))
;
	SV *	RETVAL;
#line 2141 "Expat.xs"
	{
	  Encmap_Header *emh = (Encmap_Header *) data;
	  unsigned pfxsize, bmsize;

	  if (size < sizeof(Encmap_Header)
	      || ntohl(emh->magic) != ENCMAP_MAGIC) {
	    RETVAL = &PL_sv_undef;
	  }
	  else {
	    Encinfo	*entry;
	    SV		*sv;
	    PrefixMap	*pfx;
	    unsigned short *bm;
	    int namelen;
	    int i;

	    pfxsize = ntohs(emh->pfsize);
	    bmsize  = ntohs(emh->bmsize);

	    if (size != (sizeof(Encmap_Header)
			 + pfxsize * sizeof(PrefixMap)
			 + bmsize * sizeof(unsigned short))) {
	      RETVAL = &PL_sv_undef;
	    }
	    else {
	      /* Convert to uppercase and get name length */

	      for (i = 0; i < sizeof(emh->name); i++) {
		char c = emh->name[i];

		  if (c == (char) 0)
		    break;

		if (c >= 'a' && c <= 'z')
		  emh->name[i] -= 'a' - 'A';
	      }
	      namelen = i;

	      RETVAL = newSVpvn(emh->name, namelen);

	      Newx(entry, 1, Encinfo);
	      if (! entry)
	        croak("Could not allocate encoding entry");

	      entry->prefixes_size = pfxsize;
	      entry->bytemap_size  = bmsize;
	      for (i = 0; i < 256; i++) {
		entry->firstmap[i] = ntohl(emh->map[i]);
	      }

	      pfx = (PrefixMap *) &data[sizeof(Encmap_Header)];
	      bm = (unsigned short *) (((char *) pfx)
				       + sizeof(PrefixMap) * pfxsize);

	      Newx(entry->prefixes, pfxsize, PrefixMap);
	      if (! entry->prefixes) {
	        Safefree(entry);
	        croak("Could not allocate encoding prefixes");
	      }

	      Newx(entry->bytemap, bmsize, unsigned short);
	      if (! entry->bytemap) {
	        Safefree(entry->prefixes);
	        Safefree(entry);
	        croak("Could not allocate encoding bytemap");
	      }

	      for (i = 0; i < pfxsize; i++, pfx++) {
		PrefixMap *dest = &entry->prefixes[i];

		dest->min = pfx->min;
		dest->len = pfx->len;
		dest->bmap_start = ntohs(pfx->bmap_start);
		Copy(pfx->ispfx, dest->ispfx,
		     sizeof(pfx->ispfx) + sizeof(pfx->ischar), unsigned char);
	      }

	      for (i = 0; i < bmsize; i++)
		entry->bytemap[i] = ntohs(bm[i]);

	      sv = newSViv(0);
	      sv_setref_pv(sv, "XML::Parser::Encinfo", (void *) entry);

	      if (! EncodingTable) {
		EncodingTable
		  = get_hv("XML::Parser::Expat::Encoding_Table",
				FALSE);
		if (! EncodingTable)
		  croak("Can't find XML::Parser::Expat::Encoding_Table");
	      }

	      hv_store(EncodingTable, emh->name, namelen, sv, 0);
	    }
	  }
	}
#line 2904 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


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

	if (sv_derived_from(ST(0), "XML::Parser::Encinfo")) {
	    IV tmp = SvIV((SV*)SvRV(ST(0)));
	    enc = (Encinfo *) tmp;
	}
	else
	    croak("enc is not of type XML::Parser::Encinfo")
;
#line 2243 "Expat.xs"
	Safefree(enc->bytemap);
	Safefree(enc->prefixes);
	Safefree(enc);
#line 2932 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_OriginalString); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_OriginalString)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	RETVAL;
#line 2251 "Expat.xs"
	{
	  int parsepos, size;
	  const char *buff = XML_GetInputContext(parser, &parsepos, &size);
	  if (buff) {
	    RETVAL = newSVpvn(&buff[parsepos],
			      XML_GetCurrentByteCount(parser));
	  }
	  else {
	    RETVAL = newSVpv("", 0);
	  }
	}
#line 2960 "Expat.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetStartCdataHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetStartCdataHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, startcd_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	startcd_sv = ST(1)
;
	SV *	RETVAL;
#line 2270 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
	  XML_StartCdataSectionHandler scdhndl =
	    (XML_StartCdataSectionHandler) 0;

	  XMLP_UPD(startcd_sv);
	  if (SvTRUE(startcd_sv))
	    scdhndl = startCdata;

	  XML_SetStartCdataSectionHandler(parser, scdhndl);
	  PUSHRET;
	}
#line 2993 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetEndCdataHandler); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetEndCdataHandler)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, endcd_sv");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	endcd_sv = ST(1)
;
	SV *	RETVAL;
#line 2288 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
	  XML_EndCdataSectionHandler ecdhndl =
	    (XML_EndCdataSectionHandler) 0;

	  XMLP_UPD(endcd_sv);
	  if (SvTRUE(endcd_sv))
	    ecdhndl = endCdata;

	  XML_SetEndCdataSectionHandler(parser, ecdhndl);
	  PUSHRET;
	}
#line 3024 "Expat.c"
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_UnsetAllHandlers); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_UnsetAllHandlers)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
#line 2305 "Expat.xs"
	{
	  CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);

	  suspend_callbacks(cbv);
	  if (cbv->ns) {
	    XML_SetNamespaceDeclHandler(cbv->p,
					(XML_StartNamespaceDeclHandler) 0,
					(XML_EndNamespaceDeclHandler) 0);
	  }

	  XML_SetElementHandler(parser,
				(XML_StartElementHandler) 0,
				(XML_EndElementHandler) 0);

	  XML_SetUnknownEncodingHandler(parser,
					(XML_UnknownEncodingHandler) 0,
					(void *) 0);
	}
#line 3058 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_ElementIndex); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_ElementIndex)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "parser");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	RETVAL;
	dXSTARG;
#line 2328 "Expat.xs"
        {
          CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
          RETVAL = cbv->st_serial_stack[cbv->st_serial_stackptr];
        }
#line 3080 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SkipUntil); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SkipUntil)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, index");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	unsigned int	index = (unsigned int)SvUV(ST(1))
;
#line 2340 "Expat.xs"
        {
          CallbackVector * cbv = (CallbackVector *) XML_GetUserData(parser);
	  if (index <= cbv->st_serial)
	    return;
	  cbv->skip_until = index;
	  suspend_callbacks(cbv);
	}
#line 3107 "Expat.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_XML__Parser__Expat_Do_External_Parse); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_Do_External_Parse)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, result");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	SV *	result = ST(1)
;
	int	RETVAL;
	dXSTARG;
#line 2353 "Expat.xs"
	{
	  RETVAL = 0;

	  if (SvROK(result) && SvOBJECT(SvRV(result))) {
	    RETVAL = parse_stream(parser, result);
	  }
	  else if (SvROK(result) && isGV(SvRV(result))) {
	    /* Lexical filehandle (open my $fh) - a reference to a glob */
	    IO *io = GvIOp((GV*)SvRV(result));
	    if (!io)
	      croak("ExternEnt handler returned an unopened filehandle");
	    RETVAL = parse_stream(parser,
				  sv_2mortal(newRV_inc((SV*) io)));
	  }
	  else if (isGV(result)) {
	    IO *io = GvIOp(result);
	    if (!io)
	      croak("ExternEnt handler returned an unopened filehandle");
	    RETVAL = parse_stream(parser,
				  sv_2mortal(newRV_inc((SV*) io)));
	  }
	  else if (SvPOK(result)) {
	    STRLEN  eslen;
	    char *entstr = SvPV(result, eslen);

	    RETVAL = XML_Parse(parser, entstr, eslen, 1);
	  }
	}
#line 3155 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}

#if (defined(XML_DTD) || (defined(XML_GE) && XML_GE == 1)) \
    && defined(XML_MAJOR_VERSION) \
    && (XML_MAJOR_VERSION > 2 \
        || (XML_MAJOR_VERSION == 2 && XML_MINOR_VERSION >= 4))
#define XSubPPtmpAAAA 1


XS_EUPXS(XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionMaximumAmplification); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionMaximumAmplification)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, maxamp");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	float	maxamp = (float)SvNV(ST(1))
;
	int	RETVAL;
	dXSTARG;
#line 2394 "Expat.xs"
	RETVAL = (int) XML_SetBillionLaughsAttackProtectionMaximumAmplification(
	    parser, maxamp);
#line 3185 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionActivationThreshold); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionActivationThreshold)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, threshold");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	unsigned long	threshold = (unsigned long)SvUV(ST(1))
;
	int	RETVAL;
	dXSTARG;
#line 2404 "Expat.xs"
	RETVAL = (int) XML_SetBillionLaughsAttackProtectionActivationThreshold(
	    parser, (unsigned long long) threshold);
#line 3209 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}

#endif
#ifdef HAVE_XML_SETALLOCTRACKER
#define XSubPPtmpAAAB 1


XS_EUPXS(XS_XML__Parser__Expat_SetAllocTrackerMaximumAmplification); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetAllocTrackerMaximumAmplification)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, maxamp");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	float	maxamp = (float)SvNV(ST(1))
;
	int	RETVAL;
	dXSTARG;
#line 2418 "Expat.xs"
	RETVAL = (int) XML_SetAllocTrackerMaximumAmplification(
	    parser, maxamp);
#line 3237 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_XML__Parser__Expat_SetAllocTrackerActivationThreshold); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetAllocTrackerActivationThreshold)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, threshold");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	unsigned long	threshold = (unsigned long)SvUV(ST(1))
;
	int	RETVAL;
	dXSTARG;
#line 2428 "Expat.xs"
	RETVAL = (int) XML_SetAllocTrackerActivationThreshold(
	    parser, (unsigned long long) threshold);
#line 3261 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}

#endif
#if defined(XML_MAJOR_VERSION) \
    && (XML_MAJOR_VERSION > 2 \
        || (XML_MAJOR_VERSION == 2 \
            && (XML_MINOR_VERSION > 6 \
                || (XML_MINOR_VERSION == 6 && XML_MICRO_VERSION >= 0))))
#define XSubPPtmpAAAC 1


XS_EUPXS(XS_XML__Parser__Expat_SetReparseDeferralEnabled); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_XML__Parser__Expat_SetReparseDeferralEnabled)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "parser, enabled");
    {
	XML_Parser	parser = INT2PTR(XML_Parser,SvIV(ST(0)))
;
	int	enabled = (int)SvIV(ST(1))
;
	int	RETVAL;
	dXSTARG;
#line 2446 "Expat.xs"
	RETVAL = (int) XML_SetReparseDeferralEnabled(parser,
	    (XML_Bool) enabled);
#line 3293 "Expat.c"
	TARGi((IV)RETVAL, 1);
	ST(0) = TARG;
    }
    XSRETURN(1);
}

#endif
#ifdef __cplusplus
extern "C" {
#endif
XS_EXTERNAL(boot_XML__Parser__Expat); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_XML__Parser__Expat)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("XML::Parser::Expat::ParserCreate", XS_XML__Parser__Expat_ParserCreate);
        newXS_deffile("XML::Parser::Expat::ParserRelease", XS_XML__Parser__Expat_ParserRelease);
        newXS_deffile("XML::Parser::Expat::ParserFree", XS_XML__Parser__Expat_ParserFree);
        newXS_deffile("XML::Parser::Expat::ParseString", XS_XML__Parser__Expat_ParseString);
        newXS_deffile("XML::Parser::Expat::ParseStream", XS_XML__Parser__Expat_ParseStream);
        newXS_deffile("XML::Parser::Expat::ParsePartial", XS_XML__Parser__Expat_ParsePartial);
        newXS_deffile("XML::Parser::Expat::ParseDone", XS_XML__Parser__Expat_ParseDone);
        newXS_deffile("XML::Parser::Expat::SetStartElementHandler", XS_XML__Parser__Expat_SetStartElementHandler);
        newXS_deffile("XML::Parser::Expat::SetEndElementHandler", XS_XML__Parser__Expat_SetEndElementHandler);
        newXS_deffile("XML::Parser::Expat::SetCharacterDataHandler", XS_XML__Parser__Expat_SetCharacterDataHandler);
        newXS_deffile("XML::Parser::Expat::SetProcessingInstructionHandler", XS_XML__Parser__Expat_SetProcessingInstructionHandler);
        newXS_deffile("XML::Parser::Expat::SetCommentHandler", XS_XML__Parser__Expat_SetCommentHandler);
        newXS_deffile("XML::Parser::Expat::SetDefaultHandler", XS_XML__Parser__Expat_SetDefaultHandler);
        newXS_deffile("XML::Parser::Expat::SetUnparsedEntityDeclHandler", XS_XML__Parser__Expat_SetUnparsedEntityDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetNotationDeclHandler", XS_XML__Parser__Expat_SetNotationDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetExternalEntityRefHandler", XS_XML__Parser__Expat_SetExternalEntityRefHandler);
        newXS_deffile("XML::Parser::Expat::SetExtEntFinishHandler", XS_XML__Parser__Expat_SetExtEntFinishHandler);
        newXS_deffile("XML::Parser::Expat::SetEntityDeclHandler", XS_XML__Parser__Expat_SetEntityDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetElementDeclHandler", XS_XML__Parser__Expat_SetElementDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetAttListDeclHandler", XS_XML__Parser__Expat_SetAttListDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetDoctypeHandler", XS_XML__Parser__Expat_SetDoctypeHandler);
        newXS_deffile("XML::Parser::Expat::SetEndDoctypeHandler", XS_XML__Parser__Expat_SetEndDoctypeHandler);
        newXS_deffile("XML::Parser::Expat::SetXMLDeclHandler", XS_XML__Parser__Expat_SetXMLDeclHandler);
        newXS_deffile("XML::Parser::Expat::SetBase", XS_XML__Parser__Expat_SetBase);
        newXS_deffile("XML::Parser::Expat::GetBase", XS_XML__Parser__Expat_GetBase);
        newXS_deffile("XML::Parser::Expat::PositionContext", XS_XML__Parser__Expat_PositionContext);
        newXS_deffile("XML::Parser::Expat::GenerateNSName", XS_XML__Parser__Expat_GenerateNSName);
        newXS_deffile("XML::Parser::Expat::DefaultCurrent", XS_XML__Parser__Expat_DefaultCurrent);
        newXS_deffile("XML::Parser::Expat::RecognizedString", XS_XML__Parser__Expat_RecognizedString);
        newXS_deffile("XML::Parser::Expat::GetErrorCode", XS_XML__Parser__Expat_GetErrorCode);
        newXS_deffile("XML::Parser::Expat::GetCurrentLineNumber", XS_XML__Parser__Expat_GetCurrentLineNumber);
        newXS_deffile("XML::Parser::Expat::GetCurrentColumnNumber", XS_XML__Parser__Expat_GetCurrentColumnNumber);
        newXS_deffile("XML::Parser::Expat::GetCurrentByteIndex", XS_XML__Parser__Expat_GetCurrentByteIndex);
        newXS_deffile("XML::Parser::Expat::GetCurrentByteCount", XS_XML__Parser__Expat_GetCurrentByteCount);
        newXS_deffile("XML::Parser::Expat::GetSpecifiedAttributeCount", XS_XML__Parser__Expat_GetSpecifiedAttributeCount);
        newXS_deffile("XML::Parser::Expat::ErrorString", XS_XML__Parser__Expat_ErrorString);
        newXS_deffile("XML::Parser::Expat::ExpatVersion", XS_XML__Parser__Expat_ExpatVersion);
        newXS_deffile("XML::Parser::Expat::ExpatVersionInfo", XS_XML__Parser__Expat_ExpatVersionInfo);
        newXS_deffile("XML::Parser::Expat::LoadEncoding", XS_XML__Parser__Expat_LoadEncoding);
        newXS_deffile("XML::Parser::Expat::FreeEncoding", XS_XML__Parser__Expat_FreeEncoding);
        newXS_deffile("XML::Parser::Expat::OriginalString", XS_XML__Parser__Expat_OriginalString);
        newXS_deffile("XML::Parser::Expat::SetStartCdataHandler", XS_XML__Parser__Expat_SetStartCdataHandler);
        newXS_deffile("XML::Parser::Expat::SetEndCdataHandler", XS_XML__Parser__Expat_SetEndCdataHandler);
        newXS_deffile("XML::Parser::Expat::UnsetAllHandlers", XS_XML__Parser__Expat_UnsetAllHandlers);
        newXS_deffile("XML::Parser::Expat::ElementIndex", XS_XML__Parser__Expat_ElementIndex);
        newXS_deffile("XML::Parser::Expat::SkipUntil", XS_XML__Parser__Expat_SkipUntil);
        newXS_deffile("XML::Parser::Expat::Do_External_Parse", XS_XML__Parser__Expat_Do_External_Parse);
#if XSubPPtmpAAAA
        newXS_deffile("XML::Parser::Expat::SetBillionLaughsAttackProtectionMaximumAmplification", XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionMaximumAmplification);
        newXS_deffile("XML::Parser::Expat::SetBillionLaughsAttackProtectionActivationThreshold", XS_XML__Parser__Expat_SetBillionLaughsAttackProtectionActivationThreshold);
#endif
#if XSubPPtmpAAAB
        newXS_deffile("XML::Parser::Expat::SetAllocTrackerMaximumAmplification", XS_XML__Parser__Expat_SetAllocTrackerMaximumAmplification);
        newXS_deffile("XML::Parser::Expat::SetAllocTrackerActivationThreshold", XS_XML__Parser__Expat_SetAllocTrackerActivationThreshold);
#endif
#if XSubPPtmpAAAC
        newXS_deffile("XML::Parser::Expat::SetReparseDeferralEnabled", XS_XML__Parser__Expat_SetReparseDeferralEnabled);
#endif

    /* Initialisation Section */

#if XSubPPtmpAAAA
#endif
#if XSubPPtmpAAAB
#endif
#if XSubPPtmpAAAC
#endif
#line 3396 "Expat.c"

    /* End of Initialisation Section */

#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

#ifdef __cplusplus
}
#endif
