PFDC file format (Version 4)
============================

All integers are in big-endian format.


File structure
--------------

<file header chunk>

[<comment chunk>]

For each sector:
	<sector header chunk>
	[<tags chunk>]
	[other chunks]
	[<data chunk>]

<end chunk>


Chunk format
------------

offset	size	description

0	4	Chunk ID
4	4	Chunk size (n)
8	n	Chunk data
8+n	4	Chunk CRC

	- The size does not include the chunk ID, chunk size or chunk CRC
	  fields.

	- The chunk CRC covers the chunk ID, chunk size and chunk data.


File header chunk
-----------------

offset	size	description

0	4	Chunk ID ('PFDC' = 0x50464443)
4	4	Chunk size (4)
8	2	Major version (4)
10	2	Minor version (0)
12	4	Chunk CRC


End chunk
---------

offset	size	description

0	4	Chunk ID ('END ')
4	4	Chunk size (0)
8	4	Chunk CRC (0x3d64af78)


Sector header chunk
-------------------

offset	size	description

0	4	Chunk ID ('SECT')
4	4	Chunk size (18)

8	2	Physical cylinder
10	2	Physical head
12	2	Logical cylinder
14	2	Logical head
16	2	Logical sector number
18	2	Sector size in bytes
20	1	Extra ID byte, depends on encoding:
			FM/MFM:	Real sector size as stored in the ID field
			GCR:	Sector format byte
21	1	Compressed sector data
22	2	Sector flags
			0	CRC error in ID
			1	CRC error in data
			2	Deleted data address mark
			3	Missing data adress mark
			14	Alternate sector
			15	Compressed
24	2	Encoding
			0x0000	Unknown
			0x0001	IBM FM, 250 kbit/s
			0x8001	IBM FM, 500 kbit/s
			0x0002	IBM MFM, 500 kbit/s
			0x8002	IBM MFM, 1000 kbit/s
			0x4002	IBM MFM, 2000 kbit/s
			0x0003	Apple Mac GCR, 500 kbit/s
26	4	Chunk CRC

	- The physical cylinder / head / sector are the actual position
	  of the sector on disk. The physical sector number is implied.

	- The logical cylinder / head / sector number are what's recorded
	  in the sector ID.

	- The alternate sector flag indicates that this is the same
	  physical sector as the one immediately preceding it.

	- If the sector is compressed, the <compressed sector data> byte
	  is repeated <sector size> times to generate the sector data and
	  no sector data chunk follows. Otherwise the
	  <compressed sector data> byte is unused and a sector data chunk
	  may follow.


Sector data chunk
-----------------

offset	size	description

0	4	Chunk ID ('DATA')
4	4	Chunk size (n)
8	n	Sector data
8+n	4	Chunk CRC


Comment chunk
-------------

offset	size	description

0	4	Chunk ID ('TEXT')
4	4	Chunk size
8	n	Comment
8+n	4	Chunk CRC

	- Comments should be UTF-8, with lines separated by LF (0x0a).

	- There can be more than one comment chunk in a file. The actual
	  comment is the concatenation of the chunk data of all comment
	  chunks, in the order that they appear in the file.


Tags chunk
----------

offset	size	description

0	4	Chunk ID ('TAGS')
4	4	Chunk size
8	n	Tag data
8+n	4	Chunk CRC

	- The tag data is associated with the sector in the most recent
	  sector header chunk.


CRC
---

	- The algorithm used is big-endian CRC-32C with generator
	  polynomial 0x1edc6f41. The CRC value is initialized to 0.

	unsigned long pfdc4_crc (const unsigned char *src, unsigned cnt)
	{
		unsigned      i, j;
		unsigned long crc;

		crc = 0;

		for (i = 0; i < cnt; i++) {
			crc ^= (unsigned long) (src[i] & 0xff) << 24;

			for (j = 0; j < 8; j++) {
				if (crc & 0x80000000) {
					crc = (crc << 1) ^ 0x1edc6f41;
				}
				else {
					crc = crc << 1;
				}
			}
		}

		return (crc & 0xffffffff);
	}
