Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BINARYXMLDECODER_H |
| 8 | #define NDN_BINARYXMLDECODER_H |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | struct ndn_BinaryXMLDecoder { |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 15 | unsigned char *input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 16 | unsigned int inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 17 | unsigned int offset; |
| 18 | }; |
| 19 | |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 20 | 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] | 21 | { |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 22 | self->input = input; |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 23 | self->inputLength = inputLength; |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 24 | self->offset = 0; |
| 25 | } |
| 26 | |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 27 | /** |
| 28 | * 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] | 29 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 30 | * @param type output for the header type |
| 31 | * @param value output for the header value |
Jeff Thompson | 38cbd57 | 2013-06-28 14:01:10 -0700 | [diff] [blame] | 32 | * @return 0 for success, else an error string 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] | 33 | */ |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 34 | char *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] | 35 | |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 36 | /** |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 37 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 38 | * Update offset. |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 39 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 40 | * @param expectedTag the expected value for DTAG |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 41 | * @return 0 for success, else an error string, including an error if not the expected tag |
| 42 | */ |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 43 | char *ndn_BinaryXMLDecoder_readElementStartDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag); |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Read one byte from self's input starting at offset, expecting it to be the element close. |
| 47 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 48 | * @return 0 for success, else an error string, including an error if not the element close |
| 49 | */ |
| 50 | char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self); |
| 51 | |
| 52 | /** |
| 53 | * Decode the header from self's input starting at offset, and if it is a DTAG where the value is the expectedTag, |
| 54 | * then set gotExpectedTag to 1, else 0. Do not update offset, including if returning an error. |
| 55 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 56 | * @param expectedTag the expected value for DTAG |
| 57 | * @param gotExpectedTag output a 1 if got the expected tag, else 0 |
| 58 | * @return 0 for success, else an error string for read past the end of the input |
| 59 | */ |
| 60 | char *ndn_BinaryXMLDecoder_peekDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *gotExpectedTag); |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Jeff Thompson | b4ee400 | 2013-06-28 13:41:43 -0700 | [diff] [blame] | 63 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 64 | * Then read one item of any type (presumably BLOB, UDATA, TAG or ATTR) and return the item's value and length. |
| 65 | * However, if allowNull is 1, then the item may be absent. |
| 66 | * Finally, read the element close. Update offset. |
| 67 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 68 | * @param expectedTag the expected value for DTAG |
| 69 | * @param allowNull 1 if the binary item may be missing |
| 70 | * @param value output a pointer to the binary data inside self's input buffer. However, if allowNull is 1 and the |
| 71 | * binary data item is absent, then return 0. |
| 72 | * @param valueLen output the length of the binary data. However, if allowNull is 1 and the |
| 73 | * binary data item is absent, then return 0. |
| 74 | * @return 0 for success, else an error string, including an error if not the expected tag, or if allowNull is 0 |
| 75 | * and the binary data is absent |
| 76 | */ |
| 77 | char *ndn_BinaryXMLDecoder_readBinaryDTagElement |
| 78 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int allowNull, unsigned char **value, unsigned int *valueLen); |
| 79 | |
| 80 | /** |
Jeff Thompson | 167ca5a | 2013-07-07 20:45:43 -0700 | [diff] [blame] | 81 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 82 | * Then read one item expecting it to be type UDATA, and return the item's value and length. |
| 83 | * Finally, read the element close. Update offset. |
| 84 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 85 | * @param expectedTag the expected value for DTAG |
| 86 | * @param value output a pointer to the binary data inside self's input buffer. |
| 87 | * @param valueLen output the length of the binary data. |
| 88 | * @return 0 for success, else an error string, including an error if not the expected tag, or if the item is not UDATA. |
| 89 | */ |
| 90 | char *ndn_BinaryXMLDecoder_readUDataDTagElement |
| 91 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned char **value, unsigned int *valueLen); |
| 92 | |
| 93 | /** |
| 94 | * Decode the header from self's input starting at offset, expecting the type to be DTAG and the value to be expectedTag. |
| 95 | * Then read one item expecting it to be type UDATA, parse it as an unsigned decimal integer and return the integer. |
| 96 | * Finally, read the element close. Update offset. |
| 97 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 98 | * @param expectedTag the expected value for DTAG |
| 99 | * @param value output the unsigned integer |
| 100 | * @return 0 for success, else an error string, including an error if not the expected tag, or if the item is not UDATA, |
| 101 | * or can't parse the integer |
| 102 | */ |
| 103 | char *ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement |
| 104 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, unsigned int *value); |
| 105 | |
| 106 | /** |
| 107 | * Peek at the next element, and if it has the expectedTag then call ndn_BinaryXMLDecoder_readUnsignedIntegerDTagElement. |
| 108 | * Otherwise, set value to -1. |
| 109 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 110 | * @param expectedTag the expected value for DTAG |
| 111 | * @param value output the unsigned integer cast to int, or -1 if the next element doesn't have expectedTag. |
| 112 | * @return 0 for success, else an error string, including an error if the item is not UDATA, |
| 113 | * or can't parse the integer |
| 114 | */ |
| 115 | char *ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement |
| 116 | (struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag, int *value); |
| 117 | |
| 118 | /** |
Jeff Thompson | 5abaaca | 2013-07-07 21:16:04 -0700 | [diff] [blame] | 119 | * Convert the big endian bytes to an unsigned int. Don't check for overflow. |
| 120 | * @param bytes pointer to the array of bytes |
| 121 | * @param bytesLength the length of bytes |
| 122 | * @return the result unsigned int |
| 123 | */ |
| 124 | unsigned int ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(unsigned char *bytes, unsigned int bytesLength); |
| 125 | |
| 126 | /** |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 127 | * Set the offset into the input, used for the next read. |
| 128 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 129 | * @param offset the new offset |
| 130 | */ |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 131 | static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset) |
| 132 | { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 133 | self->offset = offset; |
| 134 | } |
| 135 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 136 | #ifdef __cplusplus |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #endif |