blob: 7696541e6fb2fe4462b1266280cff20d61171795 [file] [log] [blame]
Jeff Thompson76317aa2013-06-25 19:11:48 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
7#ifndef NDN_BINARYXMLDECODER_H
8#define NDN_BINARYXMLDECODER_H
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14struct ndn_BinaryXMLDecoder {
Jeff Thompsond6f13282013-06-27 17:31:50 -070015 unsigned char *input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070016 unsigned int inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070017 unsigned int offset;
18};
19
Jeff Thompsond6f13282013-06-27 17:31:50 -070020static inline void ndn_BinaryXMLDecoder_init(struct ndn_BinaryXMLDecoder *self, unsigned char *input, unsigned int inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070021{
Jeff Thompson76317aa2013-06-25 19:11:48 -070022 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070023 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070024 self->offset = 0;
25}
26
Jeff Thompson179d0502013-06-28 11:36:00 -070027/**
28 * Decode the header's type and value from self's input starting at offset. Update offset.
29 * Even though the first byte should not be zero, this silently ignores initial zeros.
30 * @param self pointer to the ndn_BinaryXMLDecoder struct
31 * @param type output for the header type
32 * @param value output for the header value
Jeff Thompson74ab0812013-06-28 12:25:04 -070033 * @return 0 for success, else an error string for read past the end of the input
Jeff Thompson179d0502013-06-28 11:36:00 -070034 */
Jeff Thompsond6f13282013-06-27 17:31:50 -070035char *ndn_BinaryXMLDecoder_decodeTypeAndValue(struct ndn_BinaryXMLDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070036
Jeff Thompsonf7316692013-06-26 21:31:42 -070037/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070038 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
39 * Update offset.
Jeff Thompson179d0502013-06-28 11:36:00 -070040 * @param self pointer to the ndn_BinaryXMLDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070041 * @param expectedTag the expected value for DTAG
Jeff Thompson179d0502013-06-28 11:36:00 -070042 * @return 0 for success, else an error string, including an error if not the expected tag
43 */
Jeff Thompson74ab0812013-06-28 12:25:04 -070044char *ndn_BinaryXMLDecoder_readDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag);
45
46/**
47 * Read one byte from self's input starting at offset, expecting it to be the element close.
48 * @param self pointer to the ndn_BinaryXMLDecoder struct
49 * @return 0 for success, else an error string, including an error if not the element close
50 */
51char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self);
52
53/**
54 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
55 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
56 * @param self pointer to the ndn_BinaryXMLDecoder struct
57 * @param expectedTag the expected value for DTAG
58 * @param gotExpectedTag output a 1 if got the expected tag, else 0
59 * @return 0 for success, else an error string for read past the end of the input
60 */
61char *ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070062
63/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070064 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
65 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
66 * However, if allowNull is 1, then the item may be absent.
67 * Finally, read the element close. Update offset.
68 * @param self pointer to the ndn_BinaryXMLDecoder struct
69 * @param expectedTag the expected value for DTAG
70 * @param allowNull 1 if the binary item may be missing
71 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
72 * binary data item is absent, then return 0.
73 * @param valueLen output the length of the binary data. However, if allowNull is 1 and the
74 * binary data item is absent, then return 0.
75 * @return 0 for success, else an error string, including an error if not the expected tag, or if allowNull is 0
76 * and the binary data is absent
77 */
78char *ndn_BinaryXMLDecoder_readBinaryDTagElement
79 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen);
80
81/**
Jeff Thompsonf7316692013-06-26 21:31:42 -070082 * Set the offset into the input, used for the next read.
83 * @param self pointer to the ndn_BinaryXMLDecoder struct
84 * @param offset the new offset
85 */
Jeff Thompson6c9b6512013-06-27 15:59:47 -070086static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset)
87{
Jeff Thompsonf7316692013-06-26 21:31:42 -070088 self->offset = offset;
89}
90
Jeff Thompson76317aa2013-06-25 19:11:48 -070091#ifdef __cplusplus
92}
93#endif
94
95#endif