Deprecate MediaAccess::downloads (accidentally deleted)
[platform/upstream/libzypp.git] / zypp / CheckSum.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/CheckSum.cc
10  *
11 */
12 #include <iostream>
13 #include <sstream>
14
15 #include "zypp/base/Logger.h"
16 #include "zypp/base/Gettext.h"
17 #include "zypp/base/String.h"
18
19 #include "zypp/CheckSum.h"
20 #include "zypp/Digest.h"
21
22 using std::endl;
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   const std::string & CheckSum::md5Type()
29   { static std::string _type( "md5" ); return _type; }
30
31   const std::string & CheckSum::shaType()
32   { static std::string _type( "sha" ); return _type; }
33
34   const std::string & CheckSum::sha1Type()
35   { static std::string _type( "sha1" ); return _type; }
36
37   const std::string & CheckSum::sha256Type()
38   { static std::string _type( "sha256" ); return _type; }
39
40
41   CheckSum::CheckSum()
42   {}
43
44   CheckSum::CheckSum( const std::string & type, const std::string & checksum )
45   : _type( str::toLower( type ) )
46   , _checksum( checksum )
47   {
48     switch ( checksum.size() )
49     {
50       case 64:
51         if ( _type == sha256Type() )
52           return;
53         if ( _type.empty() || _type == shaType() )
54         {
55           _type = sha256Type();
56           return;
57         }
58         // else: dubious
59         break;
60
61       case 40:
62         if ( _type == sha1Type() )
63           return;
64         if ( _type.empty() || _type == shaType() )
65         {
66           _type = sha1Type();
67           return;
68         }
69         // else: dubious
70         break;
71
72       case 32:
73         if (  _type == md5Type() )
74           return;
75         if ( _type.empty() )
76         {
77           _type = md5Type();
78           return;
79         }
80         // else: dubious
81         break;
82
83       case 0:
84         return; // empty checksum is ok
85         break;
86
87       default:
88         if ( _type.empty() )
89         {
90           WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
91           return;
92         }
93         // else: dubious
94         break;
95     }
96
97     // dubious: Throw on malformed known types, otherwise log a warning.
98     std::string msg = str::form ( _("Dubious type '%s' for %u byte checksum '%s'"),
99                                   _type.c_str(), checksum.size(), _checksum.c_str() );
100     if (    _type == md5Type()
101          || _type == shaType()
102          || _type == sha1Type()
103          || _type == sha256Type() )
104     {
105       ZYPP_THROW( CheckSumException( msg ) );
106     }
107     else
108     {
109       WAR << msg << endl;
110     }
111   }
112
113   CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
114   {
115     if ( input_r.good() && ! type_r.empty() )
116       {
117         _type = str::toLower( type_r );
118         _checksum = Digest::digest( _type, input_r );
119         if ( ! input_r.eof() || _checksum.empty() )
120           {
121             _type = _checksum = std::string();
122           }
123       }
124   }
125
126   std::string CheckSum::type() const
127   { return _type; }
128
129   std::string CheckSum::checksum() const
130   { return _checksum; }
131
132   bool CheckSum::empty() const
133   { return (checksum().empty() || type().empty()); }
134
135   std::string CheckSum::asString() const
136   {
137     std::ostringstream str;
138     str << *this;
139     return str.str();
140   }
141
142   std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
143   {
144     if ( obj.checksum().empty() )
145       {
146         return str << std::string("NoCheckSum");
147       }
148
149     return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
150   }
151
152    /** \relates CheckSum */
153   bool operator==( const CheckSum & lhs, const CheckSum & rhs )
154   { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
155
156   /** \relates CheckSum */
157   bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
158   { return ! ( lhs == rhs ); }
159
160   /////////////////////////////////////////////////////////////////
161 } // namespace zypp
162 ///////////////////////////////////////////////////////////////////