summaryrefslogtreecommitdiffstats
path: root/src/xplist.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2018-12-17 01:50:11 +0100
committerGravatar Nikias Bassen2018-12-17 01:50:11 +0100
commit92e5c858c246f3a01104f511c23217fd75c272f4 (patch)
treeca8463112104d2a91fcff2dfebbf3b3bebbdf080 /src/xplist.c
parent9c8d7a6c44b0eb22ac77ca6ca5c7c29edd5f685e (diff)
downloadlibplist-92e5c858c246f3a01104f511c23217fd75c272f4.tar.gz
libplist-92e5c858c246f3a01104f511c23217fd75c272f4.tar.bz2
xplist: Write base64 directly to output buffer to improve memory usage
Now that we grow the output buffer enough before writing XML output we can just write the base64 encoded data directly to the ouput buffer instead of using a heap buffer that will then be copied to the output buffer. This makes writing XML output more memory efficient (and slightly faster).
Diffstat (limited to 'src/xplist.c')
-rw-r--r--src/xplist.c7
1 files changed, 2 insertions, 5 deletions
diff --git a/src/xplist.c b/src/xplist.c
index 2eeccf9..c89b143 100644
--- a/src/xplist.c
+++ b/src/xplist.c
@@ -290,12 +290,10 @@ static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
290 tagOpen = TRUE; 290 tagOpen = TRUE;
291 str_buf_append(*outbuf, "\n", 1); 291 str_buf_append(*outbuf, "\n", 1);
292 if (node_data->length > 0) { 292 if (node_data->length > 0) {
293 char *buf = malloc(80);
294 uint32_t j = 0; 293 uint32_t j = 0;
295 uint32_t indent = (depth > 8) ? 8 : depth; 294 uint32_t indent = (depth > 8) ? 8 : depth;
296 uint32_t maxread = MAX_DATA_BYTES_PER_LINE(indent); 295 uint32_t maxread = MAX_DATA_BYTES_PER_LINE(indent);
297 size_t count = 0; 296 size_t count = 0;
298 size_t b64count = 0;
299 size_t amount = (node_data->length / 3 * 4) + 4 + (((node_data->length / maxread) + 1) * (indent+1)); 297 size_t amount = (node_data->length / 3 * 4) + 4 + (((node_data->length / maxread) + 1) * (indent+1));
300 if ((*outbuf)->len + amount > (*outbuf)->capacity) { 298 if ((*outbuf)->len + amount > (*outbuf)->capacity) {
301 str_buf_grow(*outbuf, amount); 299 str_buf_grow(*outbuf, amount);
@@ -305,12 +303,11 @@ static void node_to_xml(node_t* node, bytearray_t **outbuf, uint32_t depth)
305 str_buf_append(*outbuf, "\t", 1); 303 str_buf_append(*outbuf, "\t", 1);
306 } 304 }
307 count = (node_data->length-j < maxread) ? node_data->length-j : maxread; 305 count = (node_data->length-j < maxread) ? node_data->length-j : maxread;
308 b64count = base64encode(buf, node_data->buff + j, count); 306 assert((*outbuf)->len + count < (*outbuf)->capacity);
309 str_buf_append(*outbuf, buf, b64count); 307 (*outbuf)->len += base64encode((char*)(*outbuf)->data + (*outbuf)->len, node_data->buff + j, count);
310 str_buf_append(*outbuf, "\n", 1); 308 str_buf_append(*outbuf, "\n", 1);
311 j+=count; 309 j+=count;
312 } 310 }
313 free(buf);
314 } 311 }
315 for (i = 0; i < depth; i++) { 312 for (i = 0; i < depth; i++) {
316 str_buf_append(*outbuf, "\t", 1); 313 str_buf_append(*outbuf, "\t", 1);