Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
digest.cpp
Go to the documentation of this file.
1 #include <xstream/config.h>
2 #include <xstream/digest.h>
3 #include <streambuf>
4 #include <algorithm>
5 
6 #include "debug.h"
7 
8 
9 namespace xstream{
10 namespace digest{
11 
12  static const int eof = std::streambuf::traits_type::eof();
13 
14  stream::stream(size_t sz)
15  : buf(sz), length(0)
16  {
17  LOG("digest::stream");
18  setp(buf.buf, buf.buf + buf.size);
19  }
20 
22  {
23  LOG("digest::~stream");
24  }
25 
27  {
28  LOG("digest::stream::sync");
29  length += pptr() - pbase();
30  LOG("\tlength = " << length);
31 
33  setp(buf.buf, buf.buf + buf.size);
34  return 0;
35  }
36 
38  {
39  LOG("digest::stream::overflow " << c);
40 
41  if (eof == c) {
42  LOG ("\tEOF");
43  sync();
44  } else {
45  sync();
46  *(pptr()) = static_cast < char >(c);
47  pbump(1);
48  }
49  return c;
50  }
51 
52  std::streamsize stream::xsputn(const char *b, std::streamsize n)
53  {
54  LOG("digest::stream::xsputn(" << b << ", " << n << ")");
55 
56  if (taken() > 0) {
57  sync();
58  }
59  setp((char*)b, (char*)b + n);
60  pbump(n);
61  sync();
62  return n;
63  }
64 
65  block_stream::block_stream(size_t chunk, unsigned int blocks)
66  : stream(chunk * blocks), chunk_size(chunk)
67  {
68  LOG("digest::block_stream (" << chunk << ", " << blocks << ")");
69  }
70 
71  int block_stream::sync()
72  {
73  LOG("digest::block_stream::sync");
74 
75  //number of bytes in the buffer
76  size_t t = taken();
77  char* beg = buf.buf;
78 
79  while (t >= chunk_size) {
80  LOG("\tchunk");
81  setp(beg, beg + chunk_size);
82  pbump(chunk_size);
83  calculate_digest();
84  beg += chunk_size;
85  t -= chunk_size;
86  length += chunk_size;
87  LOG("\tlength = " << length);
88  }
89 
90  //reset buffer
91 
92  if (0 == t) {
93  LOG("\tnothing remains, reset buffer");
94  setp(buf.buf, buf.buf + buf.size);
95  } else if (beg != buf.buf) {
96  LOG("\t" << t << " bytes remain, copying");
97  //t bytes at tail, need to be placed at the beggining and buffer set acordingly
98  std::copy(beg, beg+t, buf.buf);
99  setp(buf.buf, buf.buf + buf.size);
100  pbump(t);
101  }
102 
103  return 0;
104  }
105 
106  int block_stream::overflow(int c)
107  {
108  LOG("digest::block_stream::overflow "<<c);
109 
110  if (eof == c) {
111  LOG("\tEOF");
112  sync();
113  } else {
114  sync();
115  *(pptr()) = static_cast < char >(c);
116  pbump(1);
117  }
118 
119  return c;
120  }
121 
122 
123  std::streamsize block_stream::xsputn(const char *b, std::streamsize n)
124  {
125  LOG("digest::block_stream::xsputn " << n);
126  if (taken() > 0) {
127  sync();
128  }
129  const std::streamsize t = taken();
130  const std::streamsize r = chunk_size - t;
131  if (t > 0) {
132  LOG("\t" << t << " bytes previously in buffer, copying first " << r << " bytes");
133  std::copy(b, b + r, buf.buf + t);
134  sync();
135  }
136  setp((char*)b + r, (char*)b + n);
137  pbump(n - r);
138  sync();
139 
140  return n;
141  }
142 
143 }//namespace digest
144 }//namespace xstream
int overflow(int c)
write a character that surpasses buffer end (overloaded from streambuf)
Definition: digest.cpp:37
int sync()
update digest with as much data as possible (overloaded from streambuf)
Definition: digest.cpp:26
debugging/logging support
stream(size_t len)
default constructor
Definition: digest.cpp:14
size_t size
Definition: common.h:50
#define c
C++ objects to calculate digests of data.
char * buf
Definition: common.h:49
static const int eof
Definition: base64.cpp:14
xstream::buffer buf
Definition: digest.h:59
static const int eof
Definition: digest.cpp:12
virtual void calculate_digest()=0
updates the digest must be inplemented by classes that implement this interface
std::streamsize taken() const
number of characters in the buffer
Definition: common.h:36
#define LOG(s)
Definition: debug.h:30
std::streamsize xsputn(const char *buffer, std::streamsize n)
add an entire buffer to digest calculation (overloaded from streambuf)
Definition: digest.cpp:52