blob: d6864e95ca7b18e290dee6d5cd6f18f3fb5637a0 [file] [log] [blame]
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#include "util/ndn_memory.h"
8#include "name.h"
9
Jeff Thompson27cae532013-10-08 12:52:41 -070010uint64_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
22ndn_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 Thompsonf1ba9bd2013-08-12 17:43:08 -070038int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name)
39{
40 // This name is longer than the name we are checking it against.
41 if (self->nComponents > name->nComponents)
42 return 0;
43
44 // Check if at least one of given components doesn't match.
Jeff Thompson97223af2013-09-24 17:01:27 -070045 size_t i;
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070046 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 Thompson93034532013-10-08 11:52:43 -070050 if (selfComponent->value.length != nameComponent->value.length ||
51 ndn_memcmp(selfComponent->value.value, nameComponent->value.value, selfComponent->value.length) != 0)
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070052 return 0;
53 }
54
55 return 1;
56}