1/*
2 * Copyright (c) 2003-2004,2006,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// dbm++ - generic C++ layer interface to [n]dbm
27//
28#ifndef _H_DBMPP
29#define _H_DBMPP
30
31#include <security_utilities/utilities.h>
32#include <security_cdsa_utilities/cssmdata.h>
33#include <security_utilities/unix++.h>
34#include <string>
35#include <db.h>
36
37
38namespace Security {
39namespace UnixPlusPlus {
40
41
42class UnixDb : public FileDesc {
43public:
44	UnixDb();
45	UnixDb(const char *path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
46	UnixDb(const std::string &path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
47
48	virtual ~UnixDb();
49
50	void open(const char *path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
51	void open(const std::string &path, int flags = O_RDWR, int mode = 0666, DBTYPE type = DB_HASH);
52	void close();
53
54	bool get(const CssmData &key, CssmData &value, int flags = 0) const;
55	bool get(const CssmData &key, CssmOwnedData &value, int flags = 0) const;
56	bool put(const CssmData &key, const CssmData &value, int flags = 0);
57	void erase(const CssmData &key, int flags = 0);
58	void flush(int flags = 0);
59
60	bool next(CssmData &key, CssmData &value, int flags = R_NEXT) const;
61	bool first(CssmData &key, CssmData &value) const
62		{ return next(key, value, R_FIRST); }
63
64	operator bool () const
65		{ return mDb; }
66
67public:
68	struct Data : public PodWrapper<Data, DBT> {
69		template <class T>
70		Data(const T &src)		{ DBT::data = src.data(); DBT::size = src.length(); }
71
72		Data() { }
73		Data(void *data, size_t length) { DBT::data = data; DBT::size = length; }
74		Data(const DBT &dat)	{ DBT::data = dat.data; DBT::size = dat.size; }
75
76		void *data() const		{ return DBT::data; }
77		size_t length() const	{ return size; }
78		operator bool () const	{ return DBT::data != NULL; }
79		operator CssmData () const { return CssmData(data(), length()); }
80	};
81
82private:
83	DB *mDb;
84};
85
86
87}	// end namespace UnixPlusPlus
88}	// end namespace Security
89
90
91#endif //_H_DBMPP
92