home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / tiff / tif_compat.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  6KB  |  230 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_compat.c,v 1.15 92/03/11 09:16:49 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library Compatibility Routines.
  31.  */
  32. #include "tiffioP.h"
  33.  
  34. #if defined(unix) || defined(__unix) || defined(MSDOS) || defined(VMS)
  35. #include <sys/stat.h>
  36.  
  37. long
  38. TIFFGetFileSize(fd)
  39.     int fd;
  40. {
  41.     struct stat sb;
  42.  
  43.     return (fstat(fd, &sb) < 0 ? 0 : sb.st_size);
  44. }
  45. #endif
  46.  
  47. #if (defined(unix) || defined(__unix)) && defined(MMAP_SUPPORT)
  48. #include <sys/mman.h>
  49.  
  50. int
  51. TIFFMapFileContents(fd, pbase, psize)
  52.     int fd;
  53.     char **pbase;
  54.     long *psize;
  55. {
  56.     long size = TIFFGetFileSize(fd);
  57.     if (size != -1) {
  58.         *pbase = (char *) mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
  59.         if (*pbase != (char *)-1) {
  60.             *psize = size;
  61.             return (1);
  62.         }
  63.     }
  64.     return (0);
  65. }
  66.  
  67. void
  68. TIFFUnmapFileContents(base, size)
  69.     char *base;
  70.     long size;
  71. {
  72.     (void) munmap(base, size);
  73. }
  74. #endif /* (defined(unix) || defined(__unix)) && defined(MMAP_SUPPORT) */
  75.  
  76. #if defined(VMS) && defined(MMAP_SUPPORT)
  77. #include <fab.h>
  78. #include <secdef.h>
  79.  
  80. /*
  81.  * Table for storing information on current open sections. 
  82.  * (You may want to substitute a linked list...)
  83.  */
  84. #define MAX_MAPPED 100
  85. static int no_mapped = 0;
  86. static struct {
  87.     void *base;
  88.     void *top;
  89.     unsigned channel;
  90. } map_table[MAX_MAPPED];
  91.  
  92. /* 
  93.  * This routine maps a file into a private section. Note that this 
  94.  * method of accessing a file is by far the fastest under VMS.
  95.  * The routine may fail (i.e. return 0) for several reasons, for
  96.  * example:
  97.  * - There is no more room for storing the info on sections.
  98.  * - The process is out of open file quota, channels, ...
  99.  * - fd does not describe an opened file.
  100.  * - The file is already opened for write access by this process
  101.  *   or another process
  102.  * - There is no free "hole" in virtual memory that fits the
  103.  *   size of the file
  104.  */
  105. int
  106. TIFFMapFileContents(fd, pbase, psize)
  107.     int fd;
  108.     char **pbase;
  109.     long *psize;
  110. {
  111.     char name[256];
  112.     struct FAB fab;
  113.     unsigned short channel;
  114.     void *inadr[2], *retadr[2];
  115.     unsigned long status;
  116.     long size;
  117.     
  118.     if (no_mapped >= MAX_MAPPED)
  119.         return(0);
  120.     /*
  121.      * We cannot use a file descriptor, we
  122.      * must open the file once more.
  123.      */
  124.     if (getname(fd, name, 1) == NULL)
  125.         return(0);
  126.     /* prepare the FAB for a user file open */
  127.     fab = cc$rms_fab;
  128.     fab.fab$v_ufo = 1;
  129.     fab.fab$b_fac = FAB$M_GET;
  130.     fab.fab$b_shr = FAB$M_SHRGET;
  131.     fab.fab$l_fna = name;
  132.     fab.fab$b_fns = strlen(name);
  133.     status = sys$open(&fab);    /* open file & get channel number */
  134.     if ((status&1) == 0)
  135.         return(0);
  136.     channel = (unsigned short)fab.fab$l_stv;
  137.     inadr[0] = inadr[1] = &channel;    /* just an address in P0 space */
  138.     /*
  139.      * Map the blocks of the file up to
  140.      * the EOF block into virtual memory.
  141.      */
  142.     size = TIFFGetFileSize(fd);
  143.     status = sys$crmpsc(inadr, retadr, 0, SEC$M_EXPREG, 0,0,0, channel,
  144.         howmany(size,512), 0,0,0);
  145.     if ((status&1) == 0)
  146.         return(0);
  147.     *pbase = retadr[0];        /* starting virtual address */
  148.     /*
  149.      * Use the size of the file up to the
  150.      * EOF mark for UNIX compatibility.
  151.      */
  152.     *psize = size;
  153.     /* Record the section in the table */
  154.     map_table[no_mapped].base = retadr[0];
  155.     map_table[no_mapped].top = retadr[1];
  156.     map_table[no_mapped].channel = channel;
  157.     no_mapped++;
  158.  
  159.         return(1);
  160. }
  161.  
  162. /*
  163.  * This routine unmaps a section from the virtual address space of 
  164.  * the process, but only if the base was the one returned from a
  165.  * call to TIFFMapFileContents.
  166.  */
  167. void
  168. TIFFUnmapFileContents(base, size)
  169.     char *base;
  170.     long size;
  171. {
  172.     void *inadr[2];
  173.     int i, j;
  174.     
  175.     /* Find the section in the table */
  176.     for (i = 0;i < no_mapped; i++) {
  177.         if (map_table[i].base == base) {
  178.             /* Unmap the section */
  179.             inadr[1] = base;
  180.             inadr[0] = map_table[i].top;
  181.             sys$deltva(inadr, 0, 0);
  182.             sys$dassgn(map_table[i].channel);
  183.             /* Remove this section from the list */
  184.             for (j = i+1; j < no_mapped; j++)
  185.                 map_table[j-1] = map_table[j];
  186.             no_mapped--;
  187.             return;
  188.         }
  189.     }
  190. }
  191. #endif /* defined(VMS) && defined(MMAP_SUPPORT) */
  192.  
  193. #if defined(THINK_C) || defined(applec)
  194. long
  195. TIFFGetFileSize(int fd)
  196. {
  197.     long pos, eof;
  198.     
  199.     pos = lseek(fd, 0, SEEK_CUR);
  200.     eof = lseek(fd, 0, SEEK_END);
  201.     lseek(fd, pos, SEEK_SET);
  202.     return(eof);
  203. }
  204. #endif /* THINK_C || applec */
  205.  
  206. #if defined(applec)
  207. #include <ioctl.h>
  208. #include <Files.h>
  209. #undef lseek
  210.  
  211. long
  212. mpw_lseek(int fd, long offset, int whence)
  213. {
  214.     long filepos, filesize, newpos;
  215.     short macfd;
  216.     
  217.     if ((filepos = lseek(fd, 0, SEEK_CUR)) < 0 ||
  218.         (filesize = lseek(fd, 0, SEEK_END)) < 0)
  219.         return (EOF);
  220.     newpos = offset + (whence == SEEK_SET ? 0 : 
  221.                whence == SEEK_CUR ? filepos :
  222.                         filesize);
  223.     if (newpos > filesize)
  224.         if (ioctl(fd, FIOREFNUM, &macfd) == -1 ||
  225.             SetEOF(macfd, newpos) != 0)
  226.             return (EOF);
  227.     return (lseek(fd, newpos, SEEK_SET));
  228. }
  229. #endif /* applec */
  230.