blob: b7c062e1f91a71713398fc366725e69c30971cf3 [file] [log] [blame]
Jeff Thompsonb42e3632013-07-15 16:51:42 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
4 */
5
Jeff Thompson53412192013-08-06 13:35:50 -07006#include "binary-xml-element-reader.h"
Jeff Thompsonb42e3632013-07-15 16:51:42 -07007
Jeff Thompsonf0fea002013-07-30 17:22:42 -07008ndn_Error ndn_BinaryXmlElementReader_onReceivedData
9 (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength)
Jeff Thompsonb42e3632013-07-15 16:51:42 -070010{
11 // Process multiple objects in the data.
12 while(1) {
13 // Scan the input to check if a whole binary XML object has been read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070014 ndn_BinaryXmlStructureDecoder_seek(&self->structureDecoder, 0);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070015
16 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070017 if ((error = ndn_BinaryXmlStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength)))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070018 return error;
19 if (self->structureDecoder.gotElementEnd) {
20 // Got the remainder of an element. Report to the caller.
Jeff Thompson5e275b42013-07-16 19:10:11 -070021 if (self->usePartialData) {
22 // We have partial data from a previous call, so append this data and point to partialData.
Jeff Thompson94ddc272013-08-08 14:17:38 -070023 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070024 return error;
25 self->partialDataLength += dataLength;
26
27 (*self->elementListener->onReceivedElement)(self->elementListener, self->partialData.array, self->partialDataLength);
28 // Assume we don't need to use partialData anymore until needed.
29 self->usePartialData = 0;
30 }
31 else
32 // We are not using partialData, so just point to the input data buffer.
33 (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070034
35 // Need to read a new object.
36 data += self->structureDecoder.offset;
37 dataLength -= self->structureDecoder.offset;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070038 ndn_BinaryXmlStructureDecoder_init(&self->structureDecoder);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070039 if (dataLength == 0)
40 // No more data in the packet.
Jeff Thompsonadaf9232013-08-08 14:30:29 -070041 return NDN_ERROR_success;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070042
43 // else loop back to decode.
44 }
45 else {
Jeff Thompson5e275b42013-07-16 19:10:11 -070046 // Save remaining data for a later call.
47 if (!self->usePartialData) {
48 self->usePartialData = 1;
49 self->partialDataLength = 0;
50 }
51
Jeff Thompson94ddc272013-08-08 14:17:38 -070052 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070053 return error;
54 self->partialDataLength += dataLength;
55
Jeff Thompsonadaf9232013-08-08 14:30:29 -070056 return NDN_ERROR_success;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070057 }
58 }
59}