Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 7 | #include "BinaryXMLEncoder.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 8 | #include "BinaryXMLDecoder.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 9 | #include "BinaryXMLName.h" |
| 10 | |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 11 | char *ndn_encodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLEncoder *encoder) |
| 12 | { |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 13 | char *error; |
| 14 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Name)) |
| 15 | return error; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 16 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 17 | 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 Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 30 | char *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 Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 36 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(&decoder, ndn_BinaryXML_DTag_Name)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 37 | 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 | } |