NeoPZ
pzmd5stream.h
Go to the documentation of this file.
1 
6 #ifndef PZMD5STREAM_H
7 #define PZMD5STREAM_H
8 
9 #include "TPZStream.h"
10 
11 #include <stdio.h>
12 
13 #ifdef USING_OPENSSL
14 #include <openssl/md5.h>
15 #endif
16 
22 class TPZMD5Stream : public TPZStream
23 {
24 
25 #ifdef USING_OPENSSL
26 
27  MD5_CTX md5_sig;
28 
29  unsigned char digest[MD5_DIGEST_LENGTH];
30 #endif
31 
32  int last_status; // 1 == SUCCESS
33 
35  int compare_digests (unsigned char* d1, unsigned char* d2, unsigned int dsz)
36  {
37  for (unsigned i=0; i< dsz; i++)
38  {
39  if (d1[i] != d2[i])
40  return -2; // d1 != d2
41  }
42  return 0; // no diferences.
43  }
44 
45 public:
48  {
49  last_status = ResetMD5();
50  }
51 
53  virtual ~TPZMD5Stream()
54  {}
55 
65  int CheckMD5(const std::string &filename) {
66  FILE* fd = fopen(filename.c_str(),"rb" );
67  if (!fd) return 1;
68  int ret = CheckMD5(fd);
69  fclose(fd);
70  return ret;
71  }
72 
82  int CheckMD5(FILE* fh)
83  {
84 #ifdef USING_OPENSSL
85  unsigned char this_digest[MD5_DIGEST_LENGTH];
86  unsigned char file_digest[MD5_DIGEST_LENGTH];
87 
88  if (last_status != 1)
89  return -3;
90 
91  /* Read file digest. */
92  size_t ret = fread(file_digest, sizeof(unsigned char), MD5_DIGEST_LENGTH, fh);
93  if (ret != MD5_DIGEST_LENGTH) {
94  std::cerr << "fread could not read " << MD5_DIGEST_LENGTH << " items. Read only " << ret << " bytes." << std::endl;
95  // Error.
96  return 2;
97  }
98 
99  /* Compute this digest. */
100  if (MD5_Final(this_digest, &md5_sig) != 1) {
101  // Error.
102  return -1;
103  }
104 
105  return compare_digests (this_digest, file_digest, MD5_DIGEST_LENGTH);
106 #else
107  std::cerr << "Enable -DUSING_OPENSSL to use the TPZMD5Stream class." << std::endl;
108 #endif
109  return 1;
110  }
111 
121  int WriteMD5(const std::string &filename) {
122  FILE* fd = fopen(filename.c_str(), "wb");
123  if (fd == NULL) return 1;
124  int ret = WriteMD5(fd);
125  fclose(fd);
126  return ret;
127  }
128 
137  int WriteMD5(FILE* fh)
138  {
139 #ifdef USING_OPENSSL
140  unsigned char digest[MD5_DIGEST_LENGTH];
141 
142  if (last_status != 1)
143  return -3;
144 
145  if (MD5_Final(digest, &md5_sig) != 1) {
146  return -1;
147  }
148 
149  if (fwrite( (const void*) digest, sizeof(unsigned char), MD5_DIGEST_LENGTH, fh) < MD5_DIGEST_LENGTH)
150  return 2;
151 
152  return 0; // Return OK
153 #else
154  std::cerr << "Enable -DUSING_OPENSSL to use the TPZMD5Stream class." << std::endl;
155 #endif
156  return 1;
157  }
158 
163  int ResetMD5()
164  {
165 #ifdef USING_OPENSSL
166  return MD5_Init(&md5_sig);
167 #else
168  std::cerr << "Enable -DUSING_OPENSSL to use the TPZMD5Stream class." << std::endl;
169 #endif
170  return 0;
171  }
172 
174  virtual void Write(const int *p, int size) {
175  Writes<int>(p,size);
176  }
178  virtual void Write(const int64_t *p, int size) {
179  Writes<int64_t>(p,size);
180  }
182  virtual void Write(const unsigned int *p, int size) {
183  Writes<unsigned int>(p,size);
184  }
186  virtual void Write(const float *p, int size) {
187  Writes<float>(p,size);
188  }
190  virtual void Write(const double *p, int size) {
191  Writes<double>(p,size);
192  }
194  virtual void Write(const long double *p, int size) {
195  Writes<long double>(p,size);
196  }
198  virtual void Write(const char *p, int size) {
199  Writes<char>(p,size);
200  }
202  virtual void Write(const std::string *p, int size) {
203  int c;
204  for(c=0; c<size; c++)
205  {
206  int sz = p[c].size();
207  Write(&sz,1);
208  Write(p[c].c_str(),p[c].size());
209  }
210  }
212  virtual void Write(const std::complex <float> *p, int size) {
213  Writes< std::complex <float> >(p,size);
214  }
216  virtual void Write(const std::complex <double> *p, int size) {
217  Writes< std::complex <double> >(p,size);
218  }
220  virtual void Write(const std::complex <long double> *p, int size) {
221  Writes< std::complex <long double> >(p,size);
222  }
223 
224 #ifdef _AUTODIFF
225 
226  virtual void Write(const Fad <float> *p, int size) {
227  Writes< Fad <float> >(p,size);
228  }
229 
230  virtual void Write(const Fad <double> *p, int size) {
231  Writes< Fad <double> >(p,size);
232  }
233 
234  virtual void Write(const Fad <long double> *p, int size) {
235  Writes< Fad <long double> >(p,size);
236  }
237 
238 #endif
239 
241  template<class T>
242  void Writes(const T *p, int size) {
243 #ifdef USING_OPENSSL
244  if (last_status == 1)
245  last_status = MD5_Update(&md5_sig, (const void*) p, sizeof(T)*size);
246 #else
247  std::cerr << "Enable -DUSING_OPENSSL to use the TPZMD5Stream class." << std::endl;
248 #endif
249  }
250 #ifdef _AUTODIFF
251 
252  virtual void Read(Fad <float> *p, int size) {
253  ReadError();
254  }
255 
256  virtual void Read(Fad <double> *p, int size) {
257  ReadError();
258  }
259 
260  virtual void Read(Fad <long double> *p, int size) {
261  ReadError();
262  }
263 
264 #endif
265 
266  virtual void Read(int *p, int size) {
267  ReadError();
268  }
270  virtual void Read(int64_t *p, int size) {
271  ReadError();
272  }
274  virtual void Read(unsigned int *p, int size) {
275  ReadError();
276  }
278  virtual void Read(float *p, int size) {
279  ReadError();
280  }
282  virtual void Read(double *p, int size) {
283  ReadError();
284  }
286  virtual void Read(long double *p, int size) {
287  ReadError();
288  }
290  virtual void Read(char *p, int size) {
291  ReadError();
292  }
294  virtual void Read(std::string *p, int size)
295  {
296  ReadError();
297  }
299  virtual void Read(std::complex <float> *p, int size) {
300  ReadError();
301  }
303  virtual void Read(std::complex <double> *p, int size) {
304  ReadError();
305  }
307  virtual void Read(std::complex <long double> *p, int size) {
308  ReadError();
309  }
310 
311  void ReadError()
312  {
313  std::cerr << "ERROR: Read methods for TPZMD5Stream object invoked." << std::endl;
314  return;
315  }
316 
317 };
318 
319 #endif // PZMD5STREAM_H
int CheckMD5(FILE *fh)
Check Stream MD5 signature against MD5 signature store on file.
Definition: pzmd5stream.h:82
filename
Definition: stats.py:82
void ReadError()
Definition: pzmd5stream.h:311
virtual void Read(std::complex< long double > *p, int size)
Reads size complex-long double from pointer location p.
Definition: pzmd5stream.h:307
int ResetMD5()
Reset the MD5 signature.
Definition: pzmd5stream.h:163
int WriteMD5(FILE *fh)
Write computed MD5 signature to file.
Definition: pzmd5stream.h:137
virtual void Read(float *p, int size)
Reads size floating points from pointer location p.
Definition: pzmd5stream.h:278
virtual void Read(unsigned int *p, int size)
Reads size integers from pointer location p.
Definition: pzmd5stream.h:274
virtual ~TPZMD5Stream()
Default destructor.
Definition: pzmd5stream.h:53
virtual void Read(int *p, int size)
Reads size integers from pointer location p.
Definition: pzmd5stream.h:266
Definition: fad.h:54
virtual void Read(char *p, int size)
Reads size chars from pointer location p.
Definition: pzmd5stream.h:290
virtual void Read(std::string *p, int size)
Reads size strings from pointer location p.
Definition: pzmd5stream.h:294
virtual void Read(double *p, int size)
Reads size floating points from pointer location p.
Definition: pzmd5stream.h:282
int compare_digests(unsigned char *d1, unsigned char *d2, unsigned int dsz)
Return 0 if digests are equal. -2 otherwise.
Definition: pzmd5stream.h:35
virtual void Read(int64_t *p, int size)
Reads size longs from pointer location p.
Definition: pzmd5stream.h:270
virtual void Write(const std::complex< double > *p, int size)
Writes size complex-double at pointer location p.
Definition: pzmd5stream.h:216
void Writes(const T *p, int size)
Writes size objects of the class T at pointer location p.
Definition: pzmd5stream.h:242
virtual void Write(const float *p, int size)
Writes size floating points at pointer location p.
Definition: pzmd5stream.h:186
int CheckMD5(const std::string &filename)
Check Stream MD5 signature against MD5 signature store on file.
Definition: pzmd5stream.h:65
virtual void Read(std::complex< float > *p, int size)
Reads size complex-float from pointer location p.
Definition: pzmd5stream.h:299
virtual void Write(const unsigned int *p, int size)
Writes size integers at pointer location p.
Definition: pzmd5stream.h:182
virtual void Read(long double *p, int size)
Reads size floating points from pointer location p.
Definition: pzmd5stream.h:286
virtual void Write(const std::complex< float > *p, int size)
Writes size complex-float at pointer location p.
Definition: pzmd5stream.h:212
virtual void Write(const std::string *p, int size)
Writes size strings at pointer location p.
Definition: pzmd5stream.h:202
int WriteMD5(const std::string &filename)
Write computed MD5 signature to file.
Definition: pzmd5stream.h:121
virtual void Write(const std::complex< long double > *p, int size)
Writes size complex-long double at pointer location p.
Definition: pzmd5stream.h:220
virtual void Read(std::complex< double > *p, int size)
Reads size complex-double from pointer location p.
Definition: pzmd5stream.h:303
virtual void Write(const int64_t *p, int size)
Writes size longs at pointer location p.
Definition: pzmd5stream.h:178
Contains declaration of the abstract TPZStream class. TPZStream defines the interface for saving and ...
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
TPZMD5Stream()
Default constructor.
Definition: pzmd5stream.h:47
virtual void Write(const char *p, int size)
Writes size chars at pointer location p.
Definition: pzmd5stream.h:198
virtual void Write(const double *p, int size)
Writes size floating points at pointer location p.
Definition: pzmd5stream.h:190
virtual void Write(const int *p, int size)
Writes size integers at pointer location p.
Definition: pzmd5stream.h:174
virtual void Write(const long double *p, int size)
Writes size floating points at pointer location p.
Definition: pzmd5stream.h:194
Implements the interface to write and check MD5 files. Persistency.
Definition: pzmd5stream.h:22