2 * Copyright (c) 2000-2001 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.
20 // cssmdata.cpp -- Manager different CssmData types
22 #include <Security/cssmdata.h>
23 #include <Security/utilities.h>
31 // Comparing raw CSSM_DATA things
33 bool operator == (const CSSM_DATA
&d1
, const CSSM_DATA
&d2
)
36 return true; // identical
37 if (d1
.Length
!= d2
.Length
)
38 return false; // can't be
39 if (d1
.Data
== d2
.Data
)
40 return true; // points to same data
41 return !memcmp(d1
.Data
, d2
.Data
, d1
.Length
);
46 // Managed data objects
48 CssmManagedData::~CssmManagedData()
55 void CssmOwnedData::set(CssmManagedData
&source
)
57 if (source
.length() == 0) { // source is empty
58 reset(); // so just clear old data
59 } else if (allocator
== source
.allocator
) { // compatible allocators
60 if (referent
.data() == source
.data()) { // same data *and* we own it?!
61 assert(this == &source
); // this better *be* me!
62 } else { // different data
63 reset(); // give up our old data
64 referent
= source
.release(); // take over source's data
66 } else { // different allocators
67 copy(source
); // make a copy with our allocator
68 source
.reset(); // release source's data
76 CssmData
CssmAutoData::release()
78 CssmData result
= mData
;
83 void CssmAutoData::reset()
85 allocator
.free(mData
);
93 CssmData
CssmRemoteData::release()
99 void CssmRemoteData::reset()
102 allocator
.free(referent
);
110 CssmDateData::CssmDateData(const CSSM_DATE
&date
)
111 : CssmData(buffer
, sizeof(buffer
))
113 memcpy(buffer
, date
.Year
, 4);
114 memcpy(buffer
+ 4, date
.Month
, 2);
115 memcpy(buffer
+ 6, date
.Day
, 2);
118 CssmGuidData::CssmGuidData(const CSSM_GUID
&guid
) : CssmData(buffer
, sizeof(buffer
))
120 Guid::overlay(guid
).toString(buffer
);
123 CssmDLPolyData::operator CSSM_DATE () const
125 assert(mFormat
== CSSM_DB_ATTRIBUTE_FORMAT_BLOB
);
126 if (mData
.Length
!= 8)
127 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT
);
130 memcpy(date
.Year
, mData
.Data
, 4);
131 memcpy(date
.Month
, mData
.Data
+ 4, 2);
132 memcpy(date
.Day
, mData
.Data
+ 6, 2);
136 CssmDLPolyData::operator Guid () const
138 assert(mFormat
== CSSM_DB_ATTRIBUTE_FORMAT_BLOB
);
139 if (mData
.Length
!= Guid::stringRepLength
+ 1)
140 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT
);
142 return Guid(reinterpret_cast<const char *>(mData
.Data
));
146 } // end namespace Security