コード例 #1
0
ファイル: buf.c プロジェクト: GerHobbelt/libxml2
/**
 * xmlBufGrowInternal:
 * @buf:  the buffer
 * @len:  the minimum free size to allocate
 *
 * Grow the available space of an XML buffer, @len is the target value
 * Error checking should be done on buf->error since using the return
 * value doesn't work that well
 *
 * Returns 0 in case of error or the length made available otherwise
 */
static size_t
xmlBufGrowInternal(xmlBufPtr buf, size_t len) {
    size_t size;
    xmlChar *newbuf;

    if ((buf == NULL) || (buf->error != 0)) return(0);
    CHECK_COMPAT(buf)

    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
    if (buf->use + len < buf->size)
        return(buf->size - buf->use);

    /*
     * Windows has a BIG problem on realloc timing, so we try to double
     * the buffer size (if that's enough) (bug 146697)
     * Apparently BSD too, and it's probably best for linux too
     * On an embedded system this may be something to change
     */
#if 1
    if (buf->size > (size_t) len)
        size = buf->size * 2;
    else
        size = buf->use + len + 100;
#else
    size = buf->use + len + 100;
#endif

    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
        size_t start_buf = buf->content - buf->contentIO;

	newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size);
	if (newbuf == NULL) {
	    xmlBufMemoryError(buf, "growing buffer");
	    return(0);
	}
	buf->contentIO = newbuf;
	buf->content = newbuf + start_buf;
    } else {
	newbuf = (xmlChar *) xmlRealloc(buf->content, size);
	if (newbuf == NULL) {
	    xmlBufMemoryError(buf, "growing buffer");
	    return(0);
	}
	buf->content = newbuf;
    }
    buf->size = size;
    UPDATE_COMPAT(buf)
    return(buf->size - buf->use);
}
コード例 #2
0
ファイル: xml_utilities.c プロジェクト: anantshri/kraken
xmlChar *xml_convert_input(const char *in, const char *encoding) {
	xmlChar *out;
	int ret;
	int size;
	int out_size;
	int temp;
	xmlCharEncodingHandlerPtr handler;
	handler = xmlFindCharEncodingHandler(encoding);
	assert(handler != NULL);
	size = (int)strlen(in) + 1;
	out_size = size * 2 - 1;
	out = (unsigned char *)xmlMalloc((size_t) out_size);
	if (out != NULL) {
		temp = size - 1;
		ret = handler->input(out, &out_size, (const xmlChar *)in, &temp);
		if ((ret < 0) || (temp - size + 1)) {
			xmlFree(out);
			out = 0;
		} else {
			out = (unsigned char *)xmlRealloc(out, out_size + 1);
			out[out_size] = 0;  /*null terminating out */
		}
	}
	return out;
}
コード例 #3
0
ファイル: schematron.c プロジェクト: AllenChanAncA/WiEngine
/**
 * xmlSchematronPushInclude:
 * @ctxt:  the schema parser context
 * @doc:  the included document
 * @cur:  the current include node
 *
 * Add an included document
 */
static void
xmlSchematronPushInclude(xmlSchematronParserCtxtPtr ctxt,
                        xmlDocPtr doc, xmlNodePtr cur)
{
    if (ctxt->includes == NULL) {
        ctxt->maxIncludes = 10;
        ctxt->includes = (xmlNodePtr *)
	    xmlMalloc(ctxt->maxIncludes * 2 * sizeof(xmlNodePtr));
	if (ctxt->includes == NULL) {
	    xmlSchematronPErrMemory(NULL, "allocating parser includes",
				    NULL);
	    return;
	}
        ctxt->nbIncludes = 0;
    } else if (ctxt->nbIncludes + 2 >= ctxt->maxIncludes) {
        xmlNodePtr *tmp;

	tmp = (xmlNodePtr *)
	    xmlRealloc(ctxt->includes, ctxt->maxIncludes * 4 *
	               sizeof(xmlNodePtr));
	if (tmp == NULL) {
	    xmlSchematronPErrMemory(NULL, "allocating parser includes",
				    NULL);
	    return;
	}
        ctxt->includes = tmp;
	ctxt->maxIncludes *= 2;
    }
    ctxt->includes[2 * ctxt->nbIncludes] = cur;
    ctxt->includes[2 * ctxt->nbIncludes + 1] = (xmlNodePtr) doc;
    ctxt->nbIncludes++;
}
コード例 #4
0
/*
#define GROW_BUFFER_REENTRANT() {                 \
    buffer_size *= 2;                           \
    buffer = (xmlChar *)                        \
            xmlRealloc(buffer, buffer_size * sizeof(xmlChar));  \
    if (buffer == NULL) {                       \
        SET_OOM_FLAG;                           \
        xmlGenericError(xmlGenericErrorContext, EMBED_ERRTXT("realloc failed\n"));  \
        return(NULL);                           \
    }                                           \
}
*/
static void*
xmlGrowBufferReentrant(int* buffer_size, void* buffer) {
    void* ptr;
    (*buffer_size) *= 2;
    ptr = (xmlChar*) xmlRealloc(buffer, *buffer_size * sizeof(xmlChar));
    /// if(!ptr) removed -- handling is done by caller
    return ptr;
}
コード例 #5
0
ファイル: entities.c プロジェクト: KB3NZQ/hydrairc
static int growBuffer(void) {
    static_buffer_size *= 2;
    static_buffer = (xmlChar *) xmlRealloc(static_buffer, static_buffer_size * sizeof(xmlChar));
    if (static_buffer == NULL) {
        perror("realloc failed");
	return(-1);
    }
    return(0);
}
コード例 #6
0
ファイル: cpXmlCmdOutput.cpp プロジェクト: jsprenkle/cpXmlCmd
/**
 * ConvertInput:
 * @in: string in a given encoding
 * @encoding: the encoding used
 *
 * Converts @in into UTF-8 for processing with libxml2 APIs
 *
 * Returns the converted UTF-8 string, or NULL in case of error.
 */
xmlChar* cpXmlCmdOutput::ConvertInput( const xmlChar* in, const xmlChar* encoding )
{
   xmlChar *out;
   int ret;
   int size;
   int out_size;
   int temp;
   xmlCharEncodingHandlerPtr handler;

   if ( in == 0 )
      return 0;

   handler = xmlFindCharEncodingHandler( (const char *)encoding );

   if ( !handler )
   {
      printf( "ConvertInput: no encoding handler found for '%s'\n", encoding != 0 ? (const char *)encoding : "" );
      return 0;
   }

   size = (int) strlen( (const char*) in ) + 1;
   out_size = size * 2 - 1;
   out = (xmlChar *) xmlMalloc( (size_t) out_size );

   if ( out != 0 )
   {
      temp = size - 1;
      ret = handler->input( out, &out_size, in, &temp );
      if ( ( ret < 0 ) || ( temp - size + 1 ) )
      {
         if ( ret < 0 )
         {
            printf( "ConvertInput: conversion wasn't successful.\n" );
         }
         else
         {
            printf
               ( "ConvertInput: conversion wasn't successful. converted: %i octets.\n",
               temp );
         }

         xmlFree( out );
         out = 0;
      }
      else
      {
         out = (xmlChar *) xmlRealloc( out, out_size + 1 );
         out[out_size] = 0; //null termination
      }
   }
   else
   {
      printf( "ConvertInput: no mem\n" );
   }

   return out;
}
コード例 #7
0
/**
 * xmlXIncludeAddTxt:
 * @ctxt:  the XInclude context
 * @txt:  the new text node
 * @url:  the associated URL
 *
 * Add a new txtument to the list
 */
static void
xmlXIncludeAddTxt(xmlXIncludeCtxtPtr ctxt, xmlNodePtr txt, const xmlURL url) {
    if (ctxt->txtMax == 0) {
        ctxt->txtMax = 4;
        ctxt->txtTab = (xmlNodePtr *) xmlMalloc(ctxt->txtMax *
                                                sizeof(ctxt->txtTab[0]));
        if (ctxt->txtTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
        ctxt->txturlTab = (xmlURL *) xmlMalloc(ctxt->txtMax *
                                               sizeof(ctxt->txturlTab[0]));
        if (ctxt->txturlTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
    }
    if (ctxt->txtNr >= ctxt->txtMax) {
        ctxt->txtMax *= 2;
        ctxt->txtTab = (xmlNodePtr *) xmlRealloc(ctxt->txtTab,
                       ctxt->txtMax * sizeof(ctxt->txtTab[0]));
        if (ctxt->txtTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
        ctxt->txturlTab = (xmlURL *) xmlRealloc(ctxt->txturlTab,
                                                ctxt->txtMax * sizeof(ctxt->urlTab[0]));
        if (ctxt->txturlTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
    }
    ctxt->txtTab[ctxt->txtNr] = txt;
    ctxt->txturlTab[ctxt->txtNr] = xmlStrdup(url);
    ctxt->txtNr++;
}
コード例 #8
0
/**
 * xmlXIncludeAddNode:
 * @ctxt:  the XInclude context
 * @node:  the new node
 *
 * Add a new node to process to an XInclude context
 */
static void
xmlXIncludeAddNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) {
    if (ctxt->incMax == 0) {
        ctxt->incMax = 4;
        ctxt->incTab = (xmlNodePtr *) xmlMalloc(ctxt->incMax *
                                                sizeof(ctxt->incTab[0]));
        if (ctxt->incTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
        ctxt->repTab = (xmlNodePtr *) xmlMalloc(ctxt->incMax *
                                                sizeof(ctxt->repTab[0]));
        if (ctxt->repTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
    }
    if (ctxt->incNr >= ctxt->incMax) {
        ctxt->incMax *= 2;
        ctxt->incTab = (xmlNodePtr *) xmlRealloc(ctxt->incTab,
                       ctxt->incMax * sizeof(ctxt->incTab[0]));
        if (ctxt->incTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
        ctxt->repTab = (xmlNodePtr *) xmlRealloc(ctxt->repTab,
                       ctxt->incMax * sizeof(ctxt->repTab[0]));
        if (ctxt->repTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
    }
    ctxt->incTab[ctxt->incNr] = node;
    ctxt->repTab[ctxt->incNr] = NULL;
    ctxt->incNr++;
}
コード例 #9
0
ファイル: ext_bit.c プロジェクト: Juniper/libslax
static void
extBitFromHex (xmlXPathParserContextPtr ctxt, int nargs)
{
    xmlChar *res;
    int maxw = 0, width = 0, i;
    unsigned long long val, v2;

    if (nargs != 1 && nargs != 2) {
	xmlXPathSetArityError(ctxt);
	return;
    }

    /* Pop args in reverse order */
    if (nargs == 2) {
	maxw = xmlXPathPopNumber(ctxt);
	if (maxw < 0 || xmlXPathCheckError(ctxt))
	    return;
    }

    res = xmlXPathPopString(ctxt);
    if (res == NULL || xmlXPathCheckError(ctxt))
	return;

    val = strtoull((char *) res, 0, 0x10);
    
    for (width = 0, v2 = val; v2; width++, v2 /= 2)
	continue;

    if (width == 0)		/* Gotta have one zero */
	width = 1;

    if (maxw < width)
	maxw = width;

    res = xmlRealloc(res, maxw + 1);
    if (res) {
	res[maxw] = '\0';

	for (i = maxw - 1, v2 = val; i >= 0; i--) {
	    if (width-- <= 0)
		res[i] = '0';
	    else {
		res[i] = (v2 & 1) ? '1' : '0';
		v2 /= 2;
	    }
	}
    }

    xmlXPathReturnString(ctxt, res);
}
コード例 #10
0
ファイル: attrvt.c プロジェクト: Anthony-Mckale/node-libxslt
/**
 * xsltSetAttrVTsegment:
 * @ avt: pointer to an xsltAttrVT structure
 * @ val: the value to be set to the next available segment
 *
 * Within xsltCompileAttr there are several places where a value
 * needs to be added to the 'segments' array within the xsltAttrVT
 * structure, and at each place the allocated size may have to be
 * re-allocated.  This routine takes care of that situation.
 *
 * Returns the avt pointer, which may have been changed by a re-alloc
 */
static xsltAttrVTPtr
xsltSetAttrVTsegment(xsltAttrVTPtr avt, void *val) {
    if (avt->nb_seg >= avt->max_seg) {
	avt = (xsltAttrVTPtr) xmlRealloc(avt, sizeof(xsltAttrVT) +
			avt->max_seg * sizeof(void *));
	if (avt == NULL) {
	    return NULL;
	}
	memset(&avt->segments[avt->nb_seg], 0, MAX_AVT_SEG*sizeof(void *));
	avt->max_seg += MAX_AVT_SEG;
    }
    avt->segments[avt->nb_seg++] = val;
    return avt;
}
コード例 #11
0
ファイル: attrvt.c プロジェクト: GNOME/libxslt
/**
 * xsltSetAttrVTsegment:
 * @ avt: pointer to an xsltAttrVT structure
 * @ val: the value to be set to the next available segment
 *
 * Within xsltCompileAttr there are several places where a value
 * needs to be added to the 'segments' array within the xsltAttrVT
 * structure, and at each place the allocated size may have to be
 * re-allocated.  This routine takes care of that situation.
 *
 * Returns the avt pointer, which may have been changed by a re-alloc
 */
static xsltAttrVTPtr
xsltSetAttrVTsegment(xsltAttrVTPtr avt, void *val) {
    if (avt->nb_seg >= avt->max_seg) {
        size_t size = sizeof(xsltAttrVT) +
                      (avt->max_seg + MAX_AVT_SEG) * sizeof(void *);
	xsltAttrVTPtr tmp = (xsltAttrVTPtr) xmlRealloc(avt, size);
	if (tmp == NULL) {
            xsltFreeAttrVT(avt);
	    return NULL;
	}
        avt = tmp;
	memset(&avt->segments[avt->nb_seg], 0, MAX_AVT_SEG*sizeof(void *));
	avt->max_seg += MAX_AVT_SEG;
    }
    avt->segments[avt->nb_seg++] = val;
    return avt;
}
コード例 #12
0
ファイル: ext_bit.c プロジェクト: Juniper/libslax
static void
extBitClearOrSet (xmlXPathParserContextPtr ctxt, int nargs, xmlChar value)
{
    xmlChar *res;
    int width, bitnum = 0, delta;
    xmlXPathObjectPtr xop;

    if (nargs != 1 && nargs != 2) {
	xmlXPathSetArityError(ctxt);
	return;
    }

    /* Pop args in reverse order */
    if (nargs == 2) {
	bitnum = xmlXPathPopNumber(ctxt);
	if (bitnum < 0 || xmlXPathCheckError(ctxt))
	    return;
    }

    xop = valuePop(ctxt);
    if (xop == NULL || xmlXPathCheckError(ctxt))
	return;
    res = extBitStringVal(ctxt, xop);
    if (res == NULL)
	return;

    width = xmlStrlen(res);
    delta = width - bitnum - 1;

    if (delta < 0) {
	xmlChar *newp = xmlRealloc(res, bitnum + 2);
	if (newp == NULL)
	    return;

	delta = -delta;
	memmove(newp + delta, newp, width + 1);
	newp[0] = value;
	memset(newp + 1, '0', delta - 1);
	res = newp;

    } else {
	res[delta] = value;
    }

    xmlXPathReturnString(ctxt, res);
}
コード例 #13
0
ファイル: ermanifest.c プロジェクト: vastin/iliad-hacking
xmlNodeSetPtr ermXmlGetChildNodeSet(xmlNodePtr parent, const char* childName)
{
    int  i;

    xmlNodeSetPtr nodeSet = NULL;  // return value
    xmlNodePtr    cur     = NULL;

    //TRACE("entry: parent [%p] [%s]", parent, parent ? parent->name : NULL);
    //TRACE("entry: node [%s]", childName);

    // allocate space for nodeset
    nodeSet = xmlMalloc( sizeof(xmlNodeSet) );
    if (nodeSet == NULL) { return NULL; }
    nodeSet->nodeNr  = 0;
    nodeSet->nodeMax = 10;
    nodeSet->nodeTab = xmlMalloc(10 * sizeof(xmlNode));
    if (nodeSet->nodeTab == NULL) { return NULL; }
    
    if (parent && childName && childName[0] != '\0')
    {
        cur = parent->children;
        while (cur != NULL)
        {
            if ( xmlStrcmp(cur->name, (const xmlChar*)childName) == 0 )
            {
                //TRACE("Found child node %s", childName);

                // extend space for nodeset
                if (nodeSet->nodeNr >= nodeSet->nodeMax)
                {
                    i = 2 * nodeSet->nodeMax;
                    nodeSet->nodeMax = i;
                    nodeSet->nodeTab = xmlRealloc(nodeSet->nodeTab, i * sizeof(xmlNode));
                    if (nodeSet->nodeTab == NULL) { return NULL; }
                }

                // add node to nodeset
                nodeSet->nodeTab[ nodeSet->nodeNr ] = cur;
                nodeSet->nodeNr++;
            }
            cur = cur->next;
        }
    }
    return nodeSet;
}
コード例 #14
0
ファイル: list.c プロジェクト: Arcenciel/DDReader
static int 
xmlSecPtrListEnsureSize(xmlSecPtrListPtr list, xmlSecSize size) {
    xmlSecPtr* newData;
    xmlSecSize newSize = 0;

    xmlSecAssert2(xmlSecPtrListIsValid(list), -1);
    
    if(size < list->max) {
	return(0);
    }

    switch(list->allocMode) {
	case xmlSecAllocModeExact:
	    newSize = size + 8;
	    break;
	case xmlSecAllocModeDouble:
	    newSize = 2 * size + 32;
	    break;
    }
    
    if(newSize < gInitialSize) {
	newSize = gInitialSize;
    }
    
    if(list->data != NULL) {
    	newData = (xmlSecPtr*)xmlRealloc(list->data, sizeof(xmlSecPtr) * newSize);
    } else {
    	newData = (xmlSecPtr*)xmlMalloc(sizeof(xmlSecPtr) * newSize);
    }
    if(newData == NULL) {
	xmlSecErr_a_ignorar7(XMLSEC_ERRORS_HERE,
		    xmlSecErrorsSafeString(xmlSecPtrListGetName(list)),
		    NULL,
		    XMLSEC_ERRORS_R_MALLOC_FAILED,
		    "sizeof(xmlSecPtr)*%d=%d", 
		    newSize, sizeof(xmlSecPtr) * newSize);
	return(-1);
    }
    
    list->data = newData;
    list->max = newSize;
    
    return(0);
}
コード例 #15
0
ファイル: xmlstring.c プロジェクト: SCIInstitute/SCIRun
xmlChar *
xmlStrncat(xmlChar *cur, const xmlChar *add, int len) {
    int size;
    xmlChar *ret;

    if ((add == NULL) || (len == 0))
        return(cur);
    if (cur == NULL)
        return(xmlStrndup(add, len));

    size = xmlStrlen(cur);
    ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar));
    if (ret == NULL) {
        xmlErrMemory(NULL, NULL);
        return(cur);
    }
    memcpy(&ret[size], add, len * sizeof(xmlChar));
    ret[size + len] = 0;
    return(ret);
}
コード例 #16
0
ファイル: ext_bit.c プロジェクト: Juniper/libslax
static void
extBitToHex (xmlXPathParserContextPtr ctxt, int nargs)
{
    xmlChar *res;
    xmlXPathObjectPtr xop;
    unsigned long long val;
    int i, len1, len2;

    if (nargs != 1) {
	xmlXPathSetArityError(ctxt);
	return;
    }

    /* Pop args in reverse order */
    xop = valuePop(ctxt);
    if (xop == NULL || xmlXPathCheckError(ctxt))
	return;
    res = extBitStringVal(ctxt, xop);
    if (res == NULL)
	return;

    for (i = 0, val = 0; i <= 64 && res[i]; i++) {
	val <<= 1;
	if (res[i] == '1')
	    val += 1;
    }

    if (i > 64)
	xmlXPathReturnNumber(ctxt, (double) -1);
    else {
	len1 = xmlStrlen(res);
	len2 = snprintf((char *) res, len1 + 1, "0x%qx", val);
	if (len2 > len1) {
	    res = xmlRealloc(res, len2 + 1);
	    if (res)
		snprintf((char *) res, len2 + 1, "0x%qx", val);
	}

	xmlXPathReturnString(ctxt, res);
    }
}
コード例 #17
0
ファイル: libxml2_xmlstring.c プロジェクト: jcemelanda/wormux
/**
 * xmlStrncat:
 * @param cur the original xmlChar* array
 * @param add the xmlChar* array added
 * @param len the length of add
 *
 * a strncat for array of xmlChar's, it will extend cur with the len
 * first bytes of add.
 *
 * Returns a new xmlChar*, the original cur is reallocated if needed
 * and should not be freed
 *
 * OOM: possible --> OOM flag is set  
 */
XMLPUBFUNEXPORT xmlChar*
xmlStrncat(xmlChar* cur, const xmlChar* add, int len)
{
    int size;
    xmlChar* ret;

    if ((add == NULL) || (len == 0))
        return(cur);
    if (cur == NULL)
        return(xmlStrndup(add, len));

    size = xmlStrlen(cur);
    // DONE: Fix xmlRealloc: Nothing to fix!
    ret = (xmlChar*) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar));
    if (!ret) {
        xmlErrMemory(NULL, NULL);
        return(cur);
    }
    memcpy(&ret[size], add, len * sizeof(xmlChar));
    ret[size + len] = 0;
    return(ret);
}
コード例 #18
0
ファイル: ext_bit.c プロジェクト: Juniper/libslax
static void
extBitFromInt (xmlXPathParserContextPtr ctxt, int nargs)
{
    xmlChar *res;
    xmlXPathObjectPtr xop;
    int width = 0, len;

    if (nargs != 1 && nargs != 2) {
	xmlXPathSetArityError(ctxt);
	return;
    }

    /* Pop args in reverse order */
    if (nargs == 2) {
	width = xmlXPathPopNumber(ctxt);
	if (width < 0 || xmlXPathCheckError(ctxt))
	    return;
    }

    xop = valuePop(ctxt);
    if (xop == NULL || xmlXPathCheckError(ctxt))
	return;
    res = extBitStringVal(ctxt, xop);
    if (res == NULL)
	return;

    len = xmlStrlen(res);
    if (width > len) {
	res = xmlRealloc(res, width + 1);
	if (res) {
	    int count = width - len;
	    memmove(res + count, res, len + 1);
	    memset(res, '0', count);
	}
    }

    xmlXPathReturnString(ctxt, res);
}
コード例 #19
0
/**
 * xmlPatternAdd:
 * @comp:  the compiled match expression
 * @op:  an op
 * @value:  the first value
 * @value2:  the second value
 *
 * Add an step to an XSLT Compiled Match
 *
 * Returns -1 in case of failure, 0 otherwise.
 */
static int
xmlPatternAdd(xmlPatParserContextPtr ctxt ATTRIBUTE_UNUSED,
                xmlPatternPtr comp,
                xmlPatOp op, xmlChar * value, xmlChar * value2)
{
    if (comp->nbStep >= comp->maxStep) {
        xmlStepOpPtr temp;
	temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
	                                 sizeof(xmlStepOp));
        if (temp == NULL) {
	    ERROR(ctxt, NULL, NULL,
			     "xmlPatternAdd: realloc failed\n");
	    return (-1);
	}
	comp->steps = temp;
	comp->maxStep *= 2;
    }
    comp->steps[comp->nbStep].op = op;
    comp->steps[comp->nbStep].value = value;
    comp->steps[comp->nbStep].value2 = value2;
    comp->nbStep++;
    return (0);
}
コード例 #20
0
/**
 * xmlReversePattern:
 * @comp:  the compiled match expression
 *
 * reverse all the stack of expressions
 *
 * returns 0 in case of success and -1 in case of error.
 */
static int
xmlReversePattern(xmlPatternPtr comp) {
    int i = 0;
    int j = comp->nbStep - 1;

    if (comp->nbStep >= comp->maxStep) {
        xmlStepOpPtr temp;
	temp = (xmlStepOpPtr) xmlRealloc(comp->steps, comp->maxStep * 2 *
	                                 sizeof(xmlStepOp));
        if (temp == NULL) {
	    ERROR(ctxt, NULL, NULL,
			     "xmlReversePattern: realloc failed\n");
	    return (-1);
	}
	comp->steps = temp;
	comp->maxStep *= 2;
    }
    while (j > i) {
	register const xmlChar *tmp;
	register xmlPatOp op;
	tmp = comp->steps[i].value;
	comp->steps[i].value = comp->steps[j].value;
	comp->steps[j].value = tmp;
	tmp = comp->steps[i].value2;
	comp->steps[i].value2 = comp->steps[j].value2;
	comp->steps[j].value2 = tmp;
	op = comp->steps[i].op;
	comp->steps[i].op = comp->steps[j].op;
	comp->steps[j].op = op;
	j--;
	i++;
    }
    comp->steps[comp->nbStep].value = NULL;
    comp->steps[comp->nbStep].value2 = NULL;
    comp->steps[comp->nbStep++].op = XML_OP_END;
    return(0);
}
コード例 #21
0
ファイル: schematron.c プロジェクト: AllenChanAncA/WiEngine
/**
 * xmlSchematronAddNamespace:
 * @ctxt:  the schema parser context
 * @prefix:  the namespace prefix
 * @ns:  the namespace name
 *
 * Add a namespace definition in the context
 */
static void
xmlSchematronAddNamespace(xmlSchematronParserCtxtPtr ctxt,
                          const xmlChar *prefix, const xmlChar *ns)
{
    if (ctxt->namespaces == NULL) {
        ctxt->maxNamespaces = 10;
        ctxt->namespaces = (const xmlChar **)
	    xmlMalloc(ctxt->maxNamespaces * 2 * sizeof(const xmlChar *));
	if (ctxt->namespaces == NULL) {
	    xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
				    NULL);
	    return;
	}
        ctxt->nbNamespaces = 0;
    } else if (ctxt->nbNamespaces + 2 >= ctxt->maxNamespaces) {
        const xmlChar **tmp;

	tmp = (const xmlChar **)
	    xmlRealloc((xmlChar **) ctxt->namespaces, ctxt->maxNamespaces * 4 *
	               sizeof(const xmlChar *));
	if (tmp == NULL) {
	    xmlSchematronPErrMemory(NULL, "allocating parser namespaces",
				    NULL);
	    return;
	}
        ctxt->namespaces = tmp;
	ctxt->maxNamespaces *= 2;
    }
    ctxt->namespaces[2 * ctxt->nbNamespaces] = 
        xmlDictLookup(ctxt->dict, ns, -1);
    ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = 
        xmlDictLookup(ctxt->dict, prefix, -1);
    ctxt->nbNamespaces++;
    ctxt->namespaces[2 * ctxt->nbNamespaces] = NULL;
    ctxt->namespaces[2 * ctxt->nbNamespaces + 1] = NULL;

}
コード例 #22
0
ファイル: xmlelement.cpp プロジェクト: alilloyd/livecode
/*Modified libxml code to avoid outputting paths with namespaces..at least for now*/
static xmlChar *MyxmlGetNodePath(xmlNodePtr node)
{
    xmlNodePtr cur, tmp, next;
    xmlChar *buffer = NULL, *temp;
    size_t buf_len;
    xmlChar *buf;
    char sep;
    const char *name;
    char nametemp[100];
    int occur = 0;

    if (node == NULL)
        return (NULL);

    buf_len = 500;
    buffer = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
    if (buffer == NULL)
        return (NULL);
    buf = (xmlChar *) xmlMalloc(buf_len * sizeof(xmlChar));
    if (buf == NULL) {
        xmlFree(buffer);
        return (NULL);
    }

    buffer[0] = 0;
    cur = node;
    do {
        name = "";
        sep = '?';
        occur = 0;
        if ((cur->type == XML_DOCUMENT_NODE) ||
            (cur->type == XML_HTML_DOCUMENT_NODE)) {
            if (buffer[0] == '/')
                break;
            sep = '/';
            next = NULL;
        } else if (cur->type == XML_ELEMENT_NODE) {
            sep = '/';
            name = (const char *) cur->name;
            next = cur->parent;

            /*
             * Thumbler index computation
             */
            tmp = cur->prev;
            while (tmp != NULL) {
                if (xmlStrEqual(cur->name, tmp->name))
                    occur++;
                tmp = tmp->prev;
            }
            if (occur == 0) {
                tmp = cur->next;
                while (tmp != NULL) {
                    if (xmlStrEqual(cur->name, tmp->name))
					{
                        occur++;
						break;
					}
                    tmp = tmp->next;
                }
                if (occur != 0)
                    occur = 1;
            } else
                occur++;
        } else if (cur->type == XML_ATTRIBUTE_NODE) {
            sep = '@';
            name = (const char *) (((xmlAttrPtr) cur)->name);
            next = ((xmlAttrPtr) cur)->parent;
        } else if (cur->type == XML_TEXT_NODE || cur -> type == XML_CDATA_SECTION_NODE) {
			sep = '/';
			name = "";
			next = cur->parent;
            tmp = cur->prev;
            while (tmp != NULL) {
                if (tmp->type == XML_TEXT_NODE || tmp -> type == XML_CDATA_SECTION_NODE)
                    occur++;
                tmp = tmp->prev;
            }
            if (occur == 0) {
                tmp = cur->next;
                while (tmp != NULL) {
                    if (tmp->type == XML_TEXT_NODE || tmp -> type == XML_CDATA_SECTION_NODE)
					{
                        occur++;
						break;
					}
                    tmp = tmp->next;
                }
                if (occur != 0)
                    occur = 1;
            } else
                occur++;
			if (occur == 0)
			{
				cur = next;
				continue;
			}
		} else {
            next = cur->parent;
        }

        /*
         * Make sure there is enough room
         */
        if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
            buf_len =
                2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
            temp = (xmlChar *) xmlRealloc(buffer, buf_len);
            if (temp == NULL) {
                xmlFree(buf);
                xmlFree(buffer);
                return (NULL);
            }
            buffer = temp;
            temp = (xmlChar *) xmlRealloc(buf, buf_len);
            if (temp == NULL) {
                xmlFree(buf);
                xmlFree(buffer);
                return (NULL);
            }
            buf = temp;
        }
        if (occur == 0)
            _snprintf((char *) buf, buf_len, "%c%s%s",
                     sep, name, (char *) buffer);
        else
            _snprintf((char *) buf, buf_len, "%c%s[%d]%s",
                     sep, name, occur, (char *) buffer);
        _snprintf((char *) buffer, buf_len, "%s", buf);
        cur = next;
    } while (cur != NULL);
    xmlFree(buf);
    return (buffer);
}
コード例 #23
0
/**
 * xmlXIncludeAddDoc:
 * @ctxt:  the XInclude context
 * @doc:  the new document
 * @url:  the associated URL
 *
 * Add a new document to the list. The XInclude recursive nature is handled
 * at this point.
 */
static void
xmlXIncludeAddDoc(xmlXIncludeCtxtPtr ctxt, xmlDocPtr doc, const xmlURL url) {
    xmlXIncludeCtxtPtr newctxt;
    int i;

    if (ctxt->docMax == 0) {
        ctxt->docMax = 4;
        ctxt->docTab = (xmlDocPtr *) xmlMalloc(ctxt->docMax *
                                               sizeof(ctxt->docTab[0]));
        if (ctxt->docTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
        ctxt->urlTab = (xmlURL *) xmlMalloc(ctxt->docMax *
                                            sizeof(ctxt->urlTab[0]));
        if (ctxt->urlTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            return;
        }
    }
    if (ctxt->docNr >= ctxt->docMax) {
        ctxt->docMax *= 2;
        ctxt->docTab = (xmlDocPtr *) xmlRealloc(ctxt->docTab,
                                                ctxt->docMax * sizeof(ctxt->docTab[0]));
        if (ctxt->docTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
        ctxt->urlTab = (xmlURL *) xmlRealloc(ctxt->urlTab,
                                             ctxt->docMax * sizeof(ctxt->urlTab[0]));
        if (ctxt->urlTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "realloc failed !\n");
            return;
        }
    }
    ctxt->docTab[ctxt->docNr] = doc;
    ctxt->urlTab[ctxt->docNr] = xmlStrdup(url);
    ctxt->docNr++;

    /*
     * Handle recursion here.
     */

    newctxt = xmlXIncludeNewContext(doc);
    if (newctxt != NULL) {
        /*
         * Copy the existing document set
         */
        newctxt->docMax = ctxt->docMax;
        newctxt->docNr = ctxt->docNr;
        newctxt->docTab = (xmlDocPtr *) xmlMalloc(newctxt->docMax *
                          sizeof(newctxt->docTab[0]));
        if (newctxt->docTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            xmlFree(newctxt);
            return;
        }
        newctxt->urlTab = (xmlURL *) xmlMalloc(newctxt->docMax *
                                               sizeof(newctxt->urlTab[0]));
        if (ctxt->urlTab == NULL) {
            xmlGenericError(xmlGenericErrorContext,
                            "malloc failed !\n");
            xmlFree(newctxt);
            return;
        }

        for (i = 0; i < ctxt->docNr; i++) {
            newctxt->docTab[i] = ctxt->docTab[i];
            newctxt->urlTab[i] = ctxt->urlTab[i];
        }
        xmlXIncludeDoProcess(newctxt, doc);
        for (i = 0; i < ctxt->docNr; i++) {
            newctxt->docTab[i] = NULL;
            newctxt->urlTab[i] = NULL;
        }
        xmlXIncludeFreeContext(newctxt);
    }
}
コード例 #24
0
ファイル: buf.c プロジェクト: GerHobbelt/libxml2
/**
 * xmlBufResize:
 * @buf:  the buffer to resize
 * @size:  the desired size
 *
 * Resize a buffer to accommodate minimum size of @size.
 *
 * Returns  0 in case of problems, 1 otherwise
 */
int
xmlBufResize(xmlBufPtr buf, size_t size)
{
    unsigned int newSize;
    xmlChar* rebuf = NULL;
    size_t start_buf;

    if ((buf == NULL) || (buf->error))
        return(0);
    CHECK_COMPAT(buf)

    if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);

    /* Don't resize if we don't have to */
    if (size < buf->size)
        return 1;

    /* figure out new size */
    switch (buf->alloc){
	case XML_BUFFER_ALLOC_IO:
	case XML_BUFFER_ALLOC_DOUBLEIT:
	    /*take care of empty case*/
	    newSize = (buf->size ? buf->size*2 : size + 10);
	    while (size > newSize) {
	        if (newSize > UINT_MAX / 2) {
	            xmlBufMemoryError(buf, "growing buffer");
	            return 0;
	        }
	        newSize *= 2;
	    }
	    break;
	case XML_BUFFER_ALLOC_EXACT:
	    newSize = size+10;
	    break;
        case XML_BUFFER_ALLOC_HYBRID:
            if (buf->use < BASE_BUFFER_SIZE)
                newSize = size;
            else {
                newSize = buf->size * 2;
                while (size > newSize) {
                    if (newSize > UINT_MAX / 2) {
                        xmlBufMemoryError(buf, "growing buffer");
                        return 0;
                    }
                    newSize *= 2;
                }
            }
            break;

	default:
	    newSize = size+10;
	    break;
    }

    if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) {
        start_buf = buf->content - buf->contentIO;

        if (start_buf > newSize) {
	    /* move data back to start */
	    memmove(buf->contentIO, buf->content, buf->use);
	    buf->content = buf->contentIO;
	    buf->content[buf->use] = 0;
	    buf->size += start_buf;
	} else {
	    rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize);
	    if (rebuf == NULL) {
		xmlBufMemoryError(buf, "growing buffer");
		return 0;
	    }
	    buf->contentIO = rebuf;
	    buf->content = rebuf + start_buf;
	}
    } else {
	if (buf->content == NULL) {
	    rebuf = (xmlChar *) xmlMallocAtomic(newSize);
	} else if (buf->size - buf->use < 100) {
	    rebuf = (xmlChar *) xmlRealloc(buf->content, newSize);
        } else {
	    /*
	     * if we are reallocating a buffer far from being full, it's
	     * better to make a new allocation and copy only the used range
	     * and free the old one.
	     */
	    rebuf = (xmlChar *) xmlMallocAtomic(newSize);
	    if (rebuf != NULL) {
		memcpy(rebuf, buf->content, buf->use);
		xmlFree(buf->content);
		rebuf[buf->use] = 0;
	    }
	}
	if (rebuf == NULL) {
	    xmlBufMemoryError(buf, "growing buffer");
	    return 0;
	}
	buf->content = rebuf;
    }
    buf->size = newSize;
    UPDATE_COMPAT(buf)

    return 1;
}
コード例 #25
0
static int
xmlNanoHTTPRecv(xmlNanoHTTPCtxtPtr ctxt)
{
#ifdef HAVE_POLL_H
    struct pollfd p;
#else
    fd_set rfd;
    struct timeval tv;
#endif


    while (ctxt->state & XML_NANO_HTTP_READ) {
        if (ctxt->in == NULL) {
            ctxt->in = (char *) xmlMallocAtomic(65000 * sizeof(char));
            if (ctxt->in == NULL) {
                xmlHTTPErrMemory("allocating input");
                ctxt->last = -1;
                return (-1);
            }
            ctxt->inlen = 65000;
            ctxt->inptr = ctxt->content = ctxt->inrptr = ctxt->in;
        }
        if (ctxt->inrptr > ctxt->in + XML_NANO_HTTP_CHUNK) {
            int delta = ctxt->inrptr - ctxt->in;
            int len = ctxt->inptr - ctxt->inrptr;

            memmove(ctxt->in, ctxt->inrptr, len);
            ctxt->inrptr -= delta;
            ctxt->content -= delta;
            ctxt->inptr -= delta;
        }
        if ((ctxt->in + ctxt->inlen) < (ctxt->inptr + XML_NANO_HTTP_CHUNK)) {
            int d_inptr = ctxt->inptr - ctxt->in;
            int d_content = ctxt->content - ctxt->in;
            int d_inrptr = ctxt->inrptr - ctxt->in;
            char *tmp_ptr = ctxt->in;

            ctxt->inlen *= 2;
            ctxt->in = (char *) xmlRealloc(tmp_ptr, ctxt->inlen);
            if (ctxt->in == NULL) {
                xmlHTTPErrMemory("allocating input buffer");
                xmlFree(tmp_ptr);
                ctxt->last = -1;
                return (-1);
            }
            ctxt->inptr = ctxt->in + d_inptr;
            ctxt->content = ctxt->in + d_content;
            ctxt->inrptr = ctxt->in + d_inrptr;
        }
        ctxt->last = recv(ctxt->fd, ctxt->inptr, XML_NANO_HTTP_CHUNK, 0);
        if (ctxt->last > 0) {
            ctxt->inptr += ctxt->last;
            return (ctxt->last);
        }
        if (ctxt->last == 0) {
            return (0);
        }
        if (ctxt->last == -1) {
            switch (socket_errno()) {
                case EINPROGRESS:
                case EWOULDBLOCK:
#if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
                case EAGAIN:
#endif
                    break;

                case ECONNRESET:
                case ESHUTDOWN:
                    return (0);

                default:
                    __xmlIOErr(XML_FROM_HTTP, 0, "recv failed\n");
                    return (-1);
            }
        }
#ifdef HAVE_POLL_H
        p.fd = ctxt->fd;
        p.events = POLLIN;
        if ((poll(&p, 1, timeout * 1000) < 1)
#if defined(EINTR)
            && (errno != EINTR)
#endif
            )
            return (0);
#else /* !HAVE_POLL_H */
#ifndef _WINSOCKAPI_
        if (ctxt->fd > FD_SETSIZE)
            return 0;
#endif

        tv.tv_sec = timeout;
        tv.tv_usec = 0;
        FD_ZERO(&rfd);

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4018)
#endif

        FD_SET(ctxt->fd, &rfd);

#ifdef _MSC_VER
#pragma warning(pop)
#endif

        if ((select(ctxt->fd + 1, &rfd, NULL, NULL, &tv) < 1)
#if defined(EINTR)
            && (errno != EINTR)
#endif
            )
            return (0);
#endif /* !HAVE_POLL_H */
    }
    return (0);
}
コード例 #26
0
ファイル: xml_edit.c プロジェクト: cczurda/xmlstar
/**
 *  This is the main function for 'edit' option
 */
int
edMain(int argc, char **argv)
{
    int i, ops_count, max_ops_count = 8, n, start = 0;
    XmlEdAction* ops = xmlMalloc(sizeof(XmlEdAction) * max_ops_count);
    static edOptions g_ops;
    int nCount = 0;

    if (argc < 3) edUsage(argv[0], EXIT_BAD_ARGS);

    edInitOptions(&g_ops);
    start = edParseOptions(&g_ops, argc, argv);

    parseNSArr(ns_arr, &nCount, argc-start, argv+start);
        
    /*
     *  Parse command line and fill array of operations
     */
    ops_count = 0;
    i = start + nCount;

    while (i < argc)
    {
        const char *arg = nextArg(argv, &i);
        if (arg[0] == '-')
        {
            if (ops_count >= max_ops_count)
            {
                max_ops_count *= 2;
                ops = xmlRealloc(ops, sizeof(XmlEdAction) * max_ops_count);
            }
            ops[ops_count].type = XML_UNDEFINED;

            if (!strcmp(arg, "-d") || !strcmp(arg, "--delete"))
            {
                ops[ops_count].op = XML_ED_DELETE;
                ops[ops_count].arg1 = nextArg(argv, &i);
                ops[ops_count].arg2 = 0;
            }
            else if (!strcmp(arg, "--var"))
            {
                ops[ops_count].op = XML_ED_VAR;
                ops[ops_count].arg1 = nextArg(argv, &i);
                ops[ops_count].arg2 = nextArg(argv, &i);
            }
            else if (!strcmp(arg, "-m") || !strcmp(arg, "--move"))
            {
                ops[ops_count].op = XML_ED_MOVE;
                ops[ops_count].arg1 = nextArg(argv, &i);
                ops[ops_count].arg2 = nextArg(argv, &i);
            }
            else if (!strcmp(arg, "-u") || !strcmp(arg, "--update"))
            {
                ops[ops_count].op = XML_ED_UPDATE;
                ops[ops_count].arg1 = nextArg(argv, &i);
                ops[ops_count].type = parseNextArg(argv, &i, OPT_VAL_OR_EXP);
                ops[ops_count].arg2 = nextArg(argv, &i);
            }
            else if (!strcmp(arg, "-r") || !strcmp(arg, "--rename"))
            {
                ops[ops_count].op = XML_ED_RENAME;
                ops[ops_count].arg1 = nextArg(argv, &i);
                ops[ops_count].type = parseNextArg(argv, &i, OPT_JUST_VAL);
                ops[ops_count].arg2 = nextArg(argv, &i);
            }
            else if (!strcmp(arg, "-i") || !strcmp(arg, "--insert"))
            {
                parseInsertionArgs(XML_ED_INSERT, &ops[ops_count], argv, &i);
            }
            else if (!strcmp(arg, "-a") || !strcmp(arg, "--append"))
            {
                parseInsertionArgs(XML_ED_APPEND, &ops[ops_count], argv, &i);
            }
            else if (!strcmp(arg, "-s") || !strcmp(arg, "--subnode"))
            {
                parseInsertionArgs(XML_ED_SUBNODE, &ops[ops_count], argv, &i);
            }
            else
            {
                fprintf(stderr, "Warning: unrecognized option '%s'\n", arg);
            }
            ops_count++;
        }
        else
        {
            i--;                /* it was a filename, we didn't use it */
            break;
        }
    }

    xmlKeepBlanksDefault(0);

    if ((!g_ops.noblanks) || g_ops.preserveFormat) xmlKeepBlanksDefault(1);

    if (i >= argc)
    {
        edOutput("-", ops, ops_count, &g_ops);
    }
    
    for (n=i; n<argc; n++)
    {
        edOutput(argv[n], ops, ops_count, &g_ops);
    }

    xmlFree(ops);
    cleanupNSArr(ns_arr);
    xmlCleanupParser();
    xmlCleanupGlobals();
    return 0;
}