]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/contrib/mfs/mfs_file.c
   2 -------------------------------------------------------------------------------- 
   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 
   9 -------------------------------------------------------------------------------- 
  14 Copyright (c) 1996 Mike Johnson 
  15 Copyright (c) 1996 BancTec AB 
  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. 
  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.   
  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  
  39 -------------------------------------------------------------------------------- 
  41 -------------------------------------------------------------------------------- 
  49 -------------------------------------------------------------------------------- 
  51 -------------------------------------------------------------------------------- 
  59 -------------------------------------------------------------------------------- 
  61 -------------------------------------------------------------------------------- 
  64 static char *buf
[MAX_BUFFS
];        /* Memory for each open buf */ 
  65 static long  buf_off
[MAX_BUFFS
];    /* File pointer for each buf */ 
  66 static long  buf_size
[MAX_BUFFS
];   /* Count of bytes allocated for each buf */ 
  67 static long  fds
[MAX_BUFFS
];        /* File descriptor status */ 
  68 static int   buf_mode
[MAX_BUFFS
];   /* Mode of buffer (r, w, a) */ 
  70 static int library_init_done 
= FALSE
; 
  74 -------------------------------------------------------------------------------- 
  76 -------------------------------------------------------------------------------- 
  79 int mfs_open (void *ptr
, int size
, char *mode
); 
  80 int mfs_lseek (int fd
, int offset
, int whence
); 
  81 int mfs_read (int fd
, void *buf
, int size
); 
  82 int mfs_write (int fd
, void *buf
, int size
); 
  83 int mfs_size (int fd
); 
  84 int mfs_map (int fd
, char **addr
, size_t *len
); 
  85 int mfs_unmap (int fd
); 
  86 int mfs_close (int fd
); 
  87 static int extend_mem_file (int fd
, int size
); 
  88 static void mem_init (); 
  91 -------------------------------------------------------------------------------- 
  93 -------------------------------------------------------------------------------- 
  97 -------------------------------------------------------------------------------- 
  98 -   Function    :   mfs_open () 
 100 -   Arguments   :   Pointer to allocated buffer, initial size of buffer,  
 101 -                   mode spec (r, w, a) 
 103 -   Returns     :   File descriptor or -1 if error. 
 105 -   Description :   Register this area of memory (which has been allocated 
 106 -                   and has a file read into it) under the mem_file library. 
 107 -                   A file descriptor is returned which can the be passed 
 108 -                   back to TIFFClientOpen and used as if it was a disk 
 110 -                   If the call is for mode 'w' then pass (void *)NULL as 
 111 -                   the buffer and zero size and the library will  
 112 -                   allocate memory for you. 
 113 -                   If the mode is append then pass (void *)NULL and size 
 114 -                   zero or with a valid address. 
 116 -------------------------------------------------------------------------------- 
 119 int mfs_open (void *buffer
, int size
, char *mode
) 
 124     if (library_init_done 
== FALSE
) 
 127         library_init_done 
= TRUE
; 
 134     for (i 
= 0; i 
< MAX_BUFFS
; i
++) 
 143     if (i 
== MAX_BUFFS
)     /* No more free descriptors */ 
 149     if (ret 
>= 0 && *mode 
== 'r') 
 151         if (buffer 
== (void *)NULL
) 
 158             buf
[ret
] = (char *)buffer
; 
 159             buf_size
[ret
] = size
; 
 163     else if (ret 
>= 0 && *mode 
== 'w') 
 166         if (buffer 
!= (void *)NULL
) 
 174             tmp 
= malloc (0);   /* Get a pointer */ 
 175             if (tmp 
== (void *)NULL
) 
 182                 buf
[ret
] = (char *)tmp
; 
 188     else if (ret 
>= 0 && *mode 
== 'a') 
 190         if (buffer 
== (void *) NULL
)    /* Create space for client */ 
 192             tmp 
= malloc (0);   /* Get a pointer */ 
 193             if (tmp 
== (void *)NULL
) 
 200                 buf
[ret
] = (char *)tmp
; 
 205         else                            /* Client has file read in already */ 
 207             buf
[ret
] = (char *)buffer
; 
 208             buf_size
[ret
] = size
; 
 212     else        /* Some other invalid combination of parameters */ 
 221         buf_mode
[ret
] = *mode
; 
 228 -------------------------------------------------------------------------------- 
 229 -   Function    :   mfs_lseek () 
 231 -   Arguments   :   File descriptor, offset, whence 
 233 -   Returns     :   as per man lseek (2) 
 235 -   Description :   Does the same as lseek (2) except on a memory based file. 
 236 -                   Note: the memory area will be extended if the caller 
 237 -                   attempts to seek past the current end of file (memory). 
 239 -------------------------------------------------------------------------------- 
 242 int mfs_lseek (int fd
, int offset
, int whence
) 
 247     if (fds
[fd
] == -1)  /* Not open */ 
 252     else if (offset 
< 0 && whence 
== SEEK_SET
) 
 262                 if (offset 
> buf_size
[fd
]) 
 263                     extend_mem_file (fd
, offset
); 
 264                 buf_off
[fd
] = offset
; 
 269                 test_off 
= buf_off
[fd
] + offset
; 
 278                     if (test_off 
> buf_size
[fd
]) 
 279                         extend_mem_file (fd
, test_off
); 
 280                     buf_off
[fd
] = test_off
; 
 286                 test_off 
= buf_size
[fd
] + offset
; 
 295                     if (test_off 
> buf_size
[fd
]) 
 296                         extend_mem_file (fd
, test_off
); 
 297                     buf_off
[fd
] = test_off
; 
 313 -------------------------------------------------------------------------------- 
 314 -   Function    :   mfs_read () 
 316 -   Arguments   :   File descriptor, buffer, size 
 318 -   Returns     :   as per man read (2) 
 320 -   Description :   Does the same as read (2) except on a memory based file. 
 321 -                   Note: An attempt to read past the end of memory currently 
 322 -                   allocated to the file will return 0 (End Of File) 
 324 -------------------------------------------------------------------------------- 
 327 int mfs_read (int fd
, void *clnt_buf
, int size
) 
 331     if (fds
[fd
] == -1 || buf_mode
[fd
] != 'r') 
 333         /* File is either not open, or not opened for read */ 
 338     else if (buf_off
[fd
] + size 
> buf_size
[fd
]) 
 344         memcpy (clnt_buf
, (void *) (buf
[fd
] + buf_off
[fd
]), size
); 
 345         buf_off
[fd
] = buf_off
[fd
] + size
; 
 353 -------------------------------------------------------------------------------- 
 354 -   Function    :   mfs_write () 
 356 -   Arguments   :   File descriptor, buffer, size 
 358 -   Returns     :   as per man write (2) 
 360 -   Description :   Does the same as write (2) except on a memory based file. 
 361 -                   Note: the memory area will be extended if the caller 
 362 -                   attempts to write past the current end of file (memory). 
 364 -------------------------------------------------------------------------------- 
 367 int mfs_write (int fd
, void *clnt_buf
, int size
) 
 371     if (fds
[fd
] == -1 || buf_mode
[fd
] == 'r') 
 373         /* Either the file is not open or it is opened for reading only */ 
 378     else if (buf_mode
[fd
] == 'w') 
 382         if (buf_off
[fd
] + size 
> buf_size
[fd
]) 
 384             extend_mem_file (fd
, buf_off
[fd
] + size
); 
 385             buf_size
[fd
] = (buf_off
[fd
] + size
); 
 388         memcpy ((buf
[fd
] + buf_off
[fd
]), clnt_buf
, size
); 
 389         buf_off
[fd
] = buf_off
[fd
] + size
; 
 397         if (buf_off
[fd
] != buf_size
[fd
]) 
 398             buf_off
[fd
] = buf_size
[fd
]; 
 400         extend_mem_file (fd
, buf_off
[fd
] + size
); 
 401         buf_size
[fd
] += size
; 
 403         memcpy ((buf
[fd
] + buf_off
[fd
]), clnt_buf
, size
); 
 404         buf_off
[fd
] = buf_off
[fd
] + size
; 
 413 -------------------------------------------------------------------------------- 
 414 -   Function    :   mfs_size () 
 416 -   Arguments   :   File descriptor 
 418 -   Returns     :   integer file size 
 420 -   Description :   This function returns the current size of the file in bytes. 
 422 -------------------------------------------------------------------------------- 
 425 int mfs_size (int fd
) 
 429     if (fds
[fd
] == -1)  /* Not open */ 
 441 -------------------------------------------------------------------------------- 
 442 -   Function    :   mfs_map () 
 444 -   Arguments   :   File descriptor, ptr to address, ptr to length 
 446 -   Returns     :   Map status (succeeded or otherwise) 
 448 -   Description :   This function tells the client where the file is mapped 
 449 -                   in memory and what size the mapped area is. It is provided 
 450 -                   to satisfy the MapProc function in libtiff. It pretends 
 451 -                   that the file has been mmap (2)ped. 
 453 -------------------------------------------------------------------------------- 
 456 int mfs_map (int fd
, char **addr
, size_t *len
) 
 460     if (fds
[fd
] == -1)  /* Not open */ 
 476 -------------------------------------------------------------------------------- 
 477 -   Function    :   mfs_unmap () 
 479 -   Arguments   :   File descriptor 
 481 -   Returns     :   UnMap status (succeeded or otherwise) 
 483 -   Description :   This function does nothing as the file is always 
 486 -------------------------------------------------------------------------------- 
 489 int mfs_unmap (int fd
) 
 495 -------------------------------------------------------------------------------- 
 496 -   Function    :   mfs_close () 
 498 -   Arguments   :   File descriptor 
 500 -   Returns     :   close status (succeeded or otherwise) 
 502 -   Description :   Close the open memory file. (Make fd available again.) 
 504 -------------------------------------------------------------------------------- 
 507 int mfs_close (int fd
) 
 511     if (fds
[fd
] == -1)  /* Not open */ 
 526 -------------------------------------------------------------------------------- 
 527 -   Function    :   extend_mem_file () 
 529 -   Arguments   :   File descriptor, length to extend to. 
 531 -   Returns     :   0 - All OK, -1 - realloc () failed. 
 533 -   Description :   Increase the amount of memory allocated to a file. 
 535 -------------------------------------------------------------------------------- 
 538 static int extend_mem_file (int fd
, int size
) 
 543     if ((new_mem 
= realloc (buf
[fd
], size
)) == (void *) NULL
) 
 547         buf
[fd
] = (char *) new_mem
; 
 555 -------------------------------------------------------------------------------- 
 556 -   Function    :   mem_init () 
 562 -   Description :   Initialise the library. 
 564 -------------------------------------------------------------------------------- 
 567 static void mem_init () 
 571     for (i 
= 0; i 
< MAX_BUFFS
; i
++) 
 574         buf
[i
] = (char *)NULL
;