mirror of
https://github.com/brunoherbelin/vimix.git
synced 2025-12-11 18:34:58 +01:00
Cleanup and securing XMLElementEncode and DecodeArray
This commit is contained in:
@@ -118,7 +118,7 @@ void tinyxml2::XMLElementToGLM(XMLElement *elem, glm::mat4 &matrix)
|
||||
}
|
||||
|
||||
|
||||
XMLElement *tinyxml2::XMLElementEncodeArray(XMLDocument *doc, void *array, unsigned int arraysize)
|
||||
XMLElement *tinyxml2::XMLElementEncodeArray(XMLDocument *doc, const void *array, uint arraysize)
|
||||
{
|
||||
// create <array> node
|
||||
XMLElement *newelement = doc->NewElement( "array" );
|
||||
@@ -157,63 +157,68 @@ XMLElement *tinyxml2::XMLElementEncodeArray(XMLDocument *doc, void *array, unsig
|
||||
return newelement;
|
||||
}
|
||||
|
||||
bool tinyxml2::XMLElementDecodeArray(XMLElement *elem, void *array, unsigned int arraysize)
|
||||
bool tinyxml2::XMLElementDecodeArray(XMLElement *elem, void *array, uint arraysize)
|
||||
{
|
||||
bool ret = false;
|
||||
|
||||
// sanity check
|
||||
if (array==nullptr || arraysize==0)
|
||||
return ret;
|
||||
|
||||
// make sure we have the good type of XML node
|
||||
if ( !elem || std::string(elem->Name()).compare("array") != 0 )
|
||||
return ret;
|
||||
|
||||
// make sure the stored array is of the requested array size
|
||||
unsigned int len = 0;
|
||||
uint len = 0;
|
||||
elem->QueryUnsignedAttribute("len", &len);
|
||||
if ( arraysize != len )
|
||||
return ret;
|
||||
if (len == arraysize)
|
||||
{
|
||||
// read and decode the text field in <array>
|
||||
gsize decoded_size = 0;
|
||||
guchar *decoded_array = g_base64_decode(elem->GetText(), &decoded_size);
|
||||
|
||||
// read and decode the text field in <array>
|
||||
gsize decoded_size = 0;
|
||||
guchar *decoded_array = g_base64_decode(elem->GetText(), &decoded_size);
|
||||
// if data is z-compressed (zbytes size is indicated)
|
||||
uint zbytes = 0;
|
||||
elem->QueryUnsignedAttribute("zbytes", &zbytes);
|
||||
if ( zbytes > 0) {
|
||||
// sanity check 1: decoded data size must match the buffer size
|
||||
if ( decoded_array && zbytes == (uint) decoded_size ) {
|
||||
|
||||
// if data is z-compressed (zbytes size is indicated)
|
||||
uint zbytes = 0;
|
||||
elem->QueryUnsignedAttribute("zbytes", &zbytes);
|
||||
if ( zbytes > 0) {
|
||||
// sanity check 1: decoded data size must match the buffer size
|
||||
if ( decoded_array && zbytes == (uint) decoded_size ) {
|
||||
// allocate a temporary array for decompressing data
|
||||
uLong uncompressed_size = len;
|
||||
gchar *uncompressed_array = g_new(gchar, uncompressed_size);
|
||||
|
||||
// allocate a temporary array for decompressing data
|
||||
uLong uncompressed_size = arraysize;
|
||||
gchar *uncompressed_array = g_new(gchar, uncompressed_size);
|
||||
// zlib uncompress ((Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen));
|
||||
uncompress((Bytef *)uncompressed_array, &uncompressed_size,
|
||||
(Bytef *)decoded_array, (uLong) zbytes) ;
|
||||
|
||||
// zlib uncompress ((Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen));
|
||||
uncompress((Bytef *)uncompressed_array, &uncompressed_size,
|
||||
(Bytef *)decoded_array, (uLong) zbytes) ;
|
||||
|
||||
// sanity check 2: decompressed data size must match array size
|
||||
if ( uncompressed_array && arraysize == uncompressed_size ){
|
||||
// sanity check 2: decompressed data size must match array size
|
||||
if ( uncompressed_array && len == uncompressed_size ){
|
||||
// copy to target array
|
||||
memcpy(array, uncompressed_array, len);
|
||||
// success
|
||||
ret = true;
|
||||
}
|
||||
|
||||
// free temp decompression buffer
|
||||
g_free(uncompressed_array);
|
||||
}
|
||||
}
|
||||
// data is not z-compressed
|
||||
else {
|
||||
// copy the decoded data
|
||||
if ( decoded_array && len == decoded_size ) {
|
||||
// copy to target array
|
||||
memcpy(array, uncompressed_array, arraysize);
|
||||
memcpy(array, decoded_array, len);
|
||||
// success
|
||||
ret = true;
|
||||
}
|
||||
|
||||
// free temp decompression buffer
|
||||
g_free(uncompressed_array);
|
||||
}
|
||||
}
|
||||
// data is not z-compressed
|
||||
else {
|
||||
// copy the decoded data
|
||||
if ( decoded_array && arraysize == decoded_size )
|
||||
memcpy(array, decoded_array, arraysize);
|
||||
// success
|
||||
ret = true;
|
||||
}
|
||||
|
||||
// free temporary array
|
||||
g_free(decoded_array);
|
||||
// free temporary array
|
||||
g_free(decoded_array);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user