blob: 147d5831769138d1911df8941cda127327fb287c [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
Jeff Thompsone7ab9442013-12-10 19:34:06 -08007#include <string.h>
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -07008#include "util/ndn_memory.h"
9#include "name.h"
10
Jeff Thompson27cae532013-10-08 12:52:41 -070011uint64_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
23ndn_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 Thompsonf1ba9bd2013-08-12 17:43:08 -070039int ndn_Name_match(struct ndn_Name *self, struct ndn_Name *name)
40{
Jeff Thompsone589c3f2013-10-12 17:30:50 -070041 // This name is longer than the name we are checking it against.
42 if (self->nComponents > name->nComponents)
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070043 return 0;
44
Jeff Thompsone589c3f2013-10-12 17:30:50 -070045 // Check if at least one of given components doesn't match.
Jeff Thompson97223af2013-09-24 17:01:27 -070046 size_t i;
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070047 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 Thompson93034532013-10-08 11:52:43 -070051 if (selfComponent->value.length != nameComponent->value.length ||
52 ndn_memcmp(selfComponent->value.value, nameComponent->value.value, selfComponent->value.length) != 0)
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070053 return 0;
54 }
55
Jeff Thompsone589c3f2013-10-12 17:30:50 -070056 return 1;
Jeff Thompsonf1ba9bd2013-08-12 17:43:08 -070057}
Jeff Thompsone7ab9442013-12-10 19:34:06 -080058
59ndn_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
69ndn_Error ndn_Name_appendString(struct ndn_Name *self, char* value)
70{
71 return ndn_Name_appendComponent(self, (uint8_t*)value, strlen(value));
72}