-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsort_r.h
234 lines (181 loc) · 6.32 KB
/
sort_r.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
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
/* Isaac Turner 29 April 2014 Public Domain */
#ifndef HC_SORT_R_H
#define HC_SORT_R_H
#include <stdlib.h>
#include <string.h>
/*
sort_r function to be exported.
Parameters:
base is the array to be sorted
nel is the number of elements in the array
width is the size in bytes of each element of the array
compar is the comparison function
arg is a pointer to be passed to the comparison function
void sort_r(void *base, size_t nel, size_t width,
int (*compar)(const void *_a, const void *_b, void *_arg),
void *arg);
Slightly modified to work with hashcat to no falsely detect _SORT_R_LINUX with mingw
*/
#if (defined __APPLE__ || defined __MACH__ || defined __DARWIN__ || \
defined __FreeBSD__ || defined __DragonFly__ || defined __NetBSD__)
# define _SORT_R_BSD
# define _SORT_R_INLINE inline
#elif (defined __linux__) || defined (__CYGWIN__)
# define _SORT_R_LINUX
# define _SORT_R_INLINE inline
#elif (defined _WIN32 || defined _WIN64 || defined __WINDOWS__)
# define _SORT_R_WINDOWS
# define _SORT_R_INLINE __inline
#else
/* Using our own recursive quicksort sort_r_simple() */
#endif
#if (defined NESTED_QSORT && NESTED_QSORT == 0)
# undef NESTED_QSORT
#endif
/* swap a, b iff a>b */
/* __restrict is same as restrict but better support on old machines */
static _SORT_R_INLINE int sort_r_cmpswap(char *__restrict a, char *__restrict b, size_t w,
int (*compar)(const void *_a, const void *_b,
void *_arg),
void *arg)
{
char *end = a+w;
if(compar(a, b, arg) > 0) {
for(; a < end; a++, b++) { char tmp = *a; *a = *b; *b = tmp; }
return 1;
}
return 0;
}
/* Implement recursive quicksort ourselves */
/* Note: quicksort is not stable, equivalent values may be swapped */
static _SORT_R_INLINE void sort_r_simple(void *base, size_t nel, size_t w,
int (*compar)(const void *_a, const void *_b,
void *_arg),
void *arg)
{
char *b = (char *)base, *end = b + nel*w;
if(nel < 7) {
/* Insertion sort for arbitrarily small inputs */
char *pi, *pj;
for(pi = b+w; pi < end; pi += w) {
for(pj = pi; pj > b && sort_r_cmpswap(pj-w,pj,w,compar,arg); pj -= w) {}
}
}
else
{
/* nel > 6; Quicksort */
/* Use median of first, middle and last items as pivot */
char *x, *y, *xend;
char *pl, *pr;
char *last = b+w*(nel-1), *tmp;
char *l[3];
l[0] = b;
l[1] = b+w*(nel/2);
l[2] = last;
if(compar(l[0],l[1],arg) > 0) { tmp=l[0]; l[0]=l[1]; l[1]=tmp; }
if(compar(l[1],l[2],arg) > 0) {
tmp=l[1]; l[1]=l[2]; l[2]=tmp; /* swap(l[1],l[2]) */
if(compar(l[0],l[1],arg) > 0) { tmp=l[0]; l[0]=l[1]; l[1]=tmp; }
}
/* swap l[id], l[2] to put pivot as last element */
for(x = l[1], y = last, xend = x+w; x<xend; x++, y++) {
char ch = *x; *x = *y; *y = ch;
}
pl = b;
pr = last;
while(pl < pr) {
for(; pl < pr; pl += w) {
if(sort_r_cmpswap(pl, pr, w, compar, arg)) {
pr -= w; /* pivot now at pl */
break;
}
}
for(; pl < pr; pr -= w) {
if(sort_r_cmpswap(pl, pr, w, compar, arg)) {
pl += w; /* pivot now at pr */
break;
}
}
}
sort_r_simple(b, (pl-b)/w, w, compar, arg);
sort_r_simple(pl+w, (end-(pl+w))/w, w, compar, arg);
}
}
#if defined NESTED_QSORT
static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
int (*compar)(const void *_a, const void *_b,
void *aarg),
void *arg)
{
int nested_cmp(const void *a, const void *b)
{
return compar(a, b, arg);
}
qsort(base, nel, width, nested_cmp);
}
#else /* !NESTED_QSORT */
/* Declare structs and functions */
#if defined _SORT_R_BSD
/* Ensure qsort_r is defined */
extern void qsort_r(void *base, size_t nel, size_t width, void *thunk,
int (*compar)(void *_thunk, const void *_a, const void *_b));
#endif
#if defined _SORT_R_BSD || defined _SORT_R_WINDOWS
/* BSD (qsort_r), Windows (qsort_s) require argument swap */
struct sort_r_data
{
void *arg;
int (*compar)(const void *_a, const void *_b, void *_arg);
};
static _SORT_R_INLINE int sort_r_arg_swap(void *s, const void *a, const void *b)
{
struct sort_r_data *ss = (struct sort_r_data*)s;
return (ss->compar)(a, b, ss->arg);
}
#endif
#if defined _SORT_R_LINUX
typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
extern void qsort_r(void *base, size_t nel, size_t width,
__compar_d_fn_t __compar, void *arg)
__attribute__((nonnull (1, 4)));
#endif
/* implementation */
static _SORT_R_INLINE void sort_r(void *base, size_t nel, size_t width,
int (*compar)(const void *_a, const void *_b, void *_arg),
void *arg)
{
#if defined _SORT_R_LINUX
#if defined __GLIBC__ && ((__GLIBC__ < 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 8))
/* no qsort_r in glibc before 2.8, need to use nested qsort */
sort_r_simple(base, nel, width, compar, arg);
#elif defined __GLIBC__
qsort_r(base, nel, width, compar, arg);
#else
/* Fall back to our own quicksort implementation */
sort_r_simple(base, nel, width, compar, arg);
#endif
#elif defined _SORT_R_BSD
struct sort_r_data tmp;
tmp.arg = arg;
tmp.compar = compar;
#if defined __NetBSD__
sort_r_simple(base, nel, width, compar, arg);
#else
qsort_r(base, nel, width, &tmp, sort_r_arg_swap);
#endif
#elif defined _SORT_R_WINDOWS
struct sort_r_data tmp;
tmp.arg = arg;
tmp.compar = compar;
qsort_s(base, nel, width, sort_r_arg_swap, &tmp);
#else
/* Fall back to our own quicksort implementation */
sort_r_simple(base, nel, width, compar, arg);
#endif
}
#endif /* !NESTED_QSORT */
#undef _SORT_R_INLINE
#undef _SORT_R_WINDOWS
#undef _SORT_R_LINUX
#undef _SORT_R_BSD
#endif /* HC_SORT_R_H */