-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread.c
98 lines (75 loc) · 1.5 KB
/
read.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "snoop.h"
#include "bele.h"
int
unpackglobalhdr(GlobalHdr *hdr, uint8_t *buf, unsigned int size)
{
uint8_t *p;
if (size < GlobalHdrSize)
return -1;
p = buf;
p += be32get(p, &hdr->magic);
p += be16get(p, &hdr->version_major);
p += be16get(p, &hdr->version_minor);
p += be32get(p, (uint32_t*)&hdr->thiszone);
p += be32get(p, &hdr->snaplen);
p += be32get(p, &hdr->sigfigs);
p += be32get(p, &hdr->linktype);
return p-buf;
}
int
unpackrecordhdr(RecordHdr *hdr, uint8_t *buf, unsigned int size)
{
uint8_t *p;
if (size < RecordHdrSize)
return -1;
p = buf;
p += be32get(p, &hdr->ts_sec);
p += be32get(p, &hdr->ts_usec);
p += be32get(p, &hdr->caplen);
p += be32get(p, &hdr->len);
return p-buf;
}
int
readglobalhdr(FILE *f, GlobalHdr *hdr)
{
uint8_t buf[GlobalHdrSize];
if (fread(buf, sizeof(buf), 1, f) != 1) {
if (feof(f))
return 0;
return -1;
}
if (unpackglobalhdr(hdr, buf, sizeof(buf)) < 0)
return -1;
return 0;
}
int
readrecordhdr(FILE *f, RecordHdr *hdr)
{
uint8_t buf[RecordHdrSize];
int n;
if (fread(buf, sizeof(buf), 1, f) != 1) {
if (feof(f))
return 0;
return -1;
}
if ((n = unpackrecordhdr(hdr, buf, sizeof(buf))) < 0)
return -1;
return n;
}
int
readrecord(FILE *f, RecordHdr *hdr, uint8_t *buf)
{
int n;
if ((n = readrecordhdr(f, hdr)) <= 0)
return n;
if (fread(buf, hdr->caplen, 1, f) != 1) {
if (feof(f))
return 0;
return -1;
}
return hdr->caplen;
}