blob: 0c1427e7c9dafa3c85b98e49674d0e0b78964de6 [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
Jeff Thompson8b666002013-07-08 01:16:26 -07009#include "../errors.h"
10
Jeff Thompson76317aa2013-06-25 19:11:48 -070011#ifdef __cplusplus
12extern "C" {
13#endif
14
15struct ndn_BinaryXMLDecoder {
Jeff Thompsond6f13282013-06-27 17:31:50 -070016 unsigned char *input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070017 unsigned int inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070018 unsigned int offset;
19};
20
Jeff Thompsond6f13282013-06-27 17:31:50 -070021static inline void ndn_BinaryXMLDecoder_init(struct ndn_BinaryXMLDecoder *self, unsigned char *input, unsigned int inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070022{
Jeff Thompson76317aa2013-06-25 19:11:48 -070023 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070024 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070025 self->offset = 0;
26}
27
Jeff Thompson179d0502013-06-28 11:36:00 -070028/**
29 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompson179d0502013-06-28 11:36:00 -070030 * @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 Thompson8b666002013-07-08 01:16:26 -070033 * @return 0 for success, else an error code for read past the end of the input or if the initial byte is zero
Jeff Thompson179d0502013-06-28 11:36:00 -070034 */
Jeff Thompson8b666002013-07-08 01:16:26 -070035ndn_Error 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 Thompson8b666002013-07-08 01:16:26 -070042 * @return 0 for success, else an error code, including an error if not the expected tag
Jeff Thompson179d0502013-06-28 11:36:00 -070043 */
Jeff Thompson8b666002013-07-08 01:16:26 -070044ndn_Error ndn_BinaryXMLDecoder_readElementStartDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070045
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
Jeff Thompson8b666002013-07-08 01:16:26 -070049 * @return 0 for success, else an error code, including an error if not the element close
Jeff Thompson74ab0812013-06-28 12:25:04 -070050 */
Jeff Thompson8b666002013-07-08 01:16:26 -070051ndn_Error ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self);
Jeff Thompson74ab0812013-06-28 12:25:04 -070052
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
Jeff Thompson8b666002013-07-08 01:16:26 -070059 * @return 0 for success, else an error code for read past the end of the input
Jeff Thompson74ab0812013-06-28 12:25:04 -070060 */
Jeff Thompson8b666002013-07-08 01:16:26 -070061ndn_Error 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.
Jeff Thompson8b666002013-07-08 01:16:26 -070075 * @return 0 for success, else an error code, including an error if not the expected tag, or if allowNull is 0
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070076 * and the binary data is absent
77 */
Jeff Thompson8b666002013-07-08 01:16:26 -070078ndn_Error ndn_BinaryXMLDecoder_readBinaryDTagElement
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070079 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen);
80
81/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070082 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
83 * Then read one item expecting it to be type UDATA, and return the item's value and length.
84 * Finally, read the element close. Update offset.
85 * @param self pointer to the ndn_BinaryXMLDecoder struct
86 * @param expectedTag the expected value for DTAG
87 * @param value output a pointer to the binary data inside self's input buffer.
88 * @param valueLen output the length of the binary data.
Jeff Thompson8b666002013-07-08 01:16:26 -070089 * @return 0 for success, else an error code, including an error if not the expected tag, or if the item is not UDATA.
Jeff Thompson167ca5a2013-07-07 20:45:43 -070090 */
Jeff Thompson8b666002013-07-08 01:16:26 -070091ndn_Error ndn_BinaryXMLDecoder_readUDataDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -070092 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen);
93
94/**
95 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
96 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
97 * Finally, read the element close. Update offset.
98 * @param self pointer to the ndn_BinaryXMLDecoder struct
99 * @param expectedTag the expected value for DTAG
100 * @param value output the unsigned integer
Jeff Thompson8b666002013-07-08 01:16:26 -0700101 * @return 0 for success, else an error code, including an error if not the expected tag, or if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700102 * or can't parse the integer
103 */
Jeff Thompson8b666002013-07-08 01:16:26 -0700104ndn_Error ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700105 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned int *value);
106
107/**
108 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement.
109 * Otherwise, set value to -1.
110 * @param self pointer to the ndn_BinaryXMLDecoder struct
111 * @param expectedTag the expected value for DTAG
112 * @param value output the unsigned integer cast to int, or -1 if the next element doesn't have expectedTag.
Jeff Thompson8b666002013-07-08 01:16:26 -0700113 * @return 0 for success, else an error code, including an error if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700114 * or can't parse the integer
115 */
Jeff Thompson8b666002013-07-08 01:16:26 -0700116ndn_Error ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700117 (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value);
118
119/**
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700120 * Convert the big endian bytes to an unsigned int. Don't check for overflow.
121 * @param bytes pointer to the array of bytes
122 * @param bytesLength the length of bytes
123 * @return the result unsigned int
124 */
125unsigned int ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(unsigned char *bytes, unsigned int bytesLength);
126
127/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700128 * Set the offset into the input, used for the next read.
129 * @param self pointer to the ndn_BinaryXMLDecoder struct
130 * @param offset the new offset
131 */
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700132static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset)
133{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700134 self->offset = offset;
135}
136
Jeff Thompson76317aa2013-06-25 19:11:48 -0700137#ifdef __cplusplus
138}
139#endif
140
141#endif