forked from nwf/nwf-openamd-localizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
81 lines (67 loc) · 1.6 KB
/
util.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
#include <stdint.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "util.h"
uint16_t
crc16 (uint8_t *buffer, int size)
{
uint16_t crc = 0xFFFF;
if (buffer && size)
while (size--)
{
crc = (crc >> 8) | (crc << 8);
crc ^= *buffer++;
crc ^= ((unsigned char) crc) >> 4;
crc ^= crc << 12;
crc ^= (crc & 0xFF) << 5;
}
return crc;
}
#define MX ( (((z>>5)^(y<<2))+((y>>3)^(z<<4)))^((sum^y)+(tea_key->keybits[(p&3)^e]^z)) )
#define DELTA 0x9e3779b9UL
void
xxtea_decode (uint32_t * v, uint32_t length, const xxteakey *tea_key)
{
uint32_t z, y = v[0], sum = 0, e, p, q;
if (!v || !tea_key)
return;
q = 6 + 52 / length;
sum = q * DELTA;
while (sum)
{
e = (sum >> 2) & 3;
for (p = length - 1; p > 0; p--)
z = v[p - 1], y = v[p] -= MX;
z = v[length - 1];
y = v[0] -= MX;
sum -= DELTA;
}
}
void
shuffle_tx_byteorder (uint32_t * v, uint32_t len)
{
while (len--)
{
/* XXX This appears to be a case of misusing the hton* functions
* and so I suspect this code wouldn't work on big-endian machines.
* Go figure. In any case, it'll get fixed eventually, but for now
* this is here just because it was this way.
*/
*v = htonl (*v);
v++;
}
}
uint16_t read16(uint8_t **b) {
uint8_t *d = *b; *b += 2;
return demarshal16(d);
}
uint16_t demarshal16(uint8_t *d) {
return (*d << 8) | (*(d+1));
}
uint32_t read32(uint8_t **b) {
uint8_t *d = *b; *b += 4;
return demarshal32(d);
}
uint32_t demarshal32(uint8_t *d) {
return (*d << 24) | (*(d+1) << 16) | (*(d+2) << 8) | (*(d+3));
}