Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 6 | #include "binary-xml-element-reader.h" |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 7 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 8 | ndn_Error ndn_BinaryXmlElementReader_onReceivedData |
| 9 | (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 10 | { |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 14 | ndn_BinaryXmlStructureDecoder_seek(&self->structureDecoder, 0); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 15 | |
| 16 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame^] | 17 | if ((error = ndn_BinaryXmlStructureDecoder_findElementEnd(&self->structureDecoder, data, dataLength))) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 18 | 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 Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 24 | #endif |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 25 | if (self->usePartialData) { |
| 26 | // 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^] | 27 | 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] | 28 | 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 Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 38 | |
| 39 | // Need to read a new object. |
| 40 | data += self->structureDecoder.offset; |
| 41 | dataLength -= self->structureDecoder.offset; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 42 | ndn_BinaryXmlStructureDecoder_init(&self->structureDecoder); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 43 | if (dataLength == 0) |
| 44 | // No more data in the packet. |
| 45 | return 0; |
| 46 | |
| 47 | // else loop back to decode. |
| 48 | } |
| 49 | else { |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 50 | // Save remaining data for a later call. |
| 51 | if (!self->usePartialData) { |
| 52 | self->usePartialData = 1; |
| 53 | self->partialDataLength = 0; |
| 54 | } |
| 55 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame^] | 56 | if ((error = ndn_DynamicUCharArray_set(&self->partialData, data, dataLength, self->partialDataLength))) |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 57 | return error; |
| 58 | self->partialDataLength += dataLength; |
| 59 | |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | } |