]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmdata.cpp
3cd9548a16e5a6b209a34eae0507e2b35489cf41
[apple/security.git] / cdsa / cdsa_utilities / cssmdata.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 // cssmdata.cpp -- Manager different CssmData types
21 //
22 #ifdef __MWERKS__
23 #define _CPP_CDSA_UTILITIES_CSSMDATA
24 #endif
25 #include <Security/cssmdata.h>
26
27 #include <Security/utilities.h>
28
29
30 //
31 // Managed data objects
32 //
33 CssmManagedData::~CssmManagedData()
34 { }
35
36
37 //
38 // CssmOwnedData
39 //
40 void CssmOwnedData::set(CssmManagedData &source)
41 {
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
50 }
51 } else { // different allocators
52 copy(source); // make a copy with our allocator
53 source.reset(); // release source's data
54 }
55 }
56
57
58 //
59 // CssmAutoData
60 //
61 CssmData CssmAutoData::release()
62 {
63 CssmData result = mData;
64 mData.clear();
65 return result;
66 }
67
68 void CssmAutoData::reset()
69 {
70 allocator.free(mData);
71 mData.clear();
72 }
73
74
75 //
76 // CssmRemoteData
77 //
78 CssmData CssmRemoteData::release()
79 {
80 iOwnTheData = false;
81 return referent;
82 }
83
84 void CssmRemoteData::reset()
85 {
86 if (iOwnTheData)
87 allocator.free(referent);
88 referent.clear();
89 }
90
91
92 //
93 // Date stuff
94 //
95 CssmDateData::CssmDateData(const CSSM_DATE &date)
96 : CssmData(buffer, sizeof(buffer))
97 {
98 memcpy(buffer, date.Year, 4);
99 memcpy(buffer + 4, date.Month, 2);
100 memcpy(buffer + 6, date.Day, 2);
101 }
102
103 CssmGuidData::CssmGuidData(const CSSM_GUID &guid) : CssmData(buffer, sizeof(buffer))
104 {
105 Guid::overlay(guid).toString(buffer);
106 }
107
108 CssmDLPolyData::operator CSSM_DATE () const
109 {
110 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
111 if (mData.Length != 8)
112 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
113
114 CSSM_DATE date;
115 memcpy(date.Year, mData.Data, 4);
116 memcpy(date.Month, mData.Data + 4, 2);
117 memcpy(date.Day, mData.Data + 6, 2);
118 return date;
119 }
120
121 CssmDLPolyData::operator Guid () const
122 {
123 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
124 if (mData.Length != Guid::stringRepLength + 1)
125 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
126
127 return Guid(reinterpret_cast<const char *>(mData.Data));
128 }