-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
108 lines (93 loc) · 2.85 KB
/
io.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
/* cSploit - a simple penetration testing suite
* Copyright (C) 2014 Massimo Dragano aka tux_mind <tux_mind@csploit.org>
*
* cSploit 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.
*
* cSploit 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 cSploit. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "logger.h"
#include "io.h"
/**
* @brief ensure to read @p size bytes from @p fd into @p buff
* @param fd the file descriptor to read from
* @param buff the buffer where store the readed bytes
* @param size the number of bytes to read
* @returns 0 on success, -1 on error
*/
int read_wrapper(int fd, void *buff, unsigned int size) {
void *pos, *end;
int read_bytes;
pos=buff;
end=(buff + size);
read_bytes=0;
do {
read_bytes=read(fd, pos, end-pos);
pos+=read_bytes;
} while(read_bytes > 0 && pos < end);
if(read_bytes < 0)
print( ERROR, "read: %s", strerror(errno) );
else if(pos == end)
return 0;
else if(pos > buff) // 0 bytes read == fd closed
print( ERROR, "cannot read %d bytes, got %d", size, (unsigned int)(pos - buff) );
return -1;
}
/**
* @brief ensure to write @p size bytes to @p fd from @p buff
* @param fd the file descriptor to write to
* @param buff the source buffer
* @param size the number of bytes to write
* @returns 0 on success, -1 on error
*/
int write_wrapper(int fd, void *buff, unsigned int size) {
void *pos, *end;
int write_bytes;
pos=buff;
end=(buff + size);
write_bytes=0;
do {
write_bytes=write(fd, pos, end-pos);
pos+=write_bytes;
} while(write_bytes > 0 && pos < end);
if(write_bytes < 0)
print( ERROR, "write: %s", strerror(errno) );
else if(pos == end)
return 0;
else if(pos>buff) // 0 bytes wrote == fd closed
print( ERROR, "cannot write %d bytes, sent %d", size, (unsigned int)(pos - buff) );
return -1;
}
/**
* @brief read a @p size of bytes from a @p fd.
* @param fd the file descriptor to read from
* @param size the amount of bytes to read
* @returns a pointer to the readed bytes, or NULL on error.
*/
char *read_chunk(int fd, unsigned int size) {
char *data;
data = malloc(size);
if(!data) {
print( ERROR, "malloc: %s", strerror(errno) );
return NULL;
}
if(read_wrapper(fd, data, size)) {
free(data);
return NULL;
}
return data;
}