} // "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); }
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); }
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" { LogAgent.Error("TD0 loader: Invalid header ID"); DialogProvider.Show( "Invalid header ID", "TD0 loader", DlgButtonSet.OK, DlgIcon.Error); return null; } ushort crc = CalculateTD0CRC(result._buffer, 0, result._buffer.Length - 2); ushort stampcrc = getUInt16(result._buffer, 0xA); if (stampcrc != crc) { LogAgent.Warn("TD0 loader: Main header had bad CRC=0x"+crc.ToString("X4")+" (stamp crc=0x"+stampcrc.ToString("X4")+")"); DialogProvider.Show( "Wrong main header CRC", "TD0 loader", DlgButtonSet.OK, DlgIcon.Warning); } return result; }