xref: /PHP-8.4/ext/odbc/odbc_utils.c (revision 2920a266)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Calvin Buckley <calvin@cmpct.info>                           |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php.h"
18 #include "php_odbc_utils.h"
19 
20 /*
21  * Utility functions for dealing with ODBC connection strings and other common
22  * functionality.
23  *
24  * While useful for PDO_ODBC too, this lives in ext/odbc because there isn't a
25  * better place for it.
26  */
27 
PHP_FUNCTION(odbc_connection_string_is_quoted)28 PHP_FUNCTION(odbc_connection_string_is_quoted)
29 {
30 	zend_string *str;
31 
32 	ZEND_PARSE_PARAMETERS_START(1, 1)
33 		Z_PARAM_STR(str)
34 	ZEND_PARSE_PARAMETERS_END();
35 
36 	bool is_quoted = php_odbc_connstr_is_quoted(ZSTR_VAL(str));
37 
38 	RETURN_BOOL(is_quoted);
39 }
40 
PHP_FUNCTION(odbc_connection_string_should_quote)41 PHP_FUNCTION(odbc_connection_string_should_quote)
42 {
43 	zend_string *str;
44 
45 	ZEND_PARSE_PARAMETERS_START(1, 1)
46 		Z_PARAM_STR(str)
47 	ZEND_PARSE_PARAMETERS_END();
48 
49 	bool should_quote = php_odbc_connstr_should_quote(ZSTR_VAL(str));
50 
51 	RETURN_BOOL(should_quote);
52 }
53 
PHP_FUNCTION(odbc_connection_string_quote)54 PHP_FUNCTION(odbc_connection_string_quote)
55 {
56 	zend_string *str;
57 
58 	ZEND_PARSE_PARAMETERS_START(1, 1)
59 		Z_PARAM_STR(str)
60 	ZEND_PARSE_PARAMETERS_END();
61 
62 	size_t new_size = php_odbc_connstr_estimate_quote_length(ZSTR_VAL(str));
63 	zend_string *new_string = zend_string_alloc(new_size, 0);
64 	php_odbc_connstr_quote(ZSTR_VAL(new_string), ZSTR_VAL(str), new_size);
65 	/* reset length */
66 	ZSTR_LEN(new_string) = strlen(ZSTR_VAL(new_string));
67 	RETURN_STR(new_string);
68 }
69