blob: 30cd60f3bd75f94439320639208d0a3fd7b303f8 [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 Thompsond6e8fa12013-07-08 15:37:22 -07004 * Derived from Name.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompson333c4462013-06-28 14:04:22 -07006 */
7
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "binary-xml-encoder.h"
9#include "binary-xml-decoder.h"
10#include "binary-xml-name.h"
Jeff Thompson333c4462013-06-28 14:04:22 -070011
Jeff Thompsonf0fea002013-07-30 17:22:42 -070012ndn_Error ndn_encodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson1156ed12013-07-01 16:13:56 -070013{
Jeff Thompson8b666002013-07-08 01:16:26 -070014 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070015 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Name)))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070016 return error;
Jeff Thompson1156ed12013-07-01 16:13:56 -070017
Jeff Thompson97223af2013-09-24 17:01:27 -070018 size_t i;
Jeff Thompson1f3f5172013-07-01 19:02:36 -070019 for (i = 0; i < name->nComponents; ++i) {
Jeff Thompson93034532013-10-08 11:52:43 -070020 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Component, &name->components[i].value)))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070021 return error;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070022 }
Jeff Thompson1f3f5172013-07-01 19:02:36 -070023
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070024 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070025 return error;
26
Jeff Thompsonadaf9232013-08-08 14:30:29 -070027 return NDN_ERROR_success;
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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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
Jeff Thompson93034532013-10-08 11:52:43 -070046 struct ndn_Blob component;
47 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component)))
Jeff Thompson333c4462013-06-28 14:04:22 -070048 return error;
49
50 // Add the component to the name.
51 if (name->nComponents >= name->maxComponents)
Jeff Thompson8b666002013-07-08 01:16:26 -070052 return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name;
Jeff Thompson93034532013-10-08 11:52:43 -070053 ndn_NameComponent_initialize(name->components + name->nComponents, component.value, component.length);
Jeff Thompson333c4462013-06-28 14:04:22 -070054 ++name->nComponents;
55 }
56
Jeff Thompson94ddc272013-08-08 14:17:38 -070057 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson333c4462013-06-28 14:04:22 -070058 return error;
59
Jeff Thompsonadaf9232013-08-08 14:30:29 -070060 return NDN_ERROR_success;
Jeff Thompson333c4462013-06-28 14:04:22 -070061}