]> git.saurik.com Git - apple/security.git/blob - SecurityServer/Authorization/AuthorizationData.cpp
Security-54.1.tar.gz
[apple/security.git] / SecurityServer / Authorization / AuthorizationData.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * AuthorizationData.cpp
21 * Authorization
22 *
23 * Created by Michael Brouwer on Thu Oct 12 2000.
24 * Copyright (c) 2000 Apple Computer Inc. All rights reserved.
25 *
26 */
27
28 #include "AuthorizationData.h"
29
30
31 namespace Authorization {
32
33
34 //
35 // Right class
36 //
37 Right &
38 Right::overlay(AuthorizationItem &item)
39 {
40 return static_cast<Right &>(item);
41 }
42
43 Right *
44 Right::overlay(AuthorizationItem *item)
45 {
46 return static_cast<Right *>(item);
47 }
48
49 Right::Right()
50 {
51 name = "";
52 valueLength = 0;
53 value = NULL;
54 flags = 0;
55 }
56
57 Right::Right(AuthorizationString inName, size_t inValueLength, const void *inValue)
58 {
59 name = inName;
60 valueLength = inValueLength;
61 value = const_cast<void *>(inValue);
62 }
63
64 Right::~Right()
65 {
66 }
67
68 bool
69 Right::operator < (const Right &other) const
70 {
71 return strcmp(name, other.name) < 0;
72 }
73
74
75 //
76 // RightSet class
77 //
78 const AuthorizationRights RightSet::gEmptyRights = { 0, NULL };
79
80 RightSet::RightSet(const AuthorizationRights *rights) :
81 mRights(const_cast<AuthorizationRights *>(rights ? rights : &gEmptyRights))
82 {
83 }
84
85 RightSet::RightSet(const RightSet &other)
86 {
87 mRights = other.mRights;
88 }
89
90 RightSet::~RightSet()
91 {
92 }
93
94 RightSet::const_reference
95 RightSet::back() const
96 {
97 // @@@ Should this if empty::throwMe()?
98 return static_cast<const_reference>(mRights->items[size() - 1]);
99 }
100
101
102 //
103 // MutableRightSet class
104 //
105 MutableRightSet::MutableRightSet(size_t count, const Right &element) :
106 mCapacity(count)
107 {
108 mRights = new AuthorizationRights();
109 mRights->items = reinterpret_cast<pointer>(malloc(sizeof(Right) * mCapacity));
110 if (!mRights->items)
111 {
112 delete mRights;
113 throw std::bad_alloc();
114 }
115
116 mRights->count = count;
117 for (size_type ix = 0; ix < count; ++ix)
118 mRights->items[ix] = element;
119 }
120
121 MutableRightSet::MutableRightSet(const RightSet &other)
122 {
123 size_type count = other.size();
124 mCapacity = count;
125 mRights = new AuthorizationRights();
126
127 mRights->items = reinterpret_cast<pointer>(malloc(sizeof(Right) * mCapacity));
128 if (!mRights->items)
129 {
130 delete mRights;
131 throw std::bad_alloc();
132 }
133
134 mRights->count = count;
135 for (size_type ix = 0; ix < count; ++ix)
136 mRights->items[ix] = other.mRights->items[ix];
137 }
138
139 MutableRightSet::~MutableRightSet()
140 {
141 free(mRights->items);
142 delete mRights;
143 }
144
145 MutableRightSet &
146 MutableRightSet::operator = (const RightSet &other)
147 {
148 size_type count = other.size();
149 if (capacity() < count)
150 grow(count);
151
152 mRights->count = count;
153 for (size_type ix = 0; ix < count; ++ix)
154 mRights->items[ix] = other.mRights->items[ix];
155
156 return *this;
157 }
158
159 void
160 MutableRightSet::swap(MutableRightSet &other)
161 {
162 AuthorizationRights *rights = mRights;
163 size_t capacity = mCapacity;
164 mRights = other.mRights;
165 mCapacity = other.mCapacity;
166 other.mRights = rights;
167 other.mCapacity = capacity;
168 }
169
170 MutableRightSet::reference
171 MutableRightSet::back()
172 {
173 // @@@ Should this if empty::throwMe()?
174 return static_cast<reference>(mRights->items[size() - 1]);
175 }
176
177 void
178 MutableRightSet::push_back(const_reference right)
179 {
180 if (size() >= capacity())
181 grow(capacity() + 1);
182
183 mRights->items[mRights->count] = right;
184 mRights->count++;
185 }
186
187 void
188 MutableRightSet::pop_back()
189 {
190 // @@@ Should this if empty::throwMe()?
191 if (!empty())
192 mRights->count--;
193 }
194
195 void
196 MutableRightSet::grow(size_type min_capacity)
197 {
198 size_type newCapacity = mCapacity * mCapacity;
199 if (newCapacity < min_capacity)
200 newCapacity = min_capacity;
201
202 void *newItems = realloc(mRights->items, sizeof(*mRights->items) * newCapacity);
203 if (!newItems)
204 throw std::bad_alloc();
205
206 mRights->items = reinterpret_cast<pointer>(newItems);
207 mCapacity = newCapacity;
208 }
209
210
211 } // end namespace Authorization