Commit 128b9e7b by Francois Gygi

Added print functions for cstdio interface


git-svn-id: http://qboxcode.org/svn/qb/trunk@255 cba15fb0-1239-40c8-b417-11db7ca47a34
parent d00a96d5
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "Base64Transcoder.h" #include "Base64Transcoder.h"
#include <iostream> #include <iostream>
#include <cstdio>
#include <string> #include <string>
#include <cassert> #include <cassert>
using namespace std; using namespace std;
...@@ -16,48 +17,53 @@ Base64Transcoder::Base64Transcoder() ...@@ -16,48 +17,53 @@ Base64Transcoder::Base64Transcoder()
{ {
// initialize encoding/decoding tables // initialize encoding/decoding tables
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++)
{
etable[i] = 'A' + i; etable[i] = 'A' + i;
etable[26 + i] = 'a' + i; etable[26 + i] = 'a' + i;
} }
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++)
{
etable[52 + i] = '0' + i; etable[52 + i] = '0' + i;
} }
etable[62] = '+'; etable[62] = '+';
etable[63] = '/'; etable[63] = '/';
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++)
{
dtable[i] = 0x80; dtable[i] = 0x80;
} }
for (int i = 'A'; i <= 'Z'; i++) { for (int i = 'A'; i <= 'Z'; i++)
{
dtable[i] = 0 + (i - 'A'); dtable[i] = 0 + (i - 'A');
} }
for (int i = 'a'; i <= 'z'; i++) { for (int i = 'a'; i <= 'z'; i++)
{
dtable[i] = 26 + (i - 'a'); dtable[i] = 26 + (i - 'a');
} }
for (int i = '0'; i <= '9'; i++) { for (int i = '0'; i <= '9'; i++)
{
dtable[i] = 52 + (i - '0'); dtable[i] = 52 + (i - '0');
} }
dtable['+'] = 62; dtable['+'] = 62;
dtable['/'] = 63; dtable['/'] = 63;
dtable['='] = 0; dtable['='] = 0;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::encode(int nbytes, const byte* const from, char* const to) int Base64Transcoder::encode(int nbytes, const byte* const from, char* const to)
{ {
const byte* fptr = from; const byte* fptr = from;
char* tptr = to; char* tptr = to;
int n3 = nbytes / 3; // number of groups of three bytes int n3 = nbytes / 3; // number of groups of three bytes
while ( n3-- > 0 ) while ( n3-- > 0 )
{ {
byte ig0 = *fptr++; byte ig0 = *fptr++;
byte ig1 = *fptr++; byte ig1 = *fptr++;
byte ig2 = *fptr++; byte ig2 = *fptr++;
*tptr++ = etable[ig0 >> 2]; *tptr++ = etable[ig0 >> 2];
*tptr++ = etable[((ig0 & 3) << 4) | (ig1 >> 4)]; *tptr++ = etable[((ig0 & 3) << 4) | (ig1 >> 4)];
*tptr++ = etable[((ig1 & 0xF) << 2) | (ig2 >> 6)]; *tptr++ = etable[((ig1 & 0xF) << 2) | (ig2 >> 6)];
...@@ -284,3 +290,45 @@ int Base64Transcoder::print(int nchars, const char* const buf, ostream& o) ...@@ -284,3 +290,45 @@ int Base64Transcoder::print(int nchars, const char* const buf, ostream& o)
o << endl; o << endl;
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(const string buf, FILE* outfile)
{
const char* b = buf.c_str();
const char nlchar = '\n';
int nchars = buf.size();
int nl = nchars / 72;
for ( int i = 0; i < nl; i++ )
{
fwrite(b,sizeof(char),72,outfile);
fwrite(&nlchar,sizeof(char),1,outfile);
b += 72;
}
if ( nchars%72 != 0 )
{
fwrite(b,sizeof(char),nchars%72,outfile);
fwrite(&nlchar,sizeof(char),1,outfile);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(int nchars, const char* const buf, FILE* outfile)
{
const char* b = buf;
const char nlchar = '\n';
int nl = nchars / 72;
for ( int i = 0; i < nl; i++ )
{
fwrite(b,sizeof(char),72,outfile);
fwrite(&nlchar,sizeof(char),1,outfile);
b += 72;
}
if ( nchars%72 != 0 )
{
fwrite(b,sizeof(char),nchars%72,outfile);
fwrite(&nlchar,sizeof(char),1,outfile);
}
return 0;
}
...@@ -9,13 +9,14 @@ ...@@ -9,13 +9,14 @@
#define BASE64TRANSCODER_H #define BASE64TRANSCODER_H
#include <iostream> #include <iostream>
#include <cstdio>
#include <string> #include <string>
using namespace std; using namespace std;
typedef unsigned char byte; typedef unsigned char byte;
class Base64Transcoder class Base64Transcoder
{ {
char etable[256]; // encode table char etable[64]; // encode table
byte dtable[256]; // decode table byte dtable[256]; // decode table
public: public:
...@@ -27,6 +28,8 @@ class Base64Transcoder ...@@ -27,6 +28,8 @@ class Base64Transcoder
void byteswap_int(size_t n, int* const x); void byteswap_int(size_t n, int* const x);
int print(int nchars, const char* const buf, ostream& o); int print(int nchars, const char* const buf, ostream& o);
int print(const string buf, ostream& o); int print(const string buf, ostream& o);
int print(int nchars, const char* const buf, FILE* outfile);
int print(const string buf, FILE* outfile);
// number of chars needed to encode nbytes bytes // number of chars needed to encode nbytes bytes
int nchars(int nbytes) { return 4 * ( ( nbytes + 2 ) / 3 ); } int nchars(int nbytes) { return 4 * ( ( nbytes + 2 ) / 3 ); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment