Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_BINARYXMLENCODER_H |
| 8 | #define NDN_BINARYXMLENCODER_H |
| 9 | |
| 10 | #include "../util/DynamicUCharArray.h" |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame^] | 11 | #include "BinaryXML.h" |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 12 | |
| 13 | #ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame^] | 17 | /** An ndn_BinaryXMLEncoder struct is used by all the encoding functions. You should initialize it with |
| 18 | * ndn_BinaryXMLEncoder_init. |
| 19 | */ |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 20 | struct ndn_BinaryXMLEncoder { |
| 21 | struct ndn_DynamicUCharArray output; /**< receives the encoded output */ |
| 22 | unsigned int offset; /**< the offset into output.array for the next encoding */ |
| 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * Initialize an ndn_BinaryXMLEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray. |
| 27 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 28 | * @param outputArray the allocated array buffer to receive the encoding |
| 29 | * @param outputArrayLength the length of outputArray |
| 30 | * @param reallocFunction the realloc function used by ndn_DynamicUCharArray_ensureLength. If outputArrayLength |
| 31 | * is large enough to receive the entire encoding, this can be 0. |
| 32 | */ |
| 33 | static inline void ndn_BinaryXMLEncoder_init |
| 34 | (struct ndn_BinaryXMLEncoder *self, unsigned char *outputArray, unsigned int outputArrayLength, |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame^] | 35 | unsigned char * (*reallocFunction)(unsigned char *, unsigned int)) |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 36 | { |
| 37 | ndn_DynamicUCharArray_init(&self->output, outputArray, outputArrayLength, reallocFunction); |
| 38 | self->offset = 0; |
| 39 | } |
| 40 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame^] | 41 | /** |
| 42 | * Encode a header with the type and value and write it to self->output. |
| 43 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 44 | * @param type the header type |
| 45 | * @param value the header value |
| 46 | * @return 0 for success, else an error string |
| 47 | */ |
Jeff Thompson | 433e6da | 2013-07-01 15:09:00 -0700 | [diff] [blame] | 48 | char *ndn_BinaryXMLEncoder_encodeTypeAndValue(struct ndn_BinaryXMLEncoder *self, unsigned int type, unsigned int value); |
| 49 | |
Jeff Thompson | 5a98483 | 2013-07-01 19:28:27 -0700 | [diff] [blame^] | 50 | /** |
| 51 | * Write an element start header using DTAG with the tag to self->output. |
| 52 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 53 | * @param tag the DTAG tag |
| 54 | * @return 0 for success, else an error string |
| 55 | */ |
| 56 | static inline char *ndn_BinaryXMLEncoder_writeElementStartDTag(struct ndn_BinaryXMLEncoder *self, unsigned int tag) |
| 57 | { |
| 58 | return ndn_BinaryXMLEncoder_encodeTypeAndValue(self, ndn_BinaryXML_DTAG, tag); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Write an element close to self->output. |
| 63 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 64 | * @return 0 for success, else an error string |
| 65 | */ |
| 66 | char *ndn_BinaryXMLEncoder_writeElementClose(struct ndn_BinaryXMLEncoder *self); |
| 67 | |
| 68 | /** |
| 69 | * Write a BLOB header, then the bytes of the blob value to self->output. |
| 70 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 71 | * @param value an array of bytes for the blob value |
| 72 | * @param valueLength the length of the array |
| 73 | * @return 0 for success, else an error string |
| 74 | */ |
| 75 | char *ndn_BinaryXMLEncoder_writeBlob(struct ndn_BinaryXMLEncoder *self, unsigned char *value, unsigned int valueLength); |
| 76 | |
| 77 | /** |
| 78 | * Write an element start header using DTAG with the tag to self->output, then the blob, then an element close. |
| 79 | * (If you want to just write the blob, use ndn_BinaryXMLEncoder_writeBlob .) |
| 80 | * @param self pointer to the ndn_BinaryXMLEncoder struct |
| 81 | * @param tag the DTAG tag |
| 82 | * @param value an array of bytes for the blob value |
| 83 | * @param valueLength the length of the array |
| 84 | * @return 0 for success, else an error string |
| 85 | */ |
| 86 | char *ndn_BinaryXMLEncoder_writeBlobDTagElement(struct ndn_BinaryXMLEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength); |
| 87 | |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 88 | #ifdef __cplusplus |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #endif |
| 93 | |