PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/strigi-0.7.7/libstreamanalyzer/plugins/eventplugins/digesteventanalyzer.cpp

#
C++ | 132 lines | 104 code | 7 blank | 21 comment | 2 complexity | ef06a736f80d12199225500aa93d263d MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* This file is part of Strigi Desktop Search
  2. *
  3. * Copyright (C) 2007 Jos van den Oever <jos@vandenoever.info>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "SHA1.h"
  21. #include <strigi/streameventanalyzer.h>
  22. #include <strigi/analyzerplugin.h>
  23. #include <strigi/analysisresult.h>
  24. #include <strigi/fieldtypes.h>
  25. #include <list>
  26. using namespace std;
  27. using namespace Strigi;
  28. class DigestEventAnalyzerFactory;
  29. class DigestEventAnalyzer : public Strigi::StreamEventAnalyzer {
  30. private:
  31. CSHA1 sha1;
  32. string hash;
  33. Strigi::AnalysisResult* analysisresult;
  34. const DigestEventAnalyzerFactory* const factory;
  35. public:
  36. DigestEventAnalyzer(const DigestEventAnalyzerFactory*);
  37. ~DigestEventAnalyzer();
  38. const char* name() const { return "DigestEventAnalyzer"; }
  39. void startAnalysis(Strigi::AnalysisResult*);
  40. void endAnalysis(bool complete);
  41. void handleData(const char* data, uint32_t length);
  42. bool isReadyWithStream();
  43. };
  44. class DigestEventAnalyzerFactory
  45. : public Strigi::StreamEventAnalyzerFactory {
  46. public:
  47. const Strigi::RegisteredField* shafield;
  48. private:
  49. const char* name() const {
  50. return "DigestEventAnalyzer";
  51. }
  52. void registerFields(Strigi::FieldRegister&);
  53. Strigi::StreamEventAnalyzer* newInstance() const {
  54. return new DigestEventAnalyzer(this);
  55. }
  56. };
  57. DigestEventAnalyzer::DigestEventAnalyzer(const DigestEventAnalyzerFactory* f)
  58. :factory(f) {
  59. analysisresult = 0;
  60. hash.resize(40);
  61. }
  62. DigestEventAnalyzer::~DigestEventAnalyzer() {
  63. }
  64. void
  65. DigestEventAnalyzer::startAnalysis(AnalysisResult* ar) {
  66. analysisresult = ar;
  67. sha1.Reset();
  68. }
  69. void
  70. DigestEventAnalyzer::handleData(const char* data, uint32_t length) {
  71. sha1.Update((unsigned char*)data, length);
  72. }
  73. namespace {
  74. const string type("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
  75. const string nfoFileHash(
  76. "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileHash");
  77. const string nfohashAlgorithm(
  78. "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hashAlgorithm");
  79. const string SHA1("SHA1");
  80. const string hashValue(
  81. "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hashValue");
  82. }
  83. void
  84. DigestEventAnalyzer::endAnalysis(bool complete) {
  85. if (!complete) {
  86. return;
  87. }
  88. unsigned char digest[20];
  89. char d[41];
  90. sha1.Final();
  91. sha1.GetHash(digest);
  92. for (int i = 0; i < 20; ++i) {
  93. sprintf(d + 2 * i, "%02x", digest[i]);
  94. }
  95. hash.assign(d);
  96. const string hashUri = analysisresult->newAnonymousUri();
  97. analysisresult->addValue(factory->shafield, hashUri);
  98. analysisresult->addTriplet(hashUri, type, nfoFileHash);
  99. analysisresult->addTriplet(hashUri, nfohashAlgorithm, SHA1);
  100. analysisresult->addTriplet(hashUri, hashValue, hash);
  101. analysisresult = 0;
  102. }
  103. bool
  104. DigestEventAnalyzer::isReadyWithStream() {
  105. return false;
  106. }
  107. void
  108. DigestEventAnalyzerFactory::registerFields(FieldRegister& reg) {
  109. shafield = reg.registerField(
  110. "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hasHash");
  111. addField(shafield);
  112. }
  113. // Analyzer
  114. //Factory
  115. class Factory : public AnalyzerFactoryFactory {
  116. public:
  117. list<StreamEventAnalyzerFactory*>
  118. streamEventAnalyzerFactories() const {
  119. list<StreamEventAnalyzerFactory*> af;
  120. af.push_back(new DigestEventAnalyzerFactory());
  121. return af;
  122. }
  123. };
  124. STRIGI_ANALYZER_FACTORY(Factory)
  125. #include "SHA1.cpp"