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 | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 7 | #ifndef NDN_BINARY_XML_ELEMENT_READER_H |
| 8 | #define NDN_BINARY_XML_ELEMENT_READER_H |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 9 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 10 | #include <ndn-cpp-dev/c/encoding/element-listener.h> |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 11 | #include "../errors.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 12 | #include "binary-xml-structure-decoder.h" |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 13 | #include "../util/dynamic-uint8-array.h" |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 15 | #ifdef __cplusplus |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 16 | extern "C" { |
| 17 | #endif |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 20 | * A BinaryXmlElementReader lets you call ndn_BinaryXmlElementReader_onReceivedData multiple times which uses an |
| 21 | * ndn_BinaryXmlStructureDecoder to detect the end of a binary XML element and calls |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 22 | * (*elementListener->onReceivedElement)(element, elementLength) with the element. |
| 23 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 24 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 25 | struct ndn_BinaryXmlElementReader { |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 26 | struct ndn_ElementListener *elementListener; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 27 | struct ndn_BinaryXmlStructureDecoder structureDecoder; |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 28 | int usePartialData; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 29 | struct ndn_DynamicUInt8Array partialData; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 30 | size_t partialDataLength; |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 34 | * Initialize an ndn_BinaryXmlElementReader struct with the elementListener and a buffer for saving partial data. |
| 35 | * @param self pointer to the ndn_BinaryXmlElementReader struct |
| 36 | * @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXmlElementReader_onReceivedData. |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 37 | * @param buffer the allocated buffer. If reallocFunction is null, this should be large enough to save a full element, perhaps 8000 bytes. |
| 38 | * @param bufferLength the length of the buffer |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 39 | * @param reallocFunction see ndn_DynamicUInt8Array_ensureLength. This may be 0. |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 40 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 41 | static inline void ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 42 | (struct ndn_BinaryXmlElementReader *self, struct ndn_ElementListener *elementListener, |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 43 | uint8_t *buffer, size_t bufferLength, uint8_t * (*reallocFunction)(struct ndn_DynamicUInt8Array *self, uint8_t *, size_t)) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 44 | { |
| 45 | self->elementListener = elementListener; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 46 | ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder); |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 47 | self->usePartialData = 0; |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 48 | ndn_DynamicUInt8Array_initialize(&self->partialData, buffer, bufferLength, reallocFunction); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Continue to read binary XML data until the end of an element, then call (*elementListener->onReceivedElement)(element, elementLength). |
| 53 | * The buffer passed to onReceivedElement is only valid during this call. If you need the data later, you must copy. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | * @param self pointer to the ndn_BinaryXmlElementReader struct |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 55 | * @param data pointer to the buffer with the binary XML bytes |
| 56 | * @param dataLength length of data |
| 57 | * @return 0 for success, else an error code |
| 58 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | ndn_Error ndn_BinaryXmlElementReader_onReceivedData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 60 | (struct ndn_BinaryXmlElementReader *self, uint8_t *data, size_t dataLength); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 61 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 62 | #ifdef __cplusplus |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 63 | } |
| 64 | #endif |
| 65 | |
| 66 | #endif |