blob: a520a0f1d9cf16ded874e084607feb71922f4086 [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_BINARYXMLDECODER_H
Jeff Thompsona0d18c92013-08-06 13:55:32 -07008#define NDN_BINARYXMLDECODER_H
Jeff Thompson76317aa2013-06-25 19:11:48 -07009
Jeff Thompson10ad12a2013-09-24 16:19:11 -070010#include "../common.h"
Jeff Thompson8b666002013-07-08 01:16:26 -070011#include "../errors.h"
12
Jeff Thompsona0d18c92013-08-06 13:55:32 -070013#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -070014extern "C" {
15#endif
16
Jeff Thompsonf0fea002013-07-30 17:22:42 -070017struct ndn_BinaryXmlDecoder {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070018 uint8_t *input;
Jeff Thompson97223af2013-09-24 17:01:27 -070019 size_t inputLength;
20 size_t offset;
Jeff Thompson76317aa2013-06-25 19:11:48 -070021};
22
Jeff Thompson97223af2013-09-24 17:01:27 -070023static inline void ndn_BinaryXmlDecoder_initialize(struct ndn_BinaryXmlDecoder *self, uint8_t *input, size_t inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070024{
Jeff Thompson76317aa2013-06-25 19:11:48 -070025 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070026 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070027 self->offset = 0;
28}
29
Jeff Thompson179d0502013-06-28 11:36:00 -070030/**
31 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070032 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson179d0502013-06-28 11:36:00 -070033 * @param type output for the header type
34 * @param value output for the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070035 * @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 -070036 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070037ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070038
Jeff Thompsonf7316692013-06-26 21:31:42 -070039/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070040 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
41 * Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070043 * @param expectedTag the expected value for DTAG
Jeff Thompson8b666002013-07-08 01:16:26 -070044 * @return 0 for success, else an error code, including an error if not the expected tag
Jeff Thompson179d0502013-06-28 11:36:00 -070045 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070046ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070047
48/**
49 * Read one byte from self's input starting at offset, expecting it to be the element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070050 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070051 * @return 0 for success, else an error code, including an error if not the element close
Jeff Thompson74ab0812013-06-28 12:25:04 -070052 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070053ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self);
Jeff Thompson74ab0812013-06-28 12:25:04 -070054
55/**
56 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
57 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070058 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070059 * @param expectedTag the expected value for DTAG
60 * @param gotExpectedTag output a 1 if got the expected tag, else 0
Jeff Thompson8b666002013-07-08 01:16:26 -070061 * @return 0 for success, else an error code for read past the end of the input
Jeff Thompson74ab0812013-06-28 12:25:04 -070062 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070063ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070064
65/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070066 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
67 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
68 * However, if allowNull is 1, then the item may be absent.
69 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070070 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070071 * @param expectedTag the expected value for DTAG
72 * @param allowNull 1 if the binary item may be missing
73 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
74 * binary data item is absent, then return 0.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070075 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070076 * binary data item is absent, then return 0.
Jeff Thompson8b666002013-07-08 01:16:26 -070077 * @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 -070078 * and the binary data is absent
79 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070080ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -070081 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, uint8_t **value, size_t *valueLength);
Jeff Thompson033d7fd2013-07-11 10:44:03 -070082
83/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070084 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readBinaryDTagElement.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070085 * Otherwise, set value and valueLength to 0.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070086 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson033d7fd2013-07-11 10:44:03 -070087 * @param expectedTag the expected value for DTAG
88 * @param allowNull 1 if the binary item may be missing
89 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
90 * binary data item is absent, then return 0.
91 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
92 * binary data item is absent, then return 0.
93 * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
94 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070095ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -070096 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, uint8_t **value, size_t *valueLength);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070097
98/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070099 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
100 * Then read one item expecting it to be type UDATA, and return the item's value and length.
101 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700102 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700103 * @param expectedTag the expected value for DTAG
104 * @param value output a pointer to the binary data inside self's input buffer.
Jeff Thompson033d7fd2013-07-11 10:44:03 -0700105 * @param valueLength output the length of the binary data.
Jeff Thompson8b666002013-07-08 01:16:26 -0700106 * @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 -0700107 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700108ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -0700109 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, uint8_t **value, size_t *valueLength);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700110
111/**
Jeff Thompson57be78e2013-08-27 13:40:23 -0700112 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readUDataDTagElement.
113 * Otherwise, set value and valueLength to 0.
114 * @param self pointer to the ndn_BinaryXmlDecoder struct
115 * @param expectedTag the expected value for DTAG
116 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
117 * binary data item is absent, then return 0.
118 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
119 * binary data item is absent, then return 0.
120 * @return 0 for success, else an error code.
121 */
122ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
Jeff Thompson97223af2013-09-24 17:01:27 -0700123 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, uint8_t **value, size_t *valueLength);
Jeff Thompson57be78e2013-08-27 13:40:23 -0700124
125/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700126 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
127 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
128 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700129 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700130 * @param expectedTag the expected value for DTAG
131 * @param value output the unsigned integer
Jeff Thompson8b666002013-07-08 01:16:26 -0700132 * @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 -0700133 * or can't parse the integer
134 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700135ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement
136 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700137
138/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700139 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement.
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700140 * Otherwise, set value to -1.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700141 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700142 * @param expectedTag the expected value for DTAG
143 * @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 -0700144 * @return 0 for success, else an error code, including an error if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700145 * or can't parse the integer
146 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700147ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
148 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700149
150/**
Jeff Thompson9693d392013-07-11 14:58:53 -0700151 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
152 * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds.
153 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700154 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700155 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700156 * @param milliseconds output the number of milliseconds
Jeff Thompson9693d392013-07-11 14:58:53 -0700157 * @return 0 for success, else an error code, including an error if not the expected tag
158 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700159ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement
160 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700161
162/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700163 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement.
Jeff Thompson9693d392013-07-11 14:58:53 -0700164 * Otherwise, set value to -1.0 .
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700165 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700166 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700167 * @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 -0700168 * @return 0 for success, else an error code
169 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700170ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
171 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700172
173/**
Jeff Thompson5553d992013-07-11 14:35:45 -0700174 * Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow.
175 * 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 -0700176 * @param bytes pointer to the array of bytes
177 * @param bytesLength the length of bytes
Jeff Thompson5553d992013-07-11 14:35:45 -0700178 * @return the result
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700179 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700180double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t bytesLength);
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700181
182/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700183 * Set the offset into the input, used for the next read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700184 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonf7316692013-06-26 21:31:42 -0700185 * @param offset the new offset
186 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700187static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, size_t offset)
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700188{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700189 self->offset = offset;
190}
191
Jeff Thompsona0d18c92013-08-06 13:55:32 -0700192#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -0700193}
194#endif
195
196#endif