blob: da5ad08d5f0a977a5bd876a3504417a7e490f77f [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -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 Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson76317aa2013-06-25 19:11:48 -07005 */
6
7#ifndef NDN_BINARYXMLSTRUCTUREDECODER_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_BINARYXMLSTRUCTUREDECODER_H
Jeff Thompson76317aa2013-06-25 19:11:48 -07009
Yingdi Yu61ec2722014-01-20 14:22:32 -080010#include <ndn-cpp-dev/c/common.h>
Jeff Thompson8b666002013-07-08 01:16:26 -070011#include "../errors.h"
12
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -070014extern "C" {
15#endif
16
Jeff Thompsonf0fea002013-07-30 17:22:42 -070017struct ndn_BinaryXmlStructureDecoder {
Jeff Thompson4e278992013-06-26 18:59:17 -070018 int gotElementEnd; /**< boolean */
Jeff Thompson97223af2013-09-24 17:01:27 -070019 size_t offset;
Jeff Thompson76317aa2013-06-25 19:11:48 -070020 int level;
21 int state;
Jeff Thompson97223af2013-09-24 17:01:27 -070022 size_t headerLength;
Jeff Thompson4e278992013-06-26 18:59:17 -070023 int useHeaderBuffer; /**< boolean */
Jeff Thompson76317aa2013-06-25 19:11:48 -070024 // 10 bytes is enough to hold an encoded header with a type and a 64 bit value.
Jeff Thompson10ad12a2013-09-24 16:19:11 -070025 uint8_t headerBuffer[10];
Jeff Thompson76317aa2013-06-25 19:11:48 -070026 int nBytesToRead;
27};
28
Jeff Thompson9dc10732013-06-26 21:40:32 -070029enum {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070030 ndn_BinaryXmlStructureDecoder_READ_HEADER_OR_CLOSE,
31 ndn_BinaryXmlStructureDecoder_READ_BYTES
Jeff Thompson9dc10732013-06-26 21:40:32 -070032};
Jeff Thompson76317aa2013-06-25 19:11:48 -070033
Jeff Thompsond1427fb2013-08-29 17:20:32 -070034void ndn_BinaryXmlStructureDecoder_initialize(struct ndn_BinaryXmlStructureDecoder *self);
Jeff Thompson76317aa2013-06-25 19:11:48 -070035
Jeff Thompson9dc10732013-06-26 21:40:32 -070036/**
37 * Continue scanning input starting from self->offset to find the element end. On return, you must check
38 * self->gotElementEnd: If the end of the element which started at offset 0 is found,
39 * then self->gotElementEnd is 1 and self->offset is the length of the element. Otherwise, self-forElementEnd is 0
40 * which means you should read more into input and call again.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070041 * @param self pointer to the ndn_BinaryXmlStructureDecoder struct
Jeff Thompson9dc10732013-06-26 21:40:32 -070042 * @param input the input buffer. You have to pass in input each time because the buffer could be reallocated.
43 * @param inputLength the number of bytes in input.
Jeff Thompson8b666002013-07-08 01:16:26 -070044 * @return 0 for success, else an error code
Jeff Thompson9dc10732013-06-26 21:40:32 -070045 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070046ndn_Error ndn_BinaryXmlStructureDecoder_findElementEnd
Jeff Thompson97223af2013-09-24 17:01:27 -070047 (struct ndn_BinaryXmlStructureDecoder *self, uint8_t *input, size_t inputLength);
Jeff Thompson9dc10732013-06-26 21:40:32 -070048
Jeff Thompsonfc53bc52013-07-14 23:25:23 -070049/**
50 * Set the offset into the input, used for the next read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070051 * @param self pointer to the ndn_BinaryXmlStructureDecoder struct
Jeff Thompsonfc53bc52013-07-14 23:25:23 -070052 * @param offset the new offset
53 */
Jeff Thompson97223af2013-09-24 17:01:27 -070054static inline void ndn_BinaryXmlStructureDecoder_seek(struct ndn_BinaryXmlStructureDecoder *self, size_t offset)
Jeff Thompsonfc53bc52013-07-14 23:25:23 -070055{
56 self->offset = offset;
57}
58
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070059#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -070060}
61#endif
62
63#endif