www.pudn.com > GGBT.rar > BTFormat.cpp


// BTFormat.cpp: implementation of the CBTFormat class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "testBT.h" 
#include "BTFormat.h" 
 
 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
 
bool HasKey(VALMAP* pmap, string strKey) 
{ 
	VALMAP::iterator iter = pmap->find(strKey); 
	return iter != pmap->end(); 
} 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CBTFormat::CBTFormat() 
{ 
 
} 
 
CBTFormat::~CBTFormat() 
{ 
 
} 
 
void CBTFormat::CheckMessage(CVal *pMsg) 
{ 
	if (pMsg->vt != VT_PMAP) 
		throw string("message type error,should be VT_PMAP"); 
 
 
	/* 
	VALMAP::iterator iter = (*pMsg->pmapVal).find("info1"); 
	if (iter == (*pMsg->pmapVal).end()) 
	{		 
		int i = 0; 
	} 
	else 
	{ 
		int i = 0; 
	} 
	// CVal* pv = (*pMsg->pmapVal)["info1"]; 
	//*/ 
 
	CheckInfo((*pMsg->pmapVal)["info"]); 
 
} 
 
void CBTFormat::CheckInfo(CVal *pInfo) 
{ 
	if (!pInfo || pInfo->vt!=VT_PMAP) 
		throw string("bad metainfo - not a dictionary"); 
 
	CVal* pPieces = (*pInfo->pmapVal)["pieces"]; 
	if (!pPieces || pPieces->vt != VT_PSTR || pPieces->lstrLen% 20 != 0) 
		throw string("bad metainfo - bad pieces key"); 
 
	CVal* pPiecesLength = (*pInfo->pmapVal)["piece length"]; 
	if (!pPiecesLength || pPiecesLength->vt != VT_LONG || pPiecesLength->lVal <= 0) 
		throw string("bad metainfo - illegal piece length"); 
 
	CVal* pName = (*pInfo->pmapVal)["name"]; 
	if (!pName || pName->vt != VT_PSTR || !pName->pstrVal) 
		throw string("bad metainfo - bad name"); 
 
	if (HasKey(pInfo->pmapVal, "files") == HasKey(pInfo->pmapVal, "length")) 
		throw string("bad metainfo - single/multiple file mix"); 
 
 
	if (HasKey(pInfo->pmapVal, "length")) 
	{ 
		CVal* pLength = (*pInfo->pmapVal)["length"]; 
		if (!pLength || pLength->vt != VT_LONG || pLength->lVal<0) 
			throw string("bad metainfo - bad length"); 
	} 
	else 
	{ 
		CVal* pFiles = (*pInfo->pmapVal)["files"]; 
		if (!pFiles || pFiles->vt != VT_PLIST) 
			throw string("bad metainfo - bad file value"); 
 
		for (VALLIST::iterator iter=pFiles->plistVal->begin(); iter!= pFiles->plistVal->end(); iter++) 
		{ 
			CVal* pf = *iter; 
			if (!pf || pf->vt != VT_PMAP) 
				throw string("bad metainfo - bad file value"); 
 
			CVal* pLength = (*pf->pmapVal)["length"]; 
			if (!pLength || pLength->vt != VT_LONG || pLength->lVal<0) 
				throw string("bad metainfo - bad length"); 
 
			CVal* pPath = (*pf->pmapVal)["path"]; 
			if (!pPath || pPath ->vt != VT_PLIST) 
				throw string("bad metainfo - bad path"); 
 
			for (VALLIST::iterator it=pPath->plistVal->begin(); it!= pPath->plistVal->end(); it++) 
			{ 
				CVal* pP = *it; 
				if (!pP || pP->vt != VT_PSTR) 
					throw string("bad metainfo - bad path dir"); 
 
				char* temp = pP->pstrVal; 
			} 
 
		} 
	} 
} 
 
void CBTFormat::check_peers(CVal *pInfo) 
{ 
	if (!pInfo || pInfo->vt != VT_PMAP) 
		throw string("bad metainfo - not a dictionary"); 
 
	if (HasKey(pInfo->pmapVal, "failure reason")) 
	{ 
		if ((*pInfo)["failure reason"]->vt != VT_PSTR) 
			throw string("bad metainfo - failure reason not a string"); 
 
		return; 
	} 
 
	if (!HasKey(pInfo->pmapVal, "peers")) 
		throw string("bad metainfo - have not peers"); 
 
	CVal* pPeers = (*pInfo)["peers"]; 
	if (pPeers->vt != VT_PLIST) 
		throw string("bad metainfo - peers not a list"); 
 
	for (int i=0; iplistVal->size(); i++) 
	{ 
		CVal* p = (*pPeers)[i]; 
 
		if (p->vt != VT_PMAP) 
			throw string("bad metainfo - not a map"); 
		 
		if (!HasKey(p->pmapVal, "ip")) 
			throw string("bad metainfo - have no ip value"); 
 
		if ((*p)["ip"]->vt != VT_PSTR) 
			throw string("bad metainfo - ip value not string"); 
		 
		if (!HasKey(p->pmapVal, "port")) 
			throw string("bad metainfo - have no port value"); 
 
		if ((*p)["port"]->vt != VT_LONG || (*p)["port"]->lVal < 0) 
			throw string("bad metainfo - ip value not long"); 
 
		 
		if (!HasKey(p->pmapVal, "peer id")) 
			throw string("bad metainfo - have no peer id value"); 
 
		if ((*p)["peer id"]->vt != VT_PSTR || (*p)["peer id"]->lstrLen != 20) 
			throw string("bad metainfo - peer id value not string");		 
	} 
 
	if (HasKey(pInfo->pmapVal, "interval")) 
	{ 
		if ((*pInfo)["interval"]->vt != VT_LONG || (*pInfo)["interval"]->lVal < 0) 
			throw string("bad metainfo - interval value not long or < 0"); 
	} 
 
	if (HasKey(pInfo->pmapVal, "min interval")) 
	{ 
		if ((*pInfo)["min interval"]->vt != VT_LONG || (*pInfo)["min interval"]->lVal < 0) 
			throw string("bad metainfo - min interval value not long or < 0"); 
	} 
	 
	 
	if (HasKey(pInfo->pmapVal, "tracker id")) 
	{ 
		if ((*pInfo)["tracker id"]->vt != VT_PSTR) 
			throw string("bad metainfo - tracker id value not string"); 
	} 
 
	if (HasKey(pInfo->pmapVal, "num peers")) 
	{ 
		if ((*pInfo)["num peers"]->vt != VT_LONG || (*pInfo)["num peers"]->lVal < 0) 
			throw string("bad metainfo - 'num peers value not long or < 0"); 
	} 
 
	if (HasKey(pInfo->pmapVal, "done peers")) 
	{ 
		if ((*pInfo)["done peers"]->vt != VT_LONG || (*pInfo)["done peers"]->lVal < 0) 
			throw string("bad metainfo - done peers value not long or < 0"); 
	} 
 
	if (HasKey(pInfo->pmapVal, "last")) 
	{ 
		if ((*pInfo)["last"]->vt != VT_LONG || (*pInfo)["last"]->lVal < 0) 
			throw string("bad metainfo - interval value not long or < 0"); 
	} 
}