| 1 |
/* |
| 2 |
** io.m |
| 3 |
** |
| 4 |
** Copyright (c) 2004-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2014-2020 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/io.h> |
| 25 |
|
| 26 |
#import <Foundation/NSData.h> |
| 27 |
#import <Foundation/NSString.h> |
| 28 |
|
| 29 |
#include <errno.h> |
| 30 |
#ifdef __MINGW32__ |
| 31 |
#include <io.h> // For _read(), _write() and _close() on MinGW |
| 32 |
#include <winsock2.h> // For recv() on MinGW |
| 33 |
#else |
| 34 |
#include <sys/ioctl.h> |
| 35 |
#include <sys/socket.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <stdlib.h> // For abort() |
| 39 |
#include <stdio.h> |
| 40 |
#include <string.h> // For memset() |
| 41 |
#include <netinet/in.h> // For ntohs() and friends. |
| 42 |
|
| 43 |
#include <unistd.h> // For read(), write() and close() |
| 44 |
|
| 45 |
#ifdef MACOSX |
| 46 |
#include <sys/uio.h> // For read() and write() on OS X |
| 47 |
#endif |
| 48 |
|
| 49 |
#if !defined(FIONBIO) && !defined(__MINGW32__) |
| 50 |
#include <sys/filio.h> // For FIONBIO on Solaris |
| 51 |
#endif |
| 52 |
|
| 53 |
// |
| 54 |
// |
| 55 |
// |
| 56 |
ssize_t read_block(int fd, void *buf, size_t count) |
| 57 |
{ |
| 58 |
size_t tot; |
| 59 |
ssize_t bytes; |
| 60 |
|
| 61 |
tot = 0; |
| 62 |
|
| 63 |
while (tot < count) |
| 64 |
{ |
| 65 |
#ifdef __MINGW32__ |
| 66 |
if ((bytes = _read(fd, buf+tot, count-tot)) == -1) |
| 67 |
#else |
| 68 |
if ((bytes = read(fd, buf+tot, count-tot)) == -1) |
| 69 |
#endif |
| 70 |
{ |
| 71 |
if (errno != EINTR) |
| 72 |
{ |
| 73 |
return -1; |
| 74 |
} |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
tot += (size_t)bytes; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return (ssize_t)tot; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
// |
| 87 |
// |
| 88 |
// |
| 89 |
int safe_close(int fd) |
| 90 |
{ |
| 91 |
int value; |
| 92 |
#ifdef __MINGW32__ |
| 93 |
while (value = _close(fd), value == -1 && errno == EINTR); |
| 94 |
#else |
| 95 |
while (value = close(fd), value == -1 && errno == EINTR); |
| 96 |
#endif |
| 97 |
return value; |
| 98 |
} |
| 99 |
|
| 100 |
// |
| 101 |
// |
| 102 |
// |
| 103 |
ssize_t safe_read(int fd, void *buf, size_t count) |
| 104 |
{ |
| 105 |
ssize_t value; |
| 106 |
#ifdef __MINGW32__ |
| 107 |
while (value = _read(fd, buf, count), value == -1 && errno == EINTR); |
| 108 |
#else |
| 109 |
while (value = read(fd, buf, count), value == -1 && errno == EINTR); |
| 110 |
#endif |
| 111 |
return value; |
| 112 |
} |
| 113 |
|
| 114 |
// |
| 115 |
// |
| 116 |
// |
| 117 |
ssize_t safe_recv(int fd, void *buf, size_t count, int flags) |
| 118 |
{ |
| 119 |
ssize_t value; |
| 120 |
while (value = recv(fd, buf, count, flags), value == -1 && errno == EINTR); |
| 121 |
return value; |
| 122 |
} |
| 123 |
|
| 124 |
// |
| 125 |
// |
| 126 |
// |
| 127 |
NSString *read_string_memory(unsigned char *m, uint16_t *count) |
| 128 |
{ |
| 129 |
uint16_t c0, c1, r; |
| 130 |
|
| 131 |
c0 = *m; |
| 132 |
c1 = *(m+1); |
| 133 |
|
| 134 |
*count = r = (c0<<8)|c1; |
| 135 |
m += 2; |
| 136 |
|
| 137 |
return [[[NSString alloc] initWithBytes:m length:r encoding: NSUTF8StringEncoding] autorelease]; |
| 138 |
} |
| 139 |
|
| 140 |
// |
| 141 |
// |
| 142 |
// |
| 143 |
NSData *read_data_memory(unsigned char *m, uint16_t *count) |
| 144 |
{ |
| 145 |
uint16_t c0, c1, r; |
| 146 |
|
| 147 |
c0 = *m; |
| 148 |
c1 = *(m+1); |
| 149 |
|
| 150 |
*count = r = (c0<<8)|c1; |
| 151 |
m += 2; |
| 152 |
|
| 153 |
// when used on a file buffer, it should outlive the NSData object itself |
| 154 |
return [NSData dataWithBytesNoCopy: m length: r freeWhenDone:NO]; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
// |
| 159 |
// |
| 160 |
// |
| 161 |
uint32_t read_uint32_memory(unsigned char *m) |
| 162 |
{ |
| 163 |
uint32_t c0, c1, c2, c3, r; |
| 164 |
|
| 165 |
c0 = *m; |
| 166 |
c1 = *(m+1); |
| 167 |
c2 = *(m+2); |
| 168 |
c3 = *(m+3); |
| 169 |
|
| 170 |
r = ((c0<<24)|(c1<<16)|(c2<<8)|c3); |
| 171 |
|
| 172 |
return r; |
| 173 |
} |
| 174 |
|
| 175 |
// |
| 176 |
// |
| 177 |
// |
| 178 |
uint16_t read_uint16(int fd) |
| 179 |
{ |
| 180 |
uint16_t v; |
| 181 |
|
| 182 |
if (read(fd, &v, 2) != 2) abort(); |
| 183 |
|
| 184 |
return ntohs(v); |
| 185 |
} |
| 186 |
|
| 187 |
// |
| 188 |
// |
| 189 |
// |
| 190 |
void write_uint16(int fd, uint16_t value) |
| 191 |
{ |
| 192 |
uint16_t v; |
| 193 |
|
| 194 |
v = htons(value); |
| 195 |
|
| 196 |
if (write(fd, &v, 2) != 2) |
| 197 |
{ |
| 198 |
//printf("Error writing cache, aborting."); |
| 199 |
abort(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// |
| 204 |
// |
| 205 |
// |
| 206 |
ssize_t read_string(int fd, char *buf, uint16_t *count) |
| 207 |
{ |
| 208 |
*count = read_uint16(fd); |
| 209 |
|
| 210 |
if (*count) |
| 211 |
{ |
| 212 |
ssize_t nbytes; |
| 213 |
nbytes = read(fd, buf, *count); |
| 214 |
if (nbytes == (ssize_t)*count) |
| 215 |
return nbytes; |
| 216 |
if (nbytes == 0) |
| 217 |
{ |
| 218 |
printf("read_string: EOF\n"); |
| 219 |
} |
| 220 |
else if (nbytes == -1) |
| 221 |
{ |
| 222 |
printf("read_string: error\n"); |
| 223 |
} |
| 224 |
printf("read_string: read less bytes than expected\n"); |
| 225 |
return nbytes; |
| 226 |
} |
| 227 |
return 0; // FIXME this might be a legitimate 0 or an issue with read_uint16 failed |
| 228 |
} |
| 229 |
|
| 230 |
// |
| 231 |
// |
| 232 |
// |
| 233 |
void write_string(int fd, unsigned char *s, size_t len) |
| 234 |
{ |
| 235 |
if (s && len > 0) |
| 236 |
{ |
| 237 |
write_uint16(fd, len); |
| 238 |
if (write(fd, s, len) != (ssize_t)len) |
| 239 |
{ |
| 240 |
//NSLog(@"FAILED TO WRITE BYTES, ABORT"); |
| 241 |
abort(); |
| 242 |
} |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
write_uint16(fd, 0); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void write_data(int fd, NSData *d) |
| 251 |
{ |
| 252 |
write_string (fd, (unsigned char *)[d bytes], [d length]); |
| 253 |
} |
| 254 |
|
| 255 |
// |
| 256 |
// |
| 257 |
// |
| 258 |
int read_uint32(int fd, uint32_t *val) |
| 259 |
{ |
| 260 |
uint32_t v; |
| 261 |
int c; |
| 262 |
|
| 263 |
if ((c = (int)read(fd, &v, 4)) != 4) |
| 264 |
{ |
| 265 |
if (c == 0) |
| 266 |
printf("read_unsinged_int: EOF\n"); |
| 267 |
else |
| 268 |
printf("read_unisnged_int unexpected :read = %d\n", c); |
| 269 |
} |
| 270 |
|
| 271 |
*val = htonl(v); |
| 272 |
return c; |
| 273 |
} |
| 274 |
|
| 275 |
// |
| 276 |
// |
| 277 |
// |
| 278 |
void write_uint32(int fd, uint32_t value) |
| 279 |
{ |
| 280 |
uint32_t v; |
| 281 |
ssize_t r; |
| 282 |
|
| 283 |
v = htonl(value); |
| 284 |
|
| 285 |
//printf("b|%d| a|%d|", value, v); |
| 286 |
|
| 287 |
r = write(fd, &v, 4); |
| 288 |
if (r < 0) |
| 289 |
{ |
| 290 |
if (errno == EFBIG) |
| 291 |
printf("ERROR WRITING CACHE: file too big"); |
| 292 |
else if (errno == EACCES) |
| 293 |
printf("ERROR WRITING CACHE: permission denied"); |
| 294 |
else |
| 295 |
printf("ERROR WRITING CACHE: %d", errno); |
| 296 |
abort(); |
| 297 |
} |
| 298 |
else if (r != 4) |
| 299 |
{ |
| 300 |
printf("ERROR WRITING CACHE: Wrote %ld out of 4", (long)r); |
| 301 |
abort(); |
| 302 |
} |
| 303 |
} |