-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwobitseq.c
226 lines (187 loc) · 4.61 KB
/
twobitseq.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifdef TBS_TEST_MODE_ON
// Testing as a standalone binary in userspace
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#define MALLOC(n) malloc(n)
#define FREE(p) free(p)
#define LOG_EROR(msg, ...) do {\
printf("ERROR: twobitseq_test: %s:%d " msg "\n", \
__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while(0);
#else
// kernel
#include <linux/slab.h>
#include "dm-loki-common.h"
#define MALLOC(n) kmalloc(n, GFP_KERNEL)
#define FREE(p) kfree(p)
#endif
#include "twobitseq.h"
inline static int char_to_int(char c)
{
switch(c) {
case ALLOW_ALL_WRITE_CHAR:
return ALLOW_ALL_WRITE;
case FAIL_ALL_WRITE_CHAR:
return FAIL_ALL_WRITE;
case ALLOW_ONE_WRITE_CHAR:
return ALLOW_ONE_WRITE;
case FAIL_ONE_WRITE_CHAR:
return FAIL_ONE_WRITE;
}
// for safety, always return ALLOW_WRITE
return ALLOW_ALL_WRITE;
}
inline static char int_to_char(int v)
{
switch(v) {
case ALLOW_ALL_WRITE:
return ALLOW_ALL_WRITE_CHAR;
case FAIL_ALL_WRITE:
return FAIL_ALL_WRITE_CHAR;
case ALLOW_ONE_WRITE:
return ALLOW_ONE_WRITE_CHAR;
case FAIL_ONE_WRITE:
return FAIL_ONE_WRITE_CHAR;
}
return '-';
}
int twobitseq_create(twobitseq_t **tbs_ret, const char *s, size_t len)
{
size_t i;
int val;
int err = 0;
twobitseq_t *tbs = NULL;
if (*tbs_ret != NULL) {
// Going to allocate a new thing here.
// Replacing the old one might be a memory leak.
return -EFAULT;
}
// validate string
for(i=0; i < len; i++) {
switch(s[i]) {
case ALLOW_ALL_WRITE_CHAR:
case FAIL_ALL_WRITE_CHAR:
case ALLOW_ONE_WRITE_CHAR:
case FAIL_ONE_WRITE_CHAR:
continue;
default:
LOG_EROR("Invalid character %c", s[i]);
return -EINVAL;
}
}
tbs = (twobitseq_t *) MALLOC (sizeof(*tbs));
if (tbs == NULL) {
LOG_EROR("Failed to allocate tbs");
err = -ENOMEM;
goto bad;
}
// each character takes up 2 bits, so 4 in a byte.
tbs->arr = NULL;
tbs->len = len;
tbs->idx = 0;
tbs->arr = (uint8_t *) MALLOC ((len / 4) + 1);
if (tbs->arr == NULL) {
LOG_EROR("Failed to allocate tbs arr");
err = -ENOMEM;
goto bad;
}
for(i=0; i < (len / 4) + 1; i++) {
tbs->arr[i] = ALLOW_ALL_WRITE_BYTE;
}
for(i=0; i < len; i++) {
val = char_to_int(s[i]);
if (twobitseq_write(tbs, val) != 0) {
// TODO log failure on write
LOG_EROR("Failed to write at idx %zd", i);
err = -EIO;
goto bad;
}
}
if (twobitseq_seek(tbs, 0) != 0) {
// TODO log failure to seek back to 0
LOG_EROR("Failed to seek back to pos 0");
err = -EIO;
goto bad;
}
*tbs_ret = tbs;
return 0;
bad:
twobitseq_free(tbs);
return err;
}
ssize_t twobitseq_seek(twobitseq_t *tbs, size_t pos)
{
if (pos >= tbs->len) {
return -ESPIPE;
}
tbs->idx = pos;
return (ssize_t) tbs->idx;
}
int twobitseq_read(twobitseq_t *tbs)
{
size_t pos = tbs->idx;
uint8_t arr;
int val;
if (pos == tbs->len) {
return -EIO;
}
arr = *(tbs->arr + (pos / 4));
// positions in the arr
// 0 1 2 3 4 5 6 7
// 0 1 2 3
pos = (pos % 4) * 2;
arr = arr >> pos;
val = arr & 0x3;
tbs->idx += 1;
return val;
}
int twobitseq_read_next_or_last(twobitseq_t *tbs)
{
size_t pos = tbs->idx;
if (pos < tbs->len) {
return twobitseq_read(tbs);
}
// we're at the end
tbs->idx = tbs->len - 1;
return twobitseq_read(tbs);
}
char twobitseq_read_next_or_last_as_char(twobitseq_t *tbs) {
return int_to_char(twobitseq_read_next_or_last(tbs));
}
int twobitseq_write(twobitseq_t *tbs, int val)
{
size_t pos = tbs->idx;
uint8_t *arr;
if (pos == tbs->len) {
return -ENOSPC;
}
arr = (tbs->arr + (pos / 4));
pos = (pos % 4) * 2;
val = val << pos;
*arr = (*arr | val);
tbs->idx += 1;
return 0;
}
void twobitseq_free(twobitseq_t *tbs)
{
if (tbs == NULL) {
return;
}
if (tbs->arr != NULL) {
FREE(tbs->arr);
}
FREE(tbs);
}
void twobitseq_dumpstr(twobitseq_t *tbs, char *buf, size_t len) {
size_t i = 0;
size_t old_idx = tbs->idx;
tbs->idx = 0;
while(i < len) {
buf[i] = twobitseq_read_next_or_last_as_char(tbs);
i += 1;
}
tbs->idx = old_idx;
}