Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -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 | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | e7ab944 | 2013-12-10 19:34:06 -0800 | [diff] [blame^] | 7 | #include <string.h> |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 8 | #include "util/ndn_memory.h" |
| 9 | #include "name.h" |
| 10 | |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 11 | uint64_t ndn_NameComponent_toNumber(struct ndn_NameComponent *self) |
| 12 | { |
| 13 | uint64_t result = 0; |
| 14 | size_t i; |
| 15 | for (i = 0; i < self->value.length; ++i) { |
| 16 | result *= 256; |
| 17 | result += (uint64_t)self->value.value[i]; |
| 18 | } |
| 19 | |
| 20 | return result; |
| 21 | } |
| 22 | |
| 23 | ndn_Error ndn_NameComponent_toNumberWithMarker(struct ndn_NameComponent *self, uint8_t marker, uint64_t *result) |
| 24 | { |
| 25 | if (self->value.length == 0 || self->value.value[0] != marker) |
| 26 | return NDN_ERROR_Name_component_does_not_begin_with_the_expected_marker; |
| 27 | |
| 28 | uint64_t localResult = 0; |
| 29 | size_t i; |
| 30 | for (i = 1; i < self->value.length; ++i) { |
| 31 | localResult *= 256; |
| 32 | localResult += (uint64_t)self->value.value[i]; |
| 33 | } |
| 34 | |
| 35 | *result = localResult; |
| 36 | return NDN_ERROR_success; |
| 37 | } |
| 38 | |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 39 | int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name) |
| 40 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 41 | // This name is longer than the name we are checking it against. |
| 42 | if (self->nComponents > name->nComponents) |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 45 | // Check if at least one of given components doesn't match. |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 46 | size_t i; |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 47 | for (i = 0; i < self->nComponents; ++i) { |
| 48 | struct ndn_NameComponent *selfComponent = self->components + i; |
| 49 | struct ndn_NameComponent *nameComponent = name->components + i; |
| 50 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 51 | if (selfComponent->value.length != nameComponent->value.length || |
| 52 | ndn_memcmp(selfComponent->value.value, nameComponent->value.value, selfComponent->value.length) != 0) |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 56 | return 1; |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 57 | } |
Jeff Thompson | e7ab944 | 2013-12-10 19:34:06 -0800 | [diff] [blame^] | 58 | |
| 59 | ndn_Error ndn_Name_appendComponent(struct ndn_Name *self, uint8_t* value, size_t valueLength) |
| 60 | { |
| 61 | if (self->nComponents >= self->maxComponents) |
| 62 | return NDN_ERROR_attempt_to_add_a_component_past_the_maximum_number_of_components_allowed_in_the_name; |
| 63 | ndn_NameComponent_initialize(self->components + self->nComponents, value, valueLength); |
| 64 | ++self->nComponents; |
| 65 | |
| 66 | return NDN_ERROR_success; |
| 67 | } |
| 68 | |
| 69 | ndn_Error ndn_Name_appendString(struct ndn_Name *self, char* value) |
| 70 | { |
| 71 | return ndn_Name_appendComponent(self, (uint8_t*)value, strlen(value)); |
| 72 | } |