blob: 3f22319684d1cd8bc6fd193127c56ab06bfc90ee [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompsonc8963652013-06-28 20:17:43 -07004 */
5
6#ifndef NDN_BINARYXMLENCODER_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07007#define NDN_BINARYXMLENCODER_H
Jeff Thompsonc8963652013-06-28 20:17:43 -07008
Jeff Thompson8b666002013-07-08 01:16:26 -07009#include "../errors.h"
Jeff Thompson53412192013-08-06 13:35:50 -070010#include "../util/dynamic-uchar-array.h"
11#include "binary-xml.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070012
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070013#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -070014extern "C" {
15#endif
16
Jeff Thompsonf0fea002013-07-30 17:22:42 -070017/** An ndn_BinaryXmlEncoder struct is used by all the encoding functions. You should initialize it with
18 * ndn_BinaryXmlEncoder_init.
Jeff Thompson5a984832013-07-01 19:28:27 -070019 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070020struct ndn_BinaryXmlEncoder {
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070021 struct ndn_DynamicUCharArray *output; /**< A pointer to a ndn_DynamicUCharArray which receives the encoded output */
Jeff Thompsonc8963652013-06-28 20:17:43 -070022 unsigned int offset; /**< the offset into output.array for the next encoding */
23};
24
25/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070026 * Initialize an ndn_BinaryXmlEncoder_init struct with the arguments for initializing the ndn_DynamicUCharArray.
27 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070028 * @param output A pointer to a ndn_DynamicUCharArray struct which receives the encoded output. The struct must
29 * remain valid during the entire life of this ndn_BinaryXmlEncoder. If the output->realloc
30 * function pointer is null, its array must be large enough to receive the entire encoding.
Jeff Thompsonc8963652013-06-28 20:17:43 -070031 */
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070032static inline void ndn_BinaryXmlEncoder_init(struct ndn_BinaryXmlEncoder *self, struct ndn_DynamicUCharArray *output)
Jeff Thompsonc8963652013-06-28 20:17:43 -070033{
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070034 self->output = output;
Jeff Thompsonc8963652013-06-28 20:17:43 -070035 self->offset = 0;
36}
37
Jeff Thompson5a984832013-07-01 19:28:27 -070038/**
39 * Encode a header with the type and value and write it to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070040 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070041 * @param type the header type
42 * @param value the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070043 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070044 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070045ndn_Error ndn_BinaryXmlEncoder_encodeTypeAndValue(struct ndn_BinaryXmlEncoder *self, unsigned int type, unsigned int value);
Jeff Thompson433e6da2013-07-01 15:09:00 -070046
Jeff Thompson5a984832013-07-01 19:28:27 -070047/**
48 * Write an element start header using DTAG with the tag to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070049 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070050 * @param tag the DTAG tag
Jeff Thompson8b666002013-07-08 01:16:26 -070051 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070052 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070053static inline ndn_Error ndn_BinaryXmlEncoder_writeElementStartDTag(struct ndn_BinaryXmlEncoder *self, unsigned int tag)
Jeff Thompson5a984832013-07-01 19:28:27 -070054{
Jeff Thompsonf0fea002013-07-30 17:22:42 -070055 return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag);
Jeff Thompson5a984832013-07-01 19:28:27 -070056}
57
58/**
59 * Write an element close to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070060 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070061 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070062 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070063ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self);
Jeff Thompson5a984832013-07-01 19:28:27 -070064
65/**
66 * Write a BLOB header, then the bytes of the blob value to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070067 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070068 * @param value an array of bytes for the blob value
69 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070070 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070071 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070072ndn_Error ndn_BinaryXmlEncoder_writeBlob(struct ndn_BinaryXmlEncoder *self, unsigned char *value, unsigned int valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070073
74/**
75 * Write an element start header using DTAG with the tag to self->output, then the blob, then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070076 * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .)
77 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070078 * @param tag the DTAG tag
79 * @param value an array of bytes for the blob value
80 * @param valueLength the length of the array
Jeff Thompson8b666002013-07-08 01:16:26 -070081 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070082 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070083ndn_Error ndn_BinaryXmlEncoder_writeBlobDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength);
Jeff Thompson5a984832013-07-01 19:28:27 -070084
Jeff Thompsone2276892013-07-08 02:44:18 -070085/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -070086 * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeBlobDTagElement.
87 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsona8749a52013-07-11 11:48:05 -070088 * @param tag the DTAG tag
89 * @param value an array of bytes for the blob value
90 * @param valueLength the length of the array
91 * @return 0 for success, else an error code
92 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070093static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
94 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned char *value, unsigned int valueLength)
Jeff Thompsona8749a52013-07-11 11:48:05 -070095{
96 if (value && valueLength > 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -070097 return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value, valueLength);
Jeff Thompsona8749a52013-07-11 11:48:05 -070098 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -070099 return NDN_ERROR_success;
Jeff Thompsona8749a52013-07-11 11:48:05 -0700100}
101
102/**
Jeff Thompson5b696e02013-07-08 15:04:22 -0700103 * Write a UDATA header, then the value as an unsigned decimal integer.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700104 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsone2276892013-07-08 02:44:18 -0700105 * @param value the unsigned int
106 * @return 0 for success, else an error code
107 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700108ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value);
Jeff Thompsone2276892013-07-08 02:44:18 -0700109
Jeff Thompson5b696e02013-07-08 15:04:22 -0700110/**
111 * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer,
112 * then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700113 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
114 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5b696e02013-07-08 15:04:22 -0700115 * @param tag the DTAG tag
116 * @param value the unsigned int
117 * @return 0 for success, else an error code
118 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700119ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned int value);
Jeff Thompson5b696e02013-07-08 15:04:22 -0700120
Jeff Thompsona259cc42013-07-08 17:14:09 -0700121/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700122 * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement.
123 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson45492a72013-07-11 12:02:47 -0700124 * @param tag the DTAG tag
125 * @param value negative for none, otherwise use (unsigned int)value
126 * @return 0 for success, else an error code
127 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700128static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, int value)
Jeff Thompson45492a72013-07-11 12:02:47 -0700129{
130 if (value >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700131 return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (unsigned int)value);
Jeff Thompson45492a72013-07-11 12:02:47 -0700132 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700133 return NDN_ERROR_success;
Jeff Thompson45492a72013-07-11 12:02:47 -0700134}
135
136/**
Jeff Thompson8f3bd992013-08-12 17:01:51 -0700137 * Write a BLOB header, then the absolute value of value, rounded to an integer, to self->output encoded as big endian.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700138 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700139 * @param value the double to encode as big endian. If value is 0, the big endian encoding has zero bytes.
140 * The value is converted to absolute value.
Jeff Thompsona259cc42013-07-08 17:14:09 -0700141 * @return 0 for success, else an error code
142 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700143ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700144
145/**
146 * Write an element start header using DTAG with the tag to self->output, then the absolute value of milliseconds
147 * as a big endian BLOB converted to 4096 ticks per second, then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700148 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
149 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700150 * @param tag the DTAG tag
151 * @param milliseconds the the number of milliseconds
152 * @return 0 for success, else an error code
153 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700154ndn_Error ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700155
156/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700157 * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement.
158 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700159 * @param tag the DTAG tag
160 * @param milliseconds negative for none, otherwise the number of milliseconds
161 * @return 0 for success, else an error code
162 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700163static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
164 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds)
Jeff Thompsonedc22252013-07-11 18:05:44 -0700165{
166 if (milliseconds >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700167 return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700168 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700169 return NDN_ERROR_success;
Jeff Thompsonedc22252013-07-11 18:05:44 -0700170}
Jeff Thompsona259cc42013-07-08 17:14:09 -0700171
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700172#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -0700173}
174#endif
175
176#endif
177