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 | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 18 | unsigned int i; |
| 19 | for (i = 0; i < name->nComponents; ++i) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 20 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 21 | (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] | 22 | return error; |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 23 | } |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 24 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 25 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | 1f3f517 | 2013-07-01 19:02:36 -0700 | [diff] [blame] | 26 | return error; |
| 27 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 28 | return NDN_ERROR_success; |
Jeff Thompson | 1156ed1 | 2013-07-01 16:13:56 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 31 | ndn_Error ndn_decodeBinaryXmlName(struct ndn_Name *name, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 33 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 34 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Name))) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 35 | return error; |
| 36 | |
Jeff Thompson | 6874332 | 2013-07-09 12:19:58 -0700 | [diff] [blame] | 37 | name->nComponents = 0; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 38 | while (1) { |
| 39 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 40 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag))) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 41 | return error; |
| 42 | |
| 43 | if (!gotExpectedTag) |
| 44 | // No more components. |
| 45 | break; |
| 46 | |
| 47 | unsigned char *component; |
| 48 | unsigned int componentLen; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 49 | 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] | 50 | return error; |
| 51 | |
| 52 | // Add the component to the name. |
| 53 | if (name->nComponents >= name->maxComponents) |
Jeff Thompson | 8b66600 | 2013-07-08 01:16:26 -0700 | [diff] [blame] | 54 | 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] | 55 | ndn_NameComponent_initialize(name->components + name->nComponents, component, componentLen); |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 56 | ++name->nComponents; |
| 57 | } |
| 58 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 59 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 60 | return error; |
| 61 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 62 | return NDN_ERROR_success; |
Jeff Thompson | 333c446 | 2013-06-28 14:04:22 -0700 | [diff] [blame] | 63 | } |