-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuf.c
121 lines (100 loc) · 3.05 KB
/
buf.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
/*
libpwsafe - a portable implementation of the passwordsafe format
Copyright © 2011 Noa Resare
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buf.h"
int buf_open(char *filename, unsigned int size, buf_state **state) {
buf_state* s;
s = malloc(sizeof(buf_state));
s->buf = malloc(size);
s->size = size;
if ((s->fd = open(filename, O_RDONLY)) == -1) {
return -1;
}
s->start = 0;
s->end = read(s->fd, s->buf, size);
if (s->end == 0) {
// empty file
return -2;
}
*state = s;
return 0;
}
int buf_read(buf_state *state, int count, unsigned char **target) {
if (state->end - state->start < count) {
if (count > BUFSIZ) {
// FIXME: we can't serve requests this large
return -1;
} else {
// move remaining data to beginning of buffer
ssize_t move_count = state->end - state->start;
memcpy(state->buf, state->buf + state->start, move_count);
ssize_t read_count = read(state->fd, state->buf + move_count,
state->size - move_count);
if (read_count == -1) {
return -2;
}
state->start = 0;
state->end = move_count + read_count;
}
}
*target = state->buf + state->start;
state->start += count;
return 0;
}
int buf_close(buf_state *state) {
int i = close(state->fd);
free(state->buf);
free(state);
return i;
}
#ifdef TEST
static void cmp(void *a, void *b, int count) {
char a_copy[count + 1];
char b_copy[count + 1];
int i;
a_copy[count] = '\0';
b_copy[count] = '\0';
memcpy(a_copy, a, count);
memcpy(b_copy, b, count);
i = memcmp(a, b, count);
if (i != 0) {
printf("read failed, got '%s' instead of '%s'\n", a_copy, b_copy);
} else {
printf("read succeeded\n");
}
}
int main(int argc, char **argv) {
buf_state *s;
unsigned char *p;
char *expected0 = "01234567";
char *expected1 = "89abcdefghijklmn";
char *expected2 = "opqrstuvwxyzABCD";
buf_open("test/test.txt", 32, &s);
buf_read(s, 8, &p);
cmp(p, expected0, 8);
buf_read(s, 16, &p);
cmp(p, expected1, 16);
buf_read(s, 16, &p);
cmp(p, expected2, 16);
buf_close(s);
return 0;
}
#endif