ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/include/base64.h
Revision: 1.2
Committed: Tue Aug 28 17:08:06 2007 UTC (16 years, 8 months ago) by pippijn
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
State: FILE REMOVED
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# Content
1 /*
2 * $Id: base64.h,v 1.1 2007-07-19 08:24:50 pippijn Exp $
3 */
4 /* base64.h - base64 enc algorithm */
5
6 #ifndef BASE64_H
7 #define BASE64_H
8
9 #include <cstddef>
10
11 namespace base64
12 {
13 class encoder
14 {
15 enum step
16 {
17 step_a, step_b, step_c
18 };
19
20 struct state
21 {
22 enum step step;
23 char result;
24 int stepcnt;
25 };
26
27 static size_t enc_block (char const * const text, size_t inlen, char *code, state *st);
28 static size_t enc_blockend (char *code, state *st);
29
30 static size_t const CHARS_PER_LINE = 72;
31
32 static char enc_value (char val)
33 {
34 static char const * const enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
35 if (val > 63)
36 return '=';
37 return enc[(size_t) val];
38 }
39
40 state m_state;
41 size_t m_bufsize;
42
43 size_t enc_end (char *outtext)
44 {
45 return enc_blockend (outtext, &m_state);
46 }
47
48 public:
49 encoder (size_t bufsize = 4096)
50 : m_bufsize (bufsize)
51 {
52 }
53
54 size_t encode (char val)
55 {
56 return enc_value (val);
57 }
58
59 size_t encode (char const * const code, size_t const inlen, char *outtext)
60 {
61 return enc_block (code, inlen, outtext, &m_state);
62 }
63
64 bool encode (char const * const code, size_t const inlen, char **outtext)
65 {
66 size_t outlen = 1 + (((inlen + 2) / 3) * 4);
67 if (inlen > outlen)
68 {
69 *outtext = NULL;
70 return 0;
71 }
72
73 *outtext = new char[outlen];
74
75 if (*outtext)
76 encode (code, inlen, *outtext);
77
78 return outlen - 1;
79 }
80 };
81
82 class decoder
83 {
84 enum step
85 {
86 step_a, step_b, step_c, step_d
87 };
88
89 struct state
90 {
91 enum step step;
92 char plainchar;
93 };
94
95 static size_t dec_block (char const * const code, size_t const inlen, char *outtext, state *st);
96
97 static size_t dec_value (char val)
98 {
99 static char const dec[] = {
100 62, -1, -1, -1, 63, 52, 53, 54, 55, 56,
101 57, 58, 59, 60, 61, -1, -1, -1, -2, -1,
102 -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
103 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
104 18, 19, 20, 21, 22, 23, 24, 25, -1, -1,
105 -1, -1, -1, -1, 26, 27, 28, 29, 30, 31,
106 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
107 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
108 };
109
110 static char const decsize = sizeof (dec);
111
112 val -= 43;
113 if (val < 0 || val > decsize)
114 return -1;
115 return dec[(size_t) val];
116 }
117
118 state m_state;
119 size_t m_bufsize;
120
121 public:
122 decoder (size_t bufsize = 4096)
123 : m_bufsize (bufsize)
124 {
125 }
126
127 size_t decode (char val)
128 {
129 return dec_value (val);
130 }
131
132 size_t decode (char const * const code, size_t const inlen, char *outtext)
133 {
134 return dec_block (code, inlen, outtext, &m_state);
135 }
136
137 bool decode (char const * const code, size_t const inlen, char **outtext, size_t *outlen)
138 {
139 size_t needlen = 3 * (inlen / 4) + 3;
140 *outtext = new char[needlen];
141 if (!*outtext)
142 return false;
143
144 if (!decode (code, inlen, *outtext))
145 {
146 delete[] *outtext;
147 *outtext = NULL;
148 return false;
149 }
150
151 (*outtext)[needlen] = '\0';
152 if (outlen)
153 *outlen = needlen;
154
155 return true;
156 }
157 };
158 } // namespace base64
159
160 #endif // BASE64_H