ShaVisitor.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "ShaVisitor.h"
  23. #include "Util.h"
  24. ShaVisitor::ShaVisitor() {
  25. #ifdef ENABLE_SHA1DIGEST
  26. sha1DigestInit(ctx);
  27. sha1DigestReset(ctx);
  28. #endif // ENABLE_SHA1DIGEST
  29. }
  30. ShaVisitor::~ShaVisitor() {
  31. #ifdef ENABLE_SHA1DIGEST
  32. sha1DigestFree(ctx);
  33. #endif // ENABLE_SHA1DIGEST
  34. }
  35. void ShaVisitor::visit(const Data* d) {
  36. #ifdef ENABLE_SHA1DIGEST
  37. if(d->isNumber()) {
  38. sha1DigestUpdate(ctx, "i", 1);
  39. } else {
  40. string lenStr = Util::llitos(d->getLen());
  41. sha1DigestUpdate(ctx, lenStr.c_str(), lenStr.size());
  42. sha1DigestUpdate(ctx, ":", 1);
  43. }
  44. sha1DigestUpdate(ctx, d->getData(), d->getLen());
  45. if(d->isNumber()) {
  46. sha1DigestUpdate(ctx, "e", 1);
  47. }
  48. #endif // ENABLE_SHA1DIGEST
  49. }
  50. void ShaVisitor::visit(const Dictionary* d) {
  51. #ifdef ENABLE_SHA1DIGEST
  52. sha1DigestUpdate(ctx, "d", 1);
  53. const Order& v = d->getOrder();
  54. for(Order::const_iterator itr = v.begin(); itr != v.end(); itr++) {
  55. string lenStr = Util::llitos(itr->size());
  56. sha1DigestUpdate(ctx, lenStr.c_str(), lenStr.size());
  57. sha1DigestUpdate(ctx, ":", 1);
  58. sha1DigestUpdate(ctx, itr->c_str(), itr->size());
  59. const MetaEntry* e = d->get(*itr);
  60. this->visit(e);
  61. }
  62. sha1DigestUpdate(ctx, "e", 1);
  63. #endif // ENABLE_SHA1DIGEST
  64. }
  65. void ShaVisitor::visit(const List* l) {
  66. #ifdef ENABLE_SHA1DIGEST
  67. sha1DigestUpdate(ctx, "l", 1);
  68. for(MetaList::const_iterator itr = l->getList().begin(); itr != l->getList().end(); itr++) {
  69. this->visit(*itr);
  70. }
  71. sha1DigestUpdate(ctx, "e", 1);
  72. #endif // ENABLE_SHA1DIGEST
  73. }
  74. void ShaVisitor::visit(const MetaEntry* e) {
  75. if(dynamic_cast<const Data*>(e) != NULL) {
  76. visit((const Data*)e);
  77. } else if(dynamic_cast<const Dictionary*>(e) != NULL) {
  78. visit((const Dictionary*)e);
  79. } else if(dynamic_cast<const List*>(e) != NULL) {
  80. visit((const List*)e);
  81. }
  82. }
  83. void ShaVisitor::getHash(unsigned char* hashValue, int& len) {
  84. #ifdef ENABLE_SHA1DIGEST
  85. sha1DigestFinal(ctx, hashValue);
  86. len = 20;
  87. #endif // ENABLE_SHA1DIGEST
  88. }