/*-------------------------------------------------------------------- * DESCRIPTION * Returns KPTRUE if seems to be ICC Formatted Profile * private function * AUTHOR * doro * * DATE CREATED * August 9, 1996 *------------------------------------------------------------------*/ KpBool_t KSPAPI SpIsICCProfile( char *Filename, SpFileProps_t *Props) { KpUInt32_t ProfileSize; KpUInt32_t ProfileCode = 0; SpStatus_t Status = SpStatBadProfile; KpFileId FD; char *name; KpUInt32_t Read_Size_OK, Read_Code_OK = 0; KpInt32_t Read_Amount = 4; KpFileProps_t fileProps; KpBool_t IsICC = KPFALSE; char ReadBufferArray[8]; char *ReadBuffer; name = Filename; #if defined (KPMAC) SpCvrtSpFileProps(Props, &fileProps); #endif if (KpFileOpen(name, "r", &fileProps, &FD)) /* 0 = not opened */ { /* Read HEADER_SIZE into Buffer */ ReadBuffer = ReadBufferArray; Read_Size_OK = KpFileRead(FD, ReadBuffer, &Read_Amount); ProfileSize = SpGetUInt32(&ReadBuffer); if (KpFilePosition(FD, FROM_START, 36)) { ReadBuffer = ReadBufferArray; Read_Code_OK = KpFileRead(FD, ReadBuffer, &Read_Amount); ProfileCode = SpGetUInt32(&ReadBuffer); } /* Close File */ KpFileClose(FD); if ((Read_Size_OK) && (ProfileSize > HEADER_SIZE)) { if ((Read_Code_OK) && (ProfileCode == (0x61637370))) IsICC = KPTRUE; } } return (IsICC); }
/*-------------------------------------------------------------------- * DESCRIPTION * Map an entire file. * * AUTHOR * lsh * * DATE CREATED * November 9, 1993 *------------------------------------------------------------------*/ void FAR *KpMapFileEx ( KpChar_p FileName, KpFileProps_t FAR *FileProps, KpChar_p Mode, KpMapFile_t FAR *MapFileCtl) { /* validate mode */ switch (*Mode) { case 'R': case 'r': #if 0 case 'w': #endif break; default: return NULL; } /* get the size of the file */ if (!KpFileSize (FileName, FileProps, &MapFileCtl->NumBytes)) return NULL; /* allocate buffer to hold file */ MapFileCtl->Ptr = allocBufferPtr (MapFileCtl->NumBytes); if (NULL == MapFileCtl->Ptr) return NULL; /* open the file */ if (!KpFileOpen (FileName, Mode, FileProps, &MapFileCtl->Fd)) { freeBufferPtr (MapFileCtl->Ptr); return NULL; } /* read file into the buffer */ if (!KpFileRead (MapFileCtl->Fd, MapFileCtl->Ptr, &MapFileCtl->NumBytes)) { freeBufferPtr (MapFileCtl->Ptr); KpFileClose (MapFileCtl->Fd); return NULL; } /* done with file, close it */ if ('r' == *Mode) { KpFileClose (MapFileCtl->Fd); MapFileCtl->Fd = -1; } MapFileCtl->Mode = *Mode; return MapFileCtl->Ptr; }
/*----------------------------------------------------------------------*/ KpInt32_t Kp_read ( KpFd_t FAR *fd, KpLargeBuffer_t buf, KpInt32_t nbytes) { void KPHUGE *memPtr; if (KpFdCheck (fd) != KCMS_IO_SUCCESS) return KCMS_IO_ERROR; /* validate source */ if (buf == NULL) return KCMS_IO_ERROR; switch (fd->type) { /* if regular file, read it */ case KCMS_IO_SYSFILE: if (1 != KpFileRead (fd->fd.sys, buf, &nbytes)) return KCMS_IO_ERROR; break; /* if memory file, copy from memory */ case KCMS_IO_MEMFILE: /* check for attemptting to read too much */ if (fd->fd.mem.pos + nbytes > fd->fd.mem.size) return KCMS_IO_ERROR; /* validate source? */ if (fd->fd.mem.buf == NULL) return KCMS_IO_ERROR; memPtr = (char KPHUGE *) fd->fd.mem.buf + fd->fd.mem.pos; KpMemCpy (buf, memPtr, nbytes); fd->fd.mem.pos += nbytes; break; default: return KCMS_IO_ERROR; } return KCMS_IO_SUCCESS; }
/*-------------------------------------------------------------------- * DESCRIPTION * Return the header for the profile. * * AUTHOR * doro * * DATE CREATED * October 23, 1995 *------------------------------------------------------------------*/ SpStatus_t KSPAPI SpProfileLoadHeader( char *Filename, SpFileProps_t *Props, SpHeader_t FAR *Header) { SpHugeBuffer_t BufferAddress; SpStatus_t Status = SpStatBadProfile; KpFileId FD; char *name; KpUInt32_t Read_OK; KpInt32_t Read_Amount = HEADER_SIZE; KpFileProps_t fileProps; if (!SpIsICCProfile(Filename, Props)) return (SpStatBadProfile); name = Filename; BufferAddress = allocBufferPtr(HEADER_SIZE); if (BufferAddress == NULL) return (SpStatMemory); #if defined (KPMAC) SpCvrtSpFileProps(Props, &fileProps); #endif if (KpFileOpen(name, "r", &fileProps, &FD)) /* 0 = not opened */ { /* Read HEADER_SIZE into Buffer */ Read_OK = KpFileRead(FD, BufferAddress, &Read_Amount); /* Close File */ if (!KpFileClose(FD)) Status = SpStatBadProfile; if (Read_OK) { Status = SpHeaderToPublic(BufferAddress, HEADER_SIZE, Header); } } freeBufferPtr(BufferAddress); return Status; }
/*-------------------------------------------------------------------- * DESCRIPTION * Return the Tag from the profile. * * AUTHOR * doro * * DATE CREATED * March 12, 2002 *------------------------------------------------------------------*/ SpStatus_t KSPAPI SpProfileLoadTagEx( char *Filename, SpFileProps_t *Props, SpTagId_t TagId, SpTagValue_t FAR *Value) { SpHeader_t Header; SpHeader_t *HeaderPtr; SpHugeBuffer_t BufferAddress, TagAddress; SpStatus_t Status = SpStatSuccess; KpFileId FD; KpFileProps_t fileProps; char name[MAX_PATH]; KpUInt32_t FilePosition = HEADER_SIZE; KpInt32_t Read_Amount = sizeof(KpInt32_t); KpInt32_t i, TagArraySize, TagBufferSize; SpTagRecord_t *TagArray; if (!SpIsICCProfile(Filename, Props)) return (SpStatBadProfile); strcpy (name, Filename); /* Need Header in case we are requesting a Named Color */ HeaderPtr = (SpHeader_t *)NULL; if (TagId == SpTagNamedColor) { HeaderPtr = &Header; Status = SpProfileLoadHeader(name, Props, HeaderPtr); } if (Status == SpStatSuccess) { /* Set up for failure - yes I know I'm negative */ Status = SpStatFileNotFound; #if defined (KPMAC) SpCvrtSpFileProps(Props, &fileProps); #endif if (KpFileOpen(Filename, "r", &fileProps, &FD)) /* 0 = not opened */ { if (KpFilePosition(FD, FROM_START, FilePosition)) { Status = SpStatFileReadError; /* Read TagArraySize into Buffer */ if (KpFileRead(FD, &TagArraySize, &Read_Amount)) { #if defined (KPLSBFIRST) /* If we are on a little endian machine we need to do byte swap */ Kp_swab32 (&TagArraySize, 1); #endif TagBufferSize = TagArraySize * 3 * 4; /* each entry is 3*4 bytes */ BufferAddress = allocBufferPtr(TagBufferSize); if (BufferAddress != NULL) { if (KpFileRead(FD, BufferAddress, &TagBufferSize)) { #if defined (KPLSBFIRST) /* If we are on a little endian machine we need to do byte swap */ Kp_swab32 (BufferAddress, TagBufferSize / sizeof (KpInt32_t)); #endif TagArray = BufferAddress; Status = SpStatTagNotFound; for (i = 0; i < TagArraySize; i++) { if (TagId == (SpTagId_t)TagArray[i].Id) { Status = SpStatMemory; TagAddress = allocBufferPtr(TagArray[i].Size); if (TagAddress != NULL) { if (KpFilePosition(FD, FROM_START, TagArray[i].Offset)) { Read_Amount = TagArray[i].Size; if (KpFileRead(FD, TagAddress, &Read_Amount)) { Status = SpTagToPublic(HeaderPtr, TagId, TagArray[i].Size, TagAddress, Value); } } freeBufferPtr(TagAddress); } else { Status = SpStatMemory; break; } } } } freeBufferPtr(BufferAddress); } else /* Buffer Address != 0 */ Status = SpStatMemory; } } /* Close File */ KpFileClose(FD); } } return Status; }
/*-------------------------------------------------------------------- * DESCRIPTION * Unmap an entire file. (WIN32 Version) * * AUTHOR * lsh * * DATE CREATED * November 9, 1993 *------------------------------------------------------------------*/ KpInt32_t KpUnMapFile (KpMapFile_t FAR *MapFileCtl) { int Status; Status = KCMS_IO_SUCCESS; if (!UnmapViewOfFile (MapFileCtl->Ptr)) Status = KCMS_IO_ERROR; if (!CloseHandle (MapFileCtl->hMapObject)) Status = KCMS_IO_ERROR; #if defined(_M_IA64) if (!CloseHandle (MapFileCtl->hFile)) { #else /* non-64-bit case */ if (_lclose (MapFileCtl->hFile)) { #endif /* defined(_M_IA64) */ Status = KCMS_IO_ERROR; } return Status; } #else /*-------------------------------------------------------------------- * DESCRIPTION * Map an entire file. * * AUTHOR * lsh * * DATE CREATED * November 9, 1993 *------------------------------------------------------------------*/ void FAR *KpMapFileEx ( KpChar_p FileName, KpFileProps_t FAR *FileProps, KpChar_p Mode, KpMapFile_t FAR *MapFileCtl) { /* validate mode */ switch (*Mode) { case 'R': case 'r': #if 0 case 'w': #endif break; default: return NULL; } /* get the size of the file */ if (!KpFileSize (FileName, FileProps, &MapFileCtl->NumBytes)) return NULL; /* allocate buffer to hold file */ MapFileCtl->Ptr = allocBufferPtr (MapFileCtl->NumBytes); if (NULL == MapFileCtl->Ptr) return NULL; /* open the file */ if (!KpFileOpen (FileName, Mode, FileProps, &MapFileCtl->Fd)) { freeBufferPtr (MapFileCtl->Ptr); return NULL; } /* read file into the buffer */ if (!KpFileRead (MapFileCtl->Fd, MapFileCtl->Ptr, &MapFileCtl->NumBytes)) { freeBufferPtr (MapFileCtl->Ptr); KpFileClose (MapFileCtl->Fd); return NULL; } /* done with file, close it */ if ('r' == *Mode) { KpFileClose (MapFileCtl->Fd); MapFileCtl->Fd = -1; } MapFileCtl->Mode = *Mode; return MapFileCtl->Ptr; } /*-------------------------------------------------------------------- * DESCRIPTION * Map an entire file. (non-win32 version) * * AUTHOR * lsh * * DATE CREATED * November 9, 1993 *------------------------------------------------------------------*/ void FAR *KpMapFile ( KpChar_p FileName, ioFileChar FAR *FileProps, KpChar_p Mode, KpMapFile_t FAR *MapFileCtl) { KpFileProps_t kpFileProps, *kpFilePropsPtr; /* convert FileProps to KpFileProps_t and call KpMapFileEx */ #if defined (KPMAC) || defined (KPMSMAC) if (FileProps != NULL) { kpFileProps.vRefNum = FileProps->vRefNum; kpFileProps.dirID= 0; strncpy(kpFileProps.fileType, FileProps->fileType, 5); strncpy(kpFileProps.creatorType, FileProps->creatorType, 5); } else { kpFileProps.vRefNum = 0; kpFileProps.dirID= 0; strncpy(kpFileProps.fileType, " ", 5); strncpy(kpFileProps.creatorType, " ", 5); } kpFilePropsPtr = &kpFileProps; #else kpFilePropsPtr = FileProps; #endif return (KpMapFileEx (FileName, kpFilePropsPtr, Mode, MapFileCtl) ); }