blob: 89f8c8d08797880ebdbd3b728ade11a0fa0839d8 [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
Jeff Thompson25b4e612013-10-10 16:03:24 -07007#ifndef NDN_BINARY_XML_ELEMENT_READER_H
8#define NDN_BINARY_XML_ELEMENT_READER_H
Jeff Thompsonb42e3632013-07-15 16:51:42 -07009
Jeff Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/c/encoding/element-listener.h>
Jeff Thompsonb42e3632013-07-15 16:51:42 -070011#include "../errors.h"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "binary-xml-structure-decoder.h"
Jeff Thompson10ad12a2013-09-24 16:19:11 -070013#include "../util/dynamic-uint8-array.h"
Jeff Thompsonb42e3632013-07-15 16:51:42 -070014
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070015#ifdef __cplusplus
Jeff Thompsonb42e3632013-07-15 16:51:42 -070016extern "C" {
17#endif
Jeff Thompsonb42e3632013-07-15 16:51:42 -070018
19/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070020 * 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 Thompsonb42e3632013-07-15 16:51:42 -070022 * (*elementListener->onReceivedElement)(element, elementLength) with the element.
23 * This handles the case where a single call to onReceivedData may contain multiple elements.
24 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070025struct ndn_BinaryXmlElementReader {
Jeff Thompsonb42e3632013-07-15 16:51:42 -070026 struct ndn_ElementListener *elementListener;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070027 struct ndn_BinaryXmlStructureDecoder structureDecoder;
Jeff Thompson5e275b42013-07-16 19:10:11 -070028 int usePartialData;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070029 struct ndn_DynamicUInt8Array partialData;
Jeff Thompson97223af2013-09-24 17:01:27 -070030 size_t partialDataLength;
Jeff Thompsonb42e3632013-07-15 16:51:42 -070031};
32
33/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070034 * 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 Thompson5e275b42013-07-16 19:10:11 -070037 * @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 Thompson10ad12a2013-09-24 16:19:11 -070039 * @param reallocFunction see ndn_DynamicUInt8Array_ensureLength. This may be 0.
Jeff Thompsonb42e3632013-07-15 16:51:42 -070040 */
Jeff Thompsond1427fb2013-08-29 17:20:32 -070041static inline void ndn_BinaryXmlElementReader_initialize
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042 (struct ndn_BinaryXmlElementReader *self, struct ndn_ElementListener *elementListener,
Jeff Thompson97223af2013-09-24 17:01:27 -070043 uint8_t *buffer, size_t bufferLength, uint8_t * (*reallocFunction)(struct ndn_DynamicUInt8Array *self, uint8_t *, size_t))
Jeff Thompsonb42e3632013-07-15 16:51:42 -070044{
45 self->elementListener = elementListener;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070046 ndn_BinaryXmlStructureDecoder_initialize(&self->structureDecoder);
Jeff Thompson5e275b42013-07-16 19:10:11 -070047 self->usePartialData = 0;
Jeff Thompson10ad12a2013-09-24 16:19:11 -070048 ndn_DynamicUInt8Array_initialize(&self->partialData, buffer, bufferLength, reallocFunction);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070049}
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 Thompsonf0fea002013-07-30 17:22:42 -070054 * @param self pointer to the ndn_BinaryXmlElementReader struct
Jeff Thompsonb42e3632013-07-15 16:51:42 -070055 * @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 Thompsonf0fea002013-07-30 17:22:42 -070059ndn_Error ndn_BinaryXmlElementReader_onReceivedData
Jeff Thompson97223af2013-09-24 17:01:27 -070060 (struct ndn_BinaryXmlElementReader *self, uint8_t *data, size_t dataLength);
Jeff Thompsonb42e3632013-07-15 16:51:42 -070061
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070062#ifdef __cplusplus
Jeff Thompsonb42e3632013-07-15 16:51:42 -070063}
64#endif
65
66#endif