-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbufscap.c
111 lines (92 loc) · 2.05 KB
/
bufscap.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
#include <stdint.h>
#include <string.h>
#include "bufscap.h"
int bufscap_read(struct scap_reader* r, void* target, uint32_t len)
{
struct bufscap_handle* bsh = (struct bufscap_handle*)r->handle;
const uint8_t* const inbuf = bsh->buf;
uint8_t* outbuf = (uint8_t*)target;
if (!inbuf || !outbuf)
{
return 0;
}
uint32_t remaining_len = bsh->len - bsh->offset;
if (remaining_len < len)
{
len = remaining_len;
}
memcpy(outbuf, &inbuf[bsh->offset], len);
bsh->offset += len;
return len;
}
int64_t bufscap_offset(struct scap_reader* r)
{
struct bufscap_handle* bsh = (struct bufscap_handle*)r->handle;
return bsh->offset;
}
int64_t bufscap_tell(struct scap_reader* r)
{
// I think this is the right thing to do for this handle type...
return bufscap_offset(r);
}
int64_t bufscap_seek(struct scap_reader* r, int64_t offset, int whence)
{
struct bufscap_handle* bsh = (struct bufscap_handle*)r->handle;
int64_t start = 0;
switch (whence)
{
case SEEK_SET:
start = 0;
break;
case SEEK_CUR:
start = bsh->offset;
break;
case SEEK_END:
start = bsh->len;
break;
}
if (offset + start > bsh->len)
{
bsh->offset = bsh->len;
}
else
{
bsh->offset = offset + start;
}
return bsh->offset;
}
const char* bufscap_error(struct scap_reader* r, int* errnum)
{
// We never have errors :D
errnum = 0;
return 0;
}
int bufscap_close(struct scap_reader* r)
{
// Don't delete anything
return 0;
}
/*******************
* SCAP reader
*/
scap_reader_t* build_reader_from_buffer(const uint8_t* const buf, int64_t len)
{
uint8_t* rd_buf = malloc(sizeof(scap_reader_t) + sizeof(struct bufscap_handle));
scap_reader_t* ret = (scap_reader_t*)rd_buf;
struct bufscap_handle* handle = (struct bufscap_handle*)(rd_buf + sizeof(*ret));
handle->buf = buf;
handle->len = len;
handle->offset = 0;
ret->handle = handle;
ret->read = bufscap_read;
ret->offset = bufscap_offset;
ret->tell = bufscap_tell;
ret->seek = bufscap_seek;
ret->error = bufscap_error;
ret->close = bufscap_close;
return ret;
}
void free_reader(scap_reader_t* r)
{
free(r);
}