office-gobmx/tools/bootstrp/md5.cxx

153 lines
5.6 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_tools.hxx"
#include "md5.hxx"
#include <cstddef>
#include <stdio.h>
#include <tools/string.hxx>
#ifdef WNT
#define FILE_OPEN_READ "rb"
#else
#define FILE_OPEN_READ "r"
#endif
CWS-TOOLING: integrate CWS fwk112 2009-07-17 08:54:15 +0200 cd r274075 : #i102094# MinGW needs additional path info for linking 2009-07-13 15:09:07 +0200 cd r273938 : #i102094# Add special part for MinGW linking to imagehlp.lib 2009-07-10 08:05:14 +0200 cd r273871 : #i102094# Use size_t for constant values to fix warnings 2009-07-09 13:45:27 +0200 cd r273859 : #i102094# MinGW needs include malloc.h for function alloca 2009-07-09 12:37:39 +0200 cd r273856 : #i102094# Fix problem with signed/unsigned comparision warning 2009-06-19 11:34:40 +0200 cd r273154 : #i102094# Added comment for future maintenance 2009-06-19 11:34:11 +0200 cd r273153 : #i102094# Added comment for future maintenance 2009-06-16 17:25:03 +0200 cd r273032 : #i102094# Extend md5.cxx to detect Windows PE images and ignore PE header date+time/checksum to have the same MD5 checksum 2009-06-16 17:22:47 +0200 cd r273031 : #i102094# Added rebase custom action 2009-06-16 17:01:12 +0200 cd r273030 : #i102094# Use rebaseoo.exe instead of rebase.exe to prevent clash with build tool 2009-06-16 17:00:34 +0200 cd r273029 : #i102094# Execute rebase custom action for administrative installations 2009-06-16 16:59:11 +0200 cd r273028 : #i102094# Small changes for rebaseoo/rebasegui tools 2009-06-16 16:58:29 +0200 cd r273027 : i102094# Deliver rebase tool as rebaseoo.exe as build env already use a rebase.exe 2009-06-16 16:57:15 +0200 cd r273026 : i102094# Crash reporter should not use PE header date+time/checksum data for MD5 checksum 2009-05-29 17:03:26 +0200 cd r272474 : #i102094# Fix wrong deliver statement 2009-05-29 14:55:35 +0200 cd r272459 : #i102904# Make executables more robust. Add error message if installation cannot be found 2009-05-28 17:41:07 +0200 cd r272422 : CWS-TOOLING: rebase CWS fwk112 to trunk@272291 (milestone: DEV300:m49) 2009-05-28 17:31:27 +0200 cd r272421 : #i10000# Resolve conflict on merge 2009-05-28 15:32:09 +0200 cd r272409 : #i102094# Added resource and localization files for rebasegui tool 2009-05-27 13:56:33 +0200 cd r272341 : #i102094# Added new command line tool to rebase libraries to a default virtual load address. Additional GUI tool to check the state of an installation 2009-05-26 10:49:20 +0200 cd r272285 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 10:40:02 +0200 cd r272282 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 08:57:24 +0200 cd r272280 : #i102094# Fixed wrong variable usage 2009-05-26 08:41:29 +0200 cd r272278 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-20 16:47:24 +0200 cd r272141 : #102094# Added missing include statement for debug build 2009-05-20 15:39:21 +0200 is r272136 : #102094# rebase to default value in installation 2009-05-20 15:38:38 +0200 is r272135 : #102094# rebase to default value in installation 2009-05-20 15:37:45 +0200 is r272134 : #102094# rebase to default value in installation
2009-07-28 04:21:19 -05:00
// Extended calc_md5_checksum to recognize Windows executables and libraries. To
// create the same md5 checksum for a (code/data) identical file it ignores a different
// date and header checksum. Please see crashrep/source/win32/soreport.cpp
// where the same method is also used. The crash reporter uses the MD5
// checksums to transfer them to the crash database. You have to make sure that both
// methods use the same algorithm otherwise there could be problems with stack reports.
void normalize_pe_image(sal_uInt8* buffer, size_t nBufferSize)
{
const int OFFSET_PE_OFFSET = 0x3c;
const int OFFSET_COFF_TIMEDATESTAMP = 4;
const int PE_SIGNATURE_SIZE = 4;
const int COFFHEADER_SIZE = 20;
const int OFFSET_PE_OPTIONALHEADER_CHECKSUM = 64;
// Check the header part of the file buffer
if (buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z'))
{
unsigned long PEHeaderOffset = (long)buffer[OFFSET_PE_OFFSET];
if (PEHeaderOffset < nBufferSize-4)
{
if ( buffer[PEHeaderOffset+0] == sal_uInt8('P') &&
buffer[PEHeaderOffset+1] == sal_uInt8('E') &&
buffer[PEHeaderOffset+2] == 0 &&
buffer[PEHeaderOffset+3] == 0 )
{
PEHeaderOffset += PE_SIGNATURE_SIZE;
if (PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP < nBufferSize-4)
{
// Set timedatestamp and checksum fields to a normalized
// value to enforce the same MD5 checksum for identical
// Windows executables/libraries.
buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+0] = 0;
buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+1] = 0;
buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+2] = 0;
buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+3] = 0;
}
if (PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM < nBufferSize-4)
{
// Set checksum to a normalized value
buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM] = 0;
buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+1] = 0;
buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+2] = 0;
buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+3] = 0;
}
}
}
}
}
rtlDigestError calc_md5_checksum( const char *filename, ByteString &aChecksum )
{
CWS-TOOLING: integrate CWS fwk112 2009-07-17 08:54:15 +0200 cd r274075 : #i102094# MinGW needs additional path info for linking 2009-07-13 15:09:07 +0200 cd r273938 : #i102094# Add special part for MinGW linking to imagehlp.lib 2009-07-10 08:05:14 +0200 cd r273871 : #i102094# Use size_t for constant values to fix warnings 2009-07-09 13:45:27 +0200 cd r273859 : #i102094# MinGW needs include malloc.h for function alloca 2009-07-09 12:37:39 +0200 cd r273856 : #i102094# Fix problem with signed/unsigned comparision warning 2009-06-19 11:34:40 +0200 cd r273154 : #i102094# Added comment for future maintenance 2009-06-19 11:34:11 +0200 cd r273153 : #i102094# Added comment for future maintenance 2009-06-16 17:25:03 +0200 cd r273032 : #i102094# Extend md5.cxx to detect Windows PE images and ignore PE header date+time/checksum to have the same MD5 checksum 2009-06-16 17:22:47 +0200 cd r273031 : #i102094# Added rebase custom action 2009-06-16 17:01:12 +0200 cd r273030 : #i102094# Use rebaseoo.exe instead of rebase.exe to prevent clash with build tool 2009-06-16 17:00:34 +0200 cd r273029 : #i102094# Execute rebase custom action for administrative installations 2009-06-16 16:59:11 +0200 cd r273028 : #i102094# Small changes for rebaseoo/rebasegui tools 2009-06-16 16:58:29 +0200 cd r273027 : i102094# Deliver rebase tool as rebaseoo.exe as build env already use a rebase.exe 2009-06-16 16:57:15 +0200 cd r273026 : i102094# Crash reporter should not use PE header date+time/checksum data for MD5 checksum 2009-05-29 17:03:26 +0200 cd r272474 : #i102094# Fix wrong deliver statement 2009-05-29 14:55:35 +0200 cd r272459 : #i102904# Make executables more robust. Add error message if installation cannot be found 2009-05-28 17:41:07 +0200 cd r272422 : CWS-TOOLING: rebase CWS fwk112 to trunk@272291 (milestone: DEV300:m49) 2009-05-28 17:31:27 +0200 cd r272421 : #i10000# Resolve conflict on merge 2009-05-28 15:32:09 +0200 cd r272409 : #i102094# Added resource and localization files for rebasegui tool 2009-05-27 13:56:33 +0200 cd r272341 : #i102094# Added new command line tool to rebase libraries to a default virtual load address. Additional GUI tool to check the state of an installation 2009-05-26 10:49:20 +0200 cd r272285 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 10:40:02 +0200 cd r272282 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 08:57:24 +0200 cd r272280 : #i102094# Fixed wrong variable usage 2009-05-26 08:41:29 +0200 cd r272278 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-20 16:47:24 +0200 cd r272141 : #102094# Added missing include statement for debug build 2009-05-20 15:39:21 +0200 is r272136 : #102094# rebase to default value in installation 2009-05-20 15:38:38 +0200 is r272135 : #102094# rebase to default value in installation 2009-05-20 15:37:45 +0200 is r272134 : #102094# rebase to default value in installation
2009-07-28 04:21:19 -05:00
const size_t BUFFER_SIZE = 0x1000;
const size_t MINIMAL_SIZE = 512;
sal_uInt8 checksum[RTL_DIGEST_LENGTH_MD5];
rtlDigestError error = rtl_Digest_E_None;
FILE *fp = fopen( filename, FILE_OPEN_READ );
if ( fp )
{
rtlDigest digest = rtl_digest_createMD5();
if ( digest )
{
size_t nBytesRead;
CWS-TOOLING: integrate CWS fwk112 2009-07-17 08:54:15 +0200 cd r274075 : #i102094# MinGW needs additional path info for linking 2009-07-13 15:09:07 +0200 cd r273938 : #i102094# Add special part for MinGW linking to imagehlp.lib 2009-07-10 08:05:14 +0200 cd r273871 : #i102094# Use size_t for constant values to fix warnings 2009-07-09 13:45:27 +0200 cd r273859 : #i102094# MinGW needs include malloc.h for function alloca 2009-07-09 12:37:39 +0200 cd r273856 : #i102094# Fix problem with signed/unsigned comparision warning 2009-06-19 11:34:40 +0200 cd r273154 : #i102094# Added comment for future maintenance 2009-06-19 11:34:11 +0200 cd r273153 : #i102094# Added comment for future maintenance 2009-06-16 17:25:03 +0200 cd r273032 : #i102094# Extend md5.cxx to detect Windows PE images and ignore PE header date+time/checksum to have the same MD5 checksum 2009-06-16 17:22:47 +0200 cd r273031 : #i102094# Added rebase custom action 2009-06-16 17:01:12 +0200 cd r273030 : #i102094# Use rebaseoo.exe instead of rebase.exe to prevent clash with build tool 2009-06-16 17:00:34 +0200 cd r273029 : #i102094# Execute rebase custom action for administrative installations 2009-06-16 16:59:11 +0200 cd r273028 : #i102094# Small changes for rebaseoo/rebasegui tools 2009-06-16 16:58:29 +0200 cd r273027 : i102094# Deliver rebase tool as rebaseoo.exe as build env already use a rebase.exe 2009-06-16 16:57:15 +0200 cd r273026 : i102094# Crash reporter should not use PE header date+time/checksum data for MD5 checksum 2009-05-29 17:03:26 +0200 cd r272474 : #i102094# Fix wrong deliver statement 2009-05-29 14:55:35 +0200 cd r272459 : #i102904# Make executables more robust. Add error message if installation cannot be found 2009-05-28 17:41:07 +0200 cd r272422 : CWS-TOOLING: rebase CWS fwk112 to trunk@272291 (milestone: DEV300:m49) 2009-05-28 17:31:27 +0200 cd r272421 : #i10000# Resolve conflict on merge 2009-05-28 15:32:09 +0200 cd r272409 : #i102094# Added resource and localization files for rebasegui tool 2009-05-27 13:56:33 +0200 cd r272341 : #i102094# Added new command line tool to rebase libraries to a default virtual load address. Additional GUI tool to check the state of an installation 2009-05-26 10:49:20 +0200 cd r272285 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 10:40:02 +0200 cd r272282 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 08:57:24 +0200 cd r272280 : #i102094# Fixed wrong variable usage 2009-05-26 08:41:29 +0200 cd r272278 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-20 16:47:24 +0200 cd r272141 : #102094# Added missing include statement for debug build 2009-05-20 15:39:21 +0200 is r272136 : #102094# rebase to default value in installation 2009-05-20 15:38:38 +0200 is r272135 : #102094# rebase to default value in installation 2009-05-20 15:37:45 +0200 is r272134 : #102094# rebase to default value in installation
2009-07-28 04:21:19 -05:00
sal_uInt8 buffer[BUFFER_SIZE];
bool bHeader(true);
while ( rtl_Digest_E_None == error &&
0 != (nBytesRead = fread( buffer, 1, sizeof(buffer), fp )) )
{
CWS-TOOLING: integrate CWS fwk112 2009-07-17 08:54:15 +0200 cd r274075 : #i102094# MinGW needs additional path info for linking 2009-07-13 15:09:07 +0200 cd r273938 : #i102094# Add special part for MinGW linking to imagehlp.lib 2009-07-10 08:05:14 +0200 cd r273871 : #i102094# Use size_t for constant values to fix warnings 2009-07-09 13:45:27 +0200 cd r273859 : #i102094# MinGW needs include malloc.h for function alloca 2009-07-09 12:37:39 +0200 cd r273856 : #i102094# Fix problem with signed/unsigned comparision warning 2009-06-19 11:34:40 +0200 cd r273154 : #i102094# Added comment for future maintenance 2009-06-19 11:34:11 +0200 cd r273153 : #i102094# Added comment for future maintenance 2009-06-16 17:25:03 +0200 cd r273032 : #i102094# Extend md5.cxx to detect Windows PE images and ignore PE header date+time/checksum to have the same MD5 checksum 2009-06-16 17:22:47 +0200 cd r273031 : #i102094# Added rebase custom action 2009-06-16 17:01:12 +0200 cd r273030 : #i102094# Use rebaseoo.exe instead of rebase.exe to prevent clash with build tool 2009-06-16 17:00:34 +0200 cd r273029 : #i102094# Execute rebase custom action for administrative installations 2009-06-16 16:59:11 +0200 cd r273028 : #i102094# Small changes for rebaseoo/rebasegui tools 2009-06-16 16:58:29 +0200 cd r273027 : i102094# Deliver rebase tool as rebaseoo.exe as build env already use a rebase.exe 2009-06-16 16:57:15 +0200 cd r273026 : i102094# Crash reporter should not use PE header date+time/checksum data for MD5 checksum 2009-05-29 17:03:26 +0200 cd r272474 : #i102094# Fix wrong deliver statement 2009-05-29 14:55:35 +0200 cd r272459 : #i102904# Make executables more robust. Add error message if installation cannot be found 2009-05-28 17:41:07 +0200 cd r272422 : CWS-TOOLING: rebase CWS fwk112 to trunk@272291 (milestone: DEV300:m49) 2009-05-28 17:31:27 +0200 cd r272421 : #i10000# Resolve conflict on merge 2009-05-28 15:32:09 +0200 cd r272409 : #i102094# Added resource and localization files for rebasegui tool 2009-05-27 13:56:33 +0200 cd r272341 : #i102094# Added new command line tool to rebase libraries to a default virtual load address. Additional GUI tool to check the state of an installation 2009-05-26 10:49:20 +0200 cd r272285 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 10:40:02 +0200 cd r272282 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-26 08:57:24 +0200 cd r272280 : #i102094# Fixed wrong variable usage 2009-05-26 08:41:29 +0200 cd r272278 : #i102094# New command line tool to rebase libraries to a default virtual load address 2009-05-20 16:47:24 +0200 cd r272141 : #102094# Added missing include statement for debug build 2009-05-20 15:39:21 +0200 is r272136 : #102094# rebase to default value in installation 2009-05-20 15:38:38 +0200 is r272135 : #102094# rebase to default value in installation 2009-05-20 15:37:45 +0200 is r272134 : #102094# rebase to default value in installation
2009-07-28 04:21:19 -05:00
if (bHeader)
{
bHeader = false;
if (nBytesRead >= MINIMAL_SIZE && buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z') )
normalize_pe_image(buffer, nBytesRead);
}
error = rtl_digest_updateMD5( digest, buffer, nBytesRead );
}
if ( rtl_Digest_E_None == error )
{
error = rtl_digest_getMD5( digest, checksum, sizeof(checksum) );
}
rtl_digest_destroyMD5( digest );
for ( std::size_t i = 0; i < sizeof(checksum); i++ )
{
if ( checksum[i] < 16 )
aChecksum.Append( "0" );
aChecksum += ByteString::CreateFromInt32( checksum[i], 16 );
}
}
fclose( fp );
}
else
error = rtl_Digest_E_Unknown;
return error;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */