blob: 9c83c3b6db22fd5dc4be62efc1736e877aa916b2 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson76317aa2013-06-25 19:11:48 -07004 */
5
6#ifndef NDN_BINARYXMLDECODER_H
7#define NDN_BINARYXMLDECODER_H
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13struct ndn_BinaryXMLDecoder {
Jeff Thompsond6f13282013-06-27 17:31:50 -070014 unsigned char *input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070015 unsigned int inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070016 unsigned int offset;
17};
18
Jeff Thompsond6f13282013-06-27 17:31:50 -070019static inline void ndn_BinaryXMLDecoder_init(struct ndn_BinaryXMLDecoder *self, unsigned char *input, unsigned int inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070020{
Jeff Thompson76317aa2013-06-25 19:11:48 -070021 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070022 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070023 self->offset = 0;
24}
25
Jeff Thompson179d0502013-06-28 11:36:00 -070026/**
27 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompson179d0502013-06-28 11:36:00 -070028 * @param self pointer to the ndn_BinaryXMLDecoder struct
29 * @param type output for the header type
30 * @param value output for the header value
Jeff Thompson38cbd572013-06-28 14:01:10 -070031 * @return 0 for success, else an error string for read past the end of the input or if the initial byte is zero
Jeff Thompson179d0502013-06-28 11:36:00 -070032 */
Jeff Thompsond6f13282013-06-27 17:31:50 -070033char *ndn_BinaryXMLDecoder_decodeTypeAndValue(struct ndn_BinaryXMLDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070034
Jeff Thompsonf7316692013-06-26 21:31:42 -070035/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070036 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
37 * Update offset.
Jeff Thompson179d0502013-06-28 11:36:00 -070038 * @param self pointer to the ndn_BinaryXMLDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070039 * @param expectedTag the expected value for DTAG
Jeff Thompson179d0502013-06-28 11:36:00 -070040 * @return 0 for success, else an error string, including an error if not the expected tag
41 */
Jeff Thompson1156ed12013-07-01 16:13:56 -070042char *ndn_BinaryXMLDecoder_readElementStartDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070043
44/**
45 * Read one byte from self's input starting at offset, expecting it to be the element close.
46 * @param self pointer to the ndn_BinaryXMLDecoder struct
47 * @return 0 for success, else an error string, including an error if not the element close
48 */
49char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self);
50
51/**
52 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
53 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
54 * @param self pointer to the ndn_BinaryXMLDecoder struct
55 * @param expectedTag the expected value for DTAG
56 * @param gotExpectedTag output a 1 if got the expected tag, else 0
57 * @return 0 for success, else an error string for read past the end of the input
58 */
59char *ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070060
61/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070062 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
63 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
64 * However, if allowNull is 1, then the item may be absent.
65 * Finally, read the element close. Update offset.
66 * @param self pointer to the ndn_BinaryXMLDecoder struct
67 * @param expectedTag the expected value for DTAG
68 * @param allowNull 1 if the binary item may be missing
69 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
70 * binary data item is absent, then return 0.
71 * @param valueLen output the length of the binary data. However, if allowNull is 1 and the
72 * binary data item is absent, then return 0.
73 * @return 0 for success, else an error string, including an error if not the expected tag, or if allowNull is 0
74 * and the binary data is absent
75 */
76char *ndn_BinaryXMLDecoder_readBinaryDTagElement
77 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen);
78
79/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070080 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
81 * Then read one item expecting it to be type UDATA, and return the item's value and length.
82 * Finally, read the element close. Update offset.
83 * @param self pointer to the ndn_BinaryXMLDecoder struct
84 * @param expectedTag the expected value for DTAG
85 * @param value output a pointer to the binary data inside self's input buffer.
86 * @param valueLen output the length of the binary data.
87 * @return 0 for success, else an error string, including an error if not the expected tag, or if the item is not UDATA.
88 */
89char *ndn_BinaryXMLDecoder_readUDataDTagElement
90 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen);
91
92/**
93 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
94 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
95 * Finally, read the element close. Update offset.
96 * @param self pointer to the ndn_BinaryXMLDecoder struct
97 * @param expectedTag the expected value for DTAG
98 * @param value output the unsigned integer
99 * @return 0 for success, else an error string, including an error if not the expected tag, or if the item is not UDATA,
100 * or can't parse the integer
101 */
102char *ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement
103 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned int *value);
104
105/**
106 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement.
107 * Otherwise, set value to -1.
108 * @param self pointer to the ndn_BinaryXMLDecoder struct
109 * @param expectedTag the expected value for DTAG
110 * @param value output the unsigned integer cast to int, or -1 if the next element doesn't have expectedTag.
111 * @return 0 for success, else an error string, including an error if the item is not UDATA,
112 * or can't parse the integer
113 */
114char *ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
115 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value);
116
117/**
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700118 * Convert the big endian bytes to an unsigned int. Don't check for overflow.
119 * @param bytes pointer to the array of bytes
120 * @param bytesLength the length of bytes
121 * @return the result unsigned int
122 */
123unsigned int ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(unsigned char *bytes, unsigned int bytesLength);
124
125/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700126 * Set the offset into the input, used for the next read.
127 * @param self pointer to the ndn_BinaryXMLDecoder struct
128 * @param offset the new offset
129 */
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700130static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset)
131{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700132 self->offset = offset;
133}
134
Jeff Thompson76317aa2013-06-25 19:11:48 -0700135#ifdef __cplusplus
136}
137#endif
138
139#endif