blob: 3e1649790f06f3142e9c7395f7d7042599b84f18 [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"
Jeff Thompson93034532013-10-08 11:52:43 -070012#include "../util/blob.h"
Jeff Thompson8b666002013-07-08 01:16:26 -070013
Jeff Thompsona0d18c92013-08-06 13:55:32 -070014#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -070015extern "C" {
16#endif
17
Jeff Thompsonf0fea002013-07-30 17:22:42 -070018struct ndn_BinaryXmlDecoder {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070019 uint8_t *input;
Jeff Thompson97223af2013-09-24 17:01:27 -070020 size_t inputLength;
21 size_t offset;
Jeff Thompson76317aa2013-06-25 19:11:48 -070022};
23
Jeff Thompson97223af2013-09-24 17:01:27 -070024static inline void ndn_BinaryXmlDecoder_initialize(struct ndn_BinaryXmlDecoder *self, uint8_t *input, size_t inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070025{
Jeff Thompson76317aa2013-06-25 19:11:48 -070026 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070027 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070028 self->offset = 0;
29}
30
Jeff Thompson179d0502013-06-28 11:36:00 -070031/**
32 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070033 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson179d0502013-06-28 11:36:00 -070034 * @param type output for the header type
35 * @param value output for the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070036 * @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 -070037 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070038ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070039
Jeff Thompsonf7316692013-06-26 21:31:42 -070040/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070041 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
42 * Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070043 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070044 * @param expectedTag the expected value for DTAG
Jeff Thompson8b666002013-07-08 01:16:26 -070045 * @return 0 for success, else an error code, including an error if not the expected tag
Jeff Thompson179d0502013-06-28 11:36:00 -070046 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070047ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070048
49/**
50 * Read one byte from self's input starting at offset, expecting it to be the element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070051 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070052 * @return 0 for success, else an error code, including an error if not the element close
Jeff Thompson74ab0812013-06-28 12:25:04 -070053 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070054ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self);
Jeff Thompson74ab0812013-06-28 12:25:04 -070055
56/**
57 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
58 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070060 * @param expectedTag the expected value for DTAG
61 * @param gotExpectedTag output a 1 if got the expected tag, else 0
Jeff Thompson8b666002013-07-08 01:16:26 -070062 * @return 0 for success, else an error code for read past the end of the input
Jeff Thompson74ab0812013-06-28 12:25:04 -070063 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070064ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070065
66/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070067 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
68 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
69 * However, if allowNull is 1, then the item may be absent.
70 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070071 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070072 * @param expectedTag the expected value for DTAG
73 * @param allowNull 1 if the binary item may be missing
74 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
Jeff Thompson93034532013-10-08 11:52:43 -070075 * binary data item is absent, then set value and length to 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
Jeff Thompson93034532013-10-08 11:52:43 -070080 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value);
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
Jeff Thompson93034532013-10-08 11:52:43 -070089 * binary data item is absent, then set value and length to 0.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070090 * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
91 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070092ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070093 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070094
95/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -070096 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
97 * Then read one item expecting it to be type UDATA, and return the item's value and length.
98 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070099 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700100 * @param expectedTag the expected value for DTAG
101 * @param value output a pointer to the binary data inside self's input buffer.
Jeff Thompson8b666002013-07-08 01:16:26 -0700102 * @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 -0700103 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700104ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700105 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700106
107/**
Jeff Thompson57be78e2013-08-27 13:40:23 -0700108 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readUDataDTagElement.
109 * Otherwise, set value and valueLength to 0.
110 * @param self pointer to the ndn_BinaryXmlDecoder struct
111 * @param expectedTag the expected value for DTAG
112 * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the
Jeff Thompson93034532013-10-08 11:52:43 -0700113 * binary data item is absent, then set value and length to 0.
Jeff Thompson57be78e2013-08-27 13:40:23 -0700114 * @return 0 for success, else an error code.
115 */
116ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700117 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value);
Jeff Thompson57be78e2013-08-27 13:40:23 -0700118
119/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700120 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
121 * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer.
122 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700124 * @param expectedTag the expected value for DTAG
125 * @param value output the unsigned integer
Jeff Thompson8b666002013-07-08 01:16:26 -0700126 * @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 -0700127 * or can't parse the integer
128 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700129ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement
130 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700131
132/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700133 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement.
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700134 * Otherwise, set value to -1.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700135 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700136 * @param expectedTag the expected value for DTAG
137 * @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 -0700138 * @return 0 for success, else an error code, including an error if the item is not UDATA,
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700139 * or can't parse the integer
140 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700141ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
142 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700143
144/**
Jeff Thompson9693d392013-07-11 14:58:53 -0700145 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
146 * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds.
147 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700148 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700149 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700150 * @param milliseconds output the number of milliseconds
Jeff Thompson9693d392013-07-11 14:58:53 -0700151 * @return 0 for success, else an error code, including an error if not the expected tag
152 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700153ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement
154 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700155
156/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700157 * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement.
Jeff Thompson9693d392013-07-11 14:58:53 -0700158 * Otherwise, set value to -1.0 .
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700159 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson9693d392013-07-11 14:58:53 -0700160 * @param expectedTag the expected value for DTAG
Jeff Thompsone32dc5d2013-07-11 17:56:57 -0700161 * @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 -0700162 * @return 0 for success, else an error code
163 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700164ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
165 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds);
Jeff Thompson9693d392013-07-11 14:58:53 -0700166
167/**
Jeff Thompson5553d992013-07-11 14:35:45 -0700168 * Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow.
169 * 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 -0700170 * @param bytes pointer to the array of bytes
171 * @param bytesLength the length of bytes
Jeff Thompson5553d992013-07-11 14:35:45 -0700172 * @return the result
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700173 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700174double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t bytesLength);
Jeff Thompson5abaaca2013-07-07 21:16:04 -0700175
176/**
Jeff Thompsonf7316692013-06-26 21:31:42 -0700177 * Set the offset into the input, used for the next read.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700178 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonf7316692013-06-26 21:31:42 -0700179 * @param offset the new offset
180 */
Jeff Thompson97223af2013-09-24 17:01:27 -0700181static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, size_t offset)
Jeff Thompson6c9b6512013-06-27 15:59:47 -0700182{
Jeff Thompsonf7316692013-06-26 21:31:42 -0700183 self->offset = offset;
184}
185
Jeff Thompsona0d18c92013-08-06 13:55:32 -0700186#ifdef __cplusplus
Jeff Thompson76317aa2013-06-25 19:11:48 -0700187}
188#endif
189
190#endif