blob: 44c3ff6f7c7d20f193ff6038157a58a231124faa [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{
Jeff Thompson93034532013-10-08 11:52:43 -070012 if (component1->value.length < component2->value.length)
Jeff Thompson2e0e0882013-08-12 17:55:13 -070013 return -1;
Jeff Thompson93034532013-10-08 11:52:43 -070014 if (component1->value.length > component2->value.length)
Jeff Thompson2e0e0882013-08-12 17:55:13 -070015 return 1;
16
17 // The components are equal length. Just do a byte compare.
Jeff Thompson93034532013-10-08 11:52:43 -070018 return ndn_memcmp(component1->value.value, component2->value.value, component1->value.length);
Jeff Thompson2e0e0882013-08-12 17:55:13 -070019}
Jeff Thompson3548a962013-08-12 18:23:09 -070020
21int ndn_Exclude_matches(struct ndn_Exclude *self, struct ndn_NameComponent *component)
22{
Jeff Thompson97223af2013-09-24 17:01:27 -070023 size_t i;
Jeff Thompson3548a962013-08-12 18:23:09 -070024 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.
Jeff Thompson97223af2013-09-24 17:01:27 -070031 size_t iUpperBound;
Jeff Thompson3548a962013-08-12 18:23:09 -070032 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}