1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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    | Authors: Jouni Ahto <jouni.ahto@exdec.fi>                            |
16    |          Andrew Avdeev <andy@simgts.mv.ru>                           |
17    |          Ard Biesheuvel <a.k.biesheuvel@ewi.tudelft.nl>              |
18    +----------------------------------------------------------------------+
19  */
20 
21 #ifndef PHP_IBASE_INCLUDES_H
22 #define PHP_IBASE_INCLUDES_H
23 
24 #include <ibase.h>
25 
26 #ifndef SQLDA_CURRENT_VERSION
27 #define SQLDA_CURRENT_VERSION SQLDA_VERSION1
28 #endif
29 
30 #ifndef METADATALENGTH
31 #define METADATALENGTH 68
32 #endif
33 
34 #define RESET_ERRMSG do { IBG(errmsg)[0] = '\0'; IBG(sql_code) = 0; } while (0)
35 
36 #define IB_STATUS (IBG(status))
37 
38 #ifdef IBASE_DEBUG
39 #define IBDEBUG(a) php_printf("::: %s (%d)\n", a, __LINE__);
40 #endif
41 
42 #ifndef IBDEBUG
43 #define IBDEBUG(a)
44 #endif
45 
46 extern int le_link, le_plink, le_trans;
47 
48 #define LE_LINK "Firebird/InterBase link"
49 #define LE_PLINK "Firebird/InterBase persistent link"
50 #define LE_TRANS "Firebird/InterBase transaction"
51 
52 #define IBASE_MSGSIZE 512
53 #define MAX_ERRMSG (IBASE_MSGSIZE*2)
54 
55 #define IB_DEF_DATE_FMT "%Y-%m-%d"
56 #define IB_DEF_TIME_FMT "%H:%M:%S"
57 
58 /* this value should never be > USHRT_MAX */
59 #define IBASE_BLOB_SEG 4096
60 
61 ZEND_BEGIN_MODULE_GLOBALS(ibase)
62 	ISC_STATUS status[20];
63 	zend_resource *default_link;
64 	zend_long num_links, num_persistent;
65 	char errmsg[MAX_ERRMSG];
66 	zend_long sql_code;
67 ZEND_END_MODULE_GLOBALS(ibase)
68 
69 ZEND_EXTERN_MODULE_GLOBALS(ibase)
70 
71 typedef struct {
72 	isc_db_handle handle;
73 	struct tr_list *tr_list;
74 	unsigned short dialect;
75 	struct event *event_head;
76 } ibase_db_link;
77 
78 typedef struct {
79 	isc_tr_handle handle;
80 	unsigned short link_cnt;
81 	unsigned long affected_rows;
82 	ibase_db_link *db_link[1]; /* last member */
83 } ibase_trans;
84 
85 typedef struct tr_list {
86 	ibase_trans *trans;
87 	struct tr_list *next;
88 } ibase_tr_list;
89 
90 typedef struct {
91 	isc_blob_handle bl_handle;
92 	unsigned short type;
93 	ISC_QUAD bl_qd;
94 } ibase_blob;
95 
96 typedef struct event {
97 	ibase_db_link *link;
98 	zend_resource* link_res;
99 	ISC_LONG event_id;
100 	unsigned short event_count;
101 	char **events;
102 	char *event_buffer, *result_buffer;
103 	zval callback;
104 	void *thread_ctx;
105 	struct event *event_next;
106 	enum event_state { NEW, ACTIVE, DEAD } state;
107 } ibase_event;
108 
109 enum php_interbase_option {
110 	PHP_IBASE_DEFAULT 			= 0,
111 	PHP_IBASE_CREATE            = 0,
112 	/* fetch flags */
113 	PHP_IBASE_FETCH_BLOBS		= 1,
114 	PHP_IBASE_FETCH_ARRAYS      = 2,
115 	PHP_IBASE_UNIXTIME 			= 4,
116 	/* transaction access mode */
117 	PHP_IBASE_WRITE 			= 1,
118 	PHP_IBASE_READ 				= 2,
119 	/* transaction isolation level */
120 	PHP_IBASE_CONCURRENCY 		= 4,
121 	PHP_IBASE_COMMITTED 		= 8,
122 	  PHP_IBASE_REC_NO_VERSION 	= 32,
123 	  PHP_IBASE_REC_VERSION 	= 64,
124 	PHP_IBASE_CONSISTENCY 		= 16,
125 	/* transaction lock resolution */
126 	PHP_IBASE_WAIT 				= 128,
127 	PHP_IBASE_NOWAIT 			= 256
128 };
129 
130 #define IBG(v) ZEND_MODULE_GLOBALS_ACCESSOR(ibase, v)
131 
132 #if defined(ZTS) && defined(COMPILE_DL_INTERBASE)
133 ZEND_TSRMLS_CACHE_EXTERN()
134 #endif
135 
136 #define BLOB_ID_LEN		18
137 #define BLOB_ID_MASK	"0x%" LL_MASK "x"
138 
139 #define BLOB_INPUT		1
140 #define BLOB_OUTPUT		2
141 
142 #ifdef PHP_WIN32
143 #define LL_MASK "I64"
144 #define LL_LIT(lit) lit ## I64
145 typedef void (__stdcall *info_func_t)(char*);
146 #else
147 #define LL_MASK "ll"
148 #define LL_LIT(lit) lit ## ll
149 typedef void (*info_func_t)(char*);
150 #endif
151 
152 void _php_ibase_error(void);
153 void _php_ibase_module_error(char *, ...)
154 	PHP_ATTRIBUTE_FORMAT(printf,1,2);
155 
156 /* determine if a resource is a link or transaction handle */
157 #define PHP_IBASE_LINK_TRANS(zv, lh, th)													\
158 		do {                                                                                \
159 			if (!zv) {                                                                      \
160 				lh = (ibase_db_link *)zend_fetch_resource2(                                 \
161 						IBG(default_link), "InterBase link", le_link, le_plink);            \
162 			} else {                                                                        \
163 				_php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, zv, &lh, &th);  \
164 			}                                                                               \
165 			if (SUCCESS != _php_ibase_def_trans(lh, &th)) { RETURN_FALSE; }                 \
166 		} while (0)
167 
168 int _php_ibase_def_trans(ibase_db_link *ib_link, ibase_trans **trans);
169 void _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAMETERS, zval *link_id,
170 	ibase_db_link **ib_link, ibase_trans **trans);
171 
172 /* provided by ibase_query.c */
173 void php_ibase_query_minit(INIT_FUNC_ARGS);
174 
175 /* provided by ibase_blobs.c */
176 void php_ibase_blobs_minit(INIT_FUNC_ARGS);
177 int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd);
178 zend_string *_php_ibase_quad_to_string(ISC_QUAD const qd);
179 int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, zend_ulong max_len);
180 int _php_ibase_blob_add(zval *string_arg, ibase_blob *ib_blob);
181 
182 /* provided by ibase_events.c */
183 void php_ibase_events_minit(INIT_FUNC_ARGS);
184 void _php_ibase_free_event(ibase_event *event);
185 
186 /* provided by ibase_service.c */
187 void php_ibase_service_minit(INIT_FUNC_ARGS);
188 
189 #ifndef max
190 #define max(a,b) ((a)>(b)?(a):(b))
191 #endif
192 
193 #endif /* PHP_IBASE_INCLUDES_H */
194 
195 /*
196  * Local variables:
197  * tab-width: 4
198  * c-basic-offset: 4
199  * End:
200  */
201