prep.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "prep.h"
00014
00015 #include <cstdlib>
00016 #include <string>
00017 #include <string.h>
00018
00019 #ifdef _WIN32
00020 # include "../config.h.win"
00021 #elif defined( _WIN32_WCE )
00022 # include "../config.h.win"
00023 #else
00024 # include "config.h"
00025 #endif
00026
00027 #ifdef HAVE_LIBIDN
00028 # include <stringprep.h>
00029 # include <idna.h>
00030 #endif
00031
00032 #define JID_PORTION_SIZE 1023
00033
00034 namespace gloox
00035 {
00036
00037 namespace prep
00038 {
00039
00040 #ifdef HAVE_LIBIDN
00041
00049 static std::string prepare( const std::string& s, const Stringprep_profile* profile )
00050 {
00051 if( s.empty() || s.length() > JID_PORTION_SIZE )
00052 return std::string();
00053
00054 std::string preppedString;
00055 char* p = static_cast<char*>( calloc( JID_PORTION_SIZE, sizeof( char ) ) );
00056 strncpy( p, s.c_str(), s.length() );
00057 if( stringprep( p, JID_PORTION_SIZE, (Stringprep_profile_flags)0, profile ) == STRINGPREP_OK )
00058 preppedString = p;
00059 free( p );
00060 return preppedString;
00061 }
00062 #endif
00063
00064 std::string nodeprep( const std::string& node )
00065 {
00066 #ifdef HAVE_LIBIDN
00067 return prepare( node, stringprep_xmpp_nodeprep );
00068 #else
00069 return node;
00070 #endif
00071 }
00072
00073 std::string nameprep( const std::string& domain )
00074 {
00075 #ifdef HAVE_LIBIDN
00076 return prepare( domain, stringprep_nameprep );
00077 #else
00078 return domain;
00079 #endif
00080 }
00081
00082 std::string resourceprep( const std::string& resource )
00083 {
00084 #ifdef HAVE_LIBIDN
00085 return prepare( resource, stringprep_xmpp_resourceprep );
00086 #else
00087 return resource;
00088 #endif
00089 }
00090
00091 std::string idna( const std::string& domain )
00092 {
00093 #ifdef HAVE_LIBIDN
00094 if( domain.empty() || domain.length() > JID_PORTION_SIZE )
00095 return std::string();
00096
00097 std::string preppedString;
00098 char* prepped;
00099 int rc = idna_to_ascii_8z( domain.c_str(), &prepped, (Idna_flags)0 );
00100 if( rc == IDNA_SUCCESS )
00101 preppedString = prepped;
00102 if( rc != IDNA_MALLOC_ERROR )
00103 free( prepped );
00104 return preppedString;
00105 #else
00106 return domain;
00107 #endif
00108 }
00109 }
00110 }