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 | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame^] | 10 | #include "errors.h" |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 11 | #include "util/blob.h" |
| 12 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 13 | #ifdef __cplusplus |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 17 | /** |
| 18 | * An ndn_NameComponent holds a pointer to the component value. |
| 19 | */ |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 20 | struct ndn_NameComponent { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 21 | 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] | 22 | }; |
| 23 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 24 | /** |
| 25 | * |
| 26 | * @param self pointer to the ndn_NameComponent struct |
| 27 | * @param value the pre-allocated buffer for the component value |
| 28 | * @param valueLength the number of bytes in value |
| 29 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 30 | 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] | 31 | { |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 32 | ndn_Blob_initialize(&self->value, value, valueLength); |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 33 | } |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame^] | 34 | |
| 35 | /** |
| 36 | /** |
| 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 | /** |
| 44 | * Convert binary blob name component (network-ordered number) to number, using appropriate marker from the naming convention |
| 45 | * @param comp name component to be converted |
| 46 | * @param marker required marker from the naming convention |
| 47 | * |
| 48 | * If the required marker does not exist, an exception will be thrown |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * Interpret the name component as a network-ordered number with a marker and return an integer. |
| 53 | * @param self A pointer to the ndn_NameComponent struct. |
| 54 | * @param marker The required first byte of the component. |
| 55 | * @param result Return the integer number. |
| 56 | * @return 0 for success, or an error code if the first byte of the component does not equal the marker. |
| 57 | */ |
| 58 | ndn_Error ndn_NameComponent_toNumberWithMarker(struct ndn_NameComponent *self, uint8_t marker, uint64_t *result); |
| 59 | |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 60 | /** |
| 61 | * An ndn_Name holds an array of ndn_NameComponent. |
| 62 | */ |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 63 | struct ndn_Name { |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 64 | struct ndn_NameComponent *components; /**< pointer to the array of components. */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 65 | size_t maxComponents; /**< the number of elements in the allocated components array */ |
| 66 | size_t nComponents; /**< the number of components in the name */ |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Initialize an ndn_Name struct with the components array. |
| 71 | * @param self pointer to the ndn_Name struct |
Jeff Thompson | 2934a5b | 2013-07-03 17:45:53 -0700 | [diff] [blame] | 72 | * @param components the pre-allocated array of ndn_NameComponent |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 73 | * @param maxComponents the number of elements in the allocated components array |
| 74 | */ |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 75 | 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] | 76 | { |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 77 | self->components = components; |
| 78 | self->maxComponents = maxComponents; |
Jeff Thompson | b0fd64f | 2013-06-27 16:02:36 -0700 | [diff] [blame] | 79 | self->nComponents = 0; |
| 80 | } |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Return true if the N components of this name are the same as the first N components of the given name. |
| 84 | * @param self A pointer to the ndn_Name struct. |
| 85 | * @param name A pointer to the other name to match. |
| 86 | * @return 1 if this matches the given name, 0 otherwise. This always returns 1 if this name is empty. |
| 87 | */ |
| 88 | int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name); |
| 89 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 90 | #ifdef __cplusplus |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 91 | } |
| 92 | #endif |
| 93 | |
Jeff Thompson | 08d5629 | 2013-06-27 18:20:44 -0700 | [diff] [blame] | 94 | #endif |
Jeff Thompson | 5d89cb9 | 2013-06-27 15:57:15 -0700 | [diff] [blame] | 95 | |