aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/base64.cpp10
-rw-r--r--src/base64.h3
-rw-r--r--src/file.cpp14
-rw-r--r--src/file.h1
-rw-r--r--src/tests/base64.cpp37
5 files changed, 59 insertions, 6 deletions
diff --git a/src/base64.cpp b/src/base64.cpp
index c9a6b59..be5fe8d 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -1,5 +1,7 @@
1#include "bu/base64.h" 1#include "bu/base64.h"
2 2
3namespace Bu { subExceptionDef( Base64Exception ) }
4
3const char Bu::Base64::tblEnc[65] = { 5const char Bu::Base64::tblEnc[65] = {
4 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 6 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
5}; 7};
@@ -106,7 +108,15 @@ size_t Bu::Base64::read( void *pBuf, size_t nBytes )
106 for( int j = 0; j < 4; j++ ) 108 for( int j = 0; j < 4; j++ )
107 { 109 {
108 if( rNext.read( &buf[j], 1 ) == 0 ) 110 if( rNext.read( &buf[j], 1 ) == 0 )
111 {
112 if( rNext.isEOS() )
113 {
114 iChars = 0;
115 bEosIn = true;
116 throw Base64Exception("Premature end of stream detected while decoding Base64 data.");
117 }
109 return sIn; 118 return sIn;
119 }
110 if( buf[j] == ' ' || buf[j] == '\t' || 120 if( buf[j] == ' ' || buf[j] == '\t' ||
111 buf[j] == '\n' || buf[j] == '\r' ) 121 buf[j] == '\n' || buf[j] == '\r' )
112 { 122 {
diff --git a/src/base64.h b/src/base64.h
index c7fb737..a00ed0e 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -2,9 +2,12 @@
2#define BU_BASE64_H 2#define BU_BASE64_H
3 3
4#include "bu/filter.h" 4#include "bu/filter.h"
5#include "bu/exceptionbase.h"
5 6
6namespace Bu 7namespace Bu
7{ 8{
9 subExceptionDecl( Base64Exception );
10
8 /** 11 /**
9 * 12 *
10 *@ingroup Streams 13 *@ingroup Streams
diff --git a/src/file.cpp b/src/file.cpp
index b22461a..0b9bff2 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -16,7 +16,8 @@
16namespace Bu { subExceptionDef( FileException ) } 16namespace Bu { subExceptionDef( FileException ) }
17 17
18Bu::File::File( const Bu::FString &sName, int iFlags ) : 18Bu::File::File( const Bu::FString &sName, int iFlags ) :
19 fd( -1 ) 19 fd( -1 ),
20 bEos( true )
20{ 21{
21 fd = ::open( sName.getStr(), getPosixFlags( iFlags ), 0666 ); 22 fd = ::open( sName.getStr(), getPosixFlags( iFlags ), 0666 );
22 if( fd < 0 ) 23 if( fd < 0 )
@@ -24,11 +25,13 @@ Bu::File::File( const Bu::FString &sName, int iFlags ) :
24 throw Bu::FileException( errno, "%s: %s", 25 throw Bu::FileException( errno, "%s: %s",
25 strerror(errno), sName.getStr() ); 26 strerror(errno), sName.getStr() );
26 } 27 }
28 bEos = false;
27} 29}
28 30
29Bu::File::File( int fd ) : 31Bu::File::File( int fd ) :
30 fd( fd ) 32 fd( fd )
31{ 33{
34 bEos = false;
32} 35}
33 36
34Bu::File::~File() 37Bu::File::~File()
@@ -46,6 +49,7 @@ void Bu::File::close()
46 strerror(errno) ); 49 strerror(errno) );
47 } 50 }
48 fd = -1; 51 fd = -1;
52 bEos = true;
49 } 53 }
50} 54}
51 55
@@ -55,7 +59,11 @@ size_t Bu::File::read( void *pBuf, size_t nBytes )
55 throw FileException("File not open."); 59 throw FileException("File not open.");
56 60
57 ssize_t iRead = ::read( fd, pBuf, nBytes ); 61 ssize_t iRead = ::read( fd, pBuf, nBytes );
58 if( iRead < 0 ) 62 if( iRead == 0 )
63 bEos = true;
64 else if( iRead == -1 && errno == EAGAIN )
65 return 0;
66 else if( iRead < 0 )
59 throw FileException( errno, "%s", strerror( errno ) ); 67 throw FileException( errno, "%s", strerror( errno ) );
60 return iRead; 68 return iRead;
61} 69}
@@ -105,7 +113,7 @@ void Bu::File::setPosEnd( long pos )
105 113
106bool Bu::File::isEOS() 114bool Bu::File::isEOS()
107{ 115{
108 return false; 116 return bEos;
109} 117}
110 118
111bool Bu::File::canRead() 119bool Bu::File::canRead()
diff --git a/src/file.h b/src/file.h
index 61addae..6f28e7b 100644
--- a/src/file.h
+++ b/src/file.h
@@ -99,6 +99,7 @@ namespace Bu
99 99
100 private: 100 private:
101 int fd; 101 int fd;
102 bool bEos;
102 }; 103 };
103} 104}
104 105
diff --git a/src/tests/base64.cpp b/src/tests/base64.cpp
index 3122d5c..5e36448 100644
--- a/src/tests/base64.cpp
+++ b/src/tests/base64.cpp
@@ -1,4 +1,5 @@
1#include "bu/file.h" 1#include "bu/file.h"
2#include "bu/membuf.h"
2#include "bu/base64.h" 3#include "bu/base64.h"
3 4
4int main( int argc, char *argv[] ) 5int main( int argc, char *argv[] )
@@ -31,12 +32,42 @@ int main( int argc, char *argv[] )
31 Bu::File fOut( argv[1], Bu::File::WriteNew ); 32 Bu::File fOut( argv[1], Bu::File::WriteNew );
32 Bu::Base64 bIn( fIn ); 33 Bu::Base64 bIn( fIn );
33 34
34 char buf[16]; 35 char buf[1024];
35 for(;;) 36 for(;;)
36 { 37 {
37 int iRead = bIn.read( buf, 16 ); 38 int iRead = bIn.read( buf, 1024 );
39 printf("Read %d bytes.\n", iRead );
38 fOut.write( buf, iRead ); 40 fOut.write( buf, iRead );
39 if( iRead < 16 ) 41 if( iRead == 0 )
42 break;
43 }
44 }
45 else if( argv[0][0] == 'D' )
46 {
47 argv++;
48 Bu::MemBuf mIn;
49 {
50 Bu::File fIn( argv[0], Bu::File::Read );
51 char buf[1024];
52 for(;;)
53 {
54 int iRead = fIn.read( buf, 1024 );
55 mIn.write( buf, iRead );
56 if( iRead < 1024 )
57 break;
58 }
59 mIn.setPos( 0 );
60 }
61 Bu::File fOut( argv[1], Bu::File::WriteNew );
62 Bu::Base64 bIn( mIn );
63
64 char buf[1024];
65 for(;;)
66 {
67 int iRead = bIn.read( buf, 1024 );
68 printf("Read %d bytes.\n", iRead );
69 fOut.write( buf, iRead );
70 if( iRead == 0 )
40 break; 71 break;
41 } 72 }
42 } 73 }