]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/BoolOps.cc
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / acl / BoolOps.cc
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "acl/BoolOps.h"
11 #include "acl/Checklist.h"
12 #include "Debug.h"
13
14 /* Acl::NotNode */
15
16 Acl::NotNode::NotNode(ACL *acl)
17 {
18 assert(acl);
19 name[0] = '!';
20 strncat(&name[1], acl->name, sizeof(name)-1-1);
21 add(acl);
22 }
23
24 void
25 Acl::NotNode::parse()
26 {
27 // Not implemented: by the time an upper level parser discovers
28 // an '!' operator, there is nothing left for us to parse.
29 assert(false);
30 }
31
32 int
33 Acl::NotNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
34 {
35 assert(start == nodes.begin()); // we only have one node
36
37 if (checklist->matchChild(this, start, *start))
38 return 0; // converting match into mismatch
39
40 if (!checklist->keepMatching())
41 return -1; // suspend on async calls and stop on failures
42
43 return 1; // converting mismatch into match
44 }
45
46 char const *
47 Acl::NotNode::typeString() const
48 {
49 return "!";
50 }
51
52 ACL *
53 Acl::NotNode::clone() const
54 {
55 // Not implemented: we are not a named ACL type in squid.conf so nobody
56 // should try to create a NotNode instance by ACL type name (which is
57 // what clone() API is for -- it does not really clone anything).
58 assert(false);
59 return NULL;
60 }
61
62 SBufList
63 Acl::NotNode::dump() const
64 {
65 SBufList text;
66 text.push_back(SBuf(name));
67 return text;
68 }
69
70 /* Acl::AndNode */
71
72 char const *
73 Acl::AndNode::typeString() const
74 {
75 return "and";
76 }
77
78 ACL *
79 Acl::AndNode::clone() const
80 {
81 return new AndNode;
82 }
83
84 int
85 Acl::AndNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
86 {
87 // find the first node that does not match
88 for (Nodes::const_iterator i = start; i != nodes.end(); ++i) {
89 if (!checklist->matchChild(this, i, *i))
90 return checklist->keepMatching() ? 0 : -1;
91 }
92
93 // one and not zero on empty because in math empty product equals identity
94 return 1; // no mismatches found (i.e., all kids matched)
95 }
96
97 void
98 Acl::AndNode::parse()
99 {
100 // Not implemented: AndNode cannot be configured directly. See Acl::AllOf.
101 assert(false);
102 }
103
104 /* Acl::OrNode */
105
106 char const *
107 Acl::OrNode::typeString() const
108 {
109 return "any-of";
110 }
111
112 ACL *
113 Acl::OrNode::clone() const
114 {
115 return new OrNode;
116 }
117
118 int
119 Acl::OrNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
120 {
121 lastMatch_ = nodes.end();
122
123 // find the first node that matches, but stop if things go wrong
124 for (Nodes::const_iterator i = start; i != nodes.end(); ++i) {
125 if (checklist->matchChild(this, i, *i)) {
126 lastMatch_ = i;
127 return 1;
128 }
129
130 if (!checklist->keepMatching())
131 return -1; // suspend on async calls and stop on failures
132 }
133
134 // zero and not one on empty because in math empty sum equals zero
135 return 0; // all nodes mismatched
136 }
137
138 void
139 Acl::OrNode::parse()
140 {
141 // Not implemented: OrNode cannot be configured directly. See Acl::AnyOf.
142 assert(false);
143 }