mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-24 09:12:28 +02:00
Merge remote-tracking branch 'origin/Base32Encoding' into develop
This commit is contained in:
182
Foundation/src/Base32Decoder.cpp
Normal file
182
Foundation/src/Base32Decoder.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
//
|
||||
// Base32Decoder.cpp
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/src/Base32Decoder.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/Base32Decoder.h"
|
||||
#include "Poco/Base32Encoder.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/Mutex.h"
|
||||
#include <cstring>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
unsigned char Base32DecoderBuf::IN_ENCODING[256];
|
||||
bool Base32DecoderBuf::IN_ENCODING_INIT = false;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
static FastMutex mutex;
|
||||
}
|
||||
|
||||
|
||||
Base32DecoderBuf::Base32DecoderBuf(std::istream& istr):
|
||||
_groupLength(0),
|
||||
_groupIndex(0),
|
||||
_buf(*istr.rdbuf())
|
||||
{
|
||||
FastMutex::ScopedLock lock(mutex);
|
||||
if (!IN_ENCODING_INIT)
|
||||
{
|
||||
for (unsigned i = 0; i < sizeof(IN_ENCODING); i++)
|
||||
{
|
||||
IN_ENCODING[i] = 0xFF;
|
||||
}
|
||||
for (unsigned i = 0; i < sizeof(Base32EncoderBuf::OUT_ENCODING); i++)
|
||||
{
|
||||
IN_ENCODING[Base32EncoderBuf::OUT_ENCODING[i]] = i;
|
||||
}
|
||||
IN_ENCODING[static_cast<unsigned char>('=')] = '\0';
|
||||
IN_ENCODING_INIT = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Base32DecoderBuf::~Base32DecoderBuf()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int Base32DecoderBuf::readFromDevice()
|
||||
{
|
||||
if (_groupIndex < _groupLength)
|
||||
{
|
||||
return _group[_groupIndex++];
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned char buffer[8];
|
||||
std::memset(buffer, '=', sizeof(buffer));
|
||||
int c;
|
||||
|
||||
// per RFC-4648, Section 6, permissible block lengths are:
|
||||
// 2, 4, 5, 7, and 8 bytes. Any other length is malformed.
|
||||
//
|
||||
do {
|
||||
if ((c = readOne()) == -1) return -1;
|
||||
buffer[0] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) throw DataFormatException();
|
||||
buffer[1] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) break;
|
||||
buffer[2] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) throw DataFormatException();
|
||||
buffer[3] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) break;
|
||||
buffer[4] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[4]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) break;
|
||||
buffer[5] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[5]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) throw DataFormatException();
|
||||
buffer[6] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[6]] == 0xFF) throw DataFormatException();
|
||||
if ((c = readOne()) == -1) break;
|
||||
buffer[7] = (unsigned char) c;
|
||||
if (IN_ENCODING[buffer[7]] == 0xFF) throw DataFormatException();
|
||||
} while (false);
|
||||
|
||||
_group[0] = (IN_ENCODING[buffer[0]] << 3) | (IN_ENCODING[buffer[1]] >> 2);
|
||||
_group[1] = ((IN_ENCODING[buffer[1]] & 0x03) << 6) | (IN_ENCODING[buffer[2]] << 1) | (IN_ENCODING[buffer[3]] >> 4);
|
||||
_group[2] = ((IN_ENCODING[buffer[3]] & 0x0F) << 4) | (IN_ENCODING[buffer[4]] >> 1);
|
||||
_group[3] = ((IN_ENCODING[buffer[4]] & 0x01) << 7) | (IN_ENCODING[buffer[5]] << 2) | (IN_ENCODING[buffer[6]] >> 3);
|
||||
_group[4] = ((IN_ENCODING[buffer[6]] & 0x07) << 5) | IN_ENCODING[buffer[7]];
|
||||
|
||||
if (buffer[2] == '=')
|
||||
_groupLength = 1;
|
||||
else if (buffer[4] == '=')
|
||||
_groupLength = 2;
|
||||
else if (buffer[5] == '=')
|
||||
_groupLength = 3;
|
||||
else if (buffer[7] == '=')
|
||||
_groupLength = 4;
|
||||
else
|
||||
_groupLength = 5;
|
||||
_groupIndex = 1;
|
||||
return _group[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int Base32DecoderBuf::readOne()
|
||||
{
|
||||
int ch = _buf.sbumpc();
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
Base32DecoderIOS::Base32DecoderIOS(std::istream& istr): _buf(istr)
|
||||
{
|
||||
poco_ios_init(&_buf);
|
||||
}
|
||||
|
||||
|
||||
Base32DecoderIOS::~Base32DecoderIOS()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Base32DecoderBuf* Base32DecoderIOS::rdbuf()
|
||||
{
|
||||
return &_buf;
|
||||
}
|
||||
|
||||
|
||||
Base32Decoder::Base32Decoder(std::istream& istr): Base32DecoderIOS(istr), std::istream(&_buf)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Base32Decoder::~Base32Decoder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
224
Foundation/src/Base32Encoder.cpp
Normal file
224
Foundation/src/Base32Encoder.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
//
|
||||
// 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', '7',
|
||||
};
|
||||
|
||||
|
||||
Base32EncoderBuf::Base32EncoderBuf(std::ostream& ostr, bool padding):
|
||||
_groupLength(0),
|
||||
_buf(*ostr.rdbuf()),
|
||||
_doPadding(padding)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
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 (_doPadding) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 (_doPadding) {
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
}
|
||||
}
|
||||
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 (_doPadding) {
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
if (_buf.sputc('=') == eof) return eof;
|
||||
}
|
||||
}
|
||||
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 (_doPadding && _buf.sputc('=') == eof) return eof;
|
||||
}
|
||||
_groupLength = 0;
|
||||
return _buf.pubsync();
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr, bool padding):
|
||||
_buf(ostr, padding)
|
||||
{
|
||||
poco_ios_init(&_buf);
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderIOS::~Base32EncoderIOS()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int Base32EncoderIOS::close()
|
||||
{
|
||||
return _buf.close();
|
||||
}
|
||||
|
||||
|
||||
Base32EncoderBuf* Base32EncoderIOS::rdbuf()
|
||||
{
|
||||
return &_buf;
|
||||
}
|
||||
|
||||
|
||||
Base32Encoder::Base32Encoder(std::ostream& ostr, bool padding):
|
||||
Base32EncoderIOS(ostr, padding), std::ostream(&_buf)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Base32Encoder::~Base32Encoder()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
Reference in New Issue
Block a user