1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: md5.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_tools.hxx"
39 #include <tools/string.hxx>
42 #define FILE_OPEN_READ "rb"
44 #define FILE_OPEN_READ "r"
47 // Extended calc_md5_checksum to recognize Windows executables and libraries. To
48 // create the same md5 checksum for a (code/data) identical file it ignores a different
49 // date and header checksum. Please see crashrep/source/win32/soreport.cpp
50 // where the same method is also used. The crash reporter uses the MD5
51 // checksums to transfer them to the crash database. You have to make sure that both
52 // methods use the same algorithm otherwise there could be problems with stack reports.
54 void normalize_pe_image(sal_uInt8
* buffer
, size_t nBufferSize
)
56 const int OFFSET_PE_OFFSET
= 0x3c;
57 const int OFFSET_COFF_TIMEDATESTAMP
= 4;
58 const int PE_SIGNATURE_SIZE
= 4;
59 const int COFFHEADER_SIZE
= 20;
60 const int OFFSET_PE_OPTIONALHEADER_CHECKSUM
= 64;
62 // Check the header part of the file buffer
63 if (buffer
[0] == sal_uInt8('M') && buffer
[1] == sal_uInt8('Z'))
65 unsigned long PEHeaderOffset
= (long)buffer
[OFFSET_PE_OFFSET
];
66 if (PEHeaderOffset
< nBufferSize
-4)
68 if ( buffer
[PEHeaderOffset
+0] == sal_uInt8('P') &&
69 buffer
[PEHeaderOffset
+1] == sal_uInt8('E') &&
70 buffer
[PEHeaderOffset
+2] == 0 &&
71 buffer
[PEHeaderOffset
+3] == 0 )
73 PEHeaderOffset
+= PE_SIGNATURE_SIZE
;
74 if (PEHeaderOffset
+OFFSET_COFF_TIMEDATESTAMP
< nBufferSize
-4)
76 // Set timedatestamp and checksum fields to a normalized
77 // value to enforce the same MD5 checksum for identical
78 // Windows executables/libraries.
79 buffer
[PEHeaderOffset
+OFFSET_COFF_TIMEDATESTAMP
+0] = 0;
80 buffer
[PEHeaderOffset
+OFFSET_COFF_TIMEDATESTAMP
+1] = 0;
81 buffer
[PEHeaderOffset
+OFFSET_COFF_TIMEDATESTAMP
+2] = 0;
82 buffer
[PEHeaderOffset
+OFFSET_COFF_TIMEDATESTAMP
+3] = 0;
85 if (PEHeaderOffset
+COFFHEADER_SIZE
+OFFSET_PE_OPTIONALHEADER_CHECKSUM
< nBufferSize
-4)
87 // Set checksum to a normalized value
88 buffer
[PEHeaderOffset
+COFFHEADER_SIZE
+OFFSET_PE_OPTIONALHEADER_CHECKSUM
] = 0;
89 buffer
[PEHeaderOffset
+COFFHEADER_SIZE
+OFFSET_PE_OPTIONALHEADER_CHECKSUM
+1] = 0;
90 buffer
[PEHeaderOffset
+COFFHEADER_SIZE
+OFFSET_PE_OPTIONALHEADER_CHECKSUM
+2] = 0;
91 buffer
[PEHeaderOffset
+COFFHEADER_SIZE
+OFFSET_PE_OPTIONALHEADER_CHECKSUM
+3] = 0;
98 rtlDigestError
calc_md5_checksum( const char *filename
, ByteString
&aChecksum
)
100 const size_t BUFFER_SIZE
= 0x1000;
101 const size_t MINIMAL_SIZE
= 512;
103 sal_uInt8 checksum
[RTL_DIGEST_LENGTH_MD5
];
104 rtlDigestError error
= rtl_Digest_E_None
;
106 FILE *fp
= fopen( filename
, FILE_OPEN_READ
);
110 rtlDigest digest
= rtl_digest_createMD5();
115 sal_uInt8 buffer
[BUFFER_SIZE
];
118 while ( rtl_Digest_E_None
== error
&&
119 0 != (nBytesRead
= fread( buffer
, 1, sizeof(buffer
), fp
)) )
124 if (nBytesRead
>= MINIMAL_SIZE
&& buffer
[0] == sal_uInt8('M') && buffer
[1] == sal_uInt8('Z') )
125 normalize_pe_image(buffer
, nBytesRead
);
128 error
= rtl_digest_updateMD5( digest
, buffer
, nBytesRead
);
131 if ( rtl_Digest_E_None
== error
)
133 error
= rtl_digest_getMD5( digest
, checksum
, sizeof(checksum
) );
136 rtl_digest_destroyMD5( digest
);
138 for ( std::size_t i
= 0; i
< sizeof(checksum
); i
++ )
140 if ( checksum
[i
] < 16 )
141 aChecksum
.Append( "0" );
142 aChecksum
+= ByteString::CreateFromInt32( checksum
[i
], 16 );
149 error
= rtl_Digest_E_Unknown
;