00001
00002
00003
00004
00005 #include "stdafx.h"
00006 #include "vmsBtFileImpl.h"
00007 #include "common.h"
00008 #include <fstream>
00009
00010 __declspec(dllexport) vmsBtFile* WINAPI vmsBt_CreateTorrentFileObject ()
00011 {
00012 return new vmsBtFileImpl;
00013 }
00014
00015 vmsBtFileImpl::vmsBtFileImpl(void)
00016 {
00017 m_torrent = NULL;
00018 m_cRefs = 1;
00019 }
00020
00021 vmsBtFileImpl::~vmsBtFileImpl(void)
00022 {
00023 if (m_torrent)
00024 delete m_torrent;
00025 }
00026
00027 BOOL vmsBtFileImpl::LoadFromFile (LPCSTR pszTorrentFile)
00028 {
00029 SAFE_DELETE (m_torrent);
00030
00031 HANDLE hFile = CreateFileA (pszTorrentFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
00032 if (hFile == INVALID_HANDLE_VALUE)
00033 return FALSE;
00034
00035 DWORD dwSize = GetFileSize (hFile, NULL);
00036
00037 LPBYTE pbTorrent = new BYTE [dwSize];
00038
00039 BOOL b = ReadFile (hFile, pbTorrent, dwSize, &dwSize, NULL);
00040
00041 CloseHandle (hFile);
00042
00043 if (b)
00044 b = LoadFromBuffer (pbTorrent, dwSize);
00045
00046 delete [] pbTorrent;
00047
00048 return b;
00049 }
00050
00051 BOOL vmsBtFileImpl::LoadFromBuffer (LPBYTE pbTorrent, DWORD dwTorrentSize)
00052 {
00053 SAFE_DELETE (m_torrent);
00054
00055 try {
00056 m_torrent = new torrent_info (
00057 bdecode (pbTorrent, pbTorrent + dwTorrentSize));
00058 }
00059 catch (...) {
00060 return FALSE;
00061 }
00062
00063 return TRUE;
00064 }
00065
00066 void vmsBtFileImpl::Release ()
00067 {
00068 if (InterlockedDecrement (&m_cRefs) == 0)
00069 delete this;
00070 }
00071
00072 void vmsBtFileImpl::AddRef ()
00073 {
00074 InterlockedIncrement (&m_cRefs);
00075 }
00076
00077 int vmsBtFileImpl::get_FileCount ()
00078 {
00079 return m_torrent->num_files ();
00080 }
00081
00082 void vmsBtFileImpl::get_FileName (int nIndex, LPSTR pszRes)
00083 {
00084 strcpy (pszRes, m_torrent->file_at (nIndex).path.string ().c_str ());
00085 }
00086
00087 UINT64 vmsBtFileImpl::get_FileSize (int nIndex)
00088 {
00089 return m_torrent->file_at (nIndex).size;
00090 }
00091
00092 UINT64 vmsBtFileImpl::get_TotalFilesSize ()
00093 {
00094 return m_torrent->total_size ();
00095 }
00096
00097 void vmsBtFileImpl::get_TorrentName (LPSTR pszRes)
00098 {
00099 strcpy (pszRes, m_torrent->name ().c_str ());
00100 }
00101
00102 BOOL vmsBtFileImpl::get_TorrentBuffer (LPBYTE pbRes, DWORD dwSize, DWORD *pdwTorrentSize)
00103 {
00104 entry e = m_torrent->create_torrent ();
00105
00106 std::vector <char> v;
00107 bencode (std::back_inserter (v), e);
00108
00109 *pdwTorrentSize = v.size ();
00110
00111 if (pbRes == NULL)
00112 return TRUE;
00113
00114 if (dwSize < v.size ())
00115 return FALSE;
00116
00117 for (size_t i = 0; i < v.size (); i++)
00118 *pbRes++ = v [i];
00119
00120 return TRUE;
00121 }
00122
00123 void vmsBtFileImpl::get_InfoHash (LPSTR pszRes)
00124 {
00125 const sha1_hash& hash = m_torrent->info_hash ();
00126 for (size_t i = 0; i < hash.size; i++)
00127 {
00128 sprintf (pszRes, "%02x", (int)hash [i]);
00129 pszRes += strlen (pszRes);
00130 }
00131 }
00132
00133 void vmsBtFileImpl::get_TrackerUrl (LPSTR pszRes, int nIndex)
00134 {
00135 const std::vector <announce_entry> &v = m_torrent->trackers ();
00136 strcpy (pszRes, size_t(nIndex) < v.size () ? v [nIndex].url.c_str () : "");
00137 }
00138
00139 int vmsBtFileImpl::get_TrackerCount ()
00140 {
00141 return m_torrent->trackers ().size ();
00142 }
00143
00144 void vmsBtFileImpl::get_TorrentComment(LPSTR pszRes)
00145 {
00146 strcpy (pszRes, m_torrent->comment ().c_str ());
00147 }
00148
00149 int vmsBtFileImpl::get_PieceCount ()
00150 {
00151 return m_torrent->num_pieces ();
00152 }
00153
00154 int vmsBtFileImpl::get_PieceSize ()
00155 {
00156 return (int)m_torrent->piece_size (0);
00157 }
00158