blob: 837363c268e672eb250d9f17fadf6e59c822afe8 [file] [log] [blame]
Jeff Thompsonb42e3632013-07-15 16:51:42 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb42e3632013-07-15 16:51:42 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml-element-reader.h"
Jeff Thompsonb42e3632013-07-15 16:51:42 -07008
Jeff Thompsonf0fea002013-07-30 17:22:42 -07009ndn_Error ndn_BinaryXmlElementReader_onReceivedData
10 (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength)
Jeff Thompsonb42e3632013-07-15 16:51:42 -070011{
12 // Process multiple objects in the data.
13 while(1) {
14 // Scan the input to check if a whole binary XML object has been read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070015 ndn_BinaryXmlStructureDecoder_seek(&self->structureDecoder, 0);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070016
17 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070018 if ((error = ndn_BinaryXmlStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength)))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070019 return error;
20 if (self->structureDecoder.gotElementEnd) {
21 // Got the remainder of an element. Report to the caller.
Jeff Thompson5e275b42013-07-16 19:10:11 -070022 if (self->usePartialData) {
23 // We have partial data from a previous call, so append this data and point to partialData.
Jeff Thompson94ddc272013-08-08 14:17:38 -070024 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070025 return error;
26 self->partialDataLength += dataLength;
27
28 (*self->elementListener->onReceivedElement)(self->elementListener, self->partialData.array, self->partialDataLength);
29 // Assume we don't need to use partialData anymore until needed.
30 self->usePartialData = 0;
31 }
32 else
33 // We are not using partialData, so just point to the input data buffer.
34 (*self->elementListener->onReceivedElement)(self->elementListener, data, self->structureDecoder.offset);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070035
36 // Need to read a new object.
37 data += self->structureDecoder.offset;
38 dataLength -= self->structureDecoder.offset;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070039 ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070040 if (dataLength == 0)
41 // No more data in the packet.
Jeff Thompsonadaf9232013-08-08 14:30:29 -070042 return NDN_ERROR_success;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070043
44 // else loop back to decode.
45 }
46 else {
Jeff Thompson5e275b42013-07-16 19:10:11 -070047 // Save remaining data for a later call.
48 if (!self->usePartialData) {
49 self->usePartialData = 1;
50 self->partialDataLength = 0;
51 }
52
Jeff Thompson94ddc272013-08-08 14:17:38 -070053 if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength)))
Jeff Thompson5e275b42013-07-16 19:10:11 -070054 return error;
55 self->partialDataLength += dataLength;
56
Jeff Thompsonadaf9232013-08-08 14:30:29 -070057 return NDN_ERROR_success;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070058 }
59 }
Jeff Thompson7687dc02013-09-13 11:54:07 -070060}