-
Notifications
You must be signed in to change notification settings - Fork 8
/
dump_utils.c
126 lines (87 loc) · 2.28 KB
/
dump_utils.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
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "dump_utils.h"
#include <arpa/inet.h>
/* Reading */
size_t read_header(dump_h *h, FILE *file)
{
size_t ret = 0;
uint32_t poscount;
uint8_t flag;
uint32_t minfrac;
ret = fread(&poscount, sizeof poscount, 1, file);
ret = fread(&flag, sizeof flag, 1, file);
ret = fread(&minfrac, sizeof minfrac, 1, file);
h->poscount = ntohl(poscount);
h->flag = flag;
h->minfrac = (float)ntohl(minfrac) / 1000;
if (h->flag) {
uint32_t hhash;
ret = fread(&hhash, sizeof hhash, 1, file);
h->hhash = ntohl(hhash);
}
return ret;
}
size_t read_pos(dump_p *p, FILE *file, dump_h *h)
{
size_t ret = 0;
if (h->flag) {
uint32_t tid;
uint32_t pr;
ret = fread(&tid, sizeof tid, 1, file);
ret = fread(&pr, sizeof pr, 1, file);
p->tid = ntohl(tid);
p->p = ntohl(pr);
}
uint16_t c;
uint8_t num;
ret = fread(&c, sizeof c, 1, file);
ret = fread(&num, sizeof num, 1, file);
p->c = ntohs(c);
p->num = num;
if (ret == 0) // break early to avoid accessing undefined p->num
return ret;
uint16_t pos[6];
memset(pos, 0, sizeof(pos));
ret = fread(&pos, sizeof pos[0], p->num, file);
memset(p->pos, 0, sizeof(p->pos));
int i;
for(i = 0; i < p->num; ++i)
p->pos[i] = ntohs(pos[i]);
return ret;
}
/* Writing */
void write_header(dump_h *h, FILE *file)
{
uint32_t poscount = htonl(h->poscount);
uint8_t flag = h->flag;
uint32_t minfrac = (uint32_t)htonl(round(h->minfrac * 1000));
uint32_t hhash = htonl(h->hhash);
fwrite(&poscount, sizeof poscount, 1, file);
fwrite(&flag, sizeof flag, 1, file);
fwrite(&minfrac, sizeof minfrac, 1, file);
if (h->flag)
fwrite(&hhash, sizeof hhash, 1, file);
}
void write_pos(dump_p *p, FILE *file, dump_h *h)
{
uint32_t tid = htonl(p->tid);
uint32_t pr = htonl(p->p);
uint16_t c = htons(p->c);
uint8_t num = p->num;
if (h->flag) {
fwrite(&tid, sizeof tid, 1, file);
fwrite(&pr, sizeof pr, 1, file);
}
fwrite(&c, sizeof c, 1, file);
fwrite(&num, sizeof num, 1, file);
uint16_t pos[6];
memset(pos, 0, sizeof(pos));
int i;
for(i = 0; i < p->num; ++i)
pos[i] = htons(p->pos[i]);
fwrite(&pos, sizeof pos[0], p->num, file);
}