Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #include "binary-xml-element-reader.h" |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 9 | ndn_Error ndn_BinaryXmlElementReader_onReceivedData |
| 10 | (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 11 | { |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 15 | ndn_BinaryXmlStructureDecoder_seek(&self->structureDecoder, 0); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 16 | |
| 17 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 18 | if ((error = ndn_BinaryXmlStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength))) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 19 | return error; |
| 20 | if (self->structureDecoder.gotElementEnd) { |
| 21 | // Got the remainder of an element. Report to the caller. |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 22 | if (self->usePartialData) { |
| 23 | // We have partial data from a previous call, so append this data and point to partialData. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 24 | if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, self->structureDecoder.offset, self->partialDataLength))) |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 25 | 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 Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 35 | |
| 36 | // Need to read a new object. |
| 37 | data += self->structureDecoder.offset; |
| 38 | dataLength -= self->structureDecoder.offset; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 39 | ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 40 | if (dataLength == 0) |
| 41 | // No more data in the packet. |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 42 | return NDN_ERROR_success; |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 43 | |
| 44 | // else loop back to decode. |
| 45 | } |
| 46 | else { |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 47 | // Save remaining data for a later call. |
| 48 | if (!self->usePartialData) { |
| 49 | self->usePartialData = 1; |
| 50 | self->partialDataLength = 0; |
| 51 | } |
| 52 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 53 | if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength))) |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 54 | return error; |
| 55 | self->partialDataLength += dataLength; |
| 56 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 57 | return NDN_ERROR_success; |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame^] | 60 | } |