} // "td" public static TD0_MAIN_HEADER Deserialize(Stream stream) { TD0_MAIN_HEADER result = new TD0_MAIN_HEADER(); stream.Read(result._buffer, 0, result._buffer.Length); ushort ID = getUInt16(result._buffer, 0); if (ID != 0x4454 && ID != 0x6474) // "TD"/"td" { Logger.Error("TD0 loader: Invalid header ID"); Locator.Resolve <IUserMessage>() .Error("TD0 loader\n\nInvalid header ID"); return(null); } ushort crc = CrcTd0.Calculate(result._buffer, 0, result._buffer.Length - 2); ushort stampcrc = getUInt16(result._buffer, 0xA); if (stampcrc != crc) { Logger.Warn("TD0 loader: Main header had bad CRC=0x" + crc.ToString("X4") + " (stamp crc=0x" + stampcrc.ToString("X4") + ")"); Locator.Resolve <IUserMessage>() .Warning("TD0 loader\n\nWrong main header CRC"); } return(result); }
public static TD0_SECTOR Deserialize(Stream stream) { var sector = new TD0_SECTOR(); // C,H,R,N,Flags,hdrcrc low,hdrcrc high var adm = new byte[6]; stream.Read(adm, 0, 6); sector._admark = adm; // data size low, data size high, encoding method, rawdata,... var datahdr = new byte[2]; stream.Read(datahdr, 0, 2); var rawdata = new byte[getUInt16(datahdr, 0)]; stream.Read(rawdata, 0, rawdata.Length); sector._data = unpackData(rawdata); var crc = CrcTd0.Calculate(sector._data, 0, sector._data.Length); if (adm[5] != (crc & 0xFF)) { Logger.Warn( "TD0 loader: Sector data had bad CRC=0x{0:X4} (stamp crc=0x{1:X2}) [C:{2:X2};H:{3:X2};R:{4:X2};N:{5:X2}", crc, adm[5], sector.C, sector.H, sector.R, sector.N); Locator.Resolve <IUserMessage>() .Warning("TD0 loader\n\nSector data had bad CRC"); } sector.SetAdCrc(true); sector.SetDataCrc((sector.Td0Flags & SectorFlags.BadCrc) == 0); return(sector); }
public static TD0_TRACK Deserialize(Stream stream) { var hdr = new TD0_TRACK(); stream.Read(hdr._rawData, 0, 4); if (hdr._rawData[0] != 0xFF) // 0xFF - terminator { var crc = CrcTd0.Calculate(hdr._rawData, 0, 3); if (hdr._rawData[3] != (crc & 0xFF)) { Logger.Warn("TD0 loader: Track header had bad CRC=0x" + crc.ToString("X4") + " (stamp crc=0x" + hdr._rawData[3].ToString("X2") + ") [CYL:0x" + hdr._rawData[1].ToString("X2") + ";SIDE:" + hdr._rawData[2].ToString("X2")); Locator.Resolve <IUserMessage>() .Warning("TD0 loader\n\nTrack header had bad CRC"); } var sectors = new List <Sector>(hdr.SectorCount); for (int s = 0; s < hdr.SectorCount; s++) { hdr._sectorList.Add(TD0_SECTOR.Deserialize(stream)); } } return(hdr); }
private bool loadData(Stream stream) { var mainHdr = TD0_MAIN_HEADER.Deserialize(stream); if (mainHdr == null) { return(false); } if (mainHdr.Ver > 21 || mainHdr.Ver < 10) // 1.0 <= version <= 2.1... { Locator.Resolve <IUserMessage>() .Error("TD0 loader\n\nFormat version is not supported [0x{0:X2}]", mainHdr.Ver); return(false); } if (mainHdr.DataDOS != 0) { Locator.Resolve <IUserMessage>() .Error("TD0 loader\n\n'DOS Allocated sectors were copied' is not supported!"); return(false); } var dataStream = stream; if (mainHdr.IsAdvandcedCompression) { if (mainHdr.Ver < 20) // unsupported Old Advanced compression { Locator.Resolve <IUserMessage>() .Error("TD0 loader\n\nOld Advanced compression is not implemented!"); return(false); } dataStream = new LzssHuffmanStream(stream); } #region debug info //string state = "Main header loaded, "; //if (dataStream is LzssHuffmanStream) // state += "compressed+"; //else // state += "compressed-"; //state += ", "; //if ((mainHdr.Info & 0x80) != 0) // state += "info+"; //else // state += "info-"; //DialogProvider.ShowWarning(state, "TD0 loader"); #endregion var description = string.Empty; if ((mainHdr.Info & 0x80) != 0) { var tmp = new byte[4]; dataStream.Read(tmp, 0, 2); // crc dataStream.Read(tmp, 2, 2); // length var info = new byte[getUInt16(tmp, 2) + 10]; for (int i = 0; i < 4; i++) { info[i] = tmp[i]; } dataStream.Read(info, 4, 6); // year,month,day,hour,min,sec (year is relative to 1900) dataStream.Read(info, 10, info.Length - 10); // description if (CrcTd0.Calculate(info, 2, 8 + getUInt16(info, 2)) != getUInt16(info, 0)) { Locator.Resolve <IUserMessage>() .Warning("TD0 loader\n\nInfo crc wrong!"); } // combine lines splitted by zero var builder = new StringBuilder(); int begin = 10, end = 10; for (; end < info.Length; end++) { if (info[end] == 0 && end > begin) { builder.Append(Encoding.ASCII.GetString(info, begin, end - begin)); builder.Append("\n"); begin = end + 1; } } description = builder.ToString(); } var cylCount = -1; var sideCount = -1; var trackList = new List <TD0_TRACK>(); for (; ;) { var track = TD0_TRACK.Deserialize(dataStream); if (track.SectorCount == 0xFF) { break; } trackList.Add(track); if (cylCount < track.Cylinder) { cylCount = track.Cylinder; } if (sideCount < track.Side) { sideCount = track.Side; } } cylCount++; sideCount++; if (cylCount < 1 || sideCount < 1) { Locator.Resolve <IUserMessage>() .Error("TD0 loader\n\nInvalid disk structure"); return(false); } _diskImage.SetPhysics(cylCount, sideCount); foreach (TD0_TRACK trk in trackList) { _diskImage.GetTrackImage(trk.Cylinder, trk.Side).AssignSectors(trk.SectorList); } _diskImage.Description = description; return(true); }