Annotation of hatari/src/dim.c, revision 1.1.1.2

1.1       root        1: /*
                      2:   Hatari - dim.c
                      3: 
                      4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
                      6: 
                      7:   DIM Disc support.
                      8: */
1.1.1.2 ! root        9: char DIM_rcsid[] = "Hatari $Id: dim.c,v 1.3 2005/02/13 16:18:48 thothy Exp $";
1.1       root       10: 
                     11: #include <zlib.h>
                     12: 
                     13: #include "main.h"
                     14: #include "file.h"
                     15: #include "floppy.h"
                     16: #include "dim.h"
                     17: 
                     18: #undef SAVE_TO_DIM_IMAGES
                     19: 
                     20: /*
                     21:     .DIM FILE FORMAT
                     22:   --===============-------------------------------------------------------------
                     23: 
                     24:   The file format of the .DIM image files are quite the same as the .ST image
                     25:   files (see st.c) - the .DIM image files just have an additional header of
                     26:   32 bytes.
                     27:   
                     28:   The header contains following information:
                     29: 
                     30:   Offset  Size  Description
                     31:   ------  ----  -----------
                     32:   0x0000  Word  ID Header (0x4242('BB'))
                     33:   0x0003  Byte  Image contains all sectors (0) or only used sectors (1)
                     34:   0x0006  Byte  Sides (0 or 1; add 1 to this to get correct number of sides)
                     35:   0x0008  Byte  Sectors per track
                     36:   0x000A  Byte  Starting Track (0 based)
                     37:   0x000C  Byte  Ending Track (0 based)
                     38:   0x000D  Byte  Double-Density(0) or High-Density (1)
                     39: 
                     40:   All other header fields are unknown.
                     41:   If you have information about them, please help!
                     42: */
                     43: 
                     44: 
                     45: /*-----------------------------------------------------------------------*/
                     46: /*
                     47:   Does filename end with a .DIM extension? If so, return TRUE
                     48: */
                     49: BOOL DIM_FileNameIsDIM(char *pszFileName, BOOL bAllowGZ)
                     50: {
                     51:        return(File_DoesFileExtensionMatch(pszFileName,".dim")
                     52:               || (bAllowGZ && File_DoesFileExtensionMatch(pszFileName,".dim.gz")));
                     53: }
                     54: 
                     55: 
                     56: /*-----------------------------------------------------------------------*/
                     57: /*
                     58:   Load .DIM file into memory, set number of bytes loaded and return a pointer
                     59:   to the buffer.
                     60: */
                     61: Uint8 *DIM_ReadDisc(char *pszFileName, long *pImageSize)
                     62: {
                     63:        Uint8 *pDimFile;
                     64:        Uint8 *pDiscBuffer = NULL;
                     65: 
                     66:        /* Load file into buffer */
                     67:        pDimFile = File_Read(pszFileName, NULL, pImageSize, NULL);
                     68:        if (pDimFile)
                     69:        {
                     70:                /* Check header for valid image: */
                     71:                if (*(Uint16 *)pDimFile != 0x4242 || pDimFile[0x03] != 0 || pDimFile[0x0A] != 0)
                     72:                {
                     73:                        fprintf(stderr, "This is not a valid DIM image!\n");
                     74:                        *pImageSize = 0;
                     75:                        free(pDimFile);
                     76:                        return NULL;
                     77:                }
                     78: 
                     79:                /* Simply use disc contents without the DIM header: */
                     80:                *pImageSize -= 32;
                     81:                pDiscBuffer = malloc(*pImageSize);
                     82:                if (pDiscBuffer)
                     83:                        memcpy(pDiscBuffer, pDimFile+32, *pImageSize);
                     84:                else
                     85:                        perror("DIM_ReadDisc");
                     86: 
                     87:                /* Free DIM file we loaded */
                     88:                free(pDimFile);
                     89:        }
                     90: 
                     91:        if (pDiscBuffer == NULL)
                     92:        {
                     93:                *pImageSize = 0;
                     94:        }
                     95: 
                     96:        return(pDiscBuffer);
                     97: }
                     98: 
                     99: 
                    100: /*-----------------------------------------------------------------------*/
                    101: /*
                    102:   Save .DIM file from memory buffer. Returns TRUE is all OK
                    103: */
                    104: BOOL DIM_WriteDisc(char *pszFileName, unsigned char *pBuffer, int ImageSize)
                    105: {
                    106: #ifdef SAVE_TO_DIM_IMAGES
                    107: 
                    108:        Uint8 *pDimFile;
                    109:        gzFile hGzFile;
                    110:        unsigned short int nSectorsPerTrack, nSides;
                    111:        int nTracks;
                    112:        BOOL bRet;
                    113: 
                    114:        /* Allocate memory for the whole DIM image: */
                    115:        pDimFile = malloc(ImageSize + 32);
                    116:        if (!pDimFile)
                    117:        {
                    118:                perror("DIM_WriteDisc");
                    119:                return FALSE;
                    120:        }
                    121: 
                    122:        /* Try to load the old header data to preserve the header fields that are unknown yet: */
                    123:     hGzFile = gzopen(pszFileName, "rb");
                    124:     if (hGzFile != NULL)
                    125:     {
                    126:                gzread(hGzFile, pDimFile, 32);
                    127:                gzclose(hGzFile);
                    128:        }
                    129:        else
                    130:        {
                    131:                memset(pDimFile, 0, 32);
                    132:        }
                    133: 
                    134:        /* Now fill in the new header information: */
                    135:        Floppy_FindDiscDetails(pBuffer, ImageSize, &nSectorsPerTrack, &nSides);
                    136:        nTracks = ((ImageSize / NUMBYTESPERSECTOR) / nSectorsPerTrack) / nSides;
                    137:        pDimFile[0x00] = pDimFile[0x01] = 0x42;     /* ID */
                    138:        pDimFile[0x03] = 0;                         /* Image contains all sectors */
                    139:        pDimFile[0x06] = nSides - 1;                /* Sides */
                    140:        pDimFile[0x08] = nSectorsPerTrack;          /* Sectors per track */
                    141:        pDimFile[0x0A] = 0;                         /* Starting track */
                    142:        pDimFile[0x0C] = nTracks - 1;               /* Ending track */
                    143:        pDimFile[0x0D] = (ImageSize > 1024*1024);   /* DD / HD flag */
                    144: 
                    145:        /* Now copy the disc data: */
                    146:        memcpy(pDimFile + 32, pBuffer, ImageSize);
                    147:        
                    148:        /* And finally save it: */
                    149:        bRet = File_Save(pszFileName, pDimFile, ImageSize + 32, FALSE);
                    150: 
                    151:        free(pDimFile);
                    152: 
                    153:        return bRet;
                    154: 
                    155: #else   /*SAVE_TO_ST_IMAGES*/
                    156: 
                    157:        /* Oops, cannot save */
                    158:        return FALSE;
                    159: 
                    160: #endif  /*SAVE_TO_ST_IMAGES*/
                    161: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.