1 #ifndef REDIS_LIBRARY_H
2 #define REDIS_LIBRARY_H
3 
4 /* Non cluster command helper */
5 #define REDIS_SPPRINTF(ret, kw, fmt, ...) \
6     redis_spprintf(redis_sock, NULL, ret, kw, fmt, ##__VA_ARGS__)
7 
8 #define REDIS_CMD_APPEND_SSTR_STATIC(sstr, str) \
9     redis_cmd_append_sstr(sstr, str, sizeof(str)-1);
10 
11 #define REDIS_CMD_APPEND_SSTR_OPT_STATIC(sstr, opt, str) \
12     if (opt) REDIS_CMD_APPEND_SSTR_STATIC(sstr, str);
13 
14 #define REDIS_CMD_INIT_SSTR_STATIC(sstr, argc, keyword) \
15     redis_cmd_init_sstr(sstr, argc, keyword, sizeof(keyword)-1);
16 
17 #define REDIS_THROW_EXCEPTION(msg, code) \
18     zend_throw_exception(redis_exception_ce, (msg), code)
19 
20 #define CLUSTER_THROW_EXCEPTION(msg, code) \
21     zend_throw_exception(redis_cluster_exception_ce, (msg), code)
22 
23 #define redis_sock_write_sstr(redis_sock, sstr) \
24     redis_sock_write(redis_sock, (sstr)->c, (sstr)->len)
25 
26 #if PHP_VERSION_ID < 80000
27     #define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(ZSTR_VAL((zstr)), ZSTR_LEN((zstr)))
28 #else
29     #define redis_hash_fetch_ops(zstr) php_hash_fetch_ops(zstr)
30 #endif
31 
32 void redis_register_persistent_resource(zend_string *id, void *ptr, int le_id);
33 
34 PHP_REDIS_API int redis_extract_auth_info(zval *ztest, zend_string **user, zend_string **pass);
35 
36 int redis_cmd_init_sstr(smart_string *str, int num_args, char *keyword, int keyword_len);
37 int redis_cmd_append_sstr(smart_string *str, char *append, int append_len);
38 int redis_cmd_append_sstr_int(smart_string *str, int append);
39 int redis_cmd_append_sstr_long(smart_string *str, long append);
40 int redis_cmd_append_sstr_i64(smart_string *str, int64_t append);
41 int redis_cmd_append_sstr_dbl(smart_string *str, double value);
42 int redis_cmd_append_sstr_zstr(smart_string *str, zend_string *zstr);
43 int redis_cmd_append_sstr_zval(smart_string *str, zval *z, RedisSock *redis_sock);
44 int redis_cmd_append_sstr_key(smart_string *str, char *key, size_t len, RedisSock *redis_sock, short *slot);
45 int redis_cmd_append_sstr_arrkey(smart_string *cmd, zend_string *kstr, zend_ulong idx);
46 
47 PHP_REDIS_API int redis_spprintf(RedisSock *redis_sock, short *slot, char **ret, char *kw, char *fmt, ...);
48 PHP_REDIS_API zend_string *redis_pool_spprintf(RedisSock *redis_sock, char *fmt, ...);
49 
50 PHP_REDIS_API char *redis_sock_read(RedisSock *redis_sock, int *buf_len);
51 PHP_REDIS_API int redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size, size_t* line_len);
52 PHP_REDIS_API int redis_1_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
53 PHP_REDIS_API int redis_long_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval* z_tab, void *ctx);
54 typedef void (*SuccessCallback)(RedisSock *redis_sock);
55 PHP_REDIS_API int redis_boolean_response_impl(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx, SuccessCallback success_callback);
56 PHP_REDIS_API int redis_boolean_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
57 PHP_REDIS_API int redis_bulk_double_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
58 PHP_REDIS_API int redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
59 PHP_REDIS_API void redis_single_line_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
60     zval *z_tab, void *ctx);
61 PHP_REDIS_API int redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
62 PHP_REDIS_API int redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
63 PHP_REDIS_API void redis_parse_info_response(char *response, zval *z_ret);
64 PHP_REDIS_API void redis_parse_client_list_response(char *response, zval *z_ret);
65 PHP_REDIS_API int redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
66 PHP_REDIS_API RedisSock* redis_sock_create(char *host, int host_len, int port, double timeout, double read_timeout, int persistent, char *persistent_id, long retry_interval);
67 PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock);
68 PHP_REDIS_API int redis_sock_server_open(RedisSock *redis_sock);
69 PHP_REDIS_API int redis_sock_auth(RedisSock *redis_sock);
70 PHP_REDIS_API char *redis_sock_auth_cmd(RedisSock *redis_sock, int *cmdlen);
71 PHP_REDIS_API void redis_sock_set_auth(RedisSock *redis_sock, zend_string *user, zend_string *pass);
72 PHP_REDIS_API void redis_sock_set_auth_zval(RedisSock *redis_sock, zval *zv);
73 PHP_REDIS_API void redis_sock_copy_auth(RedisSock *dst, RedisSock *src);
74 PHP_REDIS_API void redis_sock_free_auth(RedisSock *redis_sock);
75 PHP_REDIS_API int redis_sock_disconnect(RedisSock *redis_sock, int force);
76 PHP_REDIS_API zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab);
77 PHP_REDIS_API int redis_sock_read_single_line(RedisSock *redis_sock, char *buffer,
78     size_t buflen, size_t *linelen, int set_err);
79 PHP_REDIS_API char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes);
80 PHP_REDIS_API int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *_z_tab, void *ctx);
81 PHP_REDIS_API void redis_mbulk_reply_loop(RedisSock *redis_sock, zval *z_tab, int count, int unserialize);
82 
83 
84 PHP_REDIS_API int redis_mbulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
85 PHP_REDIS_API int redis_mbulk_reply_zipped_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
86 PHP_REDIS_API int redis_mbulk_reply_zipped_vals(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
87 PHP_REDIS_API int redis_mbulk_reply_zipped_keys_int(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
88 PHP_REDIS_API int redis_mbulk_reply_zipped_keys_dbl(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
89 PHP_REDIS_API int redis_mbulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
90 
91 PHP_REDIS_API int redis_sock_read_scan_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, REDIS_SCAN_TYPE type, zend_long *iter);
92 
93 
94 PHP_REDIS_API int redis_xrange_reply(INTERNAL_FUNCTION_PARAMETERS,
95     RedisSock *redis_sock, zval *z_tab, void *ctx);
96 PHP_REDIS_API int redis_xread_reply(INTERNAL_FUNCTION_PARAMETERS,
97     RedisSock *redis_sock, zval *z_tab, void *ctx);
98 PHP_REDIS_API int redis_xclaim_reply(INTERNAL_FUNCTION_PARAMETERS,
99     RedisSock *redis_sock, zval *z_tab, void *ctx);
100 PHP_REDIS_API int redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS,
101     RedisSock *redis_sock, zval *z_tab, void *ctx);
102 
103 PHP_REDIS_API int redis_subscribe_response(INTERNAL_FUNCTION_PARAMETERS,
104     RedisSock *redis_sock, zval *z_tab, void *ctx);
105 PHP_REDIS_API int redis_unsubscribe_response(INTERNAL_FUNCTION_PARAMETERS,
106     RedisSock *redis_sock, zval *z_tab, void *ctx);
107 
108 PHP_REDIS_API int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz);
109 PHP_REDIS_API int redis_check_eof(RedisSock *redis_sock, int no_throw);
110 PHP_REDIS_API RedisSock *redis_sock_get(zval *id, int nothrow);
111 PHP_REDIS_API void redis_free_socket(RedisSock *redis_sock);
112 PHP_REDIS_API void redis_sock_set_err(RedisSock *redis_sock, const char *msg, int msg_len);
113 PHP_REDIS_API int redis_sock_set_stream_context(RedisSock *redis_sock, zval *options);
114 
115 PHP_REDIS_API int
116 redis_serialize(RedisSock *redis_sock, zval *z, char **val, size_t *val_len);
117 PHP_REDIS_API int
118 redis_key_prefix(RedisSock *redis_sock, char **key, size_t *key_len);
119 
120 PHP_REDIS_API int
121 redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval *z_ret);
122 
123 PHP_REDIS_API int
124 redis_compress(RedisSock *redis_sock, char **dst, size_t *dstlen, char *buf, size_t len);
125 PHP_REDIS_API int
126 redis_uncompress(RedisSock *redis_sock, char **dst, size_t *dstlen, const char *src, size_t len);
127 
128 PHP_REDIS_API int redis_pack(RedisSock *redis_sock, zval *z, char **val, size_t *val_len);
129 PHP_REDIS_API int redis_unpack(RedisSock *redis_sock, const char *val, int val_len, zval *z_ret);
130 
131 PHP_REDIS_API int
132 redis_read_stream_messages(RedisSock *redis_sock, int count, zval *z_ret);
133 PHP_REDIS_API int
134 redis_read_stream_messages_multi(RedisSock *redis_sock, int count, zval *z_ret);
135 PHP_REDIS_API int
136 redis_read_xclaim_response(RedisSock *redis_sock, int count, zval *rv);
137 PHP_REDIS_API int
138 redis_read_xinfo_response(RedisSock *redis_sock, zval *z_ret, int elements);
139 
140 /* Specialized ACL reply handlers */
141 PHP_REDIS_API int redis_read_acl_getuser_reply(RedisSock *redis_sock, zval *zret, long len);
142 PHP_REDIS_API int redis_acl_getuser_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
143 PHP_REDIS_API int redis_read_acl_log_reply(RedisSock *redis_sock, zval *zret, long count);
144 PHP_REDIS_API int redis_acl_log_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
145 
146 /*
147 * Variant Read methods, mostly to implement eval
148 */
149 
150 PHP_REDIS_API int redis_read_reply_type(RedisSock *redis_sock, REDIS_REPLY_TYPE *reply_type, long *reply_info);
151 PHP_REDIS_API int redis_read_variant_bulk(RedisSock *redis_sock, int size, zval *z_ret);
152 PHP_REDIS_API int redis_read_multibulk_recursive(RedisSock *redis_sock, long long elements, int status_strings, zval *z_ret);
153 PHP_REDIS_API int redis_read_variant_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
154 PHP_REDIS_API int redis_read_raw_variant_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
155 PHP_REDIS_API int redis_read_variant_reply_strings(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
156 PHP_REDIS_API int redis_client_list_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
157 
158 /* Helper methods to get configuration values from a HashTable. */
159 
160 #define REDIS_HASH_STR_FIND_STATIC(ht, sstr) \
161     zend_hash_str_find(ht, sstr, sizeof(sstr) - 1)
162 #define REDIS_HASH_STR_FIND_TYPE_STATIC(ht, sstr, type) \
163     redis_hash_str_find_type(ht, sstr, sizeof(sstr) - 1, type)
164 
165 #define REDIS_CONF_DOUBLE_STATIC(ht, sstr, dval) \
166     redis_conf_double(ht, sstr, sizeof(sstr) - 1, dval)
167 #define REDIS_CONF_BOOL_STATIC(ht, sstr, rval) \
168     redis_conf_bool(ht, sstr, sizeof(sstr) - 1, rval)
169 #define REDIS_CONF_ZEND_BOOL_STATIC(ht, sstr, bval) \
170     redis_conf_zend_bool(ht, sstr, sizeof(sstr) - 1, bval)
171 #define REDIS_CONF_LONG_STATIC(ht, sstr, lval) \
172     redis_conf_long(ht, sstr, sizeof(sstr) - 1, lval)
173 #define REDIS_CONF_INT_STATIC(ht, sstr, ival) \
174     redis_conf_int(ht, sstr, sizeof(sstr) - 1, ival)
175 #define REDIS_CONF_STRING_STATIC(ht, sstr, sval) \
176     redis_conf_string(ht, sstr, sizeof(sstr) - 1, sval)
177 #define REDIS_CONF_ZVAL_STATIC(ht, sstr, zret, copy, dtor) \
178     redis_conf_zval(ht, sstr, sizeof(sstr) - 1, zret, copy, dtor)
179 #define REDIS_CONF_AUTH_STATIC(ht, sstr, user, pass) \
180     redis_conf_auth(ht, sstr, sizeof(sstr) - 1, user, pass)
181 
182 zval *redis_hash_str_find_type(HashTable *ht, const char *key, int keylen, int type);
183 void redis_conf_double(HashTable *ht, const char *key, int keylen, double *dval);
184 void redis_conf_bool(HashTable *ht, const char *key, int keylen, int *bval);
185 void redis_conf_zend_bool(HashTable *ht, const char *key, int keylen, zend_bool *bval);
186 void redis_conf_long(HashTable *ht, const char *key, int keylen, zend_long *lval);
187 void redis_conf_int(HashTable *ht, const char *key, int keylen, int *ival);
188 void redis_conf_string(HashTable *ht, const char *key, size_t keylen, zend_string **sval);
189 void redis_conf_zval(HashTable *ht, const char *key, size_t keylen, zval *zret, int copy, int dtor);
190 void redis_conf_auth(HashTable *ht, const char *key, size_t keylen, zend_string **user, zend_string **pass);
191 
192 #endif
193