diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-06-18 19:57:12 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-06-18 19:57:12 +0000 |
commit | 158d215b33d5b5bf07f73bb7547aae1fb12593b3 (patch) | |
tree | e4a0204370bc295509d1299c603dff13efe00cc3 /src/base64.cpp | |
parent | ededeea60b8dca5320e741c7859b54f32ad75cf7 (diff) | |
download | libbu++-158d215b33d5b5bf07f73bb7547aae1fb12593b3.tar.gz libbu++-158d215b33d5b5bf07f73bb7547aae1fb12593b3.tar.bz2 libbu++-158d215b33d5b5bf07f73bb7547aae1fb12593b3.tar.xz libbu++-158d215b33d5b5bf07f73bb7547aae1fb12593b3.zip |
Added a new enum value to Bu::File, Bu::File::WriteNew which combines Write,
Truncate, and Create, the flags used most commonly when writing a new file.
Also added the Bu::Base64 filter class, it does base64 encoding and decoding,
it may need a couple more interfaces added, but for the most part, it's solid.
Diffstat (limited to '')
-rw-r--r-- | src/base64.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/base64.cpp b/src/base64.cpp new file mode 100644 index 0000000..723788b --- /dev/null +++ b/src/base64.cpp | |||
@@ -0,0 +1,132 @@ | |||
1 | #include "bu/base64.h" | ||
2 | |||
3 | const char Bu::Base64::tblEnc[65] = { | ||
4 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | ||
5 | }; | ||
6 | |||
7 | Bu::Base64::Base64( Bu::Stream &rNext, Mode eMode ) : | ||
8 | Bu::Filter( rNext ), | ||
9 | iBPos( 0 ), | ||
10 | iBuf( 0 ), | ||
11 | iTotalIn( 0 ), | ||
12 | iTotalOut( 0 ), | ||
13 | eMode( eMode ) | ||
14 | { | ||
15 | start(); | ||
16 | |||
17 | memset( tblDec, 0, 80 ); | ||
18 | for( int j = 0; j < 64; j++ ) | ||
19 | { | ||
20 | tblDec[tblEnc[j]-'+'] = j; | ||
21 | // printf("'%c' = %d\n", tblEnc[j], j ); | ||
22 | } | ||
23 | /* | ||
24 | for( int j = 0; j < 64; j++ ) | ||
25 | { | ||
26 | printf("'%c' = '%c' (%d = %d)\n", | ||
27 | tblEnc[j], tblEnc[tblDec[tblEnc[j]-'+']], | ||
28 | j, tblDec[tblEnc[j]-'+'] ); | ||
29 | }*/ | ||
30 | |||
31 | // The following is used to compute the table size for the decoding table. | ||
32 | /* | ||
33 | char low='A', high='A'; | ||
34 | for( int j = 0; j < 64; j++ ) | ||
35 | { | ||
36 | if( tblEnc[j] < low ) | ||
37 | low = tblEnc[j]; | ||
38 | if( tblEnc[j] > high ) | ||
39 | high = tblEnc[j]; | ||
40 | } | ||
41 | |||
42 | printf("'%c' - '%c' (%d - %d) (%d)\n", low, high, low, high, high-low ); | ||
43 | */ | ||
44 | } | ||
45 | |||
46 | Bu::Base64::~Base64() | ||
47 | { | ||
48 | stop(); | ||
49 | } | ||
50 | |||
51 | void Bu::Base64::start() | ||
52 | { | ||
53 | } | ||
54 | |||
55 | size_t Bu::Base64::stop() | ||
56 | { | ||
57 | if( eMode == Write ) | ||
58 | { | ||
59 | char outBuf[4]; | ||
60 | int iBUsed = 4-(3-iBPos); | ||
61 | if( iBPos == 0 ) | ||
62 | return iTotalOut; | ||
63 | for( int k = 0; k < 4; k++ ) | ||
64 | { | ||
65 | outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; | ||
66 | } | ||
67 | for( int k = iBUsed; k < 4; k++ ) | ||
68 | { | ||
69 | outBuf[k] = '='; | ||
70 | } | ||
71 | iTotalOut += rNext.write( outBuf, 4 ); | ||
72 | return iTotalOut; | ||
73 | } | ||
74 | else | ||
75 | { | ||
76 | return iTotalIn; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | size_t Bu::Base64::read( void *pBuf, size_t nBytes ) | ||
81 | { | ||
82 | size_t sIn = 0; | ||
83 | char buf[4]; | ||
84 | while( sIn < nBytes ) | ||
85 | { | ||
86 | if( rNext.read( buf, 4 ) == 0 ) | ||
87 | return sIn; | ||
88 | int iChars = 3; | ||
89 | for( int j = 0; j < 4; j++ ) | ||
90 | { | ||
91 | if( buf[j] == '=' ) | ||
92 | iChars--; | ||
93 | else | ||
94 | iBuf |= (tblDec[buf[j]-'+']&0x3f)<<((3-j)*6); | ||
95 | printf("%d: %06X (%02X)\n", j, iBuf, (tblDec[buf[j]-'+']&0x3f) ); | ||
96 | } | ||
97 | for( int j = 0; j < iChars; j++ ) | ||
98 | { | ||
99 | ((unsigned char *)pBuf)[sIn++] = (iBuf>>(8*(2-j)))&0xFF; | ||
100 | } | ||
101 | iBuf = 0; | ||
102 | } | ||
103 | |||
104 | return sIn; | ||
105 | } | ||
106 | |||
107 | size_t Bu::Base64::write( const void *pBuf, size_t nBytes ) | ||
108 | { | ||
109 | size_t sOut = 0; | ||
110 | char outBuf[4]; | ||
111 | for( size_t j = 0; j < nBytes; j++ ) | ||
112 | { | ||
113 | iBuf |= (((uint8_t *)pBuf)[j])<<((2-iBPos++)*8); | ||
114 | if( iBPos == 3 ) | ||
115 | { | ||
116 | for( int k = 0; k < 4; k++ ) | ||
117 | { | ||
118 | outBuf[3-k] = tblEnc[(iBuf>>(6*k))&0x3f]; | ||
119 | } | ||
120 | sOut += rNext.write( outBuf, 4 ); | ||
121 | iBPos = iBuf = 0; | ||
122 | } | ||
123 | } | ||
124 | iTotalOut += sOut; | ||
125 | return sOut; | ||
126 | } | ||
127 | |||
128 | bool Bu::Base64::isOpen() | ||
129 | { | ||
130 | return true; | ||
131 | } | ||
132 | |||