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 #ifndef _H_APPLEDL_DBVALUE
24 #define _H_APPLEDL_DBVALUE
26 #include "ReadWriteSection.h"
28 #include <Security/cssmdata.h>
29 #include <Security/cssmdb.h>
30 #include <Security/cssmerr.h>
38 // DbValue -- A base class for all types of database values.
46 // A collection of subclasses of DbValue that work for simple
47 // data types, e.g. uint32, sint32, and double, that have
48 // the usual C comparison and sizeof operations. Defining this
49 // template saves typing below.
52 class BasicValue
: public DbValue
56 BasicValue(T value
) : mValue(value
) {}
58 bool evaluate(const BasicValue
<T
> &other
, CSSM_DB_OPERATOR op
) const
63 return mValue
== other
.mValue
;
65 case CSSM_DB_NOT_EQUAL
:
66 return mValue
!= other
.mValue
;
68 case CSSM_DB_LESS_THAN
:
69 return mValue
< other
.mValue
;
71 case CSSM_DB_GREATER_THAN
:
72 return mValue
> other
.mValue
;
75 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_QUERY
);
80 size_t size() const { return sizeof(T
); }
81 size_t size(const ReadSection
&rs
, uint32 offset
) const { return size(); }
82 const uint8
*bytes() const { return reinterpret_cast<const uint8
*>(&mValue
); }
88 // Actual useful subclasses of DbValue as instances of BasicValue.
89 // Note that all of these require a constructor of the form
90 // (const ReadSection &, uint32 &offset) that advances the offset
91 // to just after the value.
93 class UInt32Value
: public BasicValue
<uint32
>
96 UInt32Value(const ReadSection
&rs
, uint32
&offset
);
97 UInt32Value(const CSSM_DATA
&data
);
98 virtual ~UInt32Value();
99 void pack(WriteSection
&ws
, uint32
&offset
) const;
102 class SInt32Value
: public BasicValue
<sint32
>
105 SInt32Value(const ReadSection
&rs
, uint32
&offset
);
106 SInt32Value(const CSSM_DATA
&data
);
107 virtual ~SInt32Value();
108 void pack(WriteSection
&ws
, uint32
&offset
) const;
111 class DoubleValue
: public BasicValue
<double>
114 DoubleValue(const ReadSection
&rs
, uint32
&offset
);
115 DoubleValue(const CSSM_DATA
&data
);
116 virtual ~DoubleValue();
117 void pack(WriteSection
&ws
, uint32
&offset
) const;
120 // Subclasses of Value for more complex types.
122 class BlobValue
: public DbValue
, public CssmData
126 BlobValue(const ReadSection
&rs
, uint32
&offset
);
127 BlobValue(const CSSM_DATA
&data
);
128 virtual ~BlobValue();
129 void pack(WriteSection
&ws
, uint32
&offset
) const;
130 bool evaluate(const BlobValue
&other
, CSSM_DB_OPERATOR op
) const;
132 size_t size() const { return Length
; }
133 const uint8
*bytes() const { return Data
; }
138 virtual ~Comparator();
139 virtual int operator () (const uint8
*ptr1
, const uint8
*ptr2
, uint32 length
);
142 static bool evaluate(const CssmData
&data1
, const CssmData
&data2
, CSSM_DB_OPERATOR op
,
146 class TimeDateValue
: public BlobValue
149 enum { kTimeDateSize
= 16 };
151 TimeDateValue(const ReadSection
&rs
, uint32
&offset
);
152 TimeDateValue(const CSSM_DATA
&data
);
153 virtual ~TimeDateValue();
154 void pack(WriteSection
&ws
, uint32
&offset
) const;
156 bool isValidDate() const;
159 uint32
rangeValue(uint32 start
, uint32 length
) const;
162 class StringValue
: public BlobValue
165 StringValue(const ReadSection
&rs
, uint32
&offset
);
166 StringValue(const CSSM_DATA
&data
);
167 virtual ~StringValue();
168 bool evaluate(const StringValue
&other
, CSSM_DB_OPERATOR op
) const;
171 class Comparator
: public BlobValue::Comparator
{
173 virtual int operator () (const uint8
*ptr1
, const uint8
*ptr2
, uint32 length
);
178 class BigNumValue
: public BlobValue
181 static const uint8 kSignBit
= 0x80;
183 BigNumValue(const ReadSection
&rs
, uint32
&offset
);
184 BigNumValue(const CSSM_DATA
&data
);
185 virtual ~BigNumValue();
186 bool evaluate(const BigNumValue
&other
, CSSM_DB_OPERATOR op
) const;
189 static int compare(const uint8
*a
, const uint8
*b
, int length
);
192 class MultiUInt32Value
: public DbValue
195 MultiUInt32Value(const ReadSection
&rs
, uint32
&offset
);
196 MultiUInt32Value(const CSSM_DATA
&data
);
197 virtual ~MultiUInt32Value();
198 void pack(WriteSection
&ws
, uint32
&offset
) const;
199 bool evaluate(const MultiUInt32Value
&other
, CSSM_DB_OPERATOR op
) const;
201 size_t size() const { return mNumValues
* sizeof(uint32
); }
202 const uint8
*bytes() const { return reinterpret_cast<uint8
*>(mValues
); }
210 } // end namespace Security
212 #endif // _H_APPLEDL_DBVALUE