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 | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef NDN_NAME_H |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 8 | #define NDN_NAME_H |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 10 | #include <ndn-cpp/c/common.h> |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 11 | #include "errors.h" |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 12 | #include "util/blob.h" |
| 13 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 14 | #ifdef __cplusplus |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 15 | extern "C" { |
| 16 | #endif |
| 17 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 18 | /** |
| 19 | * An ndn_NameComponent holds a pointer to the component value. |
| 20 | */ |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 21 | struct ndn_NameComponent { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 22 | struct ndn_Blob value; /**< A Blob with a pointer to the pre-allocated buffer for the component value */ |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 25 | /** |
| 26 | * |
| 27 | * @param self pointer to the ndn_NameComponent struct |
| 28 | * @param value the pre-allocated buffer for the component value |
| 29 | * @param valueLength the number of bytes in value |
| 30 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 31 | static inline void ndn_NameComponent_initialize(struct ndn_NameComponent *self, uint8_t *value, size_t valueLength) |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 32 | { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 33 | ndn_Blob_initialize(&self->value, value, valueLength); |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 34 | } |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 37 | * Interpret the name component as a network-ordered number and return an integer. |
| 38 | * @param self A pointer to the ndn_NameComponent struct. |
| 39 | * @return The integer number. |
| 40 | */ |
| 41 | uint64_t ndn_NameComponent_toNumber(struct ndn_NameComponent *self); |
| 42 | |
| 43 | /** |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 44 | * Interpret the name component as a network-ordered number with a marker and return an integer. |
| 45 | * @param self A pointer to the ndn_NameComponent struct. |
| 46 | * @param marker The required first byte of the component. |
| 47 | * @param result Return the integer number. |
| 48 | * @return 0 for success, or an error code if the first byte of the component does not equal the marker. |
| 49 | */ |
| 50 | ndn_Error ndn_NameComponent_toNumberWithMarker(struct ndn_NameComponent *self, uint8_t marker, uint64_t *result); |
| 51 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 52 | /** |
| 53 | * An ndn_Name holds an array of ndn_NameComponent. |
| 54 | */ |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 55 | struct ndn_Name { |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 56 | struct ndn_NameComponent *components; /**< pointer to the array of components. */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 57 | size_t maxComponents; /**< the number of elements in the allocated components array */ |
| 58 | size_t nComponents; /**< the number of components in the name */ |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Initialize an ndn_Name struct with the components array. |
| 63 | * @param self pointer to the ndn_Name struct |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 64 | * @param components the pre-allocated array of ndn_NameComponent |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 65 | * @param maxComponents the number of elements in the allocated components array |
| 66 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 67 | static inline void ndn_Name_initialize(struct ndn_Name *self, struct ndn_NameComponent *components, size_t maxComponents) |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 68 | { |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 69 | self->components = components; |
| 70 | self->maxComponents = maxComponents; |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 71 | self->nComponents = 0; |
| 72 | } |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Return true if the N components of this name are the same as the first N components of the given name. |
| 76 | * @param self A pointer to the ndn_Name struct. |
| 77 | * @param name A pointer to the other name to match. |
| 78 | * @return 1 if this matches the given name, 0 otherwise. This always returns 1 if this name is empty. |
| 79 | */ |
| 80 | int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name); |
| 81 | |
Jeff Thompson | e7ab944 | 2013-12-10 19:34:06 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Append a component to this name with the bytes in the given array. |
| 84 | * @param self pointer to the ndn_Name struct. |
| 85 | * @param value The bytes of the component. This does not copy the bytes. |
| 86 | * @param valueLength The number of bytes in value. |
| 87 | * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents). |
| 88 | */ |
| 89 | ndn_Error ndn_Name_appendComponent(struct ndn_Name *self, uint8_t* value, size_t valueLength); |
| 90 | |
| 91 | /** |
| 92 | * Append a component to this name with the bytes in the given blob. |
| 93 | * @param self pointer to the ndn_Name struct. |
| 94 | * @param value An ndn_Blob with the bytes of the component. This does not copy the bytes. |
| 95 | * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents). |
| 96 | */ |
| 97 | static inline ndn_Error ndn_Name_appendBlob(struct ndn_Name *self, struct ndn_Blob *value) |
| 98 | { |
| 99 | return ndn_Name_appendComponent(self, value->value, value->length); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Append a component to this name with the bytes in raw string value. |
| 104 | * @param self pointer to the ndn_Name struct. |
| 105 | * @param value The null-terminated string, treated as a byte array. This does not copy the bytes. |
| 106 | * @return 0 for success, or an error code if there is no more room in the components array (nComponents is already maxComponents). |
| 107 | */ |
| 108 | ndn_Error ndn_Name_appendString(struct ndn_Name *self, char* value); |
| 109 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 110 | #ifdef __cplusplus |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 111 | } |
| 112 | #endif |
| 113 | |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 114 | #endif |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 115 | |