1 /* -----------------------------------------------------------------------------
2 The copyright in this software is being made available under the BSD
3 License, included below. No patent rights, trademark rights and/or
4 other Intellectual Property Rights other than the copyrights concerning
5 the Software are granted under this license.
6 
7 For any license concerning other Intellectual Property rights than the software,
8 especially patent licenses, a separate Agreement needs to be closed.
9 For more information please contact:
10 
11 Fraunhofer Heinrich Hertz Institute
12 Einsteinufer 37
13 10587 Berlin, Germany
14 www.hhi.fraunhofer.de/vvc
15 vvc@hhi.fraunhofer.de
16 
17 Copyright (c) 2018-2021, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
18 All rights reserved.
19 
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 
23  * Redistributions of source code must retain the above copyright notice,
24    this list of conditions and the following disclaimer.
25  * Redistributions in binary form must reproduce the above copyright notice,
26    this list of conditions and the following disclaimer in the documentation
27    and/or other materials provided with the distribution.
28  * Neither the name of Fraunhofer nor the names of its contributors may
29    be used to endorse or promote products derived from this software without
30    specific prior written permission.
31 
32 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
36 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
42 THE POSSIBILITY OF SUCH DAMAGE.
43 
44 
45 ------------------------------------------------------------------------------------------- */
46 
47 #pragma once
48 
49 #include "MD5.h"
50 
51 #include <streambuf>
52 #include <array>
53 #include <vector>
54 
55 namespace vvdecoderapp
56 {
57 
58 const int MD5_DIGEST_LENGTH = 128/8;
59 
MD5ToHexString(unsigned char hash[MD5_DIGEST_LENGTH])60 static inline std::string MD5ToHexString(unsigned char hash[MD5_DIGEST_LENGTH])
61 {
62   static const char* hex = "0123456789abcdef";
63   std::string result;
64 
65   for(unsigned pos=0; pos < MD5_DIGEST_LENGTH; ++pos)
66   {
67     result += hex[hash[pos] >> 4];
68     result += hex[hash[pos] & 0xf];
69   }
70 
71   return result;
72 }
73 
74 class MD5StreamBuf : public std::streambuf
75 {
76 public:
77   explicit MD5StreamBuf( size_t buf_size = 64 )
m_buffer(buf_size)78     : m_buffer( buf_size )
79   {
80     std::streambuf::setp( &m_buffer.front(), &m_buffer.back() + 1 );
81   }
82 
overflow(int_type ch)83   int_type overflow(int_type ch) override
84   {
85     m_md5_ctx.update( (unsigned char*)m_buffer.data(), (unsigned int)m_buffer.size() );
86 
87     // reset start & end-pointers
88     std::streambuf::setp( &m_buffer.front(), &m_buffer.back() + 1 );
89     return std::streambuf::sputc(ch);
90   }
91 
sync()92   int sync() override
93   {
94     const auto fillLen = std::streambuf::pptr() - std::streambuf::pbase();
95     if( fillLen )
96       m_md5_ctx.update( (unsigned char*)std::streambuf::pbase(), (unsigned int)fillLen );
97     return 0;
98   }
99 
finalize(unsigned char digest[16])100   void finalize(unsigned char digest[16])
101   {
102     sync();
103     m_md5_ctx.finalize( digest);
104   }
105 
finalizeHex()106   std::string finalizeHex()
107   {
108     unsigned char digest[MD5_DIGEST_LENGTH];
109     finalize(digest);
110     return MD5ToHexString(digest);
111   }
112 
113 private:
114   vvdec::libmd5::MD5     m_md5_ctx;
115   std::vector<char_type> m_buffer;
116 };
117 
118 }   // namespace vvdecoderapp
119