blob: c6f55f727cc25551cf626bd1aa05b567f4bd01ea [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 Thompson1f3f5172013-07-01 19:02:36 -070018 unsigned int i;
19 for (i = 0; i < name->nComponents; ++i) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070020 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
21 (encoder, ndn_BinaryXml_DTag_Component, name->components[i].value, name->components[i].valueLength)))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070022 return error;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070023 }
Jeff Thompson1f3f5172013-07-01 19:02:36 -070024
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070025 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson1f3f5172013-07-01 19:02:36 -070026 return error;
27
Jeff Thompsonadaf9232013-08-08 14:30:29 -070028 return NDN_ERROR_success;
Jeff Thompson1156ed12013-07-01 16:13:56 -070029}
30
Jeff Thompsonf0fea002013-07-30 17:22:42 -070031ndn_Error ndn_decodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson333c4462013-06-28 14:04:22 -070032{
Jeff Thompson8b666002013-07-08 01:16:26 -070033 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070034 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Name)))
Jeff Thompson333c4462013-06-28 14:04:22 -070035 return error;
36
Jeff Thompson68743322013-07-09 12:19:58 -070037 name->nComponents = 0;
Jeff Thompson333c4462013-06-28 14:04:22 -070038 while (1) {
39 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -070040 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag)))
Jeff Thompson333c4462013-06-28 14:04:22 -070041 return error;
42
43 if (!gotExpectedTag)
44 // No more components.
45 break;
46
47 unsigned char *component;
48 unsigned int componentLen;
Jeff Thompson94ddc272013-08-08 14:17:38 -070049 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component, &componentLen)))
Jeff Thompson333c4462013-06-28 14:04:22 -070050 return error;
51
52 // Add the component to the name.
53 if (name->nComponents >= name->maxComponents)
Jeff Thompson8b666002013-07-08 01:16:26 -070054 return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070055 ndn_NameComponent_initialize(name->components + name->nComponents, component, componentLen);
Jeff Thompson333c4462013-06-28 14:04:22 -070056 ++name->nComponents;
57 }
58
Jeff Thompson94ddc272013-08-08 14:17:38 -070059 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson333c4462013-06-28 14:04:22 -070060 return error;
61
Jeff Thompsonadaf9232013-08-08 14:30:29 -070062 return NDN_ERROR_success;
Jeff Thompson333c4462013-06-28 14:04:22 -070063}