blob: bc3205822e0e5659d5516daf9e75858b7694e744 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
3 * See COPYING for copyright and distribution information.
Jeff Thompson333c4462013-06-28 14:04:22 -07004 */
5
Jeff Thompson1f3f5172013-07-01 19:02:36 -07006#include "BinaryXMLEncoder.h"
Jeff Thompson333c4462013-06-28 14:04:22 -07007#include "BinaryXMLDecoder.h"
Jeff Thompson333c4462013-06-28 14:04:22 -07008#include "BinaryXMLName.h"
9
Jeff Thompson8b666002013-07-08 01:16:26 -070010ndn_Error ndn_encodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLEncoder *encoder)
Jeff Thompson1156ed12013-07-01 16:13:56 -070011{
Jeff Thompson8b666002013-07-08 01:16:26 -070012 ndn_Error error;
Jeff Thompson1f3f5172013-07-01 19:02:36 -070013 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Name))
14 return error;
Jeff Thompson1156ed12013-07-01 16:13:56 -070015
Jeff Thompson1f3f5172013-07-01 19:02:36 -070016 unsigned int i;
17 for (i = 0; i < name->nComponents; ++i) {
18 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
19 (encoder, ndn_BinaryXML_DTag_Component, name->components[i].value, name->components[i].valueLength))
20 return error;
21 }
22
23 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
24 return error;
25
26 return 0;
Jeff Thompson1156ed12013-07-01 16:13:56 -070027}
28
Jeff Thompson8b666002013-07-08 01:16:26 -070029ndn_Error ndn_decodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLDecoder *decoder)
Jeff Thompson333c4462013-06-28 14:04:22 -070030{
Jeff Thompson8b666002013-07-08 01:16:26 -070031 ndn_Error error;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070032 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Name))
Jeff Thompson333c4462013-06-28 14:04:22 -070033 return error;
34
35 while (1) {
36 int gotExpectedTag;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070037 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
Jeff Thompson333c4462013-06-28 14:04:22 -070038 return error;
39
40 if (!gotExpectedTag)
41 // No more components.
42 break;
43
44 unsigned char *component;
45 unsigned int componentLen;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070046 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
Jeff Thompson333c4462013-06-28 14:04:22 -070047 return error;
48
49 // Add the component to the name.
50 if (name->nComponents >= name->maxComponents)
Jeff Thompson8b666002013-07-08 01:16:26 -070051 return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name;
Jeff Thompson333c4462013-06-28 14:04:22 -070052 ndn_NameComponent_init(name->components + name->nComponents, component, componentLen);
53 ++name->nComponents;
54 }
55
Jeff Thompsone2c232d2013-07-03 18:47:29 -070056 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
Jeff Thompson333c4462013-06-28 14:04:22 -070057 return error;
58
59 return 0;
60}