]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/DbValue.h
Security-28.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / DbValue.h
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 // DbValue.h
21 //
22
23 #ifndef _H_APPLEDL_DBVALUE
24 #define _H_APPLEDL_DBVALUE
25
26 #include "ReadWriteSection.h"
27
28 #include <Security/cssmdata.h>
29 #include <Security/cssmdb.h>
30 #include <Security/cssmerr.h>
31 #include <map>
32 #include <vector>
33
34 namespace Security
35 {
36
37 //
38 // DbValue -- A base class for all types of database values.
39 //
40 class DbValue
41 {
42 public:
43 virtual ~DbValue();
44 };
45
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.
50
51 template <class T>
52 class BasicValue : public DbValue
53 {
54 public:
55 BasicValue() {}
56 BasicValue(T value) : mValue(value) {}
57
58 bool evaluate(const BasicValue<T> &other, CSSM_DB_OPERATOR op) const
59 {
60 switch (op) {
61
62 case CSSM_DB_EQUAL:
63 return mValue == other.mValue;
64
65 case CSSM_DB_NOT_EQUAL:
66 return mValue != other.mValue;
67
68 case CSSM_DB_LESS_THAN:
69 return mValue < other.mValue;
70
71 case CSSM_DB_GREATER_THAN:
72 return mValue > other.mValue;
73
74 default:
75 CssmError::throwMe(CSSMERR_DL_UNSUPPORTED_QUERY);
76 return false;
77 }
78 }
79
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); }
83
84 protected:
85 T mValue;
86 };
87
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.
92
93 class UInt32Value : public BasicValue<uint32>
94 {
95 public:
96 UInt32Value(const ReadSection &rs, uint32 &offset);
97 UInt32Value(const CSSM_DATA &data);
98 virtual ~UInt32Value();
99 void pack(WriteSection &ws, uint32 &offset) const;
100 };
101
102 class SInt32Value : public BasicValue<sint32>
103 {
104 public:
105 SInt32Value(const ReadSection &rs, uint32 &offset);
106 SInt32Value(const CSSM_DATA &data);
107 virtual ~SInt32Value();
108 void pack(WriteSection &ws, uint32 &offset) const;
109 };
110
111 class DoubleValue : public BasicValue<double>
112 {
113 public:
114 DoubleValue(const ReadSection &rs, uint32 &offset);
115 DoubleValue(const CSSM_DATA &data);
116 virtual ~DoubleValue();
117 void pack(WriteSection &ws, uint32 &offset) const;
118 };
119
120 // Subclasses of Value for more complex types.
121
122 class BlobValue : public DbValue, public CssmData
123 {
124 public:
125 BlobValue() {}
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;
131
132 size_t size() const { return Length; }
133 const uint8 *bytes() const { return Data; }
134
135 protected:
136 class Comparator {
137 public:
138 virtual ~Comparator();
139 virtual int operator () (const uint8 *ptr1, const uint8 *ptr2, uint32 length);
140 };
141
142 static bool evaluate(const CssmData &data1, const CssmData &data2, CSSM_DB_OPERATOR op,
143 Comparator compare);
144 };
145
146 class TimeDateValue : public BlobValue
147 {
148 public:
149 enum { kTimeDateSize = 16 };
150
151 TimeDateValue(const ReadSection &rs, uint32 &offset);
152 TimeDateValue(const CSSM_DATA &data);
153 virtual ~TimeDateValue();
154 void pack(WriteSection &ws, uint32 &offset) const;
155
156 bool isValidDate() const;
157
158 private:
159 uint32 rangeValue(uint32 start, uint32 length) const;
160 };
161
162 class StringValue : public BlobValue
163 {
164 public:
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;
169
170 private:
171 class Comparator : public BlobValue::Comparator {
172 public:
173 virtual int operator () (const uint8 *ptr1, const uint8 *ptr2, uint32 length);
174 };
175
176 };
177
178 class BigNumValue : public BlobValue
179 {
180 public:
181 static const uint8 kSignBit = 0x80;
182
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;
187
188 private:
189 static int compare(const uint8 *a, const uint8 *b, int length);
190 };
191
192 class MultiUInt32Value : public DbValue
193 {
194 public:
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;
200
201 size_t size() const { return mNumValues * sizeof(uint32); }
202 const uint8 *bytes() const { return reinterpret_cast<uint8 *>(mValues); }
203
204 private:
205 uint32 mNumValues;
206 uint32 *mValues;
207 bool mOwnsValues;
208 };
209
210 } // end namespace Security
211
212 #endif // _H_APPLEDL_DBVALUE
213