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