Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Trojan Server - CDDrive.cs

CDDrive.cs

Caricato da:
Scarica il programma completo

  1. //
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
  7. //  REMAINS UNCHANGED.
  8. //
  9. //  Email:  yetiicb@hotmail.com
  10. //
  11. //  Copyright (C) 2002-2003 Idael Cardoso.
  12. //
  13.  
  14. using System;
  15. using System.Runtime.InteropServices;
  16.  
  17. namespace Ripper
  18. {
  19.   public class CDBufferFiller
  20.   {
  21.     byte[] BufferArray;
  22.     int WritePosition = 0;
  23.  
  24.     public CDBufferFiller(byte[] aBuffer)
  25.     {
  26.       BufferArray = aBuffer;
  27.     }
  28.     public void OnCdDataRead(object sender, DataReadEventArgs ea)
  29.     {
  30.       Buffer.BlockCopy(ea.Data, 0, BufferArray, WritePosition, (int)ea.DataSize);
  31.       WritePosition += (int)ea.DataSize;
  32.     }
  33.  
  34.   }
  35.  
  36.   /// <summary>
  37.         ///
  38.         /// </summary>
  39.   public class CDDrive: IDisposable
  40.         {
  41.     private IntPtr cdHandle;
  42.     private bool TocValid = false;
  43.     private Win32Functions.CDROM_TOC Toc = null;
  44.     private char m_Drive = '\0';
  45.     private DeviceChangeNotificationWindow NotWnd = null;
  46.  
  47.     public event EventHandler CDInserted;
  48.     public event EventHandler CDRemoved;
  49.    
  50.     public CDDrive()
  51.     {
  52.       Toc = new Win32Functions.CDROM_TOC();
  53.       cdHandle = IntPtr.Zero;
  54.     }
  55.    
  56.     public bool Open(char Drive)
  57.     {
  58.       Close();
  59.       if ( Win32Functions.GetDriveType(Drive+":\\") == Win32Functions.DriveTypes.DRIVE_CDROM )
  60.       {
  61.         cdHandle = Win32Functions.CreateFile("\\\\.\\"+Drive+':', Win32Functions.GENERIC_READ, Win32Functions.FILE_SHARE_READ, IntPtr.Zero, Win32Functions.OPEN_EXISTING, 0, IntPtr.Zero);
  62.         if ( ((int)cdHandle != -1) && ((int)cdHandle != 0) )
  63.         {
  64.           m_Drive = Drive;
  65.           NotWnd = new DeviceChangeNotificationWindow();
  66.           NotWnd.DeviceChange +=new DeviceChangeEventHandler(NotWnd_DeviceChange);
  67.           return true;
  68.         }          
  69.         else
  70.         {
  71.           return true;
  72.         }
  73.       }
  74.       else
  75.       {
  76.         return false;
  77.       }
  78.     }
  79.  
  80.     public void Close()
  81.     {
  82.       UnLockCD();
  83.       if ( NotWnd != null )
  84.       {
  85.         NotWnd.DestroyHandle();
  86.         NotWnd = null;
  87.       }
  88.       if ( ((int)cdHandle != -1) && ((int)cdHandle != 0) )
  89.       {
  90.         Win32Functions.CloseHandle(cdHandle);
  91.       }
  92.       cdHandle = IntPtr.Zero;
  93.       m_Drive = '\0';
  94.       TocValid = false;
  95.     }
  96.  
  97.     public bool IsOpened
  98.     {
  99.       get
  100.       {
  101.         return ((int)cdHandle != -1) && ((int)cdHandle != 0);
  102.       }
  103.     }
  104.  
  105.     public void Dispose()
  106.     {
  107.       Close();
  108.       GC.SuppressFinalize(this);
  109.     }
  110.  
  111.     ~CDDrive()      
  112.     {
  113.       Dispose();
  114.     }
  115.  
  116.     protected bool ReadTOC()
  117.     {
  118.       if ( ((int)cdHandle != -1) && ((int)cdHandle != 0) )
  119.       {
  120.         uint BytesRead = 0;
  121.         TocValid = Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_CDROM_READ_TOC, IntPtr.Zero, 0, Toc, (uint)Marshal.SizeOf(Toc), ref BytesRead, IntPtr.Zero) != 0;
  122.       }
  123.       else
  124.       {
  125.         TocValid = false;
  126.       }
  127.       return TocValid;
  128.     }
  129.     protected int GetStartSector(int track)
  130.     {
  131.       if ( TocValid && (track >= Toc.FirstTrack) && (track <= Toc.LastTrack) )
  132.       {
  133.         Win32Functions.TRACK_DATA td = Toc.TrackData[track-1];
  134.         return (td.Address_1*60*75 + td.Address_2*75 + td.Address_3)-150;
  135.       }
  136.       else
  137.       {
  138.         return -1;
  139.       }
  140.     }
  141.     protected int GetEndSector(int track)
  142.     {
  143.       if ( TocValid && (track >= Toc.FirstTrack) && (track <= Toc.LastTrack) )
  144.       {
  145.         Win32Functions.TRACK_DATA td = Toc.TrackData[track];
  146.         return (td.Address_1*60*75 + td.Address_2*75 + td.Address_3)-151;
  147.       }
  148.       else
  149.       {
  150.         return -1;
  151.       }
  152.     }
  153.  
  154.     protected const int NSECTORS = 13;
  155.     protected const int UNDERSAMPLING = 1;
  156.     protected const int CB_CDDASECTOR = 2368;
  157.     protected const int CB_QSUBCHANNEL = 16;
  158.     protected const int CB_CDROMSECTOR = 2048;
  159.     protected const int CB_AUDIO = (CB_CDDASECTOR-CB_QSUBCHANNEL);
  160.     /// <summary>
  161.     /// Read Audio Sectors
  162.     /// </summary>
  163.     /// <param name="sector">The sector where to start to read</param>
  164.     /// <param name="Buffer">The length must be at least CB_CDDASECTOR*Sectors bytes</param>
  165.     /// <param name="NumSectors">Number of sectors to read</param>
  166.     /// <returns>True on success</returns>
  167.     protected bool ReadSector(int sector, byte[] Buffer, int NumSectors)
  168.     {
  169.       if ( TocValid && ((sector+NumSectors) <= GetEndSector(Toc.LastTrack)) && (Buffer.Length >= CB_AUDIO*NumSectors))
  170.       {
  171.         Win32Functions.RAW_READ_INFO rri = new Win32Functions.RAW_READ_INFO();
  172.         rri.TrackMode = Win32Functions.TRACK_MODE_TYPE.CDDA;
  173.         rri.SectorCount = (uint)NumSectors;
  174.         rri.DiskOffset = sector*CB_CDROMSECTOR;
  175.  
  176.         uint BytesRead = 0;
  177.         if ( Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_CDROM_RAW_READ, rri, (uint)Marshal.SizeOf(rri), Buffer, (uint)NumSectors*CB_AUDIO, ref BytesRead, IntPtr.Zero) != 0)
  178.         {
  179.           return true;
  180.         }
  181.         else
  182.         {
  183.           return false;
  184.         }
  185.       }
  186.       else
  187.       {
  188.         return false;
  189.       }
  190.     }
  191.     /// <summary>
  192.     /// Lock the CD drive
  193.     /// </summary>
  194.     /// <returns>True on success</returns>
  195.     public bool LockCD()
  196.     {
  197.       if (((int)cdHandle != -1) && ((int)cdHandle != 0))
  198.       {
  199.         uint Dummy = 0;
  200.         Win32Functions.PREVENT_MEDIA_REMOVAL pmr = new Win32Functions.PREVENT_MEDIA_REMOVAL();
  201.         pmr.PreventMediaRemoval = 1;
  202.         return Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_STORAGE_MEDIA_REMOVAL, pmr, (uint)Marshal.SizeOf(pmr), IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0;
  203.       }
  204.       else
  205.       {
  206.         return false;
  207.       }
  208.     }
  209.     /// <summary>
  210.     /// Unlock CD drive
  211.     /// </summary>
  212.     /// <returns>True on success</returns>
  213.     public bool UnLockCD()
  214.     {
  215.       if (((int)cdHandle != -1) && ((int)cdHandle != 0))
  216.       {
  217.         uint Dummy = 0;
  218.         Win32Functions.PREVENT_MEDIA_REMOVAL pmr = new Win32Functions.PREVENT_MEDIA_REMOVAL();
  219.         pmr.PreventMediaRemoval = 0;
  220.         return Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_STORAGE_MEDIA_REMOVAL, pmr, (uint)Marshal.SizeOf(pmr), IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0;
  221.       }
  222.       else
  223.       {
  224.         return false;
  225.       }
  226.     }
  227.     /// <summary>
  228.     /// Close the CD drive door
  229.     /// </summary>
  230.     /// <returns>True on success</returns>
  231.     public bool LoadCD()
  232.     {
  233.       TocValid = false;
  234.       if (((int)cdHandle != -1) && ((int)cdHandle != 0))
  235.       {
  236.         uint Dummy = 0;
  237.         return Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0;
  238.       }
  239.       else
  240.       {
  241.         return false;
  242.       }
  243.     }
  244.     /// <summary>
  245.     /// Open the CD drive door
  246.     /// </summary>
  247.     /// <returns>True on success</returns>
  248.     public bool EjectCD()
  249.     {
  250.       TocValid = false;
  251.       if (((int)cdHandle != -1) && ((int)cdHandle != 0))
  252.       {
  253.         uint Dummy = 0;
  254.         return Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0;
  255.       }
  256.       else
  257.       {
  258.         return false;
  259.       }
  260.     }
  261.     /// <summary>
  262.     /// Check if there is CD in the drive
  263.     /// </summary>
  264.     /// <returns>True on success</returns>
  265.     public bool IsCDReady()
  266.     {
  267.       if (((int)cdHandle != -1) && ((int)cdHandle != 0))
  268.       {
  269.         uint Dummy = 0;
  270.         if (Win32Functions.DeviceIoControl(cdHandle, Win32Functions.IOCTL_STORAGE_CHECK_VERIFY, IntPtr.Zero, 0, IntPtr.Zero, 0, ref Dummy, IntPtr.Zero) != 0)
  271.         {
  272.           return true;
  273.         }
  274.         else
  275.         {
  276.           TocValid = false;
  277.           return false;
  278.         }
  279.       }
  280.       else
  281.       {
  282.         TocValid = false;
  283.         return false;
  284.       }
  285.     }
  286.     /// <summary>
  287.     /// If there is a CD in the drive read its TOC
  288.     /// </summary>
  289.     /// <returns>True on success</returns>
  290.     public bool Refresh()
  291.     {
  292.       if ( IsCDReady() )
  293.       {
  294.         return ReadTOC();
  295.       }
  296.       else
  297.       {
  298.         return false;
  299.       }
  300.     }
  301.     /// <summary>
  302.     /// Return the number of tracks on the CD
  303.     /// </summary>
  304.     /// <returns>-1 on error</returns>
  305.     public int GetNumTracks()
  306.     {
  307.       if ( TocValid )
  308.       {
  309.         return Toc.LastTrack - Toc.FirstTrack + 1;
  310.       }
  311.       else return -1;
  312.     }
  313.     /// <summary>
  314.     /// Return the number of audio tracks on the CD
  315.     /// </summary>
  316.     /// <returns>-1 on error</returns>
  317.     public int GetNumAudioTracks()
  318.     {
  319.       if ( TocValid )
  320.       {
  321.         int tracks = 0;
  322.         for (int i = Toc.FirstTrack - 1; i < Toc.LastTrack; i++)
  323.         {
  324.           if (Toc.TrackData[i].Control == 0 )
  325.             tracks++;
  326.         }
  327.         return tracks;
  328.       }
  329.       else
  330.       {
  331.         return -1;
  332.       }
  333.  
  334.     }
  335.     /// <summary>
  336.     /// Read the digital data of the track
  337.     /// </summary>
  338.     /// <param name="track">Track to read</param>
  339.     /// <param name="Data">Buffer that will receive the data</param>
  340.     /// <param name="DataSize">On return the size needed to read the track</param>
  341.     /// <param name="StartSecond">First second of the track to read, 0 means to start at beginning of the track</param>
  342.     /// <param name="Seconds2Read">Number of seconds to read, 0 means to read until the end of the track</param>
  343.     /// <param name="OnProgress">Delegate to indicate the reading progress</param>
  344.     /// <returns>Negative value means an error. On success returns the number of bytes read</returns>
  345.     public int ReadTrack(int track, byte[] Data, ref uint DataSize, uint StartSecond, uint Seconds2Read, CdReadProgressEventHandler ProgressEvent)
  346.     {
  347.       if ( TocValid && (track >= Toc.FirstTrack) && (track <= Toc.LastTrack) )
  348.       {
  349.         int StartSect = GetStartSector(track);
  350.         int EndSect = GetEndSector(track);
  351.         if ( (StartSect += (int)StartSecond*75) >= EndSect )
  352.         {
  353.           StartSect -= (int)StartSecond*75;
  354.         }
  355.         if ( (Seconds2Read > 0) && ( (int)(StartSect + Seconds2Read*75) < EndSect ) )
  356.         {
  357.           EndSect = StartSect + (int)Seconds2Read*75;
  358.         }
  359.         DataSize = (uint)(EndSect - StartSect)*CB_AUDIO;
  360.         if ( Data != null)
  361.         {
  362.           if ( Data.Length >= DataSize )
  363.           {
  364.             CDBufferFiller BufferFiller = new CDBufferFiller(Data);
  365.             return ReadTrack(track, new CdDataReadEventHandler(BufferFiller.OnCdDataRead), StartSecond, Seconds2Read, ProgressEvent);
  366.           }
  367.           else
  368.           {
  369.             return 0;
  370.           }
  371.         }
  372.         else
  373.         {
  374.           return 0;
  375.         }
  376.       }
  377.       else
  378.       {
  379.         return -1;
  380.       }
  381.     }
  382.     /// <summary>
  383.     /// Read the digital data of the track
  384.     /// </summary>
  385.     /// <param name="track">Track to read</param>
  386.     /// <param name="Data">Buffer that will receive the data</param>
  387.     /// <param name="DataSize">On return the size needed to read the track</param>
  388.     /// <param name="OnProgress">Delegate to indicate the reading progress</param>
  389.     /// <returns>Negative value means an error. On success returns the number of bytes read</returns>
  390.     public int ReadTrack(int track, byte[] Data, ref uint DataSize, CdReadProgressEventHandler ProgressEvent)
  391.     {
  392.       return ReadTrack(track, Data, ref DataSize, 0, 0, ProgressEvent);
  393.     }
  394.     /// <summary>
  395.     /// Read the digital data of the track
  396.     /// </summary>
  397.     /// <param name="track">Track to read</param>
  398.     /// <param name="OnDataRead">Call each time data is read</param>
  399.     /// <param name="StartSecond">First second of the track to read, 0 means to start at beginning of the track</param>
  400.     /// <param name="Seconds2Read">Number of seconds to read, 0 means to read until the end of the track</param>
  401.     /// <param name="OnProgress">Delegate to indicate the reading progress</param>
  402.     /// <returns>Negative value means an error. On success returns the number of bytes read</returns>
  403.     public int ReadTrack(int track, CdDataReadEventHandler DataReadEvent, uint StartSecond, uint Seconds2Read, CdReadProgressEventHandler ProgressEvent)
  404.     {
  405.       if ( TocValid && (track >= Toc.FirstTrack) && (track <= Toc.LastTrack) && (DataReadEvent != null) )
  406.       {
  407.         int StartSect = GetStartSector(track);
  408.         int EndSect = GetEndSector(track);
  409.         if ( (StartSect += (int)StartSecond*75) >= EndSect )
  410.         {
  411.           StartSect -= (int)StartSecond*75;
  412.         }
  413.         if ( (Seconds2Read > 0) && ( (int)(StartSect + Seconds2Read*75) < EndSect ) )
  414.         {
  415.           EndSect = StartSect + (int)Seconds2Read*75;
  416.         }
  417.         uint Bytes2Read = (uint)(EndSect - StartSect)*CB_AUDIO;
  418.         uint BytesRead = 0;
  419.         byte[] Data = new byte[CB_AUDIO*NSECTORS];
  420.         bool Cont = true;
  421.         bool ReadOk = true;
  422.         if ( ProgressEvent != null )
  423.         {
  424.           ReadProgressEventArgs rpa = new ReadProgressEventArgs(Bytes2Read, 0);
  425.           ProgressEvent(this, rpa);
  426.           Cont = !rpa.CancelRead;
  427.         }
  428.         for (int sector = StartSect; (sector < EndSect) && (Cont) && (ReadOk); sector+=NSECTORS)
  429.         {
  430.           int Sectors2Read = ( (sector + NSECTORS) < EndSect )?NSECTORS:(EndSect-sector);
  431.           ReadOk = ReadSector(sector, Data, Sectors2Read);
  432.           if ( ReadOk )
  433.           {
  434.             DataReadEventArgs dra = new DataReadEventArgs(Data, (uint)(CB_AUDIO*Sectors2Read));
  435.             DataReadEvent(this, dra);
  436.             BytesRead += (uint)(CB_AUDIO*Sectors2Read);
  437.             if ( ProgressEvent != null )
  438.             {
  439.               ReadProgressEventArgs rpa = new ReadProgressEventArgs(Bytes2Read, BytesRead);
  440.               ProgressEvent(this, rpa);
  441.               Cont = !rpa.CancelRead;
  442.             }
  443.           }
  444.         }
  445.         if ( ReadOk )
  446.         {
  447.           return (int)BytesRead;
  448.         }
  449.         else
  450.         {
  451.           return -1;
  452.         }
  453.  
  454.       }
  455.       else
  456.       {
  457.         return -1;
  458.       }
  459.     }
  460.     /// <summary>
  461.     /// Read the digital data of the track
  462.     /// </summary>
  463.     /// <param name="track">Track to read</param>
  464.     /// <param name="OnDataRead">Call each time data is read</param>
  465.     /// <param name="OnProgress">Delegate to indicate the reading progress</param>
  466.     /// <returns>Negative value means an error. On success returns the number of bytes read</returns>
  467.     public int ReadTrack(int track, CdDataReadEventHandler DataReadEvent, CdReadProgressEventHandler ProgressEvent)
  468.     {
  469.       return ReadTrack(track, DataReadEvent, 0, 0, ProgressEvent);
  470.     }
  471.     /// <summary>
  472.     /// Get track size
  473.     /// </summary>
  474.     /// <param name="track">Track</param>
  475.     /// <returns>Size in bytes of track data</returns>
  476.     public uint TrackSize(int track)
  477.     {
  478.       uint Size = 0;
  479.       ReadTrack(track, null, ref Size, null);
  480.       return Size;
  481.     }
  482.  
  483.     public bool IsAudioTrack(int track)
  484.     {
  485.       if ( (TocValid) && (track >= Toc.FirstTrack) && (track <= Toc.LastTrack) )
  486.       {
  487.         return (Toc.TrackData[track-1].Control & 4) == 0;
  488.       }
  489.       else
  490.       {
  491.         return false;
  492.       }
  493.     }
  494.  
  495.     public static char[] GetCDDriveLetters()
  496.     {
  497.       string res = "";
  498.       for ( char c = 'C'; c <= 'Z'; c++)
  499.       {
  500.         if ( Win32Functions.GetDriveType(c+":") == Win32Functions.DriveTypes.DRIVE_CDROM )
  501.         {
  502.           res += c;
  503.         }
  504.       }
  505.       return res.ToCharArray();
  506.     }
  507.    
  508.     private void OnCDInserted()
  509.     {
  510.       if ( CDInserted != null )
  511.       {
  512.         CDInserted(this, EventArgs.Empty);
  513.       }
  514.     }
  515.    
  516.     private void OnCDRemoved()
  517.     {
  518.       if ( CDRemoved != null )
  519.       {
  520.         CDRemoved(this, EventArgs.Empty);
  521.       }
  522.     }
  523.  
  524.     private void NotWnd_DeviceChange(object sender, DeviceChangeEventArgs ea)
  525.     {
  526.       if ( ea.Drive == m_Drive )
  527.       {
  528.         TocValid = false;
  529.         switch ( ea.ChangeType )
  530.         {
  531.           case DeviceChangeEventType.DeviceInserted :
  532.             OnCDInserted();
  533.             break;
  534.           case DeviceChangeEventType.DeviceRemoved :
  535.             OnCDRemoved();
  536.             break;
  537.         }
  538.       }
  539.     }
  540.   }
  541. }