Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * digest.cpp - Interface config parser 00004 * 00005 * Created: Thu Feb 28 15:51:20 2008 00006 * Copyright 2006-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include <interfaces/generator/digest.h> 00024 #include <interfaces/generator/exceptions.h> 00025 00026 #include <cstdio> 00027 #include <openssl/evp.h> 00028 00029 #define FILE_STEP 1024 00030 00031 using namespace fawkes; 00032 00033 /** @class InterfaceDigest <interfaces/generator/digest.h> 00034 * Interface digest generator. 00035 * Creates the MD5 hash of the given config file. 00036 * @author Tim Niemueller 00037 */ 00038 00039 00040 /** Constructor 00041 * @param config_filename file name of config (interface template) 00042 */ 00043 InterfaceDigest::InterfaceDigest(std::string config_filename) 00044 { 00045 digest = NULL; 00046 00047 EVP_MD_CTX ctx; 00048 if ( ! EVP_DigestInit(&ctx, EVP_md5())) { 00049 throw Exception("Could not initialize digest context"); 00050 } 00051 00052 FILE *f = fopen(config_filename.c_str(), "r"); 00053 void *buf = malloc(FILE_STEP); 00054 while ( ! feof(f) && ! ferror(f) ) { 00055 size_t rb; 00056 if ((rb = fread(buf, 1, FILE_STEP, f)) > 0) { 00057 if ( ! EVP_DigestUpdate(&ctx, buf, rb) ) { 00058 fclose(f); 00059 throw Exception("Failed to update digest"); 00060 } 00061 } 00062 } 00063 if ( ferror(f) ) { 00064 fclose(f); 00065 throw Exception("Failure while reading the file"); 00066 } 00067 fclose(f); 00068 00069 digest_size=EVP_MD_CTX_size(&ctx); 00070 digest = new unsigned char[digest_size]; 00071 00072 if ( ! EVP_DigestFinal(&ctx, digest, NULL) ) { 00073 delete digest; 00074 digest = NULL; 00075 throw Exception("Could not finalize digest"); 00076 } 00077 } 00078 00079 00080 /** Destructor. */ 00081 InterfaceDigest::~InterfaceDigest() 00082 { 00083 delete digest; 00084 } 00085 00086 00087 /** Get hash. 00088 * @return hash 00089 */ 00090 const unsigned char * 00091 InterfaceDigest::get_hash() 00092 { 00093 return digest; 00094 } 00095 00096 00097 /** Get hash size. 00098 * @return hash size 00099 */ 00100 size_t 00101 InterfaceDigest::get_hash_size() 00102 { 00103 return digest_size; 00104 }