Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | d6e8fa1 | 2013-07-08 15:37:22 -0700 | [diff] [blame] | 4 | * Derived from Name.js by Meki Cheraoui. |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 8 | #include "binary-xml-encoder.h" |
| 9 | #include "binary-xml-decoder.h" |
| 10 | #include "binary-xml-name.h" |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 11 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 12 | ndn_Error ndn_encodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 13 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 14 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 15 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Name))) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 16 | return error; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 17 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 18 | size_t i; |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 19 | for (i = 0; i < name->nComponents; ++i) { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 20 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Component, &name->components[i].value))) |
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 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 46 | struct ndn_Blob component; |
| 47 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component))) |
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) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 52 | return NDN_ERROR_read_a_component_past_the_maximum_number_of_components_allowed_in_the_name; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 53 | ndn_NameComponent_initialize(name->components + name->nComponents, component.value, component.length); |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 54 | ++name->nComponents; |
| 55 | } |
| 56 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -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 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 60 | return NDN_ERROR_success; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 61 | } |