DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/mt32/File.cpp
00001 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
00002  * Copyright (C) 2011, 2012, 2013 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
00003  *
00004  *  This program is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 2.1 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <cstdio>
00019 #include "mt32emu.h"
00020 #include "sha1/sha1.h"
00021 
00022 namespace MT32Emu {
00023 
00024 static void SHA1DigestToString(char *strDigest, const unsigned int intDigest[]) {
00025         sprintf(strDigest, "%08x%08x%08x%08x%08x", intDigest[0], intDigest[1], intDigest[2], intDigest[3], intDigest[4]);
00026 }
00027 
00028 File::File() : sha1DigestCalculated(false), fileSize(0), data(NULL) {
00029         sha1DigestCalculated = false;
00030 }
00031 
00032 const char *File::getSHA1() {
00033         if (sha1DigestCalculated) {
00034                 return sha1Digest;
00035         }
00036         sha1Digest[0] = 0;
00037         sha1DigestCalculated = true;
00038 
00039         if (getData() == NULL) {
00040                 return sha1Digest;
00041         }
00042 
00043         SHA1 sha1;
00044         unsigned int fileDigest[5];
00045 
00046         sha1.Input(data, (unsigned int)fileSize);
00047         if (sha1.Result(fileDigest)) {
00048                 SHA1DigestToString(sha1Digest, fileDigest);
00049         }
00050         return sha1Digest;
00051 }
00052 
00053 }