mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 02:18:04 +01:00
Base32 Encoder/Decoder
Base 32 Encoder/Decoder
This commit is contained in:
223
Foundation/src/Base32Encoder.cpp
Normal file
223
Foundation/src/Base32Encoder.cpp
Normal file
@@ -0,0 +1,223 @@
|
||||
//
|
||||
// Base32Encoder.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/src/Base32Encoder.cpp#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Streams
|
||||
// Module: Base32
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Poco/Base32Encoder.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
const unsigned char Base32EncoderBuf::OUT_ENCODING[32] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', '2', '3', '4', '5', '6', '8',
|
||||
};
|
||||
|
||||
|
||||
Base32EncoderBuf::Base32EncoderBuf(std::ostream& ostr):
|
||||
_groupLength(0),
|
||||
_buf(*ostr.rdbuf())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderBuf::~Base32EncoderBuf()
|
||||
{
|
||||
try
|
||||
{
|
||||
close();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int Base32EncoderBuf::writeToDevice(char c)
|
||||
{
|
||||
static const int eof = std::char_traits<char>::eof();
|
||||
|
||||
_group[_groupLength++] = (unsigned char) c;
|
||||
if (_groupLength == 5)
|
||||
{
|
||||
unsigned char idx;
|
||||
idx = _group[0] >> 3;
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[0] & 0x07) << 2) | (_group[1] >> 6);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x3E) >> 1);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x01) << 4) | (_group[2] >> 4);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[2] & 0x0F) << 1) | (_group[3] >> 7);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[3] & 0x7C) >> 2);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[3] & 0x03) << 3) | (_group[4] >> 5);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = (_group[4] & 0x1F);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
_groupLength = 0;
|
||||
}
|
||||
return charToInt(c);
|
||||
}
|
||||
|
||||
|
||||
int Base32EncoderBuf::close()
|
||||
{
|
||||
static const int eof = std::char_traits<char>::eof();
|
||||
|
||||
if (sync() == eof) return eof;
|
||||
if (_groupLength == 1)
|
||||
{
|
||||
_group[1] = 0;
|
||||
unsigned char idx;
|
||||
idx = _group[0] >> 3;
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[0] & 0x07) << 2);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
#if 0
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
#endif
|
||||
}
|
||||
else if (_groupLength == 2)
|
||||
{
|
||||
_group[2] = 0;
|
||||
unsigned char idx;
|
||||
idx = _group[0] >> 3;
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[0] & 0x07) << 2) | (_group[1] >> 6);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x3E) >> 1);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x01) << 4);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
#if 0
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
#endif
|
||||
}
|
||||
else if (_groupLength == 3)
|
||||
{
|
||||
_group[3] = 0;
|
||||
unsigned char idx;
|
||||
idx = _group[0] >> 3;
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[0] & 0x07) << 2) | (_group[1] >> 6);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x3E) >> 1);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x01) << 4) | (_group[2] >> 4);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[2] & 0x0F) << 1);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
#if 0
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
#endif
|
||||
}
|
||||
else if (_groupLength == 4)
|
||||
{
|
||||
_group[4] = 0;
|
||||
unsigned char idx;
|
||||
idx = _group[0] >> 3;
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[0] & 0x07) << 2) | (_group[1] >> 6);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x3E) >> 1);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[1] & 0x01) << 4) | (_group[2] >> 4);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[2] & 0x0F) << 1) | (_group[3] >> 7);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[3] & 0x7C) >> 2);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
idx = ((_group[3] & 0x03) << 3);
|
||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
||||
#if 0
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
#endif
|
||||
}
|
||||
_groupLength = 0;
|
||||
return _buf.pubsync();
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr): _buf(ostr)
|
||||
{
|
||||
poco_ios_init(&_buf);
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderIOS::~Base32EncoderIOS()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int Base32EncoderIOS::close()
|
||||
{
|
||||
return _buf.close();
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderBuf* Base32EncoderIOS::rdbuf()
|
||||
{
|
||||
return &_buf;
|
||||
}
|
||||
|
||||
|
||||
Base32Encoder::Base32Encoder(std::ostream& ostr): Base32EncoderIOS(ostr), std::ostream(&_buf)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Base32Encoder::~Base32Encoder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
Reference in New Issue
Block a user