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