/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
21#include "SHA1.h"
22#include <strigi/streameventanalyzer.h>
23#include <strigi/analyzerplugin.h>
24#include <strigi/analysisresult.h>
25#include <strigi/fieldtypes.h>
26#include <list>
27using namespace std;
28using namespace Strigi;
29
30class DigestEventAnalyzerFactory;
31class DigestEventAnalyzer : public Strigi::StreamEventAnalyzer {
32private:
33 CSHA1 sha1;
34 string hash;
35 Strigi::AnalysisResult* analysisresult;
36 const DigestEventAnalyzerFactory* const factory;
37public:
38 DigestEventAnalyzer(const DigestEventAnalyzerFactory*);
39 ~DigestEventAnalyzer();
40 const char* name() const { return "DigestEventAnalyzer"; }
41 void startAnalysis(Strigi::AnalysisResult*);
42 void endAnalysis(bool complete);
43 void handleData(const char* data, uint32_t length);
44 bool isReadyWithStream();
45};
46
47class DigestEventAnalyzerFactory
48 : public Strigi::StreamEventAnalyzerFactory {
49public:
50 const Strigi::RegisteredField* shafield;
51private:
52 const char* name() const {
53 return "DigestEventAnalyzer";
54 }
55 void registerFields(Strigi::FieldRegister&);
56 Strigi::StreamEventAnalyzer* newInstance() const {
57 return new DigestEventAnalyzer(this);
58 }
59};
60
61DigestEventAnalyzer::DigestEventAnalyzer(const DigestEventAnalyzerFactory* f)
62 :factory(f) {
63 analysisresult = 0;
64 hash.resize(40);
65}
66DigestEventAnalyzer::~DigestEventAnalyzer() {
67}
68void
69DigestEventAnalyzer::startAnalysis(AnalysisResult* ar) {
70 analysisresult = ar;
71 sha1.Reset();
72}
73void
74DigestEventAnalyzer::handleData(const char* data, uint32_t length) {
75 sha1.Update((unsigned char*)data, length);
76}
77namespace {
78 const string type("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
79 const string nfoFileHash(
80 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileHash");
81 const string nfohashAlgorithm(
82 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hashAlgorithm");
83 const string SHA1("SHA1");
84 const string hashValue(
85 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hashValue");
86}
87void
88DigestEventAnalyzer::endAnalysis(bool complete) {
89 if (!complete) {
90 return;
91 }
92 unsigned char digest[20];
93 char d[41];
94 sha1.Final();
95 sha1.GetHash(digest);
96 for (int i = 0; i < 20; ++i) {
97 sprintf(d + 2 * i, "%02x", digest[i]);
98 }
99 hash.assign(d);
100 const string hashUri = analysisresult->newAnonymousUri();
101 analysisresult->addValue(factory->shafield, hashUri);
102 analysisresult->addTriplet(hashUri, type, nfoFileHash);
103 analysisresult->addTriplet(hashUri, nfohashAlgorithm, SHA1);
104 analysisresult->addTriplet(hashUri, hashValue, hash);
105 analysisresult = 0;
106}
107bool
108DigestEventAnalyzer::isReadyWithStream() {
109 return false;
110}
111void
112DigestEventAnalyzerFactory::registerFields(FieldRegister& reg) {
113 shafield = reg.registerField(
114 "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#hasHash");
115 addField(shafield);
116}
117// Analyzer
118
119//Factory
120class Factory : public AnalyzerFactoryFactory {
121public:
122 list<StreamEventAnalyzerFactory*>
123 streamEventAnalyzerFactories() const {
124 list<StreamEventAnalyzerFactory*> af;
125 af.push_back(new DigestEventAnalyzerFactory());
126 return af;
127 }
128};
129
130STRIGI_ANALYZER_FACTORY(Factory)
131
132#include "SHA1.cpp"