]> git.saurik.com Git - apple/security.git/blame - OSX/include/security_filedb/DbValue.h
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / include / security_filedb / DbValue.h
CommitLineData
b1ab9ed8 1/*
d8f41ccd 2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
b1ab9ed8
A
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_cdsa_utilities/cssmdata.h>
29#include <security_cdsa_utilities/cssmdb.h>
30#include <Security/cssmerr.h>
31#include <map>
32#include <vector>
33
34namespace Security
35{
36
37//
38// DbValue -- A base class for all types of database values.
39//
40class DbValue
41{
42public:
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
51template <class T>
52class BasicValue : public DbValue
53{
54public:
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
84protected:
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
93class UInt32Value : public BasicValue<uint32>
94{
95public:
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
102class SInt32Value : public BasicValue<sint32>
103{
104public:
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
111class DoubleValue : public BasicValue<double>
112{
113public:
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
122class BlobValue : public DbValue, public CssmData
123{
124public:
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
135protected:
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
146class TimeDateValue : public BlobValue
147{
148public:
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
158private:
159 uint32 rangeValue(uint32 start, uint32 length) const;
160};
161
162class StringValue : public BlobValue
163{
164public:
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
170private:
171 class Comparator : public BlobValue::Comparator {
172 public:
173 virtual int operator () (const uint8 *ptr1, const uint8 *ptr2, uint32 length);
174 };
175
176};
177
178class BigNumValue : public BlobValue
179{
180public:
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
188private:
189 static int compare(const uint8 *a, const uint8 *b, int length);
190};
191
192class MultiUInt32Value : public DbValue
193{
194public:
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
204private:
205 uint32 mNumValues;
206 uint32 *mValues;
207 bool mOwnsValues;
208};
209
210} // end namespace Security
211
212#endif // _H_APPLEDL_DBVALUE
213