]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/cssmdata.cpp
Security-54.1.tar.gz
[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 #include <Security/cssmdata.h>
23 #include <Security/utilities.h>
24 #include <cstring>
25
26
27 namespace Security {
28
29
30 //
31 // Comparing raw CSSM_DATA things
32 //
33 bool operator == (const CSSM_DATA &d1, const CSSM_DATA &d2)
34 {
35 if (&d1 == &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);
42 }
43
44
45 //
46 // Managed data objects
47 //
48 CssmManagedData::~CssmManagedData()
49 { }
50
51
52 //
53 // CssmOwnedData
54 //
55 void CssmOwnedData::set(CssmManagedData &source)
56 {
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
65 }
66 } else { // different allocators
67 copy(source); // make a copy with our allocator
68 source.reset(); // release source's data
69 }
70 }
71
72
73 //
74 // CssmAutoData
75 //
76 CssmData CssmAutoData::release()
77 {
78 CssmData result = mData;
79 mData.clear();
80 return result;
81 }
82
83 void CssmAutoData::reset()
84 {
85 allocator.free(mData);
86 mData.clear();
87 }
88
89
90 //
91 // CssmRemoteData
92 //
93 CssmData CssmRemoteData::release()
94 {
95 iOwnTheData = false;
96 return referent;
97 }
98
99 void CssmRemoteData::reset()
100 {
101 if (iOwnTheData)
102 allocator.free(referent);
103 referent.clear();
104 }
105
106
107 //
108 // Date stuff
109 //
110 CssmDateData::CssmDateData(const CSSM_DATE &date)
111 : CssmData(buffer, sizeof(buffer))
112 {
113 memcpy(buffer, date.Year, 4);
114 memcpy(buffer + 4, date.Month, 2);
115 memcpy(buffer + 6, date.Day, 2);
116 }
117
118 CssmGuidData::CssmGuidData(const CSSM_GUID &guid) : CssmData(buffer, sizeof(buffer))
119 {
120 Guid::overlay(guid).toString(buffer);
121 }
122
123 CssmDLPolyData::operator CSSM_DATE () const
124 {
125 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
126 if (mData.Length != 8)
127 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
128
129 CSSM_DATE date;
130 memcpy(date.Year, mData.Data, 4);
131 memcpy(date.Month, mData.Data + 4, 2);
132 memcpy(date.Day, mData.Data + 6, 2);
133 return date;
134 }
135
136 CssmDLPolyData::operator Guid () const
137 {
138 assert(mFormat == CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
139 if (mData.Length != Guid::stringRepLength + 1)
140 CssmError::throwMe(CSSMERR_DL_DATABASE_CORRUPT);
141
142 return Guid(reinterpret_cast<const char *>(mData.Data));
143 }
144
145
146 } // end namespace Security