blob: 87e1b7a03e521c78d9835bb1bc090e9bbcf02ad9 [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
Jeff Thompsonf0fea002013-07-30 17:22:42 -070015struct 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 Thompsonf0fea002013-07-30 17:22:42 -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 Thompsonf0fea002013-07-30 17:22:42 -070030 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson179d0502013-06-28 11:36:00 -070031 * @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 Thompsonf0fea002013-07-30 17:22:42 -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 Thompsonf0fea002013-07-30 17:22:42 -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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070048 * @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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070056 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070057 * @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 Thompsonf0fea002013-07-30 17:22:42 -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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070068 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070069 * @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.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070073 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070074 * 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 Thompsonf0fea002013-07-30 17:22:42 -070078ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement
79 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
Jeff Thompson033d7fd2013-07-11 10:44:03 -070080
81/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070082 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readBinaryDTagElement.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070083 * Otherwise, set value and valueLength to 0.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070084 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson033d7fd2013-07-11 10:44:03 -070085 * @param expectedTag the expected value for DTAG
86 * @param allowNull 1 if the binary item may be missing
87 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
88 * binary data item is absent, then return 0.
89 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
90 * binary data item is absent, then return 0.
91 * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
92 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070093ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
94 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070095
96/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070097 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
98 * Then read one item expecting it to be type UDATA, and return the item's value and length.
99 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700100 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700101 * @param expectedTag the expected value for DTAG
102 * @param value output a pointer to the binary data inside self's input buffer.
Jeff Thompson033d7fd2013-07-11 10:44:03 -0700103 * @param valueLength output the length of the binary data.
Jeff Thompson8b666002013-07-08 01:16:26 -0700104 * @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 -0700105 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700106ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement
107 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700108
109/**
110 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
111 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
112 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700113 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700114 * @param expectedTag the expected value for DTAG
115 * @param value output the unsigned integer
Jeff Thompson8b666002013-07-08 01:16:26 -0700116 * @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 -0700117 * or can't parse the integer
118 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700119ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement
120 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700121
122/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement.
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700124 * Otherwise, set value to -1.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700125 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700126 * @param expectedTag the expected value for DTAG
127 * @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 -0700128 * @return 0 for success, else an error code, including an error if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700129 * or can't parse the integer
130 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700131ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
132 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700133
134/**
Jeff Thompson9693d392013-07-11 14:58:53 -0700135 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
136 * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds.
137 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700138 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700139 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700140 * @param milliseconds output the number of milliseconds
Jeff Thompson9693d392013-07-11 14:58:53 -0700141 * @return 0 for success, else an error code, including an error if not the expected tag
142 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700143ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement
144 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700145
146/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700147 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement.
Jeff Thompson9693d392013-07-11 14:58:53 -0700148 * Otherwise, set value to -1.0 .
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700149 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700150 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700151 * @param milliseconds output the number of milliseconds, or -1.0 if the next element doesn't have expectedTag.
Jeff Thompson9693d392013-07-11 14:58:53 -0700152 * @return 0 for success, else an error code
153 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700154ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
155 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700156
157/**
Jeff Thompson5553d992013-07-11 14:35:45 -0700158 * Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow.
159 * We use a double because it is large enough to represent NDN time (4096 ticks per second since 1970).
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700160 * @param bytes pointer to the array of bytes
161 * @param bytesLength the length of bytes
Jeff Thompson5553d992013-07-11 14:35:45 -0700162 * @return the result
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700163 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700164double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength);
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700165
166/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700167 * Set the offset into the input, used for the next read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700168 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonf7316692013-06-26 21:31:42 -0700169 * @param offset the new offset
170 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700171static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, unsigned int offset)
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700172{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700173 self->offset = offset;
174}
175
Jeff Thompson76317aa2013-06-25 19:11:48 -0700176#ifdef __cplusplus
177}
178#endif
179
180#endif