blob: 4bbd081c3af6475cf1a8d78e13bc3f7466cf55da [file] [log] [blame]
/**
* @author: Jeff Thompson
* See COPYING for copyright and distribution information.
*/
#include "binary-xml-element-reader.h"
ndn_Error ndn_BinaryXmlElementReader_onReceivedData
(struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength)
{
// Process multiple objects in the data.
while(1) {
// Scan the input to check if a whole binary XML object has been read.
ndn_BinaryXmlStructureDecoder_seek(&self->structureDecoder, 0);
ndn_Error error;
if (error = ndn_BinaryXmlStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength))
return error;
if (self->structureDecoder.gotElementEnd) {
// Got the remainder of an element. Report to the caller.
#if 0 // TODO: implement saving data parts.
this.dataParts.push(data.subarray(0, this.structureDecoder.offset));
var element = DataUtils.concatArrays(this.dataParts);
#endif
if (self->usePartialData) {
// We have partial data from a previous call, so append this data and point to partialData.
if (error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength))
return error;
self->partialDataLength += dataLength;
(*self->elementListener->onReceivedElement)(self->elementListener, self->partialData.array, self->partialDataLength);
// Assume we don't need to use partialData anymore until needed.
self->usePartialData = 0;
}
else
// We are not using partialData, so just point to the input data buffer.
(*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
// Need to read a new object.
data += self->structureDecoder.offset;
dataLength -= self->structureDecoder.offset;
ndn_BinaryXmlStructureDecoder_init(&self->structureDecoder);
if (dataLength == 0)
// No more data in the packet.
return 0;
// else loop back to decode.
}
else {
// Save remaining data for a later call.
if (!self->usePartialData) {
self->usePartialData = 1;
self->partialDataLength = 0;
}
if (error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength))
return error;
self->partialDataLength += dataLength;
return 0;
}
}
}