| 1 |
/* |
| 2 |
-------------------------------------------------------------------------------- |
| 3 |
- Module : mem_file.c |
| 4 |
- Description : A general purpose library for manipulating a memory area |
| 5 |
- as if it were a file. |
| 6 |
- mfs_ stands for memory file system. |
| 7 |
- Author : Mike Johnson - Banctec AB 03/07/96 |
| 8 |
- |
| 9 |
-------------------------------------------------------------------------------- |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
|
| 14 |
Copyright (c) 1996 Mike Johnson |
| 15 |
Copyright (c) 1996 BancTec AB |
| 16 |
|
| 17 |
Permission to use, copy, modify, distribute, and sell this software |
| 18 |
for any purpose is hereby granted without fee, provided |
| 19 |
that (i) the above copyright notices and this permission notice appear in |
| 20 |
all copies of the software and related documentation, and (ii) the names of |
| 21 |
Mike Johnson and BancTec may not be used in any advertising or |
| 22 |
publicity relating to the software. |
| 23 |
|
| 24 |
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 25 |
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 26 |
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 27 |
|
| 28 |
IN NO EVENT SHALL MIKE JOHNSON OR BANCTEC BE LIABLE FOR |
| 29 |
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 30 |
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 31 |
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 32 |
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 33 |
OF THIS SOFTWARE. |
| 34 |
|
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
-------------------------------------------------------------------------------- |
| 39 |
- Includes |
| 40 |
-------------------------------------------------------------------------------- |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <string.h> |
| 46 |
|
| 47 |
/* |
| 48 |
-------------------------------------------------------------------------------- |
| 49 |
- Definitions |
| 50 |
-------------------------------------------------------------------------------- |
| 51 |
*/ |
| 52 |
|
| 53 |
#define MAX_BUFFS 20 |
| 54 |
#define FALSE 0 |
| 55 |
#define TRUE 1 |
| 56 |
|
| 57 |
/* |
| 58 |
-------------------------------------------------------------------------------- |
| 59 |
- Globals |
| 60 |
-------------------------------------------------------------------------------- |
| 61 |
*/ |
| 62 |
|
| 63 |
static char *buf[MAX_BUFFS]; /* Memory for each open buf */ |
| 64 |
static long buf_off[MAX_BUFFS]; /* File pointer for each buf */ |
| 65 |
static long buf_size[MAX_BUFFS]; /* Count of bytes allocated for each buf */ |
| 66 |
static long fds[MAX_BUFFS]; /* File descriptor status */ |
| 67 |
static int buf_mode[MAX_BUFFS]; /* Mode of buffer (r, w, a) */ |
| 68 |
|
| 69 |
static int library_init_done = FALSE; |
| 70 |
|
| 71 |
/* |
| 72 |
-------------------------------------------------------------------------------- |
| 73 |
- Function prototypes |
| 74 |
-------------------------------------------------------------------------------- |
| 75 |
*/ |
| 76 |
|
| 77 |
int mfs_open(void *ptr, int size, char *mode); |
| 78 |
int mfs_lseek(int fd, int offset, int whence); |
| 79 |
int mfs_read(int fd, void *buf, int size); |
| 80 |
int mfs_write(int fd, void *buf, int size); |
| 81 |
int mfs_size(int fd); |
| 82 |
int mfs_map(int fd, char **addr, size_t *len); |
| 83 |
int mfs_unmap(int fd); |
| 84 |
int mfs_close(int fd); |
| 85 |
static int extend_mem_file(int fd, int size); |
| 86 |
static void mem_init(); |
| 87 |
|
| 88 |
/* |
| 89 |
-------------------------------------------------------------------------------- |
| 90 |
- Function code |
| 91 |
-------------------------------------------------------------------------------- |
| 92 |
*/ |
| 93 |
|
| 94 |
/* |
| 95 |
-------------------------------------------------------------------------------- |
| 96 |
- Function : mfs_open () |
| 97 |
- |
| 98 |
- Arguments : Pointer to allocated buffer, initial size of buffer, |
| 99 |
- mode spec (r, w, a) |
| 100 |
- |
| 101 |
- Returns : File descriptor or -1 if error. |
| 102 |
- |
| 103 |
- Description : Register this area of memory (which has been allocated |
| 104 |
- and has a file read into it) under the mem_file library. |
| 105 |
- A file descriptor is returned which can the be passed |
| 106 |
- back to TIFFClientOpen and used as if it was a disk |
| 107 |
- based fd. |
| 108 |
- If the call is for mode 'w' then pass (void *)NULL as |
| 109 |
- the buffer and zero size and the library will |
| 110 |
- allocate memory for you. |
| 111 |
- If the mode is append then pass (void *)NULL and size |
| 112 |
- zero or with a valid address. |
| 113 |
- |
| 114 |
-------------------------------------------------------------------------------- |
| 115 |
*/ |
| 116 |
|
| 117 |
int mfs_open(void *buffer, int size, char *mode) |
| 118 |
{ |
| 119 |
int ret, i; |
| 120 |
void *tmp; |
| 121 |
|
| 122 |
if (library_init_done == FALSE) |
| 123 |
{ |
| 124 |
mem_init(); |
| 125 |
library_init_done = TRUE; |
| 126 |
} |
| 127 |
|
| 128 |
ret = -1; |
| 129 |
|
| 130 |
/* Find a free fd */ |
| 131 |
|
| 132 |
for (i = 0; i < MAX_BUFFS; i++) |
| 133 |
{ |
| 134 |
if (fds[i] == -1) |
| 135 |
{ |
| 136 |
ret = i; |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (i == MAX_BUFFS) /* No more free descriptors */ |
| 142 |
{ |
| 143 |
ret = -1; |
| 144 |
errno = EMFILE; |
| 145 |
} |
| 146 |
|
| 147 |
if (ret >= 0 && *mode == 'r') |
| 148 |
{ |
| 149 |
if (buffer == (void *)NULL) |
| 150 |
{ |
| 151 |
ret = -1; |
| 152 |
errno = EINVAL; |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
buf[ret] = (char *)buffer; |
| 157 |
buf_size[ret] = size; |
| 158 |
buf_off[ret] = 0; |
| 159 |
} |
| 160 |
} |
| 161 |
else if (ret >= 0 && *mode == 'w') |
| 162 |
{ |
| 163 |
|
| 164 |
if (buffer != (void *)NULL) |
| 165 |
{ |
| 166 |
ret = -1; |
| 167 |
errno = EINVAL; |
| 168 |
} |
| 169 |
|
| 170 |
else |
| 171 |
{ |
| 172 |
tmp = malloc(0); /* Get a pointer */ |
| 173 |
if (tmp == (void *)NULL) |
| 174 |
{ |
| 175 |
ret = -1; |
| 176 |
errno = EDQUOT; |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
buf[ret] = (char *)tmp; |
| 181 |
buf_size[ret] = 0; |
| 182 |
buf_off[ret] = 0; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
else if (ret >= 0 && *mode == 'a') |
| 187 |
{ |
| 188 |
if (buffer == (void *)NULL) /* Create space for client */ |
| 189 |
{ |
| 190 |
tmp = malloc(0); /* Get a pointer */ |
| 191 |
if (tmp == (void *)NULL) |
| 192 |
{ |
| 193 |
ret = -1; |
| 194 |
errno = EDQUOT; |
| 195 |
} |
| 196 |
else |
| 197 |
{ |
| 198 |
buf[ret] = (char *)tmp; |
| 199 |
buf_size[ret] = 0; |
| 200 |
buf_off[ret] = 0; |
| 201 |
} |
| 202 |
} |
| 203 |
else /* Client has file read in already */ |
| 204 |
{ |
| 205 |
buf[ret] = (char *)buffer; |
| 206 |
buf_size[ret] = size; |
| 207 |
buf_off[ret] = 0; |
| 208 |
} |
| 209 |
} |
| 210 |
else /* Some other invalid combination of parameters */ |
| 211 |
{ |
| 212 |
ret = -1; |
| 213 |
errno = EINVAL; |
| 214 |
} |
| 215 |
|
| 216 |
if (ret != -1) |
| 217 |
{ |
| 218 |
fds[ret] = 0; |
| 219 |
buf_mode[ret] = *mode; |
| 220 |
} |
| 221 |
|
| 222 |
return (ret); |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
-------------------------------------------------------------------------------- |
| 227 |
- Function : mfs_lseek () |
| 228 |
- |
| 229 |
- Arguments : File descriptor, offset, whence |
| 230 |
- |
| 231 |
- Returns : as per man lseek (2) |
| 232 |
- |
| 233 |
- Description : Does the same as lseek (2) except on a memory based file. |
| 234 |
- Note: the memory area will be extended if the caller |
| 235 |
- attempts to seek past the current end of file (memory). |
| 236 |
- |
| 237 |
-------------------------------------------------------------------------------- |
| 238 |
*/ |
| 239 |
|
| 240 |
int mfs_lseek(int fd, int offset, int whence) |
| 241 |
{ |
| 242 |
int ret; |
| 243 |
long test_off; |
| 244 |
|
| 245 |
if (fds[fd] == -1) /* Not open */ |
| 246 |
{ |
| 247 |
ret = -1; |
| 248 |
errno = EBADF; |
| 249 |
} |
| 250 |
else if (offset < 0 && whence == SEEK_SET) |
| 251 |
{ |
| 252 |
ret = -1; |
| 253 |
errno = EINVAL; |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
switch (whence) |
| 258 |
{ |
| 259 |
case SEEK_SET: |
| 260 |
if (offset > buf_size[fd]) |
| 261 |
extend_mem_file(fd, offset); |
| 262 |
buf_off[fd] = offset; |
| 263 |
ret = offset; |
| 264 |
break; |
| 265 |
|
| 266 |
case SEEK_CUR: |
| 267 |
test_off = buf_off[fd] + offset; |
| 268 |
|
| 269 |
if (test_off < 0) |
| 270 |
{ |
| 271 |
ret = -1; |
| 272 |
errno = EINVAL; |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
if (test_off > buf_size[fd]) |
| 277 |
extend_mem_file(fd, test_off); |
| 278 |
buf_off[fd] = test_off; |
| 279 |
ret = test_off; |
| 280 |
} |
| 281 |
break; |
| 282 |
|
| 283 |
case SEEK_END: |
| 284 |
test_off = buf_size[fd] + offset; |
| 285 |
|
| 286 |
if (test_off < 0) |
| 287 |
{ |
| 288 |
ret = -1; |
| 289 |
errno = EINVAL; |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
if (test_off > buf_size[fd]) |
| 294 |
extend_mem_file(fd, test_off); |
| 295 |
buf_off[fd] = test_off; |
| 296 |
ret = test_off; |
| 297 |
} |
| 298 |
break; |
| 299 |
|
| 300 |
default: |
| 301 |
errno = EINVAL; |
| 302 |
ret = -1; |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
return (ret); |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
-------------------------------------------------------------------------------- |
| 312 |
- Function : mfs_read () |
| 313 |
- |
| 314 |
- Arguments : File descriptor, buffer, size |
| 315 |
- |
| 316 |
- Returns : as per man read (2) |
| 317 |
- |
| 318 |
- Description : Does the same as read (2) except on a memory based file. |
| 319 |
- Note: An attempt to read past the end of memory currently |
| 320 |
- allocated to the file will return 0 (End Of File) |
| 321 |
- |
| 322 |
-------------------------------------------------------------------------------- |
| 323 |
*/ |
| 324 |
|
| 325 |
int mfs_read(int fd, void *clnt_buf, int size) |
| 326 |
{ |
| 327 |
int ret; |
| 328 |
|
| 329 |
if (fds[fd] == -1 || buf_mode[fd] != 'r') |
| 330 |
{ |
| 331 |
/* File is either not open, or not opened for read */ |
| 332 |
|
| 333 |
ret = -1; |
| 334 |
errno = EBADF; |
| 335 |
} |
| 336 |
else if (buf_off[fd] + size > buf_size[fd]) |
| 337 |
{ |
| 338 |
ret = 0; /* EOF */ |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
memcpy(clnt_buf, (void *)(buf[fd] + buf_off[fd]), size); |
| 343 |
buf_off[fd] = buf_off[fd] + size; |
| 344 |
ret = size; |
| 345 |
} |
| 346 |
|
| 347 |
return (ret); |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
-------------------------------------------------------------------------------- |
| 352 |
- Function : mfs_write () |
| 353 |
- |
| 354 |
- Arguments : File descriptor, buffer, size |
| 355 |
- |
| 356 |
- Returns : as per man write (2) |
| 357 |
- |
| 358 |
- Description : Does the same as write (2) except on a memory based file. |
| 359 |
- Note: the memory area will be extended if the caller |
| 360 |
- attempts to write past the current end of file (memory). |
| 361 |
- |
| 362 |
-------------------------------------------------------------------------------- |
| 363 |
*/ |
| 364 |
|
| 365 |
int mfs_write(int fd, void *clnt_buf, int size) |
| 366 |
{ |
| 367 |
int ret; |
| 368 |
|
| 369 |
if (fds[fd] == -1 || buf_mode[fd] == 'r') |
| 370 |
{ |
| 371 |
/* Either the file is not open or it is opened for reading only */ |
| 372 |
|
| 373 |
ret = -1; |
| 374 |
errno = EBADF; |
| 375 |
} |
| 376 |
else if (buf_mode[fd] == 'w') |
| 377 |
{ |
| 378 |
/* Write */ |
| 379 |
|
| 380 |
if (buf_off[fd] + size > buf_size[fd]) |
| 381 |
{ |
| 382 |
extend_mem_file(fd, buf_off[fd] + size); |
| 383 |
buf_size[fd] = (buf_off[fd] + size); |
| 384 |
} |
| 385 |
|
| 386 |
memcpy((buf[fd] + buf_off[fd]), clnt_buf, size); |
| 387 |
buf_off[fd] = buf_off[fd] + size; |
| 388 |
|
| 389 |
ret = size; |
| 390 |
} |
| 391 |
else |
| 392 |
{ |
| 393 |
/* Append */ |
| 394 |
|
| 395 |
if (buf_off[fd] != buf_size[fd]) |
| 396 |
buf_off[fd] = buf_size[fd]; |
| 397 |
|
| 398 |
extend_mem_file(fd, buf_off[fd] + size); |
| 399 |
buf_size[fd] += size; |
| 400 |
|
| 401 |
memcpy((buf[fd] + buf_off[fd]), clnt_buf, size); |
| 402 |
buf_off[fd] = buf_off[fd] + size; |
| 403 |
|
| 404 |
ret = size; |
| 405 |
} |
| 406 |
|
| 407 |
return (ret); |
| 408 |
} |
| 409 |
|
| 410 |
/* |
| 411 |
-------------------------------------------------------------------------------- |
| 412 |
- Function : mfs_size () |
| 413 |
- |
| 414 |
- Arguments : File descriptor |
| 415 |
- |
| 416 |
- Returns : integer file size |
| 417 |
- |
| 418 |
- Description : This function returns the current size of the file in bytes. |
| 419 |
- |
| 420 |
-------------------------------------------------------------------------------- |
| 421 |
*/ |
| 422 |
|
| 423 |
int mfs_size(int fd) |
| 424 |
{ |
| 425 |
int ret; |
| 426 |
|
| 427 |
if (fds[fd] == -1) /* Not open */ |
| 428 |
{ |
| 429 |
ret = -1; |
| 430 |
errno = EBADF; |
| 431 |
} |
| 432 |
else |
| 433 |
ret = buf_size[fd]; |
| 434 |
|
| 435 |
return (ret); |
| 436 |
} |
| 437 |
|
| 438 |
/* |
| 439 |
-------------------------------------------------------------------------------- |
| 440 |
- Function : mfs_map () |
| 441 |
- |
| 442 |
- Arguments : File descriptor, ptr to address, ptr to length |
| 443 |
- |
| 444 |
- Returns : Map status (succeeded or otherwise) |
| 445 |
- |
| 446 |
- Description : This function tells the client where the file is mapped |
| 447 |
- in memory and what size the mapped area is. It is provided |
| 448 |
- to satisfy the MapProc function in libtiff. It pretends |
| 449 |
- that the file has been mmap (2)ped. |
| 450 |
- |
| 451 |
-------------------------------------------------------------------------------- |
| 452 |
*/ |
| 453 |
|
| 454 |
int mfs_map(int fd, char **addr, size_t *len) |
| 455 |
{ |
| 456 |
int ret; |
| 457 |
|
| 458 |
if (fds[fd] == -1) /* Not open */ |
| 459 |
{ |
| 460 |
ret = -1; |
| 461 |
errno = EBADF; |
| 462 |
} |
| 463 |
else |
| 464 |
{ |
| 465 |
*addr = buf[fd]; |
| 466 |
*len = buf_size[fd]; |
| 467 |
ret = 0; |
| 468 |
} |
| 469 |
|
| 470 |
return (ret); |
| 471 |
} |
| 472 |
|
| 473 |
/* |
| 474 |
-------------------------------------------------------------------------------- |
| 475 |
- Function : mfs_unmap () |
| 476 |
- |
| 477 |
- Arguments : File descriptor |
| 478 |
- |
| 479 |
- Returns : UnMap status (succeeded or otherwise) |
| 480 |
- |
| 481 |
- Description : This function does nothing as the file is always |
| 482 |
- in memory. |
| 483 |
- |
| 484 |
-------------------------------------------------------------------------------- |
| 485 |
*/ |
| 486 |
|
| 487 |
int mfs_unmap(int fd) { return (0); } |
| 488 |
|
| 489 |
/* |
| 490 |
-------------------------------------------------------------------------------- |
| 491 |
- Function : mfs_close () |
| 492 |
- |
| 493 |
- Arguments : File descriptor |
| 494 |
- |
| 495 |
- Returns : close status (succeeded or otherwise) |
| 496 |
- |
| 497 |
- Description : Close the open memory file. (Make fd available again.) |
| 498 |
- |
| 499 |
-------------------------------------------------------------------------------- |
| 500 |
*/ |
| 501 |
|
| 502 |
int mfs_close(int fd) |
| 503 |
{ |
| 504 |
int ret; |
| 505 |
|
| 506 |
if (fds[fd] == -1) /* Not open */ |
| 507 |
{ |
| 508 |
ret = -1; |
| 509 |
errno = EBADF; |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
fds[fd] = -1; |
| 514 |
ret = 0; |
| 515 |
} |
| 516 |
|
| 517 |
return (ret); |
| 518 |
} |
| 519 |
|
| 520 |
/* |
| 521 |
-------------------------------------------------------------------------------- |
| 522 |
- Function : extend_mem_file () |
| 523 |
- |
| 524 |
- Arguments : File descriptor, length to extend to. |
| 525 |
- |
| 526 |
- Returns : 0 - All OK, -1 - realloc () failed. |
| 527 |
- |
| 528 |
- Description : Increase the amount of memory allocated to a file. |
| 529 |
- |
| 530 |
-------------------------------------------------------------------------------- |
| 531 |
*/ |
| 532 |
|
| 533 |
static int extend_mem_file(int fd, int size) |
| 534 |
{ |
| 535 |
void *new_mem; |
| 536 |
int ret; |
| 537 |
|
| 538 |
if ((new_mem = realloc(buf[fd], size)) == (void *)NULL) |
| 539 |
ret = -1; |
| 540 |
else |
| 541 |
{ |
| 542 |
buf[fd] = (char *)new_mem; |
| 543 |
ret = 0; |
| 544 |
} |
| 545 |
|
| 546 |
return (ret); |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
-------------------------------------------------------------------------------- |
| 551 |
- Function : mem_init () |
| 552 |
- |
| 553 |
- Arguments : None |
| 554 |
- |
| 555 |
- Returns : void |
| 556 |
- |
| 557 |
- Description : Initialise the library. |
| 558 |
- |
| 559 |
-------------------------------------------------------------------------------- |
| 560 |
*/ |
| 561 |
|
| 562 |
static void mem_init() |
| 563 |
{ |
| 564 |
int i; |
| 565 |
|
| 566 |
for (i = 0; i < MAX_BUFFS; i++) |
| 567 |
{ |
| 568 |
fds[i] = -1; |
| 569 |
buf[i] = (char *)NULL; |
| 570 |
buf_size[i] = 0; |
| 571 |
buf_off[i] = 0; |
| 572 |
} |
| 573 |
} |