2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "ShaVisitor.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
2006-08-07 16:05:00 +00:00
|
|
|
ShaVisitor::ShaVisitor():
|
|
|
|
ctx(DIGEST_ALGO_SHA1) {
|
|
|
|
ctx.digestInit();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaVisitor::~ShaVisitor() {
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestFree();
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaVisitor::visit(const Data* d) {
|
|
|
|
if(d->isNumber()) {
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("i", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
} else {
|
|
|
|
string lenStr = Util::llitos(d->getLen());
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate(lenStr.c_str(), lenStr.size());
|
|
|
|
ctx.digestUpdate(":", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate(d->getData(), d->getLen());
|
2006-03-21 14:12:51 +00:00
|
|
|
if(d->isNumber()) {
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("e", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaVisitor::visit(const Dictionary* d) {
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("d", 1);
|
2006-03-22 16:21:11 +00:00
|
|
|
const Order& v = d->getOrder();
|
|
|
|
for(Order::const_iterator itr = v.begin(); itr != v.end(); itr++) {
|
2006-03-21 14:12:51 +00:00
|
|
|
string lenStr = Util::llitos(itr->size());
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate(lenStr.c_str(), lenStr.size());
|
|
|
|
ctx.digestUpdate(":", 1);
|
|
|
|
ctx.digestUpdate(itr->c_str(), itr->size());
|
2006-03-21 14:12:51 +00:00
|
|
|
const MetaEntry* e = d->get(*itr);
|
|
|
|
this->visit(e);
|
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("e", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaVisitor::visit(const List* l) {
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("l", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
for(MetaList::const_iterator itr = l->getList().begin(); itr != l->getList().end(); itr++) {
|
|
|
|
this->visit(*itr);
|
|
|
|
}
|
2006-08-07 16:05:00 +00:00
|
|
|
ctx.digestUpdate("e", 1);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaVisitor::visit(const MetaEntry* e) {
|
|
|
|
if(dynamic_cast<const Data*>(e) != NULL) {
|
|
|
|
visit((const Data*)e);
|
|
|
|
} else if(dynamic_cast<const Dictionary*>(e) != NULL) {
|
|
|
|
visit((const Dictionary*)e);
|
|
|
|
} else if(dynamic_cast<const List*>(e) != NULL) {
|
|
|
|
visit((const List*)e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaVisitor::getHash(unsigned char* hashValue, int& len) {
|
2006-08-07 16:05:00 +00:00
|
|
|
len = ctx.digestLength();
|
|
|
|
ctx.digestFinal(hashValue);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|