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 | |
| 6 | #ifndef NDN_BINARYXMLELEMENTREADER_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 7 | #define NDN_BINARYXMLELEMENTREADER_H |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 8 | |
| 9 | #include "../errors.h" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "binary-xml-structure-decoder.h" |
| 11 | #include "../util/dynamic-uchar-array.h" |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
| 17 | /** An ndn_ElementListener struct holds a function pointer onReceivedElement. You can extend this struct with data that |
| 18 | * will be passed to onReceivedElement. |
| 19 | */ |
| 20 | struct ndn_ElementListener { |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 21 | void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength); /**< see ndn_ElementListener_initialize */ |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * Initialize an ndn_ElementListener struct to use the onReceivedElement function pointer. |
| 26 | * @param self pointer to the ndn_ElementListener struct |
| 27 | * @param onReceivedElement pointer to a function which is called when an entire binary XML element is received. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 28 | * self is the pointer to this ndn_ElementListener struct. See ndn_BinaryXmlElementReader_onReceivedData. |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 29 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 30 | static inline void ndn_ElementListener_initialize |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 31 | (struct ndn_ElementListener *self, void (*onReceivedElement)(struct ndn_ElementListener *self, unsigned char *element, unsigned int elementLength)) |
| 32 | { |
| 33 | self->onReceivedElement = onReceivedElement; |
| 34 | } |
| 35 | |
| 36 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 37 | * A BinaryXmlElementReader lets you call ndn_BinaryXmlElementReader_onReceivedData multiple times which uses an |
| 38 | * 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] | 39 | * (*elementListener->onReceivedElement)(element, elementLength) with the element. |
| 40 | * This handles the case where a single call to onReceivedData may contain multiple elements. |
| 41 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 42 | struct ndn_BinaryXmlElementReader { |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 43 | struct ndn_ElementListener *elementListener; |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 44 | struct ndn_BinaryXmlStructureDecoder structureDecoder; |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 45 | int usePartialData; |
| 46 | struct ndn_DynamicUCharArray partialData; |
| 47 | unsigned int partialDataLength; |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 51 | * Initialize an ndn_BinaryXmlElementReader struct with the elementListener and a buffer for saving partial data. |
| 52 | * @param self pointer to the ndn_BinaryXmlElementReader struct |
| 53 | * @param elementListener pointer to the ndn_ElementListener used by ndn_BinaryXmlElementReader_onReceivedData. |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 54 | * @param buffer the allocated buffer. If reallocFunction is null, this should be large enough to save a full element, perhaps 8000 bytes. |
| 55 | * @param bufferLength the length of the buffer |
| 56 | * @param reallocFunction see ndn_DynamicUCharArray_ensureLength. This may be 0. |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 57 | */ |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 58 | static inline void ndn_BinaryXmlElementReader_initialize |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | (struct ndn_BinaryXmlElementReader *self, struct ndn_ElementListener *elementListener, |
Jeff Thompson | 9254d74 | 2013-08-12 12:04:44 -0700 | [diff] [blame] | 60 | unsigned char *buffer, unsigned int bufferLength, unsigned char * (*reallocFunction)(struct ndn_DynamicUCharArray *self, unsigned char *, unsigned int)) |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 61 | { |
| 62 | self->elementListener = elementListener; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 63 | ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder); |
Jeff Thompson | 5e275b4 | 2013-07-16 19:10:11 -0700 | [diff] [blame] | 64 | self->usePartialData = 0; |
Jeff Thompson | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 65 | ndn_DynamicUCharArray_initialize(&self->partialData, buffer, bufferLength, reallocFunction); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Continue to read binary XML data until the end of an element, then call (*elementListener->onReceivedElement)(element, elementLength). |
| 70 | * 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] | 71 | * @param self pointer to the ndn_BinaryXmlElementReader struct |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 72 | * @param data pointer to the buffer with the binary XML bytes |
| 73 | * @param dataLength length of data |
| 74 | * @return 0 for success, else an error code |
| 75 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 76 | ndn_Error ndn_BinaryXmlElementReader_onReceivedData |
| 77 | (struct ndn_BinaryXmlElementReader *self, unsigned char *data, unsigned int dataLength); |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 78 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 79 | #ifdef __cplusplus |
Jeff Thompson | b42e363 | 2013-07-15 16:51:42 -0700 | [diff] [blame] | 80 | } |
| 81 | #endif |
| 82 | |
| 83 | #endif |