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