tizen 2.3 release
[framework/web/wearable/wrt-security.git] / ace / include / ace / Attribute.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 //
17 //
18 //
19 //  @ Project : Access Control Engine
20 //  @ File Name : Attribute.h
21 //  @ Date : 2009-05-06
22 //  @ Author : Samsung
23 //
24 //
25
26 #if !defined(_ATTRIBUTE_H)
27 #define _ATTRIBUTE_H
28
29 #include <string>
30 #include <iostream>
31 #include <set>
32 #include <list>
33
34 #include <ace-dao-ro/BaseAttribute.h>
35
36 class Attribute : public AceDB::BaseAttribute
37 {
38   public:
39     /**
40      * Types of match functions
41      */
42     enum class Match { Equal, Glob, Regexp, Error };
43     /**
44      * Types of attribute value modifiers
45      */
46     enum class Modifier { Non, Scheme, Authority, SchemeAuthority, Host, Path };
47     /**
48      * Possible match results
49      */
50     enum class MatchResult { MRUndetermined = -1, MRFalse = 0, MRTrue = 1};
51
52   public:
53
54     /**
55      * New attribute constructor
56      * @param name name of the new attribute
57      * @param matchFunction match function used in the attribute
58      * @param type attribute type
59      */
60     Attribute(const std::string *name,
61               const Match matchFunction,
62               const Type type);
63
64
65     /**
66      * Constructor used to create default attribute ( used for unit tests )
67      * @param nm name of the default attribute
68      */
69     Attribute(const std::string& nm) :
70         matchFunction(Match::Error),
71         modifierFunction(Modifier::Non)
72     {
73         m_name = nm;
74         m_typeId = Type::Subject;
75         m_undetermindState = false;
76     }
77
78     /**
79      * Destructor
80      */
81     virtual ~Attribute();
82
83     std::list<std::string> * getValue() const
84     {
85         return AceDB::BaseAttribute::getValue();
86     }
87     Match getMatchFunction() const
88     {
89         return matchFunction;
90     }
91
92     /*  --- Setters --- */
93     void addValue (const std::string *value);
94
95     MatchResult  matchAttributes(const BaseAttribute *) const;
96
97     /**
98      * Operator used in for attribute set,used to distinguished only attribute names
99      * It cannot take attribute type into consideration
100      */
101     bool operator< (const Attribute & obj) const
102     {
103         int result = this->m_name.compare(*obj.getName());
104         if (result == 0) { //If names are equal check attribute types
105             if (this->m_typeId < obj.getType()) {
106                 result = -1;
107             } else if (this->m_typeId > obj.getType()) {
108                 result = 1;
109             }
110         }
111         //If result is negative that means that 'this' was '<' than obj
112         return result < 0;
113     }
114
115      /** Checks if object type is equal to argument */
116     bool instanceOf(Type type_)
117     {
118         return type_ == m_typeId;
119     }
120
121     friend std::ostream & operator<<(std::ostream & out,
122                                      const Attribute & attr);
123
124   protected:
125
126     bool searchAndCut(const char *);
127
128     /*
129      *  URI definition from rfc2396
130      *
131      *  <scheme>://<authority><path>?<query>
132      *  Each of the components may be absent, apart from the scheme.
133      *  Host is a part of authority as in definition below:
134      *
135      *  authority     = server | reg_name
136      *  server        = [ [ userinfo "@" ] hostport ]
137      *  <userinfo>@<host>:<port>
138      *
139      *  Extract from rfc2396
140      *  The authority component is preceded by a double slash "//" and is
141      *  terminated by the next slash "/", question-mark "?", or by the end of
142      *  the URI.  Within the authority component, the characters ";", ":",
143      * "@", "?", and "/" are reserved.
144      *
145      *  Modifiers should return pointer to empty string if given part of string was empty.
146      *  Modifiers should return NULL if the string to be modified was not an URI.
147      */
148     std::string * uriScheme(const std::string *) const;
149     std::string * uriAuthority(const std::string *) const;
150     std::string * uriSchemeAuthority(const std::string *) const;
151     std::string * uriHost(const std::string *) const;
152     std::string * uriPath(const std::string *) const;
153     std::string * applyModifierFunction(const std::string * val) const;
154
155     bool parse(const std::string *input,
156             std::string *part) const;
157     bool find_error(const std::string *part) const;
158
159     bool checkScheme(const std::string *scheme) const;
160     bool checkAuthority(const std::string *scheme) const;
161     std::string * getHost(const std::string *scheme) const;
162     bool checkPath(const std::string *scheme) const;
163
164     bool isSchemeAllowedCharacter(int c) const;
165     bool isSegmentAllowedCharacter(int c) const;
166     bool isUserInfoAllowedString(const std::string *str) const;
167     bool isHostAllowedString(const std::string *str) const;
168     bool isHostNameAllowedString(const std::string * str) const;
169     bool isIPv4AllowedString(const std::string * str) const;
170     bool isDomainLabelAllowedString(const char * data,
171                                     int lenght) const;
172     bool isTopLabelAllowedString(const char* data,
173                                  int lenght) const;
174
175     bool isUnreserved(int c) const;
176     bool isAlphanum(int c) const;
177     bool isEscaped(const char esc[3]) const;
178     bool isHex(int c) const;
179
180     MatchResult lists_comparator(
181         const std::list<std::string> *first,
182         const std::list<std::string> *second,
183         MatchResult (*comparator)(const std::string *,
184                                   const std::string *)) const;
185
186     /**
187      *  Map used to check if character is a 'mark'
188      */
189     static const bool mark[256];
190     /**
191      *  Map used to check if character is a 'digit'
192      *
193      */
194     static const bool digit[256];
195     /**
196      * Map used to check if character is an 'alphanumeric' value
197      *
198      */
199     static const bool alpha[256];
200
201   protected:
202     Match matchFunction;
203     Modifier modifierFunction;
204 };
205
206 typedef AceDB::BaseAttributeSet AttributeSet;
207
208 //TODO remove later or ifdef debug methods
209 void printAttributes(const AttributeSet& attrs);
210 void printAttributes(const std::list<Attribute> & attrs);
211
212 #endif  //_ATTRIBUTE_H