⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decoder.cpp

📁 多线程下载工具,程序源码,对想开发类似快车或蚂蚁的朋友有用!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * nzb * * Copyright (C) 2004-2006 Mattias Nordstrom <matta at ftlight net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * * * Authors: *   Mattias Nordstrom <matta at ftlight net> * * $Id: decoder.cpp,v 1.8 2005/10/16 12:03:31 mnordstr Exp $ *   This file provides the decoders. */#include "decoder.h"#include "mainwindow.h"Decoder::Decoder(NzbList *nzblist, int thread_id, QMutex *file_lock, QObject *parent){	this->nzblist = nzblist;	this->parent = parent;	this->thread_id = thread_id;	this->file_lock = file_lock;	connect(this, SIGNAL(decodeEvent(QString, int, int)), parent, SLOT(decodeEvent(QString, int, int)));}void Decoder::run(){	qDebug("Decoder running.");	emit decodeEvent("Idle", thread_id);		file = 0;	seg = 0;	processing = false;		this->exec();		emit decodeEvent("Halted", thread_id);	qDebug("Decoder done.");}void Decoder::stop(){	this->exit();}void Decoder::processFiles(){	if (getNext(&file, &seg)) {		emit decodeEvent("Decoding ["+QString::number(file+1)+": "+QString::number(seg+1)+"/"+QString::number(nzblist->getFile(file)->getSegments()->size())+"] "+nzblist->getFile(file)->getSegment(seg)->getMsgid(), thread_id);				processing = true;		//QTimer::singleShot(0, this, SLOT(processFile()));		processFile();	}}void Decoder::processFile(){	if (nzblist->getFile(file)->getEncoding() == NZB_ENC_YENC || nzblist->getFile(file)->getEncoding() == NZB_UNDEF) {		yDecode(file, seg);	}		if (nzblist->getFile(file)->getEncoding() == NZB_ENC_UENC || nzblist->getFile(file)->getEncoding() == NZB_UNDEF) {		if (seg == 0) qDebug("No yEnc data found, trying uudecoding.");		if (nzblist->getFile(file)->getSegments()->size() == 1) {			uDecode(file, seg);		} else {			if (seg == 0) uDecode(file, seg, true, 1);			else if (seg == nzblist->getFile(file)->getSegments()->size() - 1) uDecode(file, seg, true, 3);			else uDecode(file, seg, true, 2);		}						if (nzblist->getFile(file)->getSegment(seg)->getEncStatus() == NZB_URES_ERROR) {			nzblist->getFile(file)->getSegment(seg)->setEncStatus(NZB_ENC_ERROR);		}	}				nzblist->getFile(file)->getSegment(seg)->setStatus(NZB_DECODED);		processing = false;	//emit decodeEvent("Idle", thread_id, 1);	emit decodeEvent("Decoded ["+QString::number(file+1)+": "+QString::number(seg+1)+"/"+QString::number(nzblist->getFile(file)->getSegments()->size())+"] "+nzblist->getFile(file)->getSegment(seg)->getMsgid(), thread_id, 1);}bool Decoder::getNext(int *file, int *seg){	if (nzblist->getList()->size() == 0) return false;	if (processing == true) return false;		file_lock->lock();		for (;;) {		if (nzblist->getFile(*file)->getSegment(*seg)->getStatus() == NZB_DOWNLOADING) {			file_lock->unlock();			emit decodeEvent("Idle", thread_id, 2);			return false;		}		if (nzblist->getFile(*file)->getSegment(*seg)->getStatus() == NZB_DOWNLOADED) {			nzblist->getFile(*file)->getSegment(*seg)->setStatus(NZB_DECODING);			file_lock->unlock();			return true;		}		if (nzblist->getFile(*file)->getSegment(*seg)->getStatus() == NZB_DL_SKIPPED) {			nzblist->getFile(*file)->getSegment(*seg)->setStatus(NZB_DEC_SKIPPED);			file_lock->unlock();			emit decodeEvent("Idle", thread_id, 1);			return false;		}				if (nzblist->getFile(*file)->getSegments()->size() == *seg+1) {			if (nzblist->getList()->size() == *file+1) {				file_lock->unlock();				emit decodeEvent("Idle", thread_id, 3);				return false;			}						*file = *file + 1;			*seg = 0;		} else {			*seg = *seg + 1;		}	}		file_lock->unlock();	return false;}void Decoder::yDecode(int file, int seg){	// This decoder has been written to comply with yEnc revision 1.3	// http://www.yenc.org/yenc-draft.1.3.txt		int i;	int crc32;	QString pcrc32, header;	QByteArray decdata = "";	int ybegin, begin, end, pos = 0, pos2 = 0;	QString *data = nzblist->getFile(file)->getSegment(seg)->getData();		// Initialize struct with defaults.	nzblist->getFile(file)->getSegment(seg)->setEncStatus(NZB_YRES_ERROR);	nzblist->getFile(file)->getSegment(seg)->setFilename("");	nzblist->getFile(file)->getSegment(seg)->setPart(0);	nzblist->getFile(file)->getSegment(seg)->setSize(0);	nzblist->getFile(file)->getSegment(seg)->setTotal(0);	nzblist->getFile(file)->getSegment(seg)->setTotalSize(0);	nzblist->getFile(file)->getSegment(seg)->setCrc32("");	nzblist->getFile(file)->getSegment(seg)->setBlockBegin(0);	nzblist->getFile(file)->getSegment(seg)->setBlockEnd(0);		// Check for yenc encoded data.	if ((pos = data->indexOf("=ybegin ", 0)) == -1) {		return;	}		if (seg == 0) {		nzblist->getFile(file)->setEncoding(NZB_ENC_YENC);	}		ybegin = pos;	pos2 = data->indexOf("\r\n", pos);	begin = pos2 + 2;		header = data->mid(ybegin, 2048);	// part keyword, also means that this is a multi-part file.	if ((pos = header.indexOf(" part=")) != -1 && pos < (begin-ybegin)) {		pos2 = data->indexOf("\r\n", begin);		begin = pos2 + 2;				pos2 = data->indexOf(QRegExp("[ \r\n]"), ybegin+pos+1);				nzblist->getFile(file)->getSegment(seg)->setPart((data->mid(ybegin + pos + 6, pos2 - (ybegin + pos + 6))).toInt());	}	// total keyword.	if ((pos = header.indexOf(" total=")) != -1 && pos < (begin-ybegin)) {		pos2 = header.indexOf(QRegExp("[ \r\n]"), pos+1);		nzblist->getFile(file)->getSegment(seg)->setTotal((header.mid(pos + 7, pos2 - (pos + 7))).toInt());	}	// size keyword.	if ((pos = data->indexOf(" size=", ybegin)) != -1 && pos < begin) {		pos2 = data->indexOf(QRegExp("[ \r\n]"), pos+1);				nzblist->getFile(file)->getSegment(seg)->setTotalSize((data->mid(pos + 6, pos2 - (pos + 6))).toLong());	}		// name keyword.	if ((pos = data->indexOf(" name=", ybegin)) != -1 && pos < begin) {		pos2 = data->indexOf("\r\n", pos);				nzblist->getFile(file)->getSegment(seg)->setFilename(data->mid(pos + 6, pos2 - (pos + 6)).remove("\""));	}		// Multi-part messages have their own =ypart header line.	if (nzblist->getFile(file)->getSegment(seg)->getPart() != 0) {		if ((pos = data->indexOf("\r\n", ybegin)) != -1 && pos < begin) {			if (data->mid(pos + 2, 7) != "=ypart ") {				return;			}						ybegin = pos + 2;						// begin keyword.			if ((pos = data->indexOf(" begin=", ybegin)) != -1 && pos < begin) {				pos2 = data->indexOf(QRegExp("[ \r\n]"), pos+1);						nzblist->getFile(file)->getSegment(seg)->setBlockBegin((data->mid(pos + 7, pos2 - (pos + 7))).toLong());			}						// end keyword.			if ((pos = data->indexOf(" end=", ybegin)) != -1 && pos < begin) {				pos2 = data->indexOf(QRegExp("[ \r\n]"), pos+1);						nzblist->getFile(file)->getSegment(seg)->setBlockEnd((data->mid(pos + 5, pos2 - (pos + 5))).toLong());			}		} else {			return;		}	}		if ((pos = data->lastIndexOf("=yend ")) == -1) {		return;	}		end = pos;	// size keyword.	if ((pos = data->indexOf(" size=", end)) != -1) {		pos2 = data->indexOf(QRegExp("[ \r\n]"), pos+1);		nzblist->getFile(file)->getSegment(seg)->setSize((data->mid(pos + 6, pos2 - (pos + 6))).toLong());		decdata.reserve(nzblist->getFile(file)->getSegment(seg)->getSize());	}		// part keyword.	if ((pos = data->indexOf(" part=", end)) != -1) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -