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.
23 #include "PrimaryKey.h"
25 using namespace KeychainCore
;
26 using namespace CssmClient
;
29 PrimaryKeyImpl::PrimaryKeyImpl(const CSSM_DATA
&data
)
30 : CssmDataContainer(data
.Data
, data
.Length
)
33 //@@@ do bounds checking here, throw if invalid
37 PrimaryKeyImpl::PrimaryKeyImpl(const DbAttributes
&primaryKeyAttrs
)
39 Length
= sizeof(uint32
);
40 for (uint32 ix
= 0; ix
< primaryKeyAttrs
.size(); ++ix
)
42 if (primaryKeyAttrs
.at(ix
).size() == 0)
43 MacOSError::throwMe(errSecInvalidKeychain
);
45 Length
+= sizeof(uint32
) + primaryKeyAttrs
.at(ix
).Value
[0].Length
;
48 // Careful with exceptions
49 Data
= mAllocator
.alloc
<uint8
>(Length
);
52 putUInt32(p
, primaryKeyAttrs
.recordType());
53 for (uint32 ix
= 0; ix
< primaryKeyAttrs
.size(); ++ix
)
55 uint32 len
= primaryKeyAttrs
.at(ix
).Value
[0].Length
;
57 memcpy(p
, primaryKeyAttrs
.at(ix
).Value
[0].Data
, len
);
63 PrimaryKeyImpl::createCursor(const Keychain
&keychain
)
65 DbCursor
cursor(keychain
->database());
67 // @@@ Set up cursor to find item with this.
70 if (left
< sizeof(*p
))
71 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
73 CSSM_DB_RECORDTYPE rt
= getUInt32(p
, left
);
74 const CssmAutoDbRecordAttributeInfo
&infos
= keychain
->primaryKeyInfosFor(rt
);
76 cursor
->recordType(rt
);
77 cursor
->conjunctive(CSSM_DB_AND
);
78 for (uint32 ix
= 0; ix
< infos
.size(); ++ix
)
80 uint32 len
= getUInt32(p
, left
);
83 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
85 CssmData
value(p
, len
);
89 cursor
->add(CSSM_DB_EQUAL
, infos
.at(ix
), value
);
97 PrimaryKeyImpl::putUInt32(uint8
*&p
, uint32 value
)
100 *p
++ = (value
>> 16) & 0xff;
101 *p
++ = (value
>> 8) & 0xff;
106 PrimaryKeyImpl::getUInt32(uint8
*&p
, uint32
&left
) const
108 if (left
< sizeof(uint32
))
109 MacOSError::throwMe(errSecNoSuchAttr
); // XXX Not really but whatever.
112 // @@@ Assumes data written in big endian.
113 uint32 value
= (p
[0] << 24) + (p
[1] << 16) + (p
[2] << 8) + p
[3];
115 left
-= sizeof(uint32
);
122 PrimaryKeyImpl::recordType() const
125 uint32 length
= Length
;
126 return getUInt32(data
, length
);