]> git.saurik.com Git - apple/security.git/blob - SecurityServer/dictionary.cpp
Security-179.tar.gz
[apple/security.git] / SecurityServer / dictionary.cpp
1 /*
2 * Copyright (c) 2003 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 #include "dictionary.h"
20 #include <ctype.h>
21
22 namespace Security {
23
24 CssmData NameValuePair::CloneData (const CssmData &value)
25 {
26 void* clonedData = (void*) new unsigned char [value.length ()];
27 memcpy (clonedData, value.data (), value.length ());
28 return CssmData (clonedData, value.length ());
29 }
30
31
32
33 NameValuePair::NameValuePair (uint32 name, const CssmData &value) : mName (name), mValue (CloneData (value))
34 {
35 }
36
37
38
39 NameValuePair::NameValuePair (const CssmData &data)
40 {
41 // the first four bytes are the name
42 unsigned char* finger = (unsigned char*) data.data ();
43 mName = 0;
44
45 unsigned int i;
46 for (i = 0; i < sizeof (uint32); ++i)
47 {
48 mName = (mName << 8) | *finger++;
49 }
50
51 // the next four bytes are the length
52 uint32 length = 0;
53 for (i = 0; i < sizeof (uint32); ++i)
54 {
55 length = (length << 8) | *finger++;
56 }
57
58 // what's left is the data
59 mValue = CloneData (CssmData (finger, length));
60 }
61
62
63
64 NameValuePair::~NameValuePair ()
65 {
66 delete (unsigned char*) mValue.data ();
67 }
68
69
70
71 void NameValuePair::Export (CssmData &data) const
72 {
73 // export the data in the format name length data
74 uint32 outSize = 2 * sizeof (uint32) + mValue.length ();
75 unsigned char* d = new unsigned char [outSize];
76 unsigned char* finger = d;
77
78 // export the name
79 uint32 intBuffer = mName;
80
81 int i;
82 for (i = sizeof (uint32) - 1; i >= 0; --i)
83 {
84 finger[i] = intBuffer & 0xFF;
85 intBuffer >>= 8;
86 }
87
88 // export the length
89 finger += sizeof (uint32);
90 intBuffer = mValue.length ();
91 for (i = sizeof (uint32) - 1; i >= 0; --i)
92 {
93 finger[i] = intBuffer & 0xFF;
94 intBuffer >>= 8;
95 }
96
97 // export the data
98 finger += sizeof (uint32);
99 memcpy (finger, mValue.data (), mValue.length ());
100
101 data = CssmData (d, outSize);
102 }
103
104
105
106 NameValueDictionary::NameValueDictionary ()
107 {
108 }
109
110
111
112 NameValueDictionary::~NameValueDictionary ()
113 {
114 // to prevent leaks, delete all members of the vector
115 int i = mVector.size ();
116 while (i > 0)
117 {
118 delete mVector[--i];
119
120 mVector.erase (mVector.begin () + i);
121 }
122 }
123
124
125
126 NameValueDictionary::NameValueDictionary (const CssmData &data)
127 {
128 // reconstruct a name value dictionary from a series of exported NameValuePair blobs
129 unsigned char* finger = (unsigned char*) data.data ();
130 unsigned char* target = finger + data.length ();
131
132 do
133 {
134 // compute the length of data blob
135 unsigned int i;
136 uint32 length = 0;
137 for (i = sizeof (uint32); i < 2 * sizeof (uint32); ++i)
138 {
139 length = (length << 8) | finger[i];
140 }
141
142 // add the length of the "header"
143 length += 2 * sizeof (uint32);
144 Insert (new NameValuePair (CssmData (finger, length)));
145
146 // skip to the next data
147 finger += length;
148 } while (finger < target);
149 }
150
151
152
153 void NameValueDictionary::Insert (NameValuePair* pair)
154 {
155 mVector.push_back (pair);
156 }
157
158
159
160 void NameValueDictionary::RemoveByName (uint32 name)
161 {
162 int which = FindPositionByName (name);
163 if (which != -1)
164 {
165 NameValuePair* nvp = mVector[which];
166 mVector.erase (mVector.begin () + which);
167 delete nvp;
168 }
169 }
170
171
172
173 int NameValueDictionary::FindPositionByName (uint32 name) const
174 {
175 int target = CountElements ();
176 int i;
177
178 for (i = 0; i < target; ++i)
179 {
180 if (mVector[i]->Name () == name)
181 {
182 return i;
183 }
184 }
185
186 return -1;
187 }
188
189
190
191 const NameValuePair* NameValueDictionary::FindByName (uint32 name) const
192 {
193 int which = FindPositionByName (name);
194 return which == -1 ? NULL : mVector[which];
195 }
196
197
198
199
200 int NameValueDictionary::CountElements () const
201 {
202 return mVector.size ();
203 }
204
205
206
207 const NameValuePair* NameValueDictionary::GetElement (int which)
208 {
209 return mVector[which];
210 }
211
212
213
214 void NameValueDictionary::Export (CssmData &outData)
215 {
216 // get each element in the dictionary, and add it to the data blob
217 int i;
218 uint32 length = 0;
219 unsigned char* data = 0;
220
221 for (i = 0; i < CountElements (); ++i)
222 {
223 CssmData exportedData;
224 const NameValuePair *nvp = GetElement (i);
225 nvp->Export (exportedData);
226
227 uint32 oldLength = length;
228 length += exportedData.length ();
229 data = (unsigned char*) realloc (data, length);
230
231 memcpy (data + oldLength, exportedData.data (), exportedData.length ());
232
233 delete (unsigned char*) exportedData.data ();
234 }
235
236 outData = CssmData (data, length);
237 }
238
239
240
241 void NameValueDictionary::MakeNameValueDictionaryFromDLDbIdentifier (const DLDbIdentifier &identifier, NameValueDictionary &nvd)
242 {
243 // get the subserviceID
244 const CssmSubserviceUid &ssuid = identifier.ssuid ();
245 const CSSM_SUBSERVICE_UID* baseID = &ssuid;
246 nvd.Insert (new NameValuePair (SSUID_KEY, CssmData ((void*) (baseID), sizeof (CSSM_SUBSERVICE_UID))));
247
248 // get the name
249 const char* dbName = identifier.dbName ();
250 nvd.Insert (new NameValuePair (DB_NAME, CssmData ((void*) (dbName), strlen (dbName) + 1)));
251
252 // get the net address
253 const CSSM_NET_ADDRESS* add = identifier.dbLocation ();
254 if (add != NULL)
255 {
256 nvd.Insert (new NameValuePair (DB_LOCATION, CssmData ((void*) add, sizeof (CSSM_NET_ADDRESS))));
257 }
258 }
259
260
261
262 DLDbIdentifier NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary (const NameValueDictionary &nvd)
263 {
264 CSSM_SUBSERVICE_UID* uid = (CSSM_SUBSERVICE_UID*) nvd.FindByName (SSUID_KEY)->Value ().data ();
265 char* name = (char*) nvd.FindByName (DB_NAME)->Value ().data ();
266
267 const NameValuePair* nvp = nvd.FindByName (DB_LOCATION);
268 CSSM_NET_ADDRESS* address = nvp ? (CSSM_NET_ADDRESS*) nvp->Value ().data () : NULL;
269
270 return DLDbIdentifier (*uid, name, address);
271 }
272
273 }; // end Security namespace