blob: 50aca949f177300ce97523df10009b27366b6ea9 [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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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;
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
46 unsigned char *component;
47 unsigned int componentLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -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 Thompsond1427fb2013-08-29 17:20:32 -070054 ndn_NameComponent_initialize(name->components + name->nComponents, component, componentLen);
Jeff Thompson333c4462013-06-28 14:04:22 -070055 ++name->nComponents;
56 }
57
Jeff Thompson94ddc272013-08-08 14:17:38 -070058 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson333c4462013-06-28 14:04:22 -070059 return error;
60
Jeff Thompsonadaf9232013-08-08 14:30:29 -070061 return NDN_ERROR_success;
Jeff Thompson333c4462013-06-28 14:04:22 -070062}