-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhashids.h
127 lines (93 loc) · 3.04 KB
/
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
#ifndef HASHIDS_H
#define HASHIDS_H 1
#include <stdlib.h>
/* version constants */
#define HASHIDS_VERSION "1.2.1"
#define HASHIDS_VERSION_MAJOR 1
#define HASHIDS_VERSION_MINOR 2
#define HASHIDS_VERSION_PATCH 1
/* 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 "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"1234567890"
/* 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(void);
#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);
hashids_t *
hashids_init3(const char *salt, size_t min_hash_length,
const char *alphabet);
hashids_t *
hashids_init2(const char *salt, size_t min_hash_length);
hashids_t *
hashids_init(const char *salt);
size_t
hashids_estimate_encoded_size(hashids_t *hashids, size_t numbers_count,
unsigned long long *numbers);
size_t
hashids_estimate_encoded_size_v(hashids_t *hashids, size_t numbers_count, ...);
size_t
hashids_encode(hashids_t *hashids, char *buffer, size_t numbers_count,
unsigned long long *numbers);
size_t
hashids_encode_v(hashids_t *hashids, char *buffer, size_t numbers_count, ...);
size_t
hashids_encode_one(hashids_t *hashids, char *buffer,
unsigned long long number);
size_t
hashids_numbers_count(hashids_t *hashids, const char *str);
size_t
hashids_decode(hashids_t *hashids, const char *str,
unsigned long long *numbers, size_t numbers_max);
size_t
hashids_decode_unsafe(hashids_t *hashids, const char *str,
unsigned long long *numbers);
size_t
hashids_decode_safe(hashids_t *hashids, const char *str,
unsigned long long *numbers, size_t numbers_max);
size_t
hashids_encode_hex(hashids_t *hashids, char *buffer, const char *hex_str);
size_t
hashids_decode_hex(hashids_t *hashids, char *str, char *output);
#endif