1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*89b56da7SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*89b56da7SAndrew Rist * distributed with this work for additional information
6*89b56da7SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*89b56da7SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist * with the License. You may obtain a copy of the License at
10*89b56da7SAndrew Rist *
11*89b56da7SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*89b56da7SAndrew Rist *
13*89b56da7SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist * KIND, either express or implied. See the License for the
17*89b56da7SAndrew Rist * specific language governing permissions and limitations
18*89b56da7SAndrew Rist * under the License.
19*89b56da7SAndrew Rist *
20*89b56da7SAndrew Rist *************************************************************/
21*89b56da7SAndrew Rist
22*89b56da7SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "md5.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <cstddef>
30cdf0e10cSrcweir #include <stdio.h>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <tools/string.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #ifdef WNT
35cdf0e10cSrcweir #define FILE_OPEN_READ "rb"
36cdf0e10cSrcweir #else
37cdf0e10cSrcweir #define FILE_OPEN_READ "r"
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir
40cdf0e10cSrcweir // Extended calc_md5_checksum to recognize Windows executables and libraries. To
41cdf0e10cSrcweir // create the same md5 checksum for a (code/data) identical file it ignores a different
42cdf0e10cSrcweir // date and header checksum. Please see crashrep/source/win32/soreport.cpp
43cdf0e10cSrcweir // where the same method is also used. The crash reporter uses the MD5
44cdf0e10cSrcweir // checksums to transfer them to the crash database. You have to make sure that both
45cdf0e10cSrcweir // methods use the same algorithm otherwise there could be problems with stack reports.
46cdf0e10cSrcweir
normalize_pe_image(sal_uInt8 * buffer,size_t nBufferSize)47cdf0e10cSrcweir void normalize_pe_image(sal_uInt8* buffer, size_t nBufferSize)
48cdf0e10cSrcweir {
49cdf0e10cSrcweir const int OFFSET_PE_OFFSET = 0x3c;
50cdf0e10cSrcweir const int OFFSET_COFF_TIMEDATESTAMP = 4;
51cdf0e10cSrcweir const int PE_SIGNATURE_SIZE = 4;
52cdf0e10cSrcweir const int COFFHEADER_SIZE = 20;
53cdf0e10cSrcweir const int OFFSET_PE_OPTIONALHEADER_CHECKSUM = 64;
54cdf0e10cSrcweir
55cdf0e10cSrcweir // Check the header part of the file buffer
56cdf0e10cSrcweir if (buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z'))
57cdf0e10cSrcweir {
58cdf0e10cSrcweir unsigned long PEHeaderOffset = (long)buffer[OFFSET_PE_OFFSET];
59cdf0e10cSrcweir if (PEHeaderOffset < nBufferSize-4)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir if ( buffer[PEHeaderOffset+0] == sal_uInt8('P') &&
62cdf0e10cSrcweir buffer[PEHeaderOffset+1] == sal_uInt8('E') &&
63cdf0e10cSrcweir buffer[PEHeaderOffset+2] == 0 &&
64cdf0e10cSrcweir buffer[PEHeaderOffset+3] == 0 )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir PEHeaderOffset += PE_SIGNATURE_SIZE;
67cdf0e10cSrcweir if (PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP < nBufferSize-4)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir // Set timedatestamp and checksum fields to a normalized
70cdf0e10cSrcweir // value to enforce the same MD5 checksum for identical
71cdf0e10cSrcweir // Windows executables/libraries.
72cdf0e10cSrcweir buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+0] = 0;
73cdf0e10cSrcweir buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+1] = 0;
74cdf0e10cSrcweir buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+2] = 0;
75cdf0e10cSrcweir buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+3] = 0;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir if (PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM < nBufferSize-4)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir // Set checksum to a normalized value
81cdf0e10cSrcweir buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM] = 0;
82cdf0e10cSrcweir buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+1] = 0;
83cdf0e10cSrcweir buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+2] = 0;
84cdf0e10cSrcweir buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+3] = 0;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir }
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
calc_md5_checksum(const char * filename,ByteString & aChecksum)91cdf0e10cSrcweir rtlDigestError calc_md5_checksum( const char *filename, ByteString &aChecksum )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir const size_t BUFFER_SIZE = 0x1000;
94cdf0e10cSrcweir const size_t MINIMAL_SIZE = 512;
95cdf0e10cSrcweir
96cdf0e10cSrcweir sal_uInt8 checksum[RTL_DIGEST_LENGTH_MD5];
97cdf0e10cSrcweir rtlDigestError error = rtl_Digest_E_None;
98cdf0e10cSrcweir
99cdf0e10cSrcweir FILE *fp = fopen( filename, FILE_OPEN_READ );
100cdf0e10cSrcweir
101cdf0e10cSrcweir if ( fp )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir rtlDigest digest = rtl_digest_createMD5();
104cdf0e10cSrcweir
105cdf0e10cSrcweir if ( digest )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir size_t nBytesRead;
108cdf0e10cSrcweir sal_uInt8 buffer[BUFFER_SIZE];
109cdf0e10cSrcweir bool bHeader(true);
110cdf0e10cSrcweir
111cdf0e10cSrcweir while ( rtl_Digest_E_None == error &&
112cdf0e10cSrcweir 0 != (nBytesRead = fread( buffer, 1, sizeof(buffer), fp )) )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir if (bHeader)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir bHeader = false;
117cdf0e10cSrcweir if (nBytesRead >= MINIMAL_SIZE && buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z') )
118cdf0e10cSrcweir normalize_pe_image(buffer, nBytesRead);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
121cdf0e10cSrcweir error = rtl_digest_updateMD5( digest, buffer, nBytesRead );
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir if ( rtl_Digest_E_None == error )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir error = rtl_digest_getMD5( digest, checksum, sizeof(checksum) );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir
129cdf0e10cSrcweir rtl_digest_destroyMD5( digest );
130cdf0e10cSrcweir
131cdf0e10cSrcweir for ( std::size_t i = 0; i < sizeof(checksum); i++ )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir if ( checksum[i] < 16 )
134cdf0e10cSrcweir aChecksum.Append( "0" );
135cdf0e10cSrcweir aChecksum += ByteString::CreateFromInt32( checksum[i], 16 );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir }
138cdf0e10cSrcweir
139cdf0e10cSrcweir fclose( fp );
140cdf0e10cSrcweir }
141cdf0e10cSrcweir else
142cdf0e10cSrcweir error = rtl_Digest_E_Unknown;
143cdf0e10cSrcweir
144cdf0e10cSrcweir return error;
145cdf0e10cSrcweir }
146