]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/dictionary.cpp
2 * Copyright (c) 2003 Apple Computer, Inc. All Rights Reserved.
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
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.
19 #include "dictionary.h"
24 CssmData
NameValuePair::CloneData (const CssmData
&value
)
26 void* clonedData
= (void*) new unsigned char [value
.length ()];
27 memcpy (clonedData
, value
.data (), value
.length ());
28 return CssmData (clonedData
, value
.length ());
33 NameValuePair::NameValuePair (uint32 name
, const CssmData
&value
) : mName (name
), mValue (CloneData (value
))
39 NameValuePair::NameValuePair (const CssmData
&data
)
41 // the first four bytes are the name
42 unsigned char* finger
= (unsigned char*) data
.data ();
46 for (i
= 0; i
< sizeof (uint32
); ++i
)
48 mName
= (mName
<< 8) | *finger
++;
51 // the next four bytes are the length
53 for (i
= 0; i
< sizeof (uint32
); ++i
)
55 length
= (length
<< 8) | *finger
++;
58 // what's left is the data
59 mValue
= CloneData (CssmData (finger
, length
));
64 NameValuePair::~NameValuePair ()
66 delete (unsigned char*) mValue
.data ();
71 void NameValuePair::Export (CssmData
&data
) const
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
;
79 uint32 intBuffer
= mName
;
82 for (i
= sizeof (uint32
) - 1; i
>= 0; --i
)
84 finger
[i
] = intBuffer
& 0xFF;
89 finger
+= sizeof (uint32
);
90 intBuffer
= mValue
.length ();
91 for (i
= sizeof (uint32
) - 1; i
>= 0; --i
)
93 finger
[i
] = intBuffer
& 0xFF;
98 finger
+= sizeof (uint32
);
99 memcpy (finger
, mValue
.data (), mValue
.length ());
101 data
= CssmData (d
, outSize
);
106 NameValueDictionary::NameValueDictionary ()
112 NameValueDictionary::~NameValueDictionary ()
114 // to prevent leaks, delete all members of the vector
115 int i
= mVector
.size ();
120 mVector
.erase (mVector
.begin () + i
);
126 NameValueDictionary::NameValueDictionary (const CssmData
&data
)
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 ();
134 // compute the length of data blob
137 for (i
= sizeof (uint32
); i
< 2 * sizeof (uint32
); ++i
)
139 length
= (length
<< 8) | finger
[i
];
142 // add the length of the "header"
143 length
+= 2 * sizeof (uint32
);
144 Insert (new NameValuePair (CssmData (finger
, length
)));
146 // skip to the next data
148 } while (finger
< target
);
153 void NameValueDictionary::Insert (NameValuePair
* pair
)
155 mVector
.push_back (pair
);
160 void NameValueDictionary::RemoveByName (uint32 name
)
162 int which
= FindPositionByName (name
);
165 NameValuePair
* nvp
= mVector
[which
];
166 mVector
.erase (mVector
.begin () + which
);
173 int NameValueDictionary::FindPositionByName (uint32 name
) const
175 int target
= CountElements ();
178 for (i
= 0; i
< target
; ++i
)
180 if (mVector
[i
]->Name () == name
)
191 const NameValuePair
* NameValueDictionary::FindByName (uint32 name
) const
193 int which
= FindPositionByName (name
);
194 return which
== -1 ? NULL
: mVector
[which
];
200 int NameValueDictionary::CountElements () const
202 return mVector
.size ();
207 const NameValuePair
* NameValueDictionary::GetElement (int which
)
209 return mVector
[which
];
214 void NameValueDictionary::Export (CssmData
&outData
)
216 // get each element in the dictionary, and add it to the data blob
219 unsigned char* data
= 0;
221 for (i
= 0; i
< CountElements (); ++i
)
223 CssmData exportedData
;
224 const NameValuePair
*nvp
= GetElement (i
);
225 nvp
->Export (exportedData
);
227 uint32 oldLength
= length
;
228 length
+= exportedData
.length ();
229 data
= (unsigned char*) realloc (data
, length
);
231 memcpy (data
+ oldLength
, exportedData
.data (), exportedData
.length ());
233 delete (unsigned char*) exportedData
.data ();
236 outData
= CssmData (data
, length
);
241 void NameValueDictionary::MakeNameValueDictionaryFromDLDbIdentifier (const DLDbIdentifier
&identifier
, NameValueDictionary
&nvd
)
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
))));
249 const char* dbName
= identifier
.dbName ();
250 nvd
.Insert (new NameValuePair (DB_NAME
, CssmData ((void*) (dbName
), strlen (dbName
) + 1)));
252 // get the net address
253 const CSSM_NET_ADDRESS
* add
= identifier
.dbLocation ();
256 nvd
.Insert (new NameValuePair (DB_LOCATION
, CssmData ((void*) add
, sizeof (CSSM_NET_ADDRESS
))));
262 DLDbIdentifier
NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary (const NameValueDictionary
&nvd
)
264 CSSM_SUBSERVICE_UID
* uid
= (CSSM_SUBSERVICE_UID
*) nvd
.FindByName (SSUID_KEY
)->Value ().data ();
265 char* name
= (char*) nvd
.FindByName (DB_NAME
)->Value ().data ();
267 const NameValuePair
* nvp
= nvd
.FindByName (DB_LOCATION
);
268 CSSM_NET_ADDRESS
* address
= nvp
? (CSSM_NET_ADDRESS
*) nvp
->Value ().data () : NULL
;
270 return DLDbIdentifier (*uid
, name
, address
);
273 }; // end Security namespace