LCOV - code coverage report
Current view: top level - libreoffice/tools/bootstrp - md5.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 52 0.0 %
Date: 2012-12-27 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "md5.hxx"
      22             : 
      23             : #include <cstddef>
      24             : #include <stdio.h>
      25             : 
      26             : #include <rtl/strbuf.hxx>
      27             : 
      28             : #ifdef WNT
      29             : #define FILE_OPEN_READ  "rb"
      30             : #else
      31             : #define FILE_OPEN_READ  "r"
      32             : #endif
      33             : 
      34             : // Extended calc_md5_checksum to recognize Windows executables and libraries. To
      35             : // create the same md5 checksum for a (code/data) identical file it ignores a different
      36             : // date and header checksum. Please see crashrep/source/win32/soreport.cpp
      37             : // where the same method is also used. The crash reporter uses the MD5
      38             : // checksums to transfer them to the crash database. You have to make sure that both
      39             : // methods use the same algorithm otherwise there could be problems with stack reports.
      40             : 
      41           0 : void normalize_pe_image(sal_uInt8* buffer, size_t nBufferSize)
      42             : {
      43           0 :     const int OFFSET_PE_OFFSET = 0x3c;
      44           0 :     const int OFFSET_COFF_TIMEDATESTAMP = 4;
      45           0 :     const int PE_SIGNATURE_SIZE = 4;
      46           0 :     const int COFFHEADER_SIZE = 20;
      47           0 :     const int OFFSET_PE_OPTIONALHEADER_CHECKSUM = 64;
      48             : 
      49             :     // Check the header part of the file buffer
      50           0 :     if (buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z'))
      51             :     {
      52           0 :         unsigned long PEHeaderOffset = (long)buffer[OFFSET_PE_OFFSET];
      53           0 :         if (PEHeaderOffset < nBufferSize-4)
      54             :         {
      55           0 :             if ( buffer[PEHeaderOffset+0] == sal_uInt8('P') &&
      56           0 :                  buffer[PEHeaderOffset+1] == sal_uInt8('E') &&
      57           0 :                  buffer[PEHeaderOffset+2] == 0 &&
      58           0 :                  buffer[PEHeaderOffset+3] == 0 )
      59             :             {
      60           0 :                 PEHeaderOffset += PE_SIGNATURE_SIZE;
      61           0 :                 if (PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP < nBufferSize-4)
      62             :                 {
      63             :                     // Set timedatestamp and checksum fields to a normalized
      64             :                     // value to enforce the same MD5 checksum for identical
      65             :                     // Windows  executables/libraries.
      66           0 :                     buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+0] = 0;
      67           0 :                     buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+1] = 0;
      68           0 :                     buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+2] = 0;
      69           0 :                     buffer[PEHeaderOffset+OFFSET_COFF_TIMEDATESTAMP+3] = 0;
      70             :                 }
      71             : 
      72           0 :                 if (PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM < nBufferSize-4)
      73             :                 {
      74             :                     // Set checksum to a normalized value
      75           0 :                     buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM] = 0;
      76           0 :                     buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+1] = 0;
      77           0 :                     buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+2] = 0;
      78           0 :                     buffer[PEHeaderOffset+COFFHEADER_SIZE+OFFSET_PE_OPTIONALHEADER_CHECKSUM+3] = 0;
      79             :                 }
      80             :             }
      81             :         }
      82             :     }
      83           0 : }
      84             : 
      85           0 : rtlDigestError calc_md5_checksum(const char *filename, rtl::OString &rChecksum)
      86             : {
      87           0 :     const size_t BUFFER_SIZE  = 0x1000;
      88           0 :     const size_t MINIMAL_SIZE = 512;
      89             : 
      90             :     sal_uInt8 checksum[RTL_DIGEST_LENGTH_MD5];
      91           0 :     rtlDigestError error = rtl_Digest_E_None;
      92           0 :     rtl::OStringBuffer aChecksumBuf;
      93             : 
      94           0 :     FILE *fp = fopen( filename, FILE_OPEN_READ );
      95             : 
      96           0 :     if ( fp )
      97             :     {
      98           0 :         rtlDigest digest = rtl_digest_createMD5();
      99             : 
     100           0 :         if ( digest )
     101             :         {
     102             :             size_t          nBytesRead;
     103             :             sal_uInt8       buffer[BUFFER_SIZE];
     104           0 :             bool            bHeader(true);
     105             : 
     106           0 :             while ( rtl_Digest_E_None == error &&
     107             :                 0 != (nBytesRead = fread( buffer, 1, sizeof(buffer), fp )) )
     108             :             {
     109           0 :                 if (bHeader)
     110             :                 {
     111           0 :                     bHeader = false;
     112           0 :                     if (nBytesRead >= MINIMAL_SIZE && buffer[0] == sal_uInt8('M') && buffer[1] == sal_uInt8('Z') )
     113           0 :                         normalize_pe_image(buffer, nBytesRead);
     114             :                 }
     115             : 
     116           0 :                 error = rtl_digest_updateMD5( digest, buffer, nBytesRead );
     117             :             }
     118             : 
     119           0 :             if ( rtl_Digest_E_None == error )
     120             :             {
     121           0 :                 error = rtl_digest_getMD5( digest, checksum, sizeof(checksum) );
     122             :             }
     123             : 
     124           0 :             rtl_digest_destroyMD5( digest );
     125             : 
     126           0 :             for ( std::size_t i = 0; i < sizeof(checksum); i++ )
     127             :             {
     128           0 :                 if ( checksum[i] < 16 )
     129           0 :                     aChecksumBuf.append('0');
     130           0 :                 aChecksumBuf.append(static_cast<sal_Int32>(checksum[i]), 16);
     131             :             }
     132             :         }
     133             : 
     134           0 :         fclose( fp );
     135             :     }
     136             :     else
     137           0 :         error = rtl_Digest_E_Unknown;
     138             : 
     139           0 :     rChecksum = aChecksumBuf.makeStringAndClear();
     140             : 
     141           0 :     return error;
     142             : }
     143             : 
     144             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10