blob: 110b49666607d18c0d35e8b7ac6d1e288b326ce5 [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.
21#if 0 // TODO: implement saving data parts.
22 this.dataParts.push(data.subarray(0, this.structureDecoder.offset));
23 var element = DataUtils.concatArrays(this.dataParts);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070024#endif
Jeff Thompson5e275b42013-07-16 19:10:11 -070025 if (self->usePartialData) {
26 // We have partial data from a previous call, so append this data and point to partialData.
Jeff Thompson94ddc272013-08-08 14:17:38 -070027 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070028 return error;
29 self->partialDataLength += dataLength;
30
31 (*self->elementListener->onReceivedElement)(self->elementListener, self->partialData.array, self->partialDataLength);
32 // Assume we don't need to use partialData anymore until needed.
33 self->usePartialData = 0;
34 }
35 else
36 // We are not using partialData, so just point to the input data buffer.
37 (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070038
39 // Need to read a new object.
40 data += self->structureDecoder.offset;
41 dataLength -= self->structureDecoder.offset;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042 ndn_BinaryXmlStructureDecoder_init(&self->structureDecoder);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070043 if (dataLength == 0)
44 // No more data in the packet.
45 return 0;
46
47 // else loop back to decode.
48 }
49 else {
Jeff Thompson5e275b42013-07-16 19:10:11 -070050 // Save remaining data for a later call.
51 if (!self->usePartialData) {
52 self->usePartialData = 1;
53 self->partialDataLength = 0;
54 }
55
Jeff Thompson94ddc272013-08-08 14:17:38 -070056 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070057 return error;
58 self->partialDataLength += dataLength;
59
Jeff Thompsonb42e3632013-07-15 16:51:42 -070060 return 0;
61 }
62 }
63}