blob: 4e3f4d7ac294d7b9e880412e0e2c8231121d412c [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 Thompson333c4462013-06-28 14:04:22 -070030char *ndn_decodeBinaryXMLName(struct ndn_Name *name, unsigned char *input, unsigned int inputLength)
31{
32 struct ndn_BinaryXMLDecoder decoder;
33 ndn_BinaryXMLDecoder_init(&decoder, input, inputLength);
34
35 char *error;
Jeff Thompson1156ed12013-07-01 16:13:56 -070036 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(&decoder, ndn_BinaryXML_DTag_Name))
Jeff Thompson333c4462013-06-28 14:04:22 -070037 return error;
38
39 while (1) {
40 int gotExpectedTag;
41 if (error = ndn_BinaryXMLDecoder_peekDTag(&decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
42 return error;
43
44 if (!gotExpectedTag)
45 // No more components.
46 break;
47
48 unsigned char *component;
49 unsigned int componentLen;
50 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(&decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
51 return error;
52
53 // Add the component to the name.
54 if (name->nComponents >= name->maxComponents)
55 return "ndn_decodeBinaryXMLName: read a component past the maximum number of components allowed in the name";
56 ndn_NameComponent_init(name->components + name->nComponents, component, componentLen);
57 ++name->nComponents;
58 }
59
60 if (error = ndn_BinaryXMLDecoder_readElementClose(&decoder))
61 return error;
62
63 return 0;
64}