-
Notifications
You must be signed in to change notification settings - Fork 30
/
helpers.c
55 lines (44 loc) · 1.38 KB
/
helpers.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
#include <stdint.h>
#include <sys/types.h>
#include "structures.h"
static uint32_t hgsmiHashProcess(uint32_t hash,
const void *pvData,
size_t cbData)
{
const uint8_t *pu8Data = (const uint8_t *)pvData;
while (cbData--)
{
hash += *pu8Data++;
hash += (hash << 10);
hash ^= (hash >> 6);
}
return hash;
}
static uint32_t hgsmiHashEnd(uint32_t hash)
{
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
uint32_t HGSMIChecksum(HGSMIOFFSET offBuffer,
const HGSMIBUFFERHEADER *pHeader,
const HGSMIBUFFERTAIL *pTail)
{
uint32_t u32Checksum = 0;
u32Checksum = hgsmiHashProcess(u32Checksum, &offBuffer, sizeof(offBuffer));
u32Checksum = hgsmiHashProcess(u32Checksum, pHeader, sizeof(HGSMIBUFFERHEADER));
u32Checksum = hgsmiHashProcess(u32Checksum, pTail, RT_OFFSETOF(HGSMIBUFFERTAIL, u32Checksum));
return hgsmiHashEnd(u32Checksum);
}
void InitializeHeader(HGSMIBUFFERHEADER *pHeader, HGSMISIZE cbBuffer, uint8_t u8Channel,
uint16_t u16ChannelInfo, uint32_t offBuffer)
{
pHeader->u8Flags = HGSMI_BUFFER_HEADER_F_SEQ_SINGLE;
pHeader->u32DataSize = cbBuffer;
pHeader->u8Channel = u8Channel;
pHeader->u16ChannelInfo = u16ChannelInfo;
HGSMIBUFFERTAIL *pTail = (struct HGSMIBUFFERTAIL *)((uint64_t)pHeader + cbBuffer + sizeof(HGSMIBUFFERHEADER));
pTail->u32Reserved = 0;
pTail->u32Checksum = HGSMIChecksum(offBuffer, pHeader, pTail);
}