-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.c
80 lines (73 loc) · 1.88 KB
/
common.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
// Copyright 2018 The go-hpb Authors
// This file is part of the go-hpb.
//
// The go-hpb is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-hpb is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-hpb. If not, see <http://www.gnu.org/licenses/>.
#include "common.h"
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#define get_major(version) (version&0xFF>>0x4)
#define get_minor(version) (version&0x0F)
PublicKey_t* new_pubkey(void)
{
PublicKey_t *pub = (PublicKey_t*)malloc(sizeof(PublicKey_t));
return pub;
}
void delete_pubkey(PublicKey_t *pub)
{
if(pub)
free(pub);
}
SignResult_t* new_signresult(void)
{
SignResult_t *result = (SignResult_t*)malloc(sizeof(SignResult_t));
return result;
}
void delete_signresult(SignResult_t *result)
{
if(result)
free(result);
}
uint32_t checksum(uint8_t *data, uint32_t len)
{
uint32_t chk = 0, i = 0;
if(data != NULL && len > 0)
{
for(i = 0;i < len; i++)
{
chk += data[i];
}
}
return chk;
}
uint8_t checksum_byte(uint8_t *data, uint32_t len)
{
uint8_t chk = 0, i = 0;
if(data != NULL && len > 0)
{
for(i = 0; i < len; i++)
{
chk += data[i];
}
}
return chk;
}
uint64_t get_timestamp_us()
{
struct timeval tl;
uint64_t ts = 0;
gettimeofday(&tl, NULL);
ts = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
return ts;
}