File indexing completed on 2025-08-03 08:20:53
0001 #include <PHmd5Value.h>
0002
0003 #include <PHmd5Utils.h>
0004 #include <iomanip>
0005
0006 PHmd5Value::PHmd5Value()
0007 {
0008 int i;
0009 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0010 theDigest[i] = 0;
0011 isNotValid = 1;
0012
0013 }
0014
0015 PHmd5Value::PHmd5Value(const PHmd5Value &other)
0016 {
0017 if ( other.getMD5(theDigest) ) isNotValid =1;
0018 else isNotValid = 0;
0019 }
0020
0021 PHmd5Value::PHmd5Value(FILE *fp)
0022 {
0023 if ( PHmd5Stream(fp, theDigest, &filesize) ) isNotValid =1;
0024 else isNotValid = 0;
0025 }
0026
0027 PHmd5Value::PHmd5Value(const char *filename)
0028 {
0029 if ( PHmd5File(filename, theDigest, &filesize) ) isNotValid =1;
0030 else isNotValid = 0;
0031 }
0032
0033 PHmd5Value::~PHmd5Value()
0034 {}
0035
0036 int PHmd5Value::Status() const
0037 {
0038 return isNotValid;
0039 }
0040
0041 int PHmd5Value::FileSize() const
0042 {
0043 return filesize;
0044 }
0045
0046
0047 int PHmd5Value::operator== (const PHmd5Value &other) const
0048 {
0049
0050 if ( isNotValid || other.Status() ) return 0;
0051
0052 unsigned char otherDigest[PHMD5DIGESTLENGTH];
0053 other.getMD5 (otherDigest);
0054 int i;
0055 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0056 if (theDigest[i] != otherDigest[i]) return 0;
0057
0058 return 1;
0059 }
0060
0061 int PHmd5Value::getMD5(unsigned char * digest) const
0062 {
0063
0064
0065
0066 if ( isNotValid ) return 1;
0067
0068 int i;
0069 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0070 digest[i] = theDigest[i];
0071
0072 return 0;
0073
0074 }
0075
0076 int PHmd5Value::setMD5(const unsigned char * digest)
0077 {
0078
0079 int i;
0080 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0081 theDigest[i] = digest[i];
0082 isNotValid = 1;
0083 return 0;
0084
0085 }
0086
0087
0088 int PHmd5Value::setFileMD5(const char * filename)
0089 {
0090 if ( PHmd5File(filename, theDigest, &filesize) ) isNotValid =1;
0091 else isNotValid = 0;
0092 return isNotValid;
0093 }
0094
0095 OSTREAM & operator<< ( OSTREAM &os, const PHmd5Value &md )
0096 {
0097 int i;
0098
0099
0100
0101 if ( !md.Status() )
0102 {
0103 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0104 {
0105
0106 os << std::setw(2) << std::hex << md.theDigest[i] << std::dec;
0107 }
0108 }
0109 else
0110 {
0111 for (i = 0; i < PHMD5DIGESTLENGTH; i++)
0112 os << "00";
0113 }
0114
0115 return os;
0116 }