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
23 #define _CPP_CDSA_UTILITIES_CSSMDATA
25 #include <Security/cssmdata.h>
27 #include <Security/utilities.h>
31 // Managed data objects
33 CssmManagedData::~CssmManagedData()
40 void CssmOwnedData::set(CssmManagedData
&source
)
42 if (source
.length() == 0) { // source is empty
43 reset(); // so just clear old data
44 } else if (allocator
== source
.allocator
) { // compatible allocators
45 if (referent
.data() == source
.data()) { // same data *and* we own it?!
46 assert(this == &source
); // this better *be* me!
47 } else { // different data
48 reset(); // give up our old data
49 referent
= source
.release(); // take over source's data
51 } else { // different allocators
52 copy(source
); // make a copy with our allocator
53 source
.reset(); // release source's data
61 CssmData
CssmAutoData::release()
63 CssmData result
= mData
;
68 void CssmAutoData::reset()
70 allocator
.free(mData
);
78 CssmData
CssmRemoteData::release()
84 void CssmRemoteData::reset()
87 allocator
.free(referent
);
95 CssmDateData::CssmDateData(const CSSM_DATE
&date
)
96 : CssmData(buffer
, sizeof(buffer
))
98 memcpy(buffer
, date
.Year
, 4);
99 memcpy(buffer
+ 4, date
.Month
, 2);
100 memcpy(buffer
+ 6, date
.Day
, 2);
103 CssmGuidData::CssmGuidData(const CSSM_GUID
&guid
) : CssmData(buffer
, sizeof(buffer
))
105 Guid::overlay(guid
).toString(buffer
);
108 CssmDLPolyData::operator CSSM_DATE () const
110 assert(mFormat
== CSSM_DB_ATTRIBUTE_FORMAT_BLOB
);
111 if (mData
.Length
!= 8)
112 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT
);
115 memcpy(date
.Year
, mData
.Data
, 4);
116 memcpy(date
.Month
, mData
.Data
+ 4, 2);
117 memcpy(date
.Day
, mData
.Data
+ 6, 2);
121 CssmDLPolyData::operator Guid () const
123 assert(mFormat
== CSSM_DB_ATTRIBUTE_FORMAT_BLOB
);
124 if (mData
.Length
!= Guid::stringRepLength
+ 1)
125 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT
);
127 return Guid(reinterpret_cast<const char *>(mData
.Data
));