1 //-------------------------------------------------------------------------
2 //
3 //  For conditions of distribution and use, see copyright notice
4 //  in Flashpix.h
5 //
6 //  Copyright (c) 1999 Digital Imaging Group, Inc.
7 //
8 //  Contents:   MSF header class
9 //
10 //  Classes:    CMSFHeader
11 //
12 //--------------------------------------------------------------------------
13 
14 #ifndef __HEADER_HXX__
15 #define __HEADER_HXX__
16 
17 #include "msf.hxx"
18 #include "storagep.h"
19 
20 #define HDR_NOFORCE  0x0000
21 #define HDR_FORCE    0x0001
22 #define HDR_ALL      0x0002
23 
24 struct SPreHeader : protected SStorageFile
25 {
26 protected:
27   USHORT    _uMinorVersion;
28   USHORT    _uDllVersion;
29         USHORT          _uByteOrder;
30 
31   USHORT          _uSectorShift;
32         USHORT          _uMiniSectorShift;
33 
34   USHORT          _usReserved;
35   ULONG           _ulReserved1;
36   ULONG           _ulReserved2;
37 
38         FSINDEX   _csectFat;
39         SECT      _sectDirStart;
40 
41         DFSIGNATURE _signature;
42 
43         ULONG           _ulMiniSectorCutoff;
44 
45         SECT    _sectMiniFatStart;
46         FSINDEX   _csectMiniFat;
47 
48         SECT    _sectDifStart;
49         FSINDEX   _csectDif;
50 };
51 
52 
53 const USHORT CSECTFATREAL = (HEADERSIZE - sizeof(SPreHeader)) / sizeof(SECT);
54 
55 const USHORT CSECTFAT = CSECTFATREAL;
56 
57 class CMSFHeader: private SPreHeader
58 {
59 public:
60 
61     CMSFHeader(USHORT uSectorShift);
62 
63     SCODE Validate() const;
64 
65     inline USHORT GetMinorVersion() const;
66     inline USHORT GetDllVersion() const;
67 
68     inline SCODE SetFatLength(const FSINDEX cFatSect);
69     inline FSINDEX GetFatLength() const;
70 
71     inline SCODE SetMiniFatLength(const FSINDEX cFatSect);
72     inline FSINDEX GetMiniFatLength() const;
73 
74     inline SCODE SetDirStart(const SECT sect);
75     inline SECT GetDirStart() const;
76 
77     inline SCODE SetFatStart(const SECT sect);
78     inline SECT GetFatStart()  const;
79 
80     inline SCODE SetMiniFatStart(const SECT sect);
81     inline SECT GetMiniFatStart()  const;
82 
83     inline SCODE SetDifStart(const SECT sect);
84     inline SECT  GetDifStart() const;
85 
86     inline SCODE SetDifLength(const FSINDEX cFatSect);
87     inline FSINDEX GetDifLength() const;
88 
89     inline SECT GetFatSect(const FSINDEX oSect) const;
90     inline SCODE SetFatSect(const FSINDEX oSect, const SECT sect);
91 
92     inline USHORT GetSectorShift() const;
93     inline USHORT GetMiniSectorShift() const;
94 
95     inline ULONG  GetMiniSectorCutoff() const;
96 
97     inline DFSIGNATURE GetCommitSig() const;
98     inline void SetCommitSig(const DFSIGNATURE sig);
99 
100     // whether machine is using diff order from file format
101     inline BOOL DiffByteOrder() const;
102     inline void ByteSwap();
103 
104 private:
105     SECT  _sectFat[CSECTFAT];
106 
107     SCODE SetSig(const BYTE *pbSig);
108 };
109 
110 
SetFatLength(const FSINDEX cFatSect)111 inline SCODE CMSFHeader::SetFatLength(const FSINDEX cFatSect)
112 {
113     msfDebugOut((DEB_TRACE,"In CMSFHeader::SetFatLength(%lu)\n",cFatSect));
114     _csectFat = cFatSect;
115     msfDebugOut((DEB_TRACE,"Out CMSFHeader::SetFatLength()\n"));
116     return S_OK;
117 }
118 
GetFatLength() const119 inline FSINDEX CMSFHeader::GetFatLength() const
120 {
121     return _csectFat;
122 }
123 
SetMiniFatLength(const FSINDEX cFatSect)124 inline SCODE CMSFHeader::SetMiniFatLength(const FSINDEX cFatSect)
125 {
126     msfDebugOut((DEB_TRACE,"In CMSFHeader::SetMiniFatLength(%lu)\n",cFatSect));
127     _csectMiniFat = cFatSect;
128     msfDebugOut((DEB_TRACE,"Out CMSFHeader::SetMiniFatLength()\n"));
129     return S_OK;
130 }
131 
GetMiniFatLength() const132 inline FSINDEX CMSFHeader::GetMiniFatLength() const
133 {
134     return(_csectMiniFat);
135 }
136 
SetDirStart(const SECT sectNew)137 inline SCODE CMSFHeader::SetDirStart(const SECT sectNew)
138 {
139     _sectDirStart = sectNew;
140     return S_OK;
141 }
142 
GetDirStart() const143 inline SECT CMSFHeader::GetDirStart() const
144 {
145     return _sectDirStart;
146 }
147 
SetFatStart(const SECT sectNew)148 inline SCODE CMSFHeader::SetFatStart(const SECT sectNew)
149 {
150     _sectFat[0] = sectNew;
151     return S_OK;
152 }
153 
GetFatStart() const154 inline SECT CMSFHeader::GetFatStart() const
155 {
156     return _sectFat[0];
157 }
158 
159 //+-------------------------------------------------------------------------
160 //
161 //  Member:     CMSFHeader::SetMiniFatStart
162 //
163 //  Synopsis:   Sets minifat's first sector's index
164 //
165 //  Arguments:  [sectNew]   -- sector index
166 //
167 //  Returns:    S_OK (necessary?)
168 //
169 //  Modifies:   _sectMiniFatStart
170 //
171 //--------------------------------------------------------------------------
172 
SetMiniFatStart(const SECT sectNew)173 inline SCODE CMSFHeader::SetMiniFatStart(const SECT sectNew)
174 {
175     _sectMiniFatStart = sectNew;
176     return S_OK;
177 }
178 
179 //+-------------------------------------------------------------------------
180 //
181 //  Member:     CMSFHeader::GetMiniFatStart
182 //
183 //  Synopsis:   Gets minifat's first sector's index
184 //
185 //  Returns:    minifat's first sector's index
186 //
187 //--------------------------------------------------------------------------
188 
GetMiniFatStart() const189 inline SECT CMSFHeader::GetMiniFatStart() const
190 {
191     return(_sectMiniFatStart);
192 }
193 
SetDifStart(const SECT sectNew)194 inline SCODE CMSFHeader::SetDifStart(const SECT sectNew)
195 {
196     _sectDifStart = sectNew;
197     return S_OK;
198 }
199 
GetDifStart() const200 inline SECT CMSFHeader::GetDifStart() const
201 {
202     return _sectDifStart;
203 }
204 
GetFatSect(const FSINDEX oSect) const205 inline SECT CMSFHeader::GetFatSect(const FSINDEX oSect) const
206 {
207     msfAssert(oSect < CSECTFAT);
208     return _sectFat[oSect];
209 }
210 
SetFatSect(const FSINDEX oSect,const SECT sect)211 inline SCODE CMSFHeader::SetFatSect(const FSINDEX oSect, const SECT sect)
212 {
213     msfAssert(oSect < CSECTFAT);
214     _sectFat[oSect] = sect;
215     return S_OK;
216 }
217 
SetDifLength(const FSINDEX cFatSect)218 inline SCODE CMSFHeader::SetDifLength(const FSINDEX cFatSect)
219 {
220     _csectDif = cFatSect;
221     return S_OK;
222 }
223 
224 
GetDifLength() const225 inline FSINDEX CMSFHeader::GetDifLength() const
226 {
227     return _csectDif;
228 }
229 
230 
GetSectorShift() const231 inline USHORT CMSFHeader::GetSectorShift() const
232 {
233     return _uSectorShift;
234 }
235 
236 
GetCommitSig() const237 inline DFSIGNATURE CMSFHeader::GetCommitSig() const
238 {
239     return _signature;
240 }
241 
SetCommitSig(const DFSIGNATURE sig)242 inline void CMSFHeader::SetCommitSig(const DFSIGNATURE sig)
243 {
244     _signature = sig;
245 }
246 
GetMiniSectorShift() const247 inline USHORT CMSFHeader::GetMiniSectorShift() const
248 {
249     return _uMiniSectorShift;
250 }
251 
GetMiniSectorCutoff() const252 inline ULONG CMSFHeader::GetMiniSectorCutoff() const
253 {
254     return _ulMiniSectorCutoff;
255 }
256 
GetMinorVersion() const257 inline USHORT CMSFHeader::GetMinorVersion() const
258 {
259     return _uMinorVersion;
260 }
261 
GetDllVersion() const262 inline USHORT CMSFHeader::GetDllVersion() const
263 {
264     return _uDllVersion;
265 }
266 
267 // we always store Litte Endian on disk
268 #define DISK_BYTE_ORDER 0xFFFE
269 
270 // whether machine is using diff order than file format
DiffByteOrder() const271 inline BOOL CMSFHeader::DiffByteOrder() const
272 {
273     // _uByteOrder stores what the machine will see
274     // when it reads in a Little Endian 0xFFFE on disk
275 #ifndef _GENERATE_REVERSE_
276     return _uByteOrder != DISK_BYTE_ORDER;
277 #else
278     return _uByteOrder == DISK_BYTE_ORDER;
279 #endif
280 }
281 
ByteSwap()282 inline void CMSFHeader::ByteSwap()
283 {
284     if (DiffByteOrder())
285     {
286   ::ByteSwap(& _uMinorVersion);
287   ::ByteSwap(& _uDllVersion);
288   // note: _uByteOrder should not be swapped so that we know
289   //       which Endian the machine is operating in.
290   ::ByteSwap(& _uSectorShift);
291   ::ByteSwap(& _uMiniSectorShift);
292 
293   ::ByteSwap(& _usReserved);
294   ::ByteSwap(& _ulReserved1);
295   ::ByteSwap(& _ulReserved2);
296 
297   ::ByteSwap(& _csectFat);
298   ::ByteSwap(& _sectDirStart);
299 
300   ::ByteSwap(& _signature);
301 
302   ::ByteSwap(& _ulMiniSectorCutoff);
303 
304   ::ByteSwap(& _sectMiniFatStart);
305   ::ByteSwap(& _csectMiniFat);
306 
307   ::ByteSwap(& _sectDifStart);
308   ::ByteSwap(& _csectDif);
309 
310         // swapping the Fat table
311         for (int cnt=0; cnt < CSECTFAT; cnt++)
312             ::ByteSwap( &(_sectFat[cnt]) );
313     }
314 }
315 
316 const ULONG OACCESS = 0xFFFFFF00;
317 const ULONG OREADLOCK = OACCESS + 1;
318 const ULONG CREADLOCKS = 16;
319 const ULONG OUPDATE = OREADLOCK + CREADLOCKS + 1;
320 const ULONG OOPENLOCK = OUPDATE + 1;
321 const ULONG COPENLOCKS = 20;
322 const ULONG OOPENREADLOCK = OOPENLOCK;
323 const ULONG OOPENWRITELOCK = OOPENLOCK + COPENLOCKS;
324 const ULONG OOPENDENYREADLOCK = OOPENWRITELOCK + COPENLOCKS;
325 const ULONG OOPENDENYWRITELOCK = OOPENDENYREADLOCK + COPENLOCKS;
326 
327 
328 #endif //__HEADER_HXX__
329 
330 
331 
332 
333 
334