LCOV - code coverage report
Current view: top level - source/maths - MD5.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 9 10 90.0 %
Date: 2026-12-03 01:19:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_MD5
      19             : #define INCLUDED_MD5
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : 
      23             : #include <cstring>
      24             : 
      25             : /**
      26             :  * MD5 hashing algorithm. Note that MD5 is broken and must not be used for
      27             :  * anything that requires security.
      28             :  */
      29             : class MD5
      30             : {
      31             : public:
      32             :     static const size_t DIGESTSIZE = 16;
      33             : 
      34             :     MD5();
      35             : 
      36      790741 :     void Update(const std::uint8_t* data, size_t len)
      37             :     {
      38             :         // (Defined inline for efficiency in the common fixed-length fits-in-buffer case)
      39             : 
      40      790741 :         const size_t CHUNK_SIZE = sizeof(m_Buf);
      41             : 
      42      790741 :         m_InputLen += len;
      43             : 
      44             :         // GCC thinks `m_Buf + m_BufLen` can be outside the bound of `m_Buf`. That is because
      45             :         // `m_BufLen + len < CHUNK_SIZE` can evaluate to `true` even if `m_BufLen` is bigger than
      46             :         // `CHUNK_SIZE` due to wrapping. Tell GCC that it's not possible.
      47      790741 :         if (m_BufLen >= CHUNK_SIZE)
      48           0 :             UNREACHABLE;
      49             : 
      50             :         // If we have enough space in m_Buf and won't flush, simply append the input
      51      790741 :         if (m_BufLen + len < CHUNK_SIZE)
      52             :         {
      53      755646 :             memcpy(m_Buf + m_BufLen, data, len);
      54      755646 :             m_BufLen += len;
      55      755646 :             return;
      56             :         }
      57             : 
      58             :         // Fall back to non-inline function if we have to do more work
      59       35095 :         UpdateRest(data, len);
      60             :     }
      61             : 
      62             :     void Final(std::uint8_t* digest);
      63             : 
      64             : private:
      65             :     void InitState();
      66             :     void UpdateRest(const std::uint8_t* data, size_t len);
      67             :     void Transform(const std::uint32_t* in);
      68             :     std::uint32_t m_Digest[4]; // internal state
      69             :     std::uint8_t m_Buf[64]; // buffered input bytes
      70             :     size_t m_BufLen; // bytes in m_Buf that are valid
      71             :     std::uint64_t m_InputLen; // bytes
      72             : };
      73             : 
      74             : #endif // INCLUDED_MD5

Generated by: LCOV version 1.16