Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BINARYXMLDECODER_H |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 8 | #define NDN_BINARYXMLDECODER_H |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 10 | #include "../common.h" |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 11 | #include "../errors.h" |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 12 | #include "../util/blob.h" |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 13 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 14 | #ifdef __cplusplus |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 15 | extern "C" { |
| 16 | #endif |
| 17 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 18 | struct ndn_BinaryXmlDecoder { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 19 | uint8_t *input; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 20 | size_t inputLength; |
| 21 | size_t offset; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 24 | static inline void ndn_BinaryXmlDecoder_initialize(struct ndn_BinaryXmlDecoder *self, uint8_t *input, size_t inputLength) |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 25 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 26 | self->input = input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 27 | self->inputLength = inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 28 | self->offset = 0; |
| 29 | } |
| 30 | |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Decode the header's type and value from self's input starting at offset. Update offset. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 33 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 34 | * @param type output for the header type |
| 35 | * @param value output for the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 36 | * @return 0 for success, else an error code for read past the end of the input or if the initial byte is zero |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 37 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 38 | ndn_Error ndn_BinaryXmlDecoder_decodeTypeAndValue(struct ndn_BinaryXmlDecoder *self, unsigned int *type, unsigned int *value); |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 39 | |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 40 | /** |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 41 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 43 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 44 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 45 | * @return 0 for success, else an error code, including an error if not the expected tag |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 46 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 47 | ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Read one byte from self's input starting at offset, expecting it to be the element close. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 51 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 52 | * @return 0 for success, else an error code, including an error if not the element close |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 53 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 54 | ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 55 | |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 60 | * @param expectedTag the expected value for DTAG |
| 61 | * @param gotExpectedTag output a 1 if got the expected tag, else 0 |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 62 | * @return 0 for success, else an error code for read past the end of the input |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 63 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 64 | ndn_Error ndn_BinaryXmlDecoder_peekDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *gotExpectedTag); |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 67 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 71 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 72 | * @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 Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 75 | * binary data item is absent, then set value and length to 0. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 76 | * @return 0 for success, else an error code, including an error if not the expected tag, or if allowNull is 0 |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 77 | * and the binary data is absent |
| 78 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 79 | ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 80 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value); |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 83 | * Peek at the next element and if it is the expectedTag, call ndn_BinaryXmlDecoder_readBinaryDTagElement. |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 84 | * Otherwise, set value and valueLength to 0. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 85 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 86 | * @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 Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 89 | * binary data item is absent, then set value and length to 0. |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 90 | * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent |
| 91 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 92 | ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 93 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, struct ndn_Blob *value); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 96 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 99 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 100 | * @param expectedTag the expected value for DTAG |
| 101 | * @param value output a pointer to the binary data inside self's input buffer. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 102 | * @return 0 for success, else an error code, including an error if not the expected tag, or if the item is not UDATA. |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 103 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 104 | ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 105 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 108 | * 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 Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 113 | * binary data item is absent, then set value and length to 0. |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 114 | * @return 0 for success, else an error code. |
| 115 | */ |
| 116 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 117 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, struct ndn_Blob *value); |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 120 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 123 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 124 | * @param expectedTag the expected value for DTAG |
| 125 | * @param value output the unsigned integer |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 126 | * @return 0 for success, else an error code, including an error if not the expected tag, or if the item is not UDATA, |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 127 | * or can't parse the integer |
| 128 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 129 | ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement |
| 130 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 133 | * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement. |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 134 | * Otherwise, set value to -1. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 135 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 136 | * @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 Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 138 | * @return 0 for success, else an error code, including an error if the item is not UDATA, |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 139 | * or can't parse the integer |
| 140 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 141 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 142 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 145 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 148 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 149 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 150 | * @param milliseconds output the number of milliseconds |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 151 | * @return 0 for success, else an error code, including an error if not the expected tag |
| 152 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 153 | ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement |
| 154 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds); |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 157 | * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement. |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 158 | * Otherwise, set value to -1.0 . |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 159 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 160 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 161 | * @param milliseconds output the number of milliseconds, or -1.0 if the next element doesn't have expectedTag. |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 162 | * @return 0 for success, else an error code |
| 163 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 164 | ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 165 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds); |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 168 | * 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 Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 170 | * @param bytes pointer to the array of bytes |
| 171 | * @param bytesLength the length of bytes |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 172 | * @return the result |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 173 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 174 | double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t bytesLength); |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 175 | |
| 176 | /** |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 177 | * Set the offset into the input, used for the next read. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 178 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 179 | * @param offset the new offset |
| 180 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 181 | static inline void ndn_BinaryXmlDecoder_seek(struct ndn_BinaryXmlDecoder *self, size_t offset) |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 182 | { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 183 | self->offset = offset; |
| 184 | } |
| 185 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 186 | #ifdef __cplusplus |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #endif |