blob: c2b3777c21e2a2f54064e546445cbf1db460e99e [file] [log] [blame]
Jeff Thompsonb42e3632013-07-15 16:51:42 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonb42e3632013-07-15 16:51:42 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_BINARYXMLELEMENTREADER_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_BINARYXMLELEMENTREADER_H
Jeff Thompsonb42e3632013-07-15 16:51:42 -07009
10#include "../errors.h"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "binary-xml-structure-decoder.h"
Jeff Thompson10ad12a2013-09-24 16:19:11 -070012#include "../util/dynamic-uint8-array.h"
Jeff Thompsonb42e3632013-07-15 16:51:42 -070013
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070014#ifdef __cplusplus
Jeff Thompsonb42e3632013-07-15 16:51:42 -070015extern "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 */
21struct ndn_ElementListener {
Jeff Thompson97223af2013-09-24 17:01:27 -070022 void (*onReceivedElement)(struct ndn_ElementListener *self, uint8_t *element, size_t elementLength); /**< see ndn_ElementListener_initialize */
Jeff Thompsonb42e3632013-07-15 16:51:42 -070023};
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 Thompsonf0fea002013-07-30 17:22:42 -070029 * self is the pointer to this ndn_ElementListener struct. See ndn_BinaryXmlElementReader_onReceivedData.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070030 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070031static inline void ndn_ElementListener_initialize
Jeff Thompson97223af2013-09-24 17:01:27 -070032 (struct ndn_ElementListener *self, void (*onReceivedElement)(struct ndn_ElementListener *self, uint8_t *element, size_t elementLength))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070033{
34 self->onReceivedElement = onReceivedElement;
35}
36
37/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070038 * 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 Thompsonb42e3632013-07-15 16:51:42 -070040 * (*elementListener->onReceivedElement)(element, elementLength) with the element.
41 * This handles the case where a single call to onReceivedData may contain multiple elements.
42 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070043struct ndn_BinaryXmlElementReader {
Jeff Thompsonb42e3632013-07-15 16:51:42 -070044 struct ndn_ElementListener *elementListener;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070045 struct ndn_BinaryXmlStructureDecoder structureDecoder;
Jeff Thompson5e275b42013-07-16 19:10:11 -070046 int usePartialData;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070047 struct ndn_DynamicUInt8Array partialData;
Jeff Thompson97223af2013-09-24 17:01:27 -070048 size_t partialDataLength;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070049};
50
51/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052 * 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 Thompson5e275b42013-07-16 19:10:11 -070055 * @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
Jeff Thompson10ad12a2013-09-24 16:19:11 -070057 * @param reallocFunction see ndn_DynamicUInt8Array_ensureLength. This may be 0.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070058 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070059static inline void ndn_BinaryXmlElementReader_initialize
Jeff Thompsonf0fea002013-07-30 17:22:42 -070060 (struct ndn_BinaryXmlElementReader *self, struct ndn_ElementListener *elementListener,
Jeff Thompson97223af2013-09-24 17:01:27 -070061 uint8_t *buffer, size_t bufferLength, uint8_t * (*reallocFunction)(struct ndn_DynamicUInt8Array *self, uint8_t *, size_t))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070062{
63 self->elementListener = elementListener;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070064 ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder);
Jeff Thompson5e275b42013-07-16 19:10:11 -070065 self->usePartialData = 0;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070066 ndn_DynamicUInt8Array_initialize(&self->partialData, buffer, bufferLength, reallocFunction);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070067}
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 Thompsonf0fea002013-07-30 17:22:42 -070072 * @param self pointer to the ndn_BinaryXmlElementReader struct
Jeff Thompsonb42e3632013-07-15 16:51:42 -070073 * @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 Thompsonf0fea002013-07-30 17:22:42 -070077ndn_Error ndn_BinaryXmlElementReader_onReceivedData
Jeff Thompson97223af2013-09-24 17:01:27 -070078 (struct ndn_BinaryXmlElementReader *self, uint8_t *data, size_t dataLength);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070079
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070080#ifdef __cplusplus
Jeff Thompsonb42e3632013-07-15 16:51:42 -070081}
82#endif
83
84#endif