-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.c
68 lines (53 loc) · 1.08 KB
/
magic.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
#include "magic.h"
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "wheel.h"
u8 *magic_new(u32 *size)
{
prime p;
u8 *m;
u32 lcm;
u32 i;
lcm = 1;
for (i = 1; i <= MAGIC_PRIMES; ++i)
lcm *= wheel[i];
m = ez_malloc(lcm);
memset(m, 0xff, lcm);
for (i = 1; i <= MAGIC_PRIMES; ++i) {
init_prime(&p, 0, i, 0, 0);
mark_multiples(&p, m, lcm);
}
*size = lcm;
return m;
}
void magic_apply(u8 *m, u32 m_size, u8 *dst, u32 dst_size, u64 i)
{
u32 m_rem;
u32 dst_rem;
u32 mi;
u32 limit;
mi = i % m_size;
m_rem = m_size - mi;
if (m_rem >= dst_size) {
memcpy(dst, &m[mi], dst_size);
return;
}
memcpy(dst, &m[mi], m_rem);
dst_rem = dst_size - m_rem;
if (dst_rem <= m_size) {
memcpy(&dst[m_rem], m, dst_rem);
return;
}
mi = m_rem;
limit = dst_size - m_size;
do {
memcpy(&dst[mi], m, m_size);
mi += m_size;
} while (mi < limit);
memcpy(&dst[mi], m, dst_size - mi);
}
void magic_free(u8 *m)
{
free(m);
}