BoolOps.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 #include "sbuf/SBuf.h"
14 
15 /* Acl::NotNode */
16 
18 {
19  assert(acl);
20  Must(strlen(acl->name) <= sizeof(name)-2);
21  name[0] = '!';
22  name[1] = '\0';
23  xstrncpy(&name[1], acl->name, sizeof(name)-1); // -1 for '!'
24  add(acl);
25 }
26 
27 void
29 {
30  // Not implemented: by the time an upper level parser discovers
31  // an '!' operator, there is nothing left for us to parse.
32  assert(false);
33 }
34 
35 int
36 Acl::NotNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
37 {
38  assert(start == nodes.begin()); // we only have one node
39 
40  if (checklist->matchChild(this, start, *start))
41  return 0; // converting match into mismatch
42 
43  if (!checklist->keepMatching())
44  return -1; // suspend on async calls and stop on failures
45 
46  return 1; // converting mismatch into match
47 }
48 
49 char const *
51 {
52  return "!";
53 }
54 
55 ACL *
57 {
58  // Not implemented: we are not a named ACL type in squid.conf so nobody
59  // should try to create a NotNode instance by ACL type name (which is
60  // what clone() API is for -- it does not really clone anything).
61  assert(false);
62  return NULL;
63 }
64 
67 {
68  SBufList text;
69  text.push_back(SBuf(name));
70  return text;
71 }
72 
73 /* Acl::AndNode */
74 
75 char const *
77 {
78  return "and";
79 }
80 
81 ACL *
83 {
84  return new AndNode;
85 }
86 
87 int
88 Acl::AndNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
89 {
90  // find the first node that does not match
91  for (Nodes::const_iterator i = start; i != nodes.end(); ++i) {
92  if (!checklist->matchChild(this, i, *i))
93  return checklist->keepMatching() ? 0 : -1;
94  }
95 
96  // one and not zero on empty because in math empty product equals identity
97  return 1; // no mismatches found (i.e., all kids matched)
98 }
99 
100 void
102 {
103  // Not implemented: AndNode cannot be configured directly. See Acl::AllOf.
104  assert(false);
105 }
106 
107 /* Acl::OrNode */
108 
109 char const *
111 {
112  return "any-of";
113 }
114 
115 ACL *
117 {
118  return new OrNode;
119 }
120 
121 bool
122 Acl::OrNode::bannedAction(ACLChecklist *, Nodes::const_iterator) const
123 {
124  return false;
125 }
126 
127 int
128 Acl::OrNode::doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
129 {
130  lastMatch_ = nodes.end();
131 
132  // find the first node that matches, but stop if things go wrong
133  for (Nodes::const_iterator i = start; i != nodes.end(); ++i) {
134  if (bannedAction(checklist, i))
135  continue;
136  if (checklist->matchChild(this, i, *i)) {
137  lastMatch_ = i;
138  return 1;
139  }
140 
141  if (!checklist->keepMatching())
142  return -1; // suspend on async calls and stop on failures
143  }
144 
145  // zero and not one on empty because in math empty sum equals zero
146  return 0; // all nodes mismatched
147 }
148 
149 void
151 {
152  // Not implemented: OrNode cannot be configured directly. See Acl::AnyOf.
153  assert(false);
154 }
155 
#define assert(EX)
Definition: assert.h:17
virtual ACL * clone() const
Definition: BoolOps.cc:116
Definition: SBuf.h:86
int i
Definition: membanger.c:49
Definition: Acl.h:39
bool keepMatching() const
Whether we should continue to match tree nodes or stop/pause.
Definition: Checklist.h:146
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
virtual int doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
Definition: BoolOps.cc:36
virtual void parse()
parses node represenation in squid.conf; dies on failures
Definition: BoolOps.cc:101
void add(ACL *node)
appends the node to the collection and takes control over it
Definition: InnerNode.cc:35
virtual ACL * clone() const
Definition: BoolOps.cc:56
virtual char const * typeString() const
Definition: BoolOps.cc:110
virtual char const * typeString() const
Definition: BoolOps.cc:76
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
virtual char const * typeString() const
Definition: BoolOps.cc:50
std::list< SBuf > SBufList
Definition: forward.h:22
virtual int doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
Definition: BoolOps.cc:128
virtual ACL * clone() const
Definition: BoolOps.cc:82
bool matchChild(const Acl::InnerNode *parent, Acl::Nodes::const_iterator pos, const ACL *child)
Definition: Checklist.cc:82
char name[ACL_NAME_SZ]
Definition: Acl.h:83
virtual int doMatch(ACLChecklist *checklist, Nodes::const_iterator start) const
Definition: BoolOps.cc:88
virtual void parse()
parses node represenation in squid.conf; dies on failures
Definition: BoolOps.cc:28
virtual SBufList dump() const
Definition: BoolOps.cc:66
NotNode(ACL *acl)
Definition: BoolOps.cc:17
virtual bool bannedAction(ACLChecklist *, Nodes::const_iterator) const
Definition: BoolOps.cc:122
virtual void parse()
parses node represenation in squid.conf; dies on failures
Definition: BoolOps.cc:150
#define NULL
Definition: types.h:166
const char * text
Definition: errorpage.cc:153
std::vector< ACL * > nodes
children nodes of this intermediate node
Definition: InnerNode.h:52

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors