00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifdef WIN32
00015 #include "../config.h.win"
00016 #else
00017 #include "config.h"
00018 #endif
00019
00020 #include "prep.h"
00021
00022 #include <string>
00023
00024 #ifdef HAVE_LIBIDN
00025 #include <stringprep.h>
00026 #include <idna.h>
00027 #endif
00028
00029 #define JID_PORTION_SIZE 1023
00030
00031 namespace gloox
00032 {
00033
00034 std::string Prep::nodeprep( const std::string& node )
00035 {
00036 if( node.empty() )
00037 return node;
00038
00039 if( node.length() > JID_PORTION_SIZE )
00040 return "";
00041
00042 #ifdef HAVE_LIBIDN
00043 char* p;
00044 char buf[JID_PORTION_SIZE + 1];
00045 memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00046 strncpy( buf, node.c_str(), node.length() );
00047 p = stringprep_locale_to_utf8( buf );
00048 if ( p )
00049 {
00050 strncpy( buf, p, JID_PORTION_SIZE + 1 );
00051 free( p );
00052 }
00053
00054 int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00055 (Stringprep_profile_flags)0, stringprep_xmpp_nodeprep );
00056 if ( rc != STRINGPREP_OK )
00057 {
00058 return "";
00059 }
00060 return buf;
00061 #else
00062 return node;
00063 #endif
00064 }
00065
00066 std::string Prep::nameprep( const std::string& domain )
00067 {
00068 if( domain.empty() )
00069 return domain;
00070
00071 if( domain.length() > JID_PORTION_SIZE )
00072 return "";
00073
00074 #ifdef HAVE_LIBIDN
00075 char* p;
00076 char buf[JID_PORTION_SIZE + 1];
00077 memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00078 strncpy( buf, domain.c_str(), domain.length() );
00079 p = stringprep_locale_to_utf8( buf );
00080 if ( p )
00081 {
00082 strncpy( buf, p, JID_PORTION_SIZE + 1 );
00083 free( p );
00084 }
00085
00086 int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00087 (Stringprep_profile_flags)0, stringprep_nameprep );
00088 if ( rc != STRINGPREP_OK )
00089 {
00090 return "";
00091 }
00092 return buf;
00093 #else
00094 return domain;
00095 #endif
00096 }
00097
00098 std::string Prep::resourceprep( const std::string& resource )
00099 {
00100 if( resource.empty() )
00101 return resource;
00102
00103 if( resource.length() > JID_PORTION_SIZE )
00104 return "";
00105
00106 #ifdef HAVE_LIBIDN
00107 char* p;
00108 char buf[JID_PORTION_SIZE + 1];
00109 memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00110 strncpy( buf, resource.c_str(), resource.length() );
00111 p = stringprep_locale_to_utf8( buf );
00112 if ( p )
00113 {
00114 strncpy( buf, p, JID_PORTION_SIZE + 1 );
00115 free( p );
00116 }
00117
00118 int rc = stringprep( (char*)&buf, JID_PORTION_SIZE,
00119 (Stringprep_profile_flags)0, stringprep_xmpp_resourceprep );
00120 if ( rc != STRINGPREP_OK )
00121 {
00122 return "";
00123 }
00124 return buf;
00125 #else
00126 return resource;
00127 #endif
00128 }
00129
00130 std::string Prep::idna( const std::string& domain )
00131 {
00132 if( domain.empty() )
00133 return domain;
00134
00135 if( domain.length() > JID_PORTION_SIZE )
00136 return "";
00137
00138 #ifdef HAVE_LIBIDN
00139 char* p;
00140 char buf[JID_PORTION_SIZE + 1];
00141 memset( &buf, '\0', JID_PORTION_SIZE + 1 );
00142 strncpy( buf, domain.c_str(), domain.length() );
00143 p = stringprep_locale_to_utf8( buf );
00144 if ( p )
00145 {
00146 strncpy( buf, p, JID_PORTION_SIZE + 1 );
00147 free( p );
00148 }
00149
00150 int rc = idna_to_ascii_8z( (char*)&buf, &p, (Idna_flags)0 );
00151 if ( rc != IDNA_SUCCESS )
00152 {
00153 return "";
00154 }
00155 return p;
00156 #else
00157 return domain;
00158 #endif
00159 }
00160
00161 }