-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
hashids.c
930 lines (786 loc) · 26 KB
/
hashids.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include "hashids.h"
#include "postgres.h"
/* branch prediction hinting */
#ifndef __has_builtin
# define __has_builtin(x) (0)
#endif
#if defined(__builtin_expect) || __has_builtin(__builtin_expect)
# define HASHIDS_LIKELY(x) (__builtin_expect(!!(x), 1))
# define HASHIDS_UNLIKELY(x) (__builtin_expect(!!(x), 0))
#else
# define HASHIDS_LIKELY(x) (x)
# define HASHIDS_UNLIKELY(x) (x)
#endif
/* fallthrough warning suppression */
#ifndef __has_feature
# define __has_feature(x) (0)
#endif
#if __has_feature(fallthrough)
# define ATTRIBUTE_FALLTHROUGH __attribute__((fallthrough))
#else
# define ATTRIBUTE_FALLTHROUGH
#endif
/* thread-local storage */
#ifndef TLS
#define TLS
#endif
/* thread-safe hashids_errno indirection */
TLS int __hashids_errno_val;
int *
__hashids_errno_addr()
{
return &__hashids_errno_val;
}
/* default alloc() implementation */
static inline void *
hashids_alloc_f(size_t size)
{
return palloc0(size);
}
/* default free() implementation */
static inline void
hashids_free_f(void *ptr)
{
pfree(ptr);
}
void *(*_hashids_alloc)(size_t size) = hashids_alloc_f;
void (*_hashids_free)(void *ptr) = hashids_free_f;
/* fast ceil(x / y) for size_t arguments */
static inline size_t
hashids_div_ceil_size_t(size_t x, size_t y)
{
return x / y + !!(x % y);
}
/* fast ceil(x / y) for unsigned short arguments */
static inline unsigned short
hashids_div_ceil_unsigned_short(unsigned short x, unsigned short y) {
return x / y + !!(x % y);
}
/* fast log2(x) for unsigned long long */
const unsigned short hashids_log2_64_tab[64] = {
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
static inline unsigned short
hashids_log2_64(unsigned long long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
/* pure evil : ieee abuse */
return hashids_log2_64_tab[
((unsigned long long)((x - (x >> 1)) * 0x07EDD5E59A4E28C2)) >> 58];
}
/* shuffle loop step */
#define hashids_shuffle_step(iter) \
if (i == 0) { break; } \
if (v == salt_length) { v = 0; } \
p += salt[v]; j = (salt[v] + v + p) % (iter); \
temp = str[(iter)]; str[(iter)] = str[j]; str[j] = temp; \
--i; ++v;
/* consistent shuffle */
void
hashids_shuffle(char *str, size_t str_length, char *salt, size_t salt_length)
{
ssize_t i;
size_t j, v, p;
char temp;
/* meh, meh */
if (!salt_length) {
return;
}
/* pure evil : loop unroll */
for (i = str_length - 1, v = 0, p = 0; i > 0; /* empty */) {
switch (i % 32) {
case 31: hashids_shuffle_step(i);
/* fall through */
case 30: hashids_shuffle_step(i);
/* fall through */
case 29: hashids_shuffle_step(i);
/* fall through */
case 28: hashids_shuffle_step(i);
/* fall through */
case 27: hashids_shuffle_step(i);
/* fall through */
case 26: hashids_shuffle_step(i);
/* fall through */
case 25: hashids_shuffle_step(i);
/* fall through */
case 24: hashids_shuffle_step(i);
/* fall through */
case 23: hashids_shuffle_step(i);
/* fall through */
case 22: hashids_shuffle_step(i);
/* fall through */
case 21: hashids_shuffle_step(i);
/* fall through */
case 20: hashids_shuffle_step(i);
/* fall through */
case 19: hashids_shuffle_step(i);
/* fall through */
case 18: hashids_shuffle_step(i);
/* fall through */
case 17: hashids_shuffle_step(i);
/* fall through */
case 16: hashids_shuffle_step(i);
/* fall through */
case 15: hashids_shuffle_step(i);
/* fall through */
case 14: hashids_shuffle_step(i);
/* fall through */
case 13: hashids_shuffle_step(i);
/* fall through */
case 12: hashids_shuffle_step(i);
/* fall through */
case 11: hashids_shuffle_step(i);
/* fall through */
case 10: hashids_shuffle_step(i);
/* fall through */
case 9: hashids_shuffle_step(i);
/* fall through */
case 8: hashids_shuffle_step(i);
/* fall through */
case 7: hashids_shuffle_step(i);
/* fall through */
case 6: hashids_shuffle_step(i);
/* fall through */
case 5: hashids_shuffle_step(i);
/* fall through */
case 4: hashids_shuffle_step(i);
/* fall through */
case 3: hashids_shuffle_step(i);
/* fall through */
case 2: hashids_shuffle_step(i);
/* fall through */
case 1: hashids_shuffle_step(i);
/* fall through */
case 0: hashids_shuffle_step(i);
}
}
}
/* "destructor" */
void
hashids_free(hashids_t *hashids)
{
if (hashids) {
if (hashids->alphabet) {
_hashids_free(hashids->alphabet);
}
if (hashids->alphabet_copy_1) {
_hashids_free(hashids->alphabet_copy_1);
}
if (hashids->alphabet_copy_2) {
_hashids_free(hashids->alphabet_copy_2);
}
if (hashids->salt) {
_hashids_free(hashids->salt);
}
if (hashids->separators) {
_hashids_free(hashids->separators);
}
if (hashids->guards) {
_hashids_free(hashids->guards);
}
_hashids_free(hashids);
}
}
/* common init */
hashids_t *
hashids_init3(const char *salt, size_t min_hash_length, const char *alphabet)
{
hashids_t *result;
size_t i, j, len;
char ch, *p;
hashids_errno = HASHIDS_ERROR_OK;
/* allocate the structure */
result = (hashids_t *)_hashids_alloc(sizeof(hashids_t));
if (HASHIDS_UNLIKELY(!result)) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}
/* allocate enough space for the alphabet */
len = strlen(alphabet) + 1;
result->alphabet = (char *)_hashids_alloc(len);
/* extract only the unique characters */
result->alphabet[0] = '\0';
for (i = 0, j = 0; i < len; ++i) {
ch = alphabet[i];
if (!strchr(result->alphabet, ch)) {
result->alphabet[j++] = ch;
}
}
result->alphabet[j] = '\0';
/* store alphabet length */
result->alphabet_length = j;
/* check length and whitespace */
if (result->alphabet_length < HASHIDS_MIN_ALPHABET_LENGTH) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALPHABET_LENGTH;
return NULL;
}
if (strchr(result->alphabet, 0x20) || strchr(result->alphabet, 0x09)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALPHABET_SPACE;
return NULL;
}
/* copy salt */
result->salt_length = salt ? strlen(salt) : 0;
result->salt = (char *)_hashids_alloc(result->salt_length + 1);
if (HASHIDS_UNLIKELY(!result->salt)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}
strncpy(result->salt, salt, result->salt_length);
/* allocate enough space for separators */
len = strlen(HASHIDS_DEFAULT_SEPARATORS);
j = (size_t)
(ceil((float)result->alphabet_length / HASHIDS_SEPARATOR_DIVISOR) + 1);
if (j < len + 1) {
j = len + 1;
}
result->separators = (char *)_hashids_alloc(j);
if (HASHIDS_UNLIKELY(!result->separators)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}
/* take default separators out of the alphabet */
for (i = 0, j = 0; i < strlen(HASHIDS_DEFAULT_SEPARATORS); ++i) {
ch = HASHIDS_DEFAULT_SEPARATORS[i];
/* check if separator is actually in the used alphabet */
if ((p = strchr(result->alphabet, ch))) {
result->separators[j++] = ch;
/* remove that separator */
memmove(p, p + 1,
strlen(result->alphabet) - (p - result->alphabet));
}
}
/* store separators length */
result->separators_count = j;
/* subtract separators count from alphabet length */
result->alphabet_length -= result->separators_count;
/* shuffle the separators */
if (result->separators_count) {
hashids_shuffle(result->separators, result->separators_count,
result->salt, result->salt_length);
}
/* check if we have any/enough separators */
if (!result->separators_count
|| (((float)result->alphabet_length / (float)result->separators_count)
> HASHIDS_SEPARATOR_DIVISOR)) {
size_t separators_count = (size_t)ceil(
(float)result->alphabet_length / HASHIDS_SEPARATOR_DIVISOR);
if (separators_count == 1) {
separators_count = 2;
}
if (separators_count > result->separators_count) {
/* we need more separators - get some from alphabet */
size_t diff = separators_count - result->separators_count;
strncat(result->separators, result->alphabet, diff);
memmove(result->alphabet, result->alphabet + diff,
result->alphabet_length - diff + 1);
result->separators_count += diff;
result->alphabet_length -= diff;
} else {
/* we have more than enough - truncate */
result->separators[separators_count] = '\0';
result->separators_count = separators_count;
}
}
/* shuffle alphabet */
hashids_shuffle(result->alphabet, result->alphabet_length,
result->salt, result->salt_length);
/* allocate guards */
result->guards_count = hashids_div_ceil_size_t(result->alphabet_length,
HASHIDS_GUARD_DIVISOR);
result->guards = (char *)_hashids_alloc(result->guards_count + 1);
if (HASHIDS_UNLIKELY(!result->guards)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}
if (HASHIDS_UNLIKELY(result->alphabet_length < 3)) {
/* take some from separators */
strncpy(result->guards, result->separators, result->guards_count);
memmove(result->separators, result->separators + result->guards_count,
result->separators_count - result->guards_count + 1);
result->separators_count -= result->guards_count;
} else {
/* take them from alphabet */
strncpy(result->guards, result->alphabet, result->guards_count);
memmove(result->alphabet, result->alphabet + result->guards_count,
result->alphabet_length - result->guards_count + 1);
result->alphabet_length -= result->guards_count;
}
/* allocate enough space for the alphabet copies */
result->alphabet_copy_1 = (char *)_hashids_alloc(result->alphabet_length +
1);
result->alphabet_copy_2 = (char *)_hashids_alloc(result->alphabet_length +
1);
if (HASHIDS_UNLIKELY(!result->alphabet || !result->alphabet_copy_1
|| !result->alphabet_copy_2)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}
/* set min hash length */
result->min_hash_length = min_hash_length;
/* return result happily */
return result;
}
/* init with salt and minimum hash length */
hashids_t *
hashids_init2(const char *salt, size_t min_hash_length)
{
return hashids_init3(salt, min_hash_length, HASHIDS_DEFAULT_ALPHABET);
}
/* init with hash only */
hashids_t *
hashids_init(const char *salt)
{
return hashids_init2(salt, HASHIDS_DEFAULT_MIN_HASH_LENGTH);
}
/* estimate buffer size (generic) */
size_t
hashids_estimate_encoded_size(hashids_t *hashids,
size_t numbers_count, unsigned long long *numbers)
{
size_t i, result_len;
for (i = 0, result_len = 1; i < numbers_count; ++i) {
if (numbers[i] == 0) {
result_len += 2;
} else if (numbers[i] == 0xFFFFFFFFFFFFFFFFull) {
result_len += hashids_div_ceil_unsigned_short(
hashids_log2_64(numbers[i]),
hashids_log2_64(hashids->alphabet_length)) - 1;
} else {
result_len += hashids_div_ceil_unsigned_short(
hashids_log2_64(numbers[i] + 1),
hashids_log2_64(hashids->alphabet_length));
}
}
if (numbers_count > 1) {
result_len += numbers_count - 1;
}
if (result_len < hashids->min_hash_length) {
result_len = hashids->min_hash_length;
}
return result_len + 2 /* fast log2 & ceil sometimes undershoot by 1 */;
}
/* estimate buffer size (variadic) */
size_t
hashids_estimate_encoded_size_v(hashids_t *hashids,
size_t numbers_count, ...)
{
size_t i, result;
unsigned long long *numbers;
va_list ap;
numbers = (unsigned long long *)_hashids_alloc(numbers_count *
sizeof(unsigned long long));
if (HASHIDS_UNLIKELY(!numbers)) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return 0;
}
va_start(ap, numbers_count);
for (i = 0; i < numbers_count; ++i) {
numbers[i] = va_arg(ap, unsigned long long);
}
va_end(ap);
result = hashids_estimate_encoded_size(hashids, numbers_count, numbers);
_hashids_free(numbers);
return result;
}
/* encode many (generic) */
size_t
hashids_encode(hashids_t *hashids, char *buffer,
size_t numbers_count, unsigned long long *numbers)
{
/* bail out if no numbers */
if (HASHIDS_UNLIKELY(!numbers_count)) {
buffer[0] = '\0';
return 0;
}
size_t i, j, result_len, guard_index, half_length_ceil, half_length_floor;
unsigned long long number, number_copy, numbers_hash;
int p_max;
char lottery, ch, temp_ch, *p, *buffer_end, *buffer_temp;
/* return an estimation if no buffer */
if (HASHIDS_UNLIKELY(!buffer)) {
return hashids_estimate_encoded_size(hashids, numbers_count, numbers);
}
/* copy the alphabet into internal buffer 1 */
strncpy(hashids->alphabet_copy_1, hashids->alphabet,
hashids->alphabet_length);
/* walk arguments once and generate a hash */
for (i = 0, numbers_hash = 0; i < numbers_count; ++i) {
number = numbers[i];
numbers_hash += number % (i + 100);
}
/* lottery character */
lottery = hashids->alphabet[numbers_hash % hashids->alphabet_length];
/* start output buffer with it (or don't) */
buffer[0] = lottery;
buffer_end = buffer + 1;
/* alphabet-like buffer used for salt at each iteration */
hashids->alphabet_copy_2[0] = lottery;
hashids->alphabet_copy_2[1] = '\0';
strncat(hashids->alphabet_copy_2, hashids->salt,
hashids->alphabet_length - 1);
p = hashids->alphabet_copy_2 + hashids->salt_length + 1;
p_max = hashids->alphabet_length - 1 - hashids->salt_length;
if (p_max > 0) {
strncat(hashids->alphabet_copy_2, hashids->alphabet,
p_max);
} else {
hashids->alphabet_copy_2[hashids->alphabet_length] = '\0';
}
for (i = 0; i < numbers_count; ++i) {
/* take number */
number = number_copy = numbers[i];
/* create a salt for this iteration */
if (p_max > 0) {
strncpy(p, hashids->alphabet_copy_1, p_max);
}
/* shuffle the alphabet */
hashids_shuffle(hashids->alphabet_copy_1, hashids->alphabet_length,
hashids->alphabet_copy_2, hashids->alphabet_length);
/* hash the number */
buffer_temp = buffer_end;
do {
ch = hashids->alphabet_copy_1[number % hashids->alphabet_length];
*buffer_end++ = ch;
number /= hashids->alphabet_length;
} while (number);
/* reverse the hash we got */
for (j = 0; j < (size_t)((buffer_end - buffer_temp) / 2); ++j) {
temp_ch = *(buffer_temp + j);
*(buffer_temp + j) = *(buffer_end - 1 - j);
*(buffer_end - 1 - j) = temp_ch;
}
if (i + 1 < numbers_count) {
number_copy %= ch + i;
*buffer_end = hashids->separators[number_copy %
hashids->separators_count];
++buffer_end;
}
}
/* intermediate string length */
result_len = buffer_end - buffer;
if (result_len < hashids->min_hash_length) {
/* add a guard before the encoded numbers */
guard_index = (numbers_hash + buffer[0]) % hashids->guards_count;
memmove(buffer + 1, buffer, result_len);
buffer[0] = hashids->guards[guard_index];
++result_len;
if (result_len < hashids->min_hash_length) {
/* add a guard after the encoded numbers */
guard_index = (numbers_hash + buffer[2]) % hashids->guards_count;
buffer[result_len] = hashids->guards[guard_index];
++result_len;
/* pad with half alphabet before and after */
half_length_ceil = hashids_div_ceil_size_t(
hashids->alphabet_length, 2);
half_length_floor = floor((float)hashids->alphabet_length / 2);
/* pad, pad, pad */
while (result_len < hashids->min_hash_length) {
/* shuffle the alphabet */
strncpy(hashids->alphabet_copy_2, hashids->alphabet_copy_1,
hashids->alphabet_length);
hashids_shuffle(hashids->alphabet_copy_1,
hashids->alphabet_length, hashids->alphabet_copy_2,
hashids->alphabet_length);
/* left pad from the end of the alphabet */
i = hashids_div_ceil_size_t(
hashids->min_hash_length - result_len, 2);
/* right pad from the beginning */
j = floor((float)(hashids->min_hash_length - result_len) / 2);
/* check bounds */
if (i > half_length_ceil) {
i = half_length_ceil;
}
if (j > half_length_floor) {
j = half_length_floor;
}
/* handle excessively excessive excess */
if ((i + j) % 2 == 0 && hashids->alphabet_length % 2 == 1) {
++i; --j;
}
/* move the current result to "center" */
memmove(buffer + i, buffer, result_len);
/* pad left */
memmove(buffer,
hashids->alphabet_copy_1 + hashids->alphabet_length - i, i);
/* pad right */
memmove(buffer + i + result_len, hashids->alphabet_copy_1, j);
/* increment result_len */
result_len += i + j;
}
}
}
buffer[result_len] = '\0';
return result_len;
}
/* encode many (variadic) */
size_t
hashids_encode_v(hashids_t *hashids, char *buffer,
size_t numbers_count, ...)
{
size_t i, result;
unsigned long long *numbers;
va_list ap;
numbers = (unsigned long long *)_hashids_alloc(numbers_count *
sizeof(unsigned long long));
if (HASHIDS_UNLIKELY(!numbers)) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return 0;
}
va_start(ap, numbers_count);
for (i = 0; i < numbers_count; ++i) {
numbers[i] = va_arg(ap, unsigned long long);
}
va_end(ap);
result = hashids_encode(hashids, buffer, numbers_count, numbers);
_hashids_free(numbers);
return result;
}
/* encode one */
size_t
hashids_encode_one(hashids_t *hashids, char *buffer,
unsigned long long number)
{
return hashids_encode(hashids, buffer, 1, &number);
}
/* numbers count */
size_t
hashids_numbers_count(hashids_t *hashids, const char *str)
{
size_t numbers_count;
char ch;
const char *p;
/* skip characters until we find a guard */
if (hashids->min_hash_length) {
p = str;
while ((ch = *p)) {
if (strchr(hashids->guards, ch)) {
str = p + 1;
break;
}
p++;
}
}
/* parse */
numbers_count = 0;
while ((ch = *str)) {
if (strchr(hashids->guards, ch)) {
break;
}
if (strchr(hashids->separators, ch)) {
numbers_count++;
str++;
continue;
}
if (!strchr(hashids->alphabet, ch)) {
hashids_errno = HASHIDS_ERROR_INVALID_HASH;
return 0;
}
str++;
}
/* account for the last number */
return numbers_count + 1;
}
/* decode */
size_t
hashids_decode(hashids_t *hashids, const char *str,
unsigned long long *numbers, size_t numbers_max)
{
size_t numbers_count;
unsigned long long number;
char lottery, ch, *p, *c;
int p_max;
if (!numbers || !numbers_max) {
return hashids_numbers_count(hashids, str);
}
/* skip characters until we find a guard */
if (hashids->min_hash_length) {
p = (char *)str;
while ((ch = *p)) {
if (strchr(hashids->guards, ch)) {
str = p + 1;
break;
}
p++;
}
}
/* get the lottery character */
lottery = *str++;
/* copy the alphabet into internal buffer 1 */
strncpy(hashids->alphabet_copy_1, hashids->alphabet,
hashids->alphabet_length);
/* alphabet-like buffer used for salt at each iteration */
hashids->alphabet_copy_2[0] = lottery;
hashids->alphabet_copy_2[1] = '\0';
strncat(hashids->alphabet_copy_2, hashids->salt,
hashids->alphabet_length - 1);
p = hashids->alphabet_copy_2 + hashids->salt_length + 1;
p_max = hashids->alphabet_length - 1 - hashids->salt_length;
if (p_max > 0) {
strncat(hashids->alphabet_copy_2, hashids->alphabet,
p_max);
} else {
hashids->alphabet_copy_2[hashids->alphabet_length] = '\0';
}
/* first shuffle */
hashids_shuffle(hashids->alphabet_copy_1, hashids->alphabet_length,
hashids->alphabet_copy_2, hashids->alphabet_length);
/* parse */
numbers_count = 0;
number = 0;
while ((ch = *str)) {
if (strchr(hashids->guards, ch)) {
break;
}
if (strchr(hashids->separators, ch)) {
/* store the number */
*numbers++ = number;
/* check limit */
if (++numbers_count >= numbers_max) {
return numbers_count;
}
number = 0;
/* resalt the alphabet */
if (p_max > 0) {
strncpy(p, hashids->alphabet_copy_1, p_max);
}
hashids_shuffle(hashids->alphabet_copy_1, hashids->alphabet_length,
hashids->alphabet_copy_2, hashids->alphabet_length);
str++;
continue;
}
if (!(c = strchr(hashids->alphabet_copy_1, ch))) {
hashids_errno = HASHIDS_ERROR_INVALID_HASH;
return 0;
}
number *= hashids->alphabet_length;
number += c - hashids->alphabet_copy_1;
str++;
}
/* store last number */
*numbers = number;
return numbers_count + 1;
}
/* unsafe decode */
size_t
hashids_decode_unsafe(hashids_t *hashids, const char *str,
unsigned long long *numbers)
{
return hashids_decode(hashids, str, numbers, (size_t)-1);
}
/* safe decode */
size_t
hashids_decode_safe(hashids_t *hashids, const char *str,
unsigned long long *numbers, size_t numbers_max)
{
size_t numbers_count;
size_t len;
char *p;
numbers_count = hashids_decode(hashids, str, numbers, numbers_max);
if (HASHIDS_UNLIKELY(!numbers_count)) {
hashids_errno = HASHIDS_ERROR_INVALID_HASH;
return 0;
}
len = hashids_estimate_encoded_size(hashids, numbers_count, numbers);
p = (char *)_hashids_alloc(len);
if (HASHIDS_UNLIKELY(!p)) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return 0;
}
len = hashids_encode(hashids, p, numbers_count, numbers);
if (HASHIDS_UNLIKELY(!len)) {
_hashids_free(p);
return 0;
}
if (strcmp(str, p) != 0) {
_hashids_free(p);
hashids_errno = HASHIDS_ERROR_INVALID_HASH;
return 0;
}
_hashids_free(p);
return numbers_count;
}
/* encode hex */
size_t
hashids_encode_hex(hashids_t *hashids, char *buffer,
const char *hex_str)
{
int len;
char *temp, *p;
size_t result;
unsigned long long number;
len = strlen(hex_str);
temp = (char *)_hashids_alloc(len + 2);
if (!temp) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return 0;
}
temp[0] = '1';
strncpy(temp + 1, hex_str, len);
number = strtoull(temp, &p, 16);
if (p == temp) {
_hashids_free(temp);
hashids_errno = HASHIDS_ERROR_INVALID_NUMBER;
return 0;
}
result = hashids_encode(hashids, buffer, 1, &number);
_hashids_free(temp);
return result;
}
/* decode hex */
size_t
hashids_decode_hex(hashids_t *hashids, char *str, char *output)
{
size_t result, i;
unsigned long long number;
char ch, *temp;
result = hashids_numbers_count(hashids, str);
if (result != 1) {
return 0;
}
result = hashids_decode_unsafe(hashids, str, &number);
if (result != 1) {
return 0;
}
temp = output;
do {
ch = number % 16;
if (ch > 9) {
ch += 'A' - 10;
} else {
ch += '0';
}
*temp++ = (char)ch;
number /= 16;
} while (number);
temp--;
*temp = 0;
for (i = 0; i < (size_t)((temp - output) / 2); ++i) {
ch = *(output + i);
*(output + i) = *(temp - 1 - i);
*(temp - 1 - i) = ch;
}
return 1;
}