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 | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame^] | 30 | char *ndn_decodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLDecoder *decoder) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 31 | { |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 32 | char *error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame^] | 33 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Name)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 34 | return error; |
| 35 | |
| 36 | while (1) { |
| 37 | int gotExpectedTag; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame^] | 38 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 39 | return error; |
| 40 | |
| 41 | if (!gotExpectedTag) |
| 42 | // No more components. |
| 43 | break; |
| 44 | |
| 45 | unsigned char *component; |
| 46 | unsigned int componentLen; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame^] | 47 | if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 48 | 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 Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame^] | 57 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 58 | return error; |
| 59 | |
| 60 | return 0; |
| 61 | } |