brecsum.h

Go to the documentation of this file.
00001 ///
00002 /// \file       brecsum.h
00003 ///             The Parser class for brecsum.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2008-2012, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_TOOLS_BRECSUM_H__
00023 #define __BARRY_TOOLS_BRECSUM_H__
00024 
00025 class ChecksumParser : public Barry::Parser
00026 {
00027         bool m_IncludeIds;
00028         Barry::SHA_CTX m_ctx;
00029 
00030 public:
00031         explicit ChecksumParser(bool IncludeIds)
00032                 : m_IncludeIds(IncludeIds)
00033         {}
00034 
00035         virtual void ParseRecord(const Barry::DBData &data,
00036                                  const Barry::IConverter *ic)
00037         {
00038                 using namespace std;
00039                 using namespace Barry;
00040 
00041                 SHA1_Init(&m_ctx);
00042 
00043                 if( m_IncludeIds ) {
00044                         SHA1_Update(&m_ctx, data.GetDBName().c_str(),
00045                                 data.GetDBName().size());
00046 
00047                         uint8_t recType = data.GetRecType();
00048                         SHA1_Update(&m_ctx, &recType, sizeof(recType));
00049 
00050                         uint32_t uniqueId = data.GetUniqueId();
00051                         SHA1_Update(&m_ctx, &uniqueId, sizeof(uniqueId));
00052                 }
00053 
00054                 int len = data.GetData().GetSize() - data.GetOffset();
00055                 SHA1_Update(&m_ctx,
00056                         data.GetData().GetData() + data.GetOffset(), len);
00057 
00058                 unsigned char sha1[SHA_DIGEST_LENGTH];
00059                 SHA1_Final(sha1, &m_ctx);
00060 
00061                 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
00062                         cout << hex << setfill('0') << setw(2)
00063                                 << (unsigned int) sha1[i];
00064                 }
00065                 cout << endl;
00066         }
00067 };
00068 
00069 #endif
00070