blob: 5072b973839852b5c85c99ce611e7dd8bf711490 [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 Thompson25b4e612013-10-10 16:03:24 -070010#include <ndn-cpp/c/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 Thompson5424ab32013-12-04 15:51:15 -080022 // peekDTag sets and checks these, and readElementStartDTag uses them to avoid reading again.
23 size_t previouslyPeekedDTagStartOffset;
24 size_t previouslyPeekedDTagEndOffset;
25 unsigned int previouslyPeekedDTag;
Jeff Thompson76317aa2013-06-25 19:11:48 -070026};
27
Jeff Thompson97223af2013-09-24 17:01:27 -070028static inline void ndn_BinaryXmlDecoder_initialize(struct ndn_BinaryXmlDecoder *self, uint8_t *input, size_t inputLength)
Jeff Thompson6c9b6512013-06-27 15:59:47 -070029{
Jeff Thompson76317aa2013-06-25 19:11:48 -070030 self->input = input;
Jeff Thompsonf7316692013-06-26 21:31:42 -070031 self->inputLength = inputLength;
Jeff Thompson76317aa2013-06-25 19:11:48 -070032 self->offset = 0;
Jeff Thompson5424ab32013-12-04 15:51:15 -080033 self->previouslyPeekedDTagStartOffset = (size_t)-1;
Jeff Thompson76317aa2013-06-25 19:11:48 -070034}
35
Jeff Thompson179d0502013-06-28 11:36:00 -070036/**
37 * Decode the header's type and value from self's input starting at offset. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070038 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson179d0502013-06-28 11:36:00 -070039 * @param type output for the header type
40 * @param value output for the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070041 * @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 -070042 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070043ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *value);
Jeff Thompson76317aa2013-06-25 19:11:48 -070044
Jeff Thompsonf7316692013-06-26 21:31:42 -070045/**
Jeff Thompson74ab0812013-06-28 12:25:04 -070046 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
47 * Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070048 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070049 * @param expectedTag the expected value for DTAG
Jeff Thompson8b666002013-07-08 01:16:26 -070050 * @return 0 for success, else an error code, including an error if not the expected tag
Jeff Thompson179d0502013-06-28 11:36:00 -070051 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070052ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag);
Jeff Thompson74ab0812013-06-28 12:25:04 -070053
54/**
55 * Read one byte from self's input starting at offset, expecting it to be the element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070056 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070057 * @return 0 for success, else an error code, including an error if not the element close
Jeff Thompson74ab0812013-06-28 12:25:04 -070058 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self);
Jeff Thompson74ab0812013-06-28 12:25:04 -070060
61/**
62 * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag,
63 * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070064 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson74ab0812013-06-28 12:25:04 -070065 * @param expectedTag the expected value for DTAG
66 * @param gotExpectedTag output a 1 if got the expected tag, else 0
Jeff Thompson8b666002013-07-08 01:16:26 -070067 * @return 0 for success, else an error code for read past the end of the input
Jeff Thompson74ab0812013-06-28 12:25:04 -070068 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag);
Jeff Thompson179d0502013-06-28 11:36:00 -070070
71/**
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070072 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
73 * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length.
74 * However, if allowNull is 1, then the item may be absent.
75 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070076 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070077 * @param expectedTag the expected value for DTAG
78 * @param allowNull 1 if the binary item may be missing
79 * @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 -070080 * binary data item is absent, then set value and length to 0.
Jeff Thompson8b666002013-07-08 01:16:26 -070081 * @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 -070082 * and the binary data is absent
83 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070084ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070085 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value);
Jeff Thompson033d7fd2013-07-11 10:44:03 -070086
87/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070088 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readBinaryDTagElement.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070089 * Otherwise, set value and valueLength to 0.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070090 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson033d7fd2013-07-11 10:44:03 -070091 * @param expectedTag the expected value for DTAG
92 * @param allowNull 1 if the binary item may be missing
93 * @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 -070094 * binary data item is absent, then set value and length to 0.
Jeff Thompson033d7fd2013-07-11 10:44:03 -070095 * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent
96 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070097ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070098 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value);
Jeff Thompsonb4ee4002013-06-28 13:41:43 -070099
100/**
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700101 * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag.
102 * Then read one item expecting it to be type UDATA, and return the item's value and length.
103 * Finally, read the element close. Update offset.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700104 * @param self pointer to the ndn_BinaryXmlDecoder struct
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700105 * @param expectedTag the expected value for DTAG
106 * @param value output a pointer to the binary data inside self's input buffer.
Jeff Thompson8b666002013-07-08 01:16:26 -0700107 * @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 -0700108 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700109ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700110 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value);
Jeff Thompson167ca5a2013-07-07 20:45:43 -0700111
112/**
Jeff Thompson57be78e2013-08-27 13:40:23 -0700113 * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readUDataDTagElement.
114 * Otherwise, set value and valueLength to 0.
115 * @param self pointer to the ndn_BinaryXmlDecoder struct
116 * @param expectedTag the expected value for DTAG
117 * @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 -0700118 * binary data item is absent, then set value and length to 0.
Jeff Thompson57be78e2013-08-27 13:40:23 -0700119 * @return 0 for success, else an error code.
120 */
121ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700122 (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value);
Jeff Thompson57be78e2013-08-27 13:40:23 -0700123
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 Thompson97223af2013-09-24 17:01:27 -0700179double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t 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 Thompson97223af2013-09-24 17:01:27 -0700186static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, size_t 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