1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Sascha Schumann <sascha@schumann.cx>                         |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifndef PHP_DBA_H
20 #define PHP_DBA_H
21 
22 #include "php_version.h"
23 #define PHP_DBA_VERSION PHP_VERSION
24 
25 #if HAVE_DBA
26 
27 typedef enum {
28 	/* do not allow 0 here */
29 	DBA_READER = 1,
30 	DBA_WRITER,
31 	DBA_TRUNC,
32 	DBA_CREAT
33 } dba_mode_t;
34 
35 typedef struct dba_lock {
36 	php_stream *fp;
37 	char *name;
38 	int mode; /* LOCK_EX,LOCK_SH */
39 } dba_lock;
40 
41 typedef struct dba_info {
42 	/* public */
43 	void *dbf;               /* ptr to private data or whatever */
44 	char *path;
45 	dba_mode_t mode;
46 	php_stream *fp;  /* this is the database stream for builtin handlers */
47 	int fd;
48 	/* arg[cv] are only available when the dba_open handler is called! */
49 	int argc;
50 	zval *argv;
51 	/* private */
52 	int flags; /* whether and how dba did locking and other flags*/
53 	struct dba_handler *hnd;
54 	dba_lock lock;
55 } dba_info;
56 
57 #define DBA_LOCK_READER  (0x0001)
58 #define DBA_LOCK_WRITER  (0x0002)
59 #define DBA_LOCK_CREAT   (0x0004)
60 #define DBA_LOCK_TRUNC   (0x0008)
61 
62 #define DBA_LOCK_EXT     (0)
63 #define DBA_LOCK_ALL     (DBA_LOCK_READER|DBA_LOCK_WRITER|DBA_LOCK_CREAT|DBA_LOCK_TRUNC)
64 #define DBA_LOCK_WCT     (DBA_LOCK_WRITER|DBA_LOCK_CREAT|DBA_LOCK_TRUNC)
65 
66 #define DBA_STREAM_OPEN  (0x0010)
67 #define DBA_PERSISTENT   (0x0020)
68 
69 #define DBA_CAST_AS_FD   (0x0050)
70 #define DBA_NO_APPEND    (0x00D0)
71 
72 extern zend_module_entry dba_module_entry;
73 #define dba_module_ptr &dba_module_entry
74 
75 typedef struct dba_handler {
76 	char *name; /* handler name */
77 	int flags; /* whether and how dba does locking and other flags*/
78 	int (*open)(dba_info *, char **error);
79 	void (*close)(dba_info *);
80 	char* (*fetch)(dba_info *, char *, size_t, int, size_t *);
81 	int (*update)(dba_info *, char *, size_t, char *, size_t, int);
82 	int (*exists)(dba_info *, char *, size_t);
83 	int (*delete)(dba_info *, char *, size_t);
84 	char* (*firstkey)(dba_info *, size_t *);
85 	char* (*nextkey)(dba_info *, size_t *);
86 	int (*optimize)(dba_info *);
87 	int (*sync)(dba_info *);
88 	char* (*info)(struct dba_handler *hnd, dba_info *);
89 		/* dba_info==NULL: Handler info, dba_info!=NULL: Database info */
90 } dba_handler;
91 
92 /* common prototypes which must be supplied by modules */
93 
94 #define DBA_OPEN_FUNC(x) \
95 	int dba_open_##x(dba_info *info, char **error)
96 #define DBA_CLOSE_FUNC(x) \
97 	void dba_close_##x(dba_info *info)
98 #define DBA_FETCH_FUNC(x) \
99 	char *dba_fetch_##x(dba_info *info, char *key, size_t keylen, int skip, size_t *newlen)
100 #define DBA_UPDATE_FUNC(x) \
101 	int dba_update_##x(dba_info *info, char *key, size_t keylen, char *val, size_t vallen, int mode)
102 #define DBA_EXISTS_FUNC(x) \
103 	int dba_exists_##x(dba_info *info, char *key, size_t keylen)
104 #define DBA_DELETE_FUNC(x) \
105 	int dba_delete_##x(dba_info *info, char *key, size_t keylen)
106 #define DBA_FIRSTKEY_FUNC(x) \
107 	char *dba_firstkey_##x(dba_info *info, size_t *newlen)
108 #define DBA_NEXTKEY_FUNC(x) \
109 	char *dba_nextkey_##x(dba_info *info, size_t *newlen)
110 #define DBA_OPTIMIZE_FUNC(x) \
111 	int dba_optimize_##x(dba_info *info)
112 #define DBA_SYNC_FUNC(x) \
113 	int dba_sync_##x(dba_info *info)
114 #define DBA_INFO_FUNC(x) \
115 	char *dba_info_##x(dba_handler *hnd, dba_info *info)
116 
117 #define DBA_FUNCS(x) \
118 	DBA_OPEN_FUNC(x); \
119 	DBA_CLOSE_FUNC(x); \
120 	DBA_FETCH_FUNC(x); \
121 	DBA_UPDATE_FUNC(x); \
122 	DBA_DELETE_FUNC(x); \
123 	DBA_EXISTS_FUNC(x); \
124 	DBA_FIRSTKEY_FUNC(x); \
125 	DBA_NEXTKEY_FUNC(x); \
126 	DBA_OPTIMIZE_FUNC(x); \
127 	DBA_SYNC_FUNC(x); \
128 	DBA_INFO_FUNC(x)
129 
130 PHP_FUNCTION(dba_open);
131 PHP_FUNCTION(dba_popen);
132 PHP_FUNCTION(dba_close);
133 PHP_FUNCTION(dba_firstkey);
134 PHP_FUNCTION(dba_nextkey);
135 PHP_FUNCTION(dba_replace);
136 PHP_FUNCTION(dba_insert);
137 PHP_FUNCTION(dba_delete);
138 PHP_FUNCTION(dba_exists);
139 PHP_FUNCTION(dba_fetch);
140 PHP_FUNCTION(dba_optimize);
141 PHP_FUNCTION(dba_sync);
142 PHP_FUNCTION(dba_handlers);
143 PHP_FUNCTION(dba_list);
144 PHP_FUNCTION(dba_key_split);
145 
146 #else
147 #define dba_module_ptr NULL
148 #endif
149 
150 #define phpext_dba_ptr dba_module_ptr
151 
152 #endif
153