-
Notifications
You must be signed in to change notification settings - Fork 10
/
php_hashids.h
133 lines (102 loc) · 3.98 KB
/
php_hashids.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2018 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: ZiHang Gao <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef PHP_HASHIDS_H
#define PHP_HASHIDS_H
#include <php.h>
#include <php_ini.h>
#include <ext/standard/info.h>
extern zend_module_entry hashids_module_entry;
#define phpext_hashids_ptr &hashids_module_entry
#define PHP_HASHIDS_VERSION "1.0.0"
/* minimal alphabet length */
#define HASHIDS_MIN_ALPHABET_LENGTH 16u
/* separator divisor */
#define HASHIDS_SEPARATOR_DIVISOR 3.5f
/* guard divisor */
#define HASHIDS_GUARD_DIVISOR 12u
/* default salt */
#define HASHIDS_DEFAULT_SALT ""
/* default minimal hash length */
#define HASHIDS_DEFAULT_MIN_HASH_LENGTH 0u
/* default alphabet */
#define HASHIDS_DEFAULT_ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
/* default separators */
#define HASHIDS_DEFAULT_SEPARATORS "cfhistuCFHISTU"
/* error codes */
#define HASHIDS_ERROR_OK 0
#define HASHIDS_ERROR_ALLOC -1
#define HASHIDS_ERROR_ALPHABET_LENGTH -2
#define HASHIDS_ERROR_ALPHABET_SPACE -3
#define HASHIDS_ERROR_INVALID_HASH -4
#define HASHIDS_ERROR_INVALID_NUMBER -5
/* thread-safe hashids_errno indirection */
extern int *__hashids_errno_addr();
#define hashids_errno (*__hashids_errno_addr())
/* alloc & free */
extern void *(*_hashids_alloc)(size_t size);
extern void (*_hashids_free)(void *ptr);
/* the hashids "object" */
struct hashids_s {
char *alphabet;
char *alphabet_copy_1;
char *alphabet_copy_2;
size_t alphabet_length;
char *salt;
size_t salt_length;
char *separators;
size_t separators_count;
char *guards;
size_t guards_count;
size_t min_hash_length;
};
typedef struct hashids_s hashids_t;
/* exported function definitions */
void hashids_shuffle(char *str, size_t str_length, char *salt, size_t salt_length);
void hashids_free(hashids_t *hashids);
/* init */
hashids_t * hashids_init(const char *salt, size_t min_hash_length, const char *alphabet);
/* encode */
size_t hashids_estimate_encoded_size(hashids_t *hashids, size_t numbers_count, unsigned long long *numbers);
size_t hashids_encode(hashids_t *hashids, char *buffer, size_t numbers_count, unsigned long long *numbers);
size_t hashids_encode_hex(hashids_t *hashids, char *buffer, const char *hex_str);
/* decode */
size_t hashids_numbers_count(hashids_t *hashids, char *str);
size_t hashids_decode(hashids_t *hashids, char *str, unsigned long long *numbers);
size_t hashids_decode_hex(hashids_t *hashids, char *str, char *output);
#ifdef ZTS
#include "TSRM.h"
#endif
#define HASHIDS_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(hashids, v)
ZEND_BEGIN_MODULE_GLOBALS(hashids)
char *salt;
zend_long min_hash_length;
char *alphabet;
ZEND_END_MODULE_GLOBALS(hashids)
#if defined(ZTS) && defined(COMPILE_DL_HASHIDS)
ZEND_TSRMLS_CACHE_EXTERN()
#endif
#endif /* PHP_HASHIDS_H */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/