Archived Forum Post

Index of archived forum posts

Question:

Cannot load XML from CkByteData

Mar 19 '15 at 13:49

I am trying to load a CkXml object from a CkByteData object. However, the only interfaces available for loading from memory are LoadXml and LoadXml2. Both of those only take a char * rather than a CkByteData.

I tried loading with a call to LoadXml(data.getBytes()) as well as LoadXml(data.getData()) and was getting erratic behavior with both. I found out it is because the underlying byte array in CkByteData is not NULL-terminated! There is no way to tell LoadXml how many bytes to read, and there is no way to pass a CkByteData object, and there is no way to even set a NULL terminator on a CkByteData!

The only way I got around this was with a little ugly hack:

CkByteData data;
// load some data (in my case, from a CkZipEntry)

// here is the hack to set a NULL term.  getData() returns a const char *
// that must be cast to a char * so we can modify it.
((char*)data.getData())[data.getSize() - 1] = 0;

CkXml xml;
xml.LoadXml(data.getData());

Even with this hack, I had to be sure that there was enough room to hold the NULL term, so I had to pre-allocate my CkByteData with xml.get_UncompressedLength() + 1.

Is there any other way to get around this? If not, please consider adding a LoadXml interface that accepts a CkByteData object.


Answer

Append a null byte to the CkByteData:

data.appendChar(0);

CkXml xml;
xml.LoadXml((const char *)data.getData());