blob: f75c1ea3b5fecdaec0d2168c5fdd20a8757da3ab [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 Thompson1f3f5172013-07-01 19:02:36 -07007#include "BinaryXMLEncoder.h"
Jeff Thompson333c4462013-06-28 14:04:22 -07008#include "BinaryXMLDecoder.h"
Jeff Thompson333c4462013-06-28 14:04:22 -07009#include "BinaryXMLName.h"
10
Jeff Thompson8b666002013-07-08 01:16:26 -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 Thompson1f3f5172013-07-01 19:02:36 -070014 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Name))
15 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) {
19 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
20 (encoder, ndn_BinaryXML_DTag_Component, name->components[i].value, name->components[i].valueLength))
21 return error;
22 }
23
24 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
25 return error;
26
27 return 0;
Jeff Thompson1156ed12013-07-01 16:13:56 -070028}
29
Jeff Thompson8b666002013-07-08 01:16:26 -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 Thompsone2c232d2013-07-03 18:47:29 -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 Thompsone2c232d2013-07-03 18:47:29 -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 Thompsone2c232d2013-07-03 18:47:29 -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 Thompsone2c232d2013-07-03 18:47:29 -070058 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
Jeff Thompson333c4462013-06-28 14:04:22 -070059 return error;
60
61 return 0;
62}