home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Multimedia / xcd / core / utils.h < prev    next >
C/C++ Source or Header  |  2002-07-16  |  4KB  |  154 lines

  1. /*************************************************************************
  2. Mode 2 Form 2 related utilities.
  3.  
  4. Written by Avi Halachmi (avih [avihpit@yahoo.com])
  5. **************************************************************************
  6. */
  7.  
  8. #ifndef _UTILS_H_
  9. #define _UTILS_H_
  10.  
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include "portab.h"
  14.  
  15. #define RIFF_SIZE            44
  16.  
  17. // windows adds a 'virtual' (=doesn't really exist in the file) RIFF header.
  18. #define OS_VIRTUAL_HEADER    RIFF_SIZE
  19.  
  20.  
  21.  
  22. #define SECTOR_SYNC            12
  23. #define SECTOR_HEADER        4
  24. #define SECTOR_SUBHEADER    8
  25.  
  26. #define SECTOR_HEADERS        (SECTOR_SYNC + SECTOR_HEADER + SECTOR_SUBHEADER)
  27. #define SECTOR_USER_DATA    2324
  28. #define SECTOR_EDC            4
  29.  
  30. #define SECTOR_SIZE            (SECTOR_HEADERS + SECTOR_USER_DATA + SECTOR_EDC)
  31.  
  32. #ifndef min
  33. #define min(a,b) (((a)<(b))?(a):(b))
  34. #endif
  35.  
  36. #ifndef max
  37. #define max(a,b) (((a)<(b))?(b):(a))
  38. #endif
  39.  
  40. typedef struct sector_mode2_form2{
  41.     char    sync[SECTOR_SYNC];
  42.     char    header[SECTOR_HEADER];
  43.     char    subheader[SECTOR_SUBHEADER];
  44.     char    userData[SECTOR_USER_DATA];
  45.     char    edc[SECTOR_EDC];
  46. } SECTOR_MODE2_FORM2;
  47.  
  48. // same as printf. but using OutputDebugString as an output function.
  49.  
  50. int dprintf(char* fmt, ...){
  51.     char printString[1024];
  52.     va_list argp;
  53.     va_start(argp, fmt);
  54.     vsprintf(printString, fmt, argp);
  55.     va_end(argp);
  56.     DEBUG_PRINT (printString);
  57.     return strlen(printString);
  58. }
  59.  
  60.  
  61. long fsize(FILE* handle){
  62.     if (fseek(handle, 0, SEEK_END))
  63.         return 0;
  64.  
  65.     return ftell(handle);
  66. }
  67.  
  68. // convert a media position to actual file position (taking into account the sector structure), 
  69. // while possibly skipping empty sectors and OS virtual header.
  70. long filePosition(long mediaPosition, long posFirstMediaSectorInFile){
  71.     long numFullSectors=mediaPosition/SECTOR_USER_DATA;
  72.     long lastSector=mediaPosition%SECTOR_USER_DATA;
  73.     return posFirstMediaSectorInFile + numFullSectors*SECTOR_SIZE + SECTOR_HEADERS + lastSector;
  74. }
  75.  
  76.  
  77. // convert actual file position to a media position (taking into account the sector structure), 
  78. // while possibly adding empty sectors and OS virtual header.
  79. long mediaPosition(long filePosition, long posFirstMediaSectorInFile){
  80.     long res=0;
  81.     if (filePosition >= posFirstMediaSectorInFile)
  82.         filePosition -= posFirstMediaSectorInFile;
  83.  
  84.     long lastSector=filePosition%SECTOR_SIZE;
  85.     long numFullSectors=filePosition/SECTOR_SIZE;
  86.     long withoutLast=numFullSectors*SECTOR_USER_DATA;
  87.     dprintf ("utils::mediaPosition, lastsector=%d, #(-)sectors=%d, withoutlast=%d",(long)lastSector, (long)numFullSectors,(long)withoutLast);
  88.  
  89.     if (lastSector < SECTOR_HEADERS)
  90.         lastSector=0;
  91.     else if (lastSector >= SECTOR_HEADERS + SECTOR_USER_DATA)
  92.         lastSector = SECTOR_USER_DATA;
  93.     else
  94.         lastSector -= SECTOR_HEADERS;
  95.  
  96.     res = withoutLast + lastSector;
  97.  
  98.     dprintf ("utils::mediaPosition, filePosition=%d, mediaPosition=%d",(long)(posFirstMediaSectorInFile + filePosition), (long)res);
  99.  
  100.     return res;
  101. }
  102.  
  103. //reads a whole raw sector to a buffer
  104. //return values: -1:error occured, otherwise: number of bytes read (could be 0).
  105. int    readSector(FILE* handle, long posFirstSectorInFile, void* sectorBuffer, long sectorNumber){
  106.     long bytesRead=-1;
  107.  
  108.     if (fseek(handle, (long)posFirstSectorInFile + (long)sectorNumber*SECTOR_SIZE, SEEK_SET))
  109.         return -1;    //seek error
  110.     
  111.     bytesRead=fread (sectorBuffer, 1, SECTOR_SIZE, handle);
  112.     
  113.     if (ferror(handle))    // error reading from the file
  114.         return -1;
  115.  
  116.     return bytesRead;
  117. }
  118.  
  119.  
  120. // identifies an empty VCD or SVCD sector
  121. int isEmptySector(char* sectorBuffer){
  122.     char empty[4]={0,0,0x60,0};
  123.     SECTOR_MODE2_FORM2* sector= (SECTOR_MODE2_FORM2*)sectorBuffer;
  124.  
  125.     dprintf ("IsEmptySector: subheader= %02xh %02xh %02xh %02xh - %02xh %02xh %02xh %02xh\n", 
  126.         sector->subheader[0],
  127.         sector->subheader[1],
  128.         sector->subheader[2],
  129.         sector->subheader[3],
  130.         sector->subheader[4],
  131.         sector->subheader[5],
  132.         sector->subheader[6],
  133.         sector->subheader[7]);
  134.  
  135.     if (
  136.         (//(sector->subheader)[0] == empty[0] &&
  137.         ( sector->subheader)[1] == empty[1] &&
  138.         ( sector->subheader)[2] == empty[2] &&
  139.         ( sector->subheader)[3] == empty[3])
  140.         ||
  141.         (//(sector->subheader)[4] == empty[0] &&
  142.         ( sector->subheader)[5] == empty[1] &&
  143.         ( sector->subheader)[6] == empty[2] &&
  144.         ( sector->subheader)[7] == empty[3])
  145.     )
  146.         return 1;
  147.     else
  148.         return 0;
  149. }
  150.  
  151.  
  152.  
  153. #endif
  154.