PHP 8 compatibility
[m6w6/ext-pq] / src / php_pq_misc.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18 #include <ext/date/php_date.h>
19 #include <ext/standard/php_string.h>
20
21 #include <Zend/zend_interfaces.h>
22
23 #include <libpq/libpq-fs.h>
24
25 #include "php_pq.h"
26 #include "php_pqexc.h"
27 #include "php_pq_misc.h"
28 #include "php_pqconn_event.h"
29 #undef PHP_PQ_TYPE
30 #include "php_pq_type.h"
31
32
33 /* clear result object associated with a result handle */
34 void php_pqres_clear(PGresult *r) {
35 php_pq_object_t *o = PQresultInstanceData(r, php_pqconn_event);
36
37 if (o) {
38 php_pq_object_delref(o);
39 } else {
40 PQclear(r);
41 }
42 }
43
44 /* clear any asynchronous results */
45 void php_pqconn_clear(PGconn *conn) {
46 PGresult *r;
47 php_pqconn_event_data_t *evdata = PQinstanceData(conn, php_pqconn_event);
48
49 while ((r = PQgetResult(conn))) {
50 php_pqres_clear(r);
51 }
52
53 if (evdata && evdata->obj) {
54 if (php_pq_callback_is_enabled(&evdata->obj->intern->onevent)) {
55 if (php_pq_callback_is_locked(&evdata->obj->intern->onevent)) {
56 php_pq_callback_disable(&evdata->obj->intern->onevent);
57 } else {
58 php_pq_callback_dtor(&evdata->obj->intern->onevent);
59 }
60 }
61 }
62 }
63
64 /* safe wrappers to clear any asynchronous wrappers before querying synchronously */
65 PGresult *php_pq_exec(PGconn *conn, const char *query) {
66 php_pqconn_clear(conn);
67 return PQexec(conn, query);
68 }
69 PGresult *php_pq_exec_params(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat) {
70 php_pqconn_clear(conn);
71 return PQexecParams(conn, command, nParams, paramTypes, paramValues, paramLengths, paramFormats, resultFormat);
72 }
73 PGresult *php_pq_prepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes) {
74 php_pqconn_clear(conn);
75 return PQprepare(conn, stmtName, query, nParams, paramTypes);
76 }
77 PGresult *php_pq_exec_prepared(PGconn *conn, const char *stmtName, int nParams, const char *const * paramValues, const int *paramLengths, const int *paramFormats, int resultFormat) {
78 php_pqconn_clear(conn);
79 return PQexecPrepared(conn, stmtName, nParams, paramValues, paramLengths, paramFormats, resultFormat);
80 }
81
82 char *php_pq_rtrim(char *e)
83 {
84 size_t l = strlen(e);
85
86 while (l-- > 0 && e[l] == '\n') {
87 e[l] = '\0';
88 }
89 return e;
90 }
91
92 const char *php_pq_strmode(long mode)
93 {
94 switch (mode & (INV_READ|INV_WRITE)) {
95 case INV_READ|INV_WRITE:
96 return "rw";
97 case INV_READ:
98 return "r";
99 case INV_WRITE:
100 return "w";
101 default:
102 return "-";
103 }
104 }
105
106 int php_pq_compare_index_80(Bucket *lptr, Bucket *rptr)
107 {
108 zend_ulong l = lptr->h;
109 zend_ulong r = rptr->h;
110
111 if (l < r) {
112 return -1;
113 }
114 if (l > r) {
115 return 1;
116 }
117 return 0;
118 }
119 int php_pq_compare_index_70(const void *lptr, const void *rptr) {
120 return php_pq_compare_index_80((Bucket *) lptr, (Bucket *) rptr);
121 }
122
123 void php_pq_hash_ptr_dtor(zval *p)
124 {
125 efree(Z_PTR_P(p));
126 }
127
128 zend_class_entry *php_pqdt_class_entry;
129
130 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string, 0, 0, 0)
131 ZEND_END_ARG_INFO();
132 static PHP_METHOD(pqdt, __toString)
133 {
134 zval rv, tmp;
135
136 ZVAL_NULL(&rv);
137 php_pq_call_method(getThis(), "format", 1, &rv, php_pq_read_property(getThis(), "format", &tmp));
138 RETVAL_ZVAL(&rv, 1, 1);
139 }
140
141 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_create_from_format, 0, 0, 2)
142 ZEND_ARG_INFO(0, format)
143 ZEND_ARG_INFO(0, datetime)
144 #if PHP_VERSION_ID >= 70200
145 ZEND_ARG_OBJ_INFO(0, object, DateTimeZone, 1)
146 #else
147 ZEND_ARG_INFO(0, timezone)
148 #endif
149 ZEND_END_ARG_INFO();
150 static PHP_METHOD(pqdt, createFromFormat)
151 {
152 zend_error_handling zeh;
153 char *fmt_str, *dt_str;
154 size_t fmt_len, dt_len;
155 zval *ztz = NULL;
156 ZEND_RESULT_CODE rv;
157
158 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
159 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "ss|O", &fmt_str, &fmt_len, &dt_str, &dt_len, &ztz, php_date_get_timezone_ce());
160 zend_restore_error_handling(&zeh);
161
162 if (SUCCESS == rv) {
163 php_pqdt_from_string(return_value, fmt_str, dt_str, dt_len, "Y-m-d H:i:s.uO", ztz);
164 }
165 }
166
167 static zend_function_entry php_pqdt_methods[] = {
168 PHP_ME(pqdt, createFromFormat, ai_pqdt_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
169 PHP_ME(pqdt, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
170 PHP_MALIAS(pqdt, jsonSerialize, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
171 {0}
172 };
173
174 zval *php_pqdt_from_string(zval *zv, char *input_fmt, char *dt_str, size_t dt_len, const char *output_fmt, zval *ztimezone)
175 {
176 php_date_obj *dobj;
177
178 php_date_instantiate(php_pqdt_class_entry, zv);
179 dobj = php_date_obj_from_obj(Z_OBJ_P(zv));
180 if (!php_date_initialize(dobj, dt_str, dt_len, input_fmt, ztimezone, 1)) {
181 zval_dtor(zv);
182 ZVAL_NULL(zv);
183 } else if (output_fmt) {
184 zval fmt;
185 ZVAL_STRING(&fmt, output_fmt);
186 php_pq_update_property(zv, "format", &fmt);
187 zval_ptr_dtor(&fmt);
188 }
189
190 return zv;
191 }
192
193 zend_string *php_pqdt_to_string(zval *zdt, const char *format)
194 {
195 zval rv;
196
197 ZVAL_NULL(&rv);
198
199 if (php_pq_cast_object(zdt, IS_STRING, &rv)) {
200 return Z_STR(rv);
201 } else if (instanceof_function(Z_OBJCE_P(zdt), php_date_get_date_ce())) {
202 zval rv, zfmt;
203
204 ZVAL_NULL(&rv);
205 ZVAL_STRING(&zfmt, format);
206 php_pq_call_method(zdt, "format", 1, &rv, &zfmt);
207 zval_ptr_dtor(&zfmt);
208
209 if (Z_TYPE(rv) == IS_STRING) {
210 return Z_STR(rv);
211 }
212 zval_ptr_dtor(&rv);
213 }
214
215 return NULL;
216 }
217
218 zend_class_entry *php_pqconv_class_entry;
219
220 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_types, 0, 0, 0)
221 ZEND_END_ARG_INFO();
222
223 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_from_string, 0, 0, 2)
224 ZEND_ARG_INFO(0, data)
225 ZEND_ARG_INFO(0, type)
226 ZEND_END_ARG_INFO();
227
228 ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_to_string, 0, 0, 2)
229 ZEND_ARG_INFO(0, data)
230 ZEND_ARG_INFO(0, type)
231 ZEND_END_ARG_INFO();
232
233 zend_function_entry php_pqconv_methods[] = {
234 PHP_ABSTRACT_ME(pqconv, convertTypes, ai_pqconv_convert_types)
235 PHP_ABSTRACT_ME(pqconv, convertFromString, ai_pqconv_convert_from_string)
236 PHP_ABSTRACT_ME(pqconv, convertToString, ai_pqconv_convert_to_string)
237 {0}
238 };
239
240
241 PHP_MINIT_FUNCTION(pq_misc)
242 {
243 zend_class_entry *json, ce = {0};
244
245 INIT_NS_CLASS_ENTRY(ce, "pq", "Converter", php_pqconv_methods);
246 php_pqconv_class_entry = zend_register_internal_interface(&ce);
247
248 memset(&ce, 0, sizeof(ce));
249 INIT_NS_CLASS_ENTRY(ce ,"pq", "DateTime", php_pqdt_methods);
250 php_pqdt_class_entry = zend_register_internal_class_ex(&ce, php_date_get_date_ce());
251
252 zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.uO"), ZEND_ACC_PUBLIC);
253
254 /* stop reading this file right here! */
255 if ((json = zend_hash_str_find_ptr(CG(class_table), ZEND_STRL("jsonserializable")))) {
256 zend_class_implements(php_pqdt_class_entry, 1, json);
257 }
258
259 return SUCCESS;
260 }
261
262 typedef struct _HashTableList {
263 zval arr;
264 struct _HashTableList *parent;
265 } HashTableList;
266
267 typedef struct _ArrayParserState {
268 const char *ptr, *end;
269 HashTableList *list;
270 php_pqres_t *res;
271 Oid typ;
272 unsigned quotes:1;
273 unsigned escaped:1;
274 } ArrayParserState;
275
276 static char caa(ArrayParserState *a, const char *any, unsigned advance)
277 {
278 const char *p = any;
279
280 do {
281 if (*p == *a->ptr) {
282 a->ptr += advance;
283 return *p;
284 }
285 } while (*++p);
286
287 php_error_docref(NULL, E_WARNING, "Failed to parse array: expected one of '%s', got '%c'", any, *a->ptr); \
288 return 0;
289 }
290
291 static ZEND_RESULT_CODE add_element(ArrayParserState *a, const char *start)
292 {
293 zval zelem;
294 zend_string *zstr = zend_string_init(start, a->ptr - start, 0);
295
296 if (a->quotes) {
297 php_stripslashes(zstr);
298 ZVAL_STR(&zelem, zstr);
299 } else if (!zend_string_equals_literal(zstr, "NULL")) {
300 ZVAL_STR(&zelem, zstr);
301 } else {
302 zend_string_release(zstr);
303 ZVAL_NULL(&zelem);
304 }
305
306 if (!ZVAL_IS_NULL(&zelem)) {
307 php_pqres_typed_zval(a->res, a->typ, &zelem);
308 }
309
310 add_next_index_zval(&a->list->arr, &zelem);
311 return SUCCESS;
312 }
313
314 static ZEND_RESULT_CODE parse_array(ArrayParserState *a);
315
316 static ZEND_RESULT_CODE parse_element(ArrayParserState *a, char delim)
317 {
318 const char *el;
319
320 switch (*a->ptr) {
321 case '{':
322 return parse_array(a);
323
324 case '}':
325 return SUCCESS;
326
327 case '"':
328 a->quotes = 1;
329 ++a->ptr;
330 break;
331 }
332
333 for (el = a->ptr; a->ptr < a->end; ++a->ptr) {
334 switch (*a->ptr) {
335 case '\\':
336 a->escaped = !a->escaped;
337 break;
338
339 case '"':
340 if (a->escaped) {
341 a->escaped = 0;
342 } else if (a->quotes) {
343 if (SUCCESS != add_element(a, el)) {
344 return FAILURE;
345 }
346 a->quotes = 0;
347 ++a->ptr;
348 return SUCCESS;
349 } else {
350 php_error_docref(NULL, E_WARNING, "Failed to parse element, unexpected quote: '%.*s'", (int) (a->ptr - el), el);
351 return FAILURE;
352 }
353 break;
354
355 default:
356 if (delim != *a->ptr) {
357 a->escaped = 0;
358 break;
359 }
360 /* no break */
361 case '}':
362 if (!a->quotes) {
363 return add_element(a, el);
364 }
365 break;
366
367 }
368 }
369
370 php_error_docref(NULL, E_WARNING, "Failed to parse element, reached end of input");
371 return FAILURE;
372 }
373
374 static ZEND_RESULT_CODE parse_elements(ArrayParserState *a)
375 {
376 char delims[] = {'}', (char) PHP_PQ_DELIM_OF_ARRAY(a->typ), 0};
377
378 while (SUCCESS == parse_element(a, delims[1])) {
379 switch (caa(a, delims, 0)) {
380 case 0:
381 return FAILURE;
382
383 case '}':
384 return SUCCESS;
385
386 default:
387 if (!*++a->ptr) {
388 php_error_docref(NULL, E_WARNING, "Failed to parse elements, reached end of input");
389 return FAILURE;
390 }
391 break;
392 }
393 }
394
395 return FAILURE;
396 }
397
398 static ZEND_RESULT_CODE parse_array(ArrayParserState *a)
399 {
400 HashTableList *list;
401
402 if (!caa(a, "{", 1)) {
403 return FAILURE;
404 }
405
406 list = ecalloc(1, sizeof(*list));
407 array_init(&list->arr);
408
409 if (a->list) {
410 add_next_index_zval(&a->list->arr, &list->arr);
411 list->parent = a->list;
412 }
413 a->list = list;
414
415 if (SUCCESS != parse_elements(a)) {
416 return FAILURE;
417 }
418
419 if (!caa(a, "}", 1)) {
420 return FAILURE;
421 }
422
423 /* step one level back up */
424 if (a->list->parent) {
425 HashTableList *l = a->list->parent;
426
427 efree(a->list);
428 a->list = l;
429 }
430
431 return SUCCESS;
432 }
433
434 HashTable *php_pq_parse_array(php_pqres_t *res, const char *val_str, size_t val_len, Oid typ)
435 {
436 HashTable *ht = NULL;
437 ArrayParserState a = {0};
438
439 a.typ = typ;
440 a.ptr = val_str;
441 a.end = val_str + val_len;
442 a.res = res;
443
444 if (SUCCESS != parse_array(&a)) {
445 while (a.list) {
446 HashTableList *l = a.list->parent;
447
448 zval_dtor(&a.list->arr);
449 efree(a.list);
450 a.list = l;
451 }
452 return ht;
453 }
454
455 if (*a.ptr) {
456 php_error_docref(NULL, E_NOTICE, "Trailing input: '%s'", a.ptr);
457 }
458
459 while (a.list) {
460 HashTableList *l = a.list->parent;
461
462 ht = Z_ARRVAL(a.list->arr);
463 efree(a.list);
464 a.list = l;
465 }
466
467 return ht;
468 }
469
470
471 /*
472 * Local variables:
473 * tab-width: 4
474 * c-basic-offset: 4
475 * End:
476 * vim600: noet sw=4 ts=4 fdm=marker
477 * vim<600: noet sw=4 ts=4
478 */