-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitchain.c
More file actions
264 lines (234 loc) · 6.53 KB
/
bitchain.c
File metadata and controls
264 lines (234 loc) · 6.53 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "bitchain.h"
//#define BC_DEBUG
#ifdef BC_DEBUG
#define BC_MSG printf
#else
#define BC_MSG(...)
#endif
bc_context *bcw_open(char *fn)
{
bc_context *ctx = (bc_context*)calloc(1, sizeof(bc_context));
ctx->fp = fopen(fn, "wb");
ctx->buf = (BC_UNIT*)malloc(BC_BUF_NBYTES);
return ctx;
}
void bcw_close(bc_context *ctx)
{
if(ctx){
// flush buffer
// pending
bcw_align(ctx);
fwrite(ctx->buf, ctx->buf_pos*sizeof(BC_UNIT), 1, ctx->fp);
fclose(ctx->fp);
free(ctx->buf);
free(ctx);
}
}
void bcw_write(bc_context *ctx, uint64_t bits, uint64_t data)
{
while( bits + ctx->bit_pos >= BC_BLEN){
ctx->data |= (data >> (bits - (BC_BLEN - ctx->bit_pos)));
ctx->buf[ctx->buf_pos++] = ctx->data;
if(ctx->buf_pos == BC_BUF_NELEM){
fwrite(ctx->buf, sizeof(BC_UNIT), BC_BUF_NELEM, ctx->fp);
ctx->buf_pos = 0;
}
bits -= (BC_BLEN - ctx->bit_pos);
ctx->data = 0;
ctx->bit_pos = 0;
}
if(bits){
ctx->data |= data << (BC_BLEN - (ctx->bit_pos + bits));
ctx->bit_pos += bits;
}
}
void bcw_align(bc_context *ctx)
{
ctx->buf[ctx->buf_pos++] = ctx->data;
if(ctx->buf_pos == BC_BUF_NELEM){
fwrite(ctx->buf, sizeof(BC_UNIT), BC_BUF_NELEM, ctx->fp);
ctx->buf_pos = 0;
}
ctx->data = 0;
ctx->bit_pos = 0;
}
int _bcr_fill(bc_context *ctx)
{
if(ctx->end == 0){
ctx->buf_fill = fread(ctx->buf, sizeof(BC_UNIT), BC_BUF_NELEM, ctx->fp);
ctx->end = ctx->buf_fill == BC_BUF_NELEM ? 0 : 1;
return 1;
}
return 0;
}
bc_context *bcr_open(char *fn)
{
bc_context *ctx = (bc_context*)calloc(1, sizeof(bc_context));
ctx->buf = (BC_UNIT*)malloc(BC_BUF_NBYTES);
ctx->fp = fopen(fn, "rb");
_bcr_fill(ctx);
ctx->data = ctx->buf[0];
return ctx;
}
void bcr_close(bc_context *ctx)
{
if(ctx){
fclose(ctx->fp);
free(ctx->buf);
free(ctx);
}
}
void bcr_align(bc_context *ctx, uint64_t *err)
{
*err = BC_OK;
ctx->buf_pos++;
if(ctx->buf_pos == ctx->buf_fill){
if(_bcr_fill(ctx))
ctx->buf_pos = 0;
else{
*err = BC_FILE_END;
return;
}
}
ctx->data = ctx->buf[ctx->buf_pos];
ctx->bit_pos = 0;
}
uint64_t bcr_readbits(bc_context *ctx, uint64_t bits, uint64_t *err)
{
uint64_t value = 0;
*err = BC_OK;
while( (bits + ctx->bit_pos) >= BC_BLEN){
int nbit = (BC_BLEN - ctx->bit_pos);
BC_MSG("B:%lX %lX %ld %d %lX\n", value, ctx->data, bits, ctx->bit_pos, (ctx->data & ((UINT64_C(1) << nbit) - 1)));
uint64_t mask = (nbit == 64) ? ~0UL : ((UINT64_C(1) << nbit) - 1);
value <<= nbit;
value |= (ctx->data & mask);
ctx->buf_pos++;
if(ctx->buf_pos == ctx->buf_fill){
if(_bcr_fill(ctx)){
ctx->buf_pos = 0;
}else{
*err = BC_FILE_END;
return 0;
}
}
ctx->data = ctx->buf[ctx->buf_pos];
ctx->bit_pos = 0;
bits -= nbit;
BC_MSG("E:%lX %lX %ld %d\n", value, ctx->data, bits, ctx->bit_pos);
}
if(bits){
value <<= bits;
value |= (ctx->data >> (BC_BLEN - bits - ctx->bit_pos)) & ((UINT64_C(1) << bits) - 1);
ctx->bit_pos += bits;
}
return value;
}
uint64_t bcr_getbits(bc_context *ctx, uint64_t bits, uint64_t *err)
{
uint64_t value = 0;
*err = BC_OK;
long offset = ftell(ctx->fp);
int buf_pos = ctx->buf_pos;
int bit_pos = ctx->bit_pos;
BC_UNIT data = ctx->data;
while( bits + bit_pos >= BC_BLEN){
int nbit = (BC_BLEN - bit_pos);
uint64_t mask = (nbit == 64) ? ~0UL : ((UINT64_C(1) << nbit) - 1);
value <<= nbit;
value |= (data & mask);
buf_pos++;
if(buf_pos >= ctx->buf_fill){
BC_UNIT tmp;
if(fread(&tmp, sizeof(BC_UNIT), 1, ctx->fp) == 0){
*err = BC_FILE_END;
return 0;
}
data = tmp;
}else{
data = ctx->buf[buf_pos];
}
bits -= (BC_BLEN - bit_pos);
bit_pos = 0;
}
if(bits){
value <<= bits;
value |= (data >> (BC_BLEN - bits - bit_pos)) & ((UINT64_C(1) << bits) - 1);
}
if(offset != ftell(ctx->fp))
fseek(ctx->fp, offset - ftell(ctx->fp), SEEK_CUR);
return value;
}
void bcr_skipbits(bc_context *ctx, uint64_t bits, uint64_t *err)
{
*err = BC_OK;
while( bits + ctx->bit_pos >= BC_BLEN){
ctx->buf_pos++;
if(ctx->buf_pos == ctx->buf_fill){
if(_bcr_fill(ctx)){
ctx->buf_pos = 0;
}else{
*err = BC_FILE_END;
}
}
bits -= (BC_BLEN - ctx->bit_pos);
ctx->bit_pos = 0;
}
ctx->data = ctx->buf[ctx->buf_pos];
ctx->bit_pos += bits;
}
#ifdef BC_TEST
#include <unistd.h>
#include <time.h>
#define NUM_TESTS 1
#define NUM_ITEMS 32
int main(int ac, char **av)
{
int num_tests = NUM_TESTS;
int num_items = NUM_ITEMS;
int opt;
while ((opt = getopt(ac, av, "t:i:")) != -1) {
switch(opt){
case 't':
num_tests = atoi(optarg);
break;
case 'i':
num_items = atoi(optarg);
break;
}
}
printf("loop %d times, each %d r/w times\n", num_tests, num_items);
uint64_t *bits = (uint64_t*)calloc(sizeof(uint64_t), num_items);
uint64_t *values = (uint64_t*)calloc(sizeof(uint64_t), num_items);
srand(time(NULL));
for(int j = 0; j < num_tests; j++){
bc_context* ctx = bcw_open("test.bin");
for(int i = 0; i < num_items; i++){
bits[i] = (rand()%64) + 1;
uint64_t mask = (bits[i] == 64) ? ~0UL : ((UINT64_C(1) << bits[i]) - 1);
values[i] = ((uint64_t)rand()*rand()) & mask;
bcw_write(ctx, bits[i], values[i]);
}
bcw_close(ctx);
ctx = bcr_open("test.bin");
for(int i = 0; i < num_items; i++){
uint64_t err;
#if 1
int64_t value = bcr_readbits(ctx, bits[i], &err);
#else
int64_t value = bcr_getbits(ctx, bits[i], &err);
bcr_skipbits(ctx, bits[i], &err);
#endif
if(err != BC_OK){
printf("abnormal file end\n");
}
if(value != values[i])
printf("%d: bits:%lu %lX %lX\n", i, bits[i], value, values[i]);
}
bcr_close(ctx);
}
free(bits);
free(values);
return 0;
}
#endif