Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
Jeff Thompson | d6e8fa1 | 2013-07-08 15:37:22 -0700 | [diff] [blame] | 3 | * Derived from Name.js by Meki Cheraoui. |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 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 | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 11 | ndn_Error ndn_encodeBinaryXMLName(struct ndn_Name *name, struct ndn_BinaryXMLEncoder *encoder) |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 12 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 13 | ndn_Error error; |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 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 | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 30 | ndn_Error 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 | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 32 | ndn_Error 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 | |
Jeff Thompson | 6874332 | 2013-07-09 12:19:58 -0700 | [diff] [blame] | 36 | name->nComponents = 0; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 37 | while (1) { |
| 38 | int gotExpectedTag; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 39 | if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 40 | return error; |
| 41 | |
| 42 | if (!gotExpectedTag) |
| 43 | // No more components. |
| 44 | break; |
| 45 | |
| 46 | unsigned char *component; |
| 47 | unsigned int componentLen; |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 48 | 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] | 49 | return error; |
| 50 | |
| 51 | // Add the component to the name. |
| 52 | if (name->nComponents >= name->maxComponents) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 53 | 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] | 54 | ndn_NameComponent_init(name->components + name->nComponents, component, componentLen); |
| 55 | ++name->nComponents; |
| 56 | } |
| 57 | |
Jeff Thompson | e2c232d | 2013-07-03 18:47:29 -0700 | [diff] [blame] | 58 | if (error = ndn_BinaryXMLDecoder_readElementClose(decoder)) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 59 | return error; |
| 60 | |
| 61 | return 0; |
| 62 | } |