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 | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #include "binary-xml-encoder.h" |
| 8 | #include "binary-xml-decoder.h" |
| 9 | #include "binary-xml-name.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -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 | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 14 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Name))) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 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) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 19 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 20 | (encoder, ndn_BinaryXml_DTag_Component, name->components[i].value, name->components[i].valueLength))) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 21 | return error; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 22 | } |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 23 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 24 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 25 | return error; |
| 26 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 27 | return NDN_ERROR_success; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -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 | 94ddc27 | 2013-08-08 14:17:38 -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 | 94ddc27 | 2013-08-08 14:17:38 -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 | 94ddc27 | 2013-08-08 14:17:38 -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 | d1427fb | 2013-08-29 17:20:32 -0700 | [diff] [blame] | 54 | ndn_NameComponent_initialize(name->components + name->nComponents, component, componentLen); |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 55 | ++name->nComponents; |
| 56 | } |
| 57 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -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 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 61 | return NDN_ERROR_success; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 62 | } |