blob: ffdea75c070cb6ec4732258baea6d30bf90431ab [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
Jeff Thompsond6e8fa12013-07-08 15:37:22 -07003 * Derived from Name.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompson333c4462013-06-28 14:04:22 -07005 */
6
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml-encoder.h"
8#include "binary-xml-decoder.h"
9#include "binary-xml-name.h"
Jeff Thompson333c4462013-06-28 14:04:22 -070010
Jeff Thompsonf0fea002013-07-30 17:22:42 -070011ndn_Error ndn_encodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson1156ed12013-07-01 16:13:56 -070012{
Jeff Thompson8b666002013-07-08 01:16:26 -070013 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070014 if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Name))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070015 return error;
Jeff Thompson1156ed12013-07-01 16:13:56 -070016
Jeff Thompson1f3f5172013-07-01 19:02:36 -070017 unsigned int i;
18 for (i = 0; i < name->nComponents; ++i) {
Jeff Thompsonf0fea002013-07-30 17:22:42 -070019 if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement
20 (encoder, ndn_BinaryXml_DTag_Component, name->components[i].value, name->components[i].valueLength))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070021 return error;
22 }
23
Jeff Thompsonf0fea002013-07-30 17:22:42 -070024 if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070025 return error;
26
27 return 0;
Jeff Thompson1156ed12013-07-01 16:13:56 -070028}
29
Jeff Thompsonf0fea002013-07-30 17:22:42 -070030ndn_Error ndn_decodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson333c4462013-06-28 14:04:22 -070031{
Jeff Thompson8b666002013-07-08 01:16:26 -070032 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070033 if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Name))
Jeff Thompson333c4462013-06-28 14:04:22 -070034 return error;
35
Jeff Thompson68743322013-07-09 12:19:58 -070036 name->nComponents = 0;
Jeff Thompson333c4462013-06-28 14:04:22 -070037 while (1) {
38 int gotExpectedTag;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070039 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag))
Jeff Thompson333c4462013-06-28 14:04:22 -070040 return error;
41
42 if (!gotExpectedTag)
43 // No more components.
44 break;
45
46 unsigned char *component;
47 unsigned int componentLen;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070048 if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component, &componentLen))
Jeff Thompson333c4462013-06-28 14:04:22 -070049 return error;
50
51 // Add the component to the name.
52 if (name->nComponents >= name->maxComponents)
Jeff Thompson8b666002013-07-08 01:16:26 -070053 return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name;
Jeff Thompson333c4462013-06-28 14:04:22 -070054 ndn_NameComponent_init(name->components + name->nComponents, component, componentLen);
55 ++name->nComponents;
56 }
57
Jeff Thompsonf0fea002013-07-30 17:22:42 -070058 if (error = ndn_BinaryXmlDecoder_readElementClose(decoder))
Jeff Thompson333c4462013-06-28 14:04:22 -070059 return error;
60
61 return 0;
62}