Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef NDN_BINARYXMLDECODER_H |
| 7 | #define NDN_BINARYXMLDECODER_H |
| 8 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 9 | #include "../errors.h" |
| 10 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | struct ndn_BinaryXMLDecoder { |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 16 | unsigned char *input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 17 | unsigned int inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 18 | unsigned int offset; |
| 19 | }; |
| 20 | |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 21 | static inline void ndn_BinaryXMLDecoder_init(struct ndn_BinaryXMLDecoder *self, unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 22 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 23 | self->input = input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 24 | self->inputLength = inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 25 | self->offset = 0; |
| 26 | } |
| 27 | |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Decode the header's type and value from self's input starting at offset. Update offset. |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 30 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 31 | * @param type output for the header type |
| 32 | * @param value output for the header value |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 33 | * @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] | 34 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 35 | 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] | 36 | |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 37 | /** |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 38 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 39 | * Update offset. |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 40 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 41 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 42 | * @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] | 43 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 44 | ndn_Error ndn_BinaryXMLDecoder_readElementStartDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Read one byte from self's input starting at offset, expecting it to be the element close. |
| 48 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 49 | * @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] | 50 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 51 | ndn_Error ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag, |
| 55 | * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error. |
| 56 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 57 | * @param expectedTag the expected value for DTAG |
| 58 | * @param gotExpectedTag output a 1 if got the expected tag, else 0 |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 59 | * @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] | 60 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 61 | 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] | 62 | |
| 63 | /** |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 64 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 65 | * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length. |
| 66 | * However, if allowNull is 1, then the item may be absent. |
| 67 | * Finally, read the element close. Update offset. |
| 68 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 69 | * @param expectedTag the expected value for DTAG |
| 70 | * @param allowNull 1 if the binary item may be missing |
| 71 | * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the |
| 72 | * binary data item is absent, then return 0. |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 73 | * @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] | 74 | * binary data item is absent, then return 0. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 75 | * @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] | 76 | * and the binary data is absent |
| 77 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 78 | ndn_Error ndn_BinaryXMLDecoder_readBinaryDTagElement |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 79 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength); |
| 80 | |
| 81 | /** |
| 82 | * Peek at the next element and if it is the expectedTag, call ndn_BinaryXMLDecoder_readBinaryDTagElement. |
| 83 | * Otherwise, set value and valueLength to 0. |
| 84 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 85 | * @param expectedTag the expected value for DTAG |
| 86 | * @param allowNull 1 if the binary item may be missing |
| 87 | * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the |
| 88 | * binary data item is absent, then return 0. |
| 89 | * @param valueLength output the length of the binary data. However, if allowNull is 1 and the |
| 90 | * binary data item is absent, then return 0. |
| 91 | * @return 0 for success, else an error code, including if allowNull is 0 and the binary data is absent |
| 92 | */ |
| 93 | ndn_Error ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement |
| 94 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLength); |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 97 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 98 | * Then read one item expecting it to be type UDATA, and return the item's value and length. |
| 99 | * Finally, read the element close. Update offset. |
| 100 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 101 | * @param expectedTag the expected value for DTAG |
| 102 | * @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] | 103 | * @param valueLength output the length of the binary data. |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 104 | * @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] | 105 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 106 | ndn_Error ndn_BinaryXMLDecoder_readUDataDTagElement |
Jeff Thompson | 033d7fd | 2013-07-11 10:44:03 -0700 | [diff] [blame] | 107 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLength); |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 111 | * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer. |
| 112 | * Finally, read the element close. Update offset. |
| 113 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 114 | * @param expectedTag the expected value for DTAG |
| 115 | * @param value output the unsigned integer |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 116 | * @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] | 117 | * or can't parse the integer |
| 118 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 119 | ndn_Error ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 120 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned int *value); |
| 121 | |
| 122 | /** |
| 123 | * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement. |
| 124 | * Otherwise, set value to -1. |
| 125 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 126 | * @param expectedTag the expected value for DTAG |
| 127 | * @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] | 128 | * @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] | 129 | * or can't parse the integer |
| 130 | */ |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 131 | ndn_Error ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 132 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value); |
| 133 | |
| 134 | /** |
Jeff Thompson | 9693d39 | 2013-07-11 14:58:53 -0700 | [diff] [blame^] | 135 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 136 | * Then read one item, parse it as an unsigned big endian integer in 4096 ticks per second, and convert it to milliseconds. |
| 137 | * Finally, read the element close. Update offset. |
| 138 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 139 | * @param expectedTag the expected value for DTAG |
| 140 | * @param value output the number of milliseconds |
| 141 | * @return 0 for success, else an error code, including an error if not the expected tag |
| 142 | */ |
| 143 | ndn_Error ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement |
| 144 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value); |
| 145 | |
| 146 | /** |
| 147 | * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readTimeMillisecondsDTagElement. |
| 148 | * Otherwise, set value to -1.0 . |
| 149 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 150 | * @param expectedTag the expected value for DTAG |
| 151 | * @param value output the number of milliseconds, or -1.0 if the next element doesn't have expectedTag. |
| 152 | * @return 0 for success, else an error code |
| 153 | */ |
| 154 | ndn_Error ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement |
| 155 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, double *value); |
| 156 | |
| 157 | /** |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 158 | * Interpret the bytes as an unsigned big endian integer and convert to a double. Don't check for overflow. |
| 159 | * 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] | 160 | * @param bytes pointer to the array of bytes |
| 161 | * @param bytesLength the length of bytes |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 162 | * @return the result |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 163 | */ |
Jeff Thompson | 5553d99 | 2013-07-11 14:35:45 -0700 | [diff] [blame] | 164 | double ndn_BinaryXMLDecoder_unsignedBigEndianToDouble(unsigned char *bytes, unsigned int bytesLength); |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 167 | * Set the offset into the input, used for the next read. |
| 168 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 169 | * @param offset the new offset |
| 170 | */ |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 171 | static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset) |
| 172 | { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 173 | self->offset = offset; |
| 174 | } |
| 175 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 176 | #ifdef __cplusplus |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #endif |