You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by k b <k_...@hotmail.com> on 2007/08/28 23:55:43 UTC

DOMDocument

Hi,
I have a fairly simple application using xerces-c 
here's what i'm doing .

int main(void)
{
    DOMImplementation *impl = NULL;


    // init the DOMImplementation

    initXML(impl);

    for(int i = 0; i < 20; i++)
    {
        run(impl);
    }
}

void run(DOMImplementation *impl) 
{
    DOMDocument *doc = NULL;

    DOMWriter *serializer = NULL;
    MemBufFormatTarget target;


    if (impl) {

        serializer = ((DOMImplementationLS*)impl)->createDOMWriter();

        serializer->setNewLine(xstr("\n"));
        serializer->setFeature(XMLUni::fgDOMXMLDeclaration, false);


        if (serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, false)) {
            serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
        }

        doc =
            impl->createDocument(XercesString("http://localhost/schemas/music-title"),
                                XercesString("ml:MusicList"), NULL);

        doc->setEncoding(XercesString("UTF-8"));
        doc->setStandalone(true);
        doc->setVersion(XercesString("1.0"));

        DOMElement *root = doc->getDocumentElement();
        root->setAttribute(xstr("xmlns:xs"), xstr("http://www.w3.org/2001/XMLSchema"));

        GenerateXML(*doc, *root, *serializer);
        doc->normalizeDocument();
         sign(doc);
        serializer->writeNode(target, *doc);

        cout << "length :" << target->getLen() << endl;
        cout << target->getRawBuffer() << endl;

        doc->release();
        serializer->release();
    }
}

void initXML(DOMImplementation*& pImpl)
{
    //init xml lib
    XMLPlatformUtils::Initialize();
    OpenSSLCryptoProvider *cp = new OpenSSLCryptoProvider();
    XSECPlatformUtils::SetCryptoProvider(cp);

    pImpl = DOMImplementationRegistry::getDOMImplementation(xstr("LS"));
}

void sign(DOMDocument *pDoc) 
{    
    OpenSSLCryptoX509 *certs = NULL;
    XSECCryptoKey     *key = NULL;
    DSIGKeyInfoX509   *keyInfoX509 = NULL;

    XSECProvider  prov;
    DSIGSignature *sig     = NULL;
    DOMElement    *sigNode = NULL;
    DSIGReference *ref     = NULL; 
 
    
    X509_NAME *issuerX509Name = NULL;
    X509_NAME *subjectX509Name = NULL; 

    /* for reading the showplayer cert and the private key */
    X509     *x       = NULL;
    EVP_PKEY *pkey    = NULL;
 
    // create a empty signature 
    sig = prov.newSignature();
    sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
    sig->setPrettyPrint(true);

    // create a empty signature DOM element
    sigNode = sig->createBlankSignature(pDoc, CANON_C14N_COM,
                                        SIGNATURE_RSA, HASH_SHA1);
    // attach it to the parent. 
    pDoc->getDocumentElement()->appendChild(sigNode);

    // Create an envelope reference for the text to be signed
    ref = sig->createReference(MAKE_UNICODE_STRING(""));
    ref->appendEnvelopedSignatureTransform();

    // Load the signing key
    getSPCert(&x); // implementated elsewhere
    getPrivKey(&pkey); // implemented elsewhere 
    
    if(x == NULL) goto end;
    if(pkey == NULL) goto end;
    
    // Create the XSEC OpenSSL interface
    key = new OpenSSLCryptoKeyRSA(pkey);

    // Create the XSEC OpenSSL interface - used only to translate to Base64
    certs = new OpenSSLCryptoX509(x);

    // here where it's done, sign the doc.
    printf("sign: about to sign\r\n");
    sig->setSigningKey(key);
    sig->sign();
    printf("sign: done sign\r\n");

    // add the key info to the xml.
    keyInfoX509 = sig->appendX509Data();
    keyInfoX509->appendX509Certificate(certs->getDEREncodingSB().sbStrToXMLCh());
    printf("sign: done adding key info\r\n");

    // do clean up here 
    end:

    if (certs != NULL) { 
        printf("sign: freeing cert"); 
        delete(certs); 
        printf("...done\r\n");
    }
    if (x!= NULL) { 
        printf("sign: freeing x509"); 
        X509_free(x); 
        printf("...done\r\n");
    }
    if (issuerX509Name != NULL)
    {
        printf("sign: freeing issuerX509Name");        
        X509_NAME_free(issuerX509Name);
        issuerX509Name = NULL;
        printf("...done\r\n");        
    }
    if (subjectX509Name != NULL)
    {
        printf("sign: freeing subjectX509Name");
        X509_NAME_free(subjectX509Name);
        subjectX509Name = NULL;
        printf("...done\r\n");
    }
    if (sig != NULL) { 
        printf("sign: freeing signature"); 
        prov.releaseSignature(sig); 
        printf("...done\r\n");
    }
    if (pkey != NULL) {
        printf("sign: freeing pkey"); 
        EVP_PKEY_free(pkey); 
        printf("...done\r\n");
    }    
    // key is freed by the signature.
    printf("sign: done");
}



After calling doc->release & serializer->release() do i need to call delete on them. 
I say this because i don't see consideriable drop in mem usage after calling the release. 
if i call the run as i do in main i loop i start getting malloc errors, essentially getting out of memory.

do i need to call terminate each time which i think isn't correct .
Anyways  heres my question 
1) do i need to call delete on doc and serializer
2) what else can i do or i'm not doing that would reduce the mem usage.

thanks and hoping to get some lead.

-Bisla

_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline