Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 6 | #include "BinaryXMLEncoder.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 7 | #include "BinaryXMLDecoder.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 8 | #include "BinaryXMLName.h" |
| 9 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 10 | ndn_Error ndn_encodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLEncoder *encoder) |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 11 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 12 | ndn_Error error; |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 13 | if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Name)) |
| 14 | return error; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 15 | |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 16 | unsigned int i; |
| 17 | for (i = 0; i < name->nComponents; ++i) { |
| 18 | if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement |
| 19 | (encoder, ndn_BinaryXML_DTag_Component, name->components[i].value, name->components[i].valueLength)) |
| 20 | return error; |
| 21 | } |
| 22 | |
| 23 | if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder)) |
| 24 | return error; |
| 25 | |
| 26 | return 0; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 29 | ndn_Error ndn_decodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLDecoder *decoder) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 30 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 31 | ndn_Error error; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 32 | if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Name)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 33 | return error; |
| 34 | |
| 35 | while (1) { |
| 36 | int gotExpectedTag; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 37 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 38 | return error; |
| 39 | |
| 40 | if (!gotExpectedTag) |
| 41 | // No more components. |
| 42 | break; |
| 43 | |
| 44 | unsigned char *component; |
| 45 | unsigned int componentLen; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 46 | 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] | 47 | return error; |
| 48 | |
| 49 | // Add the component to the name. |
| 50 | if (name->nComponents >= name->maxComponents) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame^] | 51 | return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 52 | ndn_NameComponent_init(name->components + name->nComponents, component, componentLen); |
| 53 | ++name->nComponents; |
| 54 | } |
| 55 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 56 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 57 | return error; |
| 58 | |
| 59 | return 0; |
| 60 | } |