blob: 81fa6b91bceba968c8647df0626396fffa781152 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonc8963652013-06-28 20:17:43 -07005 */
6
7#ifndef NDN_BINARYXMLENCODER_H
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07008#define NDN_BINARYXMLENCODER_H
Jeff Thompsonc8963652013-06-28 20:17:43 -07009
Jeff Thompson8b666002013-07-08 01:16:26 -070010#include "../errors.h"
Jeff Thompson10ad12a2013-09-24 16:19:11 -070011#include "../util/dynamic-uint8-array.h"
Jeff Thompson93034532013-10-08 11:52:43 -070012#include "../util/blob.h"
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "binary-xml.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -070014
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070015#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -070016extern "C" {
17#endif
18
Jeff Thompsonf0fea002013-07-30 17:22:42 -070019/** An ndn_BinaryXmlEncoder struct is used by all the encoding functions. You should initialize it with
Jeff Thompsond1427fb2013-08-29 17:20:32 -070020 * ndn_BinaryXmlEncoder_initialize.
Jeff Thompson5a984832013-07-01 19:28:27 -070021 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070022struct ndn_BinaryXmlEncoder {
Jeff Thompson10ad12a2013-09-24 16:19:11 -070023 struct ndn_DynamicUInt8Array *output; /**< A pointer to a ndn_DynamicUInt8Array which receives the encoded output */
Jeff Thompson97223af2013-09-24 17:01:27 -070024 size_t offset; /**< the offset into output.array for the next encoding */
Jeff Thompsonc8963652013-06-28 20:17:43 -070025};
26
27/**
Jeff Thompson10ad12a2013-09-24 16:19:11 -070028 * Initialize an ndn_BinaryXmlEncoder_initialize struct with the arguments for initializing the ndn_DynamicUInt8Array.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070029 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson10ad12a2013-09-24 16:19:11 -070030 * @param output A pointer to a ndn_DynamicUInt8Array struct which receives the encoded output. The struct must
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070031 * remain valid during the entire life of this ndn_BinaryXmlEncoder. If the output->realloc
32 * function pointer is null, its array must be large enough to receive the entire encoding.
Jeff Thompsonc8963652013-06-28 20:17:43 -070033 */
Jeff Thompson10ad12a2013-09-24 16:19:11 -070034static inline void ndn_BinaryXmlEncoder_initialize(struct ndn_BinaryXmlEncoder *self, struct ndn_DynamicUInt8Array *output)
Jeff Thompsonc8963652013-06-28 20:17:43 -070035{
Jeff Thompsonc978a7b2013-08-12 13:17:17 -070036 self->output = output;
Jeff Thompsonc8963652013-06-28 20:17:43 -070037 self->offset = 0;
38}
39
Jeff Thompson5a984832013-07-01 19:28:27 -070040/**
41 * Encode a header with the type and value and write it to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070042 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070043 * @param type the header type
44 * @param value the header value
Jeff Thompson8b666002013-07-08 01:16:26 -070045 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070046 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070047ndn_Error ndn_BinaryXmlEncoder_encodeTypeAndValue(struct ndn_BinaryXmlEncoder *self, unsigned int type, unsigned int value);
Jeff Thompson433e6da2013-07-01 15:09:00 -070048
Jeff Thompson5a984832013-07-01 19:28:27 -070049/**
50 * Write an element start header using DTAG with the tag to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070051 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070052 * @param tag the DTAG tag
Jeff Thompson8b666002013-07-08 01:16:26 -070053 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070054 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070055static inline ndn_Error ndn_BinaryXmlEncoder_writeElementStartDTag(struct ndn_BinaryXmlEncoder *self, unsigned int tag)
Jeff Thompson5a984832013-07-01 19:28:27 -070056{
Jeff Thompsonf0fea002013-07-30 17:22:42 -070057 return ndn_BinaryXmlEncoder_encodeTypeAndValue(self, ndn_BinaryXml_DTAG, tag);
Jeff Thompson5a984832013-07-01 19:28:27 -070058}
59
60/**
61 * Write an element close to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070062 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson8b666002013-07-08 01:16:26 -070063 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070064 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070065ndn_Error ndn_BinaryXmlEncoder_writeElementClose(struct ndn_BinaryXmlEncoder *self);
Jeff Thompson5a984832013-07-01 19:28:27 -070066
67/**
68 * Write a BLOB header, then the bytes of the blob value to self->output.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson93034532013-10-08 11:52:43 -070070 * @param value A Blob with the array of bytes for the value.
Jeff Thompson8b666002013-07-08 01:16:26 -070071 * @return 0 for success, else an error code
Jeff Thompson5a984832013-07-01 19:28:27 -070072 */
Jeff Thompson93034532013-10-08 11:52:43 -070073ndn_Error ndn_BinaryXmlEncoder_writeBlob(struct ndn_BinaryXmlEncoder *self, struct ndn_Blob *value);
Jeff Thompson5a984832013-07-01 19:28:27 -070074
75/**
76 * 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 -070077 * (If you want to just write the blob, use ndn_BinaryXmlEncoder_writeBlob .)
78 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5a984832013-07-01 19:28:27 -070079 * @param tag the DTAG tag
Jeff Thompson93034532013-10-08 11:52:43 -070080 * @param value A Blob with the array of bytes for the value.
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 Thompson93034532013-10-08 11:52:43 -070083ndn_Error ndn_BinaryXmlEncoder_writeBlobDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, struct ndn_Blob *value);
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.
Jeff Thompson93034532013-10-08 11:52:43 -070087 * @param self A pointer to the ndn_BinaryXmlEncoder struct.
88 * @param tag The DTAG tag.
89 * @param value A Blob with the array of bytes for the value.
Jeff Thompsona8749a52013-07-11 11:48:05 -070090 * @return 0 for success, else an error code
91 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -070092static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070093 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, struct ndn_Blob *value)
Jeff Thompsona8749a52013-07-11 11:48:05 -070094{
Jeff Thompson93034532013-10-08 11:52:43 -070095 if (value->value && value->length > 0)
96 return ndn_BinaryXmlEncoder_writeBlobDTagElement(self, tag, value);
Jeff Thompsona8749a52013-07-11 11:48:05 -070097 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -070098 return NDN_ERROR_success;
Jeff Thompsona8749a52013-07-11 11:48:05 -070099}
100
101/**
Jeff Thompson214c26f2013-08-27 11:45:01 -0700102 * Write a UDATA header, then the bytes of the UDATA value to self->output.
103 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson93034532013-10-08 11:52:43 -0700104 * @param value A Blob with the array of bytes for the value.
Jeff Thompson214c26f2013-08-27 11:45:01 -0700105 * @return 0 for success, else an error code
106 */
Jeff Thompson93034532013-10-08 11:52:43 -0700107ndn_Error ndn_BinaryXmlEncoder_writeUData(struct ndn_BinaryXmlEncoder *self, struct ndn_Blob *value);
Jeff Thompson214c26f2013-08-27 11:45:01 -0700108
109/**
110 * Write an element start header using DTAG with the tag to self->output, then the UDATA value, then an element close.
111 * (If you want to just write the UDATA value, use ndn_BinaryXmlEncoder_writeUData .)
112 * @param self pointer to the ndn_BinaryXmlEncoder struct
113 * @param tag the DTAG tag
Jeff Thompson93034532013-10-08 11:52:43 -0700114 * @param value A Blob with the array of bytes for the value.
Jeff Thompson214c26f2013-08-27 11:45:01 -0700115 * @return 0 for success, else an error code
116 */
Jeff Thompson93034532013-10-08 11:52:43 -0700117ndn_Error ndn_BinaryXmlEncoder_writeUDataDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, struct ndn_Blob *value);
Jeff Thompson214c26f2013-08-27 11:45:01 -0700118
119/**
120 * If value or valueLen is 0 then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUDataDTagElement.
121 * @param self pointer to the ndn_BinaryXmlEncoder struct
122 * @param tag the DTAG tag
Jeff Thompson93034532013-10-08 11:52:43 -0700123 * @param value A Blob with the array of bytes for the value.
Jeff Thompson214c26f2013-08-27 11:45:01 -0700124 * @return 0 for success, else an error code
125 */
126static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700127 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, struct ndn_Blob *value)
Jeff Thompson214c26f2013-08-27 11:45:01 -0700128{
Jeff Thompson93034532013-10-08 11:52:43 -0700129 if (value->value && value->length > 0)
130 return ndn_BinaryXmlEncoder_writeUDataDTagElement(self, tag, value);
Jeff Thompson214c26f2013-08-27 11:45:01 -0700131 else
132 return NDN_ERROR_success;
133}
134
135/**
Jeff Thompson5b696e02013-07-08 15:04:22 -0700136 * Write a UDATA header, then the value as an unsigned decimal integer.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700137 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsone2276892013-07-08 02:44:18 -0700138 * @param value the unsigned int
139 * @return 0 for success, else an error code
140 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700141ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalInt(struct ndn_BinaryXmlEncoder *self, unsigned int value);
Jeff Thompsone2276892013-07-08 02:44:18 -0700142
Jeff Thompson5b696e02013-07-08 15:04:22 -0700143/**
144 * Write an element start header using DTAG with the tag to self->output, then the value as an unsigned decimal integer,
145 * then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700146 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
147 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson5b696e02013-07-08 15:04:22 -0700148 * @param tag the DTAG tag
149 * @param value the unsigned int
150 * @return 0 for success, else an error code
151 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700152ndn_Error ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, unsigned int value);
Jeff Thompson5b696e02013-07-08 15:04:22 -0700153
Jeff Thompsona259cc42013-07-08 17:14:09 -0700154/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700155 * If value is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement.
156 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompson45492a72013-07-11 12:02:47 -0700157 * @param tag the DTAG tag
158 * @param value negative for none, otherwise use (unsigned int)value
159 * @return 0 for success, else an error code
160 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700161static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, int value)
Jeff Thompson45492a72013-07-11 12:02:47 -0700162{
163 if (value >= 0)
Jeff Thompson97223af2013-09-24 17:01:27 -0700164 return ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement(self, tag, (size_t)value);
Jeff Thompson45492a72013-07-11 12:02:47 -0700165 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700166 return NDN_ERROR_success;
Jeff Thompson45492a72013-07-11 12:02:47 -0700167}
168
169/**
Jeff Thompson8f3bd992013-08-12 17:01:51 -0700170 * 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 -0700171 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700172 * @param value the double to encode as big endian. If value is 0, the big endian encoding has zero bytes.
173 * The value is converted to absolute value.
Jeff Thompsona259cc42013-07-08 17:14:09 -0700174 * @return 0 for success, else an error code
175 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700176ndn_Error ndn_BinaryXmlEncoder_writeAbsDoubleBigEndianBlob(struct ndn_BinaryXmlEncoder *self, double value);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700177
178/**
179 * Write an element start header using DTAG with the tag to self->output, then the absolute value of milliseconds
180 * as a big endian BLOB converted to 4096 ticks per second, then an element close.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700181 * (If you want to just write the integer, use ndn_BinaryXmlEncoder_writeUnsignedDecimalInt .)
182 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700183 * @param tag the DTAG tag
184 * @param milliseconds the the number of milliseconds
185 * @return 0 for success, else an error code
186 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700187ndn_Error ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700188
189/**
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700190 * If milliseconds is negative then do nothing, otherwise call ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement.
191 * @param self pointer to the ndn_BinaryXmlEncoder struct
Jeff Thompsonedc22252013-07-11 18:05:44 -0700192 * @param tag the DTAG tag
193 * @param milliseconds negative for none, otherwise the number of milliseconds
194 * @return 0 for success, else an error code
195 */
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700196static inline ndn_Error ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
197 (struct ndn_BinaryXmlEncoder *self, unsigned int tag, double milliseconds)
Jeff Thompsonedc22252013-07-11 18:05:44 -0700198{
199 if (milliseconds >= 0)
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700200 return ndn_BinaryXmlEncoder_writeTimeMillisecondsDTagElement(self, tag, milliseconds);
Jeff Thompsonedc22252013-07-11 18:05:44 -0700201 else
Jeff Thompsonf2349af2013-08-08 14:05:37 -0700202 return NDN_ERROR_success;
Jeff Thompsonedc22252013-07-11 18:05:44 -0700203}
Jeff Thompsona259cc42013-07-08 17:14:09 -0700204
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700205#ifdef __cplusplus
Jeff Thompsonc8963652013-06-28 20:17:43 -0700206}
207#endif
208
209#endif
210