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