第二步工作是解析torrent文件,有了bencoding编码解析器 解析torrent文件当然是易如反掌的任务了.
实现的封装类CTorrentParser,完成的主要任务有:
1.判断torrent文件是否有效
2.得到如下的重要信息:
        tracker服务器列表
        文件列表
        分块尺寸
        分块个数
        分块sha1的数组
3.其他的一些次要信息如发布者,发布日期,注释等
4.计算infohash

Metainfo files are bencoded dictionaries with the following keys:

announce
The URL of the tracker.

info
This maps to a dictionary, with keys described below.

The name key maps to a string which is the suggested name to save the file (or directory) as. It is purely advisory.

piece length maps to the number of bytes in each piece the file is split into. For the purposes of transfer, files are split into fixed-size pieces which are all the same length except for possibly the last one which may be truncated. Piece length is almost always a power of two, most commonly 218 = 256 K (BitTorrent prior to version 3.2 uses 220 = 1 M as default).

pieces maps to a string whose length is a multiple of 20. It is to be subdivided into strings of length 20, each of which is the SHA1 hash of the piece at the corresponding index.

There is also a key length or a key files, but not both or neither. If length is present then the download represents a single file, otherwise it represents a set of files which go in a directory structure.

In the single file case, length maps to the length of the file in bytes.

For the purposes of the other keys, the multi-file case is treated as only having a single file by concatenating the files in the order they appear in the files list. The files list is the value files maps to, and is a list of dictionaries containing the following keys:

length
The length of the file, in bytes.
path
A list of strings corresponding to subdirectory names, the last of which is the actual file name (a zero length list is an error case).
In the single file case, the name key is the name of a file, in the muliple file case, it's the name of a directory.

下面是torrent文件解析器的VC++源代码:

// TorrentParser.h: interface for the CTorrentParser class.
//
//
#if !defined(AFX_TORRENTPARSER_H__7E67DA03_B65C_427F_A241_24230BCD6D54__INCLUDED_)
#define AFX_TORRENTPARSER_H__7E67DA03_B65C_427F_A241_24230BCD6D54__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//torrent文件解析器
#include "BEncode.h"
#include "cyfile.h"
#include "cyhash.h"
class CTorrentParser
{
public:
class CFileInfo
{
public:
string strfilename;//文件相对路径
double dbfilelen;//文件长度
};
class CPieceSha1
{
public:
string getstring();
bool isempty();
BYTE btData[20];
};
CTorrentParser();
virtual ~CTorrentParser();
void clear();
bool parse(const char * szFilename);
bool isvalid();
CBEncodeObjectBase * getvalue(const char * szname);
public:
bool getinfo();
list<string> m_listAnnounce;//tracker服务器列表
list<CFileInfo> m_listFile;//文件列表
string m_strName;//建议的默认文件名或者路径名
int m_iPiecelength;//每个分块的长度
CPieceSha1  m_Infohash;//info字段的sha1
CPieceSha1* m_pPieceSha1;//块数组指针
int m_iPiececount;//块个数
int m_iCreationDate;//创建日期
string m_strComment;//注释
string m_strPublisher;  //发布者
string m_strPublisherurl;//发布者网址
string m_strCreatedBy;//创建工具
private:
string m_strfilename;
CCyFile m_cyfile;
CBEncode m_bencode;
CBEncodeDict * m_pRootDict;//解析得到的根节点
};
#endif // !defined(AFX_TORRENTPARSER_H__7E67DA03_B65C_427F_A241_24230BCD6D54__INCLUDED_)
// TorrentParser.cpp: implementation of the CTorrentParser class.
//
//
#include "stdafx.h"
#include "TorrentParser.h"
//
// Construction/Destruction
//
CTorrentParser::CTorrentParser()
{
m_pRootDict = NULL;
}
CTorrentParser::~CTorrentParser()
{
clear();
}
bool CTorrentParser::parse(const char *szFilename)
{
clear();
if(szFilename)
m_strfilename = szFilename;
if(!m_cyfile.IsExist(m_strfilename.c_str()))
return false;
if(m_cyfile.OpenFile(m_strfilename.c_str()))//打开文件
{
BYTE* pData= m_cyfile.GetData();   //读取文件数据
m_cyfile.CloseFile();
m_bencode.parse((const char*)pData);
//得到根节点
list<CBEncodeObjectBase*>::iterator it;
for(it = m_bencode.m_listObj.begin();it!=m_bencode.m_listObj.end();++it)
{
if((*it)->m_type == enum_BEncodeType_Dict)
{
m_pRootDict = (CBEncodeDict*)(*it);
break;
}
}
CBEncodeString* pEOBAnnounce = (CBEncodeString*)getvalue("announce");
CBEncodeDict* pEOBInfo = (CBEncodeDict*)getvalue("info");
if(pEOBAnnounce && pEOBInfo)
true;
}
return false;
}
void CTorrentParser::clear()
{
m_bencode.clear();
m_pRootDict = NULL;
m_cyfile.ReleaseData();
}
//检查是否有效的torrent文件
bool CTorrentParser::isvalid()
{
//检查是否存在announce和info字段
CBEncodeObjectBase* pEOBAnnounce = getvalue("announce");
CBEncodeObjectBase* pEOBInfo = getvalue("info");
if(pEOBAnnounce && pEOBInfo)
return true;
return false;
}
CBEncodeObjectBase * CTorrentParser::getvalue(const char *szname)
{
if(m_pRootDict)
return m_pRootDict->getvalue(szname);
return NULL;
}
//开始获取信息
bool CTorrentParser::getinfo()
{
string strValue;
CBEncodeString* pEOBAnnounce = (CBEncodeString*)getvalue("announce");
CBEncodeDict* pEOBInfo = (CBEncodeDict*)getvalue("info");
if(pEOBAnnounce && pEOBInfo)
{
//得到缺省tracker服务器
if(pEOBAnnounce->getstring(strValue))
m_listAnnounce.push_back(strValue);
//得到备用tracker列表
CBEncodeList* pEOBAnnounceList = (CBEncodeList*)getvalue("announce-list");
if(pEOBAnnounceList && pEOBAnnounceList->m_type == enum_BEncodeType_List)
{
list<CBEncodeObjectBase *>::iterator it;
for(it = pEOBAnnounceList->m_listObj.begin();it!=pEOBAnnounceList->m_listObj.end();++it)
{
if((*it)->m_type == enum_BEncodeType_List)
{
CBEncodeList* pEOBAnnounceList2 = (CBEncodeList*)(*it);
if(pEOBAnnounceList2->m_listObj.begin()!=pEOBAnnounceList2->m_listObj.end())
{
if(((CBEncodeString*)(*pEOBAnnounceList2->m_listObj.begin()))->getstring(strValue))
m_listAnnounce.push_back(strValue);
}
}
}
}
CBEncodeString* pEOBTmp;
CBEncodeInt * pEOBInt;
//得到创建日期
m_iCreationDate = 0;
pEOBInt = (CBEncodeInt*)getvalue("creation date");
if(pEOBInt && pEOBInt->m_type == enum_BEncodeType_Int)
m_iCreationDate = (int)pEOBInt->m_fValue;
//得到注释
pEOBTmp= (CBEncodeString*)getvalue("comment");
if(pEOBTmp)
pEOBTmp->getstring(m_strComment);
//得到创建工具
pEOBTmp = (CBEncodeString*)getvalue("createdby");
if(pEOBTmp)
pEOBTmp->getstring(m_strCreatedBy);
//得到发布者
pEOBTmp = (CBEncodeString*)getvalue("publisher");
if(pEOBTmp)
pEOBTmp->getstring(m_strPublisher);
pEOBTmp = (CBEncodeString*)getvalue("publisher-url");
if(pEOBTmp)
pEOBTmp->getstring(m_strPublisherurl);
//计算infohash
if(pEOBInfo->m_error == enm_BEncodeErr_noerr)
{
CCyHash ch;
BYTE szSha1[21];
if(ch.GetHash(CALG_SHA1,(BYTE*)pEOBInfo->szPos,pEOBInfo->ilen,szSha1))
memcpy(m_Infohash.btData,szSha1,20);
else
memset(m_Infohash.btData,0,20);
//得到建议的默认文件名或者路径名
pEOBTmp = (CBEncodeString*)pEOBInfo->getvalue("name");
if(pEOBTmp)
pEOBTmp->getstring(m_strName);
//如果没有得到发布者,再次尝试获取
if(m_strPublisher.empty())
{
pEOBTmp = (CBEncodeString*)pEOBInfo->getvalue("publisher");
if(pEOBTmp)
pEOBTmp->getstring(m_strPublisher);
pEOBTmp = (CBEncodeString*)pEOBInfo->getvalue("publisher-url");
if(pEOBTmp)
pEOBTmp->getstring(m_strPublisherurl);
}
//得到分块长度
m_iPiecelength = 0;
pEOBInt = (CBEncodeInt*)pEOBInfo->getvalue("piece length");
if(pEOBInt && pEOBInt->m_type == enum_BEncodeType_Int)
m_iPiecelength = (int)pEOBInt->m_fValue;
//得到piece的sha1数组及其piece个数
pEOBTmp = (CBEncodeString*)pEOBInfo->getvalue("pieces");
if(pEOBTmp->m_type == enum_BEncodeType_String && pEOBTmp->m_error == enm_BEncodeErr_noerr && pEOBTmp->m_szData)
{
m_pPieceSha1 = (CPieceSha1*)pEOBTmp->m_szData;
m_iPiececount = pEOBTmp->m_ilen/20;
}
//得到文件列表
CFileInfo fi;
pEOBInt = (CBEncodeInt*)pEOBInfo->getvalue("length");
if(pEOBInt && pEOBInt->m_type == enum_BEncodeType_Int)
{
fi.dbfilelen = (double)pEOBInt->m_fValue;
fi.strfilename = m_strName;
if(fi.strfilename.size()>0&&fi.dbfilelen>0)
m_listFile.push_back(fi);
}
else
{
CBEncodeList * pEOBList = (CBEncodeList*)pEOBInfo->getvalue("files");
if(pEOBList && pEOBList->m_type == enum_BEncodeType_List)
{
list<CBEncodeObjectBase *>::iterator it;
for(it = pEOBList ->m_listObj.begin();it!=pEOBList ->m_listObj.end();++it)
{
if((*it)->m_type == enum_BEncodeType_Dict)
{
fi.dbfilelen = 0;
fi.strfilename = "";
pEOBInt = (CBEncodeInt*)((CBEncodeDict*)(*it))->getvalue("length");
if(pEOBInt && pEOBInt->m_type == enum_BEncodeType_Int)
fi.dbfilelen = (double)pEOBInt->m_fValue;
CBEncodeList* pEOBPathList = (CBEncodeList*)((CBEncodeDict*)(*it))->getvalue("path");
if(pEOBPathList && pEOBPathList->m_type == enum_BEncodeType_List)
{
list<CBEncodeObjectBase *>::iterator it2;
for(it2 = pEOBPathList->m_listObj.begin();it2 != pEOBPathList->m_listObj.end();++it2)
{
if(((CBEncodeString*)(*it2))->getstring(strValue))
fi.strfilename +="//"+ strValue;
else
{
fi.strfilename = "";
break;
}
}
}
if(fi.strfilename.size()>0&&fi.dbfilelen>0)
m_listFile.push_back(fi);
}
}
}
}
//检查主要的信息是否正确获取,如果是就返回成功
if(m_listAnnounce.size()==0 ||
m_listFile.size()==0 ||
m_Infohash.isempty() ||
m_iPiececount == 0 ||
m_iPiecelength == 0 ||
m_pPieceSha1 == NULL)
return false;
return true;
}
}
return false;
}
//检查CPieceSha1是否为空
bool CTorrentParser::CPieceSha1::isempty()
{
CPieceSha1 pstmp;
memset(pstmp.btData,0,sizeof(pstmp));
if(memcmp(pstmp.btData,btData,sizeof(pstmp))==0)
return true;
return false;
}
string CTorrentParser::CPieceSha1::getstring()
{
char sz[100];
char * szTmp = sz;
BYTE bt;
for(int i = 0;i<sizeof(CPieceSha1);i++)
{
bt = btData[i];
if((bt>='a' && bt<='z')||
(bt>='A' && bt<='Z')||
(bt>='0' && bt<='9'))
{
sprintf(szTmp,"%c",bt);
szTmp +=1;
}
else
{
sprintf(szTmp,"%%%02X",bt);
szTmp +=3;
}
}
*szTmp = 0;
return sz;
}
在逸搜论坛有一个解析torrent文件的小工具可以看到测试效果.

torrent文件解析器相关推荐

  1. php解析torrent文件,PHP基于闭包思想实现的BT(torrent)文件解析工具实例详解

    本文实例讲述了PHP基于闭包思想实现的torrent文件解析工具.分享给大家供大家参考,具体如下: PHP对静态词法域的支持有点奇怪,内部匿名函数必须在参数列表后面加上use关键字,显式的说明想要使用 ...

  2. php解析bt,PHP基于闭包思想实现的BT(torrent)文件解析工具实例详解

    PHP基于闭包思想实现的BT(torrent)文件解析工具实例详解 发布于 2017-09-08 20:05:36 | 124 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP( ...

  3. [翻译]运用文件解析器在任意文件中使用虚拟应用路径(~)

    原文出处:http://www.codeproject.com    Using the FileResolver to allow virtual application paths ( ~ ) i ...

  4. 使用springMVC提供的CommonsMultipartResolver文件解析器,实现文件轻松上传

    springMVC提供的前端控制器,可以拦截所有请求,指挥调度所有后台逻辑资源. 使用传统方式进行文件上传,需要我们手动解析request对象,获取文件上传项,再进行文件的上传. springMVC框 ...

  5. Glib学习(17) Key-value文件解析器 Key-value file parser

    glib源码下载:http://ftp.gnome.org/pub/gnome/sources/glib/ glib帮助文档:https://developer.gnome.org/glib/ 本节主 ...

  6. XML - XML学习/XML文件解析器(C++)实现

    XML - XML学习/XML文件解析器(C++)实现 XML概述 ​ XML是一套定义语义标记的规则,这些标记将文档分成许多部件并对这些部件加以标识.它也是元标记语言,用于定义其他与特定领域有关的, ...

  7. 【glib】Key-value文件解析器

    1 头文件 2 描述 3 API 4 实例 4.1 本文Key-value文件解析器如下: 4.2 在准备一个Key-value文件示例文件 tt.txt 4.3 编译: 4.4 运行: 4.5 运行 ...

  8. [C#]Bencode编码算法/torrent文件解析

    最近工作上的事少了些,便想起了打入冷宫N久的博客,于是找了些小玩意做做,放到博客和github上头当是给有兴趣的朋友一起交流. 首先是想简单说下torrent文件的格式,torrent文件其实说白了就 ...

  9. 编写PE文件解析器(三)

    下面有几个表网上资料比较少,因为几乎用不到,我查文档写写吧,这篇写得比较久很抱歉. 7.IMAGE_DIRECTORY_ENTRY_EXCEPTION[异常处理表] CPU特定的并且基于表的异常处理. ...

最新文章

  1. 金融科技创业公司Revolut增加对BCH和XRP支持
  2. 服务器安全:浏览器同源策略与跨域请求、XSS攻击原理及防御策略、如何防御CSRF攻击
  3. keil MDK中如何生成*.bin格式的文件
  4. 从优秀到卓越 pdf_演讲口才培训:演讲能力是卓越领导者需要具备的能力
  5. 基于springboot助学贷款管理毕业设计源码061528
  6. 【Vue】基础入门 —— 黑马程序员
  7. Keil 5安装教程,搭建单片机环境
  8. 英雄联盟3月17日服务器维护,英雄联盟将于3月17日凌晨2点开始进行全区停机维护...
  9. 心电信号质量评估——ecg_qc工具包介绍(二)
  10. 黑苹果EFI引导启动文件,技嘉Z77P-D3+E3-1230 V2+蓝宝石RX560D+Macos10.15.x(5)
  11. Waves 10 win10 如何卸载干净 / 安装 / 如何处理waves mixer application installation failed / 改变默认安装路径
  12. 爬虫:Iron WebScraper 2022 for NET
  13. 结合实例谈谈航拍全景的方法和技巧
  14. 【元胞自动机】元胞自动机多车道信号交叉口仿真【含Matlab源码 818期】
  15. python如何实现接口?
  16. 教资计算机报高中害死初中,教师资格证报名入口必须电脑登录吗_中小学教师资格考试网...
  17. 趣店成功赴美上市,市值一度飙升113亿美元
  18. 木头机器人变魔方_【转】一个木头魔方的制作过程
  19. C语言笔记含源码(变量、输入输出、分支、循环、函数、数组、指针、字符串、结构体)小总结
  20. 2020届秋招正式批次面试经验

热门文章

  1. 保障企业总部与各分支机构之间数据传输的安全性------实验案例解析
  2. 「津津乐道播客」#297 津津有味:这位程序员把骨汤的咸度设置成了1.83%
  3. 弹弹球小游戏(Java)
  4. 容器漏水问题matlab报告,数学实验实验报告1盐水的混合问题.doc
  5. ubuntu笔记本外置显卡开展深度学习(转载)
  6. 电商行业关联“你我他”,穿越牛熊带飞“你我他”
  7. 佳明步频、垂直幅度、触地时间
  8. PO、DO、TO、DTO,BO的区分
  9. 一家公司要了你后,凭什么给你开高工资?
  10. 从商必读:经济诈骗的十四种招术