-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcmp_mem_access.c
More file actions
74 lines (65 loc) · 1.64 KB
/
Copy pathcmp_mem_access.c
File metadata and controls
74 lines (65 loc) · 1.64 KB
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
#include <string.h>
#include "cmp_mem_access.h"
static bool cmp_mem_reader(struct cmp_ctx_s *ctx, void *data, size_t len)
{
cmp_mem_access_t *mem = (cmp_mem_access_t*)ctx->buf;
if (mem->index + len <= mem->size) {
memcpy(data, &mem->buf[mem->index], len);
mem->index += len;
return true;
} else {
return false;
}
}
static size_t cmp_mem_writer(struct cmp_ctx_s *ctx, const void *data, size_t len)
{
cmp_mem_access_t *mem = (cmp_mem_access_t*)ctx->buf;
if (mem->index + len <= mem->size) {
memcpy(&mem->buf[mem->index], data, len);
mem->index += len;
return len;
} else {
return 0;
}
}
static size_t cmp_mem_writer_ro(struct cmp_ctx_s *ctx, const void *data, size_t len)
{
(void)ctx;
(void)data;
(void)len;
return 0;
}
void cmp_mem_access_init(cmp_ctx_t *cmp, cmp_mem_access_t *m, void *buf, size_t size)
{
m->buf = (char*)buf;
m->size = size;
m->index = 0;
cmp_init(cmp, m, cmp_mem_reader, cmp_mem_writer);
}
void cmp_mem_access_ro_init(cmp_ctx_t *cmp, cmp_mem_access_t *m, const void *buf, size_t size)
{
m->buf = (char*)buf;
m->size = size;
m->index = 0;
cmp_init(cmp, m, cmp_mem_reader, cmp_mem_writer_ro);
}
size_t cmp_mem_access_get_pos(cmp_mem_access_t *m)
{
return m->index;
}
void cmp_mem_access_set_pos(cmp_mem_access_t *m, size_t pos)
{
m->index = pos;
}
void *cmp_mem_access_get_ptr_at_pos(cmp_mem_access_t *m, size_t pos)
{
return m->buf + pos;
}
bool cmp_mem_access_pos_is_valid(cmp_mem_access_t *m, size_t pos)
{
if (pos >= m->size) {
return false;
} else {
return true;
}
}