blob: 3b587872e8a5b452b4cbf2886e416737fab3bb48 [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 Thompson8b666002013-07-08 01:16:26 -070010#include "../errors.h"
11
Jeff Thompsona0d18c92013-08-06 13:55:32 -070012#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -070013extern "C" {
14#endif
15
Jeff Thompsonf0fea002013-07-30 17:22:42 -070016struct ndn_BinaryXmlDecoder {
Jeff Thompsond6f13282013-06-27 17:31:50 -070017 unsigned char *input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070018 unsigned int inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070019 unsigned int offset;
20};
21
Jeff Thompsond1427fb2013-08-29 17:20:32 -070022static inline void ndn_BinaryXmlDecoder_initialize(struct ndn_BinaryXmlDecoder *self, unsigned char *input, unsigned int inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070023{
Jeff Thompson76317aa2013-06-25 19:11:48 -070024 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070025 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070026 self->offset = 0;
27}
28
Jeff Thompson179d0502013-06-28 11:36:00 -070029/**
30 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070031 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson179d0502013-06-28 11:36:00 -070032 * @param type output for the header type
33 * @param value output for the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070034 * @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 -070035 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070036ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070037
Jeff Thompsonf7316692013-06-26 21:31:42 -070038/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070039 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
40 * Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070041 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070042 * @param expectedTag the expected value for DTAG
Jeff Thompson8b666002013-07-08 01:16:26 -070043 * @return 0 for success, else an error code, including an error if not the expected tag
Jeff Thompson179d0502013-06-28 11:36:00 -070044 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070045ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070046
47/**
48 * Read one byte from self's input starting at offset, expecting it to be the element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070049 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070050 * @return 0 for success, else an error code, including an error if not the element close
Jeff Thompson74ab0812013-06-28 12:25:04 -070051 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self);
Jeff Thompson74ab0812013-06-28 12:25:04 -070053
54/**
55 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
56 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070057 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070058 * @param expectedTag the expected value for DTAG
59 * @param gotExpectedTag output a 1 if got the expected tag, else 0
Jeff Thompson8b666002013-07-08 01:16:26 -070060 * @return 0 for success, else an error code for read past the end of the input
Jeff Thompson74ab0812013-06-28 12:25:04 -070061 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070062ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070063
64/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070065 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
66 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
67 * However, if allowNull is 1, then the item may be absent.
68 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070070 * @param expectedTag the expected value for DTAG
71 * @param allowNull 1 if the binary item may be missing
72 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
73 * binary data item is absent, then return 0.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070074 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070075 * binary data item is absent, then return 0.
Jeff Thompson8b666002013-07-08 01:16:26 -070076 * @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 -070077 * and the binary data is absent
78 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070079ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement
80 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
Jeff Thompson033d7fd2013-07-11 10:44:03 -070081
82/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070083 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readBinaryDTagElement.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070084 * Otherwise, set value and valueLength to 0.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070085 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson033d7fd2013-07-11 10:44:03 -070086 * @param expectedTag the expected value for DTAG
87 * @param allowNull 1 if the binary item may be missing
88 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
89 * binary data item is absent, then return 0.
90 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
91 * binary data item is absent, then return 0.
92 * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
93 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070094ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
95 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070096
97/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070098 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
99 * Then read one item expecting it to be type UDATA, and return the item's value and length.
100 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700101 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700102 * @param expectedTag the expected value for DTAG
103 * @param value output a pointer to the binary data inside self's input buffer.
Jeff Thompson033d7fd2013-07-11 10:44:03 -0700104 * @param valueLength output the length of the binary data.
Jeff Thompson8b666002013-07-08 01:16:26 -0700105 * @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 -0700106 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700107ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement
108 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700109
110/**
Jeff Thompson57be78e2013-08-27 13:40:23 -0700111 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readUDataDTagElement.
112 * Otherwise, set value and valueLength to 0.
113 * @param self pointer to the ndn_BinaryXmlDecoder struct
114 * @param expectedTag the expected value for DTAG
115 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
116 * binary data item is absent, then return 0.
117 * @param valueLength output the length of the binary data. However, if allowNull is 1 and the
118 * binary data item is absent, then return 0.
119 * @return 0 for success, else an error code.
120 */
121ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
122 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength);
123
124/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700125 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
126 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
127 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700128 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700129 * @param expectedTag the expected value for DTAG
130 * @param value output the unsigned integer
Jeff Thompson8b666002013-07-08 01:16:26 -0700131 * @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 -0700132 * or can't parse the integer
133 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700134ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement
135 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700136
137/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700138 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement.
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700139 * Otherwise, set value to -1.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700140 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700141 * @param expectedTag the expected value for DTAG
142 * @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 -0700143 * @return 0 for success, else an error code, including an error if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700144 * or can't parse the integer
145 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700146ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
147 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700148
149/**
Jeff Thompson9693d392013-07-11 14:58:53 -0700150 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
151 * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds.
152 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700153 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700154 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700155 * @param milliseconds output the number of milliseconds
Jeff Thompson9693d392013-07-11 14:58:53 -0700156 * @return 0 for success, else an error code, including an error if not the expected tag
157 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700158ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement
159 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700160
161/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700162 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement.
Jeff Thompson9693d392013-07-11 14:58:53 -0700163 * Otherwise, set value to -1.0 .
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700164 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700165 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700166 * @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 -0700167 * @return 0 for success, else an error code
168 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700169ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
170 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700171
172/**
Jeff Thompson5553d992013-07-11 14:35:45 -0700173 * Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow.
174 * 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 -0700175 * @param bytes pointer to the array of bytes
176 * @param bytesLength the length of bytes
Jeff Thompson5553d992013-07-11 14:35:45 -0700177 * @return the result
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700178 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700179double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength);
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700180
181/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700182 * Set the offset into the input, used for the next read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700183 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonf7316692013-06-26 21:31:42 -0700184 * @param offset the new offset
185 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700186static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, unsigned int offset)
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700187{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700188 self->offset = offset;
189}
190
Jeff Thompsona0d18c92013-08-06 13:55:32 -0700191#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -0700192}
193#endif
194
195#endif