home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- Mode 2 Form 2 related utilities.
-
- Written by Avi Halachmi (avih [avihpit@yahoo.com])
- **************************************************************************
- */
-
- #ifndef _UTILS_H_
- #define _UTILS_H_
-
- #include <stdio.h>
- #include <stdarg.h>
- #include "portab.h"
-
- #define RIFF_SIZE 44
-
- // windows adds a 'virtual' (=doesn't really exist in the file) RIFF header.
- #define OS_VIRTUAL_HEADER RIFF_SIZE
-
-
-
- #define SECTOR_SYNC 12
- #define SECTOR_HEADER 4
- #define SECTOR_SUBHEADER 8
-
- #define SECTOR_HEADERS (SECTOR_SYNC + SECTOR_HEADER + SECTOR_SUBHEADER)
- #define SECTOR_USER_DATA 2324
- #define SECTOR_EDC 4
-
- #define SECTOR_SIZE (SECTOR_HEADERS + SECTOR_USER_DATA + SECTOR_EDC)
-
- #ifndef min
- #define min(a,b) (((a)<(b))?(a):(b))
- #endif
-
- #ifndef max
- #define max(a,b) (((a)<(b))?(b):(a))
- #endif
-
- typedef struct sector_mode2_form2{
- char sync[SECTOR_SYNC];
- char header[SECTOR_HEADER];
- char subheader[SECTOR_SUBHEADER];
- char userData[SECTOR_USER_DATA];
- char edc[SECTOR_EDC];
- } SECTOR_MODE2_FORM2;
-
- // same as printf. but using OutputDebugString as an output function.
-
- int dprintf(char* fmt, ...){
- char printString[1024];
- va_list argp;
- va_start(argp, fmt);
- vsprintf(printString, fmt, argp);
- va_end(argp);
- DEBUG_PRINT (printString);
- return strlen(printString);
- }
-
-
- long fsize(FILE* handle){
- if (fseek(handle, 0, SEEK_END))
- return 0;
-
- return ftell(handle);
- }
-
- // convert a media position to actual file position (taking into account the sector structure),
- // while possibly skipping empty sectors and OS virtual header.
- long filePosition(long mediaPosition, long posFirstMediaSectorInFile){
- long numFullSectors=mediaPosition/SECTOR_USER_DATA;
- long lastSector=mediaPosition%SECTOR_USER_DATA;
- return posFirstMediaSectorInFile + numFullSectors*SECTOR_SIZE + SECTOR_HEADERS + lastSector;
- }
-
-
- // convert actual file position to a media position (taking into account the sector structure),
- // while possibly adding empty sectors and OS virtual header.
- long mediaPosition(long filePosition, long posFirstMediaSectorInFile){
- long res=0;
- if (filePosition >= posFirstMediaSectorInFile)
- filePosition -= posFirstMediaSectorInFile;
-
- long lastSector=filePosition%SECTOR_SIZE;
- long numFullSectors=filePosition/SECTOR_SIZE;
- long withoutLast=numFullSectors*SECTOR_USER_DATA;
- dprintf ("utils::mediaPosition, lastsector=%d, #(-)sectors=%d, withoutlast=%d",(long)lastSector, (long)numFullSectors,(long)withoutLast);
-
- if (lastSector < SECTOR_HEADERS)
- lastSector=0;
- else if (lastSector >= SECTOR_HEADERS + SECTOR_USER_DATA)
- lastSector = SECTOR_USER_DATA;
- else
- lastSector -= SECTOR_HEADERS;
-
- res = withoutLast + lastSector;
-
- dprintf ("utils::mediaPosition, filePosition=%d, mediaPosition=%d",(long)(posFirstMediaSectorInFile + filePosition), (long)res);
-
- return res;
- }
-
- //reads a whole raw sector to a buffer
- //return values: -1:error occured, otherwise: number of bytes read (could be 0).
- int readSector(FILE* handle, long posFirstSectorInFile, void* sectorBuffer, long sectorNumber){
- long bytesRead=-1;
-
- if (fseek(handle, (long)posFirstSectorInFile + (long)sectorNumber*SECTOR_SIZE, SEEK_SET))
- return -1; //seek error
-
- bytesRead=fread (sectorBuffer, 1, SECTOR_SIZE, handle);
-
- if (ferror(handle)) // error reading from the file
- return -1;
-
- return bytesRead;
- }
-
-
- // identifies an empty VCD or SVCD sector
- int isEmptySector(char* sectorBuffer){
- char empty[4]={0,0,0x60,0};
- SECTOR_MODE2_FORM2* sector= (SECTOR_MODE2_FORM2*)sectorBuffer;
-
- dprintf ("IsEmptySector: subheader= %02xh %02xh %02xh %02xh - %02xh %02xh %02xh %02xh\n",
- sector->subheader[0],
- sector->subheader[1],
- sector->subheader[2],
- sector->subheader[3],
- sector->subheader[4],
- sector->subheader[5],
- sector->subheader[6],
- sector->subheader[7]);
-
- if (
- (//(sector->subheader)[0] == empty[0] &&
- ( sector->subheader)[1] == empty[1] &&
- ( sector->subheader)[2] == empty[2] &&
- ( sector->subheader)[3] == empty[3])
- ||
- (//(sector->subheader)[4] == empty[0] &&
- ( sector->subheader)[5] == empty[1] &&
- ( sector->subheader)[6] == empty[2] &&
- ( sector->subheader)[7] == empty[3])
- )
- return 1;
- else
- return 0;
- }
-
-
-
- #endif
-