blob: 78bd9b8a05630c675f6b12b6c6f65bd214813059 [file] [log] [blame]
Jeff Thompson2e0e0882013-08-12 17:55:13 -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 Thompson2e0e0882013-08-12 17:55:13 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#include "util/ndn_memory.h"
8#include "interest.h"
9
10int ndn_Exclude_compareComponents(struct ndn_NameComponent *component1, struct ndn_NameComponent *component2)
11{
12 if (component1->valueLength < component2->valueLength)
13 return -1;
14 if (component1->valueLength > component2->valueLength)
15 return 1;
16
17 // The components are equal length. Just do a byte compare.
18 return ndn_memcmp(component1->value, component2->value, component1->valueLength);
19}
Jeff Thompson3548a962013-08-12 18:23:09 -070020
21int ndn_Exclude_matches(struct ndn_Exclude *self, struct ndn_NameComponent *component)
22{
23 unsigned int i;
24 for (i = 0; i < self->nEntries; ++i) {
25 if (self->entries[i].type == ndn_Exclude_ANY) {
26 struct ndn_ExcludeEntry *lowerBound = 0;
27 if (i > 0)
28 lowerBound = self->entries + (i - 1);
29
30 // Find the upper bound, possibly skipping over multiple ANY in a row.
31 unsigned int iUpperBound;
32 struct ndn_ExcludeEntry *upperBound = 0;
33 for (iUpperBound = i + 1; iUpperBound < self->nEntries; ++iUpperBound) {
34 if (self->entries[iUpperBound].type == ndn_Exclude_COMPONENT) {
35 upperBound = self->entries + iUpperBound;
36 break;
37 }
38 }
39
40 // If lowerBound != 0, we already checked component equals lowerBound on the last pass.
41 // If upperBound != 0, we will check component equals upperBound on the next pass.
42 if (upperBound != 0) {
43 if (lowerBound != 0) {
44 if (ndn_Exclude_compareComponents(component, &lowerBound->component) > 0 &&
45 ndn_Exclude_compareComponents(component, &upperBound->component) < 0)
46 return 1;
47 }
48 else {
49 if (ndn_Exclude_compareComponents(component, &upperBound->component) < 0)
50 return 1;
51 }
52
53 // Make i equal iUpperBound on the next pass.
54 i = iUpperBound - 1;
55 }
56 else {
57 if (lowerBound != 0) {
58 if (ndn_Exclude_compareComponents(component, &lowerBound->component) > 0)
59 return 1;
60 }
61 else
62 // this.values has only ANY.
63 return 1;
64 }
65 }
66 else {
67 if (ndn_Exclude_compareComponents(component, &self->entries[i].component) == 0)
68 return 1;
69 }
70 }
71
72 return 0;
73}
Jeff Thompsonac9b2c82013-08-13 10:51:59 -070074
75int ndn_Interest_matchesName(struct ndn_Interest *self, struct ndn_Name *name)
76{
77 if (!ndn_Name_match(&self->name, name))
78 return 0;
79
80 if (self->minSuffixComponents >= 0 &&
81 // Add 1 for the implicit digest.
82 !(name->nComponents + 1 - self->name.nComponents >= self->minSuffixComponents))
83 return 0;
84 if (self->maxSuffixComponents >= 0 &&
85 // Add 1 for the implicit digest.
86 !(name->nComponents + 1 - self->name.nComponents <= self->maxSuffixComponents))
87 return 0;
88 if (self->exclude.nEntries > 0 && name->nComponents > self->name.nComponents &&
89 ndn_Exclude_matches(&self->exclude, name->components + self->name.nComponents))
90 return 0;
91
92 return 1;
93}