00001 #include "stdafx.h"
00002 #include "ISDb.h"
00003 #include "mplayerc.h"
00004
00005 bool hash(LPCTSTR fn, filehash& fh)
00006 {
00007 CFile f;
00008 CFileException fe;
00009 if(!f.Open(fn, CFile::modeRead|CFile::osSequentialScan|CFile::shareDenyNone, &fe))
00010 return false;
00011
00012 CPath p(fn);
00013 p.StripPath();
00014 fh.name = (LPCTSTR)p;
00015
00016 fh.size = f.GetLength();
00017
00018 fh.hash = fh.size;
00019 for(UINT64 tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.Read(&tmp, sizeof(tmp)); fh.hash += tmp, i++);
00020 f.Seek(max(0, (INT64)fh.size - 65536), CFile::begin);
00021 for(UINT64 tmp = 0, i = 0; i < 65536/sizeof(tmp) && f.Read(&tmp, sizeof(tmp)); fh.hash += tmp, i++);
00022
00023 return true;
00024 }
00025
00026 void hash(CPlaylist& pl, CList<filehash>& fhs)
00027 {
00028 fhs.RemoveAll();
00029
00030 POSITION pos = pl.GetHeadPosition();
00031 while(pos)
00032 {
00033 CString fn = pl.GetNext(pos).m_fns.GetHead();
00034 if(AfxGetAppSettings().Formats.FindExt(CPath(fn).GetExtension().MakeLower(), true))
00035 continue;
00036
00037 filehash fh;
00038 if(!hash(fn, fh))
00039 continue;
00040
00041 fhs.AddTail(fh);
00042 }
00043 }
00044
00045 CStringA makeargs(CPlaylist& pl)
00046 {
00047 CList<filehash> fhs;
00048 hash(pl, fhs);
00049
00050 CList<CStringA> args;
00051
00052 POSITION pos = fhs.GetHeadPosition();
00053 for(int i = 0; pos; i++)
00054 {
00055 filehash& fh = fhs.GetNext(pos);
00056
00057 CStringA str;
00058 str.Format("name[%d]=%s&size[%d]=%016I64x&hash[%d]=%016I64x",
00059 i, UrlEncode(CStringA(fh.name)),
00060 i, fh.size,
00061 i, fh.hash);
00062
00063 args.AddTail(str);
00064 }
00065
00066 return Implode(args, '&');
00067 }
00068
00069 bool OpenUrl(CInternetSession& is, CString url, CStringA& str)
00070 {
00071 str.Empty();
00072
00073 try
00074 {
00075 CAutoPtr<CStdioFile> f(is.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_EXISTING_CONNECT));
00076
00077 char buff[1024];
00078 for(int len; (len = f->Read(buff, sizeof(buff))) > 0; str += CStringA(buff, len));
00079
00080 f->Close();
00081 }
00082 catch(CInternetException* ie)
00083 {
00084 ie->Delete();
00085 return false;
00086 }
00087
00088 return true;
00089 }