]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/strutils.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / misc / strutils.cxx
index 47f0b4ef82132ee7b5b82836e3727429960498b0..86418b4c1daf665a78734e16644c1ca6fb419368 100644 (file)
 #include <ctype.h>
 #include <cstring>
 #include <sstream>
+#include <algorithm>
 
 #include "strutils.hxx"
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/package/md5.h>
 
 using std::string;
 using std::vector;
@@ -35,6 +37,43 @@ using std::stringstream;
 namespace simgear {
     namespace strutils {
 
+       /*
+        * utf8ToLatin1() convert utf8 to latin, useful for accent character (i.e éâàîè...)
+        */
+       template <typename Iterator> size_t get_length (Iterator p) {
+               unsigned char c = static_cast<unsigned char> (*p);
+               if (c < 0x80) return 1;
+               else if (!(c & 0x20)) return 2;
+               else if (!(c & 0x10)) return 3;
+               else if (!(c & 0x08)) return 4;
+               else if (!(c & 0x04)) return 5;
+               else return 6;
+       }
+
+       typedef unsigned int value_type;
+       template <typename Iterator> value_type get_value (Iterator p) {
+               size_t len = get_length (p);
+               if (len == 1) return *p;
+               value_type res = static_cast<unsigned char> ( *p & (0xff >> (len + 1))) << ((len - 1) * 6 );
+               for (--len; len; --len) {
+                       value_type next_byte = static_cast<unsigned char> (*(++p)) - 0x80;
+                       if (next_byte & 0xC0) return 0x00ffffff; // invalid UTF-8
+                       res |= next_byte << ((len - 1) * 6);
+                       }
+               return res;
+       }
+
+       string utf8ToLatin1( string& s_utf8 ) {
+               string s_latin1;
+               for (string::iterator p = s_utf8.begin(); p != s_utf8.end(); ++p) {
+                       value_type value = get_value<string::iterator&>(p);
+                       if (value > 0x10ffff) return s_utf8; // invalid UTF-8: guess that the input was already Latin-1
+                       if (value > 0xff) SG_LOG(SG_IO, SG_WARN, "utf8ToLatin1: wrong char value: " << value);
+                       s_latin1 += static_cast<char>(value);
+               }
+               return s_latin1;
+       }
+
        /**
         * 
         */
@@ -305,26 +344,51 @@ namespace simgear {
       return rslt;
     }
 
+    string lowercase(const string &s) {
+      string rslt(s);
+      for(string::iterator p = rslt.begin(); p != rslt.end(); p++){
+        *p = tolower(*p);
+      }
+      return rslt;
+    }
+
+    void lowercase(string &s) {
+      for(string::iterator p = s.begin(); p != s.end(); p++){
+        *p = tolower(*p);
+      }
+    }
+    
+#if defined(SG_WINDOWS)
+
+#include <windows.h>
+    
+static WCharVec convertMultiByteToWString(DWORD encoding, const std::string& a)
+{
+    WCharVec result;
+    DWORD flags = 0;
+    int requiredWideChars = MultiByteToWideChar(encoding, flags, 
+                        a.c_str(), a.size(),
+                        NULL, 0);
+    result.resize(requiredWideChars);
+    MultiByteToWideChar(encoding, flags, a.c_str(), a.size(),
+                        result.data(), result.size());
+    return result;
+}
+
+WCharVec convertUtf8ToWString(const std::string& a)
+{
+    return convertMultiByteToWString(CP_UTF8, a);
+}
 
-#ifdef SG_WINDOWS
-    #include <windows.h>
 #endif
-        
+
 std::string convertWindowsLocal8BitToUtf8(const std::string& a)
 {
 #ifdef SG_WINDOWS
     DWORD flags = 0;
-    std::vector<wchar_t> wideString;
-
-    // call to query transform size
-    int requiredWideChars = MultiByteToWideChar(CP_ACP, flags, a.c_str(), a.size(),
-                        NULL, 0);
-    // allocate storage and call for real
-    wideString.resize(requiredWideChars);
-    MultiByteToWideChar(CP_ACP, flags, a.c_str(), a.size(),
-                        wideString.data(), wideString.size());
-    
-    // now convert back down to UTF-8
+    WCharVec wideString = convertMultiByteToWString(CP_ACP, a);
+   
+    // convert down to UTF-8
     std::vector<char> result;
     int requiredUTF8Chars = WideCharToMultiByte(CP_UTF8, flags,
                                                 wideString.data(), wideString.size(),
@@ -339,12 +403,55 @@ std::string convertWindowsLocal8BitToUtf8(const std::string& a)
 #endif
 }
 
+//------------------------------------------------------------------------------
+std::string md5(const unsigned char* data, size_t num)
+{
+  SG_MD5_CTX md5_ctx;
+  SG_MD5Init(&md5_ctx);
+  SG_MD5Update(&md5_ctx, data, num);
+
+  unsigned char digest[MD5_DIGEST_LENGTH];
+  SG_MD5Final(digest, &md5_ctx);
+
+  return encodeHex(digest, MD5_DIGEST_LENGTH);
+}
+
+//------------------------------------------------------------------------------
+std::string md5(const char* data, size_t num)
+{
+  return md5(reinterpret_cast<const unsigned char*>(data), num);
+}
+
+//------------------------------------------------------------------------------
+std::string md5(const std::string& str)
+{
+  return md5(reinterpret_cast<const unsigned char*>(str.c_str()), str.size());
+}
+
+//------------------------------------------------------------------------------
 static const std::string base64_chars =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 "abcdefghijklmnopqrstuvwxyz"
 "0123456789+/";
 
-
+static const unsigned char base64_decode_map[128] =
+{
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
+    127, 127, 127,  62, 127, 127, 127,  63,  52,  53,
+    54,  55,  56,  57,  58,  59,  60,  61, 127, 127,
+    127,  64, 127, 127, 127,   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, 127, 127, 127, 127, 127, 127,  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, 127, 127, 127, 127, 127
+};
+        
 static inline bool is_base64(unsigned char c) {
   return (isalnum(c) || (c == '+') || (c == '/'));
 }
@@ -353,14 +460,13 @@ static bool is_whitespace(unsigned char c) {
     return ((c == ' ') || (c == '\r') || (c == '\n'));
 }
 
-std::string decodeBase64(const std::string& encoded_string)
+void decodeBase64(const std::string& encoded_string, std::vector<unsigned char>& ret)
 {
   int in_len = encoded_string.size();
   int i = 0;
   int j = 0;
   int in_ = 0;
   unsigned char char_array_4[4], char_array_3[3];
-  std::string ret;
   
   while (in_len-- && ( encoded_string[in_] != '=')) {
     if (is_whitespace( encoded_string[in_])) {
@@ -375,14 +481,14 @@ std::string decodeBase64(const std::string& encoded_string)
     char_array_4[i++] = encoded_string[in_]; in_++;
     if (i ==4) {
       for (i = 0; i <4; i++)
-        char_array_4[i] = base64_chars.find(char_array_4[i]);
+        char_array_4[i] = base64_decode_map[char_array_4[i]];
       
       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
       
       for (i = 0; (i < 3); i++)
-        ret += char_array_3[i];
+        ret.push_back(char_array_3[i]);
       i = 0;
     }
   }
@@ -392,40 +498,34 @@ std::string decodeBase64(const std::string& encoded_string)
       char_array_4[j] = 0;
     
     for (j = 0; j <4; j++)
-      char_array_4[j] = base64_chars.find(char_array_4[j]);
+      char_array_4[j] = base64_decode_map[char_array_4[j]];
     
     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
     
-    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
+    for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
   }
-  
-  return ret;
 }  
 
+//------------------------------------------------------------------------------
 const char hexChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
 std::string encodeHex(const std::string& bytes)
 {
-  std::string hex;
-  size_t count = bytes.size();
-  for (unsigned int i=0; i<count;++i) {
-      unsigned char c = bytes[i];
-      hex.push_back(hexChar[c >> 4]);
-      hex.push_back(hexChar[c & 0x0f]);
-  }
-  
-  return hex;
+  return encodeHex(
+    reinterpret_cast<const unsigned char*>(bytes.c_str()),
+    bytes.size()
+  );
 }
 
 std::string encodeHex(const unsigned char* rawBytes, unsigned int length)
 {
-  std::string hex;
+  std::string hex(length * 2, '\0');
   for (unsigned int i=0; i<length;++i) {
       unsigned char c = *rawBytes++;
-      hex.push_back(hexChar[c >> 4]);
-      hex.push_back(hexChar[c & 0x0f]);
+      hex[i * 2] = hexChar[c >> 4];
+      hex[i * 2 + 1] = hexChar[c & 0x0f];
   }
   
   return hex;