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. |
| 29 | * Even though the first byte should not be zero, this silently ignores initial zeros. |
| 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 | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 33 | * @return 0 for success, else an error string for read past the end of the input |
Jeff Thompson | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 34 | */ |
Jeff Thompson | d6f1328 | 2013-06-27 17:31:50 -0700 | [diff] [blame] | 35 | 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] | 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 | 179d050 | 2013-06-28 11:36:00 -0700 | [diff] [blame] | 42 | * @return 0 for success, else an error string, including an error if not the expected tag |
| 43 | */ |
Jeff Thompson | 74ab081 | 2013-06-28 12:25:04 -0700 | [diff] [blame] | 44 | char *ndn_BinaryXMLDecoder_readDTag(struct ndn_BinaryXMLDecoder *self, unsigned int expectedTag); |
| 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 |
| 49 | * @return 0 for success, else an error string, including an error if not the element close |
| 50 | */ |
| 51 | char *ndn_BinaryXMLDecoder_readElementClose(struct ndn_BinaryXMLDecoder *self); |
| 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 |
| 59 | * @return 0 for success, else an error string for read past the end of the input |
| 60 | */ |
| 61 | 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] | 62 | |
| 63 | /** |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 64 | * Set the offset into the input, used for the next read. |
| 65 | * @param self pointer to the ndn_BinaryXMLDecoder struct |
| 66 | * @param offset the new offset |
| 67 | */ |
Jeff Thompson | 6c9b651 | 2013-06-27 15:59:47 -0700 | [diff] [blame] | 68 | static inline void ndn_BinaryXMLDecoder_seek(struct ndn_BinaryXMLDecoder *self, unsigned int offset) |
| 69 | { |
Jeff Thompson | f731669 | 2013-06-26 21:31:42 -0700 | [diff] [blame] | 70 | self->offset = offset; |
| 71 | } |
| 72 | |
Jeff Thompson | 76317aa | 2013-06-25 19:11:48 -0700 | [diff] [blame] | 73 | #ifdef __cplusplus |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #endif |