Ok,
I delete may errors and now I am getting an error using libxslt.
If I parse a doc and dump it there is no problem, but if I try to apply 
a stylesheet I get a segmentation fault.
If you compile the code with -DDUMP there is no problem, while if you 
don't put it you get a segmentation.
This is the code:

#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/encoding.h>

#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>

#include <stdlib.h>

const unsigned int mgc = 0x88DEFEA7;

static void *xml2_alloc(size_t sz)
{
    const unsigned int *ss;
    const unsigned int *mark;
    void *mem = malloc(sz + 2 * sizeof(int));
   
    if (mem == NULL)
        return NULL;
       
    ss = mem;
    mark = mem + sizeof(int);
    *ss = sz;
    *mark = mgc;
    return (char *) mem + 2 * sizeof(int);
}
   
void xml2_free(void *emem)
{
    void *mem;
    const unsigned int *mark;
   
    if (emem == NULL)
        return;
       
    mem = (char *)emem - 2 * sizeof(int);
    mark = mem + sizeof(int);
    if (*mark == mgc) {
        free(mem);
    }
}

static void *xml2_realloc(void * emem, size_t sz)
{
    void *mem;
    const unsigned int *s;
    void *mem_re;
   
    if (emem == NULL)
        return xml2_alloc(sz);
    else if (sz == 0)
        xml2_free(emem);
   
    mem = (char *)emem - 2 * sizeof(int);
    s = mem;
    mem_re = xml2_alloc(sz);
   
    memcpy(mem_re, emem, *s);
   
    xml2_free(emem);
   
    return mem_re;
}

static char *xml2_strdup(const char * str)
{
    char *s = xml2_alloc(strlen(str) + 1);
    if (s == NULL)
        return NULL;
   
    strcpy(s, str);
   
    return s;
}

xmlDocPtr
parseDoc(char *xmlfile)
{
    xmlDocPtr doc;
    xmlNodePtr root;

    doc = xmlParseFile(xmlfile);

    if (doc == NULL ) {
        fprintf(stderr,"Document not parsed successfully. \n");
        xmlFreeDoc(doc);
        return (NULL);
    }

    root = xmlDocGetRootElement(doc);

    if (root == NULL) {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return (NULL);
    }

    return doc;

}

int
main(int argc, char **argv) {
    xmlDocPtr doc;
    xsltStylesheetPtr cur = NULL;
    xmlDocPtr res;

    xmlChar *bufptr;
    int size = 0;
    char *xmlfile = "prova.xml";
    char * xslfilename    = "xxx.xsl";
   
    xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xml2_strdup);
    xmlInitParser();
   
    doc = parseDoc (xmlfile);
   
    if (doc == NULL) {
        fprintf(stderr,"[ERRORE] ParseDoc ha ritornato NULL\n");
        return -1;
    }
   
    #ifdef DUMP
    xmlDocDumpMemoryEnc(doc, &bufptr, &size, "UTF-8");
      fprintf(stderr, "Out = %s\n", bufptr);
      xmlFreeDoc(doc);
      xmlFree(bufptr);
      xmlCleanupParser();
      return 1;
    #endif
   
    xmlSubstituteEntitiesDefault(1);

    exsltRegisterAll();
   
    cur = xsltParseStylesheetFile((const xmlChar *)xslfilename);
   
    res = xsltApplyStylesheet(cur, doc, NULL);
   
    if (xsltSaveResultToString(&bufptr, &size, res, cur) == -1) {
        fprintf(stderr, "Errore nella funzione xsltSaveResultToString\n");
        xmlFree(bufptr);
        xsltFreeStylesheet(cur);
        xmlFreeDoc(res);

        xmlFreeDoc(doc);

        xsltCleanupGlobals();
        xmlCleanupParser();

        return -1;
    }
   
    fprintf(stderr, "Out = %s\n", bufptr);
   
    xmlFree(bufptr);

    xsltFreeStylesheet(cur);
    xmlFreeDoc(res);

    xmlFreeDoc(doc);
   
    return 0;
}

Is tehre some problems if you use xmlMemSetup and libxslt?
Best regards

Marco


William M. Brack ha scritto:
> Your two errors are caused because you are attempting to use the xml2
> library functions xmlMemRealloc and xmlMemoryStrdup within your
> "personal" routines.  This kind of "mixing" may not be done.  Within your
> function "xml2_realloc" you should only call upon realloc, and you should
> provide "your own" version of strdup (probably calling xml2_alloc and
> memcpy).
>
> Bill
>
> Marco Spinetti wrote:
>   
>> I tried to use xmlMemSetup and libxml2 gives me an error.
>> Probably I'm missing something.
>> This is a simple main I wrote:
>>
>> #include <libxml/parser.h>
>> #include <libxml/xmlmemory.h>
>> #include <libxml/encoding.h>
>> #include <stdlib.h>
>>
>> const unsigned int mgc = 0x88DEFEA7;
>>
>> static void *xml2_alloc(size_t sz)
>> {
>>    void *mem = malloc(sz + sizeof(int));
>>    const unsigned int *mark = mem;
>>    *mark = mgc;
>>    return (char *) mem + sizeof(int);
>> }
>>   static void xml2_free(void *emem)
>> {
>>    void *mem = (char *)emem - sizeof(int);
>>    const unsigned int *mark = mem;
>>    if (*mark == mgc) {
>>        free(mem);
>>    }
>> }
>>
>> static void *xml2_realloc(void * emem, size_t sz)
>> {
>>    void *mem = (char *)emem - sizeof(int);
>>    void *mem_re = xmlMemRealloc(mem, sz);
>>    if (mem_re != NULL)
>>        return (char *) mem_re + sizeof(int);
>>    else
>>        return NULL;
>> }
>>
>> xmlDocPtr
>> parseDoc(char *xmlfile)
>> {
>>    xmlDocPtr doc;
>>    xmlNodePtr root;
>>
>>    doc = xmlParseFile(xmlfile);
>>
>>    if (doc == NULL ) {
>>        fprintf(stderr,"Document not parsed successfully. \n");
>>        xmlFreeDoc(doc);
>>        return (NULL);
>>    }
>>
>>    root = xmlDocGetRootElement(doc);
>>
>>    if (root == NULL) {
>>        fprintf(stderr,"empty document\n");
>>        xmlFreeDoc(doc);
>>        return (NULL);
>>    }
>>
>>    return doc;
>>
>> }
>>
>> int
>> main(int argc, char **argv) {
>>    xmlDocPtr doc;
>>    xmlChar *bufptr;
>>    int size = 0;
>>    char *xmlfile = "/usr/local/apache/conf/conf_prova.xml";
>>      xmlMemSetup(xml2_free, xml2_alloc, xml2_realloc, xmlMemoryStrdup);
>>    xmlInitParser();
>>      doc = parseDoc (xmlfile);
>>      if (doc == NULL) {
>>        fprintf(stderr,"[ERRORE] ParseDoc ha ritornato NULL\n");
>>        return -1;
>>    }
>>      fprintf(stderr,"[OK] ParseDoc Ok\n");
>>      xmlFreeDoc(doc);
>>    xmlCleanupParser();
>>
>>    return 0;
>> }
>>
>> The xml document is very simple:
>>
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>> <CONFIGURAZIONE>
>>        <Q>3</Q>
>>        <S>
>>                <H>e</H>
>>                <P>80</P>
>>                <L>p.l</L>
>>                <PAR>
>>                        <AD>on</AD>
>>                        <S>off</S>
>>                        <C>r</C>
>>                        <N>0</N>
>>                        <W>1</W>
>>                </PAR>
>>        </S>
>>        .....
>> </CONFIGURAZIONE>
>>
>> this is the error I get:
>>
>>
>> [EMAIL PROTECTED] test]$ ./main
>> xmlMallocBreakpoint reached on block 0
>> Memory tag error occurs :0x805c140
>>         bye
>> Memory tag error occurs :0x805c158
>>         bye
>> /usr/local/apache/conf/conf_prova.xml:1: parser error : Memory
>> allocation failed
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>>                                       ^
>> Document not parsed successfully.
>> [ERRORE] ParseDoc ha ritornato NULL
>>
>> What am I missing?
>>
>> Marco
>>
>>
>>
>> Daniel Veillard ha scritto:
>>     
>>> On Tue, Aug 07, 2007 at 12:09:05PM +0200, Marco Spinetti wrote:
>>>
>>>       
>>>> Reading the docs, I don't understand the difference between
>>>> xmlMemSetup
>>>> and xmlGcMemSetup.
>>>> I need to overwrite libxml2 memory allocation and I don't understand
>>>> if
>>>> I have to use xmlGcMemSetup or xmlMemSetup.
>>>> Any hints?
>>>>
>>>>         
>>>   http://xmlsoft.org/html/libxml-xmlmemory.html#xmlGcMemSetup
>>> the later has an extra function allowing a special function for
>>> allocation of atomic area (i.e. areas which will never be grown).
>>> In doubt use xmlMemSetup()
>>>
>>>
>>>       
>>>> Other question: is xmlInitMemory(9 should be called before use
>>>> xmlGcMemSetup or xmlMemSetup?
>>>>
>>>>         
>>>   no, xmlMemSetup must be the very first call to libxml2 then call
>>> xmlInitParser(). Don't call xmlInitMemory by yourself, xmlInitParser
>>> will do it.
>>>
>>> Daniel
>>>
>>>
>>>       
>> _______________________________________________
>> xml mailing list, project page  http://xmlsoft.org/
>> [email protected]
>> http://mail.gnome.org/mailman/listinfo/xml
>>
>>     
>
>
>
>
>   

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to