diff options
author | Jack Lloyd <[email protected]> | 2017-11-09 16:43:57 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-11-09 16:43:57 -0500 |
commit | b08f26d3658db3213d98932cbc6dd3e6efdc8b85 (patch) | |
tree | c1cfb432aaa1625d6a1cfff7bf7487b75dcd0286 /src/lib/utils | |
parent | 54baf3a3d3ee070e3740859298f92d69b042c9c6 (diff) |
Remove use of transcode
Diffstat (limited to 'src/lib/utils')
-rw-r--r-- | src/lib/utils/charset.cpp | 58 | ||||
-rw-r--r-- | src/lib/utils/charset.h | 44 |
2 files changed, 60 insertions, 42 deletions
diff --git a/src/lib/utils/charset.cpp b/src/lib/utils/charset.cpp index dadee8f78..ca32c652d 100644 --- a/src/lib/utils/charset.cpp +++ b/src/lib/utils/charset.cpp @@ -92,34 +92,6 @@ std::string ucs4_to_utf8(const uint8_t ucs4[], size_t len) return s; } -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(size_t i = 0; i != ucs2.size(); i += 2) - { - const uint8_t c1 = ucs2[i]; - const uint8_t c2 = ucs2[i+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 */ @@ -133,7 +105,9 @@ std::string utf8_to_latin1(const std::string& utf8) const uint8_t c1 = static_cast<uint8_t>(utf8[position++]); if(c1 <= 0x7F) + { iso8859 += static_cast<char>(c1); + } else if(c1 >= 0xC0 && c1 <= 0xC7) { if(position == utf8.size()) @@ -154,6 +128,34 @@ std::string utf8_to_latin1(const std::string& utf8) return iso8859; } +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(size_t i = 0; i != ucs2.size(); i += 2) + { + const uint8_t c1 = ucs2[i]; + const uint8_t c2 = ucs2[i+1]; + + if(c1 != 0) + throw Decoding_Error("UCS-2 has non-Latin1 characters"); + + latin1 += static_cast<char>(c2); + } + + return latin1; + } + /* * Convert from ISO 8859-1 to UTF-8 */ diff --git a/src/lib/utils/charset.h b/src/lib/utils/charset.h index 3f2ff9912..4913f0a5a 100644 --- a/src/lib/utils/charset.h +++ b/src/lib/utils/charset.h @@ -14,16 +14,6 @@ namespace Botan { /** -* The different charsets (nominally) supported by Botan. -*/ -enum Character_Set { - LOCAL_CHARSET, - UCS2_CHARSET, - UTF8_CHARSET, - LATIN1_CHARSET -}; - -/** * Convert a sequence of UCS-2 (big endian) characters to a UTF-8 string * This is used for ASN.1 BMPString type * @param ucs2 the sequence of UCS-2 characters @@ -39,15 +29,41 @@ std::string BOTAN_UNSTABLE_API ucs2_to_utf8(const uint8_t ucs2[], size_t len); */ std::string BOTAN_UNSTABLE_API ucs4_to_utf8(const uint8_t ucs4[], size_t len); +/** +* Convert a UTF-8 string to Latin-1 +* If a character outside the Latin-1 range is encountered, an exception is thrown. +*/ +std::string BOTAN_UNSTABLE_API utf8_to_latin1(const std::string& utf8); + +/** +* The different charsets (nominally) supported by Botan. +*/ +enum Character_Set { + LOCAL_CHARSET, + UCS2_CHARSET, + UTF8_CHARSET, + LATIN1_CHARSET +}; + namespace Charset { /* -* Character Set Handling +* Character set conversion - avoid this. +* For specific conversions, use the functions above like +* ucs2_to_utf8 and utf8_to_latin1 +* +* If you need something more complex than that, use a real library +* such as iconv, Boost.Locale, or ICU */ -std::string BOTAN_PUBLIC_API(2,0) transcode(const std::string& str, - Character_Set to, - Character_Set from); +std::string BOTAN_PUBLIC_API(2,0) + BOTAN_DEPRECATED("Avoid. See comment in header.") + transcode(const std::string& str, + Character_Set to, + Character_Set from); +/* +* Simple character classifier functions +*/ bool BOTAN_PUBLIC_API(2,0) is_digit(char c); bool BOTAN_PUBLIC_API(2,0) is_space(char c); bool BOTAN_PUBLIC_API(2,0) caseless_cmp(char x, char y); |