libzypp  14.29.1
CheckSum.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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 
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( const std::string & type, const std::string & checksum )
42  : _type( str::toLower( type ) )
43  , _checksum( checksum )
44  {
45  switch ( checksum.size() )
46  {
47  case 64:
48  if ( _type == sha256Type() )
49  return;
50  if ( _type.empty() || _type == shaType() )
51  {
52  _type = sha256Type();
53  return;
54  }
55  // else: dubious
56  break;
57 
58  case 40:
59  if ( _type == sha1Type() )
60  return;
61  if ( _type.empty() || _type == shaType() )
62  {
63  _type = sha1Type();
64  return;
65  }
66  // else: dubious
67  break;
68 
69  case 32:
70  if ( _type == md5Type() )
71  return;
72  if ( _type.empty() )
73  {
74  _type = md5Type();
75  return;
76  }
77  // else: dubious
78  break;
79 
80  case 0:
81  return; // empty checksum is ok
82  break;
83 
84  default:
85  if ( _type.empty() )
86  {
87  WAR << "Can't determine type of " << checksum.size() << " byte checksum '" << _checksum << "'" << endl;
88  return;
89  }
90  // else: dubious
91  break;
92  }
93 
94  // dubious: Throw on malformed known types, otherwise log a warning.
95  std::string msg = str::form ( _("Dubious type '%s' for %u byte checksum '%s'"),
96  _type.c_str(), checksum.size(), _checksum.c_str() );
97  if ( _type == md5Type()
98  || _type == shaType()
99  || _type == sha1Type()
100  || _type == sha256Type() )
101  {
102  ZYPP_THROW( CheckSumException( msg ) );
103  }
104  else
105  {
106  WAR << msg << endl;
107  }
108  }
109 
110  CheckSum::CheckSum( const std::string & type_r, std::istream & input_r )
111  {
112  if ( input_r.good() && ! type_r.empty() )
113  {
114  _type = str::toLower( type_r );
115  _checksum = Digest::digest( _type, input_r );
116  if ( ! input_r.eof() || _checksum.empty() )
117  {
118  _type = _checksum = std::string();
119  }
120  }
121  }
122 
123  std::string CheckSum::type() const
124  { return _type; }
125 
126  std::string CheckSum::checksum() const
127  { return _checksum; }
128 
129  bool CheckSum::empty() const
130  { return (checksum().empty() || type().empty()); }
131 
132  std::string CheckSum::asString() const
133  {
134  std::ostringstream str;
135  str << *this;
136  return str.str();
137  }
138 
139  std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
140  {
141  if ( obj.checksum().empty() )
142  {
143  return str << std::string("NoCheckSum");
144  }
145 
146  return str << ( obj.type().empty() ? std::string("UNKNOWN") : obj.type() ) << '-' << obj.checksum();
147  }
148 
149  std::ostream & dumpAsXmlOn( std::ostream & str, const CheckSum & obj )
150  {
151  const std::string & type( obj.type() );
152  const std::string & checksum( obj.checksum() );
153  str << "<checksum";
154  if ( ! type.empty() ) str << " type=\"" << type << "\"";
155  if ( checksum.empty() )
156  str << "/>";
157  else
158  str << ">" << checksum << "</checksum>";
159  return str;
160  }
161 
163  bool operator==( const CheckSum & lhs, const CheckSum & rhs )
164  { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
165 
167  bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
168  { return ! ( lhs == rhs ); }
169 
171 } // namespace zypp
Interface to gettext.
std::string digest()
get hex string representation of the digest
Definition: Digest.cc:174
bool empty() const
Definition: CheckSum.cc:129
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:320
std::ostream & dumpAsXmlOn(std::ostream &str, const CheckSum &obj)
Definition: CheckSum.cc:149
static const std::string & shaType()
Definition: CheckSum.cc:31
String related utilities and Regular expression matching.
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:34
std::string _checksum
Definition: CheckSum.h:110
std::string asString() const
Definition: CheckSum.cc:132
std::string _type
Definition: CheckSum.h:109
static const std::string & sha256Type()
Definition: CheckSum.cc:37
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
#define WAR
Definition: Logger.h:48
#define _(MSG)
Return translated text.
Definition: Gettext.h:21
std::string checksum() const
Definition: CheckSum.cc:126
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition: String.cc:166
std::string type() const
Definition: CheckSum.cc:123
static const std::string & sha1Type()
Definition: CheckSum.cc:34
std::string checksum(const Pathname &file, const std::string &algorithm)
Compute a files checksum.
Definition: PathInfo.cc:981
bool operator!=(const CheckSum &lhs, const CheckSum &rhs)
Definition: CheckSum.cc:167
CheckSum()
Default Ctor: empty checksum.
Definition: CheckSum.h:37
bool operator==(const CheckSum &lhs, const CheckSum &rhs)
Definition: CheckSum.cc:163
static const std::string & md5Type()
Definition: CheckSum.cc:28
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1