Support unrar64.dll
[xy_vsfilter.git] / src / apps / mplayerc / ISDb.cpp
blobdf00ab9c639070cbde21720fa748f8ecd8e12c22
1 #include "stdafx.h"
2 #include "ISDb.h"
3 #include "mplayerc.h"
5 bool hash(LPCTSTR fn, filehash& fh)
7 CFile f;
8 CFileException fe;
9 if(!f.Open(fn, CFile::modeRead|CFile::osSequentialScan|CFile::shareDenyNone, &fe))
10 return false;
12 CPath p(fn);
13 p.StripPath();
14 fh.name = (LPCTSTR)p;
16 fh.size = f.GetLength();
18 fh.hash = fh.size;
19 for(UINT64 tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.Read(&tmp, sizeof(tmp)); fh.hash += tmp, i++);
20 f.Seek(max(0, (INT64)fh.size - 65536), CFile::begin);
21 for(UINT64 tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.Read(&tmp, sizeof(tmp)); fh.hash += tmp, i++);
23 return true;
26 void hash(CPlaylist& pl, CList<filehash>& fhs)
28 fhs.RemoveAll();
30 POSITION pos = pl.GetHeadPosition();
31 while(pos)
33 CString fn = pl.GetNext(pos).m_fns.GetHead();
34 if(AfxGetAppSettings().Formats.FindExt(CPath(fn).GetExtension().MakeLower(), true))
35 continue;
37 filehash fh;
38 if(!hash(fn, fh))
39 continue;
41 fhs.AddTail(fh);
45 CStringA makeargs(CPlaylist& pl)
47 CList<filehash> fhs;
48 hash(pl, fhs);
50 CAtlList<CStringA> args;
52 POSITION pos = fhs.GetHeadPosition();
53 for(int i = 0; pos; i++)
55 filehash& fh = fhs.GetNext(pos);
57 CStringA str;
58 str.Format("name[%d]=%s&size[%d]=%016I64x&hash[%d]=%016I64x",
59 i, UrlEncode(CStringA(fh.name)),
60 i, fh.size,
61 i, fh.hash);
63 args.AddTail(str);
66 return Implode(args, '&');
69 bool OpenUrl(CInternetSession& is, CString url, CStringA& str)
71 str.Empty();
73 try
75 CAutoPtr<CStdioFile> f(is.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_EXISTING_CONNECT));
77 char buff[1024];
78 for(int len; (len = f->Read(buff, sizeof(buff))) > 0; str += CStringA(buff, len));
80 f->Close(); // must close it because the desctructor doesn't seem to do it and we will get an exception when "is" is destroying
82 catch(CInternetException* ie)
84 ie->Delete();
85 return false;
88 return true;