Commit 06fb83af authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

more boost stuff, necessary for base64 encoder and decoder


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@726 e5f2f494-b856-4b98-b285-d166d9295462
parent 40c690d9
#ifndef BOOST_ARCHIVE_ITERATORS_BASE64_EXCEPTION_HPP
#define BOOST_ARCHIVE_ITERATORS_BASE64_EXCEPTION_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// base64_exception.hpp:
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/config.hpp>
#ifndef BOOST_NO_EXCEPTIONS
#include <exception>
#include <cassert>
namespace boost {
namespace archive {
namespace iterators {
//////////////////////////////////////////////////////////////////////
// exceptions thrown by base64s
//
class base64_exception : public std::exception
{
public:
typedef enum {
invalid_code, // attempt to encode a value > 6 bits
invalid_character, // decode a value not in base64 char set
other_exception
} exception_code;
exception_code code;
base64_exception(exception_code c = other_exception) : code(c)
{}
virtual const char *what( ) const throw( )
{
const char *msg = "unknown exception code";
switch(code){
case invalid_code:
msg = "attempt to encode a value > 6 bits";
break;
case invalid_character:
msg = "attempt to decode a value not in base64 char set";
break;
default:
assert(false);
break;
}
return msg;
}
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif //BOOST_NO_EXCEPTIONS
#endif //BOOST_ARCHIVE_ITERATORS_ARCHIVE_EXCEPTION_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP
#define BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// base64_from_binary.hpp
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <cassert>
#include <cstddef> // size_t
#include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
using ::size_t;
} // namespace std
#endif
#include <boost/serialization/pfto.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/archive/iterators/dataflow_exception.hpp>
namespace boost {
namespace archive {
namespace iterators {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// convert binary integers to base64 characters
namespace detail {
template<class CharType>
struct from_6_bit {
typedef CharType result_type;
CharType operator()(CharType t) const{
const char * lookup_table =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";
assert(t < 64);
return lookup_table[static_cast<size_t>(t)];
}
};
} // namespace detail
// note: what we would like to do is
// template<class Base, class CharType = BOOST_DEDUCED_TYPENAME Base::value_type>
// typedef transform_iterator<
// from_6_bit<CharType>,
// transform_width<Base, 6, sizeof(Base::value_type) * 8, CharType>
// > base64_from_binary;
// but C++ won't accept this. Rather than using a "type generator" and
// using a different syntax, make a derivation which should be equivalent.
//
// Another issue addressed here is that the transform_iterator doesn't have
// a templated constructor. This makes it incompatible with the dataflow
// ideal. This is also addressed here.
//template<class Base, class CharType = BOOST_DEDUCED_TYPENAME Base::value_type>
template<
class Base,
class CharType = BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type
>
class base64_from_binary :
public transform_iterator<
detail::from_6_bit<CharType>,
Base
>
{
friend class boost::iterator_core_access;
typedef transform_iterator<
BOOST_DEDUCED_TYPENAME detail::from_6_bit<CharType>,
Base
> super_t;
public:
// make composible buy using templated constructor
template<class T>
base64_from_binary(BOOST_PFTO_WRAPPER(T) start) :
super_t(
Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))),
detail::from_6_bit<CharType>()
)
{}
// intel 7.1 doesn't like default copy constructor
base64_from_binary(const base64_from_binary & rhs) :
super_t(
Base(rhs.base_reference()),
detail::from_6_bit<CharType>()
)
{}
// base64_from_binary(){};
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif // BOOST_ARCHIVE_ITERATORS_BASE64_FROM_BINARY_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
#define BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// binary_from_base64.hpp
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <cassert>
#include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
#include <boost/serialization/throw_exception.hpp>
#include <boost/serialization/pfto.hpp>
#include <boost/static_assert.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/archive/iterators/dataflow_exception.hpp>
namespace boost {
namespace archive {
namespace iterators {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// convert base64 characters to binary data
namespace detail {
template<class CharType>
struct to_6_bit {
typedef CharType result_type;
CharType operator()(CharType t) const{
const char lookup_table[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1
};
// metrowerks trips this assertion - how come?
#if ! defined(__MWERKS__)
BOOST_STATIC_ASSERT(128 == sizeof(lookup_table));
#endif
signed char value = -1;
if((unsigned)t <= 127)
value = lookup_table[(unsigned)t];
if(-1 == value)
boost::serialization::throw_exception(
dataflow_exception(dataflow_exception::invalid_base64_character)
);
return value;
}
};
} // namespace detail
// note: what we would like to do is
// template<class Base, class CharType = BOOST_DEDUCED_TYPENAME Base::value_type>
// typedef transform_iterator<
// from_6_bit<CharType>,
// transform_width<Base, 6, sizeof(Base::value_type) * 8, CharType>
// > base64_from_binary;
// but C++ won't accept this. Rather than using a "type generator" and
// using a different syntax, make a derivation which should be equivalent.
//
// Another issue addressed here is that the transform_iterator doesn't have
// a templated constructor. This makes it incompatible with the dataflow
// ideal. This is also addressed here.
template<
class Base,
class CharType = BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type
>
class binary_from_base64 : public
transform_iterator<
detail::to_6_bit<CharType>,
Base
>
{
friend class boost::iterator_core_access;
typedef transform_iterator<
detail::to_6_bit<CharType>,
Base
> super_t;
public:
// make composible buy using templated constructor
template<class T>
binary_from_base64(BOOST_PFTO_WRAPPER(T) start) :
super_t(
Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start))),
detail::to_6_bit<CharType>()
)
{}
// intel 7.1 doesn't like default copy constructor
binary_from_base64(const binary_from_base64 & rhs) :
super_t(
Base(rhs.base_reference()),
detail::to_6_bit<CharType>()
)
{}
// binary_from_base64(){};
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif // BOOST_ARCHIVE_ITERATORS_BINARY_FROM_BASE64_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_DATAFLOW_HPP
#define BOOST_ARCHIVE_ITERATORS_DATAFLOW_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// dataflow.hpp
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <cassert>
#include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/int.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/iterator/iterator_traits.hpp>
#include <boost/static_assert.hpp>
namespace boost {
namespace archive {
namespace iterators {
// poor man's tri-state
struct tri_state {
enum state_enum {
is_false = false,
is_true = true,
is_indeterminant
} m_state;
// convert to bool
operator bool (){
assert(is_indeterminant != m_state);
return is_true == m_state ? true : false;
}
// assign from bool
tri_state & operator=(bool rhs) {
m_state = rhs ? is_true : is_false;
return *this;
}
tri_state(bool rhs) :
m_state(rhs ? is_true : is_false)
{}
tri_state(state_enum state) :
m_state(state)
{}
bool operator==(const tri_state & rhs) const {
return m_state == rhs.m_state;
}
bool operator!=(const tri_state & rhs) const {
return m_state != rhs.m_state;
}
};
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// implement functions common to dataflow iterators
template<class Derived>
class dataflow {
bool m_eoi;
protected:
// test for iterator equality
tri_state equal(const Derived & rhs) const {
if(m_eoi && rhs.m_eoi)
return true;
if(m_eoi || rhs.m_eoi)
return false;
return tri_state(tri_state::is_indeterminant);
}
void eoi(bool tf){
m_eoi = tf;
}
bool eoi() const {
return m_eoi;
}
public:
dataflow(bool tf) :
m_eoi(tf)
{}
dataflow() : // used for iterator end
m_eoi(true)
{}
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif // BOOST_ARCHIVE_ITERATORS_DATAFLOW_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_DATAFLOW_EXCEPTION_HPP
#define BOOST_ARCHIVE_ITERATORS_DATAFLOW_EXCEPTION_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// dataflow_exception.hpp:
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/config.hpp>
#ifndef BOOST_NO_EXCEPTIONS
#include <exception>
#endif //BOOST_NO_EXCEPTIONS
#include <cassert>
namespace boost {
namespace archive {
namespace iterators {
//////////////////////////////////////////////////////////////////////
// exceptions thrown by dataflows
//
class dataflow_exception : public std::exception
{
public:
typedef enum {
invalid_6_bitcode,
invalid_base64_character,
invalid_xml_escape_sequence,
comparison_not_permitted,
invalid_conversion,
other_exception
} exception_code;
exception_code code;
dataflow_exception(exception_code c = other_exception) : code(c)
{}
virtual const char *what( ) const throw( )
{
const char *msg = "unknown exception code";
switch(code){
case invalid_6_bitcode:
msg = "attempt to encode a value > 6 bits";
break;
case invalid_base64_character:
msg = "attempt to decode a value not in base64 char set";
break;
case invalid_xml_escape_sequence:
msg = "invalid xml escape_sequence";
break;
case comparison_not_permitted:
msg = "cannot invoke iterator comparison now";
break;
case invalid_conversion:
msg = "invalid multbyte/wide char conversion";
break;
default:
assert(false);
break;
}
return msg;
}
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif //BOOST_ARCHIVE_ITERATORS_DATAFLOW_EXCEPTION_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP
#define BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// escape.hpp
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <cassert>
#include <cstddef> // NULL
#include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
#include <boost/iterator/iterator_adaptor.hpp>
#include <boost/iterator/iterator_traits.hpp>
namespace boost {
namespace archive {
namespace iterators {
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// insert escapes into text
template<class Derived, class Base>
class escape :
public boost::iterator_adaptor<
Derived,
Base,
BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type,
single_pass_traversal_tag,
BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type
>
{
typedef BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type base_value_type;
typedef BOOST_DEDUCED_TYPENAME boost::iterator_reference<Base>::type reference_type;
friend class boost::iterator_core_access;
typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
Derived,
Base,
base_value_type,
single_pass_traversal_tag,
base_value_type
> super_t;
typedef escape<Derived, Base> this_t;
void dereference_impl() {
m_current_value = static_cast<Derived *>(this)->fill(m_bnext, m_bend);
m_full = true;
}
//Access the value referred to
reference_type dereference() const {
if(!m_full)
const_cast<this_t *>(this)->dereference_impl();
return m_current_value;
}
bool equal(const this_t & rhs) const {
if(m_full){
if(! rhs.m_full)
const_cast<this_t *>(& rhs)->dereference_impl();
}
else{
if(rhs.m_full)
const_cast<this_t *>(this)->dereference_impl();
}
if(m_bnext != rhs.m_bnext)
return false;
if(this->base_reference() != rhs.base_reference())
return false;
return true;
}
void increment(){
if(++m_bnext < m_bend){
m_current_value = *m_bnext;
return;
}
++(this->base_reference());
m_bnext = NULL;
m_bend = NULL;
m_full = false;
}
// buffer to handle pending characters
const base_value_type *m_bnext;
const base_value_type *m_bend;
bool m_full;
BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type m_current_value;
public:
escape(Base base) :
super_t(base),
m_bnext(NULL),
m_bend(NULL),
m_full(false)
{
}
};
} // namespace iterators
} // namespace archive
} // namespace boost
#endif // BOOST_ARCHIVE_ITERATORS_ESCAPE_HPP
#ifndef BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP
#define BOOST_ARCHIVE_ITERATORS_HEAD_ITERATOR_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif