]> Shamusworld >> Repos - wozmaker/blob - src/fileio.h
Initial commit.
[wozmaker] / src / fileio.h
1 #ifndef __FILEIO_H__
2 #define __FILEIO_H__
3
4 #include <stdint.h>
5
6 // N.B.: All 32/16-bit values are stored in little endian.  Which means, to
7 //       read/write them safely, we need to use translators as this code may or
8 //       may not be compiled on an architecture that supports little endian
9 //       natively.
10
11 struct A2RStream
12 {
13         uint8_t location;               // Quarter track for this stream
14         uint8_t captureType;    // 1 = timing, 2 = bits, 3 = ext. timing
15         uint8_t dataLength[4];  // Length of the stream
16         uint8_t estLoopPoint[4];// Estimated loop point in ticks (125 ns/tick)
17         uint8_t data[];                 // Variable length array for stream data
18 };
19
20 struct A2RMetadata
21 {
22         uint8_t metaTag[4];             // "META"
23         uint32_t metaSize;              // Size of the META chunk
24         uint8_t data[];                 // Variable length array of metadata
25 };
26
27 struct A2R
28 {
29         // Header
30         uint8_t magic1[4];              // "A2R2"
31         uint8_t magic2[4];              // $FF $0A $0D $0A
32
33         // INFO chunk
34         uint8_t infoTag[4];             // "INFO"
35         uint32_t infoSize;              // Always 36 bytes long
36         uint8_t infoVersion;    // Currently 1
37         uint8_t creator[32];    // Software that made this image, padded with 0x20
38         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
39         uint8_t writeProtected; // 1 = write protected disk
40         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
41
42         // STRM chunk
43         uint8_t strmTag[4];             // "STRM"
44         uint32_t strmSize;              // Varies, depending on # of tracks imaged
45         uint8_t data[];                 // Variable length array for stream data proper
46 };
47
48 struct WOZTrack
49 {
50         uint8_t bits[6646];
51         uint16_t byteCount;
52         uint16_t bitCount;
53         uint16_t splicePoint;
54         uint8_t spliceNibble;
55         uint8_t spliceBitCount;
56         uint16_t reserved;
57 };
58
59 struct WOZMetadata
60 {
61         uint8_t metaTag[4];             // "META"
62         uint32_t metaSize;              // Size of the META chunk
63         uint8_t data[];                 // Variable length array of metadata
64 };
65
66 struct WOZ
67 {
68         // Header
69         uint8_t magic1[4];              // "WOZ1"
70         uint8_t magic2[4];              // $FF $0A $0D $0A
71         uint32_t crc32;                 // CRC32 of the remaining data in the file
72
73         // INFO chunk
74         uint8_t infoTag[4];             // "INFO"
75         uint32_t infoSize;              // Always 60 bytes long
76         uint8_t infoVersion;    // Currently 1
77         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
78         uint8_t writeProtected; // 1 = write protected disk
79         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
80         uint8_t cleaned;                // 1 = fake bits removed from image
81         uint8_t creator[32];    // Software that made this image, padded with 0x20
82         uint8_t pad1[23];               // Padding to 60 bytes
83
84         // TMAP chunk
85         uint8_t tmapTag[4];             // "TMAP"
86         uint32_t tmapSize;              // Always 160 bytes long
87         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
88
89         // TRKS chunk
90         uint8_t trksTag[4];             // "TRKS"
91         uint32_t trksSize;              // Varies, depending on # of tracks imaged
92         WOZTrack track[];               // Variable length array for the track data proper
93 };
94
95
96 // Exported functions
97 uint32_t CRC32(const uint8_t * data, uint32_t length);
98 uint8_t * ReadFile(const char * filename, uint32_t * size);
99
100
101 // Inline functions ("get" functions--need to write "set" functions)
102 static inline uint16_t Uint16LE(uint16_t v)
103 {
104         uint8_t * w = (uint8_t *)&v;
105         return (w[1] << 8) | (w[0] << 0);
106 }
107
108 static inline uint32_t Uint32LE(uint32_t v)
109 {
110         uint8_t * w = (uint8_t *)&v;
111         return (w[3] << 24) | (w[2] << 16) | (w[1] << 8) | (w[0] << 0);
112 }
113
114 static inline uint32_t Uint32LE(uint8_t * v)
115 {
116         return (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | (v[0] << 0);
117 }
118
119 static inline uint32_t Uint32BE(uint8_t * v)
120 {
121         return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | (v[3] << 0);
122 }
123
124 static inline void SwapBytes32(uint8_t * b)
125 {
126         uint8_t temp = b[0];
127         b[0] = b[3];
128         b[3] = temp;
129         temp = b[1];
130         b[1] = b[2];
131         b[2] = temp;
132 }
133
134 #endif  // __FILEIO_H__
135