diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/charset.cpp | 103 | ||||
-rw-r--r-- | src/def_char.cpp | 121 | ||||
-rw-r--r-- | src/libstate.cpp | 27 | ||||
-rw-r--r-- | src/modules.cpp | 9 |
4 files changed, 101 insertions, 159 deletions
diff --git a/src/charset.cpp b/src/charset.cpp index 7cda83f0d..e19687163 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -6,20 +6,119 @@ #include <botan/charset.h> #include <botan/hex.h> #include <botan/base64.h> -#include <botan/libstate.h> +#include <botan/parsing.h> #include <cctype> namespace Botan { namespace Charset { +namespace { + +/************************************************* +* Convert from UCS-2 to ISO 8859-1 * +*************************************************/ +std::string ucs2_to_latin1(const std::string& ucs2) + { + if(ucs2.size() % 2 == 1) + throw Decoding_Error("UCS-2 string has an odd number of bytes"); + + std::string latin1; + + for(u32bit j = 0; j != ucs2.size(); j += 2) + { + const byte c1 = ucs2[j]; + const byte c2 = ucs2[j+1]; + + if(c1 != 0) + throw Decoding_Error("UCS-2 has non-Latin1 characters"); + + latin1 += static_cast<char>(c2); + } + + return latin1; + } + +/************************************************* +* Convert from UTF-8 to ISO 8859-1 * +*************************************************/ +std::string utf8_to_latin1(const std::string& utf8) + { + std::string iso8859; + + u32bit position = 0; + while(position != utf8.size()) + { + const byte c1 = static_cast<byte>(utf8[position++]); + + if(c1 <= 0x7F) + iso8859 += static_cast<char>(c1); + else if(c1 >= 0xC0 && c1 <= 0xC7) + { + if(position == utf8.size()) + throw Decoding_Error("UTF-8: sequence truncated"); + + const byte c2 = static_cast<byte>(utf8[position++]); + const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); + + if(iso_char <= 0x7F) + throw Decoding_Error("UTF-8: sequence longer than needed"); + + iso8859 += static_cast<char>(iso_char); + } + else + throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); + } + + return iso8859; + } + +/************************************************* +* Convert from ISO 8859-1 to UTF-8 * +*************************************************/ +std::string latin1_to_utf8(const std::string& iso8859) + { + std::string utf8; + for(u32bit j = 0; j != iso8859.size(); ++j) + { + const byte c = static_cast<byte>(iso8859[j]); + + if(c <= 0x7F) + utf8 += static_cast<char>(c); + else + { + utf8 += static_cast<char>((0xC0 | (c >> 6))); + utf8 += static_cast<char>((0x80 | (c & 0x3F))); + } + } + return utf8; + } + +} + /************************************************* * Perform character set transcoding * *************************************************/ std::string transcode(const std::string& str, Character_Set to, Character_Set from) { - return global_state().transcode(str, to, from); + if(to == LOCAL_CHARSET) + to = LATIN1_CHARSET; + if(from == LOCAL_CHARSET) + from = LATIN1_CHARSET; + + if(to == from) + return str; + + if(from == LATIN1_CHARSET && to == UTF8_CHARSET) + return latin1_to_utf8(str); + if(from == UTF8_CHARSET && to == LATIN1_CHARSET) + return utf8_to_latin1(str); + if(from == UCS2_CHARSET && to == LATIN1_CHARSET) + return ucs2_to_latin1(str); + + throw Invalid_Argument("Unknown transcoding operation from " + + to_string(from) + " to " + to_string(to)); } /************************************************* diff --git a/src/def_char.cpp b/src/def_char.cpp deleted file mode 100644 index 0bbd719f6..000000000 --- a/src/def_char.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/************************************************* -* Default Character Set Handling Source File * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#include <botan/def_char.h> -#include <botan/exceptn.h> -#include <botan/parsing.h> - -namespace Botan { - -namespace { - -/************************************************* -* Convert from UCS-2 to ISO 8859-1 * -*************************************************/ -std::string ucs2_to_latin1(const std::string& ucs2) - { - if(ucs2.size() % 2 == 1) - throw Decoding_Error("UCS-2 string has an odd number of bytes"); - - std::string latin1; - - for(u32bit j = 0; j != ucs2.size(); j += 2) - { - const byte c1 = ucs2[j]; - const byte c2 = ucs2[j+1]; - - if(c1 != 0) - throw Decoding_Error("UCS-2 has non-Latin1 characters"); - - latin1 += static_cast<char>(c2); - } - - return latin1; - } - -/************************************************* -* Convert from UTF-8 to ISO 8859-1 * -*************************************************/ -std::string utf8_to_latin1(const std::string& utf8) - { - std::string iso8859; - - u32bit position = 0; - while(position != utf8.size()) - { - const byte c1 = static_cast<byte>(utf8[position++]); - - if(c1 <= 0x7F) - iso8859 += static_cast<char>(c1); - else if(c1 >= 0xC0 && c1 <= 0xC7) - { - if(position == utf8.size()) - throw Decoding_Error("UTF-8: sequence truncated"); - - const byte c2 = static_cast<byte>(utf8[position++]); - const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); - - if(iso_char <= 0x7F) - throw Decoding_Error("UTF-8: sequence longer than needed"); - - iso8859 += static_cast<char>(iso_char); - } - else - throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); - } - - return iso8859; - } - -/************************************************* -* Convert from ISO 8859-1 to UTF-8 * -*************************************************/ -std::string latin1_to_utf8(const std::string& iso8859) - { - std::string utf8; - for(u32bit j = 0; j != iso8859.size(); ++j) - { - const byte c = static_cast<byte>(iso8859[j]); - - if(c <= 0x7F) - utf8 += static_cast<char>(c); - else - { - utf8 += static_cast<char>((0xC0 | (c >> 6))); - utf8 += static_cast<char>((0x80 | (c & 0x3F))); - } - } - return utf8; - } - -} - -/************************************************* -* Transcode between character sets * -*************************************************/ -std::string Default_Charset_Transcoder::transcode(const std::string& str, - Character_Set to, - Character_Set from) const - { - if(to == LOCAL_CHARSET) - to = LATIN1_CHARSET; - if(from == LOCAL_CHARSET) - from = LATIN1_CHARSET; - - if(to == from) - return str; - - if(from == LATIN1_CHARSET && to == UTF8_CHARSET) - return latin1_to_utf8(str); - if(from == UTF8_CHARSET && to == LATIN1_CHARSET) - return utf8_to_latin1(str); - if(from == UCS2_CHARSET && to == LATIN1_CHARSET) - return ucs2_to_latin1(str); - - throw Invalid_Argument("Unknown transcoding operation from " + - to_string(from) + " to " + to_string(to)); - } - -} diff --git a/src/libstate.cpp b/src/libstate.cpp index 21701cfa8..7d3b633cd 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -216,29 +216,6 @@ void Library_State::add_engine(Engine* engine) } /************************************************* -* Set the character set transcoder object * -*************************************************/ -void Library_State::set_transcoder(class Charset_Transcoder* transcoder) - { - if(this->transcoder) - delete this->transcoder; - this->transcoder = transcoder; - } - -/************************************************* -* Transcode a string from one charset to another * -*************************************************/ -std::string Library_State::transcode(const std::string str, - Character_Set to, - Character_Set from) const - { - if(!transcoder) - throw Invalid_State("Library_State::transcode: No transcoder set"); - - return transcoder->transcode(str, to, from); - } - -/************************************************* * Set the X509 global state class * *************************************************/ void Library_State::set_x509_state(X509_GlobalState* new_x509_state_obj) @@ -293,8 +270,6 @@ void Library_State::initialize(const InitializerOptions& args, cached_default_allocator = 0; x509_state_obj = 0; - transcoder = modules.transcoder(); - std::vector<Allocator*> mod_allocs = modules.allocators(); for(u32bit j = 0; j != mod_allocs.size(); ++j) add_allocator(mod_allocs[j]); @@ -343,7 +318,6 @@ Library_State::Library_State() config_obj = 0; x509_state_obj = 0; - transcoder = 0; rng = 0; cached_default_allocator = 0; } @@ -354,7 +328,6 @@ Library_State::Library_State() Library_State::~Library_State() { delete x509_state_obj; - delete transcoder; delete rng; delete config_obj; diff --git a/src/modules.cpp b/src/modules.cpp index 0076bbef8..e043ec970 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -5,7 +5,6 @@ #include <botan/modules.h> #include <botan/defalloc.h> -#include <botan/def_char.h> #include <botan/eng_def.h> #include <botan/timers.h> @@ -209,14 +208,6 @@ std::vector<Engine*> Builtin_Modules::engines() const } /************************************************* -* Find the best transcoder option * -*************************************************/ -Charset_Transcoder* Builtin_Modules::transcoder() const - { - return new Default_Charset_Transcoder; - } - -/************************************************* * Builtin_Modules Constructor * *************************************************/ Builtin_Modules::Builtin_Modules(const InitializerOptions& args) : |