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" |
| 12 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 17 | struct ndn_BinaryXmlDecoder { |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 18 | uint8_t *input; |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 19 | size_t inputLength; |
| 20 | size_t offset; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 23 | 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] | 24 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 25 | self->input = input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 26 | self->inputLength = inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 27 | self->offset = 0; |
| 28 | } |
| 29 | |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 30 | /** |
| 31 | * 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] | 32 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 33 | * @param type output for the header type |
| 34 | * @param value output for the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 35 | * @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] | 36 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 37 | 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] | 38 | |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 39 | /** |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 40 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 42 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 43 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 44 | * @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] | 45 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 46 | ndn_Error ndn_BinaryXmlDecoder_readElementStartDTag(struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * 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] | 50 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 51 | * @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] | 52 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 53 | ndn_Error ndn_BinaryXmlDecoder_readElementClose(struct ndn_BinaryXmlDecoder *self); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 54 | |
| 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 58 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 59 | * @param expectedTag the expected value for DTAG |
| 60 | * @param gotExpectedTag output a 1 if got the expected tag, else 0 |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 61 | * @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] | 62 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 63 | 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] | 64 | |
| 65 | /** |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 66 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 70 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 71 | * @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 Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 75 | * @param valueLength output the length of the binary data. However, if allowNull is 1 and the |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 76 | * binary data item is absent, then return 0. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 77 | * @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] | 78 | * and the binary data is absent |
| 79 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 80 | ndn_Error ndn_BinaryXmlDecoder_readBinaryDTagElement |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 81 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, uint8_t **value, size_t *valueLength); |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 84 | * 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] | 85 | * Otherwise, set value and valueLength to 0. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 86 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 87 | * @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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 95 | ndn_Error ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 96 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int allowNull, uint8_t **value, size_t *valueLength); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 99 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 102 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 103 | * @param expectedTag the expected value for DTAG |
| 104 | * @param value output a pointer to the binary data inside self's input buffer. |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 105 | * @param valueLength output the length of the binary data. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 106 | * @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] | 107 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 108 | ndn_Error ndn_BinaryXmlDecoder_readUDataDTagElement |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 109 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, uint8_t **value, size_t *valueLength); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 112 | * 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 | */ |
| 122 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUDataDTagElement |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 123 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, uint8_t **value, size_t *valueLength); |
Jeff Thompson | 57be78e | 2013-08-27 13:40:23 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 126 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 129 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 130 | * @param expectedTag the expected value for DTAG |
| 131 | * @param value output the unsigned integer |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 132 | * @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] | 133 | * or can't parse the integer |
| 134 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 135 | ndn_Error ndn_BinaryXmlDecoder_readUnsignedIntegerDTagElement |
| 136 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, unsigned int *value); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 139 | * 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] | 140 | * Otherwise, set value to -1. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 141 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 142 | * @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 Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 144 | * @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] | 145 | * or can't parse the integer |
| 146 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 147 | ndn_Error ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 148 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, int *value); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 149 | |
| 150 | /** |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 151 | * 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 Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 154 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 155 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 156 | * @param milliseconds output the number of milliseconds |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 157 | * @return 0 for success, else an error code, including an error if not the expected tag |
| 158 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 159 | ndn_Error ndn_BinaryXmlDecoder_readTimeMillisecondsDTagElement |
| 160 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds); |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 163 | * 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] | 164 | * Otherwise, set value to -1.0 . |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 165 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 166 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | e32dc5d | 2013-07-11 17:56:57 -0700 | [diff] [blame] | 167 | * @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] | 168 | * @return 0 for success, else an error code |
| 169 | */ |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 170 | ndn_Error ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 171 | (struct ndn_BinaryXmlDecoder *self, unsigned int expectedTag, double *milliseconds); |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 174 | * 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 Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 176 | * @param bytes pointer to the array of bytes |
| 177 | * @param bytesLength the length of bytes |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 178 | * @return the result |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 179 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 180 | double ndn_BinaryXmlDecoder_unsignedBigEndianToDouble(uint8_t *bytes, size_t bytesLength); |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 183 | * Set the offset into the input, used for the next read. |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 184 | * @param self pointer to the ndn_BinaryXmlDecoder struct |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 185 | * @param offset the new offset |
| 186 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 187 | 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] | 188 | { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 189 | self->offset = offset; |
| 190 | } |
| 191 | |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 192 | #ifdef __cplusplus |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 193 | } |
| 194 | #endif |
| 195 | |
| 196 | #endif |