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 | |
| 7 | #include "util/ndn_memory.h" |
| 8 | #include "name.h" |
| 9 | |
Jeff Thompson | 27cae53 | 2013-10-08 12:52:41 -0700 | [diff] [blame] | 10 | uint64_t ndn_NameComponent_toNumber(struct ndn_NameComponent *self) |
| 11 | { |
| 12 | uint64_t result = 0; |
| 13 | size_t i; |
| 14 | for (i = 0; i < self->value.length; ++i) { |
| 15 | result *= 256; |
| 16 | result += (uint64_t)self->value.value[i]; |
| 17 | } |
| 18 | |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | ndn_Error ndn_NameComponent_toNumberWithMarker(struct ndn_NameComponent *self, uint8_t marker, uint64_t *result) |
| 23 | { |
| 24 | if (self->value.length == 0 || self->value.value[0] != marker) |
| 25 | return NDN_ERROR_Name_component_does_not_begin_with_the_expected_marker; |
| 26 | |
| 27 | uint64_t localResult = 0; |
| 28 | size_t i; |
| 29 | for (i = 1; i < self->value.length; ++i) { |
| 30 | localResult *= 256; |
| 31 | localResult += (uint64_t)self->value.value[i]; |
| 32 | } |
| 33 | |
| 34 | *result = localResult; |
| 35 | return NDN_ERROR_success; |
| 36 | } |
| 37 | |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 38 | int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name) |
| 39 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 40 | // This name is longer than the name we are checking it against. |
| 41 | if (self->nComponents > name->nComponents) |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 42 | return 0; |
| 43 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 44 | // Check if at least one of given components doesn't match. |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 45 | size_t i; |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 46 | for (i = 0; i < self->nComponents; ++i) { |
| 47 | struct ndn_NameComponent *selfComponent = self->components + i; |
| 48 | struct ndn_NameComponent *nameComponent = name->components + i; |
| 49 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 50 | if (selfComponent->value.length != nameComponent->value.length || |
| 51 | ndn_memcmp(selfComponent->value.value, nameComponent->value.value, selfComponent->value.length) != 0) |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 55 | return 1; |
Jeff Thompson | f1ba9bd | 2013-08-12 17:43:08 -0700 | [diff] [blame] | 56 | } |