Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
base64.h
Go to the documentation of this file.
1 /*! \file xstream/base64.h
2  *
3  * \brief C++ streambuf interface to encode/decode base64 data
4  */
5 
6 #ifndef __XSTREAM_BASE64_H
7 #define __XSTREAM_BASE64_H
8 
9 #include <xstream/config.h>
10 
11 #include <xstream/common.h>
12 #include <streambuf>
13 
14 namespace xstream{
15 /*!
16  * \brief base64 encoding/decoding objects
17  */
18 namespace base64{
19 
20 
21 /*!
22  * \brief Base64 encode stream class
23  *
24  * encodes data to base64 and writes it to another streambuf
25  *
26  * \todo no \c xsputn support yet and \c flush doesn't flush partial data,
27  * for example suppose you wrote 2 bytes of a 3 byte chunk, it's possible to know
28  * the first 2 characters of the encoded data, but these are not flushed
29  *
30  */
32 {
33  private:
34 
35  std::streambuf* _sb;
36  char delim;
37  unsigned int delim_w;
38  unsigned int col;
39 
40  char buf[3]; /*!< buffer to store non encoded data */
41 
42  /*!
43  * \brief flush as much data as possible (overloaded from streambuf)
44  *
45  * */
46  int sync();
47 
48  /*!
49  * \brief write a character that surpasses buffer end (overloaded from streambuf)
50  *
51  */
52  int overflow(int c);
53 
54 #if 0
55  //XXX implement later
56  /*!
57  * \brief write an entire buffer (overloaded from streambuf)
58  *
59  */
60  std::streamsize xsputn(const char *buffer, std::streamsize n);
61 #endif
62 
63  /*!
64  * \brief reset input buffer
65  *
66  */
67  void reset();
68 
69  /*!
70  * \brief Takes care of inserting delimiters every \c delim_w characters
71  */
72 
73  int write(const char* buf, size_t len);
74 
75  public:
76  /*!
77  * \brief construct using a streambuf
78  *
79  * \param sb streambuf where to write the encoded data
80  * \param width width in bytes of lines (=0 for unlimited lines)
81  * \param delimiter char to delimit the lines
82  *
83  * the same parameters need to be used when decoding or it will throw an exception indicating invalid data
84  * \c width=76 and \c delim=newline are default because they are the values in the \c RFC
85  */
86  ostreambuf(std::streambuf* sb, unsigned int width=76, char delimiter='\n');
87 
88  /*!
89  * \brief closes the base64 stream
90  *
91  */
92  ~ostreambuf();
93 
94 };
95 
96 /*!
97  * \brief Base64 decode stream class
98  *
99  * decodes data encoded in base64 and makes it available for reading.
100  * Only 4 bytes buffered at a time, and this is not configurable
101  *
102  * \todo xsgetn support
103  *
104  */
105 
106 class istreambuf: public std::streambuf{
107  private:
108  std::streambuf* _sb; /*!< stream to read from */
109  bool end; /*!<signals if stream has reached the end */
110  char delim;
111  unsigned int delim_w;
112  unsigned int col;
113  char buf[3]; /*!< buffer to store decoded data */
114 
115  /*!
116  * \brief requests that input buffer be reloaded (overloaded from streambuf)
117  */
118  int underflow();
119 
120 #if 0 //implemente later
121  /*!
122  * \brief reads \c n characters to \c buffer (overloaded from streambuf)
123  *
124  */
125 
126  std::streamsize xsgetn(char *buffer, std::streamsize n);
127 #endif
128 
129  public:
130  /*!
131  * \brief construct using a streambuf
132  *
133  * \param sb streambuf where to read the encoded data from
134  * \param width width in bytes of lines (=0 for unlimited lines)
135  * \param delimiter char to delimit the lines
136  *
137  * the parameters need to be the same specified at encoding or it will throw an exception indicating invalid data
138  * \c width=76 and \c delim=newline are default because they are the values in the \c RFC
139  *
140  * if data is not delimited exactly at \c width characters with a newline, decoding will throw an exception.
141  * RFC specifies that whitespace should be ignored but this library is very strict when decoding, interpreting spurious whitespace as an error.
142  *
143  */
144  istreambuf(std::streambuf* sb, unsigned int width=76, char delimiter='\n' );
145 
146  /*!
147  * \brief closes the base64 stream
148  *
149  */
150  ~istreambuf();
151 };
152 
153 /*!
154  * \example b64_encode.cpp
155  *
156  * shows how to use the xstream::base64::ostreambuf to encode an ordinary ostream into base64
157  *
158  * it can encode from standard input to standard output or from file to file
159  *
160  */
161 
162 /*!
163  * \example b64_decode.cpp
164  *
165  * shows how to use the xstream::base64::istreambuf to decode base64 encoded data from an istream with encoded data
166  *
167  * it can decode from standard input to standard output or from file to file
168  *
169  */
170 
171 }//namespace base64
172 }//namespace xstream
173 
174 #endif
std::streambuf * _sb
Definition: base64.h:108
int sync()
flush as much data as possible (overloaded from streambuf)
Definition: base64.cpp:28
void reset()
reset input buffer
Definition: base64.cpp:22
ostreambuf(std::streambuf *sb, unsigned int width=76, char delimiter='\n')
construct using a streambuf
Definition: base64.cpp:16
#define c
std::streambuf * _sb
Definition: base64.h:35
~ostreambuf()
closes the base64 stream
Definition: base64.cpp:85
int write(const char *buf, size_t len)
Takes care of inserting delimiters every delim_w characters.
Definition: base64.cpp:48
istreambuf(std::streambuf *sb, unsigned int width=76, char delimiter='\n')
construct using a streambuf
Definition: base64.cpp:186
int underflow()
requests that input buffer be reloaded (overloaded from streambuf)
Definition: base64.cpp:192
unsigned int delim_w
Definition: base64.h:37
int overflow(int c)
write a character that surpasses buffer end (overloaded from streambuf)
Definition: base64.cpp:71
unsigned int delim_w
Definition: base64.h:111
Base64 encode stream class.
Definition: base64.h:31
buffer management
Definition: common.h:46
~istreambuf()
closes the base64 stream
Definition: base64.cpp:255
common objects
Base64 decode stream class.
Definition: base64.h:106