blob: 799e93d9f770894fe687f2e35ccc95ccc855c782 [file] [log] [blame]
Jeff Thompson333c4462013-06-28 14:04:22 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
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 Thompson1156ed12013-07-01 16:13:56 -070011char *ndn_encodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLEncoder *encoder)
12{
Jeff Thompson1f3f5172013-07-01 19:02:36 -070013 char *error;
14 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 Thompsone2c232d2013-07-03 18:47:29 -070030char *ndn_decodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLDecoder *decoder)
Jeff Thompson333c4462013-06-28 14:04:22 -070031{
Jeff Thompson333c4462013-06-28 14:04:22 -070032 char *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
36 while (1) {
37 int gotExpectedTag;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070038 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
Jeff Thompson333c4462013-06-28 14:04:22 -070039 return error;
40
41 if (!gotExpectedTag)
42 // No more components.
43 break;
44
45 unsigned char *component;
46 unsigned int componentLen;
Jeff Thompsone2c232d2013-07-03 18:47:29 -070047 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
Jeff Thompson333c4462013-06-28 14:04:22 -070048 return error;
49
50 // Add the component to the name.
51 if (name->nComponents >= name->maxComponents)
52 return "ndn_decodeBinaryXMLName: read a component past the maximum number of components allowed in the name";
53 ndn_NameComponent_init(name->components + name->nComponents, component, componentLen);
54 ++name->nComponents;
55 }
56
Jeff Thompsone2c232d2013-07-03 18:47:29 -070057 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
Jeff Thompson333c4462013-06-28 14:04:22 -070058 return error;
59
60 return 0;
61}